• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.example.android.apis.R;
20 
21 import android.content.Context;
22 import android.test.AndroidTestCase;
23 import android.test.suitebuilder.annotation.SmallTest;
24 import android.view.FocusFinder;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.Button;
29 
30 /**
31  * This exercises the same logic as {@link Focus2ActivityTest} but in a lighter
32  * weight manner; it doesn't need to launch the activity, and it can test the
33  * focus behavior by calling {@link FocusFinder} methods directly.
34  *
35  * {@link Focus2ActivityTest} is still useful to verify that, at an end to end
36  * level, key events actually translate to focus transitioning in the way we expect.
37  * A good complementary way to use both types of tests might be to have more exhaustive
38  * coverage in the lighter weight test case, and a few end to end scenarios in the
39  * functional {@link android.test.ActivityInstrumentationTestCase}.  This would provide reasonable
40  * assurance that the end to end system is working, while avoiding the overhead of
41  * having every corner case exercised in the slower, heavier weight way.
42  *
43  * Even as a lighter weight test, this test still needs access to a {@link Context}
44  * to inflate the file, which is why it extends {@link AndroidTestCase}.
45  *
46  * If you ever need a context to do your work in tests, you can extend
47  * {@link AndroidTestCase}, and when run via an {@link android.test.InstrumentationTestRunner},
48  * the context will be injected for you.
49  *
50  * See {@link com.example.android.apis.app.ForwardingTest} for an example of an Activity unit test.
51  *
52  * See {@link com.example.android.apis.AllTests} for documentation on running
53  * all tests and individual tests in this application.
54  */
55 public class Focus2AndroidTest extends AndroidTestCase {
56 
57     private FocusFinder mFocusFinder;
58 
59     private ViewGroup mRoot;
60 
61     private Button mLeftButton;
62     private Button mCenterButton;
63     private Button mRightButton;
64 
65     @Override
setUp()66     protected void setUp() throws Exception {
67         super.setUp();
68 
69         mFocusFinder = FocusFinder.getInstance();
70 
71         // inflate the layout
72         final Context context = getContext();
73         final LayoutInflater inflater = LayoutInflater.from(context);
74         mRoot = (ViewGroup) inflater.inflate(R.layout.focus_2, null);
75 
76         // manually measure it, and lay it out
77         mRoot.measure(500, 500);
78         mRoot.layout(0, 0, 500, 500);
79 
80         mLeftButton = (Button) mRoot.findViewById(R.id.leftButton);
81         mCenterButton = (Button) mRoot.findViewById(R.id.centerButton);
82         mRightButton = (Button) mRoot.findViewById(R.id.rightButton);
83     }
84 
85     /**
86      * The name 'test preconditions' is a convention to signal that if this
87      * test doesn't pass, the test case was not set up properly and it might
88      * explain any and all failures in other tests.  This is not guaranteed
89      * to run before other tests, as junit uses reflection to find the tests.
90      */
91     @SmallTest
testPreconditions()92     public void testPreconditions() {
93         assertNotNull(mLeftButton);
94         assertTrue("center button should be right of left button",
95                 mLeftButton.getRight() < mCenterButton.getLeft());
96         assertTrue("right button should be right of center button",
97                 mCenterButton.getRight() < mRightButton.getLeft());
98     }
99 
100     @SmallTest
testGoingRightFromLeftButtonJumpsOverCenterToRight()101     public void testGoingRightFromLeftButtonJumpsOverCenterToRight() {
102         assertEquals("right should be next focus from left",
103                 mRightButton,
104                 mFocusFinder.findNextFocus(mRoot, mLeftButton, View.FOCUS_RIGHT));
105     }
106 
107     @SmallTest
testGoingLeftFromRightButtonGoesToCenter()108     public void testGoingLeftFromRightButtonGoesToCenter() {
109         assertEquals("center should be next focus from right",
110                 mCenterButton,
111                 mFocusFinder.findNextFocus(mRoot, mRightButton, View.FOCUS_LEFT));
112     }
113 }
114