Home | History | Annotate | Download | only in focus
      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.focus;
     18 
     19 import android.widget.focus.VerticalFocusSearch;
     20 
     21 import android.support.test.filters.LargeTest;
     22 import android.test.ActivityInstrumentationTestCase;
     23 import android.test.suitebuilder.annotation.Suppress;
     24 import android.view.FocusFinder;
     25 import android.view.View;
     26 import android.view.ViewGroup;
     27 import android.widget.Button;
     28 import android.widget.LinearLayout;
     29 
     30 /**
     31  * Tests that focus searching works on a vertical linear layout of buttons of
     32  * various widths and horizontal placements.
     33  */
     34 // Suppress until bug http://b/issue?id=1416545 is fixed
     35 @LargeTest
     36 @Suppress
     37 public class VerticalFocusSearchTest extends ActivityInstrumentationTestCase<VerticalFocusSearch> {
     38 
     39     private LinearLayout mLayout;
     40 
     41     private Button mTopWide;
     42     private Button mMidSkinny1Left;
     43     private Button mMidSkinny2Right;
     44     private Button mBottomWide;
     45 
     46     private FocusSearchAlg mFocusFinder;
     47 
     48     // helps test old and new impls when figuring out why something that used
     49     // to work doesn't anymore (or verifying that new works as well as old).
     50     interface FocusSearchAlg {
     51         View findNextFocus(ViewGroup root, View focused, int direction);
     52     }
     53 
     54     // calls new impl
     55     static class NewFocusSearchAlg implements FocusSearchAlg {
     56 
     57         public View findNextFocus(ViewGroup root, View focused, int direction) {
     58             return FocusFinder.getInstance()
     59                     .findNextFocus(root, focused, direction);
     60         }
     61     }
     62 
     63     public VerticalFocusSearchTest() {
     64         super("com.android.frameworks.coretests", VerticalFocusSearch.class);
     65     }
     66 
     67 
     68     @Override
     69     protected void setUp() throws Exception {
     70         super.setUp();
     71 
     72         mLayout = getActivity().getLayout();
     73         mTopWide = getActivity().getTopWide();
     74         mMidSkinny1Left = getActivity().getMidSkinny1Left();
     75         mMidSkinny2Right = getActivity().getMidSkinny2Right();
     76         mBottomWide = getActivity().getBottomWide();
     77 
     78         mFocusFinder = new NewFocusSearchAlg();
     79     }
     80 
     81     public void testPreconditions() {
     82         assertNotNull(mLayout);
     83         assertNotNull(mTopWide);
     84         assertNotNull(mMidSkinny1Left);
     85         assertNotNull(mMidSkinny2Right);
     86         assertNotNull(mBottomWide);
     87     }
     88 
     89     public void testSearchFromTopButton() {
     90         assertNull("going up from mTopWide.",
     91                 mFocusFinder.findNextFocus(mLayout, mTopWide, View.FOCUS_UP));
     92 
     93         assertNull("going left from mTopWide.",
     94                 mFocusFinder.findNextFocus(mLayout, mTopWide, View.FOCUS_LEFT));
     95 
     96         assertNull("going right from mTopWide.",
     97                 mFocusFinder.findNextFocus(mLayout, mTopWide, View.FOCUS_RIGHT));
     98 
     99         assertEquals("going down from mTopWide.",
    100                 mMidSkinny1Left,
    101                 mFocusFinder
    102                 .findNextFocus(mLayout, mTopWide, View.FOCUS_DOWN));
    103     }
    104 
    105     public void testSearchFromMidLeft() {
    106         assertNull("going left should have no next focus",
    107                 mFocusFinder.findNextFocus(mLayout, mMidSkinny1Left, View.FOCUS_LEFT));
    108 
    109         assertEquals("going right from mMidSkinny1Left should go to mMidSkinny2Right",
    110                 mMidSkinny2Right,
    111                 mFocusFinder.findNextFocus(mLayout, mMidSkinny1Left, View.FOCUS_RIGHT));
    112 
    113         assertEquals("going up from mMidSkinny1Left should go to mTopWide",
    114                 mTopWide,
    115                 mFocusFinder.findNextFocus(mLayout, mMidSkinny1Left, View.FOCUS_UP));
    116 
    117         assertEquals("going down from mMidSkinny1Left should go to mMidSkinny2Right",
    118                 mMidSkinny2Right,
    119                 mFocusFinder.findNextFocus(mLayout, mMidSkinny1Left, View.FOCUS_DOWN));
    120     }
    121 
    122     public void testSearchFromMidRight() {
    123         assertEquals("going left from mMidSkinny2Right should go to mMidSkinny1Left",
    124                 mMidSkinny1Left,
    125                 mFocusFinder.findNextFocus(mLayout, mMidSkinny2Right, View.FOCUS_LEFT));
    126 
    127         assertNull("going right should have no next focus",
    128                 mFocusFinder.findNextFocus(mLayout, mMidSkinny2Right, View.FOCUS_RIGHT));
    129 
    130         assertEquals("going up from mMidSkinny2Right should go to mMidSkinny1Left",
    131                 mMidSkinny1Left,
    132                 mFocusFinder.findNextFocus(mLayout, mMidSkinny2Right, View.FOCUS_UP));
    133 
    134         assertEquals("going down from mMidSkinny2Right should go to mBottomWide",
    135                 mBottomWide,
    136                 mFocusFinder.findNextFocus(mLayout, mMidSkinny2Right, View.FOCUS_DOWN));
    137 
    138     }
    139 
    140     public void testSearchFromFromBottom() {
    141         assertNull("going down from bottom button should have no next focus.",
    142                 mFocusFinder.findNextFocus(mLayout, mBottomWide, View.FOCUS_DOWN));
    143 
    144         assertNull("going left from bottom button should have no next focus.",
    145                 mFocusFinder.findNextFocus(mLayout, mBottomWide, View.FOCUS_LEFT));
    146 
    147         assertNull("going right from bottom button should have no next focus.",
    148                 mFocusFinder.findNextFocus(mLayout, mBottomWide, View.FOCUS_RIGHT));
    149 
    150         assertEquals("going up from bottom button should go to mMidSkinny2Right.",
    151                 mMidSkinny2Right,
    152                 mFocusFinder.findNextFocus(mLayout, mBottomWide, View.FOCUS_UP));
    153     }
    154 }
    155