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

Unified Diff: Source/web/tests/WakeLockTest.cpp

Issue 399313003: Initial implementation of API WakeLock. Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Implementation of WakeLock API on JavaScript side. Created 6 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/platform/RuntimeEnabledFeatures.in ('k') | Source/web/web.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..011374771b70d60e0586ef35c608488073bb7036
--- /dev/null
+++ b/Source/web/tests/WakeLockTest.cpp
@@ -0,0 +1,266 @@
+// Copyright 2014 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.
+
+#include "config.h"
+
+#include "modules/wake_lock/WakeLock.h"
+
+#include "bindings/core/v8/ScriptState.h"
+#include "bindings/core/v8/V8Binding.h"
+#include "core/dom/Document.h"
+#include "core/html/HTMLElement.h"
+#include "core/page/Page.h"
+#include "modules/wake_lock/WakeLockController.h"
+#include "public/platform/Platform.h"
+#include "public/platform/WebUnitTestSupport.h"
+#include "public/platform/WebWakeLockClient.h"
+#include "public/platform/WebWakeLockRequestCallback.h"
+#include "public/web/WebDocument.h"
+#include "public/web/WebView.h"
+#include "web/WebLocalFrameImpl.h"
+#include "web/WebViewImpl.h"
+#include "web/tests/FrameTestHelpers.h"
+#include "web/tests/URLTestHelpers.h"
+#include "wtf/Vector.h"
+
+#include <gtest/gtest.h>
+
+namespace {
+struct WakeLockRequestInfo {
+ blink::WebWakeLockType m_type;
+ int m_requestId;
+ WakeLockRequestInfo(blink::WebWakeLockType type, int id)
+ : m_type(type)
+ , m_requestId(id)
+ {
+ }
+};
+typedef Vector<WakeLockRequestInfo> Requests;
+
+class MockWebWakeLockClient : public blink::WebWakeLockClient {
+public:
+ MockWebWakeLockClient()
+ : m_callback(0)
+ {
+ }
+
+ virtual ~MockWebWakeLockClient() { };
+
+ virtual void requestWakeLock(blink::WebWakeLockType type, int requestId, blink::WebSecurityOrigin)
+ {
+ m_lockRequests.append(WakeLockRequestInfo(type, requestId));
+ };
+
+ virtual void requestWakeUnlock(blink::WebWakeLockType type, int requestId)
+ {
+ m_unlockRequests.append(WakeLockRequestInfo(type, requestId));
+ };
+
+ virtual void wakeLockDestroyed() { m_callback = 0; };
+ virtual void setRequestCallback(blink::WebWakeLockRequestCallback* callback)
+ {
+ m_callback = callback;
+ };
+
+ void resolveLockRequest(blink::WebWakeLockType type, bool success)
+ {
+ for (size_t pos = 0; pos < m_lockRequests.size();) {
+ blink::WebWakeLockType requestType = m_lockRequests.at(pos).m_type;
+ int resolverId = m_lockRequests.at(pos).m_requestId;
+ if (m_callback && (requestType == type)) {
+ if (success)
+ m_callback->onCreatedWakeLockSuccessful(resolverId);
+ else
+ m_callback->onCreatedWakeLockFailed(resolverId);
+ m_lockRequests.remove(pos);
+ } else {
+ ++pos;
+ }
+ }
+ }
+
+ void resolveUnlockRequest(blink::WebWakeLockType type, bool success)
+ {
+ for (size_t pos = 0; pos < m_unlockRequests.size();) {
+ blink::WebWakeLockType requestType = m_unlockRequests.at(pos).m_type;
+ int resolverId = m_unlockRequests.at(pos).m_requestId;
+ if (m_callback && (requestType == type)) {
+ if (success)
+ m_callback->onUnlockedWakeLockSuccessful(resolverId);
+ else
+ m_callback->onUnlockedWakeLockFailed(resolverId);
+ m_unlockRequests.remove(pos);
+ } else {
+ ++pos;
+ }
+ }
+ }
+
+private:
+ blink::WebWakeLockRequestCallback *m_callback;
+
+ Requests m_lockRequests;
+ Requests m_unlockRequests;
+};
+
+class WakeLockTest : public testing::Test {
+public:
+ WakeLockTest()
+ : m_baseURL("http://www.test.com/")
+ {
+ }
+
+protected:
+ virtual void SetUp() OVERRIDE
+ {
+ m_webViewHelper.initialize(true, 0, &m_mockWebViewClient, 0);
+ webViewImpl()->resize(blink::WebSize(320, 240));
+ }
+
+ virtual void TearDown() OVERRIDE
+ {
+ blink::Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
+ }
+
+ void registerMockedHttpURLLoad(const std::string& fileName)
+ {
+ blink::URLTestHelpers::registerMockedURLFromBaseURL(blink::WebString::fromUTF8(m_baseURL.c_str()), blink::WebString::fromUTF8(fileName.c_str()));
+ }
+
+ blink::Document* document() const { return frame()->document(); }
+ blink::Page* page() const { return frame()->page(); }
+
+ blink::WebViewImpl* webViewImpl() const { return m_webViewHelper.webViewImpl(); }
+ blink::LocalFrame* frame() const { return m_webViewHelper.webViewImpl()->mainFrameImpl()->frame(); }
+
+ void navigateTo(const std::string& url)
+ {
+ blink::FrameTestHelpers::loadFrame(webViewImpl()->mainFrame(), url);
+ }
+
+ void forceFullCompositingUpdate() { webViewImpl()->layout(); }
+
+ std::string m_baseURL;
+
+private:
+ blink::FrameTestHelpers::TestWebViewClient m_mockWebViewClient;
+ blink::FrameTestHelpers::WebViewHelper m_webViewHelper;
+};
+
+TEST_F(WakeLockTest, requestReleasePromise)
+{
+ registerMockedHttpURLLoad("foo.html");
+ navigateTo(m_baseURL + "foo.html");
+ forceFullCompositingUpdate();
+
+ ASSERT_TRUE(document());
+ ASSERT_TRUE(page());
+
+ MockWebWakeLockClient wakeLockClient;
+ blink::WakeLockController::provideWakeLockTo(*page(), &wakeLockClient);
+ blink::WakeLockController* wakeLockController = blink::WakeLockController::from(page());
+ ASSERT_TRUE(wakeLockController);
+
+ wakeLockClient.setRequestCallback(blink::WakeLockController::from(page()));
+
+ blink::ScriptState* scriptState = blink::ScriptState::forMainWorld(frame());
+ blink::ScriptState::Scope scriptScope(scriptState);
+ RefPtr<blink::WakeLock> wakeLock = blink::WakeLock::create(document()->domWindow()->executionContext());
+
+ {
+ blink::ScriptPromise promise = wakeLock->request(scriptState, "screen");
+ wakeLockClient.resolveLockRequest(blink::WebWakeLockScreen, true);
+
+ ASSERT_FALSE(promise.isEmpty());
+ ASSERT_TRUE(promise.v8Value()->IsPromise());
+
+ ASSERT_TRUE(wakeLockController->isHeld(blink::WebWakeLockScreen));
+
+ promise = wakeLock->release(scriptState, "screen");
+ wakeLockClient.resolveUnlockRequest(blink::WebWakeLockScreen, true);
+ ASSERT_FALSE(promise.isEmpty());
+ ASSERT_TRUE(promise.v8Value()->IsPromise());
+
+ ASSERT_FALSE(wakeLockController->isHeld(blink::WebWakeLockScreen));
+ }
+
+ {
+ blink::ScriptPromise promise = wakeLock->release(scriptState, "screen");
+ ASSERT_FALSE(promise.isEmpty());
+ ASSERT_TRUE(promise.v8Value()->IsPromise());
+
+ wakeLockClient.resolveUnlockRequest(blink::WebWakeLockScreen, true);
+
+ ASSERT_FALSE(wakeLockController->isHeld(blink::WebWakeLockScreen));
+ }
+
+ {
+ blink::ScriptPromise promise = wakeLock->request(scriptState, "screen");
+ ASSERT_FALSE(promise.isEmpty());
+ ASSERT_TRUE(promise.v8Value()->IsPromise());
+
+ wakeLockClient.resolveLockRequest(blink::WebWakeLockScreen, false);
+
+ ASSERT_FALSE(wakeLockController->isHeld(blink::WebWakeLockScreen));
+ }
+
+ {
+ blink::ScriptPromise promise1 = wakeLock->request(scriptState, "screen");
+ blink::ScriptPromise promise2 = wakeLock->request(scriptState, "screen");
+ wakeLockClient.resolveLockRequest(blink::WebWakeLockScreen, true);
+ ASSERT_FALSE(promise1.isEmpty());
+ ASSERT_FALSE(promise2.isEmpty());
+ ASSERT_TRUE(promise1.v8Value()->IsPromise());
+ ASSERT_TRUE(promise2.v8Value()->IsPromise());
+
+ ASSERT_TRUE(wakeLockController->isHeld(blink::WebWakeLockScreen));
+
+ promise1 = wakeLock->release(scriptState, "screen");
+ promise2 = wakeLock->release(scriptState, "screen");
+ wakeLockClient.resolveUnlockRequest(blink::WebWakeLockScreen, true);
+ ASSERT_FALSE(promise1.isEmpty());
+ ASSERT_FALSE(promise2.isEmpty());
+ ASSERT_TRUE(promise1.v8Value()->IsPromise());
+ ASSERT_TRUE(promise2.v8Value()->IsPromise());
+
+ ASSERT_FALSE(wakeLockController->isHeld(blink::WebWakeLockScreen));
+ }
+
+ {
+ blink::ScriptPromise promise1 = wakeLock->request(scriptState, "screen");
+ blink::ScriptPromise promise2 = wakeLock->request(scriptState, "screen");
+ wakeLockClient.resolveLockRequest(blink::WebWakeLockScreen, true);
+ ASSERT_FALSE(promise1.isEmpty());
+ ASSERT_FALSE(promise2.isEmpty());
+ ASSERT_TRUE(promise1.v8Value()->IsPromise());
+ ASSERT_TRUE(promise2.v8Value()->IsPromise());
+
+ ASSERT_TRUE(wakeLockController->isHeld(blink::WebWakeLockScreen));
+
+ promise1 = wakeLock->release(scriptState, "screen");
+ ASSERT_FALSE(promise1.isEmpty());
+ ASSERT_TRUE(promise1.v8Value()->IsPromise());
+
+ wakeLockClient.resolveUnlockRequest(blink::WebWakeLockScreen, true);
+ ASSERT_TRUE(wakeLockController->isHeld(blink::WebWakeLockScreen));
+
+ promise2 = wakeLock->release(scriptState, "screen");
+ ASSERT_FALSE(promise2.isEmpty());
+ ASSERT_TRUE(promise2.v8Value()->IsPromise());
+
+ wakeLockClient.resolveUnlockRequest(blink::WebWakeLockScreen, false);
+ ASSERT_TRUE(wakeLockController->isHeld(blink::WebWakeLockScreen));
+
+ promise2 = wakeLock->release(scriptState, "screen");
+ ASSERT_FALSE(promise2.isEmpty());
+ ASSERT_TRUE(promise2.v8Value()->IsPromise());
+
+ wakeLockClient.resolveUnlockRequest(blink::WebWakeLockScreen, true);
+ ASSERT_FALSE(wakeLockController->isHeld(blink::WebWakeLockScreen));
+ }
+
+ wakeLockController->resetClient();
+}
+
+} // namespace
« no previous file with comments | « Source/platform/RuntimeEnabledFeatures.in ('k') | Source/web/web.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698