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 
     17 package android.support.test.launcherhelper;
     18 
     19 import android.app.Instrumentation;
     20 import android.app.UiAutomation;
     21 import android.content.pm.PackageManager;
     22 import android.content.Context;
     23 import android.os.RemoteException;
     24 import android.support.test.InstrumentationRegistry;
     25 import android.support.test.launcherhelper.ILauncherStrategy;
     26 import android.support.test.launcherhelper.LauncherStrategyFactory;
     27 import android.support.test.uiautomator.By;
     28 import android.support.test.uiautomator.Direction;
     29 import android.support.test.uiautomator.UiDevice;
     30 import android.support.test.uiautomator.UiObject2;
     31 import android.support.test.uiautomator.UiObjectNotFoundException;
     32 import android.support.test.uiautomator.Until;
     33 import android.system.helpers.ActivityHelper;
     34 import android.test.InstrumentationTestCase;
     35 
     36 import java.io.File;
     37 import java.io.IOException;
     38 
     39 import org.junit.Assert;
     40 
     41 public class AllAppsScreenHelper {
     42 
     43     private static final int TIMEOUT = 3000;
     44     private static final int LONG_TIMEOUT = 10000;
     45     private UiDevice mDevice;
     46     private Instrumentation mInstrumentation;
     47     private ILauncherStrategy mLauncherStrategy = LauncherStrategyFactory
     48             .getInstance(mDevice).getLauncherStrategy();
     49     private String allApps = "apps_view";
     50     private String appsListView = "apps_list_view";
     51     private String searchBox = "search_box_input";
     52     private ActivityHelper mActivityHelper;
     53 
     54     public AllAppsScreenHelper() {
     55         mInstrumentation = InstrumentationRegistry.getInstrumentation();
     56         mActivityHelper = ActivityHelper.getInstance();
     57         mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
     58     }
     59 
     60     public String getLauncherPackage() {
     61         return mDevice.getLauncherPackageName();
     62     }
     63 
     64     public void launchAllAppsScreen() {
     65         mDevice.pressHome();
     66         mDevice.findObject(mLauncherStrategy.getAllAppsButtonSelector()).click();
     67         mDevice.wait(Until.hasObject(By.res(getLauncherPackage(), allApps)), TIMEOUT);
     68     }
     69 
     70     public void searchAllAppsScreen(String searchString,
     71         String[] appNamesExpected) throws Exception {
     72         launchAllAppsScreen();
     73         UiObject2 searchBoxObject = mDevice.wait(Until.findObject
     74                 (By.res(getLauncherPackage(), searchBox)), TIMEOUT);
     75         searchBoxObject.setText(searchString);
     76         for (String appName : appNamesExpected) {
     77             Assert.assertNotNull("The following app couldn't be found in the search results: "
     78                     + appName, mDevice.wait(Until.findObject
     79                     (By.text(appName)), TIMEOUT));
     80         }
     81     }
     82 }
     83