TouchModeFocusableTest.java revision 1d3165f10b12165f02b7015ac1a817c5f60e6399
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.touchmode;
18
19import android.widget.layout.linear.LLEditTextThenButton;
20import static android.util.TouchModeFlexibleAsserts.assertInTouchModeAfterTap;
21import static android.util.TouchModeFlexibleAsserts.assertInTouchModeAfterClick;
22
23import android.test.ActivityInstrumentationTestCase;
24import android.test.suitebuilder.annotation.LargeTest;
25import android.test.suitebuilder.annotation.MediumTest;
26import android.view.KeyEvent;
27import android.widget.Button;
28import android.widget.EditText;
29
30/**
31 * Some views, like edit texts, can keep and gain focus even when in touch mode.
32 */
33public class TouchModeFocusableTest extends ActivityInstrumentationTestCase<LLEditTextThenButton> {
34    private EditText mEditText;
35    private Button mButton;
36
37
38    public TouchModeFocusableTest() {
39        super("com.android.frameworks.coretests", LLEditTextThenButton.class);
40    }
41
42    @Override
43    protected void setUp() throws Exception {
44        super.setUp();
45
46        mEditText = getActivity().getEditText();
47        mButton = getActivity().getButton();
48    }
49
50    @MediumTest
51    public void testPreconditions() {
52        assertFalse("should not be in touch mode to start off", mButton.isInTouchMode());
53        assertTrue("edit text should have focus", mEditText.isFocused());
54        assertTrue("edit text should be focusable in touch mode", mEditText.isFocusableInTouchMode());
55    }
56
57    @MediumTest
58    public void testClickButtonEditTextKeepsFocus() {
59        assertInTouchModeAfterTap(this, mButton);
60        assertTrue("should be in touch mode", mButton.isInTouchMode());
61        assertTrue("edit text should still have focus", mEditText.isFocused());
62    }
63
64    @LargeTest
65    public void testClickEditTextGivesItFocus() {
66        // go down to button
67        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
68        assertTrue("button should have focus", mButton.isFocused());
69
70        assertInTouchModeAfterClick(this, mEditText);
71        assertTrue("clicking edit text should have entered touch mode", mButton.isInTouchMode());
72        assertTrue("clicking edit text should have given it focus", mEditText.isFocused());
73    }
74
75
76    // entering touch mode takes focus away from the currently focused item if it
77    // isn't focusable in touch mode.
78    @LargeTest
79    public void testEnterTouchModeGivesFocusBackToFocusableInTouchMode() {
80        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
81
82        assertTrue("button should have focus",
83                mButton.isFocused());
84
85        assertInTouchModeAfterClick(this, mButton);
86        assertTrue("should be in touch mode", mButton.isInTouchMode());
87        assertNull("nothing should have focus", getActivity().getCurrentFocus());
88        assertFalse("layout should not have focus",
89                getActivity().getLayout().hasFocus());
90    }
91}
92