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.
6 #include "modules/wake_lock/ScreenWakeLock.h"
8 #include "core/dom/DOMImplementation.h"
9 #include "core/dom/Document.h"
10 #include "core/dom/DocumentInit.h"
11 #include "core/frame/LocalDOMWindow.h"
12 #include "platform/heap/Handle.h"
13 #include "platform/testing/URLTestHelpers.h"
14 #include "public/platform/Platform.h"
15 #include "public/platform/WebPageVisibilityState.h"
16 #include "public/platform/WebUnitTestSupport.h"
17 #include "public/platform/modules/wake_lock/WebWakeLockClient.h"
18 #include "web/WebLocalFrameImpl.h"
19 #include "web/tests/FrameTestHelpers.h"
20 #include <gtest/gtest.h>
24 using blink::ScreenWakeLock
;
25 using blink::WebWakeLockClient
;
27 class TestWebWakeLockClient
: public WebWakeLockClient
{
29 TestWebWakeLockClient(): m_keepScreenAwake(false) { }
31 void requestKeepScreenAwake(bool keepScreenAwake
) override
33 m_keepScreenAwake
= keepScreenAwake
;
36 bool keepScreenAwake() const { return m_keepScreenAwake
; }
39 bool m_keepScreenAwake
;
42 class TestWebFrameClient
: public blink::FrameTestHelpers::TestWebFrameClient
{
44 WebWakeLockClient
* wakeLockClient() override
46 return &m_testWebWakeLockClient
;
49 const TestWebWakeLockClient
& testWebWakeLockClient() const
51 return m_testWebWakeLockClient
;
55 TestWebWakeLockClient m_testWebWakeLockClient
;
58 class ScreenWakeLockTest
: public testing::Test
{
62 m_webViewHelper
.initialize(true, &m_testWebFrameClient
);
63 blink::URLTestHelpers::registerMockedURLFromBaseURL(
64 blink::WebString::fromUTF8("http://example.com/"),
65 blink::WebString::fromUTF8("foo.html"));
69 void TearDown() override
71 blink::Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
76 blink::FrameTestHelpers::loadFrame(
77 m_webViewHelper
.webView()->mainFrame(),
78 "http://example.com/foo.html");
79 m_webViewHelper
.webViewImpl()->layout();
82 blink::LocalFrame
* frame()
84 ASSERT(m_webViewHelper
.webViewImpl());
85 ASSERT(m_webViewHelper
.webViewImpl()->mainFrameImpl());
86 return m_webViewHelper
.webViewImpl()->mainFrameImpl()->frame();
89 blink::Screen
* screen()
92 ASSERT(frame()->localDOMWindow());
93 return frame()->localDOMWindow()->screen();
96 bool screenKeepAwake()
99 return ScreenWakeLock::keepAwake(*screen());
102 bool clientKeepScreenAwake()
104 return m_testWebFrameClient
.testWebWakeLockClient().keepScreenAwake();
107 void setKeepAwake(bool keepAwake
)
110 ScreenWakeLock::setKeepAwake(*screen(), keepAwake
);
115 ASSERT(m_webViewHelper
.webView());
116 m_webViewHelper
.webView()->setVisibilityState(
117 blink::WebPageVisibilityStateVisible
, false);
122 ASSERT(m_webViewHelper
.webView());
123 m_webViewHelper
.webView()->setVisibilityState(
124 blink::WebPageVisibilityStateHidden
, false);
127 // Order of these members is important as we need to make sure that
128 // m_testWebFrameClient outlives m_webViewHelper (destruction order)
129 TestWebFrameClient m_testWebFrameClient
;
130 blink::FrameTestHelpers::WebViewHelper m_webViewHelper
;
133 TEST_F(ScreenWakeLockTest
, setAndReset
)
135 ASSERT_FALSE(screenKeepAwake());
136 ASSERT_FALSE(clientKeepScreenAwake());
139 EXPECT_TRUE(screenKeepAwake());
140 EXPECT_TRUE(clientKeepScreenAwake());
143 EXPECT_FALSE(screenKeepAwake());
144 EXPECT_FALSE(clientKeepScreenAwake());
147 TEST_F(ScreenWakeLockTest
, hideWhenSet
)
149 ASSERT_FALSE(screenKeepAwake());
150 ASSERT_FALSE(clientKeepScreenAwake());
155 EXPECT_TRUE(screenKeepAwake());
156 EXPECT_FALSE(clientKeepScreenAwake());
159 TEST_F(ScreenWakeLockTest
, setWhenHidden
)
161 ASSERT_FALSE(screenKeepAwake());
162 ASSERT_FALSE(clientKeepScreenAwake());
167 EXPECT_TRUE(screenKeepAwake());
168 EXPECT_FALSE(clientKeepScreenAwake());
171 TEST_F(ScreenWakeLockTest
, showWhenSet
)
173 ASSERT_FALSE(screenKeepAwake());
174 ASSERT_FALSE(clientKeepScreenAwake());
180 EXPECT_TRUE(screenKeepAwake());
181 EXPECT_TRUE(clientKeepScreenAwake());
184 TEST_F(ScreenWakeLockTest
, navigate
)
186 ASSERT_FALSE(screenKeepAwake());
187 ASSERT_FALSE(clientKeepScreenAwake());
192 EXPECT_FALSE(screenKeepAwake());
193 EXPECT_FALSE(clientKeepScreenAwake());