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" |
11 #include "chrome/browser/extensions/extension_apitest.h" | 11 #include "chrome/browser/extensions/extension_apitest.h" |
12 #include "chrome/browser/extensions/extension_function_test_utils.h" | 12 #include "chrome/browser/extensions/extension_function_test_utils.h" |
13 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
15 #include "components/cast_channel/cast_socket.h" | 15 #include "components/cast_channel/cast_socket.h" |
| 16 #include "components/cast_channel/cast_socket_service.h" |
| 17 #include "components/cast_channel/cast_socket_service_factory.h" |
16 #include "components/cast_channel/cast_test_util.h" | 18 #include "components/cast_channel/cast_test_util.h" |
17 #include "components/cast_channel/logger.h" | 19 #include "components/cast_channel/logger.h" |
18 #include "components/cast_channel/proto/cast_channel.pb.h" | 20 #include "components/cast_channel/proto/cast_channel.pb.h" |
19 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
20 #include "extensions/browser/api/cast_channel/cast_channel_api.h" | 22 #include "extensions/browser/api/cast_channel/cast_channel_api.h" |
21 #include "extensions/common/api/cast_channel.h" | 23 #include "extensions/common/api/cast_channel.h" |
22 #include "extensions/common/switches.h" | 24 #include "extensions/common/switches.h" |
23 #include "extensions/common/test_util.h" | 25 #include "extensions/common/test_util.h" |
24 #include "extensions/test/extension_test_message_listener.h" | 26 #include "extensions/test/extension_test_message_listener.h" |
25 #include "extensions/test/result_catcher.h" | 27 #include "extensions/test/result_catcher.h" |
(...skipping 23 matching lines...) Expand all Loading... |
49 using ::testing::_; | 51 using ::testing::_; |
50 using ::testing::A; | 52 using ::testing::A; |
51 using ::testing::DoAll; | 53 using ::testing::DoAll; |
52 using ::testing::Invoke; | 54 using ::testing::Invoke; |
53 using ::testing::InSequence; | 55 using ::testing::InSequence; |
54 using ::testing::NotNull; | 56 using ::testing::NotNull; |
55 using ::testing::Return; | 57 using ::testing::Return; |
56 using ::testing::ReturnRef; | 58 using ::testing::ReturnRef; |
57 using ::testing::ReturnPointee; | 59 using ::testing::ReturnPointee; |
58 using ::testing::SaveArg; | 60 using ::testing::SaveArg; |
| 61 using ::testing::WithArgs; |
59 | 62 |
60 namespace { | 63 namespace { |
61 | 64 |
62 const char kTestExtensionId[] = "ddchlicdkolnonkihahngkmmmjnjlkkf"; | 65 const char kTestExtensionId[] = "ddchlicdkolnonkihahngkmmmjnjlkkf"; |
63 | 66 |
64 static void FillCastMessage(const std::string& message, | 67 static void FillCastMessage(const std::string& message, |
65 CastMessage* cast_message) { | 68 CastMessage* cast_message) { |
66 cast_message->set_namespace_("foo"); | 69 cast_message->set_namespace_("foo"); |
67 cast_message->set_source_id("src"); | 70 cast_message->set_source_id("src"); |
68 cast_message->set_destination_id("dest"); | 71 cast_message->set_destination_id("dest"); |
(...skipping 16 matching lines...) Expand all Loading... |
85 void SetUpCommandLine(base::CommandLine* command_line) override { | 88 void SetUpCommandLine(base::CommandLine* command_line) override { |
86 ExtensionApiTest::SetUpCommandLine(command_line); | 89 ExtensionApiTest::SetUpCommandLine(command_line); |
87 command_line->AppendSwitchASCII( | 90 command_line->AppendSwitchASCII( |
88 extensions::switches::kWhitelistedExtensionID, kTestExtensionId); | 91 extensions::switches::kWhitelistedExtensionID, kTestExtensionId); |
89 } | 92 } |
90 | 93 |
91 void SetUpMockCastSocket() { | 94 void SetUpMockCastSocket() { |
92 extensions::CastChannelAPI* api = GetApi(); | 95 extensions::CastChannelAPI* api = GetApi(); |
93 | 96 |
94 net::IPEndPoint ip_endpoint(net::IPAddress(192, 168, 1, 1), 8009); | 97 net::IPEndPoint ip_endpoint(net::IPAddress(192, 168, 1, 1), 8009); |
95 mock_cast_socket_ = new MockCastSocket; | 98 mock_cast_socket_ = new MockCastSocket(); |
| 99 mock_cast_socket_->SetIPEndpoint(ip_endpoint_); |
| 100 mock_cast_socket_->SetKeepAlive(false); |
96 // Transfers ownership of the socket. | 101 // Transfers ownership of the socket. |
97 api->SetSocketForTest(base::WrapUnique<CastSocket>(mock_cast_socket_)); | 102 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 } | 103 } |
106 | 104 |
107 void SetUpOpenSendClose() { | 105 void SetUpOpenSendClose() { |
108 SetUpMockCastSocket(); | 106 SetUpMockCastSocket(); |
109 EXPECT_CALL(*mock_cast_socket_, error_state()) | 107 mock_cast_socket_->SetErrorState(ChannelError::NONE); |
110 .WillRepeatedly(Return(ChannelError::NONE)); | |
111 { | 108 { |
112 InSequence sequence; | 109 InSequence sequence; |
113 | 110 |
| 111 EXPECT_CALL(*mock_cast_socket_, AddObserver(_)); |
114 EXPECT_CALL(*mock_cast_socket_, Connect(_)) | 112 EXPECT_CALL(*mock_cast_socket_, Connect(_)) |
115 .WillOnce(InvokeCompletionCallback<0>(ChannelError::NONE)); | 113 .WillOnce(WithArgs<0>( |
| 114 Invoke([&](const CastSocket::OnOpenCallback& callback) { |
| 115 callback.Run(mock_cast_socket_->id(), ChannelError::NONE); |
| 116 }))); |
116 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 117 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
117 .WillOnce(Return(ReadyState::OPEN)); | 118 .WillOnce(Return(ReadyState::OPEN)); |
118 EXPECT_CALL(*mock_cast_socket_->mock_transport(), | 119 EXPECT_CALL(*mock_cast_socket_->mock_transport(), |
119 SendMessage(A<const CastMessage&>(), _)) | 120 SendMessage(A<const CastMessage&>(), _)) |
120 .WillOnce(InvokeCompletionCallback<1>(net::OK)); | 121 .WillOnce(InvokeCompletionCallback<1>(net::OK)); |
121 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 122 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
122 .WillOnce(Return(ReadyState::OPEN)); | 123 .WillOnce(Return(ReadyState::OPEN)); |
123 EXPECT_CALL(*mock_cast_socket_, Close(_)) | 124 EXPECT_CALL(*mock_cast_socket_, Close(_)) |
124 .WillOnce(InvokeCompletionCallback<0>(net::OK)); | 125 .WillOnce(InvokeCompletionCallback<0>(net::OK)); |
125 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 126 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
126 .WillOnce(Return(ReadyState::CLOSED)); | 127 .WillOnce(Return(ReadyState::CLOSED)); |
127 } | 128 } |
128 } | 129 } |
129 | 130 |
130 void SetUpOpenPingTimeout() { | 131 void SetUpOpenPingTimeout() { |
131 SetUpMockCastSocket(); | 132 SetUpMockCastSocket(); |
132 EXPECT_CALL(*mock_cast_socket_, error_state()) | 133 mock_cast_socket_->SetErrorState(ChannelError::NONE); |
133 .WillRepeatedly(Return(ChannelError::NONE)); | 134 mock_cast_socket_->SetKeepAlive(true); |
134 EXPECT_CALL(*mock_cast_socket_, keep_alive()).WillRepeatedly(Return(true)); | |
135 { | 135 { |
136 InSequence sequence; | 136 InSequence sequence; |
137 EXPECT_CALL(*mock_cast_socket_, AddObserver(_)) | 137 EXPECT_CALL(*mock_cast_socket_, AddObserver(_)) |
138 .WillOnce(SaveArg<0>(&message_observer_)); | 138 .WillOnce(SaveArg<0>(&message_observer_)); |
139 EXPECT_CALL(*mock_cast_socket_, Connect(_)) | 139 EXPECT_CALL(*mock_cast_socket_, Connect(_)) |
140 .WillOnce(InvokeCompletionCallback<0>(ChannelError::NONE)); | 140 .WillOnce(WithArgs<0>( |
| 141 Invoke([&](const CastSocket::OnOpenCallback& callback) { |
| 142 callback.Run(mock_cast_socket_->id(), ChannelError::NONE); |
| 143 }))); |
141 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 144 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
142 .WillOnce(Return(ReadyState::OPEN)) | 145 .WillOnce(Return(ReadyState::OPEN)) |
143 .RetiresOnSaturation(); | 146 .RetiresOnSaturation(); |
144 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 147 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
145 .WillOnce(Return(ReadyState::CLOSED)); | 148 .WillOnce(Return(ReadyState::CLOSED)); |
146 } | 149 } |
147 } | 150 } |
148 | 151 |
149 extensions::CastChannelAPI* GetApi() { | 152 extensions::CastChannelAPI* GetApi() { |
150 return extensions::CastChannelAPI::Get(profile()); | 153 return extensions::CastChannelAPI::Get(profile()); |
151 } | 154 } |
152 | 155 |
| 156 cast_channel::CastSocketService* GetCastSocketService() { |
| 157 return cast_channel::CastSocketServiceFactory::GetForBrowserContext( |
| 158 profile()) |
| 159 .get(); |
| 160 } |
| 161 |
153 // Logs some bogus error details and calls the OnError handler. | 162 // Logs some bogus error details and calls the OnError handler. |
154 void DoCallOnError(extensions::CastChannelAPI* api) { | 163 void DoCallOnError(cast_channel::CastSocketService* cast_socket_service) { |
155 api->GetLogger()->LogSocketEventWithRv( | 164 cast_socket_service->GetLogger()->LogSocketEventWithRv( |
156 mock_cast_socket_->id(), ::cast_channel::ChannelEvent::SOCKET_WRITE, | 165 mock_cast_socket_->id(), cast_channel::ChannelEvent::SOCKET_WRITE, |
157 net::ERR_FAILED); | 166 net::ERR_FAILED); |
158 message_observer_->OnError(*mock_cast_socket_, ChannelError::CONNECT_ERROR); | 167 message_observer_->OnError(*mock_cast_socket_, ChannelError::CONNECT_ERROR); |
159 } | 168 } |
160 | 169 |
161 protected: | 170 protected: |
162 void CallOnMessage(const std::string& message) { | 171 void CallOnMessage(const std::string& message) { |
163 content::BrowserThread::PostTask( | 172 content::BrowserThread::PostTask( |
164 content::BrowserThread::IO, FROM_HERE, | 173 content::BrowserThread::IO, FROM_HERE, |
165 base::Bind(&CastChannelAPITest::DoCallOnMessage, base::Unretained(this), | 174 base::Bind(&CastChannelAPITest::DoCallOnMessage, base::Unretained(this), |
166 GetApi(), mock_cast_socket_, message)); | 175 GetApi(), mock_cast_socket_, message)); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 } | 213 } |
205 | 214 |
206 MockCastSocket* mock_cast_socket_; | 215 MockCastSocket* mock_cast_socket_; |
207 net::IPEndPoint ip_endpoint_; | 216 net::IPEndPoint ip_endpoint_; |
208 LastError last_error_; | 217 LastError last_error_; |
209 CastSocket::Observer* message_observer_; | 218 CastSocket::Observer* message_observer_; |
210 net::TestNetLog capturing_net_log_; | 219 net::TestNetLog capturing_net_log_; |
211 int channel_id_; | 220 int channel_id_; |
212 }; | 221 }; |
213 | 222 |
214 ACTION_P2(InvokeObserverOnError, api_test, api) { | 223 ACTION_P2(InvokeObserverOnError, api_test, cast_socket_service) { |
215 content::BrowserThread::PostTask( | 224 content::BrowserThread::PostTask( |
216 content::BrowserThread::IO, FROM_HERE, | 225 content::BrowserThread::IO, FROM_HERE, |
217 base::Bind(&CastChannelAPITest::DoCallOnError, base::Unretained(api_test), | 226 base::Bind(&CastChannelAPITest::DoCallOnError, base::Unretained(api_test), |
218 base::Unretained(api))); | 227 cast_socket_service)); |
219 } | 228 } |
220 | 229 |
221 // TODO(kmarshall): Win Dbg has a workaround that makes RunExtensionSubtest | 230 // TODO(kmarshall): Win Dbg has a workaround that makes RunExtensionSubtest |
222 // always return true without actually running the test. Remove when fixed. | 231 // always return true without actually running the test. Remove when fixed. |
223 #if defined(OS_WIN) && !defined(NDEBUG) | 232 #if defined(OS_WIN) && !defined(NDEBUG) |
224 #define MAYBE_TestOpenSendClose DISABLED_TestOpenSendClose | 233 #define MAYBE_TestOpenSendClose DISABLED_TestOpenSendClose |
225 #else | 234 #else |
226 #define MAYBE_TestOpenSendClose TestOpenSendClose | 235 #define MAYBE_TestOpenSendClose TestOpenSendClose |
227 #endif | 236 #endif |
228 // Test loading extension, opening a channel with ConnectInfo, adding a | 237 // Test loading extension, opening a channel with ConnectInfo, adding a |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 // always return true without actually running the test. Remove when fixed. | 290 // always return true without actually running the test. Remove when fixed. |
282 #if defined(OS_WIN) && !defined(NDEBUG) | 291 #if defined(OS_WIN) && !defined(NDEBUG) |
283 #define MAYBE_TestOpenReceiveClose DISABLED_TestOpenReceiveClose | 292 #define MAYBE_TestOpenReceiveClose DISABLED_TestOpenReceiveClose |
284 #else | 293 #else |
285 #define MAYBE_TestOpenReceiveClose TestOpenReceiveClose | 294 #define MAYBE_TestOpenReceiveClose TestOpenReceiveClose |
286 #endif | 295 #endif |
287 // Test loading extension, opening a channel, adding a listener, | 296 // Test loading extension, opening a channel, adding a listener, |
288 // writing, reading, and closing. | 297 // writing, reading, and closing. |
289 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenReceiveClose) { | 298 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenReceiveClose) { |
290 SetUpMockCastSocket(); | 299 SetUpMockCastSocket(); |
291 EXPECT_CALL(*mock_cast_socket_, error_state()) | 300 mock_cast_socket_->SetErrorState(ChannelError::NONE); |
292 .WillRepeatedly(Return(ChannelError::NONE)); | |
293 | 301 |
294 { | 302 { |
295 InSequence sequence; | 303 InSequence sequence; |
296 EXPECT_CALL(*mock_cast_socket_, AddObserver(_)) | 304 EXPECT_CALL(*mock_cast_socket_, AddObserver(_)) |
297 .WillOnce(SaveArg<0>(&message_observer_)); | 305 .WillOnce(SaveArg<0>(&message_observer_)); |
298 EXPECT_CALL(*mock_cast_socket_, Connect(_)) | 306 EXPECT_CALL(*mock_cast_socket_, Connect(_)) |
299 .WillOnce(InvokeCompletionCallback<0>(ChannelError::NONE)); | 307 .WillOnce( |
| 308 WithArgs<0>(Invoke([&](const CastSocket::OnOpenCallback& callback) { |
| 309 callback.Run(mock_cast_socket_->id(), ChannelError::NONE); |
| 310 }))); |
300 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 311 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
301 .Times(3) | 312 .Times(3) |
302 .WillRepeatedly(Return(ReadyState::OPEN)); | 313 .WillRepeatedly(Return(ReadyState::OPEN)); |
303 EXPECT_CALL(*mock_cast_socket_, Close(_)) | 314 EXPECT_CALL(*mock_cast_socket_, Close(_)) |
304 .WillOnce(InvokeCompletionCallback<0>(net::OK)); | 315 .WillOnce(InvokeCompletionCallback<0>(net::OK)); |
305 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 316 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
306 .WillOnce(Return(ReadyState::CLOSED)); | 317 .WillOnce(Return(ReadyState::CLOSED)); |
307 } | 318 } |
308 | 319 |
309 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", | 320 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", |
(...skipping 11 matching lines...) Expand all Loading... |
321 #define MAYBE_TestOpenError DISABLED_TestOpenError | 332 #define MAYBE_TestOpenError DISABLED_TestOpenError |
322 #else | 333 #else |
323 #define MAYBE_TestOpenError TestOpenError | 334 #define MAYBE_TestOpenError TestOpenError |
324 #endif | 335 #endif |
325 // Test the case when socket open results in an error. | 336 // Test the case when socket open results in an error. |
326 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenError) { | 337 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenError) { |
327 SetUpMockCastSocket(); | 338 SetUpMockCastSocket(); |
328 | 339 |
329 EXPECT_CALL(*mock_cast_socket_, AddObserver(_)) | 340 EXPECT_CALL(*mock_cast_socket_, AddObserver(_)) |
330 .WillOnce(DoAll(SaveArg<0>(&message_observer_), | 341 .WillOnce(DoAll(SaveArg<0>(&message_observer_), |
331 InvokeObserverOnError(this, GetApi()))); | 342 InvokeObserverOnError(this, GetCastSocketService()))); |
332 EXPECT_CALL(*mock_cast_socket_, Connect(_)) | 343 EXPECT_CALL(*mock_cast_socket_, Connect(_)) |
333 .WillOnce(InvokeCompletionCallback<0>(ChannelError::CONNECT_ERROR)); | 344 .WillOnce( |
334 EXPECT_CALL(*mock_cast_socket_, error_state()) | 345 WithArgs<0>(Invoke([&](const CastSocket::OnOpenCallback& callback) { |
335 .WillRepeatedly(Return(ChannelError::CONNECT_ERROR)); | 346 callback.Run(mock_cast_socket_->id(), ChannelError::CONNECT_ERROR); |
| 347 }))); |
| 348 mock_cast_socket_->SetErrorState(ChannelError::CONNECT_ERROR); |
336 EXPECT_CALL(*mock_cast_socket_, ready_state()) | 349 EXPECT_CALL(*mock_cast_socket_, ready_state()) |
337 .WillRepeatedly(Return(ReadyState::CLOSED)); | 350 .WillRepeatedly(Return(ReadyState::CLOSED)); |
338 EXPECT_CALL(*mock_cast_socket_, Close(_)) | 351 EXPECT_CALL(*mock_cast_socket_, Close(_)) |
339 .WillOnce(InvokeCompletionCallback<0>(net::OK)); | 352 .WillOnce(InvokeCompletionCallback<0>(net::OK)); |
340 | 353 |
341 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", | 354 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", |
342 "test_open_error.html")); | 355 "test_open_error.html")); |
343 } | 356 } |
344 | 357 |
345 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, TestOpenInvalidConnectInfo) { | 358 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, TestOpenInvalidConnectInfo) { |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 "\"keepAlive\": true, " | 437 "\"keepAlive\": true, " |
425 "\"audioOnly\": false, " | 438 "\"audioOnly\": false, " |
426 "\"connectInfo\": " | 439 "\"connectInfo\": " |
427 "{\"ipAddress\": \"127.0.0.1\", \"port\": 8009, " | 440 "{\"ipAddress\": \"127.0.0.1\", \"port\": 8009, " |
428 "\"auth\": \"ssl_verified\"}, \"readyState\": \"open\"}, " | 441 "\"auth\": \"ssl_verified\"}, \"readyState\": \"open\"}, " |
429 "{\"namespace_\": \"foo\", \"sourceId\": \"src\", " | 442 "{\"namespace_\": \"foo\", \"sourceId\": \"src\", " |
430 "\"destinationId\": \"\", \"data\": \"data\"}]", | 443 "\"destinationId\": \"\", \"data\": \"data\"}]", |
431 browser()); | 444 browser()); |
432 EXPECT_EQ(error, "message_info.destination_id is required"); | 445 EXPECT_EQ(error, "message_info.destination_id is required"); |
433 } | 446 } |
OLD | NEW |