Home | History | Annotate | Download | only in launcherhelper
      1 /*
      2  * Copyright (C) 2015 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 package android.support.test.launcherhelper;
     17 
     18 import android.support.test.uiautomator.By;
     19 import android.support.test.uiautomator.BySelector;
     20 import android.support.test.uiautomator.Direction;
     21 import android.support.test.uiautomator.UiDevice;
     22 import android.support.test.uiautomator.UiObject2;
     23 import android.support.test.uiautomator.Until;
     24 import android.widget.Button;
     25 import android.widget.TextView;
     26 
     27 import junit.framework.Assert;
     28 
     29 /**
     30  * Implementation of {@link ILauncherStrategy} to support AOSP launcher
     31  */
     32 public class AospLauncherStrategy implements ILauncherStrategy {
     33 
     34     private static final String LAUNCHER_PKG = "com.android.launcher";
     35     private static final BySelector APPS_CONTAINER =
     36             By.res(LAUNCHER_PKG, "apps_customize_pane_content");
     37     private static final BySelector WORKSPACE = By.res(LAUNCHER_PKG, "workspace");
     38     private static final BySelector HOTSEAT = By.res(LAUNCHER_PKG, "hotseat");
     39     private UiDevice mDevice;
     40 
     41     /**
     42      * {@inheritDoc}
     43      */
     44     @Override
     45     public void open() {
     46         // if we see hotseat, assume at home screen already
     47         if (!mDevice.hasObject(getHotSeatSelector())) {
     48             mDevice.pressHome();
     49             Assert.assertTrue("Failed to open launcher",
     50                     mDevice.wait(Until.hasObject(By.pkg(LAUNCHER_PKG)), 5000));
     51             mDevice.waitForIdle();
     52         }
     53         // remove cling if it exists
     54         UiObject2 cling = mDevice.findObject(By.res(LAUNCHER_PKG, "workspace_cling"));
     55         if (cling != null) {
     56             cling.findObject(By.clazz(Button.class).text("OK")).click();
     57         }
     58     }
     59 
     60     /**
     61      * {@inheritDoc}
     62      */
     63     @Override
     64     public UiObject2 openAllApps(boolean reset) {
     65         // if we see apps container, skip the opening step, only ensure that the "Apps" tab is
     66         // selected
     67         if (!mDevice.hasObject(APPS_CONTAINER)) {
     68             open();
     69             // taps on the "apps" button at the bottom of the screen
     70             mDevice.findObject(By.desc("Apps")).click();
     71             // wait until hotseat disappears, so that we know that we are no longer on home screen
     72             mDevice.wait(Until.gone(getHotSeatSelector()), 2000);
     73             mDevice.waitForIdle();
     74         }
     75         // check if there's a "cling" on screen
     76         UiObject2 cling = mDevice.findObject(By.res(LAUNCHER_PKG, "cling_dismiss")
     77                 .clazz(Button.class).text("OK"));
     78         if (cling != null) {
     79             cling.click();
     80         }
     81         // taps on the "apps" page selector near the top of the screen
     82         UiObject2 appsTab = mDevice.findObject(By.desc("Apps")
     83                 .clazz(TextView.class).selected(false));
     84         if (appsTab != null) {
     85             appsTab.click();
     86         }
     87         UiObject2 allAppsContainer = mDevice.findObject(APPS_CONTAINER);
     88         Assert.assertNotNull("openAllApps: did not find all apps container", allAppsContainer);
     89         if (reset) {
     90             CommonLauncherHelper.getInstance(mDevice).scrollBackToBeginning(allAppsContainer,
     91                     Direction.reverse(getAllAppsScrollDirection()));
     92         }
     93         return allAppsContainer;
     94     }
     95 
     96     /**
     97      * {@inheritDoc}
     98      */
     99     @Override
    100     public Direction getAllAppsScrollDirection() {
    101         return Direction.RIGHT;
    102     }
    103 
    104     /**
    105      * {@inheritDoc}
    106      */
    107     @Override
    108     public UiObject2 openAllWidgets(boolean reset) {
    109         boolean needReset = true;
    110         // if we see apps container, skip the opening step, only ensure that the "Widgets" tab is
    111         // selected
    112         if (!mDevice.hasObject(APPS_CONTAINER)) {
    113             open();
    114             // taps on the "apps" button at the bottom of the screen
    115             mDevice.findObject(getAllAppsButtonSelector()).click();
    116             // wait until hotseat disappears, so that we know that we are no longer on home screen
    117             mDevice.wait(Until.gone(getHotSeatSelector()), 2000);
    118             mDevice.waitForIdle();
    119         }
    120         // taps on the "Widgets" page selector near the top of the screen
    121         UiObject2 widgetsTab = mDevice.findObject(By.desc("Widgets")
    122                 .clazz(TextView.class).selected(false));
    123         if (widgetsTab != null) {
    124             widgetsTab.click();
    125             // if we switched into widget page, then there's no need to reset, since it will go
    126             // back to beginning
    127             needReset = false;
    128         }
    129         UiObject2 allWidgetsContainer = mDevice.findObject(APPS_CONTAINER);
    130         Assert.assertNotNull(
    131                 "openAllWidgets: did not find all widgets container", allWidgetsContainer);
    132         if (reset && needReset) {
    133             // only way to reset is to switch to "Apps" then back to "Widget", scroll to beginning
    134             // won't work
    135             mDevice.findObject(By.desc("Apps").selected(false)).click();
    136             mDevice.waitForIdle();
    137             mDevice.findObject(By.desc("Widgets").selected(false)).click();
    138             mDevice.waitForIdle();
    139         }
    140         return allWidgetsContainer;
    141     }
    142 
    143     /**
    144      * {@inheritDoc}
    145      */
    146     @Override
    147     public Direction getAllWidgetsScrollDirection() {
    148         return Direction.RIGHT;
    149     }
    150 
    151     /**
    152      * {@inheritDoc}
    153      */
    154     @Override
    155     public long launch(String appName, String packageName) {
    156         return CommonLauncherHelper.getInstance(mDevice).launchApp(this,
    157                 By.res("").clazz(TextView.class).desc(appName), packageName);
    158     }
    159 
    160     /**
    161      * {@inheritDoc}
    162      */
    163     @Override
    164     public void setUiDevice(UiDevice uiDevice) {
    165         mDevice = uiDevice;
    166     }
    167 
    168     /**
    169      * {@inheritDoc}
    170      */
    171     @Override
    172     public String getSupportedLauncherPackage() {
    173         return LAUNCHER_PKG;
    174     }
    175 
    176     /**
    177      * {@inheritDoc}
    178      */
    179     @Override
    180     public BySelector getAllAppsButtonSelector() {
    181         return By.desc("Apps");
    182     }
    183 
    184     /**
    185      * {@inheritDoc}
    186      */
    187     @Override
    188     public BySelector getAllAppsSelector() {
    189         return APPS_CONTAINER;
    190     }
    191 
    192     /**
    193      * {@inheritDoc}
    194      */
    195     @Override
    196     public BySelector getAllWidgetsSelector() {
    197         return APPS_CONTAINER;
    198     }
    199 
    200     /**
    201      * {@inheritDoc}
    202      */
    203     @Override
    204     public BySelector getWorkspaceSelector() {
    205         return WORKSPACE;
    206     }
    207 
    208     /**
    209      * {@inheritDoc}
    210      */
    211     @Override
    212     public BySelector getHotSeatSelector() {
    213         return HOTSEAT;
    214     }
    215 
    216     /**
    217      * {@inheritDoc}
    218      */
    219     @Override
    220     public Direction getWorkspaceScrollDirection() {
    221         return Direction.RIGHT;
    222     }
    223 
    224 }
    225