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

Side by Side Diff: content/browser/wake_lock/wake_lock_browsertest.cc

Issue 1107333002: Wake Lock API implementation (Chromium part) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: added visibility check to browsertest Created 5 years, 3 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 #include "base/command_line.h"
6 #include "base/test/test_timeouts.h"
7 #include "content/browser/wake_lock/wake_lock_service_context.h"
8 #include "content/browser/web_contents/web_contents_impl.h"
9 #include "content/public/common/content_switches.h"
10 #include "content/public/test/browser_test_utils.h"
11 #include "content/public/test/content_browser_test.h"
12 #include "content/public/test/content_browser_test_utils.h"
13 #include "content/public/test/test_utils.h"
14 #include "content/shell/browser/shell.h"
15 #include "content/test/content_browser_test_utils_internal.h"
16 #include "net/dns/mock_host_resolver.h"
17 #include "net/test/embedded_test_server/embedded_test_server.h"
18
19 namespace content {
20
21 class WakeLockTest : public ContentBrowserTest {
22 public:
23 void SetUpCommandLine(base::CommandLine* command_line) override {
24 command_line->AppendSwitch(
25 switches::kEnableExperimentalWebPlatformFeatures);
26 command_line->AppendSwitch(switches::kSitePerProcess);
27 }
28
29 void SetUpOnMainThread() override {
30 host_resolver()->AddRule("*", "127.0.0.1");
31 EXPECT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
32 }
33
34 protected:
35 WebContents* GetWebContents() { return shell()->web_contents(); }
36
37 WebContentsImpl* GetWebContentsImpl() {
38 return static_cast<WebContentsImpl*>(GetWebContents());
39 }
40
41 RenderFrameHost* GetMainFrame() { return GetWebContents()->GetMainFrame(); }
42
43 FrameTreeNode* GetNestedFrameNode() {
44 FrameTreeNode* root = GetWebContentsImpl()->GetFrameTree()->root();
45 CHECK_EQ(1U, root->child_count());
46 return root->child_at(0);
47 }
48
49 RenderFrameHost* GetNestedFrame() {
50 return GetNestedFrameNode()->current_frame_host();
51 }
52
53 WakeLockServiceContext* GetWakeLockServiceContext() {
54 return GetWebContentsImpl()->GetWakeLockServiceContext();
55 }
56
57 bool HasWakeLock() {
58 return GetWakeLockServiceContext()->HasWakeLockForTests();
59 }
60
61 void WaitForPossibleUpdate() {
62 // As Mojo channels have no common FIFO order in respect to each other and
63 // to the Chromium IPC, we cannot assume that when screen.keepAwake state
64 // is changed from within a script, WakeLockService will receive an update
65 // request before ExecuteScript() returns. Therefore, some time slack is
66 // needed to make sure that WakeLockService has received any possible update
67 // requests before checking the resulting wake lock state.
68 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
69 RunAllPendingInMessageLoop();
70 }
71
72 void ScreenWakeLockInMainFrame() {
73 EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = true;"));
74 WaitForPossibleUpdate();
75 EXPECT_TRUE(HasWakeLock());
76 }
77
78 bool EvaluateAsBool(const ToRenderFrameHost& adapter,
79 const std::string& expr) {
80 bool result;
81 CHECK(ExecuteScriptAndExtractBool(
82 adapter, "window.domAutomationController.send(" + expr + ");",
83 &result));
84 return result;
85 }
86 };
87
88 IN_PROC_BROWSER_TEST_F(WakeLockTest, WakeLockApiIsPresent) {
89 NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html"));
90 EXPECT_EQ(blink::WebPageVisibilityStateVisible,
91 GetMainFrame()->GetVisibilityState());
92 EXPECT_TRUE(
93 EvaluateAsBool(GetMainFrame(), "typeof screen.keepAwake !== undefined"));
94 }
95
96 IN_PROC_BROWSER_TEST_F(WakeLockTest, LockAndUnlockScreenInMainFrame) {
97 NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html"));
98 EXPECT_EQ(blink::WebPageVisibilityStateVisible,
99 GetMainFrame()->GetVisibilityState());
100
101 // Should not have screen wake lock initially.
102 EXPECT_FALSE(HasWakeLock());
103
104 // Check attribute 'screen.keepAwake' in main frame.
105 EXPECT_FALSE(EvaluateAsBool(GetMainFrame(), "screen.keepAwake"));
106
107 // Set keep awake flag in main frame.
108 EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = true;"));
109 WaitForPossibleUpdate();
110
111 // Keep awake flag should be set in main frame.
112 EXPECT_TRUE(EvaluateAsBool(GetMainFrame(), "screen.keepAwake"));
113
114 // Should create screen wake lock.
115 EXPECT_TRUE(HasWakeLock());
116
117 // Reset keep awake flag in main frame.
118 EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = false;"));
119 WaitForPossibleUpdate();
120
121 // Keep awake flag should not be set in main frame.
122 EXPECT_FALSE(EvaluateAsBool(GetMainFrame(), "screen.keepAwake"));
123
124 // Should release screen wake lock.
125 EXPECT_FALSE(HasWakeLock());
126 }
127
128 IN_PROC_BROWSER_TEST_F(WakeLockTest, MultipleLockThenUnlock) {
129 NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html"));
130 EXPECT_EQ(blink::WebPageVisibilityStateVisible,
131 GetMainFrame()->GetVisibilityState());
132
133 // Set keep awake flag
134 EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = true;"));
135 WaitForPossibleUpdate();
136
137 // Set keep awake flag again.
138 EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = true;"));
139 WaitForPossibleUpdate();
140
141 // Screen should still be locked.
142 EXPECT_TRUE(HasWakeLock());
143
144 // Reset keep awake flag.
145 EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = false;"));
146 WaitForPossibleUpdate();
147
148 // Should release screen wake lock.
149 EXPECT_FALSE(HasWakeLock());
150 }
151
152 IN_PROC_BROWSER_TEST_F(WakeLockTest, LockInMainFrameAndNestedFrame) {
153 NavigateToURL(shell(),
154 embedded_test_server()->GetURL("/frame_tree/2-4.html"));
155 EXPECT_EQ(blink::WebPageVisibilityStateVisible,
156 GetMainFrame()->GetVisibilityState());
157 EXPECT_FALSE(HasWakeLock());
158
159 // Lock screen in nested frame.
160 EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;"));
161 WaitForPossibleUpdate();
162
163 // Should create screen wake lock.
164 EXPECT_TRUE(HasWakeLock());
165
166 // screen.keepAwake should be false in the main frame.
167 EXPECT_FALSE(EvaluateAsBool(GetMainFrame(), "screen.keepAwake"));
168
169 // Lock screen in main frame.
170 EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = true;"));
171 WaitForPossibleUpdate();
172
173 // screen.keepAwake should be true in the main frame.
174 EXPECT_TRUE(EvaluateAsBool(GetMainFrame(), "screen.keepAwake"));
175
176 // Screen wake lock should not change.
177 EXPECT_TRUE(HasWakeLock());
178
179 // Unlock screen in nested frame.
180 EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = false;"));
181 WaitForPossibleUpdate();
182
183 // Screen wake lock should be present, as the main frame is still requesting
184 // it.
185 EXPECT_TRUE(HasWakeLock());
186
187 // Unlock screen in main frame.
188 EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = false;"));
189 WaitForPossibleUpdate();
190
191 // Screen wake lock should be released, as no frames are requesting it.
192 EXPECT_FALSE(HasWakeLock());
193 }
194
195 IN_PROC_BROWSER_TEST_F(WakeLockTest, FrameRemoved) {
196 NavigateToURL(shell(),
197 embedded_test_server()->GetURL("/frame_tree/2-4.html"));
198 EXPECT_EQ(blink::WebPageVisibilityStateVisible,
199 GetMainFrame()->GetVisibilityState());
200 EXPECT_FALSE(HasWakeLock());
201
202 // Lock screen in nested frame.
203 EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;"));
204 WaitForPossibleUpdate();
205
206 EXPECT_TRUE(HasWakeLock());
207
208 // Remove nested frame.
209 EXPECT_TRUE(ExecuteScript(GetMainFrame(),
210 "var iframe = document.getElementById('3-1-id');"
211 "iframe.parentNode.removeChild(iframe);"));
212
213 // Screen wake lock should be released.
214 EXPECT_FALSE(HasWakeLock());
215 }
216
217 IN_PROC_BROWSER_TEST_F(WakeLockTest, UnlockAfterTabCrashed) {
218 NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html"));
219 EXPECT_EQ(blink::WebPageVisibilityStateVisible,
220 GetMainFrame()->GetVisibilityState());
221 ScreenWakeLockInMainFrame();
222
223 // Crash the tab.
224 CrashTab(GetWebContents());
225
226 // Screen wake lock should be released.
227 EXPECT_FALSE(HasWakeLock());
228 }
229
230 IN_PROC_BROWSER_TEST_F(WakeLockTest, UnlockAfterNavigation) {
231 NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html"));
232 EXPECT_EQ(blink::WebPageVisibilityStateVisible,
233 GetMainFrame()->GetVisibilityState());
234 ScreenWakeLockInMainFrame();
235
236 // Navigate to a different document.
237 NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html"));
238
239 // Screen wake lock should be released after navigation.
240 EXPECT_FALSE(HasWakeLock());
241 }
242
243 IN_PROC_BROWSER_TEST_F(WakeLockTest, UnlockAfterNavigationToSelf) {
244 NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html"));
245 EXPECT_EQ(blink::WebPageVisibilityStateVisible,
246 GetMainFrame()->GetVisibilityState());
247 ScreenWakeLockInMainFrame();
248
249 // Navigate to the same document.
250 NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html"));
251
252 // Screen wake lock should be released after navigation to the same URL.
253 EXPECT_FALSE(HasWakeLock());
254 }
255
256 IN_PROC_BROWSER_TEST_F(WakeLockTest, KeepLockAfterInPageNavigation) {
257 GURL test_url(
258 embedded_test_server()->GetURL("/session_history/fragment.html"));
259 GURL test_in_page_url(test_url.spec() + "#a");
260
261 NavigateToURL(shell(), test_url);
262 EXPECT_EQ(blink::WebPageVisibilityStateVisible,
263 GetMainFrame()->GetVisibilityState());
264 ScreenWakeLockInMainFrame();
265
266 NavigateToURL(shell(), test_in_page_url);
267 EXPECT_TRUE(HasWakeLock());
268 }
269
270 IN_PROC_BROWSER_TEST_F(WakeLockTest, UnlockAfterReload) {
271 NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html"));
272 EXPECT_EQ(blink::WebPageVisibilityStateVisible,
273 GetMainFrame()->GetVisibilityState());
274 ScreenWakeLockInMainFrame();
275
276 shell()->Reload();
277 WaitForLoadStop(GetWebContents());
278
279 // Screen wake lock should be released after reload.
280 EXPECT_FALSE(HasWakeLock());
281 }
282
283 IN_PROC_BROWSER_TEST_F(WakeLockTest, BrowserInitiatedFrameNavigation) {
284 NavigateToURL(shell(),
285 embedded_test_server()->GetURL("/frame_tree/2-4.html"));
286 EXPECT_EQ(blink::WebPageVisibilityStateVisible,
287 GetMainFrame()->GetVisibilityState());
288
289 EXPECT_FALSE(HasWakeLock());
290
291 // Lock screen in nested frame.
292 EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;"));
293 WaitForPossibleUpdate();
294
295 // Screen wake lock should be present.
296 EXPECT_TRUE(HasWakeLock());
297
298 // Navigate the nested frame (browser-initiated).
299 NavigateFrameToURL(GetNestedFrameNode(),
300 embedded_test_server()->GetURL("/simple_page.html"));
301 WaitForPossibleUpdate();
302
303 // Screen wake lock should be released.
304 EXPECT_FALSE(HasWakeLock());
305 }
306
307 IN_PROC_BROWSER_TEST_F(WakeLockTest, RendererInitiatedFrameNavigation) {
308 NavigateToURL(shell(),
309 embedded_test_server()->GetURL("/frame_tree/2-4.html"));
310 EXPECT_EQ(blink::WebPageVisibilityStateVisible,
311 GetMainFrame()->GetVisibilityState());
312
313 EXPECT_FALSE(HasWakeLock());
314
315 // Lock screen in nested frame.
316 EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;"));
317 WaitForPossibleUpdate();
318
319 // Screen wake lock should be present.
320 EXPECT_TRUE(HasWakeLock());
321
322 // Navigate the nested frame (renderer-initiated).
323 NavigateIframeToURL(GetWebContents(), "3-1-id",
324 embedded_test_server()->GetURL("/simple_page.html"));
325 WaitForPossibleUpdate();
326
327 // Screen wake lock should be released.
328 EXPECT_FALSE(HasWakeLock());
329 }
330
331 IN_PROC_BROWSER_TEST_F(WakeLockTest, OutOfProcessFrame) {
332 NavigateToURL(shell(), embedded_test_server()->GetURL(
333 "a.com", "/cross_site_iframe_factory.html?a(a)"));
334 EXPECT_EQ(blink::WebPageVisibilityStateVisible,
335 GetMainFrame()->GetVisibilityState());
336 EXPECT_FALSE(HasWakeLock());
337
338 // Ensure that the nested frame is same-process.
339 EXPECT_FALSE(GetNestedFrame()->IsCrossProcessSubframe());
340
341 // Lock screen in same-site nested frame.
342 EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;"));
343 WaitForPossibleUpdate();
344 EXPECT_TRUE(HasWakeLock());
345
346 // Navigate nested frame to a cross-site document.
347 NavigateFrameToURL(GetNestedFrameNode(), embedded_test_server()->GetURL(
348 "b.com", "/simple_page.html"));
349 WaitForPossibleUpdate();
350
351 // Ensure that a new process has been created for the nested frame.
352 EXPECT_TRUE(GetNestedFrame()->IsCrossProcessSubframe());
353
354 // Screen wake lock should be released.
355 EXPECT_FALSE(HasWakeLock());
356
357 // Lock screen in the cross-site nested frame.
358 EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;"));
359 WaitForPossibleUpdate();
360
361 // Screen wake lock should be created.
362 EXPECT_TRUE(HasWakeLock());
363 }
364
365 IN_PROC_BROWSER_TEST_F(WakeLockTest, UnlockAfterCrashOutOfProcessFrame) {
366 // Load a page with cross-site iframe.
367 NavigateToURL(shell(), embedded_test_server()->GetURL(
368 "a.com", "/cross_site_iframe_factory.html?a(b)"));
369 EXPECT_EQ(blink::WebPageVisibilityStateVisible,
370 GetMainFrame()->GetVisibilityState());
371 EXPECT_FALSE(HasWakeLock());
372
373 // Ensure that a new process has been created for the nested frame.
374 EXPECT_TRUE(GetNestedFrame()->IsCrossProcessSubframe());
375
376 // Lock screen in cross-site nested frame.
377 EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;"));
378 WaitForPossibleUpdate();
379 EXPECT_TRUE(HasWakeLock());
380
381 // Crash process that owns the out-of-process frame.
382 RenderProcessHostWatcher watcher(
383 GetNestedFrame()->GetProcess(),
384 RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
385 GetNestedFrame()->GetProcess()->Shutdown(0, false);
386 watcher.Wait();
387
388 // Screen wake lock should be released.
389 EXPECT_FALSE(HasWakeLock());
390 }
391
392 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/frame_host/render_frame_host_impl.cc ('k') | content/browser/wake_lock/wake_lock_service_context.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698