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. | |
mlamouri (slow - plz ping)
2015/05/05 13:45:44
It's a bit odd to have unit tests in Source/web/ f
| |
4 | |
5 #include "config.h" | |
6 #include "modules/wake_lock/WakeLock.h" | |
7 | |
8 #include "core/dom/DOMImplementation.h" | |
9 #include "core/dom/Document.h" | |
10 #include "core/dom/DocumentInit.h" | |
11 #include "modules/wake_lock/DocumentWakeLock.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> | |
21 | |
22 namespace { | |
23 | |
24 using blink::DocumentWakeLock; | |
25 using blink::WebWakeLockClient; | |
26 | |
27 class TestWebWakeLockClient: public WebWakeLockClient { | |
28 public: | |
29 TestWebWakeLockClient(): m_keepScreenAwake(false) { } | |
30 | |
31 void requestKeepScreenAwake(bool keepScreenAwake) override | |
32 { | |
33 m_keepScreenAwake = keepScreenAwake; | |
34 } | |
35 | |
36 bool keepScreenAwake() const { return m_keepScreenAwake; } | |
37 | |
38 private: | |
39 bool m_keepScreenAwake; | |
40 }; | |
41 | |
42 class TestWebFrameClient: public blink::FrameTestHelpers::TestWebFrameClient { | |
43 public: | |
44 WebWakeLockClient* wakeLockClient() override | |
45 { | |
46 return &m_testWebWakeLockClient; | |
47 } | |
48 | |
49 const TestWebWakeLockClient& testWebWakeLockClient() const | |
50 { | |
51 return m_testWebWakeLockClient; | |
52 } | |
53 | |
54 private: | |
55 TestWebWakeLockClient m_testWebWakeLockClient; | |
56 }; | |
57 | |
58 class WakeLockTest: public testing::Test { | |
59 protected: | |
60 void SetUp() override | |
61 { | |
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(); | |
67 } | |
68 | |
69 void TearDown() override | |
70 { | |
71 blink::Platform::current()->unitTestSupport()->unregisterAllMockedURLs() ; | |
72 } | |
73 | |
74 void loadFrame() | |
75 { | |
76 blink::FrameTestHelpers::loadFrame( | |
77 m_webViewHelper.webView()->mainFrame(), | |
78 "http://example.com/foo.html"); | |
79 m_webViewHelper.webViewImpl()->layout(); | |
80 } | |
81 | |
82 blink::LocalFrame* frame() | |
83 { | |
84 ASSERT(m_webViewHelper.webViewImpl()); | |
85 ASSERT(m_webViewHelper.webViewImpl()->mainFrameImpl()); | |
86 return m_webViewHelper.webViewImpl()->mainFrameImpl()->frame(); | |
87 } | |
88 | |
89 blink::Document* document() | |
90 { | |
91 ASSERT(frame()); | |
92 return frame()->document(); | |
93 } | |
94 | |
95 bool documentKeepScreenAwake() | |
96 { | |
97 ASSERT(document()); | |
98 return DocumentWakeLock::keepScreenAwake(*document()); | |
99 } | |
100 | |
101 bool clientKeepScreenAwake() | |
102 { | |
103 return m_testWebFrameClient.testWebWakeLockClient().keepScreenAwake(); | |
104 } | |
105 | |
106 void setKeepScreenAwake(bool keepScreenAwake) | |
107 { | |
108 ASSERT(document()); | |
109 DocumentWakeLock::setKeepScreenAwake(*document(), keepScreenAwake); | |
110 } | |
111 | |
112 void show() | |
113 { | |
114 ASSERT(m_webViewHelper.webView()); | |
115 m_webViewHelper.webView()->setVisibilityState( | |
116 blink::WebPageVisibilityStateVisible, false); | |
117 ASSERT(!document()->hidden()); | |
118 } | |
119 | |
120 void hide() | |
121 { | |
122 ASSERT(m_webViewHelper.webView()); | |
123 m_webViewHelper.webView()->setVisibilityState( | |
124 blink::WebPageVisibilityStateHidden, false); | |
125 ASSERT(document()->hidden()); | |
126 } | |
127 | |
128 // Order of these members is important as we need to make sure that | |
129 // m_testWebFrameClient outlives m_webViewHelper (destruction order) | |
130 TestWebFrameClient m_testWebFrameClient; | |
131 blink::FrameTestHelpers::WebViewHelper m_webViewHelper; | |
132 }; | |
133 | |
134 TEST_F(WakeLockTest, setAndReset) | |
135 { | |
136 ASSERT_FALSE(documentKeepScreenAwake()); | |
137 ASSERT_FALSE(clientKeepScreenAwake()); | |
138 | |
139 setKeepScreenAwake(true); | |
140 EXPECT_TRUE(documentKeepScreenAwake()); | |
141 EXPECT_TRUE(clientKeepScreenAwake()); | |
142 | |
143 setKeepScreenAwake(false); | |
144 EXPECT_FALSE(documentKeepScreenAwake()); | |
145 EXPECT_FALSE(clientKeepScreenAwake()); | |
146 } | |
147 | |
148 TEST_F(WakeLockTest, hideWhenSet) | |
149 { | |
150 ASSERT_FALSE(documentKeepScreenAwake()); | |
151 ASSERT_FALSE(clientKeepScreenAwake()); | |
152 | |
153 setKeepScreenAwake(true); | |
154 hide(); | |
155 | |
156 EXPECT_TRUE(documentKeepScreenAwake()); | |
157 EXPECT_FALSE(clientKeepScreenAwake()); | |
158 } | |
159 | |
160 TEST_F(WakeLockTest, setWhenHidden) | |
161 { | |
162 ASSERT_FALSE(documentKeepScreenAwake()); | |
163 ASSERT_FALSE(clientKeepScreenAwake()); | |
164 | |
165 hide(); | |
166 setKeepScreenAwake(true); | |
167 | |
168 EXPECT_TRUE(documentKeepScreenAwake()); | |
169 EXPECT_FALSE(clientKeepScreenAwake()); | |
170 } | |
171 | |
172 TEST_F(WakeLockTest, showWhenSet) | |
173 { | |
174 ASSERT_FALSE(documentKeepScreenAwake()); | |
175 ASSERT_FALSE(clientKeepScreenAwake()); | |
176 | |
177 hide(); | |
178 setKeepScreenAwake(true); | |
179 show(); | |
180 | |
181 EXPECT_TRUE(documentKeepScreenAwake()); | |
182 EXPECT_TRUE(clientKeepScreenAwake()); | |
183 } | |
184 | |
185 TEST_F(WakeLockTest, navigate) | |
186 { | |
187 ASSERT_FALSE(documentKeepScreenAwake()); | |
188 ASSERT_FALSE(clientKeepScreenAwake()); | |
189 | |
190 setKeepScreenAwake(true); | |
191 loadFrame(); | |
192 | |
193 EXPECT_FALSE(documentKeepScreenAwake()); | |
194 EXPECT_FALSE(clientKeepScreenAwake()); | |
195 } | |
196 | |
197 TEST_F(WakeLockTest, documentWithoutBrowsingContext) | |
198 { | |
199 RefPtrWillBeRawPtr<blink::Document> document = | |
200 blink::DOMImplementation::createDocument("", blink::DocumentInit(), fals e); | |
201 ASSERT_FALSE(document->frame()); | |
202 | |
203 blink::WakeLock::setKeepScreenAwake(*document.get(), true); | |
204 EXPECT_TRUE(blink::WakeLock::keepScreenAwake(*document.get())); | |
205 } | |
206 | |
207 } // namespace | |
OLD | NEW |