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

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: 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(
93 int render_process_id,
94 int render_frame_id,
95 const PresentationSessionMessageCallback& message_cb));
80 }; 96 };
81 97
82 class PresentationServiceImplTest : public RenderViewHostImplTestHarness { 98 class PresentationServiceImplTest : public RenderViewHostImplTestHarness {
83 public: 99 public:
84 PresentationServiceImplTest() 100 PresentationServiceImplTest()
85 : callback_count_(0), default_session_started_count_(0) {} 101 : callback_count_(0), default_session_started_count_(0) {}
86 102
87 void SetUp() override { 103 void SetUp() override {
88 RenderViewHostImplTestHarness::SetUp(); 104 RenderViewHostImplTestHarness::SetUp();
89 105
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 void ExpectReset() { 185 void ExpectReset() {
170 EXPECT_CALL(mock_delegate_, Reset(_, _)).Times(1); 186 EXPECT_CALL(mock_delegate_, Reset(_, _)).Times(1);
171 } 187 }
172 188
173 void ExpectCleanState() { 189 void ExpectCleanState() {
174 EXPECT_TRUE(service_impl_->availability_contexts_.empty()); 190 EXPECT_TRUE(service_impl_->availability_contexts_.empty());
175 EXPECT_TRUE(service_impl_->default_presentation_url_.empty()); 191 EXPECT_TRUE(service_impl_->default_presentation_url_.empty());
176 EXPECT_TRUE(service_impl_->default_presentation_id_.empty()); 192 EXPECT_TRUE(service_impl_->default_presentation_id_.empty());
177 EXPECT_TRUE(service_impl_->queued_start_session_requests_.empty()); 193 EXPECT_TRUE(service_impl_->queued_start_session_requests_.empty());
178 EXPECT_FALSE(service_impl_->default_session_start_context_.get()); 194 EXPECT_FALSE(service_impl_->default_session_start_context_.get());
195 EXPECT_FALSE(service_impl_->on_session_messages_callback_.get());
179 } 196 }
180 197
181 void ExpectNewSessionMojoCallbackSuccess( 198 void ExpectNewSessionMojoCallbackSuccess(
182 presentation::PresentationSessionInfoPtr info, 199 presentation::PresentationSessionInfoPtr info,
183 presentation::PresentationErrorPtr error) { 200 presentation::PresentationErrorPtr error) {
184 EXPECT_FALSE(info.is_null()); 201 EXPECT_FALSE(info.is_null());
185 EXPECT_TRUE(error.is_null()); 202 EXPECT_TRUE(error.is_null());
186 if (!run_loop_quit_closure_.is_null()) 203 if (!run_loop_quit_closure_.is_null())
187 run_loop_quit_closure_.Run(); 204 run_loop_quit_closure_.Run();
188 } 205 }
(...skipping 19 matching lines...) Expand all
208 } 225 }
209 226
210 void ExpectDefaultSessionNull( 227 void ExpectDefaultSessionNull(
211 presentation::PresentationSessionInfoPtr actual_session) { 228 presentation::PresentationSessionInfoPtr actual_session) {
212 EXPECT_TRUE(actual_session.is_null()); 229 EXPECT_TRUE(actual_session.is_null());
213 ++default_session_started_count_; 230 ++default_session_started_count_;
214 if (!run_loop_quit_closure_.is_null()) 231 if (!run_loop_quit_closure_.is_null())
215 run_loop_quit_closure_.Run(); 232 run_loop_quit_closure_.Run();
216 } 233 }
217 234
235 void ExpectSessionMessages(
236 mojo::Array<presentation::SessionMessagePtr> actual_msgs) {
237 EXPECT_TRUE(actual_msgs.size() == expected_msgs_.size());
238 for (size_t i = 0; i < actual_msgs.size(); ++i) {
239 EXPECT_TRUE(ArePresentationSessionMessagesEqual(expected_msgs_[i].get(),
240 actual_msgs[i].get()));
241 }
242 if (!run_loop_quit_closure_.is_null())
243 run_loop_quit_closure_.Run();
244 }
245
246 void RunListenForSessionMessages(std::string& text_msg,
247 std::vector<uint8_t>& binary_data) {
248 std::string presentation_url("http://fooUrl");
249 std::string presentation_id("presentationId");
250
251 expected_msgs_ = mojo::Array<presentation::SessionMessagePtr>::New(2);
252 expected_msgs_[0] = presentation::SessionMessage::New();
253 expected_msgs_[0]->presentation_url = presentation_url;
254 expected_msgs_[0]->presentation_id = presentation_id;
255 expected_msgs_[0]->type =
256 presentation::PresentationMessageType::PRESENTATION_MESSAGE_TYPE_TEXT;
257 expected_msgs_[0]->message = text_msg;
258 expected_msgs_[1] = presentation::SessionMessage::New();
259 expected_msgs_[1]->presentation_url = presentation_url;
260 expected_msgs_[1]->presentation_id = presentation_id;
261 expected_msgs_[1]->type = presentation::PresentationMessageType::
262 PRESENTATION_MESSAGE_TYPE_ARRAY_BUFFER;
263 expected_msgs_[1]->data = mojo::Array<uint8_t>::From(binary_data);
264
265 service_ptr_->ListenForSessionMessages(
266 base::Bind(&PresentationServiceImplTest::ExpectSessionMessages,
267 base::Unretained(this)));
268
269 base::RunLoop run_loop;
270 base::Callback<void(scoped_ptr<ScopedVector<PresentationSessionMessage>>)>
271 message_cb;
272 EXPECT_CALL(mock_delegate_, ListenForSessionMessages(_, _, _))
273 .WillOnce(DoAll(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit),
274 SaveArg<2>(&message_cb)));
275 run_loop.Run();
276
277 scoped_ptr<ScopedVector<PresentationSessionMessage>> messages(
278 new ScopedVector<PresentationSessionMessage>());
279 messages->push_back(
280 content::PresentationSessionMessage::CreateStringMessage(
281 presentation_url, presentation_id,
282 scoped_ptr<std::string>(new std::string(text_msg))));
283 messages->push_back(
284 content::PresentationSessionMessage::CreateBinaryMessage(
285 presentation_url, presentation_id,
286 scoped_ptr<std::vector<uint8_t>>(
287 new std::vector<uint8_t>(binary_data))));
288 message_cb.Run(messages.Pass());
289 SaveQuitClosureAndRunLoop();
290 }
291
218 MockPresentationServiceDelegate mock_delegate_; 292 MockPresentationServiceDelegate mock_delegate_;
219 scoped_ptr<PresentationServiceImpl> service_impl_; 293 scoped_ptr<PresentationServiceImpl> service_impl_;
220 mojo::InterfacePtr<presentation::PresentationService> service_ptr_; 294 mojo::InterfacePtr<presentation::PresentationService> service_ptr_;
221 base::Closure run_loop_quit_closure_; 295 base::Closure run_loop_quit_closure_;
222 int callback_count_; 296 int callback_count_;
223 int default_session_started_count_; 297 int default_session_started_count_;
298 mojo::Array<presentation::SessionMessagePtr> expected_msgs_;
224 }; 299 };
225 300
226 TEST_F(PresentationServiceImplTest, ListenForScreenAvailability) { 301 TEST_F(PresentationServiceImplTest, ListenForScreenAvailability) {
227 std::string presentation_url("http://fooUrl"); 302 std::string presentation_url("http://fooUrl");
228 ListenForScreenAvailabilityAndWait( 303 ListenForScreenAvailabilityAndWait(
229 presentation_url, 304 presentation_url,
230 base::Bind( 305 base::Bind(
231 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, 306 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback,
232 base::Unretained(this), true), 307 base::Unretained(this), true),
233 true); 308 true);
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 EXPECT_CALL(mock_delegate_, JoinSession( 646 EXPECT_CALL(mock_delegate_, JoinSession(
572 _, _, Eq(presentation_url), Eq(presentation_id), _, _)) 647 _, _, Eq(presentation_url), Eq(presentation_id), _, _))
573 .WillOnce(DoAll( 648 .WillOnce(DoAll(
574 InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit), 649 InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit),
575 SaveArg<5>(&error_cb))); 650 SaveArg<5>(&error_cb)));
576 run_loop.Run(); 651 run_loop.Run();
577 error_cb.Run(PresentationError(PRESENTATION_ERROR_UNKNOWN, "Error message")); 652 error_cb.Run(PresentationError(PRESENTATION_ERROR_UNKNOWN, "Error message"));
578 SaveQuitClosureAndRunLoop(); 653 SaveQuitClosureAndRunLoop();
579 } 654 }
580 655
656 TEST_F(PresentationServiceImplTest, ListenForSessionMessages) {
657 std::string text_msg("123");
658 std::vector<uint8_t> binary_data(3, '\1');
659 RunListenForSessionMessages(text_msg, binary_data);
660 }
661
662 TEST_F(PresentationServiceImplTest, ListenForSessionMessagesWithEmptyMsg) {
663 std::string text_msg("");
664 std::vector<uint8_t> binary_data{};
665 RunListenForSessionMessages(text_msg, binary_data);
666 }
667
668 TEST_F(PresentationServiceImplTest, ReceiveSessionMessagesAfterReset) {
669 std::string presentation_url("http://fooUrl");
670 std::string presentation_id("presentationId");
671 std::string text_msg("123");
672 expected_msgs_ = mojo::Array<presentation::SessionMessagePtr>();
673 service_ptr_->ListenForSessionMessages(
674 base::Bind(&PresentationServiceImplTest::ExpectSessionMessages,
675 base::Unretained(this)));
676
677 base::RunLoop run_loop;
678 base::Callback<void(scoped_ptr<ScopedVector<PresentationSessionMessage>>)>
679 message_cb;
680 EXPECT_CALL(mock_delegate_, ListenForSessionMessages(_, _, _))
681 .WillOnce(DoAll(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit),
682 SaveArg<2>(&message_cb)));
683 run_loop.Run();
684
685 scoped_ptr<ScopedVector<PresentationSessionMessage>> messages(
686 new ScopedVector<PresentationSessionMessage>());
687 messages->push_back(content::PresentationSessionMessage::CreateStringMessage(
688 presentation_url, presentation_id,
689 scoped_ptr<std::string>(new std::string(text_msg))));
690 ExpectReset();
691 service_impl_->Reset();
692 message_cb.Run(messages.Pass());
693 SaveQuitClosureAndRunLoop();
694 }
695
581 TEST_F(PresentationServiceImplTest, StartSessionInProgress) { 696 TEST_F(PresentationServiceImplTest, StartSessionInProgress) {
582 std::string presentation_url1("http://fooUrl"); 697 std::string presentation_url1("http://fooUrl");
583 std::string presentation_id1("presentationId1"); 698 std::string presentation_id1("presentationId1");
584 std::string presentation_url2("http://barUrl"); 699 std::string presentation_url2("http://barUrl");
585 std::string presentation_id2("presentationId2"); 700 std::string presentation_id2("presentationId2");
586 service_ptr_->StartSession( 701 service_ptr_->StartSession(
587 presentation_url1, 702 presentation_url1,
588 presentation_id1, 703 presentation_id1,
589 base::Bind( 704 base::Bind(
590 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackSuccess, 705 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackSuccess,
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 RunLoopFor(TestTimeouts::tiny_timeout()); 777 RunLoopFor(TestTimeouts::tiny_timeout());
663 778
664 ExpectReset(); 779 ExpectReset();
665 service_impl_->Reset(); 780 service_impl_->Reset();
666 ExpectCleanState(); 781 ExpectCleanState();
667 SaveQuitClosureAndRunLoop(); 782 SaveQuitClosureAndRunLoop();
668 EXPECT_EQ(1, default_session_started_count_); 783 EXPECT_EQ(1, default_session_started_count_);
669 } 784 }
670 785
671 } // namespace content 786 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/presentation/presentation_service_impl.cc ('k') | content/common/presentation/presentation_service.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698