| 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/fake_invalidation_service.h" |
| 6 #include "chrome/browser/invalidation/gcm_invalidation_bridge.h" | 7 #include "chrome/browser/invalidation/gcm_invalidation_bridge.h" |
| 8 #include "chrome/browser/invalidation/invalidation_auth_provider.h" |
| 7 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 9 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
| 8 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 10 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| 9 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" | 11 #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/profile_oauth2_token_service.h" | |
| 12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 13 #include "chrome/test/base/testing_profile.h" | 12 #include "chrome/test/base/testing_profile.h" |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 15 #include "google_apis/gaia/google_service_auth_error.h" | 14 #include "google_apis/gaia/google_service_auth_error.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 16 |
| 18 namespace invalidation { | 17 namespace invalidation { |
| 19 namespace { | 18 namespace { |
| 20 | 19 |
| 21 // Implementation of GCMProfileService::Register that always succeeds with the | 20 // Implementation of GCMProfileService::Register that always succeeds with the |
| 22 // same registrationId. | 21 // same registrationId. |
| 23 class FakeGCMProfileService : public gcm::GCMProfileService { | 22 class FakeGCMProfileService : public gcm::GCMProfileService { |
| 24 public: | 23 public: |
| 25 static KeyedService* Build(content::BrowserContext* context) { | 24 explicit FakeGCMProfileService() |
| 26 Profile* profile = static_cast<Profile*>(context); | 25 : gcm::GCMProfileService(NULL) {} |
| 27 return new FakeGCMProfileService(profile); | |
| 28 } | |
| 29 | |
| 30 explicit FakeGCMProfileService(Profile* profile) | |
| 31 : gcm::GCMProfileService(profile) {} | |
| 32 | 26 |
| 33 virtual void Register(const std::string& app_id, | 27 virtual void Register(const std::string& app_id, |
| 34 const std::vector<std::string>& sender_ids, | 28 const std::vector<std::string>& sender_ids, |
| 35 RegisterCallback callback) OVERRIDE { | 29 RegisterCallback callback) OVERRIDE { |
| 36 base::MessageLoop::current()->PostTask( | 30 base::MessageLoop::current()->PostTask( |
| 37 FROM_HERE, | 31 FROM_HERE, |
| 38 base::Bind( | 32 base::Bind( |
| 39 callback, std::string("registration.id"), gcm::GCMClient::SUCCESS)); | 33 callback, std::string("registration.id"), gcm::GCMClient::SUCCESS)); |
| 40 } | 34 } |
| 41 | 35 |
| 42 private: | 36 private: |
| 43 DISALLOW_COPY_AND_ASSIGN(FakeGCMProfileService); | 37 DISALLOW_COPY_AND_ASSIGN(FakeGCMProfileService); |
| 44 }; | 38 }; |
| 45 | 39 |
| 46 class GCMInvalidationBridgeTest : public ::testing::Test { | 40 class GCMInvalidationBridgeTest : public ::testing::Test { |
| 47 protected: | 41 protected: |
| 48 GCMInvalidationBridgeTest() {} | 42 GCMInvalidationBridgeTest() {} |
| 49 | 43 |
| 50 virtual ~GCMInvalidationBridgeTest() {} | 44 virtual ~GCMInvalidationBridgeTest() {} |
| 51 | 45 |
| 52 virtual void SetUp() OVERRIDE { | 46 virtual void SetUp() OVERRIDE { |
| 53 TestingProfile::Builder builder; | 47 auth_provider_.fake_token_service()->IssueRefreshTokenForUser( |
| 54 builder.AddTestingFactory( | 48 auth_provider_.GetAccountId(), "fake_refresh_token"); |
| 55 ProfileOAuth2TokenServiceFactory::GetInstance(), | 49 bridge_.reset( |
| 56 &FakeProfileOAuth2TokenServiceWrapper::BuildAutoIssuingTokenService); | 50 new GCMInvalidationBridge(&gcm_profile_service_, &auth_provider_)); |
| 57 builder.AddTestingFactory(gcm::GCMProfileServiceFactory::GetInstance(), | |
| 58 &FakeGCMProfileService::Build); | |
| 59 profile_ = builder.Build(); | |
| 60 | |
| 61 FakeProfileOAuth2TokenService* token_service = | |
| 62 (FakeProfileOAuth2TokenService*) | |
| 63 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()); | |
| 64 token_service->IssueRefreshTokenForUser("", "refresh_token"); | |
| 65 gcm_profile_service_ = | |
| 66 (FakeGCMProfileService*)gcm::GCMProfileServiceFactory::GetForProfile( | |
| 67 profile_.get()); | |
| 68 | |
| 69 bridge_.reset(new GCMInvalidationBridge(profile_.get())); | |
| 70 | 51 |
| 71 delegate_ = bridge_->CreateDelegate(); | 52 delegate_ = bridge_->CreateDelegate(); |
| 72 delegate_->Initialize(); | 53 delegate_->Initialize(); |
| 73 base::RunLoop run_loop; | 54 base::RunLoop run_loop; |
| 74 run_loop.RunUntilIdle(); | 55 run_loop.RunUntilIdle(); |
| 75 } | 56 } |
| 76 | 57 |
| 77 public: | 58 public: |
| 78 void RegisterFinished(const std::string& registration_id, | 59 void RegisterFinished(const std::string& registration_id, |
| 79 gcm::GCMClient::Result result) { | 60 gcm::GCMClient::Result result) { |
| 80 registration_id_ = registration_id; | 61 registration_id_ = registration_id; |
| 81 } | 62 } |
| 82 | 63 |
| 83 void RequestTokenFinished(const GoogleServiceAuthError& error, | 64 void RequestTokenFinished(const GoogleServiceAuthError& error, |
| 84 const std::string& token) { | 65 const std::string& token) { |
| 85 issued_tokens_.push_back(token); | 66 issued_tokens_.push_back(token); |
| 86 request_token_errors_.push_back(error); | 67 request_token_errors_.push_back(error); |
| 87 } | 68 } |
| 88 | 69 |
| 89 content::TestBrowserThreadBundle thread_bundle_; | 70 content::TestBrowserThreadBundle thread_bundle_; |
| 90 scoped_ptr<Profile> profile_; | 71 FakeGCMProfileService gcm_profile_service_; |
| 91 FakeGCMProfileService* gcm_profile_service_; | 72 FakeInvalidationAuthProvider auth_provider_; |
| 92 | 73 |
| 93 std::vector<std::string> issued_tokens_; | 74 std::vector<std::string> issued_tokens_; |
| 94 std::vector<GoogleServiceAuthError> request_token_errors_; | 75 std::vector<GoogleServiceAuthError> request_token_errors_; |
| 95 std::string registration_id_; | 76 std::string registration_id_; |
| 96 | 77 |
| 97 scoped_ptr<GCMInvalidationBridge> bridge_; | 78 scoped_ptr<GCMInvalidationBridge> bridge_; |
| 98 scoped_ptr<syncer::GCMNetworkChannelDelegate> delegate_; | 79 scoped_ptr<syncer::GCMNetworkChannelDelegate> delegate_; |
| 99 }; | 80 }; |
| 100 | 81 |
| 101 TEST_F(GCMInvalidationBridgeTest, RequestToken) { | 82 TEST_F(GCMInvalidationBridgeTest, RequestToken) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 delegate_->Register(base::Bind(&GCMInvalidationBridgeTest::RegisterFinished, | 119 delegate_->Register(base::Bind(&GCMInvalidationBridgeTest::RegisterFinished, |
| 139 base::Unretained(this))); | 120 base::Unretained(this))); |
| 140 base::RunLoop run_loop; | 121 base::RunLoop run_loop; |
| 141 run_loop.RunUntilIdle(); | 122 run_loop.RunUntilIdle(); |
| 142 | 123 |
| 143 EXPECT_FALSE(registration_id_.empty()); | 124 EXPECT_FALSE(registration_id_.empty()); |
| 144 } | 125 } |
| 145 | 126 |
| 146 } // namespace | 127 } // namespace |
| 147 } // namespace invalidation | 128 } // namespace invalidation |
| OLD | NEW |