Extract NativeLibraryTestRule logic to static util class
NativeLibraryTestRule doesn't use any TestRule behavior, so refactor it to a static util class so that it can be more easily referenced by other TestRules. Original class kept as a wrapper class until all usages (including downstream) are removed in follow-up CLs. Bug: 1096666 Change-Id: Ieacec3cc04b34be321b6eaa65f6c9a524b7f71c7 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2251099 Reviewed-by: Tommy Nyquist <nyquist@chromium.org> Commit-Queue: Natalie Chouinard <chouinard@chromium.org> Cr-Commit-Position: refs/heads/master@{#780341}
This commit is contained in:

committed by
Commit Bot

parent
520fb3f2cd
commit
b8f72c7b42
content/public/test/android
@ -30,6 +30,7 @@ android_library("content_java_test_support") {
|
||||
"javatests/src/org/chromium/content_public/browser/test/ChildProcessAllocatorSettingsHook.java",
|
||||
"javatests/src/org/chromium/content_public/browser/test/ContentJUnit4ClassRunner.java",
|
||||
"javatests/src/org/chromium/content_public/browser/test/NativeLibraryTestRule.java",
|
||||
"javatests/src/org/chromium/content_public/browser/test/NativeLibraryTestUtils.java",
|
||||
"javatests/src/org/chromium/content_public/browser/test/RenderFrameHostTestExt.java",
|
||||
"javatests/src/org/chromium/content_public/browser/test/mock/MockNavigationController.java",
|
||||
"javatests/src/org/chromium/content_public/browser/test/mock/MockRenderFrameHost.java",
|
||||
|
@ -4,29 +4,25 @@
|
||||
|
||||
package org.chromium.content_public.browser.test;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
import org.chromium.base.ThreadUtils;
|
||||
import org.chromium.base.library_loader.LibraryLoader;
|
||||
import org.chromium.base.library_loader.LibraryProcessType;
|
||||
import org.chromium.content_public.browser.BrowserStartupController;
|
||||
import org.chromium.content_public.browser.UiThreadTaskTraits;
|
||||
import org.chromium.ui.resources.ResourceExtractor;
|
||||
|
||||
/**
|
||||
* TestRule that adds support for loading and dealing with native libraries.
|
||||
*
|
||||
* NativeLibraryTestRule does not interact with any Activity.
|
||||
*
|
||||
* TODO(crbug.com/1096666): Remove this class in favor of using {@link NativeLibraryTestUtils}
|
||||
* directly.
|
||||
*/
|
||||
@Deprecated
|
||||
public class NativeLibraryTestRule implements TestRule {
|
||||
/**
|
||||
* Loads the native library on the activity UI thread (must not be called from the UI thread).
|
||||
*/
|
||||
public void loadNativeLibraryNoBrowserProcess() {
|
||||
handleNativeInitialization(false);
|
||||
NativeLibraryTestUtils.loadNativeLibraryNoBrowserProcess();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -34,32 +30,7 @@ public class NativeLibraryTestRule implements TestRule {
|
||||
* 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());
|
||||
|
||||
// LibraryLoader is not in general multithreaded; as other InstrumentationTestCase code
|
||||
// (specifically, ChromeBrowserProvider) uses it from the main thread we must do
|
||||
// likewise.
|
||||
ThreadUtils.runOnUiThreadBlocking(() -> { nativeInitialization(initBrowserProcess); });
|
||||
}
|
||||
|
||||
private void nativeInitialization(boolean initBrowserProcess) {
|
||||
LibraryLoader.getInstance().setLibraryProcessType(LibraryProcessType.PROCESS_BROWSER);
|
||||
if (initBrowserProcess) {
|
||||
// Extract compressed resource paks.
|
||||
ResourceExtractor resourceExtractor = ResourceExtractor.get();
|
||||
resourceExtractor.setResultTraits(UiThreadTaskTraits.BOOTSTRAP);
|
||||
resourceExtractor.startExtractingResources("en");
|
||||
resourceExtractor.waitForCompletion();
|
||||
|
||||
BrowserStartupController.getInstance().startBrowserProcessesSync(
|
||||
LibraryProcessType.PROCESS_BROWSER, false);
|
||||
} else {
|
||||
LibraryLoader.getInstance().ensureInitialized();
|
||||
}
|
||||
NativeLibraryTestUtils.loadNativeLibraryAndInitBrowserProcess();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
59
content/public/test/android/javatests/src/org/chromium/content_public/browser/test/NativeLibraryTestUtils.java
Normal file
59
content/public/test/android/javatests/src/org/chromium/content_public/browser/test/NativeLibraryTestUtils.java
Normal file
@ -0,0 +1,59 @@
|
||||
// Copyright 2020 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_public.browser.test;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import org.chromium.base.ThreadUtils;
|
||||
import org.chromium.base.library_loader.LibraryLoader;
|
||||
import org.chromium.base.library_loader.LibraryProcessType;
|
||||
import org.chromium.content_public.browser.BrowserStartupController;
|
||||
import org.chromium.content_public.browser.UiThreadTaskTraits;
|
||||
import org.chromium.ui.resources.ResourceExtractor;
|
||||
|
||||
/**
|
||||
* Provides test support for loading and dealing with native libraries.
|
||||
*/
|
||||
public class NativeLibraryTestUtils {
|
||||
/**
|
||||
* Loads the native library on the activity UI thread (must not be called from the UI thread).
|
||||
*/
|
||||
public static 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 static void loadNativeLibraryAndInitBrowserProcess() {
|
||||
handleNativeInitialization(true);
|
||||
}
|
||||
|
||||
private static void handleNativeInitialization(final boolean initBrowserProcess) {
|
||||
Assert.assertFalse(ThreadUtils.runningOnUiThread());
|
||||
|
||||
// LibraryLoader is not in general multithreaded; as other InstrumentationTestCase code
|
||||
// (specifically, ChromeBrowserProvider) uses it from the main thread we must do
|
||||
// likewise.
|
||||
ThreadUtils.runOnUiThreadBlocking(() -> { nativeInitialization(initBrowserProcess); });
|
||||
}
|
||||
|
||||
private static void nativeInitialization(boolean initBrowserProcess) {
|
||||
LibraryLoader.getInstance().setLibraryProcessType(LibraryProcessType.PROCESS_BROWSER);
|
||||
if (initBrowserProcess) {
|
||||
// Extract compressed resource paks.
|
||||
ResourceExtractor resourceExtractor = ResourceExtractor.get();
|
||||
resourceExtractor.setResultTraits(UiThreadTaskTraits.BOOTSTRAP);
|
||||
resourceExtractor.startExtractingResources("en");
|
||||
resourceExtractor.waitForCompletion();
|
||||
|
||||
BrowserStartupController.getInstance().startBrowserProcessesSync(
|
||||
LibraryProcessType.PROCESS_BROWSER, false);
|
||||
} else {
|
||||
LibraryLoader.getInstance().ensureInitialized();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user