Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 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.chrome.browser.test; | |
| 6 | |
| 7 import android.app.Instrumentation; | |
| 8 import android.support.test.InstrumentationRegistry; | |
| 9 | |
| 10 import org.junit.Assert; | |
| 11 import org.junit.rules.TestRule; | |
| 12 import org.junit.runner.Description; | |
| 13 import org.junit.runners.model.Statement; | |
| 14 | |
| 15 import org.chromium.base.PathUtils; | |
| 16 import org.chromium.base.ThreadUtils; | |
| 17 import org.chromium.base.library_loader.LibraryLoader; | |
| 18 import org.chromium.base.library_loader.LibraryProcessType; | |
| 19 import org.chromium.base.library_loader.ProcessInitException; | |
| 20 import org.chromium.chrome.browser.webapps.ChromeWebApkHost; | |
| 21 import org.chromium.chrome.test.util.ApplicationData; | |
| 22 import org.chromium.chrome.test.util.browser.signin.SigninTestUtil; | |
| 23 import org.chromium.content.browser.BrowserStartupController; | |
| 24 import org.chromium.content.browser.test.util.ApplicationUtils; | |
| 25 | |
| 26 /** | |
| 27 * JUnit test rule that takes care of important initialization for Chrome-specif ic tests, such as | |
| 28 * initializing the AccountManagerHelper. | |
| 29 */ | |
| 30 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.
| |
| 31 private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "chrome"; | |
| 32 | |
| 33 private final boolean mInitBrowserProcess; | |
| 34 | |
| 35 public ChromeBrowserTestRule(boolean initBrowserProcess) { | |
| 36 mInitBrowserProcess = initBrowserProcess; | |
| 37 } | |
| 38 | |
| 39 void handleNativeInitialization( | |
| 40 final boolean initBrowserProcess, Instrumentation instrumentation) { | |
| 41 Assert.assertFalse(ThreadUtils.runningOnUiThread()); | |
| 42 | |
| 43 PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX); | |
| 44 ApplicationUtils.waitForLibraryDependencies(instrumentation); | |
| 45 ChromeWebApkHost.initForTesting(false); | |
| 46 SigninTestUtil.setUpAuthForTest(instrumentation); | |
| 47 ApplicationData.clearAppData(instrumentation.getContext()); | |
| 48 | |
| 49 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
| 50 @Override | |
| 51 public void run() { | |
| 52 nativeInitialization(initBrowserProcess); | |
| 53 } | |
| 54 }); | |
| 55 } | |
| 56 | |
| 57 void nativeInitialization(boolean initBrowserProcess) { | |
| 58 if (initBrowserProcess) { | |
| 59 try { | |
| 60 BrowserStartupController.get(LibraryProcessType.PROCESS_BROWSER) | |
| 61 .startBrowserProcessesSync(false); | |
| 62 } catch (ProcessInitException e) { | |
| 63 throw new Error(e); | |
| 64 } | |
| 65 } else { | |
| 66 try { | |
| 67 LibraryLoader.get(LibraryProcessType.PROCESS_BROWSER).ensureInit ialized(); | |
| 68 } catch (ProcessInitException e) { | |
| 69 throw new Error(e); | |
| 70 } | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 @Override | |
| 75 public Statement apply(Statement base, Description description) { | |
| 76 return new Statement() { | |
| 77 @Override | |
| 78 public void evaluate() throws Throwable { | |
| 79 /** | |
| 80 * Loads the native library on the activity UI thread (must not be called from the | |
| 81 * UI thread). After loading the library, this will initialize the browser process | |
| 82 * if necessary. | |
| 83 */ | |
| 84 handleNativeInitialization( | |
| 85 mInitBrowserProcess, InstrumentationRegistry.getInstrume ntation()); | |
| 86 } | |
| 87 }; | |
| 88 } | |
| 89 | |
| 90 public void tearDown() { | |
| 91 SigninTestUtil.resetSigninState(); | |
| 92 SigninTestUtil.tearDownAuthForTest(); | |
| 93 } | |
| 94 } | |
| OLD | NEW |