1616
1717package android .widget .focus ;
1818
19+ import android .widget .focus .RequestFocus ;
20+ import com .android .frameworks .coretests .R ;
21+
1922import android .os .Handler ;
20- import android .test .ActivityInstrumentationTestCase2 ;
21- import android .test .UiThreadTest ;
23+ import android .test .ActivityInstrumentationTestCase ;
2224import android .test .suitebuilder .annotation .LargeTest ;
2325import android .test .suitebuilder .annotation .MediumTest ;
24- import android .util .AndroidRuntimeException ;
25- import android .view .View ;
26- import android .view .View .OnFocusChangeListener ;
27- import android .view .ViewTreeObserver .OnGlobalFocusChangeListener ;
2826import android .widget .Button ;
29-
30- import com .android .frameworks .coretests .R ;
31-
32- import java .util .ArrayList ;
33- import java .util .List ;
27+ import android .util .AndroidRuntimeException ;
3428
3529/**
3630 * {@link RequestFocusTest} is set up to exercise cases where the views that
3731 * have focus become invisible or GONE.
3832 */
39- public class RequestFocusTest extends ActivityInstrumentationTestCase2 <RequestFocus > {
33+ public class RequestFocusTest extends ActivityInstrumentationTestCase <RequestFocus > {
4034
4135 private Button mTopLeftButton ;
4236 private Button mBottomLeftButton ;
@@ -45,7 +39,7 @@ public class RequestFocusTest extends ActivityInstrumentationTestCase2<RequestFo
4539 private Handler mHandler ;
4640
4741 public RequestFocusTest () {
48- super (RequestFocus .class );
42+ super ("com.android.frameworks.coretests" , RequestFocus .class );
4943 }
5044
5145 @ Override
@@ -100,145 +94,4 @@ public void testWrongThreadRequestFocusFails() throws Exception {
10094 e .getClass ().getName ());
10195 }
10296 }
103-
104- /**
105- * This tests checks the case in which the first focusable View clears focus.
106- * In such a case the framework tries to give the focus to another View starting
107- * from the top. Hence, the framework will try to give focus to the view that
108- * wants to clear its focus. From a client perspective, the view does not loose
109- * focus after the call, therefore no callback for focus change should be invoked.
110- *
111- * @throws Exception If an error occurs.
112- */
113- @ UiThreadTest
114- public void testOnFocusChangeNotCalledIfFocusDoesNotMove () throws Exception {
115- // Get the first focusable.
116- Button button = mTopLeftButton ;
117-
118- // Make sure that the button is the first focusable and focus it.
119- button .getRootView ().requestFocus (View .FOCUS_DOWN );
120- assertTrue (button .hasFocus ());
121-
122- // Attach on focus change listener that should not be called.
123- button .setOnFocusChangeListener (new OnFocusChangeListener () {
124- @ Override
125- public void onFocusChange (View v , boolean hasFocus ) {
126- throw new IllegalStateException ("Unexpeced call to"
127- + "OnFocusChangeListener#onFocusChange" );
128- }
129- });
130-
131- // Attach on global focus change listener that should not be called.
132- button .getViewTreeObserver ().addOnGlobalFocusChangeListener (
133- new OnGlobalFocusChangeListener () {
134- @ Override
135- public void onGlobalFocusChanged (View oldFocus , View newFocus ) {
136- throw new IllegalStateException ("Unexpeced call to"
137- + "OnFocusChangeListener#onFocusChange" );
138- }
139- });
140-
141- // Try to clear focus.
142- button .clearFocus ();
143- }
144-
145- /**
146- * This tests check whether the on focus change callbacks are invoked in
147- * the proper order when a View loses focus and the framework gives it to
148- * the fist focusable one.
149- *
150- * @throws Exception
151- */
152- @ UiThreadTest
153- public void testOnFocusChangeCallbackOrder () throws Exception {
154- // Get the first focusable.
155- Button clearingFocusButton = mTopRightButton ;
156- Button gainingFocusButton = mTopLeftButton ;
157-
158- // Make sure that the clearing focus is not the first focusable.
159- View focusCandidate = clearingFocusButton .getRootView ().getParent ().focusSearch (null ,
160- View .FOCUS_FORWARD );
161- assertNotSame ("The clearing focus button is not the first focusable." ,
162- clearingFocusButton , focusCandidate );
163- assertSame ("The gaining focus button is the first focusable." ,
164- gainingFocusButton , focusCandidate );
165-
166- // Focus the clearing focus button.
167- clearingFocusButton .requestFocus ();
168- assertTrue (clearingFocusButton .hasFocus ());
169-
170- // Register the invocation order checker.
171- CallbackOrderChecker checker = new CallbackOrderChecker (clearingFocusButton ,
172- gainingFocusButton );
173- clearingFocusButton .setOnFocusChangeListener (checker );
174- gainingFocusButton .setOnFocusChangeListener (checker );
175- clearingFocusButton .getViewTreeObserver ().addOnGlobalFocusChangeListener (checker );
176-
177- // Try to clear focus.
178- clearingFocusButton .clearFocus ();
179-
180- // Check that no callback was invoked since focus did not move.
181- checker .verify ();
182- }
183-
184- /**
185- * This class check whether the focus change callback are invoked in order.
186- */
187- private class CallbackOrderChecker implements OnFocusChangeListener ,
188- OnGlobalFocusChangeListener {
189-
190- private class CallbackInvocation {
191- final String mMethodName ;
192- final Object [] mArguments ;
193-
194- CallbackInvocation (String methodName , Object [] arguments ) {
195- mMethodName = methodName ;
196- mArguments = arguments ;
197- }
198- }
199-
200- private final View mClearingFocusView ;
201- private final View mGainingFocusView ;
202-
203- private final List <CallbackInvocation > mInvocations = new ArrayList <CallbackInvocation >();
204-
205- public CallbackOrderChecker (View clearingFocusView , View gainingFocusView ) {
206- mClearingFocusView = clearingFocusView ;
207- mGainingFocusView = gainingFocusView ;
208- }
209-
210- @ Override
211- public void onFocusChange (View view , boolean hasFocus ) {
212- CallbackInvocation invocation = new CallbackInvocation (
213- "OnFocusChangeListener#onFocusChange" , new Object [] {view , hasFocus });
214- mInvocations .add (invocation );
215- }
216-
217- @ Override
218- public void onGlobalFocusChanged (View oldFocus , View newFocus ) {
219- CallbackInvocation invocation = new CallbackInvocation (
220- "OnFocusChangeListener#onFocusChange" , new Object [] {oldFocus , newFocus });
221- mInvocations .add (invocation );
222- }
223-
224- public void verify () {
225- assertSame ("All focus change callback should be invoked." , 3 , mInvocations .size ());
226- assertInvioked ("Callback for View clearing focus explected." , 0 ,
227- "OnFocusChangeListener#onFocusChange" ,
228- new Object [] {mClearingFocusView , false });
229- assertInvioked ("Callback for View global focus change explected." , 1 ,
230- "OnFocusChangeListener#onFocusChange" , new Object [] {mClearingFocusView ,
231- mGainingFocusView });
232- assertInvioked ("Callback for View gaining focus explected." , 2 ,
233- "OnFocusChangeListener#onFocusChange" , new Object [] {mGainingFocusView , true });
234- }
235-
236- private void assertInvioked (String message , int order , String methodName ,
237- Object [] arguments ) {
238- CallbackInvocation invocation = mInvocations .get (order );
239- assertEquals (message , methodName , invocation .mMethodName );
240- assertEquals (message , arguments [0 ], invocation .mArguments [0 ]);
241- assertEquals (message , arguments [1 ], invocation .mArguments [1 ]);
242- }
243- }
24497}
0 commit comments