Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5092)

Unified Diff: chrome/android/junit/src/org/chromium/chrome/browser/offlinepages/BackgroundOfflinerTaskTest.java

Issue 2164503003: [OfflinePages] Do not start background loading on low-end devices if app has any visible Activities. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Resets the SysUtils and ApplicationStatus static state so it does not leak to other robolectric tes… Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/junit/src/org/chromium/chrome/browser/offlinepages/BackgroundOfflinerTaskTest.java
diff --git a/chrome/android/junit/src/org/chromium/chrome/browser/offlinepages/BackgroundOfflinerTaskTest.java b/chrome/android/junit/src/org/chromium/chrome/browser/offlinepages/BackgroundOfflinerTaskTest.java
index e4d58c9bcd3321ec0209bdc81202f0171917463f..ec39b81db381b59da37de6fe793f13f1d103f43d 100644
--- a/chrome/android/junit/src/org/chromium/chrome/browser/offlinepages/BackgroundOfflinerTaskTest.java
+++ b/chrome/android/junit/src/org/chromium/chrome/browser/offlinepages/BackgroundOfflinerTaskTest.java
@@ -13,17 +13,24 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.when;
+import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import com.google.android.gms.gcm.Task;
+import org.chromium.base.ActivityState;
+import org.chromium.base.ApplicationStatus;
import org.chromium.base.BaseChromiumApplication;
+import org.chromium.base.BaseSwitches;
+import org.chromium.base.CommandLine;
+import org.chromium.base.SysUtils;
import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.test.util.Feature;
import org.chromium.chrome.browser.ChromeBackgroundServiceWaiter;
import org.chromium.net.ConnectionType;
import org.chromium.testing.local.LocalRobolectricTestRunner;
+import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -44,6 +51,8 @@ public class BackgroundOfflinerTaskTest {
private static final boolean REQUIRE_UNMETERED = true;
private static final boolean POWER_CONNECTED = true;
private static final int MINIMUM_BATTERY_LEVEL = 33;
+ private static final String IS_LOW_END_DEVICE_SWITCH =
+ "--" + BaseSwitches.ENABLE_LOW_END_DEVICE_MODE;
@Mock
private OfflinePageUtils mOfflinePageUtils;
@@ -55,6 +64,7 @@ public class BackgroundOfflinerTaskTest {
new TriggerConditions(!REQUIRE_POWER, MINIMUM_BATTERY_LEVEL, REQUIRE_UNMETERED);
private DeviceConditions mDeviceConditions = new DeviceConditions(
!POWER_CONNECTED, MINIMUM_BATTERY_LEVEL + 5, ConnectionType.CONNECTION_3G);
+ private Activity mTestActivity;
@Before
public void setUp() throws Exception {
@@ -71,6 +81,23 @@ public class BackgroundOfflinerTaskTest {
mStubBackgroundSchedulerProcessor = new StubBackgroundSchedulerProcessor();
RecordHistogram.disableForTests();
ShadowGcmNetworkManager.clear();
+
+ // Run tests as a low-end device.
+ CommandLine.init(new String[] {"testcommand", IS_LOW_END_DEVICE_SWITCH});
pasko 2016/07/29 09:58:17 did you not choose @CommandLineFlags.Add(BaseSwitc
dougarnett 2016/07/29 15:55:26 This was a bit interesting. I did try that annotat
+
+ // Set up single, stopped Activity.
+ ApplicationStatus.destroyForJUnitTests();
+ mTestActivity = new Activity();
+ ApplicationStatus.onStateChangeForTesting(mTestActivity, ActivityState.CREATED);
+ ApplicationStatus.onStateChangeForTesting(mTestActivity, ActivityState.STOPPED);
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ // Clean up static state for subsequent Robolectric tests.
+ CommandLine.reset();
+ SysUtils.reset();
+ ApplicationStatus.destroyForJUnitTests();
}
@Test
@@ -138,4 +165,33 @@ public class BackgroundOfflinerTaskTest {
ChromeBackgroundServiceWaiter waiter2 = new ChromeBackgroundServiceWaiter(1);
assertTrue(task2.startBackgroundRequests(Robolectric.application, mTaskExtras, waiter2));
}
+
+ @Test
+ @Feature({"OfflinePages"})
+ public void testStartBackgroundRequestsForRunningActivityOnLowEndDevice() {
+ BackgroundOfflinerTask task = new BackgroundOfflinerTask(mStubBackgroundSchedulerProcessor);
+ ChromeBackgroundServiceWaiter waiter = new ChromeBackgroundServiceWaiter(1);
+ assertNull("Nothing scheduled", ShadowGcmNetworkManager.getScheduledTask());
+
+ // Transition the test Activity to a running state.
+ ApplicationStatus.onStateChangeForTesting(mTestActivity, ActivityState.STARTED);
+
+ assertFalse(task.startBackgroundRequests(Robolectric.application, mTaskExtras, waiter));
+
+ // Check that the backup task was scheduled.
+ Task gcmTask = ShadowGcmNetworkManager.getScheduledTask();
+ assertNotNull("Backup task scheduled", gcmTask);
+ assertEquals(mTriggerConditions,
+ TaskExtrasPacker.unpackTriggerConditionsFromBundle(gcmTask.getExtras()));
+
+ // Check that startProcessing was NOT called.
+ assertFalse(mStubBackgroundSchedulerProcessor.getStartProcessingCalled());
+
+ // Now verify will start processing when Activity is stopped.
+ ApplicationStatus.onStateChangeForTesting(mTestActivity, ActivityState.STOPPED);
+ BackgroundOfflinerTask task2 =
+ new BackgroundOfflinerTask(mStubBackgroundSchedulerProcessor);
+ ChromeBackgroundServiceWaiter waiter2 = new ChromeBackgroundServiceWaiter(1);
+ assertTrue(task2.startBackgroundRequests(Robolectric.application, mTaskExtras, waiter2));
+ }
}

Powered by Google App Engine
This is Rietveld 408576698