Commit aebed7cb authored by Yoland Yan's avatar Yoland Yan Committed by Commit Bot

Merge NativeLibraryTestCommon implementation to NativeLibraryTestRule

Merge TestCommon into TestRule since there is no long need for sharing
the implementation with other classes

Bug: 711517
Change-Id: I40f53f07d734112fb85fb2bf8d1efe1ad4752097
Reviewed-on: https://chromium-review.googlesource.com/887747Reviewed-by: default avatarBo <boliu@chromium.org>
Commit-Queue: Yoland Yan <yolandyan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#532580}
parent 7d22d62d
...@@ -34,7 +34,6 @@ android_library("content_java_test_support") { ...@@ -34,7 +34,6 @@ android_library("content_java_test_support") {
"javatests/src/org/chromium/content/browser/test/ChildProcessAllocatorSettingsHook.java", "javatests/src/org/chromium/content/browser/test/ChildProcessAllocatorSettingsHook.java",
"javatests/src/org/chromium/content/browser/test/ContentInstrumentationTestRunner.java", "javatests/src/org/chromium/content/browser/test/ContentInstrumentationTestRunner.java",
"javatests/src/org/chromium/content/browser/test/ContentJUnit4ClassRunner.java", "javatests/src/org/chromium/content/browser/test/ContentJUnit4ClassRunner.java",
"javatests/src/org/chromium/content/browser/test/NativeLibraryTestCommon.java",
"javatests/src/org/chromium/content/browser/test/NativeLibraryTestRule.java", "javatests/src/org/chromium/content/browser/test/NativeLibraryTestRule.java",
"javatests/src/org/chromium/content/browser/test/util/ClickUtils.java", "javatests/src/org/chromium/content/browser/test/util/ClickUtils.java",
"javatests/src/org/chromium/content/browser/test/util/Coordinates.java", "javatests/src/org/chromium/content/browser/test/util/Coordinates.java",
......
// 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.app.Instrumentation;
import org.junit.Assert;
import org.chromium.base.PathUtils;
import org.chromium.base.ResourceExtractor;
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;
class NativeLibraryTestCommon {
private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "content";
void handleNativeInitialization(
final boolean initBrowserProcess, Instrumentation instrumentation) {
Assert.assertFalse(ThreadUtils.runningOnUiThread());
PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX);
// 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);
}
});
}
void nativeInitialization(boolean initBrowserProcess) {
if (initBrowserProcess) {
try {
// Extract compressed resource paks.
ResourceExtractor resourceExtractor = ResourceExtractor.get();
resourceExtractor.startExtractingResources();
resourceExtractor.waitForCompletion();
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);
}
}
}
}
...@@ -4,25 +4,33 @@ ...@@ -4,25 +4,33 @@
package org.chromium.content.browser.test; package org.chromium.content.browser.test;
import android.support.test.InstrumentationRegistry; import org.junit.Assert;
import org.junit.rules.TestRule; import org.junit.rules.TestRule;
import org.junit.runner.Description; import org.junit.runner.Description;
import org.junit.runners.model.Statement; import org.junit.runners.model.Statement;
import org.chromium.base.PathUtils;
import org.chromium.base.ResourceExtractor;
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;
/** /**
* TestRule that adds support for loading and dealing with native libraries. * TestRule that adds support for loading and dealing with native libraries.
* *
* NativeLibraryTestRule does not interact with any Activity. * NativeLibraryTestRule does not interact with any Activity.
*/ */
public class NativeLibraryTestRule implements TestRule { public class NativeLibraryTestRule implements TestRule {
private final NativeLibraryTestCommon mTestCommon = new NativeLibraryTestCommon(); 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). * Loads the native library on the activity UI thread (must not be called from the UI thread).
*/ */
public void loadNativeLibraryNoBrowserProcess() { public void loadNativeLibraryNoBrowserProcess() {
mTestCommon.handleNativeInitialization(false, InstrumentationRegistry.getInstrumentation()); handleNativeInitialization(false);
} }
/** /**
...@@ -30,7 +38,45 @@ public class NativeLibraryTestRule implements TestRule { ...@@ -30,7 +38,45 @@ public class NativeLibraryTestRule implements TestRule {
* After loading the library, this will initialize the browser process. * After loading the library, this will initialize the browser process.
*/ */
public void loadNativeLibraryAndInitBrowserProcess() { public void loadNativeLibraryAndInitBrowserProcess() {
mTestCommon.handleNativeInitialization(true, InstrumentationRegistry.getInstrumentation()); handleNativeInitialization(true);
}
private void handleNativeInitialization(final boolean initBrowserProcess) {
Assert.assertFalse(ThreadUtils.runningOnUiThread());
PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX);
// 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 {
// Extract compressed resource paks.
ResourceExtractor resourceExtractor = ResourceExtractor.get();
resourceExtractor.startExtractingResources();
resourceExtractor.waitForCompletion();
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 @Override
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment