Commit c32d8952 authored by Mohamed Heikal's avatar Mohamed Heikal Committed by Commit Bot

[Android] Create a ReducedModeNativeTestRule

Currently browsertests that require native, load it using
ChromeTabbedActivityTestRule#startMainActivityOnBlankPage() which loads
the native library in full browser mode as well as load the chrome
tabbed UI.

ReducedModeNativeTestRule is a reduced mode counterpart to that. It only
loads native in reduced mode and without UI.

Bug: 959957
Change-Id: I76dabc8f4fc4cc702e9193bf69486198ba8dfd3b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1615984
Commit-Queue: Mohamed Heikal <mheikal@chromium.org>
Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Reviewed-by: default avatarCarlos Knippschild <carlosk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#667155}
parent ea5d6db4
...@@ -15,7 +15,6 @@ import org.chromium.base.library_loader.ProcessInitException; ...@@ -15,7 +15,6 @@ import org.chromium.base.library_loader.ProcessInitException;
import org.chromium.chrome.browser.init.BrowserParts; import org.chromium.chrome.browser.init.BrowserParts;
import org.chromium.chrome.browser.init.ChromeBrowserInitializer; import org.chromium.chrome.browser.init.ChromeBrowserInitializer;
import org.chromium.chrome.browser.init.EmptyBrowserParts; import org.chromium.chrome.browser.init.EmptyBrowserParts;
import org.chromium.chrome.browser.init.ServiceManagerStartupUtils;
import org.chromium.content_public.browser.BrowserStartupController; import org.chromium.content_public.browser.BrowserStartupController;
import org.chromium.content_public.browser.test.util.Criteria; import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper; import org.chromium.content_public.browser.test.util.CriteriaHelper;
...@@ -29,13 +28,6 @@ public class ServicificationBackgroundService extends ChromeBackgroundService { ...@@ -29,13 +28,6 @@ public class ServicificationBackgroundService extends ChromeBackgroundService {
private boolean mNativeLoaded; private boolean mNativeLoaded;
private boolean mSupportsServiceManagerOnly; private boolean mSupportsServiceManagerOnly;
public static void launchChromeInBackground(boolean serviceManagerOnlyMode) {
ServicificationBackgroundService service =
new ServicificationBackgroundService(serviceManagerOnlyMode);
service.onRunTask(new TaskParams(ServiceManagerStartupUtils.TASK_TAG));
service.waitForNativeLoaded();
}
public ServicificationBackgroundService(boolean supportsServiceManagerOnly) { public ServicificationBackgroundService(boolean supportsServiceManagerOnly) {
mSupportsServiceManagerOnly = supportsServiceManagerOnly; mSupportsServiceManagerOnly = supportsServiceManagerOnly;
} }
......
...@@ -8,8 +8,6 @@ import android.graphics.Bitmap; ...@@ -8,8 +8,6 @@ import android.graphics.Bitmap;
import android.support.test.InstrumentationRegistry; import android.support.test.InstrumentationRegistry;
import android.support.test.filters.LargeTest; import android.support.test.filters.LargeTest;
import com.google.android.gms.gcm.TaskParams;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
...@@ -21,9 +19,8 @@ import org.chromium.base.test.util.CommandLineFlags; ...@@ -21,9 +19,8 @@ import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.RetryOnFailure; import org.chromium.base.test.util.RetryOnFailure;
import org.chromium.base.test.util.TestFileUtil; import org.chromium.base.test.util.TestFileUtil;
import org.chromium.chrome.browser.ServicificationBackgroundService;
import org.chromium.chrome.browser.init.ServiceManagerStartupUtils;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner; import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.ReducedModeNativeTestRule;
import org.chromium.components.offline_items_collection.ContentId; import org.chromium.components.offline_items_collection.ContentId;
import org.chromium.content_public.browser.test.util.Criteria; import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper; import org.chromium.content_public.browser.test.util.CriteriaHelper;
...@@ -38,10 +35,11 @@ import org.chromium.net.test.EmbeddedTestServerRule; ...@@ -38,10 +35,11 @@ import org.chromium.net.test.EmbeddedTestServerRule;
public final class ServicificationDownloadTest { public final class ServicificationDownloadTest {
@Rule @Rule
public EmbeddedTestServerRule mEmbeddedTestServerRule = new EmbeddedTestServerRule(); public EmbeddedTestServerRule mEmbeddedTestServerRule = new EmbeddedTestServerRule();
@Rule
public ReducedModeNativeTestRule mNativeTestRule = new ReducedModeNativeTestRule();
private static final String TEST_DOWNLOAD_FILE = "/chrome/test/data/android/download/test.gzip"; private static final String TEST_DOWNLOAD_FILE = "/chrome/test/data/android/download/test.gzip";
private static final String DOWNLOAD_GUID = "F7FB1F59-7DE1-4845-AFDB-8A688F70F583"; private static final String DOWNLOAD_GUID = "F7FB1F59-7DE1-4845-AFDB-8A688F70F583";
private ServicificationBackgroundService mServicificationBackgroundService;
private MockDownloadNotificationService mNotificationService; private MockDownloadNotificationService mNotificationService;
static class MockDownloadNotificationService extends DownloadNotificationService { static class MockDownloadNotificationService extends DownloadNotificationService {
...@@ -70,8 +68,6 @@ public final class ServicificationDownloadTest { ...@@ -70,8 +68,6 @@ public final class ServicificationDownloadTest {
@Before @Before
public void setUp() throws InterruptedException { public void setUp() throws InterruptedException {
RecordHistogram.setDisabledForTests(true); RecordHistogram.setDisabledForTests(true);
mServicificationBackgroundService =
new ServicificationBackgroundService(true /*supportsServiceManagerOnly*/);
TestThreadUtils.runOnUiThreadBlocking( TestThreadUtils.runOnUiThreadBlocking(
() -> { mNotificationService = new MockDownloadNotificationService(); }); () -> { mNotificationService = new MockDownloadNotificationService(); });
} }
...@@ -86,10 +82,7 @@ public final class ServicificationDownloadTest { ...@@ -86,10 +82,7 @@ public final class ServicificationDownloadTest {
@Feature({"Download"}) @Feature({"Download"})
@CommandLineFlags.Add({"enable-features=NetworkService,AllowStartingServiceManagerOnly"}) @CommandLineFlags.Add({"enable-features=NetworkService,AllowStartingServiceManagerOnly"})
public void testResumeInterruptedDownload() { public void testResumeInterruptedDownload() {
mServicificationBackgroundService.onRunTask( mNativeTestRule.assertOnlyServiceManagerStarted();
new TaskParams(ServiceManagerStartupUtils.TASK_TAG));
mServicificationBackgroundService.waitForNativeLoaded();
ServicificationBackgroundService.assertOnlyServiceManagerStarted();
String tempFile = InstrumentationRegistry.getInstrumentation() String tempFile = InstrumentationRegistry.getInstrumentation()
.getTargetContext() .getTargetContext()
......
...@@ -13,6 +13,7 @@ import android.support.test.filters.SmallTest; ...@@ -13,6 +13,7 @@ import android.support.test.filters.SmallTest;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -21,9 +22,9 @@ import org.chromium.base.task.PostTask; ...@@ -21,9 +22,9 @@ import org.chromium.base.task.PostTask;
import org.chromium.base.test.util.CommandLineFlags; import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.DisabledTest; import org.chromium.base.test.util.DisabledTest;
import org.chromium.chrome.browser.ChromeSwitches; import org.chromium.chrome.browser.ChromeSwitches;
import org.chromium.chrome.browser.ServicificationBackgroundService;
import org.chromium.chrome.browser.offlinepages.OfflineTestUtil; import org.chromium.chrome.browser.offlinepages.OfflineTestUtil;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner; import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.ReducedModeNativeTestRule;
import org.chromium.components.background_task_scheduler.BackgroundTask.TaskFinishedCallback; import org.chromium.components.background_task_scheduler.BackgroundTask.TaskFinishedCallback;
import org.chromium.components.background_task_scheduler.BackgroundTaskScheduler; import org.chromium.components.background_task_scheduler.BackgroundTaskScheduler;
import org.chromium.components.background_task_scheduler.BackgroundTaskSchedulerFactory; import org.chromium.components.background_task_scheduler.BackgroundTaskSchedulerFactory;
...@@ -44,6 +45,9 @@ import java.util.concurrent.TimeUnit; ...@@ -44,6 +45,9 @@ import java.util.concurrent.TimeUnit;
"enable-features=OfflinePagesPrefetching,NetworkService,AllowStartingServiceManagerOnly," "enable-features=OfflinePagesPrefetching,NetworkService,AllowStartingServiceManagerOnly,"
+ "InterestFeedContentSuggestions"}) + "InterestFeedContentSuggestions"})
public class PrefetchBackgroundTaskTest { public class PrefetchBackgroundTaskTest {
@Rule
public ReducedModeNativeTestRule mNativeTestRule = new ReducedModeNativeTestRule();
private static final double BACKOFF_JITTER_FACTOR = 0.33; private static final double BACKOFF_JITTER_FACTOR = 0.33;
private static final int SEMAPHORE_TIMEOUT_MS = 5000; private static final int SEMAPHORE_TIMEOUT_MS = 5000;
private static final String GCM_TOKEN = "dummy_gcm_token"; private static final String GCM_TOKEN = "dummy_gcm_token";
...@@ -183,9 +187,6 @@ public class PrefetchBackgroundTaskTest { ...@@ -183,9 +187,6 @@ public class PrefetchBackgroundTaskTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
ServicificationBackgroundService.launchChromeInBackground(true /*serviceManagerOnlyMode*/);
ServicificationBackgroundService.assertOnlyServiceManagerStarted();
TestThreadUtils.runOnUiThreadBlocking(() -> { TestThreadUtils.runOnUiThreadBlocking(() -> {
mScheduler = new TestBackgroundTaskScheduler(); mScheduler = new TestBackgroundTaskScheduler();
BackgroundTaskSchedulerFactory.setSchedulerForTesting(mScheduler); BackgroundTaskSchedulerFactory.setSchedulerForTesting(mScheduler);
...@@ -197,7 +198,7 @@ public class PrefetchBackgroundTaskTest { ...@@ -197,7 +198,7 @@ public class PrefetchBackgroundTaskTest {
@After @After
public void tearDown() { public void tearDown() {
ServicificationBackgroundService.assertOnlyServiceManagerStarted(); mNativeTestRule.assertOnlyServiceManagerStarted();
} }
private void scheduleTask(int additionalDelaySeconds) { private void scheduleTask(int additionalDelaySeconds) {
......
...@@ -9,7 +9,7 @@ import static org.junit.Assert.assertTrue; ...@@ -9,7 +9,7 @@ import static org.junit.Assert.assertTrue;
import android.support.test.filters.MediumTest; import android.support.test.filters.MediumTest;
import org.junit.Before; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -18,9 +18,9 @@ import org.chromium.base.test.util.Feature; ...@@ -18,9 +18,9 @@ import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.Restriction; import org.chromium.base.test.util.Restriction;
import org.chromium.chrome.browser.ChromeFeatureList; import org.chromium.chrome.browser.ChromeFeatureList;
import org.chromium.chrome.browser.ChromeSwitches; import org.chromium.chrome.browser.ChromeSwitches;
import org.chromium.chrome.browser.ServicificationBackgroundService;
import org.chromium.chrome.browser.offlinepages.OfflineTestUtil; import org.chromium.chrome.browser.offlinepages.OfflineTestUtil;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner; import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.ReducedModeNativeTestRule;
import org.chromium.chrome.test.util.ChromeRestriction; import org.chromium.chrome.test.util.ChromeRestriction;
import org.chromium.chrome.test.util.browser.Features; import org.chromium.chrome.test.util.browser.Features;
import org.chromium.content_public.browser.test.util.TestThreadUtils; import org.chromium.content_public.browser.test.util.TestThreadUtils;
...@@ -32,11 +32,8 @@ import java.util.concurrent.atomic.AtomicBoolean; ...@@ -32,11 +32,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
@Restriction({ChromeRestriction.RESTRICTION_TYPE_REQUIRES_TOUCH}) @Restriction({ChromeRestriction.RESTRICTION_TYPE_REQUIRES_TOUCH})
@RunWith(ChromeJUnit4ClassRunner.class) @RunWith(ChromeJUnit4ClassRunner.class)
public class PrefetchConfigurationTest { public class PrefetchConfigurationTest {
@Before @Rule
public void setUp() throws Exception { public ReducedModeNativeTestRule mNativeTestRule = new ReducedModeNativeTestRule();
// Start Chrome.
ServicificationBackgroundService.launchChromeInBackground(true /*serviceManagerOnlyMode*/);
}
@Test @Test
@MediumTest @MediumTest
......
...@@ -59,6 +59,7 @@ android_library("chrome_java_test_support") { ...@@ -59,6 +59,7 @@ android_library("chrome_java_test_support") {
"javatests/src/org/chromium/chrome/test/omaha/MockRequestGenerator.java", "javatests/src/org/chromium/chrome/test/omaha/MockRequestGenerator.java",
"javatests/src/org/chromium/chrome/test/partnercustomizations/TestPartnerBrowserCustomizationsDelayedProvider.java", "javatests/src/org/chromium/chrome/test/partnercustomizations/TestPartnerBrowserCustomizationsDelayedProvider.java",
"javatests/src/org/chromium/chrome/test/partnercustomizations/TestPartnerBrowserCustomizationsProvider.java", "javatests/src/org/chromium/chrome/test/partnercustomizations/TestPartnerBrowserCustomizationsProvider.java",
"javatests/src/org/chromium/chrome/test/ReducedModeNativeTestRule.java",
"javatests/src/org/chromium/chrome/test/TestContentProvider.java", "javatests/src/org/chromium/chrome/test/TestContentProvider.java",
"javatests/src/org/chromium/chrome/test/util/ActivityUtils.java", "javatests/src/org/chromium/chrome/test/util/ActivityUtils.java",
"javatests/src/org/chromium/chrome/test/util/ApplicationTestUtils.java", "javatests/src/org/chromium/chrome/test/util/ApplicationTestUtils.java",
......
// Copyright 2019 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.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.library_loader.LibraryProcessType;
import org.chromium.base.library_loader.ProcessInitException;
import org.chromium.base.task.PostTask;
import org.chromium.chrome.browser.ChromeApplication;
import org.chromium.chrome.browser.ChromeFeatureList;
import org.chromium.chrome.browser.init.BrowserParts;
import org.chromium.chrome.browser.init.ChromeBrowserInitializer;
import org.chromium.chrome.browser.init.EmptyBrowserParts;
import org.chromium.chrome.test.util.browser.Features;
import org.chromium.content_public.browser.BrowserStartupController;
import org.chromium.content_public.browser.UiThreadTaskTraits;
import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Custom {@link TestRule} for test using native in reduced mode. This also enables the flags
* required for reduced mode to work.
*/
public class ReducedModeNativeTestRule implements TestRule {
private final AtomicBoolean mNativeLoaded = new AtomicBoolean();
private final boolean mAutoLoadNative;
public ReducedModeNativeTestRule() {
this(true /*autoLoadNative*/);
}
public ReducedModeNativeTestRule(boolean autoLoadNative) {
mAutoLoadNative = autoLoadNative;
}
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Features.getInstance().enable(ChromeFeatureList.NETWORK_SERVICE,
ChromeFeatureList.ALLOW_STARTING_SERVICE_MANAGER_ONLY);
// Unfortunately, Features has already formed the command line at this point. This
// forces another redo after we added new flags.
Features.ensureCommandLineIsUpToDate();
if (mAutoLoadNative) {
loadNative();
}
base.evaluate();
}
};
}
public void loadNative() {
final BrowserParts parts = new EmptyBrowserParts() {
@Override
public void finishNativeInitialization() {
mNativeLoaded.set(true);
}
@Override
public boolean startServiceManagerOnly() {
return true;
}
};
PostTask.postTask(UiThreadTaskTraits.DEFAULT, () -> {
try {
ChromeBrowserInitializer.getInstance().handlePreNativeStartup(parts);
ChromeBrowserInitializer.getInstance().handlePostNativeStartup(true, parts);
} catch (ProcessInitException e) {
ChromeApplication.reportStartupErrorAndExit(e);
}
});
waitForNativeLoaded();
}
private void waitForNativeLoaded() {
CriteriaHelper.pollUiThread(
new Criteria("Failed while waiting for starting Service Manager.") {
@Override
public boolean isSatisfied() {
return mNativeLoaded.get();
}
});
}
public void assertOnlyServiceManagerStarted() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
Assert.assertTrue("The native service manager has not been started.",
BrowserStartupController.get(LibraryProcessType.PROCESS_BROWSER)
.isServiceManagerSuccessfullyStarted());
Assert.assertFalse("The full browser is started instead of ServiceManager only.",
BrowserStartupController.get(LibraryProcessType.PROCESS_BROWSER)
.isStartupSuccessfullyCompleted());
});
}
}
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