Home | History | Annotate | Download | only in arrowscroll
      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.scroll.arrowscroll;
     18 
     19 import android.widget.scroll.ShortButtons;
     20 
     21 import android.graphics.Rect;
     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 import android.widget.Button;
     27 import android.widget.ScrollView;
     28 
     29 public class ShortButtonsTest extends ActivityInstrumentationTestCase<ShortButtons> {
     30 
     31     private ScrollView mScrollView;
     32 
     33     public ShortButtonsTest() {
     34         super("com.android.frameworks.coretests", ShortButtons.class);
     35     }
     36 
     37     @Override
     38     protected void setUp() throws Exception {
     39         super.setUp();
     40 
     41         mScrollView = getActivity().getScrollView();
     42     }
     43 
     44     @MediumTest
     45     public void testPreconditions() {
     46         assertTrue("buttons should be shorter than screen",
     47                 getActivity().getButtonAt(0).getHeight()
     48                         < mScrollView.getHeight());
     49 
     50         assertTrue("should be enough buttons to have some scrolled off screen",
     51                 getActivity().getLinearLayout().getHeight()
     52                         > getActivity().getScrollView().getHeight());
     53     }
     54 
     55     @LargeTest
     56     public void testScrollDownToBottomThroughButtons() throws Exception {
     57         final int numButtons = getActivity().getNumButtons();
     58 
     59         for (int i = 0; i < numButtons; i++) {
     60             String prefix = "after " + i + " downs expected button " + i;
     61             final Button button = getActivity().getButtonAt(i);
     62             assertTrue(prefix + "  to have focus", button.isFocused());
     63             assertTrue(prefix + " to be on screen", isButtonOnScreen(button));
     64             sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
     65         }
     66 
     67         assertEquals("should be fully scrolled to bottom",
     68                 getActivity().getLinearLayout().getHeight() - mScrollView.getHeight(),
     69                 mScrollView.getScrollY());
     70     }
     71 
     72     @LargeTest
     73     public void testScrollFromBottomToTopThroughButtons() throws Exception {
     74         final int numButtons = getActivity().getNumButtons();
     75 
     76         final Button lastButton = getActivity().getButtonAt(numButtons - 1);
     77 
     78         lastButton.post(new Runnable() {
     79             public void run() {
     80                 lastButton.requestFocus();
     81             }
     82         });
     83 
     84         getInstrumentation().waitForIdleSync();
     85 
     86         assertTrue("lastButton.isFocused()", lastButton.isFocused());
     87 
     88         for (int i = numButtons - 1; i >= 0; i--) {
     89             String prefix = "after " + i + " ups expected button " + i;
     90             final Button button = getActivity().getButtonAt(i);
     91             assertTrue(prefix + "  to have focus", button.isFocused());
     92             assertTrue(prefix + " to be on screen", isButtonOnScreen(button));
     93             sendKeys(KeyEvent.KEYCODE_DPAD_UP);
     94         }
     95 
     96         assertEquals("should be fully scrolled to top",
     97                 0,
     98                 mScrollView.getScrollY());
     99     }
    100 
    101     private Rect mTempRect = new Rect();
    102     protected boolean isButtonOnScreen(Button b) {
    103         b.getDrawingRect(mTempRect);
    104         mScrollView.offsetDescendantRectToMyCoords(b, mTempRect);
    105         return mTempRect.bottom >= mScrollView.getScrollY()
    106                 && mTempRect.top <= (mScrollView.getScrollY() + mScrollView.getHeight());
    107     }
    108 
    109 }
    110