| 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 "mojo/public/cpp/bindings/interface_request.h" | 10 #include "mojo/public/cpp/bindings/interface_request.h" |
| 11 #include "mojo/public/cpp/bindings/strong_binding.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 "platform/testing/UnitTestHelpers.h" |
| 14 #include "public/platform/InterfaceProvider.h" | 14 #include "public/platform/InterfaceProvider.h" |
| 15 #include "public/platform/Platform.h" | 15 #include "public/platform/Platform.h" |
| 16 #include "public/platform/WebPageVisibilityState.h" | 16 #include "public/platform/WebPageVisibilityState.h" |
| 17 #include "public/platform/WebURLLoaderMockFactory.h" | 17 #include "public/platform/WebURLLoaderMockFactory.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "web/WebLocalFrameImpl.h" | 19 #include "web/WebLocalFrameImpl.h" |
| 20 #include "web/tests/FrameTestHelpers.h" | 20 #include "web/tests/FrameTestHelpers.h" |
| 21 | 21 |
| 22 #include <memory> | 22 #include <memory> |
| 23 | 23 |
| 24 namespace blink { |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 using blink::ScreenWakeLock; | |
| 27 using device::mojom::blink::WakeLockService; | 27 using device::mojom::blink::WakeLockService; |
| 28 using device::mojom::blink::WakeLockServiceRequest; | 28 using device::mojom::blink::WakeLockServiceRequest; |
| 29 | 29 |
| 30 // This class allows binding interface requests to a MockWakeLockService. | 30 // This class allows binding interface requests to a MockWakeLockService. |
| 31 class MockInterfaceProvider : public blink::InterfaceProvider { | 31 class MockInterfaceProvider : public InterfaceProvider { |
| 32 public: | 32 public: |
| 33 MockInterfaceProvider() : m_wakeLockStatus(false) {} | 33 MockInterfaceProvider() : wake_lock_status_(false) {} |
| 34 ~MockInterfaceProvider() {} | 34 ~MockInterfaceProvider() {} |
| 35 | 35 |
| 36 void GetInterface(const char* name, mojo::ScopedMessagePipeHandle) override; | 36 void GetInterface(const char* name, mojo::ScopedMessagePipeHandle) override; |
| 37 | 37 |
| 38 bool wakeLockStatus() const { return m_wakeLockStatus; } | 38 bool WakeLockStatus() const { return wake_lock_status_; } |
| 39 void setWakeLockStatus(bool status) { m_wakeLockStatus = status; } | 39 void SetWakeLockStatus(bool status) { wake_lock_status_ = status; } |
| 40 | 40 |
| 41 private: | 41 private: |
| 42 // A mock WakeLockService used to intercept calls to the mojo methods. | 42 // A mock WakeLockService used to intercept calls to the mojo methods. |
| 43 class MockWakeLockService : public WakeLockService { | 43 class MockWakeLockService : public WakeLockService { |
| 44 public: | 44 public: |
| 45 MockWakeLockService(MockInterfaceProvider* registry, | 45 MockWakeLockService(MockInterfaceProvider* registry, |
| 46 WakeLockServiceRequest request) | 46 WakeLockServiceRequest request) |
| 47 : m_binding(this, std::move(request)), m_registry(registry) {} | 47 : binding_(this, std::move(request)), registry_(registry) {} |
| 48 ~MockWakeLockService() {} | 48 ~MockWakeLockService() {} |
| 49 | 49 |
| 50 private: | 50 private: |
| 51 // mojom::WakeLockService | 51 // mojom::WakeLockService |
| 52 void RequestWakeLock() override { m_registry->setWakeLockStatus(true); } | 52 void RequestWakeLock() override { registry_->SetWakeLockStatus(true); } |
| 53 void CancelWakeLock() override { m_registry->setWakeLockStatus(false); } | 53 void CancelWakeLock() override { registry_->SetWakeLockStatus(false); } |
| 54 | 54 |
| 55 mojo::Binding<WakeLockService> m_binding; | 55 mojo::Binding<WakeLockService> binding_; |
| 56 MockInterfaceProvider* const m_registry; | 56 MockInterfaceProvider* const registry_; |
| 57 }; | 57 }; |
| 58 std::unique_ptr<MockWakeLockService> m_mockWakeLockService; | 58 std::unique_ptr<MockWakeLockService> mock_wake_lock_service_; |
| 59 | 59 |
| 60 bool m_wakeLockStatus; | 60 bool wake_lock_status_; |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 void MockInterfaceProvider::GetInterface(const char* name, | 63 void MockInterfaceProvider::GetInterface(const char* name, |
| 64 mojo::ScopedMessagePipeHandle handle) { | 64 mojo::ScopedMessagePipeHandle handle) { |
| 65 m_mockWakeLockService.reset(new MockWakeLockService( | 65 mock_wake_lock_service_.reset(new MockWakeLockService( |
| 66 this, mojo::MakeRequest<WakeLockService>(std::move(handle)))); | 66 this, mojo::MakeRequest<WakeLockService>(std::move(handle)))); |
| 67 } | 67 } |
| 68 | 68 |
| 69 // A TestWebFrameClient to allow overriding the interfaceProvider() with a mock. | 69 // A TestWebFrameClient to allow overriding the interfaceProvider() with a mock. |
| 70 class TestWebFrameClient : public blink::FrameTestHelpers::TestWebFrameClient { | 70 class TestWebFrameClient : public FrameTestHelpers::TestWebFrameClient { |
| 71 public: | 71 public: |
| 72 ~TestWebFrameClient() override = default; | 72 ~TestWebFrameClient() override = default; |
| 73 blink::InterfaceProvider* GetInterfaceProvider() override { | 73 InterfaceProvider* GetInterfaceProvider() override { |
| 74 return &m_interfaceProvider; | 74 return &interface_provider_; |
| 75 } | 75 } |
| 76 | 76 |
| 77 private: | 77 private: |
| 78 MockInterfaceProvider m_interfaceProvider; | 78 MockInterfaceProvider interface_provider_; |
| 79 }; | 79 }; |
| 80 | 80 |
| 81 class ScreenWakeLockTest : public testing::Test { | 81 class ScreenWakeLockTest : public ::testing::Test { |
| 82 protected: | 82 protected: |
| 83 void SetUp() override { | 83 void SetUp() override { |
| 84 m_webViewHelper.Initialize(true, &m_testWebFrameClient); | 84 web_view_helper_.Initialize(true, &test_web_frame_client_); |
| 85 blink::URLTestHelpers::RegisterMockedURLLoadFromBase( | 85 URLTestHelpers::RegisterMockedURLLoadFromBase( |
| 86 blink::WebString::FromUTF8("http://example.com/"), | 86 WebString::FromUTF8("http://example.com/"), testing::WebTestDataPath(), |
| 87 blink::testing::WebTestDataPath(), | 87 WebString::FromUTF8("foo.html")); |
| 88 blink::WebString::FromUTF8("foo.html")); | 88 LoadFrame(); |
| 89 loadFrame(); | |
| 90 } | 89 } |
| 91 | 90 |
| 92 void TearDown() override { | 91 void TearDown() override { |
| 93 blink::Platform::Current() | 92 Platform::Current() |
| 94 ->GetURLLoaderMockFactory() | 93 ->GetURLLoaderMockFactory() |
| 95 ->UnregisterAllURLsAndClearMemoryCache(); | 94 ->UnregisterAllURLsAndClearMemoryCache(); |
| 96 blink::testing::RunPendingTasks(); | 95 testing::RunPendingTasks(); |
| 97 } | 96 } |
| 98 | 97 |
| 99 void loadFrame() { | 98 void LoadFrame() { |
| 100 blink::FrameTestHelpers::LoadFrame(m_webViewHelper.WebView()->MainFrame(), | 99 FrameTestHelpers::LoadFrame(web_view_helper_.WebView()->MainFrame(), |
| 101 "http://example.com/foo.html"); | 100 "http://example.com/foo.html"); |
| 102 m_webViewHelper.WebView()->UpdateAllLifecyclePhases(); | 101 web_view_helper_.WebView()->UpdateAllLifecyclePhases(); |
| 103 } | 102 } |
| 104 | 103 |
| 105 blink::LocalFrame* frame() { | 104 LocalFrame* GetFrame() { |
| 106 DCHECK(m_webViewHelper.WebView()); | 105 DCHECK(web_view_helper_.WebView()); |
| 107 DCHECK(m_webViewHelper.WebView()->MainFrameImpl()); | 106 DCHECK(web_view_helper_.WebView()->MainFrameImpl()); |
| 108 return m_webViewHelper.WebView()->MainFrameImpl()->GetFrame(); | 107 return web_view_helper_.WebView()->MainFrameImpl()->GetFrame(); |
| 109 } | 108 } |
| 110 | 109 |
| 111 blink::Screen* screen() { | 110 Screen* GetScreen() { |
| 112 DCHECK(frame()); | 111 DCHECK(GetFrame()); |
| 113 DCHECK(frame()->DomWindow()); | 112 DCHECK(GetFrame()->DomWindow()); |
| 114 return frame()->DomWindow()->screen(); | 113 return GetFrame()->DomWindow()->screen(); |
| 115 } | 114 } |
| 116 | 115 |
| 117 bool screenKeepAwake() { | 116 bool ScreenKeepAwake() { |
| 118 DCHECK(screen()); | 117 DCHECK(GetScreen()); |
| 119 return ScreenWakeLock::keepAwake(*screen()); | 118 return ScreenWakeLock::keepAwake(*GetScreen()); |
| 120 } | 119 } |
| 121 | 120 |
| 122 bool clientKeepScreenAwake() { | 121 bool ClientKeepScreenAwake() { |
| 123 return static_cast<MockInterfaceProvider*>( | 122 return static_cast<MockInterfaceProvider*>( |
| 124 m_testWebFrameClient.GetInterfaceProvider()) | 123 test_web_frame_client_.GetInterfaceProvider()) |
| 125 ->wakeLockStatus(); | 124 ->WakeLockStatus(); |
| 126 } | 125 } |
| 127 | 126 |
| 128 void setKeepAwake(bool keepAwake) { | 127 void SetKeepAwake(bool keepAwake) { |
| 129 DCHECK(screen()); | 128 DCHECK(GetScreen()); |
| 130 ScreenWakeLock::setKeepAwake(*screen(), keepAwake); | 129 ScreenWakeLock::setKeepAwake(*GetScreen(), keepAwake); |
| 131 // Let the notification sink through the mojo pipes. | 130 // Let the notification sink through the mojo pipes. |
| 132 blink::testing::RunPendingTasks(); | 131 testing::RunPendingTasks(); |
| 133 } | 132 } |
| 134 | 133 |
| 135 void show() { | 134 void Show() { |
| 136 DCHECK(m_webViewHelper.WebView()); | 135 DCHECK(web_view_helper_.WebView()); |
| 137 m_webViewHelper.WebView()->SetVisibilityState( | 136 web_view_helper_.WebView()->SetVisibilityState( |
| 138 blink::kWebPageVisibilityStateVisible, false); | 137 kWebPageVisibilityStateVisible, false); |
| 139 // Let the notification sink through the mojo pipes. | 138 // Let the notification sink through the mojo pipes. |
| 140 blink::testing::RunPendingTasks(); | 139 testing::RunPendingTasks(); |
| 141 } | 140 } |
| 142 | 141 |
| 143 void hide() { | 142 void Hide() { |
| 144 DCHECK(m_webViewHelper.WebView()); | 143 DCHECK(web_view_helper_.WebView()); |
| 145 m_webViewHelper.WebView()->SetVisibilityState( | 144 web_view_helper_.WebView()->SetVisibilityState( |
| 146 blink::kWebPageVisibilityStateHidden, false); | 145 kWebPageVisibilityStateHidden, false); |
| 147 // Let the notification sink through the mojo pipes. | 146 // Let the notification sink through the mojo pipes. |
| 148 blink::testing::RunPendingTasks(); | 147 testing::RunPendingTasks(); |
| 149 } | 148 } |
| 150 | 149 |
| 151 // Order of these members is important as we need to make sure that | 150 // Order of these members is important as we need to make sure that |
| 152 // m_testWebFrameClient outlives m_webViewHelper (destruction order) | 151 // test_web_frame_client_ outlives web_view_helper_ (destruction order) |
| 153 TestWebFrameClient m_testWebFrameClient; | 152 TestWebFrameClient test_web_frame_client_; |
| 154 blink::FrameTestHelpers::WebViewHelper m_webViewHelper; | 153 FrameTestHelpers::WebViewHelper web_view_helper_; |
| 155 }; | 154 }; |
| 156 | 155 |
| 157 TEST_F(ScreenWakeLockTest, setAndReset) { | 156 TEST_F(ScreenWakeLockTest, setAndReset) { |
| 158 ASSERT_FALSE(screenKeepAwake()); | 157 ASSERT_FALSE(ScreenKeepAwake()); |
| 159 ASSERT_FALSE(clientKeepScreenAwake()); | 158 ASSERT_FALSE(ClientKeepScreenAwake()); |
| 160 | 159 |
| 161 setKeepAwake(true); | 160 SetKeepAwake(true); |
| 162 EXPECT_TRUE(screenKeepAwake()); | 161 EXPECT_TRUE(ScreenKeepAwake()); |
| 163 EXPECT_TRUE(clientKeepScreenAwake()); | 162 EXPECT_TRUE(ClientKeepScreenAwake()); |
| 164 | 163 |
| 165 setKeepAwake(false); | 164 SetKeepAwake(false); |
| 166 EXPECT_FALSE(screenKeepAwake()); | 165 EXPECT_FALSE(ScreenKeepAwake()); |
| 167 EXPECT_FALSE(clientKeepScreenAwake()); | 166 EXPECT_FALSE(ClientKeepScreenAwake()); |
| 168 } | 167 } |
| 169 | 168 |
| 170 TEST_F(ScreenWakeLockTest, hideWhenSet) { | 169 TEST_F(ScreenWakeLockTest, hideWhenSet) { |
| 171 ASSERT_FALSE(screenKeepAwake()); | 170 ASSERT_FALSE(ScreenKeepAwake()); |
| 172 ASSERT_FALSE(clientKeepScreenAwake()); | 171 ASSERT_FALSE(ClientKeepScreenAwake()); |
| 173 | 172 |
| 174 setKeepAwake(true); | 173 SetKeepAwake(true); |
| 175 hide(); | 174 Hide(); |
| 176 | 175 |
| 177 EXPECT_TRUE(screenKeepAwake()); | 176 EXPECT_TRUE(ScreenKeepAwake()); |
| 178 EXPECT_FALSE(clientKeepScreenAwake()); | 177 EXPECT_FALSE(ClientKeepScreenAwake()); |
| 179 } | 178 } |
| 180 | 179 |
| 181 TEST_F(ScreenWakeLockTest, setWhenHidden) { | 180 TEST_F(ScreenWakeLockTest, setWhenHidden) { |
| 182 ASSERT_FALSE(screenKeepAwake()); | 181 ASSERT_FALSE(ScreenKeepAwake()); |
| 183 ASSERT_FALSE(clientKeepScreenAwake()); | 182 ASSERT_FALSE(ClientKeepScreenAwake()); |
| 184 | 183 |
| 185 hide(); | 184 Hide(); |
| 186 setKeepAwake(true); | 185 SetKeepAwake(true); |
| 187 | 186 |
| 188 EXPECT_TRUE(screenKeepAwake()); | 187 EXPECT_TRUE(ScreenKeepAwake()); |
| 189 EXPECT_FALSE(clientKeepScreenAwake()); | 188 EXPECT_FALSE(ClientKeepScreenAwake()); |
| 190 } | 189 } |
| 191 | 190 |
| 192 TEST_F(ScreenWakeLockTest, showWhenSet) { | 191 TEST_F(ScreenWakeLockTest, showWhenSet) { |
| 193 ASSERT_FALSE(screenKeepAwake()); | 192 ASSERT_FALSE(ScreenKeepAwake()); |
| 194 ASSERT_FALSE(clientKeepScreenAwake()); | 193 ASSERT_FALSE(ClientKeepScreenAwake()); |
| 195 | 194 |
| 196 hide(); | 195 Hide(); |
| 197 setKeepAwake(true); | 196 SetKeepAwake(true); |
| 198 show(); | 197 Show(); |
| 199 | 198 |
| 200 EXPECT_TRUE(screenKeepAwake()); | 199 EXPECT_TRUE(ScreenKeepAwake()); |
| 201 EXPECT_TRUE(clientKeepScreenAwake()); | 200 EXPECT_TRUE(ClientKeepScreenAwake()); |
| 202 } | 201 } |
| 203 | 202 |
| 204 TEST_F(ScreenWakeLockTest, navigate) { | 203 TEST_F(ScreenWakeLockTest, navigate) { |
| 205 ASSERT_FALSE(screenKeepAwake()); | 204 ASSERT_FALSE(ScreenKeepAwake()); |
| 206 ASSERT_FALSE(clientKeepScreenAwake()); | 205 ASSERT_FALSE(ClientKeepScreenAwake()); |
| 207 | 206 |
| 208 setKeepAwake(true); | 207 SetKeepAwake(true); |
| 209 loadFrame(); | 208 LoadFrame(); |
| 210 | 209 |
| 211 EXPECT_FALSE(screenKeepAwake()); | 210 EXPECT_FALSE(ScreenKeepAwake()); |
| 212 EXPECT_FALSE(clientKeepScreenAwake()); | 211 EXPECT_FALSE(ClientKeepScreenAwake()); |
| 213 } | 212 } |
| 214 | 213 |
| 215 } // namespace | 214 } // namespace |
| 215 } // namespace blink |
| OLD | NEW |