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 com.android.androidbvt; 18 19 import android.app.DownloadManager; 20 import android.app.UiAutomation; 21 import android.content.Context; 22 import android.net.ConnectivityManager; 23 import android.net.wifi.WifiManager; 24 import android.os.ParcelFileDescriptor; 25 import android.support.test.uiautomator.UiDevice; 26 import android.telecom.TelecomManager; 27 import android.util.DisplayMetrics; 28 import android.util.Log; 29 import android.view.WindowManager; 30 31 import java.io.BufferedReader; 32 import java.io.FileInputStream; 33 import java.io.IOException; 34 import java.io.InputStreamReader; 35 import java.util.ArrayList; 36 import java.util.List; 37 38 /** 39 * Defines constants & implements common methods to be used by Framework, SysUI, System e2e BVT 40 * tests. Also ensures single instance of this object 41 */ 42 public class AndroidBvtHelper { 43 public static final String TEST_TAG = "AndroidBVT"; 44 public static final int SHORT_TIMEOUT = 1000; 45 public static final int LONG_TIMEOUT = 5000; 46 // 600dp is the threshold value for 7-inch tablets. 47 private static final int TABLET_DP_THRESHOLD = 600; 48 private static AndroidBvtHelper sInstance = null; 49 private Context mContext = null; 50 private UiDevice mDevice = null; 51 private UiAutomation mUiAutomation = null; 52 AndroidBvtHelper(UiDevice device, Context context, UiAutomation uiAutomation)53 public AndroidBvtHelper(UiDevice device, Context context, UiAutomation uiAutomation) { 54 mContext = context; 55 mDevice = device; 56 mUiAutomation = uiAutomation; 57 } 58 getInstance(UiDevice device, Context context, UiAutomation uiAutomation)59 public static AndroidBvtHelper getInstance(UiDevice device, Context context, 60 UiAutomation uiAutomation) { 61 if (sInstance == null) { 62 sInstance = new AndroidBvtHelper(device, context, uiAutomation); 63 } 64 return sInstance; 65 } 66 getTelecomManager()67 public TelecomManager getTelecomManager() { 68 return (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); 69 } 70 getWifiManager()71 public WifiManager getWifiManager() { 72 return (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); 73 } 74 getConnectivityManager()75 public ConnectivityManager getConnectivityManager() { 76 return (ConnectivityManager) (ConnectivityManager) mContext 77 .getSystemService(Context.CONNECTIVITY_SERVICE); 78 } 79 getDownloadManager()80 public DownloadManager getDownloadManager() { 81 return (DownloadManager) (DownloadManager) mContext 82 .getSystemService(Context.DOWNLOAD_SERVICE); 83 } 84 85 /** 86 * Only executes 'adb shell' commands that run in the same process as the runner. Converts output 87 * of the command from ParcelFileDescriptior to user friendly list of strings 88 * https://developer.android.com/reference/android/app/UiAutomation.html#executeShellCommand( 89 * java.lang.String) 90 */ executeShellCommand(String cmd)91 public List<String> executeShellCommand(String cmd) { 92 if (cmd == null || cmd.isEmpty()) { 93 return null; 94 } 95 List<String> output = new ArrayList<String>(); 96 ParcelFileDescriptor pfd = mUiAutomation.executeShellCommand(cmd); 97 try (BufferedReader reader = new BufferedReader( 98 new InputStreamReader(new FileInputStream(pfd.getFileDescriptor())))) { 99 String line; 100 while ((line = reader.readLine()) != null) { 101 output.add(line); 102 } 103 } catch (IOException e) { 104 Log.e(TEST_TAG, e.getMessage()); 105 return null; 106 } 107 return output; 108 } 109 110 /** Returns true if the device is a tablet */ isTablet()111 public boolean isTablet() { 112 // Get screen density & screen size from window manager 113 WindowManager wm = (WindowManager) mContext.getSystemService( 114 Context.WINDOW_SERVICE); 115 DisplayMetrics metrics = new DisplayMetrics(); 116 wm.getDefaultDisplay().getMetrics(metrics); 117 // Determines the smallest screen width DP which is 118 // calculated as ( pixels * density-independent pixel unit ) / density. 119 // http://developer.android.com/guide/practices/screens_support.html. 120 int screenDensity = metrics.densityDpi; 121 int screenWidth = Math.min( 122 metrics.widthPixels, metrics.heightPixels); 123 int screenHeight = Math.max( 124 metrics.widthPixels, metrics.heightPixels); 125 int smallestScreenWidthDp = (Math.min(screenWidth, screenHeight) 126 * DisplayMetrics.DENSITY_DEFAULT) / screenDensity; 127 return smallestScreenWidthDp >= TABLET_DP_THRESHOLD; 128 } 129 } 130