Move helper methods and BROWSER_TESTS_FLAGS to NativeBrowserTest.
This adds a NativeBrowserTest helper class for sharing code between
content and chrome browser tests.
ChromeBrowserTestActivity is going to have to inherit from
ChromeTabbedActivity so NativeBrowserTestActivity will no longer be
a suitable place for sharing code. I will collapse it back into
ContentShellBrowserTestActivity again in the future.
For now, shared code moves out of NativeBrowserTestActivity so that
ChromeBrowserTestActivity can change its inheritance.
We clarify the ordering in onCreate() so that when the subclass uses
async startup we can be sure that browser initialization and runTests()
all occur after super.onCreate() by moving that call to the top of
the onCreate() override. mTest.postStart() is moved out to runTests()
though it is called directly from onCreate() for now. The call will
move to the subclass when it uses async initialization.
[email protected]
Bug: 961849
Change-Id: I892a382643e0d8e3029fcac0a406225829cd3ad0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1670407
Commit-Queue: danakj <[email protected]>
Reviewed-by: John Budorick <[email protected]>
Cr-Commit-Position: refs/heads/master@{#671171}
diff --git a/testing/android/native_test/BUILD.gn b/testing/android/native_test/BUILD.gn
index 319d1e9e..d98b78a 100644
--- a/testing/android/native_test/BUILD.gn
+++ b/testing/android/native_test/BUILD.gn
@@ -52,10 +52,11 @@
"//testing/android/reporter:reporter_java",
]
java_files = [
- "java/src/org/chromium/native_test/NativeTestApplication.java",
+ "java/src/org/chromium/native_test/NativeBrowserTest.java",
"java/src/org/chromium/native_test/NativeBrowserTestActivity.java",
"java/src/org/chromium/native_test/NativeBrowserTestApplication.java",
"java/src/org/chromium/native_test/NativeTest.java",
+ "java/src/org/chromium/native_test/NativeTestApplication.java",
"java/src/org/chromium/native_test/NativeTestInstrumentationTestRunner.java",
"java/src/org/chromium/native_test/NativeUnitTest.java",
"java/src/org/chromium/native_test/NativeUnitTestActivity.java",
diff --git a/testing/android/native_test/java/src/org/chromium/native_test/NativeBrowserTest.java b/testing/android/native_test/java/src/org/chromium/native_test/NativeBrowserTest.java
new file mode 100644
index 0000000..c1a3afb1
--- /dev/null
+++ b/testing/android/native_test/java/src/org/chromium/native_test/NativeBrowserTest.java
@@ -0,0 +1,63 @@
+// Copyright 2019 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.native_test;
+
+import org.chromium.base.Log;
+import org.chromium.base.StrictModeContext;
+import org.chromium.base.annotations.JNINamespace;
+
+import java.io.File;
+
+/**
+ * Helper for browser tests running inside a java Activity.
+ */
+@JNINamespace("testing::android")
+public class NativeBrowserTest {
+ private static final String TAG = "NativeBrowserTest";
+
+ // Set the command line flags to be passed to the C++ main() method. Each
+ // browser tests Activity should ensure these are included.
+ public static final String BROWSER_TESTS_FLAGS[] = {
+ // content::kSingleProcessTestsFlag
+ "--single_process",
+
+ // switches::kUseFakeDeviceForMediaStream
+ "--use-fake-device-for-media-stream"};
+
+ /**
+ * Deletes a file or directory along with any of its children.
+ *
+ * Note that, like File.delete(), this returns false if the file or directory couldn't be
+ * fully deleted. This means that, in the directory case, some files may be deleted even if
+ * the entire directory couldn't be.
+ *
+ * @param file The file or directory to delete.
+ * @return Whether or not the file or directory was deleted.
+ */
+ private static boolean deleteRecursive(File file) {
+ if (file == null) return true;
+
+ File[] children;
+ try (StrictModeContext unused = StrictModeContext.allowDiskReads()) {
+ children = file.listFiles();
+ }
+ if (children != null) {
+ for (File child : children) {
+ if (!deleteRecursive(child)) {
+ return false;
+ }
+ }
+ }
+ try (StrictModeContext unused = StrictModeContext.allowDiskWrites()) {
+ return file.delete();
+ }
+ }
+
+ public static void deletePrivateDataDirectory(File privateDataDirectory) {
+ if (!deleteRecursive(privateDataDirectory)) {
+ Log.e(TAG, "Failed to remove %s", privateDataDirectory.getAbsolutePath());
+ }
+ }
+}
diff --git a/testing/android/native_test/java/src/org/chromium/native_test/NativeBrowserTestActivity.java b/testing/android/native_test/java/src/org/chromium/native_test/NativeBrowserTestActivity.java
index 4781f962..61ac642a 100644
--- a/testing/android/native_test/java/src/org/chromium/native_test/NativeBrowserTestActivity.java
+++ b/testing/android/native_test/java/src/org/chromium/native_test/NativeBrowserTestActivity.java
@@ -7,9 +7,6 @@
import android.app.Activity;
import android.os.Bundle;
-import org.chromium.base.Log;
-import org.chromium.base.StrictModeContext;
-
import java.io.File;
/**
@@ -18,14 +15,6 @@
public abstract class NativeBrowserTestActivity extends Activity {
private static final String TAG = "cr_NativeTest";
- private static final String BROWSER_TESTS_FLAGS[] = {
- // content::kSingleProcessTestsFlag
- "--single_process",
-
- // switches::kUseFakeDeviceForMediaStream
- "--use-fake-device-for-media-stream"
- };
-
private NativeTest mTest = new NativeTest();
private boolean mStarted;
@@ -34,25 +23,30 @@
mTest.preCreate(this);
super.onCreate(savedInstanceState);
mTest.postCreate(this);
- for (String flag : BROWSER_TESTS_FLAGS) {
+ for (String flag : NativeBrowserTest.BROWSER_TESTS_FLAGS) {
appendCommandLineFlags(flag);
}
}
@Override
public void onStart() {
+ super.onStart();
+
// onStart can be called any number of times see:
// https://developer.android.com/guide/components/activities/activity-lifecycle#onstart
// We only want to run the test once (or bad things can happen) so bail out if we've
// already started.
- if (mStarted) {
- super.onStart();
- return;
- }
+ if (mStarted) return;
+
mStarted = true;
- deletePrivateDataDirectory();
+ NativeBrowserTest.deletePrivateDataDirectory(getPrivateDataDirectory());
initializeBrowserProcess();
- super.onStart();
+ // TODO(danakj): Move to ContentShellBrowserTestActivity when async
+ // initialization is done there.
+ runTests();
+ }
+
+ protected void runTests() {
mTest.postStart(this, false);
}
@@ -60,41 +54,6 @@
mTest.appendCommandLineFlags(flags);
}
- /** Deletes a file or directory along with any of its children.
- *
- * Note that, like File.delete(), this returns false if the file or directory couldn't be
- * fully deleted. This means that, in the directory case, some files may be deleted even if
- * the entire directory couldn't be.
- *
- * @param file The file or directory to delete.
- * @return Whether or not the file or directory was deleted.
- */
- private static boolean deleteRecursive(File file) {
- if (file == null) return true;
-
- File[] children;
- try (StrictModeContext unused = StrictModeContext.allowDiskReads()) {
- children = file.listFiles();
- }
- if (children != null) {
- for (File child : children) {
- if (!deleteRecursive(child)) {
- return false;
- }
- }
- }
- try (StrictModeContext unused = StrictModeContext.allowDiskWrites()) {
- return file.delete();
- }
- }
-
- private void deletePrivateDataDirectory() {
- File privateDataDirectory = getPrivateDataDirectory();
- if (!deleteRecursive(privateDataDirectory)) {
- Log.e(TAG, "Failed to remove %s", privateDataDirectory.getAbsolutePath());
- }
- }
-
/** Returns the test suite's private data directory. */
protected abstract File getPrivateDataDirectory();