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/services/gcm/gcm_profile_service.h" | 7 #include "chrome/browser/services/gcm/gcm_driver.h" |
8 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | |
9 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" | 8 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" |
10 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" | 9 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" |
11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 10 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
12 #include "chrome/test/base/testing_profile.h" | 11 #include "chrome/test/base/testing_profile.h" |
13 #include "content/public/test/test_browser_thread_bundle.h" | 12 #include "content/public/test/test_browser_thread_bundle.h" |
14 #include "google_apis/gaia/fake_identity_provider.h" | 13 #include "google_apis/gaia/fake_identity_provider.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 GCMDriver::Register that always succeeds with the same |
22 // same registrationId. | 21 // registrationId. |
23 class FakeGCMProfileService : public gcm::GCMProfileService { | 22 class FakeGCMDriver : public gcm::GCMDriver { |
24 public: | 23 public: |
25 static KeyedService* Build(content::BrowserContext* context) { | 24 FakeGCMDriver() {} |
26 Profile* profile = static_cast<Profile*>(context); | 25 virtual ~FakeGCMDriver() {} |
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 const RegisterCallback& callback) OVERRIDE { | 29 const 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(FakeGCMDriver); |
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 TestingProfile::Builder builder; |
54 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), | 48 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), |
55 &BuildAutoIssuingFakeProfileOAuth2TokenService); | 49 &BuildAutoIssuingFakeProfileOAuth2TokenService); |
56 builder.AddTestingFactory(gcm::GCMProfileServiceFactory::GetInstance(), | |
57 &FakeGCMProfileService::Build); | |
58 profile_ = builder.Build(); | 50 profile_ = builder.Build(); |
59 | 51 |
60 FakeProfileOAuth2TokenService* token_service = | 52 FakeProfileOAuth2TokenService* token_service = |
61 (FakeProfileOAuth2TokenService*) | 53 (FakeProfileOAuth2TokenService*) |
62 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()); | 54 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()); |
63 token_service->IssueRefreshTokenForUser("", "fake_refresh_token"); | 55 token_service->IssueRefreshTokenForUser("", "fake_refresh_token"); |
64 gcm_profile_service_ = | 56 gcm_driver_.reset(new FakeGCMDriver()); |
65 (FakeGCMProfileService*)gcm::GCMProfileServiceFactory::GetForProfile( | |
66 profile_.get()); | |
67 | 57 |
68 identity_provider_.reset(new FakeIdentityProvider(token_service)); | 58 identity_provider_.reset(new FakeIdentityProvider(token_service)); |
69 bridge_.reset(new GCMInvalidationBridge(gcm_profile_service_, | 59 bridge_.reset(new GCMInvalidationBridge(gcm_driver_.get(), |
70 identity_provider_.get())); | 60 identity_provider_.get())); |
71 | 61 |
72 delegate_ = bridge_->CreateDelegate(); | 62 delegate_ = bridge_->CreateDelegate(); |
73 delegate_->Initialize(); | 63 delegate_->Initialize(); |
74 base::RunLoop run_loop; | 64 base::RunLoop run_loop; |
75 run_loop.RunUntilIdle(); | 65 run_loop.RunUntilIdle(); |
76 } | 66 } |
77 | 67 |
78 public: | 68 public: |
79 void RegisterFinished(const std::string& registration_id, | 69 void RegisterFinished(const std::string& registration_id, |
80 gcm::GCMClient::Result result) { | 70 gcm::GCMClient::Result result) { |
81 registration_id_ = registration_id; | 71 registration_id_ = registration_id; |
82 } | 72 } |
83 | 73 |
84 void RequestTokenFinished(const GoogleServiceAuthError& error, | 74 void RequestTokenFinished(const GoogleServiceAuthError& error, |
85 const std::string& token) { | 75 const std::string& token) { |
86 issued_tokens_.push_back(token); | 76 issued_tokens_.push_back(token); |
87 request_token_errors_.push_back(error); | 77 request_token_errors_.push_back(error); |
88 } | 78 } |
89 | 79 |
90 content::TestBrowserThreadBundle thread_bundle_; | 80 content::TestBrowserThreadBundle thread_bundle_; |
91 scoped_ptr<Profile> profile_; | 81 scoped_ptr<Profile> profile_; |
92 FakeGCMProfileService* gcm_profile_service_; | 82 scoped_ptr<gcm::GCMDriver> gcm_driver_; |
93 scoped_ptr<FakeIdentityProvider> identity_provider_; | 83 scoped_ptr<FakeIdentityProvider> identity_provider_; |
94 | 84 |
95 std::vector<std::string> issued_tokens_; | 85 std::vector<std::string> issued_tokens_; |
96 std::vector<GoogleServiceAuthError> request_token_errors_; | 86 std::vector<GoogleServiceAuthError> request_token_errors_; |
97 std::string registration_id_; | 87 std::string registration_id_; |
98 | 88 |
99 scoped_ptr<GCMInvalidationBridge> bridge_; | 89 scoped_ptr<GCMInvalidationBridge> bridge_; |
100 scoped_ptr<syncer::GCMNetworkChannelDelegate> delegate_; | 90 scoped_ptr<syncer::GCMNetworkChannelDelegate> delegate_; |
101 }; | 91 }; |
102 | 92 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 delegate_->Register(base::Bind(&GCMInvalidationBridgeTest::RegisterFinished, | 130 delegate_->Register(base::Bind(&GCMInvalidationBridgeTest::RegisterFinished, |
141 base::Unretained(this))); | 131 base::Unretained(this))); |
142 base::RunLoop run_loop; | 132 base::RunLoop run_loop; |
143 run_loop.RunUntilIdle(); | 133 run_loop.RunUntilIdle(); |
144 | 134 |
145 EXPECT_FALSE(registration_id_.empty()); | 135 EXPECT_FALSE(registration_id_.empty()); |
146 } | 136 } |
147 | 137 |
148 } // namespace | 138 } // namespace |
149 } // namespace invalidation | 139 } // namespace invalidation |
OLD | NEW |