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.LLOfButtons1;
20import static android.util.TouchModeFlexibleAsserts.assertInTouchModeAfterClick;
21import static android.util.TouchModeFlexibleAsserts.assertInTouchModeAfterTap;
22import static android.util.TouchModeFlexibleAsserts.assertNotInTouchModeAfterKey;
23
24import android.test.ActivityInstrumentationTestCase;
25import android.test.suitebuilder.annotation.MediumTest;
26import android.view.KeyEvent;
27import android.widget.Button;
28
29/**
30 * Make sure focus isn't kept by buttons when entering touch mode.
31 *
32 * When in touch mode and hitting the d-pad, we should leave touch mode and the
33 * top most focusable gets focus.
34 */
35public class TouchModeFocusChangeTest extends ActivityInstrumentationTestCase<LLOfButtons1> {
36    private LLOfButtons1 mActivity;
37    private Button mFirstButton;
38
39    public TouchModeFocusChangeTest() {
40        super("com.android.frameworks.coretests", LLOfButtons1.class);
41    }
42
43
44    @Override
45    protected void setUp() throws Exception {
46        super.setUp();
47
48        mActivity = getActivity();
49        mFirstButton = mActivity.getFirstButton();
50    }
51
52    @MediumTest
53    public void testPreconditions() {
54        assertFalse("we should not be in touch mode", mActivity.isInTouchMode());
55        assertTrue("top button should have focus", mFirstButton.isFocused());
56    }
57
58    @MediumTest
59    public void testTouchButtonNotTakeFocus() {
60        assertInTouchModeAfterTap(this, mFirstButton);
61
62        assertTrue("should be in touch mode", mActivity.isInTouchMode());
63        assertFalse("button.isFocused",
64                mFirstButton.isFocused());
65        assertFalse("button.hasFocus",
66                mFirstButton.hasFocus());
67        assertNull("activity shouldn't have focus", mActivity.getCurrentFocus());
68        assertFalse("linear layout should not have focus",
69                mActivity.getLayout().hasFocus());
70
71        assertTrue("button's onClickListener should have fired",
72                mActivity.buttonClickListenerFired());
73    }
74
75    // TODO: reenable when more reliable
76    public void DISABLE_testLeaveTouchModeWithDpadEvent() {
77        assertInTouchModeAfterClick(this, mFirstButton);
78
79        assertTrue("should be in touch mode", mActivity.isInTouchMode());
80        assertFalse("button should not have focus when touched",
81                mFirstButton.isFocused());
82
83        assertNotInTouchModeAfterKey(this, KeyEvent.KEYCODE_DPAD_RIGHT, mFirstButton);
84        assertFalse("should be out of touch mode", mActivity.isInTouchMode());
85        assertTrue("first button (the top most focusable) should have gained focus",
86                mFirstButton.isFocused());
87    }
88
89
90}
91