private void cleanDirectoryOnDevice(String fullPath, IDevice device) throws Exception { CollectingOutputReceiver deleteDirOutputReceiver = new CollectingOutputReceiver(); device.executeShellCommand("rm -rf " + fullPath, deleteDirOutputReceiver); }
/** Turn on debug logging in ddmlib classes. */ static void setDdmlibInternalLoggingLevel() { DdmPreferences.setLogLevel("debug"); }
public SpoonAndroidTestRunner(String appPackageName, String packageName, String runnerName, IShellEnabledDevice remoteDevice, boolean clearAppDataBeforeEachTest, boolean debug) { super(packageName, runnerName, remoteDevice); this.remoteDevice = remoteDevice; this.outputReceiver = new CollectingOutputReceiver(); this.clearCommandStr = "pm clear " + appPackageName; this.clearAppDataBeforeEachTest = clearAppDataBeforeEachTest; this.debug = debug; }
/** Fetch or create a real device that corresponds to a device model. */ static IDevice obtainRealDevice(AndroidDebugBridge adb, String serial) { // Get an existing real device. for (IDevice adbDevice : adb.getDevices()) { if (adbDevice.getSerialNumber().equals(serial)) { return adbDevice; } } throw new IllegalArgumentException("Unknown device serial: " + serial); }
private String getExternalStoragePath(IDevice device, final String path) throws Exception { CollectingOutputReceiver pathNameOutputReceiver = new CollectingOutputReceiver(); device.executeShellCommand("echo $EXTERNAL_STORAGE", pathNameOutputReceiver); return pathNameOutputReceiver.getOutput().trim() + "/" + path; }
/** * Initialize the Android Debug Bridge and wait for it to start. Does not reinitialize it if it has * already been initialized (that would through and IllegalStateException...). Synchronized sine * the init call in the library is also synchronized .. just in case. */ protected AndroidDebugBridge initAndroidDebugBridge() throws MojoExecutionException { synchronized ( ADB_LOCK ) { if ( ! adbInitialized ) { DdmPreferences.setTimeOut( adbConnectionTimeout ); AndroidDebugBridge.init( false ); adbInitialized = true; } AndroidDebugBridge androidDebugBridge = AndroidDebugBridge .createBridge( getAndroidSdk().getAdbPath(), false ); waitUntilConnected( androidDebugBridge ); return androidDebugBridge; } }
/** Get an {@link com.android.ddmlib.AndroidDebugBridge} instance given an SDK path. */ public static AndroidDebugBridge initAdb(File sdk, Duration timeOut) { AndroidDebugBridge.initIfNeeded(false); File adbPath = FileUtils.getFile(sdk, "platform-tools", "adb"); AndroidDebugBridge adb = AndroidDebugBridge.createBridge(adbPath.getAbsolutePath(), false); waitForAdb(adb, timeOut); return adb; }
private void adbPullFile(IDevice device, String remoteFile, String localDir) { try { device.getSyncService() .pullFile(remoteFile, localDir, getNullProgressMonitor()); } catch (Exception e) { logDebug(debug, e.getMessage(), e); } }
private void adbPull(IDevice device, FileEntry remoteDirName, String localDirName) { try { device.getSyncService().pull(new FileEntry[]{remoteDirName}, localDirName, getNullProgressMonitor()); } catch (Exception e) { logDebug(debug, e.getMessage(), e); } }
/** * @return the descriptive name with online/offline/unknown status string appended. */ public static String getDescriptiveNameWithStatus( IDevice device ) { String status; if ( device.isOnline() ) { status = "Online"; } else { if ( device.isOffline() ) { status = "Offline"; } else { status = "Unknown"; } } return getDescriptiveName( device ) + " " + status; } }
/** * Checks whether the given device has the same AVD name as the device which the current command * is related to. <code>true</code> returned if the device AVD names are identical (independent of case) * and <code>false</code> if the device AVD names are different. * * @param device The device to check * @return Boolean results of the check */ private boolean isExistingEmulator( IDevice device ) { return ( ( device.getAvdName() != null ) && ( device.getAvdName().equalsIgnoreCase( parsedAvd ) ) ); }
/** * @return the model of the device as set in #MODEL_PROPERTY, typically "sdk" for emulators */ public static String getModel( IDevice device ) { return StringUtils.deleteWhitespace( device.getProperty( MODEL_PROPERTY ) ); }
private IDevice findExistingEmulator( List<IDevice> devices ) { IDevice existingEmulator = null; for ( IDevice device : devices ) { if ( device.isEmulator() ) { if ( isExistingEmulator( device ) ) { existingEmulator = device; break; } } } return existingEmulator; }
@Override public void done() { handleTestEnd(); handleTestRunEnded(); super.done(); }
private void cleanFilesDirectoriesOnDevice(IDevice device) throws Exception { FileEntry externalFileDir = getDirectoryOnExternalStorage(device, DEVICE_FILE_DIR); cleanDirectoryOnDevice(externalFileDir.getFullPath(), device); FileEntry internalFileDir = getDirectoryOnInternalStorage(DEVICE_FILE_DIR); cleanDirectoryOnDevice(internalFileDir.getFullPath(), device); }
/** * Find all device serials that are plugged in through ADB. * * @param minApiLevel If <code>null</code>, all devices will be returned. Otherwise, only * those device serials that are greater than or equal to the provided * version will be returned. */ public static Set<String> findAllDevices(AndroidDebugBridge adb, Integer minApiLevel) { Set<String> devices = new LinkedHashSet<>(); for (IDevice realDevice : adb.getDevices()) { if (minApiLevel == null) { devices.add(realDevice.getSerialNumber()); } else { DeviceDetails deviceDetails = DeviceDetails.createForDevice(realDevice); int apiLevel = deviceDetails.getApiLevel(); if (apiLevel == DeviceDetails.UNKNOWN_API_LEVEL || apiLevel >= minApiLevel) { devices.add(realDevice.getSerialNumber()); } } } return devices; }
private void grantReadWriteExternalStorage(DeviceDetails deviceDetails, IDevice device) throws Exception { // If this is Android Marshmallow or above grant WRITE_EXTERNAL_STORAGE if (deviceDetails.getApiLevel() >= DeviceDetails.MARSHMALLOW_API_LEVEL) { String appPackage = instrumentationInfo.getApplicationPackage(); CollectingOutputReceiver grantOutputReceiver = new CollectingOutputReceiver(); device.executeShellCommand( "pm grant " + appPackage + " android.permission.READ_EXTERNAL_STORAGE", grantOutputReceiver); device.executeShellCommand( "pm grant " + appPackage + " android.permission.WRITE_EXTERNAL_STORAGE", grantOutputReceiver); } }
/** * @return the manufacturer of the device as set in #MANUFACTURER_PROPERTY, typically "unknown" for emulators */ public static String getManufacturer( IDevice device ) { return StringUtils.deleteWhitespace( device.getProperty( MANUFACTURER_PROPERTY ) ); }
private void cleanScreenshotsDirectoriesOnDevice(IDevice device) throws Exception { FileEntry externalScreenShotDir = getDirectoryOnExternalStorage(device, DEVICE_SCREENSHOT_DIR); cleanDirectoryOnDevice(externalScreenShotDir.getFullPath(), device); FileEntry internalScreenShotDir = getDirectoryOnInternalStorage(DEVICE_SCREENSHOT_DIR); cleanDirectoryOnDevice(internalScreenShotDir.getFullPath(), device); }
private void pullDirectory(final IDevice device, final String name) throws Exception { // Output path on private internal storage, for KitKat and below. FileEntry internalDir = getDirectoryOnInternalStorage(name); logDebug(debug, "Internal path is " + internalDir.getFullPath()); // Output path on public external storage, for Lollipop and above. FileEntry externalDir = getDirectoryOnExternalStorage(device, name); logDebug(debug, "External path is " + externalDir.getFullPath()); // Sync test output files to the local filesystem. logDebug(debug, "Pulling files from external dir on [%s]", serial); String localDirName = work.getAbsolutePath(); adbPull(device, externalDir, localDirName); logDebug(debug, "Pulling files from internal dir on [%s]", serial); adbPull(device, internalDir, localDirName); logDebug(debug, "Done pulling %s from on [%s]", name, serial); }