| 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_invalidation_bridge.h" | 6 #include "chrome/browser/invalidation/gcm_invalidation_bridge.h" |
| 7 #include "chrome/browser/invalidation/invalidation_auth_provider.h" |
| 7 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 8 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
| 8 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 9 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| 9 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" | 10 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" |
| 10 #include "chrome/browser/signin/fake_profile_oauth2_token_service_wrapper.h" | 11 #include "chrome/browser/signin/fake_profile_oauth2_token_service_wrapper.h" |
| 11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 12 #include "chrome/test/base/testing_profile.h" | 13 #include "chrome/test/base/testing_profile.h" |
| 13 #include "components/signin/core/profile_oauth2_token_service.h" | 14 #include "components/signin/core/profile_oauth2_token_service.h" |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
| 15 #include "google_apis/gaia/google_service_auth_error.h" | 16 #include "google_apis/gaia/google_service_auth_error.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 36 base::MessageLoop::current()->PostTask( | 37 base::MessageLoop::current()->PostTask( |
| 37 FROM_HERE, | 38 FROM_HERE, |
| 38 base::Bind( | 39 base::Bind( |
| 39 callback, std::string("registration.id"), gcm::GCMClient::SUCCESS)); | 40 callback, std::string("registration.id"), gcm::GCMClient::SUCCESS)); |
| 40 } | 41 } |
| 41 | 42 |
| 42 private: | 43 private: |
| 43 DISALLOW_COPY_AND_ASSIGN(FakeGCMProfileService); | 44 DISALLOW_COPY_AND_ASSIGN(FakeGCMProfileService); |
| 44 }; | 45 }; |
| 45 | 46 |
| 47 // Fake invalidation auth provider implementation. |
| 48 class FakeInvalidationAuthProvider : public InvalidationAuthProvider { |
| 49 public: |
| 50 explicit FakeInvalidationAuthProvider( |
| 51 ProfileOAuth2TokenService* token_service) |
| 52 : token_service_(token_service) {} |
| 53 virtual ~FakeInvalidationAuthProvider() {} |
| 54 |
| 55 // InvalidationAuthProvider: |
| 56 virtual OAuth2TokenService* GetTokenService() OVERRIDE { |
| 57 return token_service_; |
| 58 } |
| 59 virtual std::string GetAccountId() OVERRIDE { return std::string(); } |
| 60 virtual bool ShowLoginUI() OVERRIDE { return false; } |
| 61 |
| 62 private: |
| 63 OAuth2TokenService* token_service_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(FakeInvalidationAuthProvider); |
| 66 }; |
| 67 |
| 46 class GCMInvalidationBridgeTest : public ::testing::Test { | 68 class GCMInvalidationBridgeTest : public ::testing::Test { |
| 47 protected: | 69 protected: |
| 48 GCMInvalidationBridgeTest() {} | 70 GCMInvalidationBridgeTest() {} |
| 49 | 71 |
| 50 virtual ~GCMInvalidationBridgeTest() {} | 72 virtual ~GCMInvalidationBridgeTest() {} |
| 51 | 73 |
| 52 virtual void SetUp() OVERRIDE { | 74 virtual void SetUp() OVERRIDE { |
| 53 TestingProfile::Builder builder; | 75 TestingProfile::Builder builder; |
| 54 builder.AddTestingFactory( | 76 builder.AddTestingFactory( |
| 55 ProfileOAuth2TokenServiceFactory::GetInstance(), | 77 ProfileOAuth2TokenServiceFactory::GetInstance(), |
| 56 &FakeProfileOAuth2TokenServiceWrapper::BuildAutoIssuingTokenService); | 78 &FakeProfileOAuth2TokenServiceWrapper::BuildAutoIssuingTokenService); |
| 57 builder.AddTestingFactory(gcm::GCMProfileServiceFactory::GetInstance(), | 79 builder.AddTestingFactory(gcm::GCMProfileServiceFactory::GetInstance(), |
| 58 &FakeGCMProfileService::Build); | 80 &FakeGCMProfileService::Build); |
| 59 profile_ = builder.Build(); | 81 profile_ = builder.Build(); |
| 60 | 82 |
| 61 FakeProfileOAuth2TokenService* token_service = | 83 FakeProfileOAuth2TokenService* token_service = |
| 62 (FakeProfileOAuth2TokenService*) | 84 (FakeProfileOAuth2TokenService*) |
| 63 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()); | 85 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()); |
| 64 token_service->IssueRefreshTokenForUser("", "refresh_token"); | 86 token_service->IssueRefreshTokenForUser("", "fake_refresh_token"); |
| 65 gcm_profile_service_ = | 87 gcm_profile_service_ = |
| 66 (FakeGCMProfileService*)gcm::GCMProfileServiceFactory::GetForProfile( | 88 (FakeGCMProfileService*)gcm::GCMProfileServiceFactory::GetForProfile( |
| 67 profile_.get()); | 89 profile_.get()); |
| 68 | 90 |
| 69 bridge_.reset(new GCMInvalidationBridge(profile_.get())); | 91 auth_provider_.reset(new FakeInvalidationAuthProvider(token_service)); |
| 92 bridge_.reset( |
| 93 new GCMInvalidationBridge(gcm_profile_service_, auth_provider_.get())); |
| 70 | 94 |
| 71 delegate_ = bridge_->CreateDelegate(); | 95 delegate_ = bridge_->CreateDelegate(); |
| 72 delegate_->Initialize(); | 96 delegate_->Initialize(); |
| 73 base::RunLoop run_loop; | 97 base::RunLoop run_loop; |
| 74 run_loop.RunUntilIdle(); | 98 run_loop.RunUntilIdle(); |
| 75 } | 99 } |
| 76 | 100 |
| 77 public: | 101 public: |
| 78 void RegisterFinished(const std::string& registration_id, | 102 void RegisterFinished(const std::string& registration_id, |
| 79 gcm::GCMClient::Result result) { | 103 gcm::GCMClient::Result result) { |
| 80 registration_id_ = registration_id; | 104 registration_id_ = registration_id; |
| 81 } | 105 } |
| 82 | 106 |
| 83 void RequestTokenFinished(const GoogleServiceAuthError& error, | 107 void RequestTokenFinished(const GoogleServiceAuthError& error, |
| 84 const std::string& token) { | 108 const std::string& token) { |
| 85 issued_tokens_.push_back(token); | 109 issued_tokens_.push_back(token); |
| 86 request_token_errors_.push_back(error); | 110 request_token_errors_.push_back(error); |
| 87 } | 111 } |
| 88 | 112 |
| 89 content::TestBrowserThreadBundle thread_bundle_; | 113 content::TestBrowserThreadBundle thread_bundle_; |
| 90 scoped_ptr<Profile> profile_; | 114 scoped_ptr<Profile> profile_; |
| 91 FakeGCMProfileService* gcm_profile_service_; | 115 FakeGCMProfileService* gcm_profile_service_; |
| 116 scoped_ptr<FakeInvalidationAuthProvider> auth_provider_; |
| 92 | 117 |
| 93 std::vector<std::string> issued_tokens_; | 118 std::vector<std::string> issued_tokens_; |
| 94 std::vector<GoogleServiceAuthError> request_token_errors_; | 119 std::vector<GoogleServiceAuthError> request_token_errors_; |
| 95 std::string registration_id_; | 120 std::string registration_id_; |
| 96 | 121 |
| 97 scoped_ptr<GCMInvalidationBridge> bridge_; | 122 scoped_ptr<GCMInvalidationBridge> bridge_; |
| 98 scoped_ptr<syncer::GCMNetworkChannelDelegate> delegate_; | 123 scoped_ptr<syncer::GCMNetworkChannelDelegate> delegate_; |
| 99 }; | 124 }; |
| 100 | 125 |
| 101 TEST_F(GCMInvalidationBridgeTest, RequestToken) { | 126 TEST_F(GCMInvalidationBridgeTest, RequestToken) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 delegate_->Register(base::Bind(&GCMInvalidationBridgeTest::RegisterFinished, | 163 delegate_->Register(base::Bind(&GCMInvalidationBridgeTest::RegisterFinished, |
| 139 base::Unretained(this))); | 164 base::Unretained(this))); |
| 140 base::RunLoop run_loop; | 165 base::RunLoop run_loop; |
| 141 run_loop.RunUntilIdle(); | 166 run_loop.RunUntilIdle(); |
| 142 | 167 |
| 143 EXPECT_FALSE(registration_id_.empty()); | 168 EXPECT_FALSE(registration_id_.empty()); |
| 144 } | 169 } |
| 145 | 170 |
| 146 } // namespace | 171 } // namespace |
| 147 } // namespace invalidation | 172 } // namespace invalidation |
| OLD | NEW |