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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/renderer/wake_lock/wake_lock_dispatcher.h"
6
7 #include "content/common/wake_lock_messages.h"
8 #include "content/public/test/test_utils.h"
9 #include "ipc/ipc_test_sink.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/WebKit/public/platform/WebString.h"
12 #include "third_party/WebKit/public/platform/WebWakeLockRequestCallback.h"
13 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
14
15 namespace content {
16
17 class MockWakeLockRequestCallback
18 : public blink::WebWakeLockRequestCallback {
19 public:
20 struct WakeLockResultHolder {
21 WakeLockResultHolder() {
22 cleanup();
23 }
24 void cleanup() {
25 lock_succeeded_ = false;
26 lock_failed_ = false;
27 unlock_succeeded_ = false;
28 unlock_failed_ = false;
29 request_id_ = -1;
30 }
31
32 bool lock_succeeded_;
33 bool lock_failed_;
34 bool unlock_succeeded_;
35 bool unlock_failed_;
36 int request_id_;
37 };
38
39 explicit MockWakeLockRequestCallback(WakeLockResultHolder* results)
40 : results_(results) {}
41
42 void cleanupResults() {
43 results_->cleanup();
44 }
45
46 virtual void onCreatedWakeLockSuccessful(int request_id) {
47 results_->lock_succeeded_ = true;
48 results_->request_id_ = request_id;
49 }
50
51 virtual void onCreatedWakeLockFailed(int request_id) {
52 results_->lock_failed_ = true;
53 results_->request_id_ = request_id;
54 }
55
56 virtual void onUnlockedWakeLockSuccessful(int request_id) {
57 results_->unlock_succeeded_ = true;
58 results_->request_id_ = request_id;
59 }
60
61 virtual void onUnlockedWakeLockFailed(int request_id) {
62 results_->unlock_failed_ = true;
63 results_->request_id_ = request_id;
64 }
65
66 private:
67 WakeLockResultHolder* results_;
68 };
69
70 class WakeLockDispatcherWithSink : public WakeLockDispatcher {
71 public:
72 explicit WakeLockDispatcherWithSink(IPC::TestSink* sink)
73 : WakeLockDispatcher(NULL),
74 sink_(sink) {
75 }
76
77 virtual bool Send(IPC::Message* message) OVERRIDE {
78 return sink_->Send(message);
79 }
80
81 IPC::TestSink* sink_;
82 };
83
84 class WakeLockDispatcherTest : public testing::Test {
85 protected:
86 virtual void SetUp() OVERRIDE {
87 dispatcher_.reset(new WakeLockDispatcherWithSink(&sink_));
88 }
89
90 IPC::TestSink& sink() {
91 return sink_;
92 }
93
94 void RequestWakeLock(const blink::WakeLockRequestInfo& info,
95 const blink::WebSecurityOrigin& origin) {
96 dispatcher_->requestWakeLock(info, origin);
97 }
98
99 void RequestWakeUnlock(const blink::WakeLockRequestInfo& info) {
100 dispatcher_->requestWakeUnlock(info);
101 }
102
103 bool OnMessageReceived(const IPC::Message& message) {
104 return dispatcher_->OnMessageReceived(message);
105 }
106
107 void SetRequestCallback(blink::WebWakeLockRequestCallback* callback) {
108 dispatcher_->setRequestCallback(callback);
109 }
110
111 int routing_id() const {
112 // We return a fake routing_id() in the context of this test.
113 return 0;
114 }
115
116 IPC::TestSink sink_;
117 scoped_ptr<WakeLockDispatcher> dispatcher_;
118 };
119
120 int getNextRequestId() {
121 static int id = 0;
122 return ++id;
123 }
124
125 TEST_F(WakeLockDispatcherTest, RequestWakeLock_Simple) {
126 blink::WakeLockRequestInfo info;
127 info.type = blink::WebWakeLockScreen;
128 info.requestId = getNextRequestId();
129 info.contextId = 1;
130 const blink::WebSecurityOrigin origin =
131 blink::WebSecurityOrigin::createFromString("http://www.foo.html/");
132 MockWakeLockRequestCallback::WakeLockResultHolder callback_results;
133 MockWakeLockRequestCallback callback(&callback_results);
134 SetRequestCallback(&callback);
135
136 RequestWakeLock(info, origin);
137 EXPECT_TRUE(OnMessageReceived(WakeLockViewMsg_LockedSuccessful(
138 routing_id(), info.requestId)));
139
140 EXPECT_TRUE(callback_results.lock_succeeded_);
141 EXPECT_FALSE(callback_results.lock_failed_);
142 EXPECT_FALSE(callback_results.unlock_succeeded_);
143 EXPECT_FALSE(callback_results.unlock_failed_);
144 EXPECT_EQ(callback_results.request_id_, info.requestId);
145
146 callback.cleanupResults();
147 RequestWakeUnlock(info);
148 EXPECT_TRUE(OnMessageReceived(WakeLockViewMsg_UnlockedSuccessful(
149 routing_id(), info.requestId)));
150
151 EXPECT_FALSE(callback_results.lock_succeeded_);
152 EXPECT_FALSE(callback_results.lock_failed_);
153 EXPECT_TRUE(callback_results.unlock_succeeded_);
154 EXPECT_FALSE(callback_results.unlock_failed_);
155 EXPECT_EQ(callback_results.request_id_, info.requestId);
156
157 callback.cleanupResults();
158 RequestWakeLock(info, origin);
159 EXPECT_TRUE(OnMessageReceived(WakeLockViewMsg_LockedFailed(
160 routing_id(), info.requestId)));
161
162 EXPECT_FALSE(callback_results.lock_succeeded_);
163 EXPECT_TRUE(callback_results.lock_failed_);
164 EXPECT_FALSE(callback_results.unlock_succeeded_);
165 EXPECT_FALSE(callback_results.unlock_failed_);
166 EXPECT_EQ(callback_results.request_id_, info.requestId);
167
168 callback.cleanupResults();
169 RequestWakeUnlock(info);
170 EXPECT_TRUE(OnMessageReceived(WakeLockViewMsg_UnlockedFailed(
171 routing_id(), info.requestId)));
172
173 EXPECT_FALSE(callback_results.lock_succeeded_);
174 EXPECT_FALSE(callback_results.lock_failed_);
175 EXPECT_FALSE(callback_results.unlock_succeeded_);
176 EXPECT_TRUE(callback_results.unlock_failed_);
177 EXPECT_EQ(callback_results.request_id_, info.requestId);
178 }
179
180 } // namespace content
OLDNEW
« 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