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

Unified Diff: content/renderer/wake_lock/wake_lock_dispatcher_unittest.cc

Issue 486383003: WakeLock API: IPC at renderer side. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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
Index: content/renderer/wake_lock/wake_lock_dispatcher_unittest.cc
diff --git a/content/renderer/wake_lock/wake_lock_dispatcher_unittest.cc b/content/renderer/wake_lock/wake_lock_dispatcher_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..981dd97894db131eb02dec9bb4c5e067ad6499e9
--- /dev/null
+++ b/content/renderer/wake_lock/wake_lock_dispatcher_unittest.cc
@@ -0,0 +1,180 @@
+ // 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 "content/renderer/wake_lock/wake_lock_dispatcher.h"
+
+#include "content/common/wake_lock_messages.h"
+#include "content/public/test/test_utils.h"
+#include "ipc/ipc_test_sink.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/WebKit/public/platform/WebString.h"
+#include "third_party/WebKit/public/platform/WebWakeLockRequestCallback.h"
+#include "third_party/WebKit/public/web/WebSecurityOrigin.h"
+
+namespace content {
+
+class MockWakeLockRequestCallback
+ : public blink::WebWakeLockRequestCallback {
+ public:
+ struct WakeLockResultHolder {
+ WakeLockResultHolder() {
+ cleanup();
+ }
+ void cleanup() {
+ lock_succeeded_ = false;
+ lock_failed_ = false;
+ unlock_succeeded_ = false;
+ unlock_failed_ = false;
+ request_id_ = -1;
+ }
+
+ bool lock_succeeded_;
+ bool lock_failed_;
+ bool unlock_succeeded_;
+ bool unlock_failed_;
+ int request_id_;
+ };
+
+ explicit MockWakeLockRequestCallback(WakeLockResultHolder* results)
+ : results_(results) {}
+
+ void cleanupResults() {
+ results_->cleanup();
+ }
+
+ virtual void onCreatedWakeLockSuccessful(int request_id) {
+ results_->lock_succeeded_ = true;
+ results_->request_id_ = request_id;
+ }
+
+ virtual void onCreatedWakeLockFailed(int request_id) {
+ results_->lock_failed_ = true;
+ results_->request_id_ = request_id;
+ }
+
+ virtual void onUnlockedWakeLockSuccessful(int request_id) {
+ results_->unlock_succeeded_ = true;
+ results_->request_id_ = request_id;
+ }
+
+ virtual void onUnlockedWakeLockFailed(int request_id) {
+ results_->unlock_failed_ = true;
+ results_->request_id_ = request_id;
+ }
+
+ private:
+ WakeLockResultHolder* results_;
+};
+
+class WakeLockDispatcherWithSink : public WakeLockDispatcher {
+ public:
+ explicit WakeLockDispatcherWithSink(IPC::TestSink* sink)
+ : WakeLockDispatcher(NULL),
+ sink_(sink) {
+ }
+
+ virtual bool Send(IPC::Message* message) OVERRIDE {
+ return sink_->Send(message);
+ }
+
+ IPC::TestSink* sink_;
+};
+
+class WakeLockDispatcherTest : public testing::Test {
+ protected:
+ virtual void SetUp() OVERRIDE {
+ dispatcher_.reset(new WakeLockDispatcherWithSink(&sink_));
+ }
+
+ IPC::TestSink& sink() {
+ return sink_;
+ }
+
+ void RequestWakeLock(const blink::WakeLockRequestInfo& info,
+ const blink::WebSecurityOrigin& origin) {
+ dispatcher_->requestWakeLock(info, origin);
+ }
+
+ void RequestWakeUnlock(const blink::WakeLockRequestInfo& info) {
+ dispatcher_->requestWakeUnlock(info);
+ }
+
+ bool OnMessageReceived(const IPC::Message& message) {
+ return dispatcher_->OnMessageReceived(message);
+ }
+
+ void SetRequestCallback(blink::WebWakeLockRequestCallback* callback) {
+ dispatcher_->setRequestCallback(callback);
+ }
+
+ int routing_id() const {
+ // We return a fake routing_id() in the context of this test.
+ return 0;
+ }
+
+ IPC::TestSink sink_;
+ scoped_ptr<WakeLockDispatcher> dispatcher_;
+};
+
+int getNextRequestId() {
+ static int id = 0;
+ return ++id;
+}
+
+TEST_F(WakeLockDispatcherTest, RequestWakeLock_Simple) {
+ blink::WakeLockRequestInfo info;
+ info.type = blink::WebWakeLockScreen;
+ info.requestId = getNextRequestId();
+ info.contextId = 1;
+ const blink::WebSecurityOrigin origin =
+ blink::WebSecurityOrigin::createFromString("http://www.foo.html/");
+ MockWakeLockRequestCallback::WakeLockResultHolder callback_results;
+ MockWakeLockRequestCallback callback(&callback_results);
+ SetRequestCallback(&callback);
+
+ RequestWakeLock(info, origin);
+ EXPECT_TRUE(OnMessageReceived(WakeLockViewMsg_LockedSuccessful(
+ routing_id(), info.requestId)));
+
+ EXPECT_TRUE(callback_results.lock_succeeded_);
+ EXPECT_FALSE(callback_results.lock_failed_);
+ EXPECT_FALSE(callback_results.unlock_succeeded_);
+ EXPECT_FALSE(callback_results.unlock_failed_);
+ EXPECT_EQ(callback_results.request_id_, info.requestId);
+
+ callback.cleanupResults();
+ RequestWakeUnlock(info);
+ EXPECT_TRUE(OnMessageReceived(WakeLockViewMsg_UnlockedSuccessful(
+ routing_id(), info.requestId)));
+
+ EXPECT_FALSE(callback_results.lock_succeeded_);
+ EXPECT_FALSE(callback_results.lock_failed_);
+ EXPECT_TRUE(callback_results.unlock_succeeded_);
+ EXPECT_FALSE(callback_results.unlock_failed_);
+ EXPECT_EQ(callback_results.request_id_, info.requestId);
+
+ callback.cleanupResults();
+ RequestWakeLock(info, origin);
+ EXPECT_TRUE(OnMessageReceived(WakeLockViewMsg_LockedFailed(
+ routing_id(), info.requestId)));
+
+ EXPECT_FALSE(callback_results.lock_succeeded_);
+ EXPECT_TRUE(callback_results.lock_failed_);
+ EXPECT_FALSE(callback_results.unlock_succeeded_);
+ EXPECT_FALSE(callback_results.unlock_failed_);
+ EXPECT_EQ(callback_results.request_id_, info.requestId);
+
+ callback.cleanupResults();
+ RequestWakeUnlock(info);
+ EXPECT_TRUE(OnMessageReceived(WakeLockViewMsg_UnlockedFailed(
+ routing_id(), info.requestId)));
+
+ EXPECT_FALSE(callback_results.lock_succeeded_);
+ EXPECT_FALSE(callback_results.lock_failed_);
+ EXPECT_FALSE(callback_results.unlock_succeeded_);
+ EXPECT_TRUE(callback_results.unlock_failed_);
+ EXPECT_EQ(callback_results.request_id_, info.requestId);
+}
+
+} // namespace content
« content/content_tests.gypi ('K') | « content/renderer/wake_lock/wake_lock_dispatcher.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698