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/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 Loading... |
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 Loading... |
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 |
114 EXPECT_CALL(*mock_cast_socket_, Connect(_)) | 109 EXPECT_CALL(*mock_cast_socket_, Connect(_)) |
115 .WillOnce(InvokeCompletionCallback<0>(ChannelError::NONE)); | 110 .WillOnce(WithArgs<0>( |
| 111 Invoke([&](const CastSocket::OnOpenCallback& callback) { |
| 112 callback.Run(mock_cast_socket_->id(), ChannelError::NONE); |
| 113 }))); |
116 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 114 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
117 .WillOnce(Return(ReadyState::OPEN)); | 115 .WillOnce(Return(ReadyState::OPEN)); |
118 EXPECT_CALL(*mock_cast_socket_->mock_transport(), | 116 EXPECT_CALL(*mock_cast_socket_->mock_transport(), |
119 SendMessage(A<const CastMessage&>(), _)) | 117 SendMessage(A<const CastMessage&>(), _)) |
120 .WillOnce(InvokeCompletionCallback<1>(net::OK)); | 118 .WillOnce(InvokeCompletionCallback<1>(net::OK)); |
121 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 119 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
122 .WillOnce(Return(ReadyState::OPEN)); | 120 .WillOnce(Return(ReadyState::OPEN)); |
123 EXPECT_CALL(*mock_cast_socket_, Close(_)) | 121 EXPECT_CALL(*mock_cast_socket_, Close(_)) |
124 .WillOnce(InvokeCompletionCallback<0>(net::OK)); | 122 .WillOnce(InvokeCompletionCallback<0>(net::OK)); |
125 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 123 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
126 .WillOnce(Return(ReadyState::CLOSED)); | 124 .WillOnce(Return(ReadyState::CLOSED)); |
127 } | 125 } |
128 } | 126 } |
129 | 127 |
130 void SetUpOpenPingTimeout() { | 128 void SetUpOpenPingTimeout() { |
131 SetUpMockCastSocket(); | 129 SetUpMockCastSocket(); |
132 EXPECT_CALL(*mock_cast_socket_, error_state()) | 130 mock_cast_socket_->SetErrorState(ChannelError::NONE); |
133 .WillRepeatedly(Return(ChannelError::NONE)); | 131 mock_cast_socket_->SetKeepAlive(true); |
134 EXPECT_CALL(*mock_cast_socket_, keep_alive()).WillRepeatedly(Return(true)); | |
135 { | 132 { |
136 InSequence sequence; | 133 InSequence sequence; |
137 EXPECT_CALL(*mock_cast_socket_, AddObserver(_)) | 134 EXPECT_CALL(*mock_cast_socket_, AddObserver(_)) |
138 .WillOnce(SaveArg<0>(&message_observer_)); | 135 .WillOnce(SaveArg<0>(&message_observer_)); |
139 EXPECT_CALL(*mock_cast_socket_, Connect(_)) | 136 EXPECT_CALL(*mock_cast_socket_, Connect(_)) |
140 .WillOnce(InvokeCompletionCallback<0>(ChannelError::NONE)); | 137 .WillOnce(WithArgs<0>( |
| 138 Invoke([&](const CastSocket::OnOpenCallback& callback) { |
| 139 callback.Run(mock_cast_socket_->id(), ChannelError::NONE); |
| 140 }))); |
141 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 141 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
142 .WillOnce(Return(ReadyState::OPEN)) | 142 .WillOnce(Return(ReadyState::OPEN)) |
143 .RetiresOnSaturation(); | 143 .RetiresOnSaturation(); |
144 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 144 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
145 .WillOnce(Return(ReadyState::CLOSED)); | 145 .WillOnce(Return(ReadyState::CLOSED)); |
146 } | 146 } |
147 } | 147 } |
148 | 148 |
149 extensions::CastChannelAPI* GetApi() { | 149 extensions::CastChannelAPI* GetApi() { |
150 return extensions::CastChannelAPI::Get(profile()); | 150 return extensions::CastChannelAPI::Get(profile()); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 // always return true without actually running the test. Remove when fixed. | 281 // always return true without actually running the test. Remove when fixed. |
282 #if defined(OS_WIN) && !defined(NDEBUG) | 282 #if defined(OS_WIN) && !defined(NDEBUG) |
283 #define MAYBE_TestOpenReceiveClose DISABLED_TestOpenReceiveClose | 283 #define MAYBE_TestOpenReceiveClose DISABLED_TestOpenReceiveClose |
284 #else | 284 #else |
285 #define MAYBE_TestOpenReceiveClose TestOpenReceiveClose | 285 #define MAYBE_TestOpenReceiveClose TestOpenReceiveClose |
286 #endif | 286 #endif |
287 // Test loading extension, opening a channel, adding a listener, | 287 // Test loading extension, opening a channel, adding a listener, |
288 // writing, reading, and closing. | 288 // writing, reading, and closing. |
289 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenReceiveClose) { | 289 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenReceiveClose) { |
290 SetUpMockCastSocket(); | 290 SetUpMockCastSocket(); |
291 EXPECT_CALL(*mock_cast_socket_, error_state()) | 291 mock_cast_socket_->SetErrorState(ChannelError::NONE); |
292 .WillRepeatedly(Return(ChannelError::NONE)); | |
293 | 292 |
294 { | 293 { |
295 InSequence sequence; | 294 InSequence sequence; |
296 EXPECT_CALL(*mock_cast_socket_, AddObserver(_)) | 295 EXPECT_CALL(*mock_cast_socket_, AddObserver(_)) |
297 .WillOnce(SaveArg<0>(&message_observer_)); | 296 .WillOnce(SaveArg<0>(&message_observer_)); |
298 EXPECT_CALL(*mock_cast_socket_, Connect(_)) | 297 EXPECT_CALL(*mock_cast_socket_, Connect(_)) |
299 .WillOnce(InvokeCompletionCallback<0>(ChannelError::NONE)); | 298 .WillOnce( |
| 299 WithArgs<0>(Invoke([&](const CastSocket::OnOpenCallback& callback) { |
| 300 callback.Run(mock_cast_socket_->id(), ChannelError::NONE); |
| 301 }))); |
300 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 302 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
301 .Times(3) | 303 .Times(3) |
302 .WillRepeatedly(Return(ReadyState::OPEN)); | 304 .WillRepeatedly(Return(ReadyState::OPEN)); |
303 EXPECT_CALL(*mock_cast_socket_, Close(_)) | 305 EXPECT_CALL(*mock_cast_socket_, Close(_)) |
304 .WillOnce(InvokeCompletionCallback<0>(net::OK)); | 306 .WillOnce(InvokeCompletionCallback<0>(net::OK)); |
305 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 307 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
306 .WillOnce(Return(ReadyState::CLOSED)); | 308 .WillOnce(Return(ReadyState::CLOSED)); |
307 } | 309 } |
308 | 310 |
309 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", | 311 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", |
(...skipping 13 matching lines...) Expand all Loading... |
323 #define MAYBE_TestOpenError TestOpenError | 325 #define MAYBE_TestOpenError TestOpenError |
324 #endif | 326 #endif |
325 // Test the case when socket open results in an error. | 327 // Test the case when socket open results in an error. |
326 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenError) { | 328 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenError) { |
327 SetUpMockCastSocket(); | 329 SetUpMockCastSocket(); |
328 | 330 |
329 EXPECT_CALL(*mock_cast_socket_, AddObserver(_)) | 331 EXPECT_CALL(*mock_cast_socket_, AddObserver(_)) |
330 .WillOnce(DoAll(SaveArg<0>(&message_observer_), | 332 .WillOnce(DoAll(SaveArg<0>(&message_observer_), |
331 InvokeObserverOnError(this, GetApi()))); | 333 InvokeObserverOnError(this, GetApi()))); |
332 EXPECT_CALL(*mock_cast_socket_, Connect(_)) | 334 EXPECT_CALL(*mock_cast_socket_, Connect(_)) |
333 .WillOnce(InvokeCompletionCallback<0>(ChannelError::CONNECT_ERROR)); | 335 .WillOnce( |
334 EXPECT_CALL(*mock_cast_socket_, error_state()) | 336 WithArgs<0>(Invoke([&](const CastSocket::OnOpenCallback& callback) { |
335 .WillRepeatedly(Return(ChannelError::CONNECT_ERROR)); | 337 callback.Run(mock_cast_socket_->id(), ChannelError::CONNECT_ERROR); |
| 338 }))); |
| 339 mock_cast_socket_->SetErrorState(ChannelError::CONNECT_ERROR); |
336 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 340 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
337 .WillRepeatedly(Return(ReadyState::CLOSED)); | 341 .WillRepeatedly(Return(ReadyState::CLOSED)); |
338 EXPECT_CALL(*mock_cast_socket_, Close(_)) | 342 EXPECT_CALL(*mock_cast_socket_, Close(_)) |
339 .WillOnce(InvokeCompletionCallback<0>(net::OK)); | 343 .WillOnce(InvokeCompletionCallback<0>(net::OK)); |
340 | 344 |
341 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", | 345 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", |
342 "test_open_error.html")); | 346 "test_open_error.html")); |
343 } | 347 } |
344 | 348 |
345 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, TestOpenInvalidConnectInfo) { | 349 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, TestOpenInvalidConnectInfo) { |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 "\"keepAlive\": true, " | 428 "\"keepAlive\": true, " |
425 "\"audioOnly\": false, " | 429 "\"audioOnly\": false, " |
426 "\"connectInfo\": " | 430 "\"connectInfo\": " |
427 "{\"ipAddress\": \"127.0.0.1\", \"port\": 8009, " | 431 "{\"ipAddress\": \"127.0.0.1\", \"port\": 8009, " |
428 "\"auth\": \"ssl_verified\"}, \"readyState\": \"open\"}, " | 432 "\"auth\": \"ssl_verified\"}, \"readyState\": \"open\"}, " |
429 "{\"namespace_\": \"foo\", \"sourceId\": \"src\", " | 433 "{\"namespace_\": \"foo\", \"sourceId\": \"src\", " |
430 "\"destinationId\": \"\", \"data\": \"data\"}]", | 434 "\"destinationId\": \"\", \"data\": \"data\"}]", |
431 browser()); | 435 browser()); |
432 EXPECT_EQ(error, "message_info.destination_id is required"); | 436 EXPECT_EQ(error, "message_info.destination_id is required"); |
433 } | 437 } |
OLD | NEW |