Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: third_party/WebKit/Source/modules/wake_lock/ScreenWakeLockTest.cpp

Issue 1794553002: [Playground] Onion Soup: moving ScreenWakeLock to Blink modules/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nit DEPS Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "modules/wake_lock/ScreenWakeLock.h" 5 #include "modules/wake_lock/ScreenWakeLock.h"
6 6
7 #include "base/run_loop.h"
7 #include "core/dom/DOMImplementation.h" 8 #include "core/dom/DOMImplementation.h"
8 #include "core/dom/Document.h" 9 #include "core/dom/Document.h"
9 #include "core/dom/DocumentInit.h" 10 #include "core/dom/DocumentInit.h"
10 #include "core/frame/LocalDOMWindow.h" 11 #include "mojo/public/cpp/bindings/interface_request.h"
11 #include "platform/heap/Handle.h" 12 #include "mojo/public/cpp/bindings/strong_binding.h"
12 #include "platform/testing/URLTestHelpers.h" 13 #include "platform/testing/URLTestHelpers.h"
13 #include "public/platform/Platform.h" 14 #include "public/platform/Platform.h"
15 #include "public/platform/ServiceRegistry.h"
14 #include "public/platform/WebPageVisibilityState.h" 16 #include "public/platform/WebPageVisibilityState.h"
15 #include "public/platform/WebURLLoaderMockFactory.h" 17 #include "public/platform/WebURLLoaderMockFactory.h"
16 #include "public/platform/modules/wake_lock/WebWakeLockClient.h"
17 #include "public/web/WebCache.h" 18 #include "public/web/WebCache.h"
18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
19 #include "web/WebLocalFrameImpl.h" 20 #include "web/WebLocalFrameImpl.h"
20 #include "web/tests/FrameTestHelpers.h" 21 #include "web/tests/FrameTestHelpers.h"
21 22
22 namespace { 23 namespace {
23 24
24 using blink::ScreenWakeLock; 25 using blink::ScreenWakeLock;
25 using blink::WebWakeLockClient; 26 using blink::mojom::WakeLockService;
27 using blink::mojom::WakeLockServiceRequest;
26 28
27 class TestWebWakeLockClient: public WebWakeLockClient { 29 // This class allows connecting service requests to a MockWakeLockService.
30 class MockServiceRegistry : public blink::ServiceRegistry {
28 public: 31 public:
29 TestWebWakeLockClient(): m_keepScreenAwake(false) { } 32 MockServiceRegistry() : m_wakeLockStatus(false) {}
33 ~MockServiceRegistry() = default;
30 34
31 void requestKeepScreenAwake(bool keepScreenAwake) override 35 void connectToRemoteService(const char* name, mojo::ScopedMessagePipeHandle) override;
32 {
33 m_keepScreenAwake = keepScreenAwake;
34 }
35 36
36 bool keepScreenAwake() const { return m_keepScreenAwake; } 37 bool wakeLockStatus() const { return m_wakeLockStatus; }
38 void setWakeLockStatus(bool status) { m_wakeLockStatus = status; }
37 39
38 private: 40 private:
39 bool m_keepScreenAwake; 41 bool m_wakeLockStatus;
40 }; 42 };
41 43
42 class TestWebFrameClient: public blink::FrameTestHelpers::TestWebFrameClient { 44 // This class is a mock WakeLockService to intercept calls to the mojo methods.
45 class MockWakeLockService : public WakeLockService {
43 public: 46 public:
44 WebWakeLockClient* wakeLockClient() override 47 static void Create(MockServiceRegistry* registry, WakeLockServiceRequest req uest)
45 { 48 {
46 return &m_testWebWakeLockClient; 49 // See comment below in |m_binding_| for why this doesn't leak.
50 new MockWakeLockService(registry, std::move(request));
47 } 51 }
48 52 ~MockWakeLockService() = default;
49 const TestWebWakeLockClient& testWebWakeLockClient() const
50 {
51 return m_testWebWakeLockClient;
52 }
53 53
54 private: 54 private:
55 TestWebWakeLockClient m_testWebWakeLockClient; 55 MockWakeLockService(MockServiceRegistry* registry, WakeLockServiceRequest re quest)
56 : m_binding(this, std::move(request))
57 , m_registry(registry) {}
58
59 // mojom::WakeLockService
60 void RequestWakeLock() override { m_registry->setWakeLockStatus(true); }
61 void CancelWakeLock() override { m_registry->setWakeLockStatus(false); }
62
63 // A StrongBinding is just like a Binding, except that it takes ownership of
64 // its bound implementation and deletes itself (and the impl) if and when th e
65 // bound pipe encounters an error or is closed on the other end.
66 mojo::StrongBinding<WakeLockService> m_binding;
67 MockServiceRegistry* const m_registry;
68 };
69
70 // This implementation goes here for MockWakeLockService to be defined.
71 void MockServiceRegistry::connectToRemoteService(const char* name, mojo::ScopedM essagePipeHandle handle)
72 {
73 MockWakeLockService::Create(this, mojo::MakeRequest<WakeLockService>(std::mo ve(handle)));
74 }
75
76 // A TestWebFrameClient to allow overriding the serviceRegistry() with a mock.
77 class TestWebFrameClient : public blink::FrameTestHelpers::TestWebFrameClient {
78 public:
79 ~TestWebFrameClient() override = default;
80 blink::ServiceRegistry* serviceRegistry() override { return &m_serviceRegist ry; }
81
82 private:
83 MockServiceRegistry m_serviceRegistry;
56 }; 84 };
57 85
58 class ScreenWakeLockTest: public testing::Test { 86 class ScreenWakeLockTest: public testing::Test {
59 protected: 87 protected:
60 void SetUp() override 88 void SetUp() override
61 { 89 {
62 m_webViewHelper.initialize(true, &m_testWebFrameClient); 90 m_webViewHelper.initialize(true, &m_testWebFrameClient);
63 blink::URLTestHelpers::registerMockedURLFromBaseURL( 91 blink::URLTestHelpers::registerMockedURLFromBaseURL(
64 blink::WebString::fromUTF8("http://example.com/"), 92 blink::WebString::fromUTF8("http://example.com/"),
65 blink::WebString::fromUTF8("foo.html")); 93 blink::WebString::fromUTF8("foo.html"));
(...skipping 29 matching lines...) Expand all
95 } 123 }
96 124
97 bool screenKeepAwake() 125 bool screenKeepAwake()
98 { 126 {
99 DCHECK(screen()); 127 DCHECK(screen());
100 return ScreenWakeLock::keepAwake(*screen()); 128 return ScreenWakeLock::keepAwake(*screen());
101 } 129 }
102 130
103 bool clientKeepScreenAwake() 131 bool clientKeepScreenAwake()
104 { 132 {
105 return m_testWebFrameClient.testWebWakeLockClient().keepScreenAwake(); 133 return static_cast<MockServiceRegistry*>(m_testWebFrameClient.serviceReg istry())->wakeLockStatus();
106 } 134 }
107 135
108 void setKeepAwake(bool keepAwake) 136 void setKeepAwake(bool keepAwake)
109 { 137 {
110 DCHECK(screen()); 138 DCHECK(screen());
111 ScreenWakeLock::setKeepAwake(*screen(), keepAwake); 139 ScreenWakeLock::setKeepAwake(*screen(), keepAwake);
140 // Let the notification sink through the mojo pipes.
141 base::RunLoop().RunUntilIdle();
112 } 142 }
113 143
114 void show() 144 void show()
115 { 145 {
116 DCHECK(m_webViewHelper.webView()); 146 DCHECK(m_webViewHelper.webView());
117 m_webViewHelper.webView()->setVisibilityState( 147 m_webViewHelper.webView()->setVisibilityState(
118 blink::WebPageVisibilityStateVisible, false); 148 blink::WebPageVisibilityStateVisible, false);
149 // Let the notification sink through the mojo pipes.
150 base::RunLoop().RunUntilIdle();
119 } 151 }
120 152
121 void hide() 153 void hide()
122 { 154 {
123 DCHECK(m_webViewHelper.webView()); 155 DCHECK(m_webViewHelper.webView());
124 m_webViewHelper.webView()->setVisibilityState( 156 m_webViewHelper.webView()->setVisibilityState(
125 blink::WebPageVisibilityStateHidden, false); 157 blink::WebPageVisibilityStateHidden, false);
158 // Let the notification sink through the mojo pipes.
159 base::RunLoop().RunUntilIdle();
126 } 160 }
127 161
128 // Order of these members is important as we need to make sure that 162 // Order of these members is important as we need to make sure that
129 // m_testWebFrameClient outlives m_webViewHelper (destruction order) 163 // m_testWebFrameClient outlives m_webViewHelper (destruction order)
130 TestWebFrameClient m_testWebFrameClient; 164 TestWebFrameClient m_testWebFrameClient;
131 blink::FrameTestHelpers::WebViewHelper m_webViewHelper; 165 blink::FrameTestHelpers::WebViewHelper m_webViewHelper;
132 }; 166 };
133 167
134 TEST_F(ScreenWakeLockTest, setAndReset) 168 TEST_F(ScreenWakeLockTest, setAndReset)
135 { 169 {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 ASSERT_FALSE(clientKeepScreenAwake()); 222 ASSERT_FALSE(clientKeepScreenAwake());
189 223
190 setKeepAwake(true); 224 setKeepAwake(true);
191 loadFrame(); 225 loadFrame();
192 226
193 EXPECT_FALSE(screenKeepAwake()); 227 EXPECT_FALSE(screenKeepAwake());
194 EXPECT_FALSE(clientKeepScreenAwake()); 228 EXPECT_FALSE(clientKeepScreenAwake());
195 } 229 }
196 230
197 } // namespace 231 } // namespace
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/wake_lock/ScreenWakeLock.cpp ('k') | third_party/WebKit/Source/platform/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698