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

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

Issue 2942993003: [cast_channel] Make CastMessageHandler a CastSocket::Observer instead of CastTransport::Delegate (Closed)
Patch Set: 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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 .WillByDefault(ReturnRef(ip_endpoint_)); 103 .WillByDefault(ReturnRef(ip_endpoint_));
104 ON_CALL(*mock_cast_socket_, keep_alive()).WillByDefault(Return(false)); 104 ON_CALL(*mock_cast_socket_, keep_alive()).WillByDefault(Return(false));
105 } 105 }
106 106
107 void SetUpOpenSendClose() { 107 void SetUpOpenSendClose() {
108 SetUpMockCastSocket(); 108 SetUpMockCastSocket();
109 EXPECT_CALL(*mock_cast_socket_, error_state()) 109 EXPECT_CALL(*mock_cast_socket_, error_state())
110 .WillRepeatedly(Return(ChannelError::NONE)); 110 .WillRepeatedly(Return(ChannelError::NONE));
111 { 111 {
112 InSequence sequence; 112 InSequence sequence;
113 EXPECT_CALL(*mock_cast_socket_, ConnectRawPtr(_, _)) 113
114 .WillOnce(InvokeCompletionCallback<1>(ChannelError::NONE)); 114 EXPECT_CALL(*mock_cast_socket_, Connect(_))
115 .WillOnce(InvokeCompletionCallback<0>(ChannelError::NONE));
115 EXPECT_CALL(*mock_cast_socket_, ready_state()) 116 EXPECT_CALL(*mock_cast_socket_, ready_state())
116 .WillOnce(Return(ReadyState::OPEN)); 117 .WillOnce(Return(ReadyState::OPEN));
117 EXPECT_CALL(*mock_cast_socket_->mock_transport(), 118 EXPECT_CALL(*mock_cast_socket_->mock_transport(),
118 SendMessage(A<const CastMessage&>(), _)) 119 SendMessage(A<const CastMessage&>(), _))
119 .WillOnce(InvokeCompletionCallback<1>(net::OK)); 120 .WillOnce(InvokeCompletionCallback<1>(net::OK));
120 EXPECT_CALL(*mock_cast_socket_, ready_state()) 121 EXPECT_CALL(*mock_cast_socket_, ready_state())
121 .WillOnce(Return(ReadyState::OPEN)); 122 .WillOnce(Return(ReadyState::OPEN));
122 EXPECT_CALL(*mock_cast_socket_, Close(_)) 123 EXPECT_CALL(*mock_cast_socket_, Close(_))
123 .WillOnce(InvokeCompletionCallback<0>(net::OK)); 124 .WillOnce(InvokeCompletionCallback<0>(net::OK));
124 EXPECT_CALL(*mock_cast_socket_, ready_state()) 125 EXPECT_CALL(*mock_cast_socket_, ready_state())
125 .WillOnce(Return(ReadyState::CLOSED)); 126 .WillOnce(Return(ReadyState::CLOSED));
126 } 127 }
127 } 128 }
128 129
129 void SetUpOpenPingTimeout() { 130 void SetUpOpenPingTimeout() {
130 SetUpMockCastSocket(); 131 SetUpMockCastSocket();
131 EXPECT_CALL(*mock_cast_socket_, error_state()) 132 EXPECT_CALL(*mock_cast_socket_, error_state())
132 .WillRepeatedly(Return(ChannelError::NONE)); 133 .WillRepeatedly(Return(ChannelError::NONE));
133 EXPECT_CALL(*mock_cast_socket_, keep_alive()).WillRepeatedly(Return(true)); 134 EXPECT_CALL(*mock_cast_socket_, keep_alive()).WillRepeatedly(Return(true));
134 { 135 {
135 InSequence sequence; 136 InSequence sequence;
136 EXPECT_CALL(*mock_cast_socket_, ConnectRawPtr(_, _)) 137 EXPECT_CALL(*mock_cast_socket_, AddObserverInternal(_, _))
137 .WillOnce(DoAll(SaveArg<0>(&message_delegate_), 138 .WillOnce(SaveArg<1>(&message_observer_));
138 InvokeCompletionCallback<1>(ChannelError::NONE))); 139 EXPECT_CALL(*mock_cast_socket_, Connect(_))
140 .WillOnce(InvokeCompletionCallback<0>(ChannelError::NONE));
139 EXPECT_CALL(*mock_cast_socket_, ready_state()) 141 EXPECT_CALL(*mock_cast_socket_, ready_state())
140 .WillOnce(Return(ReadyState::OPEN)) 142 .WillOnce(Return(ReadyState::OPEN))
141 .RetiresOnSaturation(); 143 .RetiresOnSaturation();
142 EXPECT_CALL(*mock_cast_socket_, ready_state()) 144 EXPECT_CALL(*mock_cast_socket_, ready_state())
143 .WillOnce(Return(ReadyState::CLOSED)); 145 .WillOnce(Return(ReadyState::CLOSED));
144 } 146 }
145 } 147 }
146 148
147 extensions::CastChannelAPI* GetApi() { 149 extensions::CastChannelAPI* GetApi() {
148 return extensions::CastChannelAPI::Get(profile()); 150 return extensions::CastChannelAPI::Get(profile());
149 } 151 }
150 152
151 // Logs some bogus error details and calls the OnError handler. 153 // Logs some bogus error details and calls the OnError handler.
152 void DoCallOnError(extensions::CastChannelAPI* api) { 154 void DoCallOnError(extensions::CastChannelAPI* api) {
153 api->GetLogger()->LogSocketEventWithRv( 155 api->GetLogger()->LogSocketEventWithRv(
154 mock_cast_socket_->id(), ::cast_channel::ChannelEvent::SOCKET_WRITE, 156 mock_cast_socket_->id(), ::cast_channel::ChannelEvent::SOCKET_WRITE,
155 net::ERR_FAILED); 157 net::ERR_FAILED);
156 message_delegate_->OnError(ChannelError::CONNECT_ERROR); 158 message_observer_->OnError(*mock_cast_socket_, ChannelError::CONNECT_ERROR);
157 } 159 }
158 160
159 protected: 161 protected:
160 void CallOnMessage(const std::string& message) { 162 void CallOnMessage(const std::string& message) {
161 content::BrowserThread::PostTask( 163 content::BrowserThread::PostTask(
162 content::BrowserThread::IO, FROM_HERE, 164 content::BrowserThread::IO, FROM_HERE,
163 base::Bind(&CastChannelAPITest::DoCallOnMessage, base::Unretained(this), 165 base::Bind(&CastChannelAPITest::DoCallOnMessage, base::Unretained(this),
164 GetApi(), mock_cast_socket_, message)); 166 GetApi(), mock_cast_socket_, message));
165 } 167 }
166 168
167 void DoCallOnMessage(extensions::CastChannelAPI* api, 169 void DoCallOnMessage(extensions::CastChannelAPI* api,
168 MockCastSocket* cast_socket, 170 MockCastSocket* cast_socket,
169 const std::string& message) { 171 const std::string& message) {
170 CastMessage cast_message; 172 CastMessage cast_message;
171 FillCastMessage(message, &cast_message); 173 FillCastMessage(message, &cast_message);
172 message_delegate_->OnMessage(cast_message); 174 message_observer_->OnMessage(*cast_socket, cast_message);
173 }
174
175 // Starts the read delegate on the IO thread.
176 void StartDelegate() {
177 CHECK(message_delegate_);
178 content::BrowserThread::PostTask(
179 content::BrowserThread::IO, FROM_HERE,
180 base::Bind(&CastTransport::Delegate::Start,
181 base::Unretained(message_delegate_)));
182 } 175 }
183 176
184 // Fires a timer on the IO thread. 177 // Fires a timer on the IO thread.
185 void FireTimeout() { 178 void FireTimeout() {
186 content::BrowserThread::PostTask( 179 content::BrowserThread::PostTask(
187 content::BrowserThread::IO, FROM_HERE, 180 content::BrowserThread::IO, FROM_HERE,
188 base::Bind(&CastTransport::Delegate::OnError, 181 base::Bind(&CastChannelAPITest::DoFireTimeout, base::Unretained(this),
189 base::Unretained(message_delegate_), 182 mock_cast_socket_));
190 cast_channel::ChannelError::PING_TIMEOUT)); 183 }
184
185 void DoFireTimeout(MockCastSocket* cast_socket) {
186 message_observer_->OnError(*cast_socket,
187 cast_channel::ChannelError::PING_TIMEOUT);
191 } 188 }
192 189
193 extensions::CastChannelOpenFunction* CreateOpenFunction( 190 extensions::CastChannelOpenFunction* CreateOpenFunction(
194 scoped_refptr<Extension> extension) { 191 scoped_refptr<Extension> extension) {
195 extensions::CastChannelOpenFunction* cast_channel_open_function = 192 extensions::CastChannelOpenFunction* cast_channel_open_function =
196 new extensions::CastChannelOpenFunction; 193 new extensions::CastChannelOpenFunction;
197 cast_channel_open_function->set_extension(extension.get()); 194 cast_channel_open_function->set_extension(extension.get());
198 return cast_channel_open_function; 195 return cast_channel_open_function;
199 } 196 }
200 197
201 extensions::CastChannelSendFunction* CreateSendFunction( 198 extensions::CastChannelSendFunction* CreateSendFunction(
202 scoped_refptr<Extension> extension) { 199 scoped_refptr<Extension> extension) {
203 extensions::CastChannelSendFunction* cast_channel_send_function = 200 extensions::CastChannelSendFunction* cast_channel_send_function =
204 new extensions::CastChannelSendFunction; 201 new extensions::CastChannelSendFunction;
205 cast_channel_send_function->set_extension(extension.get()); 202 cast_channel_send_function->set_extension(extension.get());
206 return cast_channel_send_function; 203 return cast_channel_send_function;
207 } 204 }
208 205
209 MockCastSocket* mock_cast_socket_; 206 MockCastSocket* mock_cast_socket_;
210 net::IPEndPoint ip_endpoint_; 207 net::IPEndPoint ip_endpoint_;
211 LastError last_error_; 208 LastError last_error_;
212 CastTransport::Delegate* message_delegate_; 209 CastSocket::Observer* message_observer_;
213 net::TestNetLog capturing_net_log_; 210 net::TestNetLog capturing_net_log_;
214 int channel_id_; 211 int channel_id_;
215 }; 212 };
216 213
217 ACTION_P2(InvokeDelegateOnError, api_test, api) { 214 ACTION_P2(InvokeObserverOnError, api_test, api) {
218 content::BrowserThread::PostTask( 215 content::BrowserThread::PostTask(
219 content::BrowserThread::IO, FROM_HERE, 216 content::BrowserThread::IO, FROM_HERE,
220 base::Bind(&CastChannelAPITest::DoCallOnError, base::Unretained(api_test), 217 base::Bind(&CastChannelAPITest::DoCallOnError, base::Unretained(api_test),
221 base::Unretained(api))); 218 base::Unretained(api)));
222 } 219 }
223 220
224 // TODO(kmarshall): Win Dbg has a workaround that makes RunExtensionSubtest 221 // TODO(kmarshall): Win Dbg has a workaround that makes RunExtensionSubtest
225 // always return true without actually running the test. Remove when fixed. 222 // always return true without actually running the test. Remove when fixed.
226 #if defined(OS_WIN) && !defined(NDEBUG) 223 #if defined(OS_WIN) && !defined(NDEBUG)
227 #define MAYBE_TestOpenSendClose DISABLED_TestOpenSendClose 224 #define MAYBE_TestOpenSendClose DISABLED_TestOpenSendClose
(...skipping 19 matching lines...) Expand all
247 // Verify that timeout events are propagated through the API layer. 244 // Verify that timeout events are propagated through the API layer.
248 // (SSL, non-verified). 245 // (SSL, non-verified).
249 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestPingTimeout) { 246 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestPingTimeout) {
250 SetUpOpenPingTimeout(); 247 SetUpOpenPingTimeout();
251 248
252 ExtensionTestMessageListener channel_opened("channel_opened_ssl", false); 249 ExtensionTestMessageListener channel_opened("channel_opened_ssl", false);
253 ExtensionTestMessageListener timeout("timeout_ssl", false); 250 ExtensionTestMessageListener timeout("timeout_ssl", false);
254 EXPECT_TRUE( 251 EXPECT_TRUE(
255 RunExtensionSubtest("cast_channel/api", "test_open_timeout.html")); 252 RunExtensionSubtest("cast_channel/api", "test_open_timeout.html"));
256 EXPECT_TRUE(channel_opened.WaitUntilSatisfied()); 253 EXPECT_TRUE(channel_opened.WaitUntilSatisfied());
257 StartDelegate();
258 FireTimeout(); 254 FireTimeout();
259 EXPECT_TRUE(timeout.WaitUntilSatisfied()); 255 EXPECT_TRUE(timeout.WaitUntilSatisfied());
260 } 256 }
261 257
262 // TODO(kmarshall): Win Dbg has a workaround that makes RunExtensionSubtest 258 // TODO(kmarshall): Win Dbg has a workaround that makes RunExtensionSubtest
263 // always return true without actually running the test. Remove when fixed. 259 // always return true without actually running the test. Remove when fixed.
264 #if defined(OS_WIN) && !defined(NDEBUG) 260 #if defined(OS_WIN) && !defined(NDEBUG)
265 #define MAYBE_TestPingTimeoutSslVerified DISABLED_TestPingTimeoutSslVerified 261 #define MAYBE_TestPingTimeoutSslVerified DISABLED_TestPingTimeoutSslVerified
266 #else 262 #else
267 #define MAYBE_TestPingTimeoutSslVerified TestPingTimeoutSslVerified 263 #define MAYBE_TestPingTimeoutSslVerified TestPingTimeoutSslVerified
268 #endif 264 #endif
269 // Verify that timeout events are propagated through the API layer. 265 // Verify that timeout events are propagated through the API layer.
270 // (SSL, verified). 266 // (SSL, verified).
271 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestPingTimeoutSslVerified) { 267 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestPingTimeoutSslVerified) {
272 SetUpOpenPingTimeout(); 268 SetUpOpenPingTimeout();
273 269
274 ExtensionTestMessageListener channel_opened("channel_opened_ssl_verified", 270 ExtensionTestMessageListener channel_opened("channel_opened_ssl_verified",
275 false); 271 false);
276 ExtensionTestMessageListener timeout("timeout_ssl_verified", false); 272 ExtensionTestMessageListener timeout("timeout_ssl_verified", false);
277 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", 273 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api",
278 "test_open_timeout_verified.html")); 274 "test_open_timeout_verified.html"));
279 EXPECT_TRUE(channel_opened.WaitUntilSatisfied()); 275 EXPECT_TRUE(channel_opened.WaitUntilSatisfied());
280 StartDelegate();
281 FireTimeout(); 276 FireTimeout();
282 EXPECT_TRUE(timeout.WaitUntilSatisfied()); 277 EXPECT_TRUE(timeout.WaitUntilSatisfied());
283 } 278 }
284 279
285 // TODO(kmarshall): Win Dbg has a workaround that makes RunExtensionSubtest 280 // TODO(kmarshall): Win Dbg has a workaround that makes RunExtensionSubtest
286 // always return true without actually running the test. Remove when fixed. 281 // always return true without actually running the test. Remove when fixed.
287 #if defined(OS_WIN) && !defined(NDEBUG) 282 #if defined(OS_WIN) && !defined(NDEBUG)
288 #define MAYBE_TestOpenReceiveClose DISABLED_TestOpenReceiveClose 283 #define MAYBE_TestOpenReceiveClose DISABLED_TestOpenReceiveClose
289 #else 284 #else
290 #define MAYBE_TestOpenReceiveClose TestOpenReceiveClose 285 #define MAYBE_TestOpenReceiveClose TestOpenReceiveClose
291 #endif 286 #endif
292 // Test loading extension, opening a channel, adding a listener, 287 // Test loading extension, opening a channel, adding a listener,
293 // writing, reading, and closing. 288 // writing, reading, and closing.
294 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenReceiveClose) { 289 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenReceiveClose) {
295 SetUpMockCastSocket(); 290 SetUpMockCastSocket();
296 EXPECT_CALL(*mock_cast_socket_, error_state()) 291 EXPECT_CALL(*mock_cast_socket_, error_state())
297 .WillRepeatedly(Return(ChannelError::NONE)); 292 .WillRepeatedly(Return(ChannelError::NONE));
298 293
299 { 294 {
300 InSequence sequence; 295 InSequence sequence;
301 EXPECT_CALL(*mock_cast_socket_, ConnectRawPtr(NotNull(), _)) 296 EXPECT_CALL(*mock_cast_socket_, AddObserverInternal(_, _))
302 .WillOnce(DoAll(SaveArg<0>(&message_delegate_), 297 .WillOnce(SaveArg<1>(&message_observer_));
303 InvokeCompletionCallback<1>(ChannelError::NONE))); 298 EXPECT_CALL(*mock_cast_socket_, Connect(_))
299 .WillOnce(InvokeCompletionCallback<0>(ChannelError::NONE));
304 EXPECT_CALL(*mock_cast_socket_, ready_state()) 300 EXPECT_CALL(*mock_cast_socket_, ready_state())
305 .Times(3) 301 .Times(3)
306 .WillRepeatedly(Return(ReadyState::OPEN)); 302 .WillRepeatedly(Return(ReadyState::OPEN));
307 EXPECT_CALL(*mock_cast_socket_, Close(_)) 303 EXPECT_CALL(*mock_cast_socket_, Close(_))
308 .WillOnce(InvokeCompletionCallback<0>(net::OK)); 304 .WillOnce(InvokeCompletionCallback<0>(net::OK));
309 EXPECT_CALL(*mock_cast_socket_, ready_state()) 305 EXPECT_CALL(*mock_cast_socket_, ready_state())
310 .WillOnce(Return(ReadyState::CLOSED)); 306 .WillOnce(Return(ReadyState::CLOSED));
311 } 307 }
312 308
313 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", 309 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api",
314 "test_open_receive_close.html")); 310 "test_open_receive_close.html"));
315 311
316 extensions::ResultCatcher catcher; 312 extensions::ResultCatcher catcher;
317 CallOnMessage("some-message"); 313 CallOnMessage("some-message");
318 CallOnMessage("some-message"); 314 CallOnMessage("some-message");
319 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); 315 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
320 } 316 }
321 317
322 // TODO(kmarshall): Win Dbg has a workaround that makes RunExtensionSubtest 318 // TODO(kmarshall): Win Dbg has a workaround that makes RunExtensionSubtest
323 // always return true without actually running the test. Remove when fixed. 319 // always return true without actually running the test. Remove when fixed.
324 #if defined(OS_WIN) && !defined(NDEBUG) 320 #if defined(OS_WIN) && !defined(NDEBUG)
325 #define MAYBE_TestOpenError DISABLED_TestOpenError 321 #define MAYBE_TestOpenError DISABLED_TestOpenError
326 #else 322 #else
327 #define MAYBE_TestOpenError TestOpenError 323 #define MAYBE_TestOpenError TestOpenError
328 #endif 324 #endif
329 // Test the case when socket open results in an error. 325 // Test the case when socket open results in an error.
330 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenError) { 326 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenError) {
331 SetUpMockCastSocket(); 327 SetUpMockCastSocket();
332 328
333 EXPECT_CALL(*mock_cast_socket_, ConnectRawPtr(NotNull(), _)) 329 EXPECT_CALL(*mock_cast_socket_, AddObserverInternal(_, _))
334 .WillOnce(DoAll( 330 .WillOnce(DoAll(SaveArg<1>(&message_observer_),
335 SaveArg<0>(&message_delegate_), InvokeDelegateOnError(this, GetApi()), 331 InvokeObserverOnError(this, GetApi())));
336 InvokeCompletionCallback<1>(ChannelError::CONNECT_ERROR))); 332 EXPECT_CALL(*mock_cast_socket_, Connect(_))
333 .WillOnce(InvokeCompletionCallback<0>(ChannelError::CONNECT_ERROR));
337 EXPECT_CALL(*mock_cast_socket_, error_state()) 334 EXPECT_CALL(*mock_cast_socket_, error_state())
338 .WillRepeatedly(Return(ChannelError::CONNECT_ERROR)); 335 .WillRepeatedly(Return(ChannelError::CONNECT_ERROR));
339 EXPECT_CALL(*mock_cast_socket_, ready_state()) 336 EXPECT_CALL(*mock_cast_socket_, ready_state())
340 .WillRepeatedly(Return(ReadyState::CLOSED)); 337 .WillRepeatedly(Return(ReadyState::CLOSED));
341 EXPECT_CALL(*mock_cast_socket_, Close(_)) 338 EXPECT_CALL(*mock_cast_socket_, Close(_))
342 .WillOnce(InvokeCompletionCallback<0>(net::OK)); 339 .WillOnce(InvokeCompletionCallback<0>(net::OK));
343 340
344 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", 341 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api",
345 "test_open_error.html")); 342 "test_open_error.html"));
346 } 343 }
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 "\"keepAlive\": true, " 424 "\"keepAlive\": true, "
428 "\"audioOnly\": false, " 425 "\"audioOnly\": false, "
429 "\"connectInfo\": " 426 "\"connectInfo\": "
430 "{\"ipAddress\": \"127.0.0.1\", \"port\": 8009, " 427 "{\"ipAddress\": \"127.0.0.1\", \"port\": 8009, "
431 "\"auth\": \"ssl_verified\"}, \"readyState\": \"open\"}, " 428 "\"auth\": \"ssl_verified\"}, \"readyState\": \"open\"}, "
432 "{\"namespace_\": \"foo\", \"sourceId\": \"src\", " 429 "{\"namespace_\": \"foo\", \"sourceId\": \"src\", "
433 "\"destinationId\": \"\", \"data\": \"data\"}]", 430 "\"destinationId\": \"\", \"data\": \"data\"}]",
434 browser()); 431 browser());
435 EXPECT_EQ(error, "message_info.destination_id is required"); 432 EXPECT_EQ(error, "message_info.destination_id is required");
436 } 433 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698