| 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" | 
| 11 #include "content/public/browser/presentation_session.h" | 11 #include "content/public/browser/presentation_session.h" | 
|  | 12 #include "content/public/common/presentation_constants.h" | 
| 12 #include "content/test/test_render_frame_host.h" | 13 #include "content/test/test_render_frame_host.h" | 
| 13 #include "content/test/test_render_view_host.h" | 14 #include "content/test/test_render_view_host.h" | 
| 14 #include "content/test/test_web_contents.h" | 15 #include "content/test/test_web_contents.h" | 
| 15 #include "mojo/public/cpp/bindings/interface_ptr.h" | 16 #include "mojo/public/cpp/bindings/interface_ptr.h" | 
| 16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" | 
| 17 | 18 | 
| 18 using ::testing::_; | 19 using ::testing::_; | 
| 19 using ::testing::Eq; | 20 using ::testing::Eq; | 
| 20 using ::testing::InvokeWithoutArgs; | 21 using ::testing::InvokeWithoutArgs; | 
| 21 using ::testing::Mock; | 22 using ::testing::Mock; | 
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 81           const PresentationSessionSuccessCallback& success_cb, | 82           const PresentationSessionSuccessCallback& success_cb, | 
| 82           const PresentationSessionErrorCallback& error_cb)); | 83           const PresentationSessionErrorCallback& error_cb)); | 
| 83   MOCK_METHOD6(JoinSession, | 84   MOCK_METHOD6(JoinSession, | 
| 84       void( | 85       void( | 
| 85           int render_process_id, | 86           int render_process_id, | 
| 86           int render_frame_id, | 87           int render_frame_id, | 
| 87           const std::string& presentation_url, | 88           const std::string& presentation_url, | 
| 88           const std::string& presentation_id, | 89           const std::string& presentation_id, | 
| 89           const PresentationSessionSuccessCallback& success_cb, | 90           const PresentationSessionSuccessCallback& success_cb, | 
| 90           const PresentationSessionErrorCallback& error_cb)); | 91           const PresentationSessionErrorCallback& error_cb)); | 
|  | 92 | 
| 91   MOCK_METHOD3(ListenForSessionMessages, | 93   MOCK_METHOD3(ListenForSessionMessages, | 
| 92       void( | 94       void( | 
| 93           int render_process_id, | 95           int render_process_id, | 
| 94           int render_frame_id, | 96           int render_frame_id, | 
| 95           const PresentationSessionMessageCallback& message_cb)); | 97           const PresentationSessionMessageCallback& message_cb)); | 
|  | 98 | 
|  | 99   MOCK_METHOD4(SendMessageRawPtr, | 
|  | 100       void( | 
|  | 101           int render_process_id, | 
|  | 102           int render_frame_id, | 
|  | 103           PresentationSessionMessage* message_request, | 
|  | 104           const SendMessageCallback& send_message_cb)); | 
|  | 105 | 
|  | 106   void SendMessage( | 
|  | 107       int render_process_id, | 
|  | 108       int render_frame_id, | 
|  | 109       scoped_ptr<PresentationSessionMessage> message_request, | 
|  | 110       const SendMessageCallback& send_message_cb) { | 
|  | 111     SendMessageRawPtr( | 
|  | 112         render_process_id, | 
|  | 113         render_frame_id, | 
|  | 114         message_request.release(), | 
|  | 115         send_message_cb); | 
|  | 116   } | 
| 96 }; | 117 }; | 
| 97 | 118 | 
| 98 class PresentationServiceImplTest : public RenderViewHostImplTestHarness { | 119 class PresentationServiceImplTest : public RenderViewHostImplTestHarness { | 
| 99  public: | 120  public: | 
| 100   PresentationServiceImplTest() | 121   PresentationServiceImplTest() | 
| 101       : callback_count_(0), default_session_started_count_(0) {} | 122       : callback_count_(0), default_session_started_count_(0) {} | 
| 102 | 123 | 
| 103   void SetUp() override { | 124   void SetUp() override { | 
| 104     RenderViewHostImplTestHarness::SetUp(); | 125     RenderViewHostImplTestHarness::SetUp(); | 
| 105 | 126 | 
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 236       mojo::Array<presentation::SessionMessagePtr> actual_msgs) { | 257       mojo::Array<presentation::SessionMessagePtr> actual_msgs) { | 
| 237     EXPECT_TRUE(actual_msgs.size() == expected_msgs_.size()); | 258     EXPECT_TRUE(actual_msgs.size() == expected_msgs_.size()); | 
| 238     for (size_t i = 0; i < actual_msgs.size(); ++i) { | 259     for (size_t i = 0; i < actual_msgs.size(); ++i) { | 
| 239       EXPECT_TRUE(ArePresentationSessionMessagesEqual(expected_msgs_[i].get(), | 260       EXPECT_TRUE(ArePresentationSessionMessagesEqual(expected_msgs_[i].get(), | 
| 240                                                       actual_msgs[i].get())); | 261                                                       actual_msgs[i].get())); | 
| 241     } | 262     } | 
| 242     if (!run_loop_quit_closure_.is_null()) | 263     if (!run_loop_quit_closure_.is_null()) | 
| 243       run_loop_quit_closure_.Run(); | 264       run_loop_quit_closure_.Run(); | 
| 244   } | 265   } | 
| 245 | 266 | 
|  | 267   void ExpectSendMessageMojoCallback(bool success) { | 
|  | 268     EXPECT_TRUE(success); | 
|  | 269     EXPECT_FALSE(service_impl_->send_message_callback_); | 
|  | 270     if (!run_loop_quit_closure_.is_null()) | 
|  | 271       run_loop_quit_closure_.Run(); | 
|  | 272   } | 
|  | 273 | 
| 246   void RunListenForSessionMessages(std::string& text_msg, | 274   void RunListenForSessionMessages(std::string& text_msg, | 
| 247                                    std::vector<uint8_t>& binary_data) { | 275                                    std::vector<uint8_t>& binary_data) { | 
| 248     std::string presentation_url("http://fooUrl"); | 276     std::string presentation_url("http://fooUrl"); | 
| 249     std::string presentation_id("presentationId"); | 277     std::string presentation_id("presentationId"); | 
| 250 | 278 | 
| 251     expected_msgs_ = mojo::Array<presentation::SessionMessagePtr>::New(2); | 279     expected_msgs_ = mojo::Array<presentation::SessionMessagePtr>::New(2); | 
| 252     expected_msgs_[0] = presentation::SessionMessage::New(); | 280     expected_msgs_[0] = presentation::SessionMessage::New(); | 
| 253     expected_msgs_[0]->presentation_url = presentation_url; | 281     expected_msgs_[0]->presentation_url = presentation_url; | 
| 254     expected_msgs_[0]->presentation_id = presentation_id; | 282     expected_msgs_[0]->presentation_id = presentation_id; | 
| 255     expected_msgs_[0]->type = | 283     expected_msgs_[0]->type = | 
| (...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 776                  base::Unretained(this))); | 804                  base::Unretained(this))); | 
| 777   RunLoopFor(TestTimeouts::tiny_timeout()); | 805   RunLoopFor(TestTimeouts::tiny_timeout()); | 
| 778 | 806 | 
| 779   ExpectReset(); | 807   ExpectReset(); | 
| 780   service_impl_->Reset(); | 808   service_impl_->Reset(); | 
| 781   ExpectCleanState(); | 809   ExpectCleanState(); | 
| 782   SaveQuitClosureAndRunLoop(); | 810   SaveQuitClosureAndRunLoop(); | 
| 783   EXPECT_EQ(1, default_session_started_count_); | 811   EXPECT_EQ(1, default_session_started_count_); | 
| 784 } | 812 } | 
| 785 | 813 | 
|  | 814 TEST_F(PresentationServiceImplTest, SendStringMessage) { | 
|  | 815   std::string presentation_url("http://foo.com/index.html"); | 
|  | 816   std::string presentation_id("presentationId"); | 
|  | 817   std::string message("Test presentation session message"); | 
|  | 818 | 
|  | 819   presentation::SessionMessagePtr message_request( | 
|  | 820       presentation::SessionMessage::New()); | 
|  | 821   message_request->presentation_url = presentation_url; | 
|  | 822   message_request->presentation_id = presentation_id; | 
|  | 823   message_request->type = presentation::PresentationMessageType:: | 
|  | 824                           PRESENTATION_MESSAGE_TYPE_TEXT; | 
|  | 825   message_request->message = message; | 
|  | 826   service_ptr_->SendSessionMessage( | 
|  | 827       message_request.Pass(), | 
|  | 828       base::Bind( | 
|  | 829           &PresentationServiceImplTest::ExpectSendMessageMojoCallback, | 
|  | 830           base::Unretained(this))); | 
|  | 831 | 
|  | 832   base::RunLoop run_loop; | 
|  | 833   base::Closure send_message_cb; | 
|  | 834   PresentationSessionMessage* test_message = nullptr; | 
|  | 835   EXPECT_CALL(mock_delegate_, SendMessageRawPtr( | 
|  | 836       _, _, _, _)) | 
|  | 837       .WillOnce(DoAll( | 
|  | 838           InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit), | 
|  | 839           SaveArg<2>(&test_message), | 
|  | 840           SaveArg<3>(&send_message_cb))); | 
|  | 841   run_loop.Run(); | 
|  | 842 | 
|  | 843   EXPECT_TRUE(test_message); | 
|  | 844   EXPECT_EQ(presentation_url, test_message->presentation_url); | 
|  | 845   EXPECT_EQ(presentation_id, test_message->presentation_id); | 
|  | 846   EXPECT_FALSE(test_message->is_binary()); | 
|  | 847   EXPECT_TRUE(test_message->message.get()->size() <= | 
|  | 848               kMaxPresentationSessionMessageSize); | 
|  | 849   EXPECT_EQ(message, *(test_message->message.get())); | 
|  | 850   EXPECT_FALSE(test_message->data); | 
|  | 851   delete test_message; | 
|  | 852   send_message_cb.Run(); | 
|  | 853   SaveQuitClosureAndRunLoop(); | 
|  | 854 } | 
|  | 855 | 
|  | 856 TEST_F(PresentationServiceImplTest, SendArrayBuffer) { | 
|  | 857   std::string presentation_url("http://foo.com/index.html"); | 
|  | 858   std::string presentation_id("presentationId"); | 
|  | 859   // Test Array buffer data. | 
|  | 860   const uint8 buffer[] = {0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48}; | 
|  | 861   std::vector<uint8> data; | 
|  | 862   data.assign(buffer, buffer + sizeof(buffer)); | 
|  | 863 | 
|  | 864   presentation::SessionMessagePtr message_request( | 
|  | 865       presentation::SessionMessage::New()); | 
|  | 866   message_request->presentation_url = presentation_url; | 
|  | 867   message_request->presentation_id = presentation_id; | 
|  | 868   message_request->type = presentation::PresentationMessageType:: | 
|  | 869                           PRESENTATION_MESSAGE_TYPE_ARRAY_BUFFER; | 
|  | 870   message_request->data = mojo::Array<uint8>::From(data); | 
|  | 871   service_ptr_->SendSessionMessage( | 
|  | 872       message_request.Pass(), | 
|  | 873       base::Bind( | 
|  | 874           &PresentationServiceImplTest::ExpectSendMessageMojoCallback, | 
|  | 875           base::Unretained(this))); | 
|  | 876 | 
|  | 877   base::RunLoop run_loop; | 
|  | 878   base::Closure send_message_cb; | 
|  | 879   PresentationSessionMessage* test_message = nullptr; | 
|  | 880   EXPECT_CALL(mock_delegate_, SendMessageRawPtr( | 
|  | 881       _, _, _, _)) | 
|  | 882       .WillOnce(DoAll( | 
|  | 883           InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit), | 
|  | 884           SaveArg<2>(&test_message), | 
|  | 885           SaveArg<3>(&send_message_cb))); | 
|  | 886   run_loop.Run(); | 
|  | 887 | 
|  | 888   EXPECT_TRUE(test_message); | 
|  | 889   EXPECT_EQ(presentation_url, test_message->presentation_url); | 
|  | 890   EXPECT_EQ(presentation_id, test_message->presentation_id); | 
|  | 891   EXPECT_TRUE(test_message->is_binary()); | 
|  | 892   EXPECT_FALSE(test_message->message); | 
|  | 893   EXPECT_EQ(data.size(), test_message->data.get()->size()); | 
|  | 894   EXPECT_TRUE(test_message->data.get()->size() <= | 
|  | 895               kMaxPresentationSessionMessageSize); | 
|  | 896   EXPECT_EQ(0, memcmp(buffer, &(*test_message->data.get())[0], sizeof(buffer))); | 
|  | 897   delete test_message; | 
|  | 898   send_message_cb.Run(); | 
|  | 899   SaveQuitClosureAndRunLoop(); | 
|  | 900 } | 
|  | 901 | 
|  | 902 TEST_F(PresentationServiceImplTest, SendArrayBufferWithExceedingLimit) { | 
|  | 903   std::string presentation_url("http://foo.com/index.html"); | 
|  | 904   std::string presentation_id("presentationId"); | 
|  | 905   // Create buffer with size exceeding the limit. | 
|  | 906   // Use same size as in content::kMaxPresentationSessionMessageSize. | 
|  | 907   const size_t kMaxBufferSizeInBytes = 64 * 1024; // 64 KB. | 
|  | 908   uint8 buffer[kMaxBufferSizeInBytes+1]; | 
|  | 909   memset(buffer, 0, kMaxBufferSizeInBytes+1); | 
|  | 910   std::vector<uint8> data; | 
|  | 911   data.assign(buffer, buffer + sizeof(buffer)); | 
|  | 912 | 
|  | 913   presentation::SessionMessagePtr message_request( | 
|  | 914       presentation::SessionMessage::New()); | 
|  | 915   message_request->presentation_url = presentation_url; | 
|  | 916   message_request->presentation_id = presentation_id; | 
|  | 917   message_request->type = presentation::PresentationMessageType:: | 
|  | 918                           PRESENTATION_MESSAGE_TYPE_ARRAY_BUFFER; | 
|  | 919   message_request->data = mojo::Array<uint8>::From(data); | 
|  | 920   service_ptr_->SendSessionMessage( | 
|  | 921       message_request.Pass(), | 
|  | 922       base::Bind( | 
|  | 923           &PresentationServiceImplTest::ExpectSendMessageMojoCallback, | 
|  | 924           base::Unretained(this))); | 
|  | 925 | 
|  | 926   base::RunLoop run_loop; | 
|  | 927   base::Closure send_message_cb; | 
|  | 928   PresentationSessionMessage* test_message = nullptr; | 
|  | 929   EXPECT_CALL(mock_delegate_, SendMessageRawPtr( | 
|  | 930       _, _, _, _)) | 
|  | 931       .WillOnce(DoAll( | 
|  | 932           InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit), | 
|  | 933           SaveArg<2>(&test_message), | 
|  | 934           SaveArg<3>(&send_message_cb))); | 
|  | 935   run_loop.Run(); | 
|  | 936 | 
|  | 937   EXPECT_FALSE(test_message); | 
|  | 938   send_message_cb.Run(); | 
|  | 939   SaveQuitClosureAndRunLoop(); | 
|  | 940 } | 
|  | 941 | 
| 786 }  // namespace content | 942 }  // namespace content | 
| OLD | NEW | 
|---|