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

Side by Side Diff: content/renderer/presentation/presentation_dispatcher_unittest.cc

Issue 2471573005: [Presentation API] (5th) (1-UA) integrate controller and receiver side for 1-UA messaging (Closed)
Patch Set: merge and refactor Created 3 years, 11 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
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 <memory> 5 #include <memory>
6 #include <utility> 6 #include <utility>
7 7
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "content/public/test/test_browser_thread_bundle.h" 9 #include "content/public/test/test_browser_thread_bundle.h"
10 #include "content/renderer/presentation/presentation_connection_proxy.h"
10 #include "content/renderer/presentation/presentation_dispatcher.h" 11 #include "content/renderer/presentation/presentation_dispatcher.h"
11 #include "testing/gmock/include/gmock/gmock.h" 12 #include "testing/gmock/include/gmock/gmock.h"
12 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nAvailabilityObserver.h" 13 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nAvailabilityObserver.h"
13 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nConnection.h" 14 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nConnection.h"
14 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nConnectionCallbacks.h" 15 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nConnectionCallbacks.h"
15 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nController.h" 16 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nController.h"
16 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nError.h" 17 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nError.h"
17 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nSessionInfo.h" 18 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nSessionInfo.h"
18 #include "third_party/WebKit/public/web/WebArrayBuffer.h" 19 #include "third_party/WebKit/public/web/WebArrayBuffer.h"
19 20
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 ListenForConnectionMessagesInternal(session_info.get()); 106 ListenForConnectionMessagesInternal(session_info.get());
106 } 107 }
107 MOCK_METHOD1(ListenForConnectionMessagesInternal, 108 MOCK_METHOD1(ListenForConnectionMessagesInternal,
108 void(PresentationSessionInfo* session_info)); 109 void(PresentationSessionInfo* session_info));
109 110
110 void SetPresentationConnection( 111 void SetPresentationConnection(
111 blink::mojom::PresentationSessionInfoPtr session, 112 blink::mojom::PresentationSessionInfoPtr session,
112 blink::mojom::PresentationConnectionPtr controller_conn_ptr, 113 blink::mojom::PresentationConnectionPtr controller_conn_ptr,
113 blink::mojom::PresentationConnectionRequest receiver_conn_request) 114 blink::mojom::PresentationConnectionRequest receiver_conn_request)
114 override { 115 override {
115 SetPresentationConnection(session.get(), controller_conn_ptr.get()); 116 SetPresentationConnectionInternal(session.get(), controller_conn_ptr.get());
116 } 117 }
117 MOCK_METHOD2(SetPresentationConnection, 118 MOCK_METHOD2(SetPresentationConnectionInternal,
118 void(PresentationSessionInfo* session_info, 119 void(PresentationSessionInfo* session_info,
119 PresentationConnection* connection)); 120 PresentationConnection* connection));
120 }; 121 };
121 122
123 class TestPresentationConnection : public blink::WebPresentationConnection {
124 public:
125 void setProxy(
126 std::unique_ptr<blink::WebPresentationConnectionProxy> proxy) override {
127 proxy_ = std::move(proxy);
128 }
129 MOCK_METHOD1(didReceiveTextMessage, void(const WebString& message));
130 MOCK_METHOD2(didReceiveBinaryMessage,
131 void(const uint8_t* data, size_t length));
132 MOCK_METHOD1(didChangeState, void(blink::WebPresentationConnectionState));
133
134 std::unique_ptr<blink::WebPresentationConnectionProxy> proxy_;
135 };
136
137 class TestPresentationConnectionProxy : public PresentationConnectionProxy {
138 public:
139 TestPresentationConnectionProxy(blink::WebPresentationConnection* connection)
140 : PresentationConnectionProxy(connection) {}
141
142 void SendConnectionMessage(blink::mojom::ConnectionMessagePtr session_message,
143 const OnMessageCallback& callback) const override {
144 SendConnectionMessageInternal(session_message.get(), callback);
145 }
146 MOCK_CONST_METHOD2(SendConnectionMessageInternal,
147 void(blink::mojom::ConnectionMessage*,
148 const OnMessageCallback&));
149 };
150
122 class TestWebPresentationConnectionCallback 151 class TestWebPresentationConnectionCallback
123 : public WebPresentationConnectionCallbacks { 152 : public WebPresentationConnectionCallbacks {
124 public: 153 public:
125 TestWebPresentationConnectionCallback(WebURL url, WebString id) 154 TestWebPresentationConnectionCallback(WebURL url, WebString id)
126 : url_(url), id_(id), callback_called_(false) {} 155 : url_(url), id_(id), callback_called_(false) {}
127 ~TestWebPresentationConnectionCallback() override { 156 ~TestWebPresentationConnectionCallback() override {
128 EXPECT_TRUE(callback_called_); 157 EXPECT_TRUE(callback_called_);
129 } 158 }
130 159
131 void onSuccess(const WebPresentationSessionInfo& info) override { 160 void onSuccess(const WebPresentationSessionInfo& info) override {
132 callback_called_ = true; 161 callback_called_ = true;
133 EXPECT_EQ(info.url, url_); 162 EXPECT_EQ(info.url, url_);
134 EXPECT_EQ(info.id, id_); 163 EXPECT_EQ(info.id, id_);
135 } 164 }
136 165
137 blink::WebPresentationConnection* getConnection() override { return nullptr; } 166 blink::WebPresentationConnection* getConnection() override {
167 return &connection_;
168 }
138 169
139 private: 170 private:
140 const WebURL url_; 171 const WebURL url_;
141 const WebString id_; 172 const WebString id_;
142 bool callback_called_; 173 bool callback_called_;
174 TestPresentationConnection connection_;
143 }; 175 };
144 176
145 class TestWebPresentationConnectionErrorCallback 177 class TestWebPresentationConnectionErrorCallback
146 : public WebPresentationConnectionCallbacks { 178 : public WebPresentationConnectionCallbacks {
147 public: 179 public:
148 TestWebPresentationConnectionErrorCallback( 180 TestWebPresentationConnectionErrorCallback(
149 WebPresentationError::ErrorType error_type, 181 WebPresentationError::ErrorType error_type,
150 WebString message) 182 WebString message)
151 : error_type_(error_type), message_(message), callback_called_(false) {} 183 : error_type_(error_type), message_(message), callback_called_(false) {}
152 ~TestWebPresentationConnectionErrorCallback() override { 184 ~TestWebPresentationConnectionErrorCallback() override {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 const WebString presentation_id_; 254 const WebString presentation_id_;
223 const WebArrayBuffer array_buffer_; 255 const WebArrayBuffer array_buffer_;
224 MockPresentationAvailabilityObserver observer_; 256 MockPresentationAvailabilityObserver observer_;
225 MockPresentationService presentation_service_; 257 MockPresentationService presentation_service_;
226 TestPresentationDispatcher dispatcher_; 258 TestPresentationDispatcher dispatcher_;
227 259
228 private: 260 private:
229 content::TestBrowserThreadBundle thread_bundle_; 261 content::TestBrowserThreadBundle thread_bundle_;
230 }; 262 };
231 263
232 TEST_F(PresentationDispatcherTest, TestStartSession) { 264 TEST_F(PresentationDispatcherTest, TestStartSession) {
mark a. foltz 2017/01/23 22:29:34 Are there expectations covering that the proxies a
zhaobin 2017/01/24 19:28:46 Done.
233 base::RunLoop run_loop; 265 base::RunLoop run_loop;
234 266
267 EXPECT_CALL(presentation_service_, ListenForConnectionMessagesInternal(_));
268 EXPECT_CALL(presentation_service_, SetPresentationConnectionInternal(_, _));
235 EXPECT_CALL(presentation_service_, StartSession(gurls_, _)) 269 EXPECT_CALL(presentation_service_, StartSession(gurls_, _))
236 .WillOnce(Invoke([this]( 270 .WillOnce(Invoke([this](
237 const std::vector<GURL>& presentation_urls, 271 const std::vector<GURL>& presentation_urls,
238 const PresentationService::StartSessionCallback& callback) { 272 const PresentationService::StartSessionCallback& callback) {
239 PresentationSessionInfoPtr session_info(PresentationSessionInfo::New()); 273 PresentationSessionInfoPtr session_info(PresentationSessionInfo::New());
240 session_info->url = gurl1_; 274 session_info->url = gurl1_;
241 session_info->id = presentation_id_.utf8(); 275 session_info->id = presentation_id_.utf8();
242 callback.Run(std::move(session_info), PresentationErrorPtr()); 276 callback.Run(std::move(session_info), PresentationErrorPtr());
243 })); 277 }));
244 dispatcher_.startSession( 278 dispatcher_.startSession(
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 dispatcher_.joinSession( 320 dispatcher_.joinSession(
287 urls_, presentation_id_, 321 urls_, presentation_id_,
288 base::MakeUnique<TestWebPresentationConnectionErrorCallback>( 322 base::MakeUnique<TestWebPresentationConnectionErrorCallback>(
289 WebPresentationError::ErrorTypeNoAvailableScreens, error_message)); 323 WebPresentationError::ErrorTypeNoAvailableScreens, error_message));
290 run_loop.RunUntilIdle(); 324 run_loop.RunUntilIdle();
291 } 325 }
292 326
293 TEST_F(PresentationDispatcherTest, TestJoinSession) { 327 TEST_F(PresentationDispatcherTest, TestJoinSession) {
294 base::RunLoop run_loop; 328 base::RunLoop run_loop;
295 329
330 EXPECT_CALL(presentation_service_, ListenForConnectionMessagesInternal(_));
331 EXPECT_CALL(presentation_service_, SetPresentationConnectionInternal(_, _));
296 EXPECT_CALL(presentation_service_, JoinSession(gurls_, _, _)) 332 EXPECT_CALL(presentation_service_, JoinSession(gurls_, _, _))
297 .WillOnce(Invoke([this]( 333 .WillOnce(Invoke([this](
298 const std::vector<GURL>& presentation_urls, 334 const std::vector<GURL>& presentation_urls,
299 const base::Optional<std::string>& presentation_id, 335 const base::Optional<std::string>& presentation_id,
300 const PresentationService::JoinSessionCallback& callback) { 336 const PresentationService::JoinSessionCallback& callback) {
301 EXPECT_TRUE(presentation_id.has_value()); 337 EXPECT_TRUE(presentation_id.has_value());
302 EXPECT_EQ(presentation_id_.utf8(), presentation_id.value()); 338 EXPECT_EQ(presentation_id_.utf8(), presentation_id.value());
303 PresentationSessionInfoPtr session_info(PresentationSessionInfo::New()); 339 PresentationSessionInfoPtr session_info(PresentationSessionInfo::New());
304 session_info->url = gurl1_; 340 session_info->url = gurl1_;
305 session_info->id = presentation_id_.utf8(); 341 session_info->id = presentation_id_.utf8();
306 callback.Run(std::move(session_info), PresentationErrorPtr()); 342 callback.Run(std::move(session_info), PresentationErrorPtr());
307 })); 343 }));
308 dispatcher_.joinSession( 344 dispatcher_.joinSession(
309 urls_, presentation_id_, 345 urls_, presentation_id_,
310 base::MakeUnique<TestWebPresentationConnectionCallback>( 346 base::MakeUnique<TestWebPresentationConnectionCallback>(
311 url1_, presentation_id_)); 347 url1_, presentation_id_));
312 run_loop.RunUntilIdle(); 348 run_loop.RunUntilIdle();
313 } 349 }
314 350
315 TEST_F(PresentationDispatcherTest, TestSendString) { 351 TEST_F(PresentationDispatcherTest, TestSendString) {
316 WebString message = WebString::fromUTF8("test message"); 352 WebString message = WebString::fromUTF8("test message");
353 TestPresentationConnection connection;
354 TestPresentationConnectionProxy connection_proxy(&connection);
355
317 base::RunLoop run_loop; 356 base::RunLoop run_loop;
318 EXPECT_CALL(presentation_service_, SendConnectionMessageInternal(_, _, _)) 357 EXPECT_CALL(connection_proxy, SendConnectionMessageInternal(_, _))
319 .WillOnce(Invoke([this, &message]( 358 .WillOnce(Invoke([this, &message](ConnectionMessage* session_message,
320 PresentationSessionInfo* session_info, 359 const OnMessageCallback& callback) {
321 ConnectionMessage* message_request, 360 EXPECT_EQ(blink::mojom::PresentationMessageType::TEXT,
322 const PresentationService::SendConnectionMessageCallback& callback) { 361 session_message->type);
323 EXPECT_EQ(gurl1_, session_info->url); 362 EXPECT_EQ(message.utf8(), session_message->message.value());
324 EXPECT_EQ(presentation_id_.utf8(), session_info->id);
325 EXPECT_TRUE(message_request->message.has_value());
326 EXPECT_EQ(message.utf8(), message_request->message.value());
327 callback.Run(true);
328 })); 363 }));
329 dispatcher_.sendString(url1_, presentation_id_, message, nullptr); 364 dispatcher_.sendString(url1_, presentation_id_, message, &connection_proxy);
330 run_loop.RunUntilIdle(); 365 run_loop.RunUntilIdle();
331 } 366 }
332 367
333 TEST_F(PresentationDispatcherTest, TestSendArrayBuffer) { 368 TEST_F(PresentationDispatcherTest, TestSendArrayBuffer) {
369 TestPresentationConnection connection;
370 TestPresentationConnectionProxy connection_proxy(&connection);
371
334 base::RunLoop run_loop; 372 base::RunLoop run_loop;
335 EXPECT_CALL(presentation_service_, SendConnectionMessageInternal(_, _, _)) 373 EXPECT_CALL(connection_proxy, SendConnectionMessageInternal(_, _))
336 .WillOnce(Invoke([this]( 374 .WillOnce(Invoke([this](ConnectionMessage* message_request,
337 PresentationSessionInfo* session_info, 375 const OnMessageCallback& callback) {
338 ConnectionMessage* message_request,
339 const PresentationService::SendConnectionMessageCallback& callback) {
340 EXPECT_EQ(gurl1_, session_info->url);
341 EXPECT_EQ(presentation_id_.utf8(), session_info->id);
342 std::vector<uint8_t> data( 376 std::vector<uint8_t> data(
343 array_buffer_data(), 377 array_buffer_data(),
344 array_buffer_data() + array_buffer_.byteLength()); 378 array_buffer_data() + array_buffer_.byteLength());
345 EXPECT_TRUE(message_request->data.has_value()); 379 EXPECT_TRUE(message_request->data.has_value());
346 EXPECT_EQ(data, message_request->data.value()); 380 EXPECT_EQ(data, message_request->data.value());
347 callback.Run(true);
348 })); 381 }));
349 dispatcher_.sendArrayBuffer(url1_, presentation_id_, array_buffer_data(), 382 dispatcher_.sendArrayBuffer(url1_, presentation_id_, array_buffer_data(),
350 array_buffer_.byteLength(), nullptr); 383 array_buffer_.byteLength(), &connection_proxy);
351 run_loop.RunUntilIdle(); 384 run_loop.RunUntilIdle();
352 } 385 }
353 386
354 TEST_F(PresentationDispatcherTest, TestSendBlobData) { 387 TEST_F(PresentationDispatcherTest, TestSendBlobData) {
mark a. foltz 2017/01/23 22:29:34 Does there need to be a test case added for OnRece
zhaobin 2017/01/24 19:28:46 Done.
388 TestPresentationConnection connection;
389 TestPresentationConnectionProxy connection_proxy(&connection);
390
355 base::RunLoop run_loop; 391 base::RunLoop run_loop;
356 EXPECT_CALL(presentation_service_, SendConnectionMessageInternal(_, _, _)) 392 EXPECT_CALL(connection_proxy, SendConnectionMessageInternal(_, _))
357 .WillOnce(Invoke([this]( 393 .WillOnce(Invoke([this](ConnectionMessage* message_request,
358 PresentationSessionInfo* session_info, 394 const OnMessageCallback& callback) {
359 ConnectionMessage* message_request,
360 const PresentationService::SendConnectionMessageCallback& callback) {
361 EXPECT_EQ(gurl1_, session_info->url);
362 EXPECT_EQ(presentation_id_.utf8(), session_info->id);
363 std::vector<uint8_t> data( 395 std::vector<uint8_t> data(
364 array_buffer_data(), 396 array_buffer_data(),
365 array_buffer_data() + array_buffer_.byteLength()); 397 array_buffer_data() + array_buffer_.byteLength());
366 EXPECT_TRUE(message_request->data.has_value()); 398 EXPECT_TRUE(message_request->data.has_value());
367 EXPECT_EQ(data, message_request->data.value()); 399 EXPECT_EQ(data, message_request->data.value());
368 callback.Run(true); 400 callback.Run(true);
369 })); 401 }));
370 dispatcher_.sendBlobData(url1_, presentation_id_, array_buffer_data(), 402 dispatcher_.sendBlobData(url1_, presentation_id_, array_buffer_data(),
371 array_buffer_.byteLength(), nullptr); 403 array_buffer_.byteLength(), &connection_proxy);
372 run_loop.RunUntilIdle(); 404 run_loop.RunUntilIdle();
373 } 405 }
374 406
375 TEST_F(PresentationDispatcherTest, TestCloseSession) { 407 TEST_F(PresentationDispatcherTest, TestCloseSession) {
376 base::RunLoop run_loop; 408 base::RunLoop run_loop;
377 EXPECT_CALL(presentation_service_, 409 EXPECT_CALL(presentation_service_,
378 CloseConnection(gurl1_, presentation_id_.utf8())); 410 CloseConnection(gurl1_, presentation_id_.utf8()));
379 dispatcher_.closeSession(url1_, presentation_id_); 411 dispatcher_.closeSession(url1_, presentation_id_);
380 run_loop.RunUntilIdle(); 412 run_loop.RunUntilIdle();
381 } 413 }
382 414
383 TEST_F(PresentationDispatcherTest, TestTerminateSession) { 415 TEST_F(PresentationDispatcherTest, TestTerminateSession) {
384 base::RunLoop run_loop; 416 base::RunLoop run_loop;
385 EXPECT_CALL(presentation_service_, 417 EXPECT_CALL(presentation_service_,
386 Terminate(gurl1_, presentation_id_.utf8())); 418 Terminate(gurl1_, presentation_id_.utf8()));
387 dispatcher_.terminateSession(url1_, presentation_id_); 419 dispatcher_.terminateSession(url1_, presentation_id_);
388 run_loop.RunUntilIdle(); 420 run_loop.RunUntilIdle();
389 } 421 }
390 422
391 TEST_F(PresentationDispatcherTest, TestListenForScreenAvailability) { 423 TEST_F(PresentationDispatcherTest, TestListenForScreenAvailability) {
392 base::RunLoop run_loop1; 424 base::RunLoop run_loop1;
393 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl1_)); 425 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl1_));
394 dispatcher_.getAvailability( 426 dispatcher_.getAvailability(
395 url1_, base::MakeUnique<WebPresentationAvailabilityCallbacks>()); 427 url1_, base::MakeUnique<WebPresentationAvailabilityCallbacks>());
396 dispatcher_.OnScreenAvailabilityUpdated(url1_, true); 428 dispatcher_.OnScreenAvailabilityUpdated(url1_, true);
429 EXPECT_CALL(presentation_service_,
430 StopListeningForScreenAvailability(gurl1_));
397 run_loop1.RunUntilIdle(); 431 run_loop1.RunUntilIdle();
398 432
399 base::RunLoop run_loop2; 433 base::RunLoop run_loop2;
400 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl1_)); 434 EXPECT_CALL(presentation_service_, ListenForScreenAvailability(gurl1_));
401 dispatcher_.startListening(&observer_); 435 dispatcher_.startListening(&observer_);
402 run_loop2.RunUntilIdle(); 436 run_loop2.RunUntilIdle();
403 437
404 base::RunLoop run_loop3; 438 base::RunLoop run_loop3;
405 EXPECT_CALL(observer_, availabilityChanged(false)); 439 EXPECT_CALL(observer_, availabilityChanged(false));
406 dispatcher_.OnScreenAvailabilityUpdated(url1_, false); 440 dispatcher_.OnScreenAvailabilityUpdated(url1_, false);
(...skipping 12 matching lines...) Expand all
419 } 453 }
420 454
421 TEST_F(PresentationDispatcherTest, TestSetDefaultPresentationUrls) { 455 TEST_F(PresentationDispatcherTest, TestSetDefaultPresentationUrls) {
422 base::RunLoop run_loop; 456 base::RunLoop run_loop;
423 EXPECT_CALL(presentation_service_, SetDefaultPresentationUrls(gurls_)); 457 EXPECT_CALL(presentation_service_, SetDefaultPresentationUrls(gurls_));
424 dispatcher_.setDefaultPresentationUrls(urls_); 458 dispatcher_.setDefaultPresentationUrls(urls_);
425 run_loop.RunUntilIdle(); 459 run_loop.RunUntilIdle();
426 } 460 }
427 461
428 } // namespace content 462 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698