Home | History | Annotate | Download | only in focus
      1 /*
      2  * Copyright (C) 2008 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.focus;
     18 
     19 import android.widget.focus.DescendantFocusability;
     20 
     21 import android.test.ActivityInstrumentationTestCase;
     22 import android.test.suitebuilder.annotation.LargeTest;
     23 import android.test.suitebuilder.annotation.MediumTest;
     24 import android.test.UiThreadTest;
     25 import android.test.TouchUtils;
     26 import android.view.ViewGroup;
     27 
     28 public class DescendantFocusabilityTest extends ActivityInstrumentationTestCase<DescendantFocusability> {
     29 
     30     private DescendantFocusability a;
     31 
     32     public DescendantFocusabilityTest() {
     33         super("com.android.frameworks.coretests", DescendantFocusability.class);
     34     }
     35 
     36     @Override
     37     protected void setUp() throws Exception {
     38         super.setUp();
     39         a = getActivity();
     40 
     41     }
     42 
     43     @MediumTest
     44     public void testPreconditions() {
     45         assertEquals(ViewGroup.FOCUS_BEFORE_DESCENDANTS,
     46             a.beforeDescendants.getDescendantFocusability());
     47         assertEquals(ViewGroup.FOCUS_AFTER_DESCENDANTS,
     48             a.afterDescendants.getDescendantFocusability());
     49         assertEquals(ViewGroup.FOCUS_BLOCK_DESCENDANTS,
     50             a.blocksDescendants.getDescendantFocusability());
     51 
     52         assertTrue(a.beforeDescendantsChild.isFocusable());
     53         assertTrue(a.afterDescendantsChild.isFocusable());
     54         assertTrue(a.blocksDescendantsChild.isFocusable());
     55     }
     56 
     57     @UiThreadTest
     58     @MediumTest
     59     public void testBeforeDescendants() {
     60         a.beforeDescendants.setFocusable(true);
     61 
     62         assertTrue(a.beforeDescendants.requestFocus());
     63         assertTrue(a.beforeDescendants.isFocused());
     64 
     65         a.beforeDescendants.setFocusable(false);
     66         a.beforeDescendants.requestFocus();
     67         assertTrue(a.beforeDescendantsChild.isFocused());
     68     }
     69 
     70     @UiThreadTest
     71     @MediumTest
     72     public void testAfterDescendants() {
     73         a.afterDescendants.setFocusable(true);
     74 
     75         assertTrue(a.afterDescendants.requestFocus());
     76         assertTrue(a.afterDescendantsChild.isFocused());
     77 
     78         a.afterDescendants.setFocusable(false);
     79         assertTrue(a.afterDescendants.requestFocus());
     80         assertTrue(a.afterDescendantsChild.isFocused());
     81     }
     82 
     83     @UiThreadTest
     84     @MediumTest
     85     public void testBlocksDescendants() {
     86         a.blocksDescendants.setFocusable(true);
     87         assertTrue(a.blocksDescendants.requestFocus());
     88         assertTrue(a.blocksDescendants.isFocused());
     89         assertFalse(a.blocksDescendantsChild.isFocused());
     90 
     91         a.blocksDescendants.setFocusable(false);
     92         assertFalse(a.blocksDescendants.requestFocus());
     93         assertFalse(a.blocksDescendants.isFocused());
     94         assertFalse(a.blocksDescendantsChild.isFocused());
     95     }
     96 
     97     @UiThreadTest
     98     @MediumTest
     99     public void testChildOfDescendantBlockerRequestFocusFails() {
    100         assertFalse(a.blocksDescendantsChild.requestFocus());
    101     }
    102 
    103     @LargeTest
    104     public void testBeforeDescendantsEnterTouchMode() {
    105         a.runOnUiThread(new Runnable() {
    106             public void run() {
    107                 a.beforeDescendants.setFocusableInTouchMode(true);
    108                 a.beforeDescendantsChild.requestFocus();
    109             }
    110         });
    111         getInstrumentation().waitForIdleSync();
    112         assertTrue(a.beforeDescendantsChild.isFocused());
    113         assertFalse(a.beforeDescendantsChild.isInTouchMode());
    114 
    115         TouchUtils.clickView(this, a.beforeDescendantsChild);
    116         assertTrue(a.beforeDescendantsChild.isInTouchMode());
    117         assertFalse(a.beforeDescendants.isFocused());
    118     }
    119 
    120     @LargeTest
    121     public void testAfterDescendantsEnterTouchMode() {
    122         a.runOnUiThread(new Runnable() {
    123             public void run() {
    124                 a.afterDescendants.setFocusableInTouchMode(true);
    125                 a.afterDescendantsChild.requestFocus();
    126             }
    127         });
    128         getInstrumentation().waitForIdleSync();
    129         assertTrue(a.afterDescendantsChild.isFocused());
    130         assertFalse(a.afterDescendantsChild.isInTouchMode());
    131 
    132         TouchUtils.clickView(this, a.afterDescendantsChild);
    133         assertTrue(a.afterDescendantsChild.isInTouchMode());
    134         assertTrue(a.afterDescendants.isFocused());
    135     }
    136 
    137 
    138 }
    139