Chromium Code Reviews| Index: Source/web/tests/WakeLockTest.cpp |
| diff --git a/Source/web/tests/WakeLockTest.cpp b/Source/web/tests/WakeLockTest.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ca5f4d9cb971054aa944f48fb99d06b243f77b0b |
| --- /dev/null |
| +++ b/Source/web/tests/WakeLockTest.cpp |
| @@ -0,0 +1,207 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// 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
|
| + |
| +#include "config.h" |
| +#include "modules/wake_lock/WakeLock.h" |
| + |
| +#include "core/dom/DOMImplementation.h" |
| +#include "core/dom/Document.h" |
| +#include "core/dom/DocumentInit.h" |
| +#include "modules/wake_lock/DocumentWakeLock.h" |
| +#include "platform/heap/Handle.h" |
| +#include "platform/testing/URLTestHelpers.h" |
| +#include "public/platform/Platform.h" |
| +#include "public/platform/WebPageVisibilityState.h" |
| +#include "public/platform/WebUnitTestSupport.h" |
| +#include "public/platform/modules/wake_lock/WebWakeLockClient.h" |
| +#include "web/WebLocalFrameImpl.h" |
| +#include "web/tests/FrameTestHelpers.h" |
| +#include <gtest/gtest.h> |
| + |
| +namespace { |
| + |
| +using blink::DocumentWakeLock; |
| +using blink::WebWakeLockClient; |
| + |
| +class TestWebWakeLockClient: public WebWakeLockClient { |
| +public: |
| + TestWebWakeLockClient(): m_keepScreenAwake(false) { } |
| + |
| + void requestKeepScreenAwake(bool keepScreenAwake) override |
| + { |
| + m_keepScreenAwake = keepScreenAwake; |
| + } |
| + |
| + bool keepScreenAwake() const { return m_keepScreenAwake; } |
| + |
| +private: |
| + bool m_keepScreenAwake; |
| +}; |
| + |
| +class TestWebFrameClient: public blink::FrameTestHelpers::TestWebFrameClient { |
| +public: |
| + WebWakeLockClient* wakeLockClient() override |
| + { |
| + return &m_testWebWakeLockClient; |
| + } |
| + |
| + const TestWebWakeLockClient& testWebWakeLockClient() const |
| + { |
| + return m_testWebWakeLockClient; |
| + } |
| + |
| +private: |
| + TestWebWakeLockClient m_testWebWakeLockClient; |
| +}; |
| + |
| +class WakeLockTest: public testing::Test { |
| +protected: |
| + void SetUp() override |
| + { |
| + m_webViewHelper.initialize(true, &m_testWebFrameClient); |
| + blink::URLTestHelpers::registerMockedURLFromBaseURL( |
| + blink::WebString::fromUTF8("http://example.com/"), |
| + blink::WebString::fromUTF8("foo.html")); |
| + loadFrame(); |
| + } |
| + |
| + void TearDown() override |
| + { |
| + blink::Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); |
| + } |
| + |
| + void loadFrame() |
| + { |
| + blink::FrameTestHelpers::loadFrame( |
| + m_webViewHelper.webView()->mainFrame(), |
| + "http://example.com/foo.html"); |
| + m_webViewHelper.webViewImpl()->layout(); |
| + } |
| + |
| + blink::LocalFrame* frame() |
| + { |
| + ASSERT(m_webViewHelper.webViewImpl()); |
| + ASSERT(m_webViewHelper.webViewImpl()->mainFrameImpl()); |
| + return m_webViewHelper.webViewImpl()->mainFrameImpl()->frame(); |
| + } |
| + |
| + blink::Document* document() |
| + { |
| + ASSERT(frame()); |
| + return frame()->document(); |
| + } |
| + |
| + bool documentKeepScreenAwake() |
| + { |
| + ASSERT(document()); |
| + return DocumentWakeLock::keepScreenAwake(*document()); |
| + } |
| + |
| + bool clientKeepScreenAwake() |
| + { |
| + return m_testWebFrameClient.testWebWakeLockClient().keepScreenAwake(); |
| + } |
| + |
| + void setKeepScreenAwake(bool keepScreenAwake) |
| + { |
| + ASSERT(document()); |
| + DocumentWakeLock::setKeepScreenAwake(*document(), keepScreenAwake); |
| + } |
| + |
| + void show() |
| + { |
| + ASSERT(m_webViewHelper.webView()); |
| + m_webViewHelper.webView()->setVisibilityState( |
| + blink::WebPageVisibilityStateVisible, false); |
| + ASSERT(!document()->hidden()); |
| + } |
| + |
| + void hide() |
| + { |
| + ASSERT(m_webViewHelper.webView()); |
| + m_webViewHelper.webView()->setVisibilityState( |
| + blink::WebPageVisibilityStateHidden, false); |
| + ASSERT(document()->hidden()); |
| + } |
| + |
| + // Order of these members is important as we need to make sure that |
| + // m_testWebFrameClient outlives m_webViewHelper (destruction order) |
| + TestWebFrameClient m_testWebFrameClient; |
| + blink::FrameTestHelpers::WebViewHelper m_webViewHelper; |
| +}; |
| + |
| +TEST_F(WakeLockTest, setAndReset) |
| +{ |
| + ASSERT_FALSE(documentKeepScreenAwake()); |
| + ASSERT_FALSE(clientKeepScreenAwake()); |
| + |
| + setKeepScreenAwake(true); |
| + EXPECT_TRUE(documentKeepScreenAwake()); |
| + EXPECT_TRUE(clientKeepScreenAwake()); |
| + |
| + setKeepScreenAwake(false); |
| + EXPECT_FALSE(documentKeepScreenAwake()); |
| + EXPECT_FALSE(clientKeepScreenAwake()); |
| +} |
| + |
| +TEST_F(WakeLockTest, hideWhenSet) |
| +{ |
| + ASSERT_FALSE(documentKeepScreenAwake()); |
| + ASSERT_FALSE(clientKeepScreenAwake()); |
| + |
| + setKeepScreenAwake(true); |
| + hide(); |
| + |
| + EXPECT_TRUE(documentKeepScreenAwake()); |
| + EXPECT_FALSE(clientKeepScreenAwake()); |
| +} |
| + |
| +TEST_F(WakeLockTest, setWhenHidden) |
| +{ |
| + ASSERT_FALSE(documentKeepScreenAwake()); |
| + ASSERT_FALSE(clientKeepScreenAwake()); |
| + |
| + hide(); |
| + setKeepScreenAwake(true); |
| + |
| + EXPECT_TRUE(documentKeepScreenAwake()); |
| + EXPECT_FALSE(clientKeepScreenAwake()); |
| +} |
| + |
| +TEST_F(WakeLockTest, showWhenSet) |
| +{ |
| + ASSERT_FALSE(documentKeepScreenAwake()); |
| + ASSERT_FALSE(clientKeepScreenAwake()); |
| + |
| + hide(); |
| + setKeepScreenAwake(true); |
| + show(); |
| + |
| + EXPECT_TRUE(documentKeepScreenAwake()); |
| + EXPECT_TRUE(clientKeepScreenAwake()); |
| +} |
| + |
| +TEST_F(WakeLockTest, navigate) |
| +{ |
| + ASSERT_FALSE(documentKeepScreenAwake()); |
| + ASSERT_FALSE(clientKeepScreenAwake()); |
| + |
| + setKeepScreenAwake(true); |
| + loadFrame(); |
| + |
| + EXPECT_FALSE(documentKeepScreenAwake()); |
| + EXPECT_FALSE(clientKeepScreenAwake()); |
| +} |
| + |
| +TEST_F(WakeLockTest, documentWithoutBrowsingContext) |
| +{ |
| + RefPtrWillBeRawPtr<blink::Document> document = |
| + blink::DOMImplementation::createDocument("", blink::DocumentInit(), false); |
| + ASSERT_FALSE(document->frame()); |
| + |
| + blink::WakeLock::setKeepScreenAwake(*document.get(), true); |
| + EXPECT_TRUE(blink::WakeLock::keepScreenAwake(*document.get())); |
| +} |
| + |
| +} // namespace |