| 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/run_loop.h" | 5 #include "base/run_loop.h" |
| 6 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" | 6 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" |
| 7 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" | 7 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" |
| 8 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 8 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 9 #include "chrome/test/base/testing_profile.h" | 9 #include "chrome/test/base/testing_profile.h" |
| 10 #include "components/gcm_driver/fake_gcm_driver.h" | 10 #include "components/gcm_driver/fake_gcm_driver.h" |
| 11 #include "components/gcm_driver/gcm_driver.h" | 11 #include "components/gcm_driver/gcm_driver.h" |
| 12 #include "components/invalidation/gcm_invalidation_bridge.h" | 12 #include "components/invalidation/gcm_invalidation_bridge.h" |
| 13 #include "content/public/test/test_browser_thread_bundle.h" | 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "google_apis/gaia/fake_identity_provider.h" | 14 #include "google_apis/gaia/fake_identity_provider.h" |
| 15 #include "google_apis/gaia/google_service_auth_error.h" | 15 #include "google_apis/gaia/google_service_auth_error.h" |
| 16 #include "net/base/ip_endpoint.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 namespace invalidation { | 19 namespace invalidation { |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 // Implementation of GCMDriver::Register that always succeeds with the same | 22 // Implementation of GCMDriver::Register that always succeeds with the same |
| 22 // registrationId. | 23 // registrationId. |
| 23 class CustomFakeGCMDriver : public gcm::FakeGCMDriver { | 24 class CustomFakeGCMDriver : public gcm::FakeGCMDriver { |
| 24 public: | 25 public: |
| 25 CustomFakeGCMDriver() {} | 26 CustomFakeGCMDriver() {} |
| (...skipping 12 matching lines...) Expand all Loading... |
| 38 std::string("registration.id"), | 39 std::string("registration.id"), |
| 39 gcm::GCMClient::SUCCESS)); | 40 gcm::GCMClient::SUCCESS)); |
| 40 } | 41 } |
| 41 | 42 |
| 42 private: | 43 private: |
| 43 DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver); | 44 DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver); |
| 44 }; | 45 }; |
| 45 | 46 |
| 46 class GCMInvalidationBridgeTest : public ::testing::Test { | 47 class GCMInvalidationBridgeTest : public ::testing::Test { |
| 47 protected: | 48 protected: |
| 48 GCMInvalidationBridgeTest() {} | 49 GCMInvalidationBridgeTest() |
| 50 : connection_state_( |
| 51 syncer::GCMNetworkChannelDelegate::CONNECTION_STATE_OFFLINE) {} |
| 49 | 52 |
| 50 virtual ~GCMInvalidationBridgeTest() {} | 53 virtual ~GCMInvalidationBridgeTest() {} |
| 51 | 54 |
| 52 virtual void SetUp() OVERRIDE { | 55 virtual void SetUp() OVERRIDE { |
| 53 TestingProfile::Builder builder; | 56 TestingProfile::Builder builder; |
| 54 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), | 57 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), |
| 55 &BuildAutoIssuingFakeProfileOAuth2TokenService); | 58 &BuildAutoIssuingFakeProfileOAuth2TokenService); |
| 56 profile_ = builder.Build(); | 59 profile_ = builder.Build(); |
| 57 | 60 |
| 58 FakeProfileOAuth2TokenService* token_service = | 61 FakeProfileOAuth2TokenService* token_service = |
| 59 (FakeProfileOAuth2TokenService*) | 62 (FakeProfileOAuth2TokenService*) |
| 60 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()); | 63 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()); |
| 61 token_service->IssueRefreshTokenForUser("", "fake_refresh_token"); | 64 token_service->IssueRefreshTokenForUser("", "fake_refresh_token"); |
| 62 gcm_driver_.reset(new CustomFakeGCMDriver()); | 65 gcm_driver_.reset(new CustomFakeGCMDriver()); |
| 63 | 66 |
| 64 identity_provider_.reset(new FakeIdentityProvider(token_service)); | 67 identity_provider_.reset(new FakeIdentityProvider(token_service)); |
| 65 bridge_.reset(new GCMInvalidationBridge(gcm_driver_.get(), | 68 bridge_.reset(new GCMInvalidationBridge(gcm_driver_.get(), |
| 66 identity_provider_.get())); | 69 identity_provider_.get())); |
| 67 | 70 |
| 68 delegate_ = bridge_->CreateDelegate(); | 71 delegate_ = bridge_->CreateDelegate(); |
| 69 delegate_->Initialize(); | 72 delegate_->Initialize( |
| 73 base::Bind(&GCMInvalidationBridgeTest::ConnectionStateChanged, |
| 74 base::Unretained(this))); |
| 75 RunLoop(); |
| 76 } |
| 77 |
| 78 void RunLoop() { |
| 70 base::RunLoop run_loop; | 79 base::RunLoop run_loop; |
| 71 run_loop.RunUntilIdle(); | 80 run_loop.RunUntilIdle(); |
| 72 } | 81 } |
| 73 | 82 |
| 74 public: | 83 public: |
| 75 void RegisterFinished(const std::string& registration_id, | 84 void RegisterFinished(const std::string& registration_id, |
| 76 gcm::GCMClient::Result result) { | 85 gcm::GCMClient::Result result) { |
| 77 registration_id_ = registration_id; | 86 registration_id_ = registration_id; |
| 78 } | 87 } |
| 79 | 88 |
| 80 void RequestTokenFinished(const GoogleServiceAuthError& error, | 89 void RequestTokenFinished(const GoogleServiceAuthError& error, |
| 81 const std::string& token) { | 90 const std::string& token) { |
| 82 issued_tokens_.push_back(token); | 91 issued_tokens_.push_back(token); |
| 83 request_token_errors_.push_back(error); | 92 request_token_errors_.push_back(error); |
| 84 } | 93 } |
| 85 | 94 |
| 95 void ConnectionStateChanged( |
| 96 syncer::GCMNetworkChannelDelegate::ConnectionState connection_state) { |
| 97 connection_state_ = connection_state; |
| 98 } |
| 99 |
| 86 content::TestBrowserThreadBundle thread_bundle_; | 100 content::TestBrowserThreadBundle thread_bundle_; |
| 87 scoped_ptr<Profile> profile_; | 101 scoped_ptr<Profile> profile_; |
| 88 scoped_ptr<gcm::GCMDriver> gcm_driver_; | 102 scoped_ptr<gcm::GCMDriver> gcm_driver_; |
| 89 scoped_ptr<FakeIdentityProvider> identity_provider_; | 103 scoped_ptr<FakeIdentityProvider> identity_provider_; |
| 90 | 104 |
| 91 std::vector<std::string> issued_tokens_; | 105 std::vector<std::string> issued_tokens_; |
| 92 std::vector<GoogleServiceAuthError> request_token_errors_; | 106 std::vector<GoogleServiceAuthError> request_token_errors_; |
| 93 std::string registration_id_; | 107 std::string registration_id_; |
| 108 syncer::GCMNetworkChannelDelegate::ConnectionState connection_state_; |
| 94 | 109 |
| 95 scoped_ptr<GCMInvalidationBridge> bridge_; | 110 scoped_ptr<GCMInvalidationBridge> bridge_; |
| 96 scoped_ptr<syncer::GCMNetworkChannelDelegate> delegate_; | 111 scoped_ptr<syncer::GCMNetworkChannelDelegate> delegate_; |
| 97 }; | 112 }; |
| 98 | 113 |
| 99 TEST_F(GCMInvalidationBridgeTest, RequestToken) { | 114 TEST_F(GCMInvalidationBridgeTest, RequestToken) { |
| 100 // Make sure that call to RequestToken reaches OAuth2TokenService and gets | 115 // Make sure that call to RequestToken reaches OAuth2TokenService and gets |
| 101 // back to callback. | 116 // back to callback. |
| 102 delegate_->RequestToken( | 117 delegate_->RequestToken( |
| 103 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished, | 118 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished, |
| 104 base::Unretained(this))); | 119 base::Unretained(this))); |
| 105 base::RunLoop run_loop; | 120 RunLoop(); |
| 106 run_loop.RunUntilIdle(); | |
| 107 EXPECT_EQ(1U, issued_tokens_.size()); | 121 EXPECT_EQ(1U, issued_tokens_.size()); |
| 108 EXPECT_NE("", issued_tokens_[0]); | 122 EXPECT_NE("", issued_tokens_[0]); |
| 109 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[0]); | 123 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[0]); |
| 110 } | 124 } |
| 111 | 125 |
| 112 TEST_F(GCMInvalidationBridgeTest, RequestTokenTwoConcurrentRequests) { | 126 TEST_F(GCMInvalidationBridgeTest, RequestTokenTwoConcurrentRequests) { |
| 113 // First call should finish with REQUEST_CANCELLED error. | 127 // First call should finish with REQUEST_CANCELLED error. |
| 114 delegate_->RequestToken( | 128 delegate_->RequestToken( |
| 115 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished, | 129 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished, |
| 116 base::Unretained(this))); | 130 base::Unretained(this))); |
| 117 // Second request should succeed. | 131 // Second request should succeed. |
| 118 delegate_->RequestToken( | 132 delegate_->RequestToken( |
| 119 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished, | 133 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished, |
| 120 base::Unretained(this))); | 134 base::Unretained(this))); |
| 121 base::RunLoop run_loop; | 135 RunLoop(); |
| 122 run_loop.RunUntilIdle(); | |
| 123 | 136 |
| 124 EXPECT_EQ(2U, issued_tokens_.size()); | 137 EXPECT_EQ(2U, issued_tokens_.size()); |
| 125 | 138 |
| 126 EXPECT_EQ("", issued_tokens_[0]); | 139 EXPECT_EQ("", issued_tokens_[0]); |
| 127 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED, | 140 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED, |
| 128 request_token_errors_[0].state()); | 141 request_token_errors_[0].state()); |
| 129 | 142 |
| 130 EXPECT_NE("", issued_tokens_[1]); | 143 EXPECT_NE("", issued_tokens_[1]); |
| 131 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[1]); | 144 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[1]); |
| 132 } | 145 } |
| 133 | 146 |
| 134 TEST_F(GCMInvalidationBridgeTest, Register) { | 147 TEST_F(GCMInvalidationBridgeTest, Register) { |
| 135 EXPECT_TRUE(registration_id_.empty()); | 148 EXPECT_TRUE(registration_id_.empty()); |
| 136 delegate_->Register(base::Bind(&GCMInvalidationBridgeTest::RegisterFinished, | 149 delegate_->Register(base::Bind(&GCMInvalidationBridgeTest::RegisterFinished, |
| 137 base::Unretained(this))); | 150 base::Unretained(this))); |
| 138 base::RunLoop run_loop; | 151 RunLoop(); |
| 139 run_loop.RunUntilIdle(); | |
| 140 | 152 |
| 141 EXPECT_FALSE(registration_id_.empty()); | 153 EXPECT_FALSE(registration_id_.empty()); |
| 142 } | 154 } |
| 143 | 155 |
| 156 TEST_F(GCMInvalidationBridgeTest, ConnectionState) { |
| 157 EXPECT_EQ(syncer::GCMNetworkChannelDelegate::CONNECTION_STATE_OFFLINE, |
| 158 connection_state_); |
| 159 bridge_->OnConnected(net::IPEndPoint()); |
| 160 RunLoop(); |
| 161 EXPECT_EQ(syncer::GCMNetworkChannelDelegate::CONNECTION_STATE_ONLINE, |
| 162 connection_state_); |
| 163 bridge_->OnDisconnected(); |
| 164 RunLoop(); |
| 165 EXPECT_EQ(syncer::GCMNetworkChannelDelegate::CONNECTION_STATE_OFFLINE, |
| 166 connection_state_); |
| 167 } |
| 168 |
| 144 } // namespace | 169 } // namespace |
| 145 } // namespace invalidation | 170 } // namespace invalidation |
| OLD | NEW |