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