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

Side by Side Diff: components/invalidation/gcm_network_channel_unittest.cc

Issue 348503002: Report InvalidatorState correctly from GCMNetworkChannel (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@GCM.ConnectionState
Patch Set: Fix SyncNetworkChannelTest Created 6 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/run_loop.h" 5 #include "base/run_loop.h"
6 #include "base/strings/string_util.h" 6 #include "base/strings/string_util.h"
7 #include "components/invalidation/gcm_network_channel.h" 7 #include "components/invalidation/gcm_network_channel.h"
8 #include "google_apis/gaia/google_service_auth_error.h" 8 #include "google_apis/gaia/google_service_auth_error.h"
9 #include "net/url_request/test_url_fetcher_factory.h" 9 #include "net/url_request/test_url_fetcher_factory.h"
10 #include "net/url_request/url_request_test_util.h" 10 #include "net/url_request/url_request_test_util.h"
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 } 243 }
244 244
245 TEST_F(GCMNetworkChannelTest, HappyCase) { 245 TEST_F(GCMNetworkChannelTest, HappyCase) {
246 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, get_last_invalidator_state()); 246 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, get_last_invalidator_state());
247 EXPECT_FALSE(delegate()->message_callback.is_null()); 247 EXPECT_FALSE(delegate()->message_callback.is_null());
248 url_fetcher_factory()->SetFakeResponse(GURL("http://test.url.com"), 248 url_fetcher_factory()->SetFakeResponse(GURL("http://test.url.com"),
249 std::string(), 249 std::string(),
250 net::HTTP_NO_CONTENT, 250 net::HTTP_NO_CONTENT,
251 net::URLRequestStatus::SUCCESS); 251 net::URLRequestStatus::SUCCESS);
252 252
253 // Emulate gcm connection state to be online.
254 delegate()->connection_state_callback.Run(true);
253 // After construction GCMNetworkChannel should have called Register. 255 // After construction GCMNetworkChannel should have called Register.
254 EXPECT_FALSE(delegate()->register_callback.is_null()); 256 EXPECT_FALSE(delegate()->register_callback.is_null());
255 // Return valid registration id. 257 // Return valid registration id.
256 delegate()->register_callback.Run("registration.id", gcm::GCMClient::SUCCESS); 258 delegate()->register_callback.Run("registration.id", gcm::GCMClient::SUCCESS);
257 259
258 network_channel()->SendMessage("abra.cadabra"); 260 network_channel()->SendMessage("abra.cadabra");
259 // SendMessage should have triggered RequestToken. No HTTP request should be 261 // SendMessage should have triggered RequestToken. No HTTP request should be
260 // started yet. 262 // started yet.
261 EXPECT_FALSE(delegate()->request_token_callback.is_null()); 263 EXPECT_FALSE(delegate()->request_token_callback.is_null());
262 EXPECT_EQ(url_fetchers_created_count(), 0); 264 EXPECT_EQ(url_fetchers_created_count(), 0);
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 EXPECT_EQ(input, plain); 405 EXPECT_EQ(input, plain);
404 } 406 }
405 // Presence of '-', '_'. 407 // Presence of '-', '_'.
406 input = "\xfb\xff"; 408 input = "\xfb\xff";
407 Base64EncodeURLSafe(input, &base64); 409 Base64EncodeURLSafe(input, &base64);
408 EXPECT_EQ("-_8", base64); 410 EXPECT_EQ("-_8", base64);
409 EXPECT_TRUE(Base64DecodeURLSafe(base64, &plain)); 411 EXPECT_TRUE(Base64DecodeURLSafe(base64, &plain));
410 EXPECT_EQ(input, plain); 412 EXPECT_EQ(input, plain);
411 } 413 }
412 414
413 TEST_F(GCMNetworkChannelTest, HttpTransientError) { 415 TEST_F(GCMNetworkChannelTest, ChannelState) {
414 EXPECT_FALSE(delegate()->message_callback.is_null()); 416 EXPECT_FALSE(delegate()->message_callback.is_null());
415 // POST will fail. 417 // POST will fail.
416 url_fetcher_factory()->SetFakeResponse(GURL("http://test.url.com"), 418 url_fetcher_factory()->SetFakeResponse(GURL("http://test.url.com"),
417 std::string(), 419 std::string(),
418 net::HTTP_SERVICE_UNAVAILABLE, 420 net::HTTP_SERVICE_UNAVAILABLE,
419 net::URLRequestStatus::SUCCESS); 421 net::URLRequestStatus::SUCCESS);
420 422
423 delegate()->connection_state_callback.Run(true);
421 delegate()->register_callback.Run("registration.id", gcm::GCMClient::SUCCESS); 424 delegate()->register_callback.Run("registration.id", gcm::GCMClient::SUCCESS);
422 425
423 network_channel()->SendMessage("abra.cadabra"); 426 network_channel()->SendMessage("abra.cadabra");
424 EXPECT_FALSE(delegate()->request_token_callback.is_null()); 427 EXPECT_FALSE(delegate()->request_token_callback.is_null());
425 delegate()->request_token_callback.Run( 428 delegate()->request_token_callback.Run(
426 GoogleServiceAuthError::AuthErrorNone(), "access.token"); 429 GoogleServiceAuthError::AuthErrorNone(), "access.token");
427 RunLoopUntilIdle(); 430 RunLoopUntilIdle();
428 EXPECT_EQ(url_fetchers_created_count(), 1); 431 EXPECT_EQ(url_fetchers_created_count(), 1);
429 // Failing HTTP POST should cause TRANSIENT_INVALIDATION_ERROR. 432 // Failing HTTP POST should cause TRANSIENT_INVALIDATION_ERROR.
430 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, get_last_invalidator_state()); 433 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, get_last_invalidator_state());
431 // Network change to CONNECTION_NONE shouldn't affect invalidator state. 434
432 network_channel()->OnNetworkChanged( 435 // Setup POST to succeed.
433 net::NetworkChangeNotifier::CONNECTION_NONE); 436 url_fetcher_factory()->SetFakeResponse(GURL("http://test.url.com"),
434 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, get_last_invalidator_state()); 437 "",
435 // Network change to something else should trigger retry. 438 net::HTTP_NO_CONTENT,
436 network_channel()->OnNetworkChanged( 439 net::URLRequestStatus::SUCCESS);
437 net::NetworkChangeNotifier::CONNECTION_WIFI); 440 network_channel()->SendMessage("abra.cadabra");
441 EXPECT_FALSE(delegate()->request_token_callback.is_null());
442 delegate()->request_token_callback.Run(
443 GoogleServiceAuthError::AuthErrorNone(), "access.token");
444 RunLoopUntilIdle();
445 EXPECT_EQ(url_fetchers_created_count(), 2);
446 // Successful post should set invalidator state to enabled.
438 EXPECT_EQ(INVALIDATIONS_ENABLED, get_last_invalidator_state()); 447 EXPECT_EQ(INVALIDATIONS_ENABLED, get_last_invalidator_state());
448 // Network changed event shouldn't affect invalidator state.
439 network_channel()->OnNetworkChanged( 449 network_channel()->OnNetworkChanged(
440 net::NetworkChangeNotifier::CONNECTION_NONE); 450 net::NetworkChangeNotifier::CONNECTION_NONE);
441 EXPECT_EQ(INVALIDATIONS_ENABLED, get_last_invalidator_state()); 451 EXPECT_EQ(INVALIDATIONS_ENABLED, get_last_invalidator_state());
442 }
443 452
444 TEST_F(GCMNetworkChannelTest, GcmConnectionState) { 453 // GCM connection state should affect invalidator state.
445 delegate()->connection_state_callback.Run( 454 delegate()->connection_state_callback.Run(false);
446 GCMNetworkChannelDelegate::CONNECTION_STATE_OFFLINE);
447 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, get_last_invalidator_state()); 455 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, get_last_invalidator_state());
448 delegate()->connection_state_callback.Run( 456 delegate()->connection_state_callback.Run(true);
449 GCMNetworkChannelDelegate::CONNECTION_STATE_ONLINE);
450 EXPECT_EQ(INVALIDATIONS_ENABLED, get_last_invalidator_state()); 457 EXPECT_EQ(INVALIDATIONS_ENABLED, get_last_invalidator_state());
451 } 458 }
452 459
453 #if !defined(OS_ANDROID) 460 #if !defined(OS_ANDROID)
454 TEST_F(GCMNetworkChannelTest, BuildUrl) { 461 TEST_F(GCMNetworkChannelTest, BuildUrl) {
455 GURL url = BuildUrl("registration.id"); 462 GURL url = BuildUrl("registration.id");
456 EXPECT_TRUE(url.SchemeIsHTTPOrHTTPS()); 463 EXPECT_TRUE(url.SchemeIsHTTPOrHTTPS());
457 EXPECT_FALSE(url.host().empty()); 464 EXPECT_FALSE(url.host().empty());
458 EXPECT_FALSE(url.path().empty()); 465 EXPECT_FALSE(url.path().empty());
459 std::vector<std::string> parts; 466 std::vector<std::string> parts;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 delegate()->request_token_callback.Run( 505 delegate()->request_token_callback.Run(
499 GoogleServiceAuthError::AuthErrorNone(), "access.token"); 506 GoogleServiceAuthError::AuthErrorNone(), "access.token");
500 RunLoopUntilIdle(); 507 RunLoopUntilIdle();
501 EXPECT_EQ(url_fetchers_created_count(), 3); 508 EXPECT_EQ(url_fetchers_created_count(), 3);
502 // Echo_token should be from second message. 509 // Echo_token should be from second message.
503 EXPECT_EQ("echo.token", get_last_echo_token()); 510 EXPECT_EQ("echo.token", get_last_echo_token());
504 } 511 }
505 #endif 512 #endif
506 513
507 } // namespace syncer 514 } // namespace syncer
OLDNEW
« no previous file with comments | « components/invalidation/gcm_network_channel_delegate.h ('k') | components/invalidation/push_client_channel.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698