summaryrefslogtreecommitdiff
path: root/chromium/content/browser/wake_lock/wake_lock_browsertest.cc
blob: 86ae40f008895a40fc895945a106475d4d827126 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Copyright 2015 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.

#include "base/command_line.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/test/test_timeouts.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "content/shell/browser/shell.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"

namespace content {

namespace {

const char kBlinkWakeLockFeature[] = "WakeLock";

void OnHasWakeLock(bool* out, bool has_wakelock) {
  *out = has_wakelock;
  base::RunLoop::QuitCurrentDeprecated();
}

}  // namespace

class WakeLockTest : public ContentBrowserTest {
 public:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->AppendSwitchASCII(switches::kEnableBlinkFeatures,
                                    kBlinkWakeLockFeature);
    command_line->AppendSwitch(switches::kSitePerProcess);
  }

  void SetUpOnMainThread() override {
    host_resolver()->AddRule("*", "127.0.0.1");
    EXPECT_TRUE(embedded_test_server()->Start());
    // To prevent occlusion events from changing page visibility.
    GetWebContents()->IncrementCapturerCount(gfx::Size());
  }

  void TearDownOnMainThread() override {
    GetWebContents()->DecrementCapturerCount();
  }

 protected:
  WebContents* GetWebContents() { return shell()->web_contents(); }

  WebContentsImpl* GetWebContentsImpl() {
    return static_cast<WebContentsImpl*>(GetWebContents());
  }

  RenderFrameHost* GetMainFrame() { return GetWebContents()->GetMainFrame(); }

  FrameTreeNode* GetNestedFrameNode() {
    FrameTreeNode* root = GetWebContentsImpl()->GetFrameTree()->root();
    CHECK_EQ(1U, root->child_count());
    return root->child_at(0);
  }

  RenderFrameHost* GetNestedFrame() {
    return GetNestedFrameNode()->current_frame_host();
  }

  device::mojom::WakeLock* GetRendererWakeLock() {
    return GetWebContentsImpl()->GetRendererWakeLock();
  }

  bool HasWakeLock() {
    bool has_wakelock = false;
    base::RunLoop run_loop;

    GetRendererWakeLock()->HasWakeLockForTests(
        base::Bind(&OnHasWakeLock, &has_wakelock));
    run_loop.Run();
    return has_wakelock;
  }

  void WaitForPossibleUpdate() {
    // As Mojo channels have no common FIFO order in respect to each other and
    // to the Chromium IPC, we cannot assume that when screen.keepAwake state
    // is changed from within a script, mojom::WakeLock will receive an
    // update request before ExecuteScript() returns. Therefore, some time slack
    // is needed to make sure that mojom::WakeLock has received any
    // possible update requests before checking the resulting wake lock state.
    base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
    RunAllPendingInMessageLoop();
  }

  void ScreenWakeLockInMainFrame() {
    EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = true;"));
    WaitForPossibleUpdate();
    EXPECT_TRUE(HasWakeLock());
  }

  bool EvaluateAsBool(const ToRenderFrameHost& adapter,
                      const std::string& expr) {
    bool result;
    CHECK(ExecuteScriptAndExtractBool(
        adapter, "window.domAutomationController.send(" + expr + ");",
        &result));
    return result;
  }
};

IN_PROC_BROWSER_TEST_F(WakeLockTest, WakeLockApiIsPresent) {
  NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html"));
  EXPECT_TRUE(
      EvaluateAsBool(GetMainFrame(), "typeof screen.keepAwake !== undefined"));
}

IN_PROC_BROWSER_TEST_F(WakeLockTest, LockAndUnlockScreenInMainFrame) {
  NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html"));

  // Should not have screen wake lock initially.
  EXPECT_FALSE(HasWakeLock());

  // Check attribute 'screen.keepAwake' in main frame.
  EXPECT_FALSE(EvaluateAsBool(GetMainFrame(), "screen.keepAwake"));

  // Set keep awake flag in main frame.
  EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = true;"));
  WaitForPossibleUpdate();

  // Keep awake flag should be set in main frame.
  EXPECT_TRUE(EvaluateAsBool(GetMainFrame(), "screen.keepAwake"));

  // Should create screen wake lock.
  EXPECT_TRUE(HasWakeLock());

  // Reset keep awake flag in main frame.
  EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = false;"));
  WaitForPossibleUpdate();

  // Keep awake flag should not be set in main frame.
  EXPECT_FALSE(EvaluateAsBool(GetMainFrame(), "screen.keepAwake"));

  // Should release screen wake lock.
  EXPECT_FALSE(HasWakeLock());
}

