Home | History | Annotate | Download | only in touch
      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.gridview.touch;
     18 
     19 import android.content.Context;
     20 import android.test.ActivityInstrumentationTestCase;
     21 import android.test.suitebuilder.annotation.LargeTest;
     22 import android.test.suitebuilder.annotation.MediumTest;
     23 import android.test.TouchUtils;
     24 import android.view.Gravity;
     25 import android.view.View;
     26 import android.view.ViewConfiguration;
     27 import android.widget.GridView;
     28 
     29 import android.widget.gridview.GridVerticalSpacingStackFromBottom;
     30 
     31 public class GridTouchVerticalSpacingStackFromBottomTest extends ActivityInstrumentationTestCase<GridVerticalSpacingStackFromBottom> {
     32     private GridVerticalSpacingStackFromBottom mActivity;
     33     private GridView mGridView;
     34     private ViewConfiguration mViewConfig;
     35 
     36     public GridTouchVerticalSpacingStackFromBottomTest() {
     37         super("com.android.frameworks.coretests", GridVerticalSpacingStackFromBottom.class);
     38     }
     39 
     40     @Override
     41     protected void setUp() throws Exception {
     42         super.setUp();
     43 
     44         mActivity = getActivity();
     45         mGridView = getActivity().getGridView();
     46         final Context context = mActivity.getApplicationContext();
     47         mViewConfig = ViewConfiguration.get(context);
     48     }
     49 
     50     @MediumTest
     51     public void testPreconditions() {
     52         assertNotNull(mActivity);
     53         assertNotNull(mGridView);
     54 
     55         // Last item should be selected
     56         assertEquals(mGridView.getAdapter().getCount() - 1, mGridView.getSelectedItemPosition());
     57 
     58     }
     59 
     60     @MediumTest
     61     public void testNoScroll() {
     62         View firstChild = mGridView.getChildAt(0);
     63         View lastChild = mGridView.getChildAt(mGridView.getChildCount() - 1);
     64 
     65         int lastTop = lastChild.getTop();
     66 
     67         TouchUtils.dragViewBy(this, firstChild, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0,
     68                 ViewConfiguration.getTouchSlop());
     69 
     70         View newLastChild = mGridView.getChildAt(mGridView.getChildCount() - 1);
     71 
     72         assertEquals("View scrolled too early", lastTop, newLastChild.getTop());
     73         assertEquals("Wrong view in last position", mGridView.getAdapter().getCount() - 1,
     74                 newLastChild.getId());
     75     }
     76 
     77     // TODO: needs to be adjusted to pass on non-HVGA displays
     78     // @LargeTest
     79     public void testShortScroll() {
     80         View firstChild = mGridView.getChildAt(0);
     81         if (firstChild.getTop() < this.mGridView.getListPaddingTop()) {
     82             firstChild = mGridView.getChildAt(1);
     83         }
     84 
     85         View lastChild = mGridView.getChildAt(mGridView.getChildCount() - 1);
     86 
     87         int lastTop = lastChild.getTop();
     88 
     89         TouchUtils.dragViewBy(this, firstChild, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0,
     90                 mViewConfig.getScaledTouchSlop() + 1 + 10);
     91 
     92         View newLastChild = mGridView.getChildAt(mGridView.getChildCount() - 1);
     93 
     94         assertEquals("View scrolled to wrong position", lastTop, newLastChild.getTop() - 10);
     95         assertEquals("Wrong view in last position", mGridView.getAdapter().getCount() - 1,
     96                 newLastChild.getId());
     97     }
     98 
     99     // TODO: needs to be adjusted to pass on non-HVGA displays
    100     // @LargeTest
    101     public void testLongScroll() {
    102         View firstChild = mGridView.getChildAt(0);
    103         if (firstChild.getTop() < mGridView.getListPaddingTop()) {
    104             firstChild = mGridView.getChildAt(1);
    105         }
    106 
    107         int firstTop = firstChild.getTop();
    108 
    109         int distance = TouchUtils.dragViewBy(this, firstChild,
    110                 Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0,
    111                 (int) (mActivity.getWindowManager().getDefaultDisplay().getHeight() * 0.75f));
    112 
    113         assertEquals("View scrolled to wrong position", firstTop
    114                 + (distance - mViewConfig.getScaledTouchSlop() - 1), firstChild.getTop());
    115     }
    116 
    117     @LargeTest
    118     public void testManyScrolls() {
    119         int originalCount = mGridView.getChildCount();
    120 
    121         View firstChild;
    122         int firstId = Integer.MIN_VALUE;
    123         int firstTop = Integer.MIN_VALUE;
    124         int prevId;
    125         int prevTop;
    126         do {
    127             prevId = firstId;
    128             prevTop = firstTop;
    129             TouchUtils.dragQuarterScreenDown(this);
    130             assertTrue(String.format("Too many children created: %d expected no more than %d",
    131                     mGridView.getChildCount(), originalCount + 4),
    132                     mGridView.getChildCount() <= originalCount + 4);
    133             firstChild = mGridView.getChildAt(0);
    134             firstId = firstChild.getId();
    135             firstTop = firstChild.getTop();
    136         } while ((prevId != firstId) || (prevTop != firstTop));
    137 
    138 
    139         firstChild = mGridView.getChildAt(0);
    140         assertEquals("View scrolled to wrong position", 0, firstChild.getId());
    141 
    142         firstId = Integer.MIN_VALUE;
    143         firstTop = Integer.MIN_VALUE;
    144         do {
    145             prevId = firstId;
    146             prevTop = firstTop;
    147             TouchUtils.dragQuarterScreenUp(this);
    148             assertTrue(String.format("Too many children created: %d expected no more than %d",
    149                     mGridView.getChildCount(), originalCount + 4),
    150                     mGridView.getChildCount() <= originalCount + 4);
    151             firstChild = mGridView.getChildAt(0);
    152             firstId = firstChild.getId();
    153             firstTop = firstChild.getTop();
    154         } while ((prevId != firstId) || (prevTop != firstTop));
    155 
    156         View lastChild = mGridView.getChildAt(mGridView.getChildCount() - 1);
    157         assertEquals("Grid is not scrolled to the bottom", mGridView.getAdapter().getCount() - 1,
    158                 lastChild.getId());
    159     }
    160 }
    161