| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/macros.h" | 5 #include "base/macros.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "mojo/public/cpp/application/application_connection.h" | 8 #include "mojo/public/cpp/application/application_connection.h" |
| 9 #include "mojo/public/cpp/application/application_impl.h" | 9 #include "mojo/public/cpp/application/application_impl.h" |
| 10 #include "mojo/public/cpp/application/application_test_base.h" | 10 #include "mojo/public/cpp/application/application_test_base.h" |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 NetAddressPtr src_addr_; | 221 NetAddressPtr src_addr_; |
| 222 Array<uint8_t> data_; | 222 Array<uint8_t> data_; |
| 223 }; | 223 }; |
| 224 | 224 |
| 225 struct ReceiveResult { | 225 struct ReceiveResult { |
| 226 NetworkErrorPtr result; | 226 NetworkErrorPtr result; |
| 227 NetAddressPtr addr; | 227 NetAddressPtr addr; |
| 228 Array<uint8_t> data; | 228 Array<uint8_t> data; |
| 229 }; | 229 }; |
| 230 | 230 |
| 231 class UDPSocketClientImpl : public UDPSocketClient { | 231 class UDPSocketReceiverImpl : public UDPSocketReceiver { |
| 232 public: | 232 public: |
| 233 UDPSocketClientImpl() : run_loop_(nullptr), expected_receive_count_(0) {} | 233 UDPSocketReceiverImpl() : run_loop_(nullptr), expected_receive_count_(0) {} |
| 234 | 234 |
| 235 ~UDPSocketClientImpl() override { | 235 ~UDPSocketReceiverImpl() override { |
| 236 while (!results_.empty()) { | 236 while (!results_.empty()) { |
| 237 delete results_.front(); | 237 delete results_.front(); |
| 238 results_.pop(); | 238 results_.pop(); |
| 239 } | 239 } |
| 240 } | 240 } |
| 241 | 241 |
| 242 std::queue<ReceiveResult*>* results() { | 242 std::queue<ReceiveResult*>* results() { |
| 243 return &results_; | 243 return &results_; |
| 244 } | 244 } |
| 245 | 245 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 268 if (results_.size() == expected_receive_count_ && run_loop_) { | 268 if (results_.size() == expected_receive_count_ && run_loop_) { |
| 269 expected_receive_count_ = 0; | 269 expected_receive_count_ = 0; |
| 270 run_loop_->Quit(); | 270 run_loop_->Quit(); |
| 271 } | 271 } |
| 272 } | 272 } |
| 273 | 273 |
| 274 base::RunLoop* run_loop_; | 274 base::RunLoop* run_loop_; |
| 275 std::queue<ReceiveResult*> results_; | 275 std::queue<ReceiveResult*> results_; |
| 276 size_t expected_receive_count_; | 276 size_t expected_receive_count_; |
| 277 | 277 |
| 278 DISALLOW_COPY_AND_ASSIGN(UDPSocketClientImpl); | 278 DISALLOW_COPY_AND_ASSIGN(UDPSocketReceiverImpl); |
| 279 }; | 279 }; |
| 280 | 280 |
| 281 class UDPSocketAppTest : public test::ApplicationTestBase { | 281 class UDPSocketAppTest : public test::ApplicationTestBase { |
| 282 public: | 282 public: |
| 283 UDPSocketAppTest() {} | 283 UDPSocketAppTest() : receiver_binding_(&receiver_) {} |
| 284 ~UDPSocketAppTest() override {} | 284 ~UDPSocketAppTest() override {} |
| 285 | 285 |
| 286 void SetUp() override { | 286 void SetUp() override { |
| 287 ApplicationTestBase::SetUp(); | 287 ApplicationTestBase::SetUp(); |
| 288 | 288 |
| 289 ApplicationConnection* connection = | 289 ApplicationConnection* connection = |
| 290 application_impl()->ConnectToApplication("mojo:network_service"); | 290 application_impl()->ConnectToApplication("mojo:network_service"); |
| 291 connection->ConnectToService(&network_service_); | 291 connection->ConnectToService(&network_service_); |
| 292 | 292 |
| 293 network_service_->CreateUDPSocket(GetProxy(&socket_)); | 293 network_service_->CreateUDPSocket(GetProxy(&socket_)); |
| 294 socket_.set_client(&receiver_); | 294 |
| 295 UDPSocketReceiverPtr receiver_proxy; |
| 296 receiver_binding_.Bind(&receiver_proxy); |
| 297 socket_->SetReceiver(receiver_proxy.Pass()); |
| 295 } | 298 } |
| 296 | 299 |
| 297 protected: | 300 protected: |
| 298 NetworkServicePtr network_service_; | 301 NetworkServicePtr network_service_; |
| 299 UDPSocketPtr socket_; | 302 UDPSocketPtr socket_; |
| 300 UDPSocketClientImpl receiver_; | 303 UDPSocketReceiverImpl receiver_; |
| 304 Binding<UDPSocketReceiver> receiver_binding_; |
| 301 | 305 |
| 302 DISALLOW_COPY_AND_ASSIGN(UDPSocketAppTest); | 306 DISALLOW_COPY_AND_ASSIGN(UDPSocketAppTest); |
| 303 }; | 307 }; |
| 304 | 308 |
| 305 } // namespace | 309 } // namespace |
| 306 | 310 |
| 307 TEST_F(UDPSocketAppTest, Settings) { | 311 TEST_F(UDPSocketAppTest, Settings) { |
| 308 TestCallback callback1; | 312 TestCallback callback1; |
| 309 socket_->AllowAddressReuse(callback1.callback()); | 313 socket_->AllowAddressReuse(callback1.callback()); |
| 310 callback1.WaitForResult(); | 314 callback1.WaitForResult(); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 TestCallbackWithAddress callback1; | 409 TestCallbackWithAddress callback1; |
| 406 socket_->Bind(GetLocalHostWithAnyPort(), callback1.callback()); | 410 socket_->Bind(GetLocalHostWithAnyPort(), callback1.callback()); |
| 407 callback1.WaitForResult(); | 411 callback1.WaitForResult(); |
| 408 ASSERT_EQ(net::OK, callback1.result()->code); | 412 ASSERT_EQ(net::OK, callback1.result()->code); |
| 409 ASSERT_NE(0u, callback1.net_address()->ipv4->port); | 413 ASSERT_NE(0u, callback1.net_address()->ipv4->port); |
| 410 | 414 |
| 411 NetAddressPtr server_addr = callback1.net_address().Clone(); | 415 NetAddressPtr server_addr = callback1.net_address().Clone(); |
| 412 | 416 |
| 413 UDPSocketPtr client_socket; | 417 UDPSocketPtr client_socket; |
| 414 network_service_->CreateUDPSocket(GetProxy(&client_socket)); | 418 network_service_->CreateUDPSocket(GetProxy(&client_socket)); |
| 415 UDPSocketClientImpl client_socket_receiver; | 419 |
| 416 client_socket.set_client(&client_socket_receiver); | 420 UDPSocketReceiverImpl client_socket_receiver; |
| 421 UDPSocketReceiverPtr receiver_proxy; |
| 422 Binding<UDPSocketReceiver> receiver_binding(&client_socket_receiver, |
| 423 &receiver_proxy); |
| 424 client_socket->SetReceiver(receiver_proxy.Pass()); |
| 417 | 425 |
| 418 TestCallbackWithAddress callback2; | 426 TestCallbackWithAddress callback2; |
| 419 client_socket->Connect(server_addr.Clone(), callback2.callback()); | 427 client_socket->Connect(server_addr.Clone(), callback2.callback()); |
| 420 callback2.WaitForResult(); | 428 callback2.WaitForResult(); |
| 421 ASSERT_EQ(net::OK, callback2.result()->code); | 429 ASSERT_EQ(net::OK, callback2.result()->code); |
| 422 ASSERT_NE(0u, callback2.net_address()->ipv4->port); | 430 ASSERT_NE(0u, callback2.net_address()->ipv4->port); |
| 423 | 431 |
| 424 NetAddressPtr client_addr = callback2.net_address().Clone(); | 432 NetAddressPtr client_addr = callback2.net_address().Clone(); |
| 425 | 433 |
| 426 const size_t kDatagramCount = 6; | 434 const size_t kDatagramCount = 6; |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 601 receive_callbacks[j].result()->code); | 609 receive_callbacks[j].result()->code); |
| 602 EXPECT_FALSE(receive_callbacks[j].src_addr()); | 610 EXPECT_FALSE(receive_callbacks[j].src_addr()); |
| 603 EXPECT_TRUE(receive_callbacks[j].data().Equals( | 611 EXPECT_TRUE(receive_callbacks[j].data().Equals( |
| 604 CreateTestMessage(static_cast<uint8_t>(j), kDatagramSize))); | 612 CreateTestMessage(static_cast<uint8_t>(j), kDatagramSize))); |
| 605 } | 613 } |
| 606 } | 614 } |
| 607 } | 615 } |
| 608 | 616 |
| 609 } // namespace service | 617 } // namespace service |
| 610 } // namespace mojo | 618 } // namespace mojo |
| OLD | NEW |