OLD | NEW |
---|---|
(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() { | |
36 return shell()->web_contents(); | |
37 } | |
38 | |
39 RenderFrameHost* GetMainFrame() { | |
40 return GetWebContents()->GetMainFrame(); | |
41 } | |
42 | |
43 FrameTreeNode* GetNestedFrameNode() { | |
44 FrameTreeNode* root = | |
45 static_cast<WebContentsImpl*>(GetWebContents())->GetFrameTree()->root(); | |
46 CHECK(root->child_count() == 1); | |
47 return root->child_at(0); | |
48 } | |
49 | |
50 RenderFrameHost* GetNestedFrame() { | |
51 return GetNestedFrameNode()->current_frame_host(); | |
52 } | |
53 | |
54 bool HasWakeLock() { | |
55 WebContentsImpl* web_contents_impl = | |
56 static_cast<WebContentsImpl*>(GetWebContents()); | |
57 return web_contents_impl->wake_lock_service_context()->wake_lock_; | |
58 } | |
59 | |
60 void WaitForPossibleUpdate() { | |
61 // As Mojo channels have no common FIFO order in respect to each other and | |
62 // to the Chromium IPC, we cannot assume that when screen.keepAwake state | |
63 // is changed from within a script, WakeLockService will receive an update | |
64 // request before ExecuteScript() returns. Therefore, some time slack is | |
65 // needed to make sure that WakeLockService has received any possible update | |
66 // requests before checking the resulting wake lock state. | |
67 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | |
68 RunAllPendingInMessageLoop(); | |
69 } | |
70 | |
71 void LockScreenInMainFrame() { | |
mlamouri (slow - plz ping)
2015/08/31 14:59:15
nit: rename ScreenWakeLockInMainFrame() (name is c
alogvinov
2015/09/02 17:06:22
Done.
| |
72 EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = true;")); | |
73 WaitForPossibleUpdate(); | |
74 EXPECT_TRUE(HasWakeLock()); | |
75 } | |
76 | |
77 bool EvaluateAsBool(const ToRenderFrameHost& adapter, | |
78 const std::string& expr) { | |
79 bool result; | |
80 CHECK(ExecuteScriptAndExtractBool( | |
81 adapter, | |
82 "window.domAutomationController.send(" + expr + ");", | |
83 &result)); | |
84 return result; | |
85 } | |
86 | |
87 WakeLockServiceContext* GetWakeLockServiceContext() { | |
88 WebContentsImpl* web_contents_impl = | |
89 static_cast<WebContentsImpl*>(shell()->web_contents()); | |
90 return web_contents_impl->wake_lock_service_context(); | |
91 } | |
92 }; | |
93 | |
94 IN_PROC_BROWSER_TEST_F(WakeLockTest, WakeLockApiIsPresent) { | |
95 NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html")); | |
96 EXPECT_TRUE(EvaluateAsBool(GetMainFrame(), | |
97 "typeof screen.keepAwake !== undefined")); | |
98 } | |
99 | |
100 IN_PROC_BROWSER_TEST_F(WakeLockTest, LockAndUnlockScreenInMainFrame) { | |
101 NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html")); | |
102 | |
103 // Should not have screen wake lock initially. | |
104 EXPECT_FALSE(HasWakeLock()); | |
105 | |
106 // Check attribute 'screen.keepAwake' in main frame. | |
107 EXPECT_FALSE(EvaluateAsBool(GetMainFrame(), "screen.keepAwake")); | |
108 | |
109 // Set keep awake flag in main frame. | |
110 EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = true;")); | |
111 WaitForPossibleUpdate(); | |
112 | |
113 // Keep awake flag should be set in main frame. | |
114 EXPECT_TRUE(EvaluateAsBool(GetMainFrame(), "screen.keepAwake")); | |
115 | |
116 // Should create screen wake lock. | |
117 EXPECT_TRUE(HasWakeLock()); | |
118 | |
119 // Reset keep awake flag in main frame. | |
120 EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = false;")); | |
121 WaitForPossibleUpdate(); | |
122 | |
123 // Keep awake flag should not be set in main frame. | |
124 EXPECT_FALSE(EvaluateAsBool(GetMainFrame(), "screen.keepAwake")); | |
125 | |
126 // Should release screen wake lock. | |
127 EXPECT_FALSE(HasWakeLock()); | |
128 } | |
129 | |
130 IN_PROC_BROWSER_TEST_F(WakeLockTest, MultipleLockThenUnlock) { | |
131 NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html")); | |
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, LockScreenInMainFrameAndNestedFrame) { | |
mlamouri (slow - plz ping)
2015/08/31 14:59:15
nit: rename to "LockInMainFrameandNestedFrame"
alogvinov
2015/09/02 17:06:22
Done.
| |
153 NavigateToURL(shell(), | |
154 embedded_test_server()->GetURL("/wake_lock/page_with_iframe.html")); | |
155 EXPECT_FALSE(HasWakeLock()); | |
156 | |
157 // Lock screen in nested frame. | |
158 EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;")); | |
159 WaitForPossibleUpdate(); | |
160 | |
161 // Should create screen wake lock. | |
162 EXPECT_TRUE(HasWakeLock()); | |
mlamouri (slow - plz ping)
2015/08/31 14:59:15
I'm not sure we want that to work like that by def
alogvinov
2015/09/02 17:06:22
Acknowledged.
| |
163 | |
164 // screen.keepAwake should be false in the main frame. | |
165 EXPECT_FALSE(EvaluateAsBool(GetMainFrame(), "screen.keepAwake")); | |
166 | |
167 // Lock screen in main frame. | |
168 EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = true;")); | |
169 WaitForPossibleUpdate(); | |
170 | |
171 // screen.keepAwake should be true in the main frame. | |
172 EXPECT_TRUE(EvaluateAsBool(GetMainFrame(), "screen.keepAwake")); | |
173 | |
174 // Screen wake lock should not change. | |
175 EXPECT_TRUE(HasWakeLock()); | |
176 | |
177 // Unlock screen in nested frame. | |
178 EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = false;")); | |
179 WaitForPossibleUpdate(); | |
180 | |
181 // Screen wake lock should be present, as the main frame is still requesting | |
182 // it. | |
183 EXPECT_TRUE(HasWakeLock()); | |
184 | |
185 // Unlock screen in main frame. | |
186 EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = false;")); | |
187 WaitForPossibleUpdate(); | |
188 | |
189 // Screen wake lock should be released, as no frames are requesting it. | |
190 EXPECT_FALSE(HasWakeLock()); | |
191 } | |
192 | |
193 IN_PROC_BROWSER_TEST_F(WakeLockTest, FrameRemoved) { | |
194 NavigateToURL(shell(), | |
195 embedded_test_server()->GetURL("/wake_lock/page_with_iframe.html")); | |
196 EXPECT_FALSE(HasWakeLock()); | |
197 | |
198 // Lock screen in nested frame. | |
199 EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;")); | |
200 WaitForPossibleUpdate(); | |
201 | |
202 EXPECT_TRUE(HasWakeLock()); | |
203 | |
204 // Remove nested frame. | |
205 EXPECT_TRUE(ExecuteScript( | |
206 GetMainFrame(), | |
207 "var iframe = document.getElementById('nested_frame');" | |
208 "iframe.parentNode.removeChild(iframe);")); | |
209 | |
210 // Screen wake lock should be released. | |
211 EXPECT_FALSE(HasWakeLock()); | |
212 } | |
213 | |
214 IN_PROC_BROWSER_TEST_F(WakeLockTest, UnlockAfterTabCrashed) { | |
215 NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html")); | |
216 LockScreenInMainFrame(); | |
217 | |
218 // Crash the tab. | |
219 CrashTab(shell()->web_contents()); | |
mlamouri (slow - plz ping)
2015/08/31 14:59:15
nit: GetWebContents()
alogvinov
2015/09/02 17:06:22
Done.
| |
220 | |
221 // Screen wake lock should be released. | |
222 EXPECT_FALSE(HasWakeLock()); | |
223 } | |
224 | |
225 IN_PROC_BROWSER_TEST_F(WakeLockTest, UnlockAfterNavigation) { | |
226 NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html")); | |
227 LockScreenInMainFrame(); | |
228 | |
229 // Navigate to a different document. | |
230 NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html")); | |
231 | |
232 // Screen wake lock should be released after navigation. | |
233 EXPECT_FALSE(HasWakeLock()); | |
234 } | |
235 | |
236 IN_PROC_BROWSER_TEST_F(WakeLockTest, UnlockAfterNavigationToSelf) { | |
mlamouri (slow - plz ping)
2015/08/31 14:59:15
Could you check that in-page navigation keeps the
alogvinov
2015/09/02 17:06:22
Done.
| |
237 NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html")); | |
238 LockScreenInMainFrame(); | |
239 | |
240 // Navigate to the same document. | |
241 NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html")); | |
242 | |
243 // Screen wake lock should be released after navigation to the same URL. | |
244 EXPECT_FALSE(HasWakeLock()); | |
245 } | |
246 | |
247 IN_PROC_BROWSER_TEST_F(WakeLockTest, UnlockAfterReload) { | |
248 NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html")); | |
249 LockScreenInMainFrame(); | |
250 | |
251 shell()->Reload(); | |
252 WaitForLoadStop(GetWebContents()); | |
253 | |
254 // Screen wake lock should be released after reload. | |
255 EXPECT_FALSE(HasWakeLock()); | |
256 } | |
257 | |
258 IN_PROC_BROWSER_TEST_F(WakeLockTest, BrowserInitiatedFrameNavigation) { | |
259 NavigateToURL(shell(), | |
260 embedded_test_server()->GetURL("/wake_lock/page_with_iframe.html")); | |
261 | |
262 EXPECT_FALSE(HasWakeLock()); | |
263 | |
264 // Lock screen in nested frame. | |
265 EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;")); | |
266 WaitForPossibleUpdate(); | |
267 | |
268 // Screen wake lock should be present. | |
269 EXPECT_TRUE(HasWakeLock()); | |
270 | |
271 // Navigate the nested frame (browser-initiated). | |
272 NavigateFrameToURL(GetNestedFrameNode(), | |
273 embedded_test_server()->GetURL("/simple_page.html")); | |
274 | |
275 // Screen wake lock should be released. | |
276 EXPECT_FALSE(HasWakeLock()); | |
277 } | |
278 | |
279 IN_PROC_BROWSER_TEST_F(WakeLockTest, RendererInitiatedFrameNavigation) { | |
280 NavigateToURL(shell(), | |
281 embedded_test_server()->GetURL("/wake_lock/page_with_iframe.html")); | |
282 | |
283 EXPECT_FALSE(HasWakeLock()); | |
284 | |
285 // Lock screen in nested frame. | |
286 EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;")); | |
287 WaitForPossibleUpdate(); | |
288 | |
289 // Screen wake lock should be present. | |
290 EXPECT_TRUE(HasWakeLock()); | |
291 | |
292 // Navigate the nested frame (renderer-initiated). | |
293 NavigateIframeToURL(GetWebContents(), | |
294 "nested_frame", | |
295 embedded_test_server()->GetURL("/simple_page.html")); | |
296 WaitForPossibleUpdate(); | |
297 | |
298 // Screen wake lock should be released. | |
299 EXPECT_FALSE(HasWakeLock()); | |
300 } | |
301 | |
302 IN_PROC_BROWSER_TEST_F(WakeLockTest, OutOfProcessFrame) { | |
303 NavigateToURL(shell(), | |
304 embedded_test_server()->GetURL("/wake_lock/page_with_iframe.html")); | |
305 EXPECT_FALSE(HasWakeLock()); | |
306 | |
307 // Lock screen in same-site nested frame. | |
308 EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;")); | |
309 WaitForPossibleUpdate(); | |
310 EXPECT_TRUE(HasWakeLock()); | |
311 | |
312 // Navigate nested frame to a cross-site document. | |
313 NavigateFrameToURL(GetNestedFrameNode(), | |
314 embedded_test_server()->GetURL("foo.com", | |
315 "/simple_page.html")); | |
316 | |
317 // Ensure that a new process has been created for the subframe. | |
318 EXPECT_TRUE(GetNestedFrame()->IsCrossProcessSubframe()); | |
319 | |
320 // Screen wake lock should be released. | |
321 EXPECT_FALSE(HasWakeLock()); | |
322 | |
323 // Lock screen in the cross-site nested frame. | |
324 EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;")); | |
325 WaitForPossibleUpdate(); | |
326 | |
327 // Screen wake lock should be created. | |
328 EXPECT_TRUE(HasWakeLock()); | |
329 } | |
330 | |
331 IN_PROC_BROWSER_TEST_F(WakeLockTest, UnlockAfterCrashOutOfProcessFrame) { | |
332 NavigateToURL(shell(), | |
333 embedded_test_server()->GetURL("/wake_lock/page_with_iframe.html")); | |
334 EXPECT_FALSE(HasWakeLock()); | |
335 | |
336 // Load cross-site page into the nested frame. | |
337 NavigateFrameToURL(GetNestedFrameNode(), | |
338 embedded_test_server()->GetURL("foo.com", | |
339 "/simple_page.html")); | |
340 | |
341 // Ensure that a new process has been created for the nested frame. | |
342 EXPECT_TRUE(GetNestedFrame()->IsCrossProcessSubframe()); | |
343 | |
344 // Lock screen in cross-site nested frame. | |
345 EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;")); | |
346 WaitForPossibleUpdate(); | |
347 EXPECT_TRUE(HasWakeLock()); | |
348 | |
349 // Crash process that owns the out-of-process frame. | |
350 RenderProcessHostWatcher watcher( | |
351 GetNestedFrame()->GetProcess(), | |
352 RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); | |
353 GetNestedFrame()->GetProcess()->Shutdown(0, false); | |
354 watcher.Wait(); | |
355 | |
356 // Screen wake lock should be released. | |
357 EXPECT_FALSE(HasWakeLock()); | |
358 } | |
359 | |
360 } // namespace content | |
OLD | NEW |