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

Side by Side Diff: extensions/browser/api/cast_channel/cast_channel_apitest.cc

Issue 2925053005: [cast_channel] Implement CastSocketService::OpenSocket() (Closed)
Patch Set: resolve code review comments from Mark Created 3 years, 6 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 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/bind.h" 5 #include "base/bind.h"
6 #include "base/command_line.h" 6 #include "base/command_line.h"
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/timer/mock_timer.h" 9 #include "base/timer/mock_timer.h"
10 #include "build/build_config.h" 10 #include "build/build_config.h"
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 using ::testing::_; 49 using ::testing::_;
50 using ::testing::A; 50 using ::testing::A;
51 using ::testing::DoAll; 51 using ::testing::DoAll;
52 using ::testing::Invoke; 52 using ::testing::Invoke;
53 using ::testing::InSequence; 53 using ::testing::InSequence;
54 using ::testing::NotNull; 54 using ::testing::NotNull;
55 using ::testing::Return; 55 using ::testing::Return;
56 using ::testing::ReturnRef; 56 using ::testing::ReturnRef;
57 using ::testing::ReturnPointee; 57 using ::testing::ReturnPointee;
58 using ::testing::SaveArg; 58 using ::testing::SaveArg;
59 using ::testing::WithArgs;
59 60
60 namespace { 61 namespace {
61 62
62 const char kTestExtensionId[] = "ddchlicdkolnonkihahngkmmmjnjlkkf"; 63 const char kTestExtensionId[] = "ddchlicdkolnonkihahngkmmmjnjlkkf";
63 64
64 static void FillCastMessage(const std::string& message, 65 static void FillCastMessage(const std::string& message,
65 CastMessage* cast_message) { 66 CastMessage* cast_message) {
66 cast_message->set_namespace_("foo"); 67 cast_message->set_namespace_("foo");
67 cast_message->set_source_id("src"); 68 cast_message->set_source_id("src");
68 cast_message->set_destination_id("dest"); 69 cast_message->set_destination_id("dest");
(...skipping 16 matching lines...) Expand all
85 void SetUpCommandLine(base::CommandLine* command_line) override { 86 void SetUpCommandLine(base::CommandLine* command_line) override {
86 ExtensionApiTest::SetUpCommandLine(command_line); 87 ExtensionApiTest::SetUpCommandLine(command_line);
87 command_line->AppendSwitchASCII( 88 command_line->AppendSwitchASCII(
88 extensions::switches::kWhitelistedExtensionID, kTestExtensionId); 89 extensions::switches::kWhitelistedExtensionID, kTestExtensionId);
89 } 90 }
90 91
91 void SetUpMockCastSocket() { 92 void SetUpMockCastSocket() {
92 extensions::CastChannelAPI* api = GetApi(); 93 extensions::CastChannelAPI* api = GetApi();
93 94
94 net::IPEndPoint ip_endpoint(net::IPAddress(192, 168, 1, 1), 8009); 95 net::IPEndPoint ip_endpoint(net::IPAddress(192, 168, 1, 1), 8009);
95 mock_cast_socket_ = new MockCastSocket; 96 mock_cast_socket_ = new MockCastSocket();
97 mock_cast_socket_->SetIPEndpoint(ip_endpoint_);
98 mock_cast_socket_->SetKeepAlive(false);
96 // Transfers ownership of the socket. 99 // Transfers ownership of the socket.
97 api->SetSocketForTest(base::WrapUnique<CastSocket>(mock_cast_socket_)); 100 api->SetSocketForTest(base::WrapUnique<CastSocket>(mock_cast_socket_));
98 ON_CALL(*mock_cast_socket_, set_id(_))
99 .WillByDefault(SaveArg<0>(&channel_id_));
100 ON_CALL(*mock_cast_socket_, id())
101 .WillByDefault(ReturnPointee(&channel_id_));
102 ON_CALL(*mock_cast_socket_, ip_endpoint())
103 .WillByDefault(ReturnRef(ip_endpoint_));
104 ON_CALL(*mock_cast_socket_, keep_alive()).WillByDefault(Return(false));
105 } 101 }
106 102
107 void SetUpOpenSendClose() { 103 void SetUpOpenSendClose() {
108 SetUpMockCastSocket(); 104 SetUpMockCastSocket();
109 EXPECT_CALL(*mock_cast_socket_, error_state()) 105 mock_cast_socket_->SetErrorState(ChannelError::NONE);
110 .WillRepeatedly(Return(ChannelError::NONE));
111 { 106 {
112 InSequence sequence; 107 InSequence sequence;
113 108
109 EXPECT_CALL(*mock_cast_socket_, AddObserver(_));
114 EXPECT_CALL(*mock_cast_socket_, Connect(_)) 110 EXPECT_CALL(*mock_cast_socket_, Connect(_))
115 .WillOnce(InvokeCompletionCallback<0>(ChannelError::NONE)); 111 .WillOnce(WithArgs<0>(
112 Invoke([&](const CastSocket::OnOpenCallback& callback) {
113 callback.Run(mock_cast_socket_->id(), ChannelError::NONE);
114 })));
116 EXPECT_CALL(*mock_cast_socket_, ready_state()) 115 EXPECT_CALL(*mock_cast_socket_, ready_state())
117 .WillOnce(Return(ReadyState::OPEN)); 116 .WillOnce(Return(ReadyState::OPEN));
118 EXPECT_CALL(*mock_cast_socket_->mock_transport(), 117 EXPECT_CALL(*mock_cast_socket_->mock_transport(),
119 SendMessage(A<const CastMessage&>(), _)) 118 SendMessage(A<const CastMessage&>(), _))
120 .WillOnce(InvokeCompletionCallback<1>(net::OK)); 119 .WillOnce(InvokeCompletionCallback<1>(net::OK));
121 EXPECT_CALL(*mock_cast_socket_, ready_state()) 120 EXPECT_CALL(*mock_cast_socket_, ready_state())
122 .WillOnce(Return(ReadyState::OPEN)); 121 .WillOnce(Return(ReadyState::OPEN));
123 EXPECT_CALL(*mock_cast_socket_, Close(_)) 122 EXPECT_CALL(*mock_cast_socket_, Close(_))
124 .WillOnce(InvokeCompletionCallback<0>(net::OK)); 123 .WillOnce(InvokeCompletionCallback<0>(net::OK));
125 EXPECT_CALL(*mock_cast_socket_, ready_state()) 124 EXPECT_CALL(*mock_cast_socket_, ready_state())
126 .WillOnce(Return(ReadyState::CLOSED)); 125 .WillOnce(Return(ReadyState::CLOSED));
127 } 126 }
128 } 127 }
129 128
130 void SetUpOpenPingTimeout() { 129 void SetUpOpenPingTimeout() {
131 SetUpMockCastSocket(); 130 SetUpMockCastSocket();
132 EXPECT_CALL(*mock_cast_socket_, error_state()) 131 mock_cast_socket_->SetErrorState(ChannelError::NONE);
133 .WillRepeatedly(Return(ChannelError::NONE)); 132 mock_cast_socket_->SetKeepAlive(true);
134 EXPECT_CALL(*mock_cast_socket_, keep_alive()).WillRepeatedly(Return(true));
135 { 133 {
136 InSequence sequence; 134 InSequence sequence;
137 EXPECT_CALL(*mock_cast_socket_, AddObserver(_)) 135 EXPECT_CALL(*mock_cast_socket_, AddObserver(_))
138 .WillOnce(SaveArg<0>(&message_observer_)); 136 .WillOnce(SaveArg<0>(&message_observer_));
139 EXPECT_CALL(*mock_cast_socket_, Connect(_)) 137 EXPECT_CALL(*mock_cast_socket_, Connect(_))
140 .WillOnce(InvokeCompletionCallback<0>(ChannelError::NONE)); 138 .WillOnce(WithArgs<0>(
139 Invoke([&](const CastSocket::OnOpenCallback& callback) {
140 callback.Run(mock_cast_socket_->id(), ChannelError::NONE);
141 })));
141 EXPECT_CALL(*mock_cast_socket_, ready_state()) 142 EXPECT_CALL(*mock_cast_socket_, ready_state())
142 .WillOnce(Return(ReadyState::OPEN)) 143 .WillOnce(Return(ReadyState::OPEN))
143 .RetiresOnSaturation(); 144 .RetiresOnSaturation();
144 EXPECT_CALL(*mock_cast_socket_, ready_state()) 145 EXPECT_CALL(*mock_cast_socket_, ready_state())
145 .WillOnce(Return(ReadyState::CLOSED)); 146 .WillOnce(Return(ReadyState::CLOSED));
146 } 147 }
147 } 148 }
148 149
149 extensions::CastChannelAPI* GetApi() { 150 extensions::CastChannelAPI* GetApi() {
150 return extensions::CastChannelAPI::Get(profile()); 151 return extensions::CastChannelAPI::Get(profile());
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 // always return true without actually running the test. Remove when fixed. 282 // always return true without actually running the test. Remove when fixed.
282 #if defined(OS_WIN) && !defined(NDEBUG) 283 #if defined(OS_WIN) && !defined(NDEBUG)
283 #define MAYBE_TestOpenReceiveClose DISABLED_TestOpenReceiveClose 284 #define MAYBE_TestOpenReceiveClose DISABLED_TestOpenReceiveClose
284 #else 285 #else
285 #define MAYBE_TestOpenReceiveClose TestOpenReceiveClose 286 #define MAYBE_TestOpenReceiveClose TestOpenReceiveClose
286 #endif 287 #endif
287 // Test loading extension, opening a channel, adding a listener, 288 // Test loading extension, opening a channel, adding a listener,
288 // writing, reading, and closing. 289 // writing, reading, and closing.
289 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenReceiveClose) { 290 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenReceiveClose) {
290 SetUpMockCastSocket(); 291 SetUpMockCastSocket();
291 EXPECT_CALL(*mock_cast_socket_, error_state()) 292 mock_cast_socket_->SetErrorState(ChannelError::NONE);
292 .WillRepeatedly(Return(ChannelError::NONE));
293 293
294 { 294 {
295 InSequence sequence; 295 InSequence sequence;
296 EXPECT_CALL(*mock_cast_socket_, AddObserver(_)) 296 EXPECT_CALL(*mock_cast_socket_, AddObserver(_))
297 .WillOnce(SaveArg<0>(&message_observer_)); 297 .WillOnce(SaveArg<0>(&message_observer_));
298 EXPECT_CALL(*mock_cast_socket_, Connect(_)) 298 EXPECT_CALL(*mock_cast_socket_, Connect(_))
299 .WillOnce(InvokeCompletionCallback<0>(ChannelError::NONE)); 299 .WillOnce(
300 WithArgs<0>(Invoke([&](const CastSocket::OnOpenCallback& callback) {
301 callback.Run(mock_cast_socket_->id(), ChannelError::NONE);
302 })));
300 EXPECT_CALL(*mock_cast_socket_, ready_state()) 303 EXPECT_CALL(*mock_cast_socket_, ready_state())
301 .Times(3) 304 .Times(3)
302 .WillRepeatedly(Return(ReadyState::OPEN)); 305 .WillRepeatedly(Return(ReadyState::OPEN));
303 EXPECT_CALL(*mock_cast_socket_, Close(_)) 306 EXPECT_CALL(*mock_cast_socket_, Close(_))
304 .WillOnce(InvokeCompletionCallback<0>(net::OK)); 307 .WillOnce(InvokeCompletionCallback<0>(net::OK));
305 EXPECT_CALL(*mock_cast_socket_, ready_state()) 308 EXPECT_CALL(*mock_cast_socket_, ready_state())
306 .WillOnce(Return(ReadyState::CLOSED)); 309 .WillOnce(Return(ReadyState::CLOSED));
307 } 310 }
308 311
309 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", 312 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api",
(...skipping 13 matching lines...) Expand all
323 #define MAYBE_TestOpenError TestOpenError 326 #define MAYBE_TestOpenError TestOpenError
324 #endif 327 #endif
325 // Test the case when socket open results in an error. 328 // Test the case when socket open results in an error.
326 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenError) { 329 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenError) {
327 SetUpMockCastSocket(); 330 SetUpMockCastSocket();
328 331
329 EXPECT_CALL(*mock_cast_socket_, AddObserver(_)) 332 EXPECT_CALL(*mock_cast_socket_, AddObserver(_))
330 .WillOnce(DoAll(SaveArg<0>(&message_observer_), 333 .WillOnce(DoAll(SaveArg<0>(&message_observer_),
331 InvokeObserverOnError(this, GetApi()))); 334 InvokeObserverOnError(this, GetApi())));
332 EXPECT_CALL(*mock_cast_socket_, Connect(_)) 335 EXPECT_CALL(*mock_cast_socket_, Connect(_))
333 .WillOnce(InvokeCompletionCallback<0>(ChannelError::CONNECT_ERROR)); 336 .WillOnce(
334 EXPECT_CALL(*mock_cast_socket_, error_state()) 337 WithArgs<0>(Invoke([&](const CastSocket::OnOpenCallback& callback) {
335 .WillRepeatedly(Return(ChannelError::CONNECT_ERROR)); 338 callback.Run(mock_cast_socket_->id(), ChannelError::CONNECT_ERROR);
339 })));
340 mock_cast_socket_->SetErrorState(ChannelError::CONNECT_ERROR);
336 EXPECT_CALL(*mock_cast_socket_, ready_state()) 341 EXPECT_CALL(*mock_cast_socket_, ready_state())
337 .WillRepeatedly(Return(ReadyState::CLOSED)); 342 .WillRepeatedly(Return(ReadyState::CLOSED));
338 EXPECT_CALL(*mock_cast_socket_, Close(_)) 343 EXPECT_CALL(*mock_cast_socket_, Close(_))
339 .WillOnce(InvokeCompletionCallback<0>(net::OK)); 344 .WillOnce(InvokeCompletionCallback<0>(net::OK));
340 345
341 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", 346 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api",
342 "test_open_error.html")); 347 "test_open_error.html"));
343 } 348 }
344 349
345 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, TestOpenInvalidConnectInfo) { 350 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, TestOpenInvalidConnectInfo) {
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 "\"keepAlive\": true, " 429 "\"keepAlive\": true, "
425 "\"audioOnly\": false, " 430 "\"audioOnly\": false, "
426 "\"connectInfo\": " 431 "\"connectInfo\": "
427 "{\"ipAddress\": \"127.0.0.1\", \"port\": 8009, " 432 "{\"ipAddress\": \"127.0.0.1\", \"port\": 8009, "
428 "\"auth\": \"ssl_verified\"}, \"readyState\": \"open\"}, " 433 "\"auth\": \"ssl_verified\"}, \"readyState\": \"open\"}, "
429 "{\"namespace_\": \"foo\", \"sourceId\": \"src\", " 434 "{\"namespace_\": \"foo\", \"sourceId\": \"src\", "
430 "\"destinationId\": \"\", \"data\": \"data\"}]", 435 "\"destinationId\": \"\", \"data\": \"data\"}]",
431 browser()); 436 browser());
432 EXPECT_EQ(error, "message_info.destination_id is required"); 437 EXPECT_EQ(error, "message_info.destination_id is required");
433 } 438 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698