| 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/invalidation/gcm_network_channel_delegate_impl.h" | 6 #include "chrome/browser/invalidation/gcm_invalidation_bridge.h" |
| 7 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" | 7 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" |
| 8 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 8 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| 9 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" | 9 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" |
| 10 #include "chrome/browser/signin/fake_profile_oauth2_token_service_wrapper.h" | 10 #include "chrome/browser/signin/fake_profile_oauth2_token_service_wrapper.h" |
| 11 #include "chrome/browser/signin/profile_oauth2_token_service.h" | 11 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
| 12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 13 #include "chrome/test/base/testing_profile.h" | 13 #include "chrome/test/base/testing_profile.h" |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | 14 #include "content/public/test/test_browser_thread_bundle.h" |
| 15 #include "google_apis/gaia/google_service_auth_error.h" | 15 #include "google_apis/gaia/google_service_auth_error.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 namespace invalidation { | 18 namespace invalidation { |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 class GCMNetworkChannelDelegateImplTest : public ::testing::Test { | 21 class GCMInvalidationBridgeTest : public ::testing::Test { |
| 22 protected: | 22 protected: |
| 23 GCMNetworkChannelDelegateImplTest() {} | 23 GCMInvalidationBridgeTest() {} |
| 24 | 24 |
| 25 virtual ~GCMNetworkChannelDelegateImplTest() {} | 25 virtual ~GCMInvalidationBridgeTest() {} |
| 26 | 26 |
| 27 virtual void SetUp() OVERRIDE { | 27 virtual void SetUp() OVERRIDE { |
| 28 TestingProfile::Builder builder; | 28 TestingProfile::Builder builder; |
| 29 builder.AddTestingFactory( | 29 builder.AddTestingFactory( |
| 30 ProfileOAuth2TokenServiceFactory::GetInstance(), | 30 ProfileOAuth2TokenServiceFactory::GetInstance(), |
| 31 FakeProfileOAuth2TokenServiceWrapper::BuildAutoIssuingTokenService); | 31 FakeProfileOAuth2TokenServiceWrapper::BuildAutoIssuingTokenService); |
| 32 profile_ = builder.Build(); | 32 profile_ = builder.Build(); |
| 33 | 33 |
| 34 FakeProfileOAuth2TokenService* token_service = | 34 FakeProfileOAuth2TokenService* token_service = |
| 35 (FakeProfileOAuth2TokenService*) | 35 (FakeProfileOAuth2TokenService*) |
| 36 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()); | 36 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()); |
| 37 token_service->IssueRefreshTokenForUser("", "refresh_token"); | 37 token_service->IssueRefreshTokenForUser("", "refresh_token"); |
| 38 | 38 |
| 39 delegate_.reset(new GCMNetworkChannelDelegateImpl(profile_.get())); | 39 bridge_.reset(new GCMInvalidationBridge(profile_.get())); |
| 40 |
| 41 delegate_ = bridge_->CreateDelegate(); |
| 42 delegate_->Initialize(); |
| 43 base::RunLoop run_loop; |
| 44 run_loop.RunUntilIdle(); |
| 40 } | 45 } |
| 41 | 46 |
| 42 public: | 47 public: |
| 43 void RegisterFinished(const std::string& registration_id, | 48 void RegisterFinished(const std::string& registration_id, |
| 44 gcm::GCMClient::Result result) {} | 49 gcm::GCMClient::Result result) {} |
| 45 | 50 |
| 46 void RequestTokenFinished(const GoogleServiceAuthError& error, | 51 void RequestTokenFinished(const GoogleServiceAuthError& error, |
| 47 const std::string& token) { | 52 const std::string& token) { |
| 48 issued_tokens_.push_back(token); | 53 issued_tokens_.push_back(token); |
| 49 request_token_errors_.push_back(error); | 54 request_token_errors_.push_back(error); |
| 50 } | 55 } |
| 51 | 56 |
| 52 content::TestBrowserThreadBundle thread_bundle_; | 57 content::TestBrowserThreadBundle thread_bundle_; |
| 53 scoped_ptr<Profile> profile_; | 58 scoped_ptr<Profile> profile_; |
| 54 FakeProfileOAuth2TokenService* token_service_; | 59 FakeProfileOAuth2TokenService* token_service_; |
| 55 | 60 |
| 56 std::vector<std::string> issued_tokens_; | 61 std::vector<std::string> issued_tokens_; |
| 57 std::vector<GoogleServiceAuthError> request_token_errors_; | 62 std::vector<GoogleServiceAuthError> request_token_errors_; |
| 58 | 63 |
| 59 scoped_ptr<GCMNetworkChannelDelegateImpl> delegate_; | 64 scoped_ptr<GCMInvalidationBridge> bridge_; |
| 65 scoped_ptr<syncer::GCMNetworkChannelDelegate> delegate_; |
| 60 }; | 66 }; |
| 61 | 67 |
| 62 TEST_F(GCMNetworkChannelDelegateImplTest, RequestToken) { | 68 TEST_F(GCMInvalidationBridgeTest, RequestToken) { |
| 63 // Make sure that call to RequestToken reaches OAuth2TokenService and gets | 69 // Make sure that call to RequestToken reaches OAuth2TokenService and gets |
| 64 // back to callback. | 70 // back to callback. |
| 65 delegate_->RequestToken( | 71 delegate_->RequestToken( |
| 66 base::Bind(&GCMNetworkChannelDelegateImplTest::RequestTokenFinished, | 72 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished, |
| 67 base::Unretained(this))); | 73 base::Unretained(this))); |
| 68 base::RunLoop run_loop; | 74 base::RunLoop run_loop; |
| 69 run_loop.RunUntilIdle(); | 75 run_loop.RunUntilIdle(); |
| 70 EXPECT_EQ(1U, issued_tokens_.size()); | 76 EXPECT_EQ(1U, issued_tokens_.size()); |
| 71 EXPECT_NE("", issued_tokens_[0]); | 77 EXPECT_NE("", issued_tokens_[0]); |
| 72 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[0]); | 78 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[0]); |
| 73 } | 79 } |
| 74 | 80 |
| 75 TEST_F(GCMNetworkChannelDelegateImplTest, RequestTokenTwoConcurrentRequests) { | 81 TEST_F(GCMInvalidationBridgeTest, RequestTokenTwoConcurrentRequests) { |
| 76 // First call should finish with REQUEST_CANCELLED error. | 82 // First call should finish with REQUEST_CANCELLED error. |
| 77 delegate_->RequestToken( | 83 delegate_->RequestToken( |
| 78 base::Bind(&GCMNetworkChannelDelegateImplTest::RequestTokenFinished, | 84 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished, |
| 79 base::Unretained(this))); | 85 base::Unretained(this))); |
| 80 // Second request should succeed. | 86 // Second request should succeed. |
| 81 delegate_->RequestToken( | 87 delegate_->RequestToken( |
| 82 base::Bind(&GCMNetworkChannelDelegateImplTest::RequestTokenFinished, | 88 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished, |
| 83 base::Unretained(this))); | 89 base::Unretained(this))); |
| 84 base::RunLoop run_loop; | 90 base::RunLoop run_loop; |
| 85 run_loop.RunUntilIdle(); | 91 run_loop.RunUntilIdle(); |
| 86 | 92 |
| 87 EXPECT_EQ(2U, issued_tokens_.size()); | 93 EXPECT_EQ(2U, issued_tokens_.size()); |
| 88 | 94 |
| 89 EXPECT_EQ("", issued_tokens_[0]); | 95 EXPECT_EQ("", issued_tokens_[0]); |
| 90 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED, | 96 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED, |
| 91 request_token_errors_[0].state()); | 97 request_token_errors_[0].state()); |
| 92 | 98 |
| 93 EXPECT_NE("", issued_tokens_[1]); | 99 EXPECT_NE("", issued_tokens_[1]); |
| 94 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[1]); | 100 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[1]); |
| 95 } | 101 } |
| 96 | 102 |
| 97 } // namespace | 103 } // namespace |
| 98 } // namespace invalidation | 104 } // namespace invalidation |
| OLD | NEW |