1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.apis.view;
18 
19 import android.test.ActivityInstrumentationTestCase2;
20 import android.view.KeyEvent;
21 import android.widget.Button;
22 
23 import androidx.test.filters.MediumTest;
24 
25 import com.example.android.apis.R;
26 
27 /**
28  * An example of an {@link ActivityInstrumentationTestCase} of a specific activity {@link Focus2}.
29  * By virtue of extending {@link ActivityInstrumentationTestCase}, the target activity is automatically
30  * launched and finished before and after each test.  This also extends
31  * {@link android.test.InstrumentationTestCase}, which provides
32  * access to methods for sending events to the target activity, such as key and
33  * touch events.  See {@link #sendKeys}.
34  *
35  * In general, {@link android.test.InstrumentationTestCase}s and {@link ActivityInstrumentationTestCase}s
36  * are heavier weight functional tests available for end to end testing of your
37  * user interface.  When run via a {@link android.test.InstrumentationTestRunner},
38  * the necessary {@link android.app.Instrumentation} will be injected for you to
39  * user via {@link #getInstrumentation} in your tests.
40  *
41  * See {@link com.example.android.apis.app.ForwardingTest} for an example of an Activity unit test.
42  *
43  * See {@link com.example.android.apis.AllTests} for documentation on running
44  * all tests and individual tests in this application.
45  */
46 public class Focus2ActivityTest extends ActivityInstrumentationTestCase2<Focus2> {
47 
48     private Button mLeftButton;
49     private Button mCenterButton;
50     private Button mRightButton;
51 
52     /**
53      * Creates an {@link ActivityInstrumentationTestCase2} that tests the {@link Focus2} activity.
54      */
Focus2ActivityTest()55     public Focus2ActivityTest() {
56         super(Focus2.class);
57     }
58 
59     @Override
setUp()60     protected void setUp() throws Exception {
61         super.setUp();
62         final Focus2 a = getActivity();
63         // ensure a valid handle to the activity has been returned
64         assertNotNull(a);
65         mLeftButton = (Button) a.findViewById(R.id.leftButton);
66         mCenterButton = (Button) a.findViewById(R.id.centerButton);
67         mRightButton = (Button) a.findViewById(R.id.rightButton);
68     }
69 
70     /**
71      * The name 'test preconditions' is a convention to signal that if this
72      * test doesn't pass, the test case was not set up properly and it might
73      * explain any and all failures in other tests.  This is not guaranteed
74      * to run before other tests, as junit uses reflection to find the tests.
75      */
76     @MediumTest
testPreconditions()77     public void testPreconditions() {
78         assertTrue("center button should be right of left button",
79                 mLeftButton.getRight() < mCenterButton.getLeft());
80         assertTrue("right button should be right of center button",
81                 mCenterButton.getRight() < mRightButton.getLeft());
82         assertTrue("left button should be focused", mLeftButton.isFocused());
83     }
84 
85     @MediumTest
testGoingRightFromLeftButtonJumpsOverCenterToRight()86     public void testGoingRightFromLeftButtonJumpsOverCenterToRight() {
87         sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
88         assertTrue("right button should be focused", mRightButton.isFocused());
89     }
90 
91     @MediumTest
testGoingLeftFromRightButtonGoesToCenter()92     public void testGoingLeftFromRightButtonGoesToCenter()  {
93         // Give right button focus by having it request focus.  We post it
94         // to the UI thread because we are not running on the same thread, and
95         // any direct api calls that change state must be made from the UI thread.
96         // This is in contrast to instrumentation calls that send events that are
97         // processed through the framework and eventually find their way to
98         // affecting the ui thread.
99         getActivity().runOnUiThread(new Runnable() {
100             public void run() {
101                 mRightButton.requestFocus();
102             }
103         });
104         // wait for the request to go through
105         getInstrumentation().waitForIdleSync();
106 
107         assertTrue(mRightButton.isFocused());
108 
109         sendKeys(KeyEvent.KEYCODE_DPAD_LEFT);
110         assertTrue("center button should be focused", mCenterButton.isFocused());
111     }
112 }
113