2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.widget.focus;
19 import android.widget.focus.RequestFocus;
20 import com.android.frameworks.coretests.R;
22 import android.os.Handler;
23 import android.test.ActivityInstrumentationTestCase;
24 import android.test.suitebuilder.annotation.LargeTest;
25 import android.test.suitebuilder.annotation.MediumTest;
26 import android.widget.Button;
27 import android.util.AndroidRuntimeException;
30 * {@link RequestFocusTest} is set up to exercise cases where the views that
31 * have focus become invisible or GONE.
33 public class RequestFocusTest extends ActivityInstrumentationTestCase<RequestFocus> {
35 private Button mTopLeftButton;
36 private Button mBottomLeftButton;
37 private Button mTopRightButton;
38 private Button mBottomRightButton;
39 private Handler mHandler;
41 public RequestFocusTest() {
42 super("com.android.frameworks.coretests", RequestFocus.class);
46 public void setUp() throws Exception {
49 final RequestFocus a = getActivity();
50 mHandler = a.getHandler();
51 mTopLeftButton = (Button) a.findViewById(R.id.topLeftButton);
52 mBottomLeftButton = (Button) a.findViewById(R.id.bottomLeftButton);
53 mTopRightButton = (Button) a.findViewById(R.id.topRightButton);
54 mBottomRightButton = (Button) a.findViewById(R.id.bottomRightButton);
57 // Test that setUp did what we expect it to do. These asserts
58 // can't go in SetUp, or the test will hang.
60 public void testSetUpConditions() throws Exception {
61 assertNotNull(mHandler);
62 assertNotNull(mTopLeftButton);
63 assertNotNull(mTopRightButton);
64 assertNotNull(mBottomLeftButton);
65 assertNotNull(mBottomRightButton);
66 assertTrue("requestFocus() should work from onCreate.", mBottomRightButton.hasFocus());
69 // Test that a posted requestFocus works.
71 public void testPostedRequestFocus() throws Exception {
72 mHandler.post(new Runnable() { public void run() {
73 mBottomLeftButton.requestFocus();
78 } catch (InterruptedException e) {
82 assertTrue("Focus should move to bottom left", mBottomLeftButton.hasFocus());
85 // Test that a requestFocus from the wrong thread fails.
87 public void testWrongThreadRequestFocusFails() throws Exception {
89 mTopRightButton.requestFocus();
90 fail("requestFocus from wrong thread should raise exception.");
91 } catch (AndroidRuntimeException e) {
92 // Expected. The actual exception is not public, so we can't catch it.
93 assertEquals("android.view.ViewRoot$CalledFromWrongThreadException",
94 e.getClass().getName());