Home | History | Annotate | Download | only in launcherhelper
      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 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.util.Log;
     25 import android.widget.TextView;
     26 
     27 import junit.framework.Assert;
     28 
     29 import java.io.ByteArrayOutputStream;
     30 import java.io.IOException;
     31 
     32 public abstract class BaseLauncher3Strategy implements ILauncherStrategy {
     33     private static final String LOG_TAG = BaseLauncher3Strategy.class.getSimpleName();
     34     protected UiDevice mDevice;
     35 
     36     /**
     37      * {@inheritDoc}
     38      */
     39     @Override
     40     public void setUiDevice(UiDevice uiDevice) {
     41         mDevice = uiDevice;
     42     }
     43 
     44     /**
     45      * {@inheritDoc}
     46      */
     47     @Override
     48     public void open() {
     49         // if we see hotseat, assume at home screen already
     50         if (!mDevice.hasObject(getHotSeatSelector())) {
     51             mDevice.pressHome();
     52             // ensure launcher is shown
     53             if (!mDevice.wait(Until.hasObject(getHotSeatSelector()), 5000)) {
     54                 // HACK: dump hierarchy to logcat
     55                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
     56                 try {
     57                     mDevice.dumpWindowHierarchy(baos);
     58                     baos.flush();
     59                     baos.close();
     60                     String[] lines = baos.toString().split("\\r?\\n");
     61                     for (String line : lines) {
     62                         Log.d(LOG_TAG, line.trim());
     63                     }
     64                 } catch (IOException ioe) {
     65                     Log.e(LOG_TAG, "error dumping XML to logcat", ioe);
     66                 }
     67                 Assert.fail("Failed to open launcher");
     68             }
     69             mDevice.waitForIdle();
     70         }
     71         dismissHomeScreenCling();
     72     }
     73 
     74     /**
     75      * Checks and dismisses home screen cling
     76      */
     77     protected void dismissHomeScreenCling() {
     78         // empty default implementation
     79     }
     80 
     81     /**
     82      * {@inheritDoc}
     83      */
     84     @Override
     85     public UiObject2 openAllApps(boolean reset) {
     86         // if we see all apps container, skip the opening step
     87         if (!mDevice.hasObject(getAllAppsSelector())) {
     88             open();
     89             // taps on the "apps" button at the bottom of the screen
     90             UiObject2 allAppsButton =
     91                     mDevice.wait(Until.findObject(getAllAppsButtonSelector()), 2000);
     92             Assert.assertNotNull("openAllApps: did not find open all apps button.", allAppsButton);
     93             allAppsButton.click();
     94             // wait until hotseat disappears, so that we know that we are no longer on home screen
     95             mDevice.wait(Until.gone(getHotSeatSelector()), 2000);
     96             mDevice.waitForIdle();
     97         }
     98         UiObject2 allAppsContainer = mDevice.wait(Until.findObject(getAllAppsSelector()), 2000);
     99         Assert.assertNotNull("openAllApps: did not find all apps container", allAppsContainer);
    100         if (reset) {
    101             CommonLauncherHelper.getInstance(mDevice).scrollBackToBeginning(
    102                     allAppsContainer, Direction.reverse(getAllAppsScrollDirection()));
    103         }
    104         return allAppsContainer;
    105     }
    106 
    107     /**
    108      * {@inheritDoc}
    109      */
    110     @Override
    111     public Direction getAllAppsScrollDirection() {
    112         return Direction.DOWN;
    113     }
    114 
    115     /**
    116      * {@inheritDoc}
    117      */
    118     @Override
    119     public UiObject2 openAllWidgets(boolean reset) {
    120         if (!mDevice.hasObject(getAllWidgetsSelector())) {
    121             open();
    122             // trigger the wallpapers/widgets/settings view
    123             mDevice.pressMenu();
    124             mDevice.waitForIdle();
    125             mDevice.findObject(By.res(getSupportedLauncherPackage(), "widget_button")).click();
    126         }
    127         UiObject2 allWidgetsContainer = mDevice.wait(
    128                 Until.findObject(getAllWidgetsSelector()), 2000);
    129         Assert.assertNotNull("openAllWidgets: did not find all widgets container",
    130                 allWidgetsContainer);
    131         if (reset) {
    132             CommonLauncherHelper.getInstance(mDevice).scrollBackToBeginning(
    133                     allWidgetsContainer, Direction.reverse(getAllWidgetsScrollDirection()));
    134         }
    135         return allWidgetsContainer;
    136     }
    137 
    138     /**
    139      * {@inheritDoc}
    140      */
    141     @Override
    142     public Direction getAllWidgetsScrollDirection() {
    143         return Direction.DOWN;
    144     }
    145 
    146     /**
    147      * {@inheritDoc}
    148      */
    149     @Override
    150     public long launch(String appName, String packageName) {
    151         BySelector app = By.clazz(TextView.class).text(appName);
    152         return CommonLauncherHelper.getInstance(mDevice).launchApp(this, app, packageName);
    153     }
    154 
    155     /**
    156      * {@inheritDoc}
    157      */
    158     @Override
    159     public BySelector getAllAppsSelector() {
    160         return By.res(getSupportedLauncherPackage(), "apps_list_view");
    161     }
    162 
    163     /**
    164      * {@inheritDoc}
    165      */
    166     @Override
    167     public BySelector getAllWidgetsSelector() {
    168         return By.res(getSupportedLauncherPackage(), "widgets_list_view");
    169     }
    170 
    171     /**
    172      * {@inheritDoc}
    173      */
    174     @Override
    175     public BySelector getWorkspaceSelector() {
    176         return By.res(getSupportedLauncherPackage(), "workspace");
    177     }
    178 
    179     /**
    180      * {@inheritDoc}
    181      */
    182     @Override
    183     public BySelector getHotSeatSelector() {
    184         return By.res(getSupportedLauncherPackage(), "search_container_hotseat");
    185     }
    186 
    187     /**
    188      * {@inheritDoc}
    189      */
    190     @Override
    191     public Direction getWorkspaceScrollDirection() {
    192         return Direction.RIGHT;
    193     }
    194 }
    195