Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / web / tests / ScreenWakeLockTest.cpp
blob51a7e44453c3b6e92954168228c1c4bf3afbcfbf
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.
5 #include "config.h"
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>
22 namespace {
24 using blink::ScreenWakeLock;
25 using blink::WebWakeLockClient;
27 class TestWebWakeLockClient: public WebWakeLockClient {
28 public:
29 TestWebWakeLockClient(): m_keepScreenAwake(false) { }
31 void requestKeepScreenAwake(bool keepScreenAwake) override
33 m_keepScreenAwake = keepScreenAwake;
36 bool keepScreenAwake() const { return m_keepScreenAwake; }
38 private:
39 bool m_keepScreenAwake;
42 class TestWebFrameClient: public blink::FrameTestHelpers::TestWebFrameClient {
43 public:
44 WebWakeLockClient* wakeLockClient() override
46 return &m_testWebWakeLockClient;
49 const TestWebWakeLockClient& testWebWakeLockClient() const
51 return m_testWebWakeLockClient;
54 private:
55 TestWebWakeLockClient m_testWebWakeLockClient;
58 class ScreenWakeLockTest: public testing::Test {
59 protected:
60 void SetUp() override
62 m_webViewHelper.initialize(true, &m_testWebFrameClient);
63 blink::URLTestHelpers::registerMockedURLFromBaseURL(
64 blink::WebString::fromUTF8("http://example.com/"),
65 blink::WebString::fromUTF8("foo.html"));
66 loadFrame();
69 void TearDown() override
71 blink::Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
74 void loadFrame()
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()
91 ASSERT(frame());
92 ASSERT(frame()->localDOMWindow());
93 return frame()->localDOMWindow()->screen();
96 bool screenKeepAwake()
98 ASSERT(screen());
99 return ScreenWakeLock::keepAwake(*screen());
102 bool clientKeepScreenAwake()
104 return m_testWebFrameClient.testWebWakeLockClient().keepScreenAwake();
107 void setKeepAwake(bool keepAwake)
109 ASSERT(screen());
110 ScreenWakeLock::setKeepAwake(*screen(), keepAwake);
113 void show()
115 ASSERT(m_webViewHelper.webView());
116 m_webViewHelper.webView()->setVisibilityState(
117 blink::WebPageVisibilityStateVisible, false);
120 void hide()
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());
138 setKeepAwake(true);
139 EXPECT_TRUE(screenKeepAwake());
140 EXPECT_TRUE(clientKeepScreenAwake());
142 setKeepAwake(false);
143 EXPECT_FALSE(screenKeepAwake());
144 EXPECT_FALSE(clientKeepScreenAwake());
147 TEST_F(ScreenWakeLockTest, hideWhenSet)
149 ASSERT_FALSE(screenKeepAwake());
150 ASSERT_FALSE(clientKeepScreenAwake());
152 setKeepAwake(true);
153 hide();
155 EXPECT_TRUE(screenKeepAwake());
156 EXPECT_FALSE(clientKeepScreenAwake());
159 TEST_F(ScreenWakeLockTest, setWhenHidden)
161 ASSERT_FALSE(screenKeepAwake());
162 ASSERT_FALSE(clientKeepScreenAwake());
164 hide();
165 setKeepAwake(true);
167 EXPECT_TRUE(screenKeepAwake());
168 EXPECT_FALSE(clientKeepScreenAwake());
171 TEST_F(ScreenWakeLockTest, showWhenSet)
173 ASSERT_FALSE(screenKeepAwake());
174 ASSERT_FALSE(clientKeepScreenAwake());
176 hide();
177 setKeepAwake(true);
178 show();
180 EXPECT_TRUE(screenKeepAwake());
181 EXPECT_TRUE(clientKeepScreenAwake());
184 TEST_F(ScreenWakeLockTest, navigate)
186 ASSERT_FALSE(screenKeepAwake());
187 ASSERT_FALSE(clientKeepScreenAwake());
189 setKeepAwake(true);
190 loadFrame();
192 EXPECT_FALSE(screenKeepAwake());
193 EXPECT_FALSE(clientKeepScreenAwake());
196 } // namespace