OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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, | |
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 Loading... | |
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 Loading... | |
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 messages->push_back(new content::PresentationSessionMessage( | |
647 presentation_url, presentation_id, | |
648 scoped_ptr<std::vector<uint8_t>>(new std::vector<uint8_t>(binary_data)))); | |
649 message_cb.Run(messages.Pass()); | |
650 SaveQuitClosureAndRunLoop(); | |
651 } | |
mark a. foltz
2015/05/01 19:42:07
Would be a good idea to test:
- Reset() runs the s
haibinlu
2015/05/02 00:32:55
Done. Now DCHECK multiple listen, thus this case i
| |
652 | |
581 TEST_F(PresentationServiceImplTest, StartSessionInProgress) { | 653 TEST_F(PresentationServiceImplTest, StartSessionInProgress) { |
582 std::string presentation_url1("http://fooUrl"); | 654 std::string presentation_url1("http://fooUrl"); |
583 std::string presentation_id1("presentationId1"); | 655 std::string presentation_id1("presentationId1"); |
584 std::string presentation_url2("http://barUrl"); | 656 std::string presentation_url2("http://barUrl"); |
585 std::string presentation_id2("presentationId2"); | 657 std::string presentation_id2("presentationId2"); |
586 service_ptr_->StartSession( | 658 service_ptr_->StartSession( |
587 presentation_url1, | 659 presentation_url1, |
588 presentation_id1, | 660 presentation_id1, |
589 base::Bind( | 661 base::Bind( |
590 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackSuccess, | 662 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackSuccess, |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
662 RunLoopFor(TestTimeouts::tiny_timeout()); | 734 RunLoopFor(TestTimeouts::tiny_timeout()); |
663 | 735 |
664 ExpectReset(); | 736 ExpectReset(); |
665 service_impl_->Reset(); | 737 service_impl_->Reset(); |
666 ExpectCleanState(); | 738 ExpectCleanState(); |
667 SaveQuitClosureAndRunLoop(); | 739 SaveQuitClosureAndRunLoop(); |
668 EXPECT_EQ(1, default_session_started_count_); | 740 EXPECT_EQ(1, default_session_started_count_); |
669 } | 741 } |
670 | 742 |
671 } // namespace content | 743 } // namespace content |
OLD | NEW |