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.FocusAfterRemoval;
20 import com.android.frameworks.coretests.R;
22 import android.test.ActivityInstrumentationTestCase;
23 import android.test.suitebuilder.annotation.MediumTest;
24 import android.widget.Button;
25 import android.widget.LinearLayout;
26 import android.view.KeyEvent;
27 import android.view.View;
30 * {@link FocusAfterRemoval} is set up to exercise cases where the views that
31 * have focus become invisible or GONE.
33 public class FocusAfterRemovalTest extends ActivityInstrumentationTestCase<FocusAfterRemoval> {
35 private LinearLayout mLeftLayout;
36 private Button mTopLeftButton;
37 private Button mBottomLeftButton;
38 private Button mTopRightButton;
39 private Button mBottomRightButton;
41 public FocusAfterRemovalTest() {
42 super("com.android.frameworks.coretests", FocusAfterRemoval.class);
46 public void setUp() throws Exception {
49 final FocusAfterRemoval a = getActivity();
50 mLeftLayout = (LinearLayout) a.findViewById(R.id.leftLayout);
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(mLeftLayout);
62 assertNotNull(mTopLeftButton);
63 assertNotNull(mTopRightButton);
64 assertNotNull(mBottomLeftButton);
65 assertNotNull(mBottomRightButton);
67 assertTrue(mTopLeftButton.hasFocus());
70 // if a parent layout becomes GONE when one of its children has focus,
71 // make sure the focus moves to something visible (bug 827087)
73 public void testFocusLeavesWhenParentLayoutIsGone() throws Exception {
75 // clicking on this button makes its parent linear layout GONE
76 sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
77 assertEquals(View.GONE, mLeftLayout.getVisibility());
79 assertTrue("focus should jump to visible button",
80 mTopRightButton.hasFocus());
85 public void testFocusLeavesWhenParentLayoutInvisible() throws Exception {
87 // move down to bottom left button
88 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
89 assertTrue(mBottomLeftButton.hasFocus());
91 // clicking on this button makes its parent linear layout INVISIBLE
92 sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
93 assertEquals(View.INVISIBLE,
94 getActivity().findViewById(R.id.leftLayout).getVisibility());
96 assertTrue("focus should jump to visible button",
97 mTopRightButton.hasFocus());
101 public void testFocusLeavesWhenFocusedViewBecomesGone() throws Exception {
104 sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
105 assertTrue(mTopRightButton.hasFocus());
107 // click making it GONE
108 sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
109 assertEquals(View.GONE, mTopRightButton.getVisibility());
111 assertTrue("focus should jump to visible button",
112 mTopLeftButton.hasFocus());
116 public void testFocusLeavesWhenFocusedViewBecomesInvisible() throws Exception {
118 // move to bottom right
119 sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
120 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
121 assertTrue(mBottomRightButton.hasFocus());
123 // click making it INVISIBLE
124 sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
125 assertEquals(View.INVISIBLE, mBottomRightButton.getVisibility());
127 assertTrue("focus should jump to visible button",
128 mTopLeftButton.hasFocus());