Home | History | Annotate | Download | only in functional
      1 /*
      2  * Copyright (C) 2016 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.overview.functional;
     18 
     19 import java.io.File;
     20 import java.io.IOException;
     21 
     22 import android.app.UiAutomation;
     23 import android.content.Intent;
     24 import android.content.pm.PackageManager;
     25 import android.content.Context;
     26 import android.graphics.Point;
     27 import android.graphics.Rect;
     28 import android.os.PowerManager;
     29 import android.os.RemoteException;
     30 import android.os.SystemClock;
     31 import android.platform.test.annotations.Presubmit;
     32 import android.support.test.launcherhelper.ILauncherStrategy;
     33 import android.support.test.launcherhelper.LauncherStrategyFactory;
     34 import android.support.test.uiautomator.By;
     35 import android.support.test.uiautomator.Direction;
     36 import android.support.test.uiautomator.UiDevice;
     37 import android.support.test.uiautomator.UiObject2;
     38 import android.support.test.uiautomator.UiObjectNotFoundException;
     39 import android.support.test.uiautomator.Until;
     40 import android.test.InstrumentationTestCase;
     41 import android.test.suitebuilder.annotation.MediumTest;
     42 import android.util.Log;
     43 import android.view.KeyEvent;
     44 
     45 public class RecentsTests extends InstrumentationTestCase {
     46 
     47     private static final int TIMEOUT = 3000;
     48     private static final String RECENTS = "com.android.systemui:id/recents_view";
     49     private UiDevice mDevice;
     50     private PackageManager mPackageManager;
     51     private ILauncherStrategy mLauncherStrategy = null;
     52     private OverviewHelper mOverviewHelper;
     53 
     54     @Override
     55     public void setUp() throws Exception {
     56         super.setUp();
     57         mDevice = UiDevice.getInstance(getInstrumentation());
     58         mPackageManager = getInstrumentation().getContext().getPackageManager();
     59         mDevice.setOrientationNatural();
     60         mLauncherStrategy = LauncherStrategyFactory.getInstance(mDevice).getLauncherStrategy();
     61         mOverviewHelper = new OverviewHelper(mDevice, getInstrumentation());
     62     }
     63 
     64     @Override
     65     protected void tearDown() throws Exception {
     66         mDevice.pressHome();
     67         mDevice.unfreezeRotation();
     68         mDevice.waitForIdle();
     69         super.tearDown();
     70     }
     71 
     72     @Presubmit
     73     @MediumTest
     74     public void testNavigateToRecents() throws Exception {
     75         UiObject2 recents = mOverviewHelper.navigateToRecents();
     76         assertNotNull("Recents view not reached on tapping recents", recents);
     77     }
     78 
     79     @Presubmit
     80     @MediumTest
     81     public void testAddAndDismissItemInRecents() throws Exception {
     82         mOverviewHelper.launchAppWithIntent("com.google.android.calculator");
     83         mOverviewHelper.navigateToRecents();
     84         UiObject2 dismissCalculator = mDevice.wait(Until.findObject
     85                 (By.desc("Dismiss Calculator.")),TIMEOUT);
     86         assertNotNull("Calculator not found in Recents", dismissCalculator);
     87         dismissCalculator.click();
     88         mDevice.waitForIdle();
     89         // Adding an extra sleep here so UiAutomator picks up
     90         // the refreshed UI and not the old one, otherwise the
     91         // test fails because it thinks the 'Dismiss Calculator'
     92         // button is still present.
     93         Thread.sleep(TIMEOUT);
     94         UiObject2 afterDismissCalculator = mDevice.wait(Until.findObject
     95                 (By.desc("Dismiss Calculator.")),TIMEOUT);
     96         assertNull("Calculator not dismissed from Recents", afterDismissCalculator);
     97     }
     98 
     99     @Presubmit
    100     @MediumTest
    101     public void testScrollThroughRecents() throws Exception {
    102         mOverviewHelper.populateRecents();
    103         mOverviewHelper.navigateToRecents();
    104         UiObject2 recentsView = mDevice.wait(Until.findObject
    105                 (By.res(RECENTS)),TIMEOUT);
    106         mOverviewHelper.scrollToTopOfRecents(recentsView);
    107         // After scrolling we look for the 'Clear All' button on the top
    108         // right. This ensures a successful scroll.
    109         UiObject2 clearAll = mDevice.wait(Until.findObject
    110                 (By.text("CLEAR ALL")),TIMEOUT);
    111         assertNotNull("Unable to scroll to top of recents", clearAll);
    112         clearAll.click();
    113         Thread.sleep(TIMEOUT);
    114     }
    115 
    116     @Presubmit
    117     @MediumTest
    118     public void testSwipeItemAwayFromRecents() throws Exception {
    119         mOverviewHelper.launchAppWithIntent("com.google.android.calculator");
    120         Thread.sleep(TIMEOUT);
    121         mOverviewHelper.navigateToRecents();
    122         UiObject2 calculatorText = mDevice.wait(Until.findObject
    123                 (By.desc("Calculator")),TIMEOUT);
    124         // Get the bounds of the text 'Calculator' which is
    125         // easily identifiable and swipe all the way from the
    126         // left to the right to dismiss.
    127         Rect calcBounds = calculatorText.getVisibleBounds();
    128         mDevice.swipe(calcBounds.left, calcBounds.top + calcBounds.height() / 2,
    129                 calcBounds.right, calcBounds.top + calcBounds.height() / 2, 5);
    130         // Try to refetch Calculator text
    131         calculatorText = mDevice.wait(Until.findObject
    132                 (By.desc("Calculator")),TIMEOUT);
    133         assertNull("Calculator app still present after swiping away",
    134                 calculatorText);
    135     }
    136 
    137     @Presubmit
    138     @MediumTest
    139     public void testClearAllFromRecents() throws Exception {
    140         mOverviewHelper.populateRecents();
    141         mOverviewHelper.navigateToRecents();
    142         UiObject2 recentsView = mDevice.wait(Until.findObject
    143                             (By.res(RECENTS)),TIMEOUT);
    144         // fling to top
    145         mOverviewHelper.scrollToTopOfRecents(recentsView);
    146         // click clear all
    147         UiObject2 clearAll = mDevice.wait(Until.findObject
    148                 (By.text("CLEAR ALL")),TIMEOUT);
    149         clearAll.click();
    150         Thread.sleep(TIMEOUT);
    151         mOverviewHelper.navigateToRecents();
    152         // verify empty recents list
    153         assertTrue("Recent items not empty", mDevice.wait
    154                 (Until.hasObject(By.text("No recent items")), TIMEOUT));
    155     }
    156 
    157     @MediumTest
    158     public void testDoubleTapToSwitchRecents() throws Exception {
    159         mOverviewHelper.launchAppWithIntent("com.google.android.calculator");
    160         mOverviewHelper.launchAppWithIntent("com.google.android.gm");
    161         // Literally tapping twice as there's no 'double tap'
    162         // method.
    163         mDevice.pressRecentApps();
    164         mDevice.pressRecentApps();
    165         mDevice.waitForIdle();
    166         // Verify that the app has switched to calculator after
    167         // the double tap
    168         UiObject2 calculatorText = mDevice.wait(Until.findObject
    169                 (By.desc("Calculator")),TIMEOUT);
    170     }
    171 }
    172