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.AdjacentVerticalRectLists;
     20 import android.util.InternalSelectionView;
     21 
     22 import android.test.ActivityInstrumentationTestCase;
     23 import android.test.suitebuilder.annotation.LargeTest;
     24 import android.test.suitebuilder.annotation.MediumTest;
     25 import android.view.KeyEvent;
     26 
     27 /**
     28  * {@link android.view.FocusFinder#findNextFocus(android.view.ViewGroup, android.view.View, int)}
     29  * and
     30  * {@link android.view.View#requestFocus(int, android.graphics.Rect)}
     31  * work together to give a newly focused item a hint about the most interesting
     32  * rectangle of the previously focused view.  The view taking focus can use this
     33  * to set an internal selection more appropriate using this rect.
     34  *
     35  * This tests that behavior using three adjacent {@link android.util.InternalSelectionView}
     36  * that report interesting rects when giving up focus, and use interesting rects
     37  * when taking focus to best select the internal row to show as selected.
     38  *
     39  */
     40 public class FocusChangeWithInterestingRectHintTest extends ActivityInstrumentationTestCase<AdjacentVerticalRectLists> {
     41 
     42     private InternalSelectionView mLeftColumn;
     43     private InternalSelectionView mMiddleColumn;
     44     private InternalSelectionView mRightColumn;
     45 
     46     public FocusChangeWithInterestingRectHintTest() {
     47         super("com.android.frameworks.coretests", AdjacentVerticalRectLists.class);
     48     }
     49 
     50     @Override
     51     public void setUp() throws Exception {
     52         super.setUp();
     53         mLeftColumn = getActivity().getLeftColumn();
     54         mMiddleColumn = getActivity().getMiddleColumn();
     55         mRightColumn = getActivity().getRightColumn();
     56     }
     57 
     58     @MediumTest
     59     public void testPreconditions() {
     60         assertNotNull(mLeftColumn);
     61         assertNotNull(mMiddleColumn);
     62         assertNotNull(mRightColumn);
     63         assertTrue(mLeftColumn.hasFocus());
     64         assertTrue("need at least 3 rows", mLeftColumn.getNumRows() > 2);
     65         assertEquals(mLeftColumn.getNumRows(), mMiddleColumn.getNumRows());
     66         assertEquals(mMiddleColumn.getNumRows(), mRightColumn.getNumRows());
     67     }
     68 
     69 
     70     @LargeTest
     71     public void testSnakeBackAndForth() {
     72         final int numRows = mLeftColumn.getNumRows();
     73         for (int row = 0; row < numRows; row++) {
     74 
     75             if ((row % 2) == 0) {
     76                 assertEquals("row " + row + ": should be at left column",
     77                         row, mLeftColumn.getSelectedRow());
     78 
     79                 sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
     80                 assertTrue("row " + row + ": should be at middle column",
     81                         mMiddleColumn.hasFocus());
     82                 assertEquals(row, mMiddleColumn.getSelectedRow());
     83 
     84                 sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
     85                 assertTrue("row " + row + ": should be at right column",
     86                         mRightColumn.hasFocus());
     87                 assertEquals(row, mRightColumn.getSelectedRow());
     88 
     89                 if (row < numRows - 1) {
     90                     sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
     91                     assertEquals(row + 1, mRightColumn.getSelectedRow());
     92                 }
     93             } else {
     94                 assertTrue("row " + row + ": should be at right column",
     95                         mRightColumn.hasFocus());
     96 
     97                 sendKeys(KeyEvent.KEYCODE_DPAD_LEFT);
     98                 assertTrue("row " + row + ": should be at middle column",
     99                         mMiddleColumn.hasFocus());
    100                 assertEquals(row, mMiddleColumn.getSelectedRow());
    101 
    102                 sendKeys(KeyEvent.KEYCODE_DPAD_LEFT);
    103                 assertEquals("row " + row + ": should be at left column",
    104                         row, mLeftColumn.getSelectedRow());
    105 
    106                 if (row < numRows - 1) {
    107                     sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
    108                     assertEquals(row + 1, mLeftColumn.getSelectedRow());
    109                 }
    110            }
    111         }
    112     }
    113 
    114 }
    115