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

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/PowerBroadcastReceiverTest.java

Issue 1141283003: Upstream oodles of Chrome for Android code into Chromium. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: final patch? Created 5 years, 7 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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;
6
7 import android.content.Context;
8 import android.content.Intent;
9 import android.test.suitebuilder.annotation.MediumTest;
10
11 import org.chromium.base.test.util.DisabledTest;
12 import org.chromium.base.test.util.Feature;
13 import org.chromium.chrome.browser.preferences.Preferences;
14 import org.chromium.chrome.test.ChromeTabbedActivityTestBase;
15 import org.chromium.content.browser.test.util.Criteria;
16 import org.chromium.content.browser.test.util.CriteriaHelper;
17 import org.chromium.content.browser.test.util.UiUtils;
18
19 /**
20 * Tests for the PowerBroadcastReceiver.
21 */
22 public class PowerBroadcastReceiverTest extends ChromeTabbedActivityTestBase {
23 private static final long MS_INTERVAL = 1000;
24 private static final long MS_RUNNABLE_DELAY = 2500;
25 private static final long MS_TIMEOUT = 5000;
26
27 private static final MockPowerManagerHelper sScreenOff = new MockPowerManage rHelper(false);
28 private static final MockPowerManagerHelper sScreenOn = new MockPowerManager Helper(true);
29
30 /** Mocks out PowerBroadcastReceiver.ServiceRunnable. */
31 private static class MockServiceRunnable extends PowerBroadcastReceiver.Serv iceRunnable {
32 private boolean mRan;
33 private final long mDelay;
34
35 MockServiceRunnable(long delay) {
36 mDelay = delay;
37 }
38
39 @Override
40 public long delayToRun() {
41 return mDelay;
42 }
43
44 @Override
45 public void run() {
46 mRan = true;
47 }
48 }
49
50 /** Mocks out PowerBroadcastReceiver.PowerManagerHelper. */
51 private static class MockPowerManagerHelper extends PowerBroadcastReceiver.P owerManagerHelper {
52 private final boolean mScreenIsOn;
53
54 public MockPowerManagerHelper(boolean screenIsOn) {
55 mScreenIsOn = screenIsOn;
56 }
57
58 @Override
59 public boolean isScreenOn(Context context) {
60 return mScreenIsOn;
61 }
62 }
63
64 @Override
65 public void startMainActivity() throws InterruptedException {
66 startMainActivityFromLauncher();
67 }
68
69 private PowerBroadcastReceiver getPowerBroadcastReceiver() {
70 ChromeMobileApplication app = (ChromeMobileApplication) getActivity().ge tApplication();
71 return app.getPowerBroadcastReceiver();
72 }
73
74 /**
75 * Replaces the ServiceRunnable and PowerManagerHelper used by the PowerBroa dcastReceiver
76 * with mocked versions.
77 * @return the MockServiceRunnable created by this function.
78 */
79 private MockServiceRunnable setUpMockRunnable() {
80 MockServiceRunnable runnable = new MockServiceRunnable(MS_RUNNABLE_DELAY );
81 getPowerBroadcastReceiver().setServiceRunnableForTests(runnable);
82 return runnable;
83 }
84
85 /**
86 * Pauses (and optionally resumes) Main by starting a different Activity.
87 * @param resumeAfterPause Resume Main after the pause.
88 */
89 private void pauseMain(boolean resumeAfterPause) throws Exception {
90 // Bring up the Preferences activity to put the Main activity in the bac kground.
91 UiUtils.settleDownUI(getInstrumentation());
92 Preferences prefActivity = startPreferences(null);
93 assertNotNull("Could not launch preferences activity.", prefActivity);
94
95 if (resumeAfterPause) {
96 // Finish the Activity to bring Main back.
97 UiUtils.settleDownUI(getInstrumentation());
98 prefActivity.finish();
99 }
100 }
101
102 /**
103 * Waits to see if the runnable was run.
104 */
105 public boolean runnableRan(final MockServiceRunnable runnable) throws Except ion {
106 return CriteriaHelper.pollForCriteria(
107 new Criteria() {
108 @Override
109 public boolean isSatisfied() {
110 return runnable.mRan;
111 }
112 },
113 MS_TIMEOUT, MS_INTERVAL);
114 }
115
116 /**
117 * Check if the runnable is posted and run while the screen is on.
118 */
119 /*
120 @MediumTest
121 @Feature({"Omaha", "Sync"})
122 Bug http://crbug.com/156745
123 */
124 @DisabledTest
125 public void testRunnableRunsWithScreenOn() throws Exception {
126 // Claim the screen is on.
127 PowerBroadcastReceiver receiver = getPowerBroadcastReceiver();
128 receiver.setPowerManagerHelperForTests(sScreenOn);
129
130 // Switch out the PowerBroadcastReceiver's ServiceRunnable.
131 MockServiceRunnable runnable = setUpMockRunnable();
132
133 // Pause & resume to post our mocked runnable.
134 pauseMain(true);
135
136 // Check to see that the runnable gets run.
137 assertTrue("MockServiceRunnable didn't run after resuming Chrome.", runn ableRan(runnable));
138 assertFalse("PowerBroadcastReceiver was still registered.", receiver.isR egistered());
139 }
140
141 /**
142 * Check that the runnable gets posted and canceled when Main is sent to the background.
143 */
144 @MediumTest
145 @Feature({"Omaha", "Sync"})
146 public void testRunnableGetsCanceled() throws Exception {
147 // Claim the screen is on.
148 PowerBroadcastReceiver receiver = getPowerBroadcastReceiver();
149 receiver.setPowerManagerHelperForTests(sScreenOn);
150
151 // Switch out the PowerBroadcastReceiver's Runnable with a MockServiceRu nnable.
152 MockServiceRunnable runnable = setUpMockRunnable();
153
154 // Pause & resume to post our mocked runnable, then pause immediately af ter resuming so that
155 // Main's handler cancels the runnable.
156 pauseMain(true);
157 pauseMain(false);
158
159 // Wait enough time for the runnable to run(), if it's still in the hand ler.
160 assertFalse("MockServiceRunnable ran after resuming Chrome.", runnableRa n(runnable));
161 assertFalse("PowerBroadcastReceiver was still registered.", receiver.isR egistered());
162 }
163
164 /**
165 * Check that the runnable gets run only while the screen is on.
166 */
167 /*
168 @MediumTest
169 @Feature({"Omaha", "Sync"})
170 Bug http://crbug.com/156745
171 */
172 @DisabledTest
173 public void testRunnableGetsRunWhenScreenIsOn() throws Exception {
174 ChromeTabbedActivity main = getActivity();
175 PowerBroadcastReceiver receiver = getPowerBroadcastReceiver();
176
177 // Claim the screen is off.
178 receiver.setPowerManagerHelperForTests(sScreenOff);
179
180 // Switch out the PowerBroadcastReceiver's Runnable with a MockServiceRu nnable.
181 MockServiceRunnable runnable = setUpMockRunnable();
182
183 // Pause & resume to post our mocked runnable.
184 pauseMain(true);
185
186 // Wait enough time for the runnable to run(), if it's still in the hand ler.
187 boolean ranWhileScreenOff = runnableRan(runnable);
188 assertFalse("MockServiceRunnable ran even though screen was off.", ranWh ileScreenOff);
189 assertTrue("PowerBroadcastReceiver was not registered.", receiver.isRegi stered());
190
191 // Pretend to turn the screen on.
192 receiver.setPowerManagerHelperForTests(sScreenOn);
193 Intent intent = new Intent(Intent.ACTION_SCREEN_ON);
194 receiver.onReceive(main, intent);
195
196 // Wait enough time for the runnable to run(), if it's still in the hand ler.
197 boolean ranWhileScreenOn = runnableRan(runnable);
198 assertTrue("MockServiceRunnable didn't run when screen turned on.", ranW hileScreenOn);
199 assertFalse("PowerBroadcastReceiver wasn't unregistered.", receiver.isRe gistered());
200 }
201 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698