Home | History | Annotate | Download | only in helpers
      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.platform.test.helpers;
     18 
     19 import android.app.Instrumentation;
     20 import android.content.pm.PackageInfo;
     21 import android.content.pm.PackageManager;
     22 import android.content.pm.PackageManager.NameNotFoundException;
     23 import android.support.test.launcherhelper.ILauncherStrategy;
     24 import android.support.test.launcherhelper.LauncherStrategyFactory;
     25 import android.support.test.uiautomator.By;
     26 import android.support.test.uiautomator.UiDevice;
     27 
     28 public abstract class AbstractStandardAppHelper implements IStandardAppHelper {
     29     public UiDevice mDevice;
     30     public Instrumentation mInstrumentation;
     31     public ILauncherStrategy mLauncherStrategy;
     32 
     33     public AbstractStandardAppHelper(Instrumentation instr) {
     34         mInstrumentation = instr;
     35         mDevice = UiDevice.getInstance(instr);
     36         mLauncherStrategy = LauncherStrategyFactory.getInstance(mDevice).getLauncherStrategy();
     37     }
     38 
     39     /**
     40      * {@inheritDoc}
     41      */
     42     @Override
     43     public void open() {
     44         String pkg = getPackage();
     45         String id = getLauncherName();
     46         if (!mDevice.hasObject(By.pkg(pkg).depth(0))) {
     47             mLauncherStrategy.launch(id, pkg);
     48         }
     49     }
     50 
     51     /**
     52      * {@inheritDoc}
     53      */
     54     @Override
     55     public void exit() {
     56         int maxBacks = 4;
     57         while (!mDevice.hasObject(mLauncherStrategy.getWorkspaceSelector()) && maxBacks > 0) {
     58             mDevice.pressBack();
     59             mDevice.waitForIdle();
     60             maxBacks--;
     61         }
     62 
     63         if (maxBacks == 0) {
     64             mDevice.pressHome();
     65         }
     66     }
     67 
     68     /**
     69      * {@inheritDoc}
     70      */
     71     @Override
     72     public String getVersion() throws NameNotFoundException {
     73         String pkg = getPackage();
     74 
     75         if (null == pkg || pkg.isEmpty()) {
     76             throw new RuntimeException("Cannot find version of empty package");
     77         }
     78         PackageManager pm = mInstrumentation.getContext().getPackageManager();
     79         PackageInfo pInfo = pm.getPackageInfo(pkg, 0);
     80         String version = pInfo.versionName;
     81         if (null == version || version.isEmpty()) {
     82             throw new RuntimeException(String.format("Version isn't found for package, %s", pkg));
     83         }
     84 
     85         return version;
     86     }
     87 
     88     protected int getOrientation() {
     89         return mInstrumentation.getContext().getResources().getConfiguration().orientation;
     90     }
     91 }
     92