AdbFileOperations { @NotNull public ListenableFuture<Void> createNewFileRunAs(@NotNull String parentPath, @NotNull String fileName, @Nullable String runAs) { return myExecutor.executeAsync(() -> { if (fileName.contains(AdbPathUtil.FILE_SEPARATOR)) { throw AdbShellCommandException.create("File name \"%s\" contains invalid characters", fileName); } String remotePath = AdbPathUtil.resolve(parentPath, fileName); String command; if (myDeviceCapabilities.supportsTestCommand()) { command = getCommand(runAs, "test -e ").withEscapedPath(remotePath).build(); } else { command = getCommand(runAs, "ls -d -a ").withEscapedPath(remotePath).build(); } AdbShellCommandResult commandResult = AdbShellCommandsUtil.executeCommand(myDevice, command); if (!commandResult.isError()) { throw AdbShellCommandException.create("File \"%s\" already exists on device", remotePath); } touchFileRunAs(remotePath, runAs); return null; }); } AdbFileOperations(@NotNull IDevice device, @NotNull AdbDeviceCapabilities deviceCapabilities, @NotNull Executor taskExecutor); @NotNull ListenableFuture<Void> createNewFile(@NotNull String parentPath, @NotNull String fileName); @NotNull ListenableFuture<Void> createNewFileRunAs(@NotNull String parentPath, @NotNull String fileName, @Nullable String runAs); @NotNull ListenableFuture<Void> createNewDirectory(@NotNull String parentPath, @NotNull String directoryName); @NotNull ListenableFuture<Void> createNewDirectoryRunAs(@NotNull String parentPath, @NotNull String directoryName, @Nullable String runAs); ListenableFuture<List<String>> listPackages(); ListenableFuture<List<PackageInfo>> listPackageInfo(); @NotNull ListenableFuture<Void> deleteFile(@NotNull String path); @NotNull ListenableFuture<Void> deleteFileRunAs(@NotNull String path, @Nullable String runAs); @NotNull ListenableFuture<Void> deleteRecursive(@NotNull String path); @NotNull ListenableFuture<Void> deleteRecursiveRunAs(@NotNull String path, @Nullable String runAs); @NotNull ListenableFuture<Void> copyFile(@NotNull String source, @NotNull String destination); @NotNull ListenableFuture<Void> copyFileRunAs(@NotNull String source, @NotNull String destination, @Nullable String runAs); @NotNull ListenableFuture<String> createTempFile(@NotNull String tempPath); @SuppressWarnings({"SameParameterValue", "WeakerAccess"}) @NotNull ListenableFuture<String> createTempFileRunAs(@NotNull String tempDirectoy, @Nullable String runAs); @NotNull ListenableFuture<Void> touchFileAsDefaultUser(@NotNull String remotePath);  }
@Test public void testCreateNewFileRunAsSuccess() throws Exception { AdbFileOperations fileOperations = setupMockDevice(); Void result = waitForFuture(fileOperations.createNewFileRunAs("/data/data/com.example.rpaquay.myapplication", "NewTextFile.txt", "com.example.rpaquay.myapplication")); assertThat(result).isNull(); }