1/*
2 * Copyright (C) 2007 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
17package android.widget.focus;
18
19import android.widget.focus.FocusAfterRemoval;
20import com.android.frameworks.coretests.R;
21
22import android.test.ActivityInstrumentationTestCase;
23import android.test.suitebuilder.annotation.MediumTest;
24import android.widget.Button;
25import android.widget.LinearLayout;
26import android.view.KeyEvent;
27import android.view.View;
28
29/**
30 * {@link FocusAfterRemoval} is set up to exercise cases where the views that
31 * have focus become invisible or GONE.
32 */
33public class FocusAfterRemovalTest extends ActivityInstrumentationTestCase<FocusAfterRemoval> {
34
35    private LinearLayout mLeftLayout;
36    private Button mTopLeftButton;
37    private Button mBottomLeftButton;
38    private Button mTopRightButton;
39    private Button mBottomRightButton;
40
41    public FocusAfterRemovalTest() {
42        super("com.android.frameworks.coretests", FocusAfterRemoval.class);
43    }
44
45    @Override
46    public void setUp() throws Exception {
47        super.setUp();
48
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);
55    }
56
57    // Test that setUp did what we expect it to do.  These asserts
58    // can't go in SetUp, or the test will hang.
59    @MediumTest
60    public void testSetUpConditions() throws Exception {
61        assertNotNull(mLeftLayout);
62        assertNotNull(mTopLeftButton);
63        assertNotNull(mTopRightButton);
64        assertNotNull(mBottomLeftButton);
65        assertNotNull(mBottomRightButton);
66
67        assertTrue(mTopLeftButton.hasFocus());
68    }
69
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)
72    @MediumTest
73    public void testFocusLeavesWhenParentLayoutIsGone() throws Exception {
74
75        // clicking on this button makes its parent linear layout GONE
76        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
77        assertEquals(View.GONE, mLeftLayout.getVisibility());
78
79        assertTrue("focus should jump to visible button",
80                mTopRightButton.hasFocus());
81
82    }
83
84    @MediumTest
85    public void testFocusLeavesWhenParentLayoutInvisible() throws Exception {
86
87        // move down to bottom left button
88        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
89        assertTrue(mBottomLeftButton.hasFocus());
90
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());
95
96        assertTrue("focus should jump to visible button",
97                mTopRightButton.hasFocus());
98    }
99
100    @MediumTest
101    public void testFocusLeavesWhenFocusedViewBecomesGone() throws Exception {
102
103        // move to top right
104        sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
105        assertTrue(mTopRightButton.hasFocus());
106
107        // click making it GONE
108        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
109        assertEquals(View.GONE, mTopRightButton.getVisibility());
110
111        assertTrue("focus should jump to visible button",
112                mTopLeftButton.hasFocus());
113    }
114
115    @MediumTest
116    public void testFocusLeavesWhenFocusedViewBecomesInvisible() throws Exception {
117
118        // move to bottom right
119        sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
120        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
121        assertTrue(mBottomRightButton.hasFocus());
122
123        // click making it INVISIBLE
124        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
125        assertEquals(View.INVISIBLE, mBottomRightButton.getVisibility());
126
127        assertTrue("focus should jump to visible button",
128                mTopLeftButton.hasFocus());
129    }
130
131}
132