IN_PROC_BROWSER_TEST_F(WakeLockTest, MultipleLockThenUnlock) {
  NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html"));

  // Set keep awake flag.
  EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = true;"));
  WaitForPossibleUpdate();

  // Set keep awake flag again.
  EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = true;"));
  WaitForPossibleUpdate();

  // Screen should still be locked.
  EXPECT_TRUE(HasWakeLock());

  // Reset keep awake flag.
  EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = false;"));
  WaitForPossibleUpdate();

  // Should release screen wake lock.
  EXPECT_FALSE(HasWakeLock());
}

IN_PROC_BROWSER_TEST_F(WakeLockTest, LockInMainFrameAndNestedFrame) {
  NavigateToURL(shell(),
                embedded_test_server()->GetURL("/frame_tree/2-4.html"));
  EXPECT_FALSE(HasWakeLock());

  // Lock screen in nested frame.
  EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;"));
  WaitForPossibleUpdate();

  // Should create screen wake lock.
  EXPECT_TRUE(HasWakeLock());

  // screen.keepAwake should be false in the main frame.
  EXPECT_FALSE(EvaluateAsBool(GetMainFrame(), "screen.keepAwake"));

  // Lock screen in main frame.
  EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = true;"));
  WaitForPossibleUpdate();

  // screen.keepAwake should be true in the main frame.
  EXPECT_TRUE(EvaluateAsBool(GetMainFrame(), "screen.keepAwake"));

  // Screen wake lock should not change.
  EXPECT_TRUE(HasWakeLock());

  // Unlock screen in nested frame.
  EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = false;"));
  WaitForPossibleUpdate();

  // Screen wake lock should be present, as the main frame is still requesting
  // it.
  EXPECT_TRUE(HasWakeLock());

  // Unlock screen in main frame.
  EXPECT_TRUE(ExecuteScript(GetMainFrame(), "screen.keepAwake = false;"));
  WaitForPossibleUpdate();

  // Screen wake lock should be released, as no frames are requesting it.
  EXPECT_FALSE(HasWakeLock());
}

IN_PROC_BROWSER_TEST_F(WakeLockTest, FrameRemoved) {
  NavigateToURL(shell(),
                embedded_test_server()->GetURL("/frame_tree/2-4.html"));
  EXPECT_FALSE(HasWakeLock());

  // Lock screen in nested frame.
  EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;"));
  WaitForPossibleUpdate();

  EXPECT_TRUE(HasWakeLock());

  // Remove nested frame.
  EXPECT_TRUE(ExecuteScript(GetMainFrame(),
                            "var iframe = document.getElementById('3-1-id');"
                            "iframe.parentNode.removeChild(iframe);"));

  // Screen wake lock should be released.
  EXPECT_FALSE(HasWakeLock());
}

IN_PROC_BROWSER_TEST_F(WakeLockTest, UnlockAfterTabCrashed) {
  NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html"));
  ScreenWakeLockInMainFrame();

  // Crash the tab.
  CrashTab(GetWebContents());

  // Screen wake lock should be released.
  EXPECT_FALSE(HasWakeLock());
}

IN_PROC_BROWSER_TEST_F(WakeLockTest, UnlockAfterNavigation) {
  NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html"));
  ScreenWakeLockInMainFrame();

  // Navigate to a different document.
  NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html"));

  // Screen wake lock should be released after navigation.
  EXPECT_FALSE(HasWakeLock());
}

IN_PROC_BROWSER_TEST_F(WakeLockTest, UnlockAfterNavigationToSelf) {
  NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html"));
  ScreenWakeLockInMainFrame();

  // Navigate to the same document.
  NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html"));

  // Screen wake lock should be released after navigation to the same URL.
  EXPECT_FALSE(HasWakeLock());
}

IN_PROC_BROWSER_TEST_F(WakeLockTest, KeepLockAfterInPageNavigation) {
  GURL test_url(
      embedded_test_server()->GetURL("/session_history/fragment.html"));
  GURL test_in_page_url(test_url.spec() + "#a");

  NavigateToURL(shell(), test_url);
  ScreenWakeLockInMainFrame();

  NavigateToURL(shell(), test_in_page_url);
  EXPECT_TRUE(HasWakeLock());
}

