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 17 package android.widget.touchmode; 18 19 import static android.util.TouchModeFlexibleAsserts.assertInTouchModeAfterClick; 20 import static android.util.TouchModeFlexibleAsserts.assertInTouchModeAfterTap; 21 22 import android.test.ActivityInstrumentationTestCase; 23 import android.test.suitebuilder.annotation.LargeTest; 24 import android.test.suitebuilder.annotation.MediumTest; 25 import android.widget.Button; 26 import android.widget.EditText; 27 import android.widget.layout.linear.LLEditTextThenButton; 28 29 /** 30 * Some views, like edit texts, can keep and gain focus even when in touch mode. 31 */ 32 public class TouchModeFocusableTest extends ActivityInstrumentationTestCase<LLEditTextThenButton> { 33 private EditText mEditText; 34 private Button mButton; 35 36 37 public TouchModeFocusableTest() { 38 super("com.android.frameworks.coretests", LLEditTextThenButton.class); 39 } 40 41 @Override 42 protected void setUp() throws Exception { 43 super.setUp(); 44 45 mEditText = getActivity().getEditText(); 46 mButton = getActivity().getButton(); 47 } 48 49 @MediumTest 50 public void testPreconditions() { 51 assertFalse("should not be in touch mode to start off", mButton.isInTouchMode()); 52 assertTrue("edit text should have focus", mEditText.isFocused()); 53 assertTrue("edit text should be focusable in touch mode", mEditText.isFocusableInTouchMode()); 54 } 55 56 @MediumTest 57 public void testClickButtonEditTextKeepsFocus() { 58 assertInTouchModeAfterTap(this, mButton); 59 assertTrue("should be in touch mode", mButton.isInTouchMode()); 60 assertTrue("edit text should still have focus", mEditText.isFocused()); 61 } 62 63 @LargeTest 64 public void testClickEditTextGivesItFocus() { 65 // go down to button 66 getActivity().runOnUiThread(() -> mButton.requestFocus()); 67 getInstrumentation().waitForIdleSync(); 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 getActivity().runOnUiThread(() -> mButton.requestFocus()); 81 getInstrumentation().waitForIdleSync(); 82 83 assertTrue("button should have focus", 84 mButton.isFocused()); 85 86 assertInTouchModeAfterClick(this, mButton); 87 assertTrue("should be in touch mode", mButton.isInTouchMode()); 88 assertNull("nothing should have focus", getActivity().getCurrentFocus()); 89 assertFalse("layout should not have focus", 90 getActivity().getLayout().hasFocus()); 91 } 92 } 93