1 // Copyright 2019 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.native_test; 6 7 import org.chromium.base.Log; 8 import org.chromium.base.StrictModeContext; 9 import org.chromium.base.annotations.JNINamespace; 10 import org.chromium.base.annotations.NativeMethods; 11 12 import java.io.File; 13 14 /** 15 * Helper for browser tests running inside a java Activity. 16 */ 17 @JNINamespace("testing::android") 18 public class NativeBrowserTest { 19 private static final String TAG = "NativeBrowserTest"; 20 21 // Set the command line flags to be passed to the C++ main() method. Each 22 // browser tests Activity should ensure these are included. 23 public static final String BROWSER_TESTS_FLAGS[] = { 24 // switches::kSingleProcessTests 25 "--single-process-tests"}; 26 27 /** 28 * Deletes a file or directory along with any of its children. 29 * 30 * Note that, like File.delete(), this returns false if the file or directory couldn't be 31 * fully deleted. This means that, in the directory case, some files may be deleted even if 32 * the entire directory couldn't be. 33 * 34 * @param file The file or directory to delete. 35 * @return Whether or not the file or directory was deleted. 36 */ deleteRecursive(File file)37 private static boolean deleteRecursive(File file) { 38 if (file == null) return true; 39 40 File[] children; 41 try (StrictModeContext ignored = StrictModeContext.allowDiskReads()) { 42 children = file.listFiles(); 43 } 44 if (children != null) { 45 for (File child : children) { 46 if (!deleteRecursive(child)) { 47 return false; 48 } 49 } 50 } 51 try (StrictModeContext ignored = StrictModeContext.allowDiskWrites()) { 52 return file.delete(); 53 } 54 } 55 deletePrivateDataDirectory(File privateDataDirectory)56 public static void deletePrivateDataDirectory(File privateDataDirectory) { 57 if (!deleteRecursive(privateDataDirectory)) { 58 Log.e(TAG, "Failed to remove %s", privateDataDirectory.getAbsolutePath()); 59 } 60 } 61 62 /** 63 * To be called when the browser tests Activity has completed any asynchronous 64 * initialization and is ready for the test to run. This informs C++ to run the test. 65 */ javaStartupTasksComplete()66 public static void javaStartupTasksComplete() { 67 NativeBrowserTestJni.get().javaStartupTasksCompleteForBrowserTests(); 68 } 69 70 @NativeMethods 71 interface Natives { javaStartupTasksCompleteForBrowserTests()72 void javaStartupTasksCompleteForBrowserTests(); 73 } 74 } 75