IN_PROC_BROWSER_TEST_F(WakeLockTest, UnlockAfterReload) {
  NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html"));
  ScreenWakeLockInMainFrame();

  shell()->Reload();
  WaitForLoadStop(GetWebContents());

  // Screen wake lock should be released after reload.
  EXPECT_FALSE(HasWakeLock());
}

IN_PROC_BROWSER_TEST_F(WakeLockTest, BrowserInitiatedFrameNavigation) {
  NavigateToURL(shell(),
                embedded_test_server()->GetURL("/frame_tree/2-4.html"));

  EXPECT_FALSE(HasWakeLock());

  // Lock screen in nested frame.
  EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;"));
  WaitForPossibleUpdate();

  // Screen wake lock should be present.
  EXPECT_TRUE(HasWakeLock());

  // Navigate the nested frame (browser-initiated).
  NavigateFrameToURL(GetNestedFrameNode(),
                     embedded_test_server()->GetURL("/simple_page.html"));
  WaitForPossibleUpdate();

  // Screen wake lock should be released.
  EXPECT_FALSE(HasWakeLock());
}

IN_PROC_BROWSER_TEST_F(WakeLockTest, RendererInitiatedFrameNavigation) {
  NavigateToURL(shell(),
                embedded_test_server()->GetURL("/frame_tree/2-4.html"));

  EXPECT_FALSE(HasWakeLock());

  // Lock screen in nested frame.
  EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;"));
  WaitForPossibleUpdate();

  // Screen wake lock should be present.
  EXPECT_TRUE(HasWakeLock());

  // Navigate the nested frame (renderer-initiated).
  NavigateIframeToURL(GetWebContents(), "3-1-id",
                      embedded_test_server()->GetURL("/simple_page.html"));
  WaitForPossibleUpdate();

  // Screen wake lock should be released.
  EXPECT_FALSE(HasWakeLock());
}

IN_PROC_BROWSER_TEST_F(WakeLockTest, OutOfProcessFrame) {
  NavigateToURL(shell(), embedded_test_server()->GetURL(
                             "a.com", "/cross_site_iframe_factory.html?a(a)"));
  EXPECT_FALSE(HasWakeLock());

  // Ensure that the nested frame is same-process.
  EXPECT_FALSE(GetNestedFrame()->IsCrossProcessSubframe());

  // Lock screen in same-site nested frame.
  EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;"));
  WaitForPossibleUpdate();
  EXPECT_TRUE(HasWakeLock());

  // Navigate nested frame to a cross-site document.
  NavigateFrameToURL(GetNestedFrameNode(), embedded_test_server()->GetURL(
                                               "b.com", "/simple_page.html"));
  WaitForPossibleUpdate();

  // Ensure that a new process has been created for the nested frame.
  EXPECT_TRUE(GetNestedFrame()->IsCrossProcessSubframe());

  // Screen wake lock should be released.
  EXPECT_FALSE(HasWakeLock());

  // Lock screen in the cross-site nested frame.
  EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;"));
  WaitForPossibleUpdate();

  // Screen wake lock should be created.
  EXPECT_TRUE(HasWakeLock());
}

IN_PROC_BROWSER_TEST_F(WakeLockTest, UnlockAfterCrashOutOfProcessFrame) {
  // Load a page with cross-site iframe.
  NavigateToURL(shell(), embedded_test_server()->GetURL(
                             "a.com", "/cross_site_iframe_factory.html?a(b)"));
  EXPECT_FALSE(HasWakeLock());

  // Ensure that a new process has been created for the nested frame.
  EXPECT_TRUE(GetNestedFrame()->IsCrossProcessSubframe());

  // Lock screen in cross-site nested frame.
  EXPECT_TRUE(ExecuteScript(GetNestedFrame(), "screen.keepAwake = true;"));
  WaitForPossibleUpdate();
  EXPECT_TRUE(HasWakeLock());

  // Crash process that owns the out-of-process frame.
  RenderProcessHostWatcher watcher(
      GetNestedFrame()->GetProcess(),
      RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
  GetNestedFrame()->GetProcess()->Shutdown(0, false);
  watcher.Wait();

  // Screen wake lock should be released.
  EXPECT_FALSE(HasWakeLock());
}

}  // namespace content