| OLD | NEW |
| 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 "core/dom/DOMImplementation.h" | 7 #include "core/dom/DOMImplementation.h" |
| 8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 9 #include "core/dom/DocumentInit.h" | 9 #include "core/dom/DocumentInit.h" |
| 10 #include "core/frame/LocalDOMWindow.h" | 10 #include "mojo/public/cpp/bindings/interface_request.h" |
| 11 #include "platform/heap/Handle.h" | 11 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 12 #include "platform/testing/URLTestHelpers.h" | 12 #include "platform/testing/URLTestHelpers.h" |
| 13 #include "platform/testing/UnitTestHelpers.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::wtf::WakeLockService; |
| 27 using blink::mojom::wtf::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() {} |
| 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 // A mock WakeLockService used to intercept calls to the mojo methods. |
| 42 class MockWakeLockService : public WakeLockService { |
| 43 public: |
| 44 MockWakeLockService(MockServiceRegistry* registry, WakeLockServiceReques
t request) |
| 45 : m_binding(this, std::move(request)) |
| 46 , m_registry(registry) {} |
| 47 ~MockWakeLockService() {} |
| 48 |
| 49 private: |
| 50 // mojom::WakeLockService |
| 51 void RequestWakeLock() override { m_registry->setWakeLockStatus(true); } |
| 52 void CancelWakeLock() override { m_registry->setWakeLockStatus(false); } |
| 53 |
| 54 mojo::Binding<WakeLockService> m_binding; |
| 55 MockServiceRegistry* const m_registry; |
| 56 }; |
| 57 scoped_ptr<MockWakeLockService> m_mockWakeLockService; |
| 58 |
| 59 bool m_wakeLockStatus; |
| 40 }; | 60 }; |
| 41 | 61 |
| 42 class TestWebFrameClient: public blink::FrameTestHelpers::TestWebFrameClient { | 62 void MockServiceRegistry::connectToRemoteService(const char* name, mojo::ScopedM
essagePipeHandle handle) |
| 63 { |
| 64 m_mockWakeLockService.reset( |
| 65 new MockWakeLockService(this, mojo::MakeRequest<WakeLockService>(std::mo
ve(handle)))); |
| 66 } |
| 67 |
| 68 // A TestWebFrameClient to allow overriding the serviceRegistry() with a mock. |
| 69 class TestWebFrameClient : public blink::FrameTestHelpers::TestWebFrameClient { |
| 43 public: | 70 public: |
| 44 WebWakeLockClient* wakeLockClient() override | 71 ~TestWebFrameClient() override = default; |
| 45 { | 72 blink::ServiceRegistry* serviceRegistry() override { return &m_serviceRegist
ry; } |
| 46 return &m_testWebWakeLockClient; | |
| 47 } | |
| 48 | |
| 49 const TestWebWakeLockClient& testWebWakeLockClient() const | |
| 50 { | |
| 51 return m_testWebWakeLockClient; | |
| 52 } | |
| 53 | 73 |
| 54 private: | 74 private: |
| 55 TestWebWakeLockClient m_testWebWakeLockClient; | 75 MockServiceRegistry m_serviceRegistry; |
| 56 }; | 76 }; |
| 57 | 77 |
| 58 class ScreenWakeLockTest: public testing::Test { | 78 class ScreenWakeLockTest: public testing::Test { |
| 59 protected: | 79 protected: |
| 60 void SetUp() override | 80 void SetUp() override |
| 61 { | 81 { |
| 62 m_webViewHelper.initialize(true, &m_testWebFrameClient); | 82 m_webViewHelper.initialize(true, &m_testWebFrameClient); |
| 63 blink::URLTestHelpers::registerMockedURLFromBaseURL( | 83 blink::URLTestHelpers::registerMockedURLFromBaseURL( |
| 64 blink::WebString::fromUTF8("http://example.com/"), | 84 blink::WebString::fromUTF8("http://example.com/"), |
| 65 blink::WebString::fromUTF8("foo.html")); | 85 blink::WebString::fromUTF8("foo.html")); |
| 66 loadFrame(); | 86 loadFrame(); |
| 67 } | 87 } |
| 68 | 88 |
| 69 void TearDown() override | 89 void TearDown() override |
| 70 { | 90 { |
| 71 blink::Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs
(); | 91 blink::Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs
(); |
| 72 blink::WebCache::clear(); | 92 blink::WebCache::clear(); |
| 93 blink::testing::runPendingTasks(); |
| 73 } | 94 } |
| 74 | 95 |
| 75 void loadFrame() | 96 void loadFrame() |
| 76 { | 97 { |
| 77 blink::FrameTestHelpers::loadFrame( | 98 blink::FrameTestHelpers::loadFrame( |
| 78 m_webViewHelper.webView()->mainFrame(), | 99 m_webViewHelper.webView()->mainFrame(), |
| 79 "http://example.com/foo.html"); | 100 "http://example.com/foo.html"); |
| 80 m_webViewHelper.webViewImpl()->updateAllLifecyclePhases(); | 101 m_webViewHelper.webViewImpl()->updateAllLifecyclePhases(); |
| 81 } | 102 } |
| 82 | 103 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 95 } | 116 } |
| 96 | 117 |
| 97 bool screenKeepAwake() | 118 bool screenKeepAwake() |
| 98 { | 119 { |
| 99 DCHECK(screen()); | 120 DCHECK(screen()); |
| 100 return ScreenWakeLock::keepAwake(*screen()); | 121 return ScreenWakeLock::keepAwake(*screen()); |
| 101 } | 122 } |
| 102 | 123 |
| 103 bool clientKeepScreenAwake() | 124 bool clientKeepScreenAwake() |
| 104 { | 125 { |
| 105 return m_testWebFrameClient.testWebWakeLockClient().keepScreenAwake(); | 126 return static_cast<MockServiceRegistry*>(m_testWebFrameClient.serviceReg
istry())->wakeLockStatus(); |
| 106 } | 127 } |
| 107 | 128 |
| 108 void setKeepAwake(bool keepAwake) | 129 void setKeepAwake(bool keepAwake) |
| 109 { | 130 { |
| 110 DCHECK(screen()); | 131 DCHECK(screen()); |
| 111 ScreenWakeLock::setKeepAwake(*screen(), keepAwake); | 132 ScreenWakeLock::setKeepAwake(*screen(), keepAwake); |
| 133 // Let the notification sink through the mojo pipes. |
| 134 blink::testing::runPendingTasks(); |
| 112 } | 135 } |
| 113 | 136 |
| 114 void show() | 137 void show() |
| 115 { | 138 { |
| 116 DCHECK(m_webViewHelper.webView()); | 139 DCHECK(m_webViewHelper.webView()); |
| 117 m_webViewHelper.webView()->setVisibilityState( | 140 m_webViewHelper.webView()->setVisibilityState( |
| 118 blink::WebPageVisibilityStateVisible, false); | 141 blink::WebPageVisibilityStateVisible, false); |
| 142 // Let the notification sink through the mojo pipes. |
| 143 blink::testing::runPendingTasks(); |
| 119 } | 144 } |
| 120 | 145 |
| 121 void hide() | 146 void hide() |
| 122 { | 147 { |
| 123 DCHECK(m_webViewHelper.webView()); | 148 DCHECK(m_webViewHelper.webView()); |
| 124 m_webViewHelper.webView()->setVisibilityState( | 149 m_webViewHelper.webView()->setVisibilityState( |
| 125 blink::WebPageVisibilityStateHidden, false); | 150 blink::WebPageVisibilityStateHidden, false); |
| 151 // Let the notification sink through the mojo pipes. |
| 152 blink::testing::runPendingTasks(); |
| 126 } | 153 } |
| 127 | 154 |
| 128 // Order of these members is important as we need to make sure that | 155 // Order of these members is important as we need to make sure that |
| 129 // m_testWebFrameClient outlives m_webViewHelper (destruction order) | 156 // m_testWebFrameClient outlives m_webViewHelper (destruction order) |
| 130 TestWebFrameClient m_testWebFrameClient; | 157 TestWebFrameClient m_testWebFrameClient; |
| 131 blink::FrameTestHelpers::WebViewHelper m_webViewHelper; | 158 blink::FrameTestHelpers::WebViewHelper m_webViewHelper; |
| 132 }; | 159 }; |
| 133 | 160 |
| 134 TEST_F(ScreenWakeLockTest, setAndReset) | 161 TEST_F(ScreenWakeLockTest, setAndReset) |
| 135 { | 162 { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 ASSERT_FALSE(clientKeepScreenAwake()); | 215 ASSERT_FALSE(clientKeepScreenAwake()); |
| 189 | 216 |
| 190 setKeepAwake(true); | 217 setKeepAwake(true); |
| 191 loadFrame(); | 218 loadFrame(); |
| 192 | 219 |
| 193 EXPECT_FALSE(screenKeepAwake()); | 220 EXPECT_FALSE(screenKeepAwake()); |
| 194 EXPECT_FALSE(clientKeepScreenAwake()); | 221 EXPECT_FALSE(clientKeepScreenAwake()); |
| 195 } | 222 } |
| 196 | 223 |
| 197 } // namespace | 224 } // namespace |
| OLD | NEW |