Index: content/public/test/android/javatests/src/org/chromium/content/browser/test/NativeLibraryTestRule.java |
diff --git a/content/public/test/android/javatests/src/org/chromium/content/browser/test/NativeLibraryTestRule.java b/content/public/test/android/javatests/src/org/chromium/content/browser/test/NativeLibraryTestRule.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1c75d4370fd71a592839de0faa05772cabbe6d04 |
--- /dev/null |
+++ b/content/public/test/android/javatests/src/org/chromium/content/browser/test/NativeLibraryTestRule.java |
@@ -0,0 +1,87 @@ |
+// Copyright 2017 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.content.browser.test; |
+ |
+import android.support.test.InstrumentationRegistry; |
+ |
+import org.junit.Assert; |
+import org.junit.rules.TestRule; |
+import org.junit.runner.Description; |
+import org.junit.runners.model.Statement; |
+ |
+import org.chromium.base.PathUtils; |
+import org.chromium.base.ThreadUtils; |
+import org.chromium.base.library_loader.LibraryLoader; |
+import org.chromium.base.library_loader.LibraryProcessType; |
+import org.chromium.base.library_loader.ProcessInitException; |
+import org.chromium.content.browser.BrowserStartupController; |
+import org.chromium.content.browser.test.util.ApplicationUtils; |
+ |
+/** |
+ * TestRule that adds support for loading and dealing with native libraries. |
+ */ |
+public class NativeLibraryTestRule implements TestRule { |
+ private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "content"; |
+ |
+ /** |
+ * Loads the native library on the activity UI thread (must not be called from the UI thread). |
+ */ |
+ public void loadNativeLibraryNoBrowserProcess() { |
+ handleNativeInitialization(false); |
+ } |
+ |
+ /** |
+ * Loads the native library on the activity UI thread (must not be called from the UI thread). |
+ * After loading the library, this will initialize the browser process. |
+ */ |
+ public void loadNativeLibraryAndInitBrowserProcess() { |
+ handleNativeInitialization(true); |
+ } |
+ |
+ private void handleNativeInitialization(final boolean initBrowserProcess) { |
+ Assert.assertFalse(ThreadUtils.runningOnUiThread()); |
+ |
+ PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX); |
+ |
+ ApplicationUtils.waitForLibraryDependencies(InstrumentationRegistry.getInstrumentation()); |
+ |
+ // LibraryLoader is not in general multithreaded; as other InstrumentationTestCase code |
+ // (specifically, ChromeBrowserProvider) uses it from the main thread we must do |
+ // likewise. |
+ ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
+ @Override |
+ public void run() { |
+ nativeInitialization(initBrowserProcess); |
+ } |
+ }); |
+ } |
+ |
+ private void nativeInitialization(boolean initBrowserProcess) { |
+ if (initBrowserProcess) { |
+ try { |
+ BrowserStartupController.get(LibraryProcessType.PROCESS_BROWSER) |
+ .startBrowserProcessesSync(false); |
+ } catch (ProcessInitException e) { |
+ throw new Error(e); |
+ } |
+ } else { |
+ try { |
+ LibraryLoader.get(LibraryProcessType.PROCESS_BROWSER).ensureInitialized(); |
+ } catch (ProcessInitException e) { |
+ throw new Error(e); |
+ } |
+ } |
+ } |
+ |
+ @Override |
+ public Statement apply(Statement base, Description description) { |
+ return new Statement() { |
+ @Override |
+ public void evaluate() throws Throwable { |
+ base.evaluate(); |
+ } |
+ }; |
+ } |
+} |