Chromium Code Reviews| Index: chrome/android/javatests/src/org/chromium/chrome/browser/test/ChromeBrowserTestRule.java |
| diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/test/ChromeBrowserTestRule.java b/chrome/android/javatests/src/org/chromium/chrome/browser/test/ChromeBrowserTestRule.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ce647f18ed378585c22b48f751bfda8dbd19b65a |
| --- /dev/null |
| +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/test/ChromeBrowserTestRule.java |
| @@ -0,0 +1,94 @@ |
| +// 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.chrome.browser.test; |
| + |
| +import android.app.Instrumentation; |
| +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.chrome.browser.webapps.ChromeWebApkHost; |
| +import org.chromium.chrome.test.util.ApplicationData; |
| +import org.chromium.chrome.test.util.browser.signin.SigninTestUtil; |
| +import org.chromium.content.browser.BrowserStartupController; |
| +import org.chromium.content.browser.test.util.ApplicationUtils; |
| + |
| +/** |
| + * JUnit test rule that takes care of important initialization for Chrome-specific tests, such as |
| + * initializing the AccountManagerHelper. |
| + */ |
| +public class ChromeBrowserTestRule implements TestRule { |
|
Maria
2017/05/17 00:29:18
Seems like this ought to inherit from NativeLibrar
troyhildebrandt
2017/05/17 20:19:10
Done.
|
| + private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "chrome"; |
| + |
| + private final boolean mInitBrowserProcess; |
| + |
| + public ChromeBrowserTestRule(boolean initBrowserProcess) { |
| + mInitBrowserProcess = initBrowserProcess; |
| + } |
| + |
| + void handleNativeInitialization( |
| + final boolean initBrowserProcess, Instrumentation instrumentation) { |
| + Assert.assertFalse(ThreadUtils.runningOnUiThread()); |
| + |
| + PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX); |
| + ApplicationUtils.waitForLibraryDependencies(instrumentation); |
| + ChromeWebApkHost.initForTesting(false); |
| + SigninTestUtil.setUpAuthForTest(instrumentation); |
| + ApplicationData.clearAppData(instrumentation.getContext()); |
| + |
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| + @Override |
| + public void run() { |
| + nativeInitialization(initBrowserProcess); |
| + } |
| + }); |
| + } |
| + |
| + 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 { |
| + /** |
| + * 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 |
| + * if necessary. |
| + */ |
| + handleNativeInitialization( |
| + mInitBrowserProcess, InstrumentationRegistry.getInstrumentation()); |
| + } |
| + }; |
| + } |
| + |
| + public void tearDown() { |
| + SigninTestUtil.resetSigninState(); |
| + SigninTestUtil.tearDownAuthForTest(); |
| + } |
| +} |