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

Side by Side Diff: content/browser/presentation/presentation_service_impl_unittest.cc

Issue 1118103002: Implements ListenForSessionMessages in PresentationServiceImpl; uses Swap to avoid copying large da… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added unit test Created 5 years, 7 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "base/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 #include "base/message_loop/message_loop.h" 6 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h" 7 #include "base/run_loop.h"
8 #include "base/test/test_timeouts.h" 8 #include "base/test/test_timeouts.h"
9 #include "content/browser/presentation/presentation_service_impl.h" 9 #include "content/browser/presentation/presentation_service_impl.h"
10 #include "content/public/browser/presentation_service_delegate.h" 10 #include "content/public/browser/presentation_service_delegate.h"
(...skipping 13 matching lines...) Expand all
24 24
25 namespace content { 25 namespace content {
26 26
27 namespace { 27 namespace {
28 28
29 bool ArePresentationSessionsEqual( 29 bool ArePresentationSessionsEqual(
30 const presentation::PresentationSessionInfo& expected, 30 const presentation::PresentationSessionInfo& expected,
31 const presentation::PresentationSessionInfo& actual) { 31 const presentation::PresentationSessionInfo& actual) {
32 return expected.url == actual.url && expected.id == actual.id; 32 return expected.url == actual.url && expected.id == actual.id;
33 } 33 }
34
35 bool ArePresentationSessionMessagesEqual(
36 const presentation::SessionMessage* expected,
37 const presentation::SessionMessage* actual) {
38 return expected->presentation_url == actual->presentation_url &&
39 expected->presentation_id == actual->presentation_id &&
40 expected->type == actual->type &&
41 expected->message == actual->message &&
42 expected->data.Equals(actual->data);
43 }
44
34 } // namespace 45 } // namespace
35 46
36 class MockPresentationServiceDelegate : public PresentationServiceDelegate { 47 class MockPresentationServiceDelegate : public PresentationServiceDelegate {
37 public: 48 public:
38 MOCK_METHOD3(AddObserver, 49 MOCK_METHOD3(AddObserver,
39 void(int render_process_id, 50 void(int render_process_id,
40 int render_frame_id, 51 int render_frame_id,
41 PresentationServiceDelegate::Observer* observer)); 52 PresentationServiceDelegate::Observer* observer));
42 MOCK_METHOD2(RemoveObserver, 53 MOCK_METHOD2(RemoveObserver,
43 void(int render_process_id, int render_frame_id)); 54 void(int render_process_id, int render_frame_id));
(...skipping 26 matching lines...) Expand all
70 const PresentationSessionSuccessCallback& success_cb, 81 const PresentationSessionSuccessCallback& success_cb,
71 const PresentationSessionErrorCallback& error_cb)); 82 const PresentationSessionErrorCallback& error_cb));
72 MOCK_METHOD6(JoinSession, 83 MOCK_METHOD6(JoinSession,
73 void( 84 void(
74 int render_process_id, 85 int render_process_id,
75 int render_frame_id, 86 int render_frame_id,
76 const std::string& presentation_url, 87 const std::string& presentation_url,
77 const std::string& presentation_id, 88 const std::string& presentation_id,
78 const PresentationSessionSuccessCallback& success_cb, 89 const PresentationSessionSuccessCallback& success_cb,
79 const PresentationSessionErrorCallback& error_cb)); 90 const PresentationSessionErrorCallback& error_cb));
91 MOCK_METHOD3(ListenForSessionMessages,
92 void(int render_process_id,
whywhat 2015/05/01 15:26:12 nit: use normal indent, e.g. 4 spaces.
haibinlu 2015/05/02 00:32:55 Done.
93 int render_frame_id,
94 const PresentationSessionMessageCallback& message_cb));
80 }; 95 };
81 96
82 class PresentationServiceImplTest : public RenderViewHostImplTestHarness { 97 class PresentationServiceImplTest : public RenderViewHostImplTestHarness {
83 public: 98 public:
84 PresentationServiceImplTest() 99 PresentationServiceImplTest()
85 : callback_count_(0), default_session_started_count_(0) {} 100 : callback_count_(0), default_session_started_count_(0) {}
86 101
87 void SetUp() override { 102 void SetUp() override {
88 RenderViewHostImplTestHarness::SetUp(); 103 RenderViewHostImplTestHarness::SetUp();
89 104
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 } 223 }
209 224
210 void ExpectDefaultSessionNull( 225 void ExpectDefaultSessionNull(
211 presentation::PresentationSessionInfoPtr actual_session) { 226 presentation::PresentationSessionInfoPtr actual_session) {
212 EXPECT_TRUE(actual_session.is_null()); 227 EXPECT_TRUE(actual_session.is_null());
213 ++default_session_started_count_; 228 ++default_session_started_count_;
214 if (!run_loop_quit_closure_.is_null()) 229 if (!run_loop_quit_closure_.is_null())
215 run_loop_quit_closure_.Run(); 230 run_loop_quit_closure_.Run();
216 } 231 }
217 232
233 void ExpectSessionMessages(
234 mojo::Array<presentation::SessionMessagePtr> actual_msgs) {
235 EXPECT_TRUE(!actual_msgs.is_null());
236 EXPECT_TRUE(actual_msgs.size() == expected_msgs_.size());
237 for (size_t i = 0; i < actual_msgs.size(); ++i) {
238 EXPECT_TRUE(ArePresentationSessionMessagesEqual(expected_msgs_[i].get(),
239 actual_msgs[i].get()));
240 }
241 if (!run_loop_quit_closure_.is_null())
242 run_loop_quit_closure_.Run();
243 }
244
218 MockPresentationServiceDelegate mock_delegate_; 245 MockPresentationServiceDelegate mock_delegate_;
219 scoped_ptr<PresentationServiceImpl> service_impl_; 246 scoped_ptr<PresentationServiceImpl> service_impl_;
220 mojo::InterfacePtr<presentation::PresentationService> service_ptr_; 247 mojo::InterfacePtr<presentation::PresentationService> service_ptr_;
221 base::Closure run_loop_quit_closure_; 248 base::Closure run_loop_quit_closure_;
222 int callback_count_; 249 int callback_count_;
223 int default_session_started_count_; 250 int default_session_started_count_;
251 mojo::Array<presentation::SessionMessagePtr> expected_msgs_;
224 }; 252 };
225 253
226 TEST_F(PresentationServiceImplTest, ListenForScreenAvailability) { 254 TEST_F(PresentationServiceImplTest, ListenForScreenAvailability) {
227 std::string presentation_url("http://fooUrl"); 255 std::string presentation_url("http://fooUrl");
228 ListenForScreenAvailabilityAndWait( 256 ListenForScreenAvailabilityAndWait(
229 presentation_url, 257 presentation_url,
230 base::Bind( 258 base::Bind(
231 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, 259 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback,
232 base::Unretained(this), true), 260 base::Unretained(this), true),
233 true); 261 true);
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 EXPECT_CALL(mock_delegate_, JoinSession( 599 EXPECT_CALL(mock_delegate_, JoinSession(
572 _, _, Eq(presentation_url), Eq(presentation_id), _, _)) 600 _, _, Eq(presentation_url), Eq(presentation_id), _, _))
573 .WillOnce(DoAll( 601 .WillOnce(DoAll(
574 InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit), 602 InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit),
575 SaveArg<5>(&error_cb))); 603 SaveArg<5>(&error_cb)));
576 run_loop.Run(); 604 run_loop.Run();
577 error_cb.Run(PresentationError(PRESENTATION_ERROR_UNKNOWN, "Error message")); 605 error_cb.Run(PresentationError(PRESENTATION_ERROR_UNKNOWN, "Error message"));
578 SaveQuitClosureAndRunLoop(); 606 SaveQuitClosureAndRunLoop();
579 } 607 }
580 608
609 TEST_F(PresentationServiceImplTest, ListenForSessionMessages) {
610 std::string presentation_url("http://fooUrl");
611 std::string presentation_id("presentationId");
612 std::string text_msg("123");
613 std::vector<uint8_t> binary_data{'\1', '\2', '\3'};
614
615 expected_msgs_ = mojo::Array<presentation::SessionMessagePtr>::New(2);
616 expected_msgs_[0] = presentation::SessionMessage::New();
617 expected_msgs_[0]->presentation_url = presentation_url;
618 expected_msgs_[0]->presentation_id = presentation_id;
619 expected_msgs_[0]->type =
620 presentation::PresentationMessageType::PRESENTATION_MESSAGE_TYPE_TEXT;
621 expected_msgs_[0]->message = text_msg;
622 expected_msgs_[1] = presentation::SessionMessage::New();
623 expected_msgs_[1]->presentation_url = presentation_url;
624 expected_msgs_[1]->presentation_id = presentation_id;
625 expected_msgs_[1]->type = presentation::PresentationMessageType::
626 PRESENTATION_MESSAGE_TYPE_ARRAY_BUFFER;
627 expected_msgs_[1]->data = mojo::Array<uint8_t>::From(binary_data);
628
629 service_ptr_->ListenForSessionMessages(
630 base::Bind(&PresentationServiceImplTest::ExpectSessionMessages,
631 base::Unretained(this)));
632
633 base::RunLoop run_loop;
634 base::Callback<void(scoped_ptr<ScopedVector<PresentationSessionMessage>>)>
635 message_cb;
636 EXPECT_CALL(mock_delegate_, ListenForSessionMessages(_, _, _))
637 .WillOnce(DoAll(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit),
638 SaveArg<2>(&message_cb)));
639 run_loop.Run();
640
641 scoped_ptr<ScopedVector<PresentationSessionMessage>> messages(
642 new ScopedVector<PresentationSessionMessage>());
643 messages->push_back(new content::PresentationSessionMessage(
644 presentation_url, presentation_id,
645 scoped_ptr<std::string>(new std::string(text_msg)),
646 scoped_ptr<std::vector<uint8_t>>(new std::vector<uint8_t>())));
647 messages->push_back(new content::PresentationSessionMessage(
648 presentation_url, presentation_id,
649 scoped_ptr<std::string>(new std::string()),
650 scoped_ptr<std::vector<uint8_t>>(new std::vector<uint8_t>(binary_data))));
651 message_cb.Run(messages.Pass());
652 SaveQuitClosureAndRunLoop();
653 }
654
581 TEST_F(PresentationServiceImplTest, StartSessionInProgress) { 655 TEST_F(PresentationServiceImplTest, StartSessionInProgress) {
582 std::string presentation_url1("http://fooUrl"); 656 std::string presentation_url1("http://fooUrl");
583 std::string presentation_id1("presentationId1"); 657 std::string presentation_id1("presentationId1");
584 std::string presentation_url2("http://barUrl"); 658 std::string presentation_url2("http://barUrl");
585 std::string presentation_id2("presentationId2"); 659 std::string presentation_id2("presentationId2");
586 service_ptr_->StartSession( 660 service_ptr_->StartSession(
587 presentation_url1, 661 presentation_url1,
588 presentation_id1, 662 presentation_id1,
589 base::Bind( 663 base::Bind(
590 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackSuccess, 664 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackSuccess,
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 RunLoopFor(TestTimeouts::tiny_timeout()); 736 RunLoopFor(TestTimeouts::tiny_timeout());
663 737
664 ExpectReset(); 738 ExpectReset();
665 service_impl_->Reset(); 739 service_impl_->Reset();
666 ExpectCleanState(); 740 ExpectCleanState();
667 SaveQuitClosureAndRunLoop(); 741 SaveQuitClosureAndRunLoop();
668 EXPECT_EQ(1, default_session_started_count_); 742 EXPECT_EQ(1, default_session_started_count_);
669 } 743 }
670 744
671 } // namespace content 745 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698