Fire an error if a pref used in the UI is missing once all prefs are fetched.
[chromium-blink-merge.git] / chrome / browser / invalidation / gcm_invalidation_bridge_unittest.cc
blob2552b14d16dc436a5e389eded431f9eb13a10875
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
3 // found in the LICENSE file.
5 #include "base/run_loop.h"
6 #include "chrome/browser/signin/account_tracker_service_factory.h"
7 #include "chrome/browser/signin/chrome_signin_client_factory.h"
8 #include "chrome/browser/signin/fake_account_tracker_service.h"
9 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
10 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
12 #include "chrome/browser/signin/test_signin_client_builder.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "components/gcm_driver/fake_gcm_driver.h"
15 #include "components/gcm_driver/gcm_driver.h"
16 #include "components/invalidation/gcm_invalidation_bridge.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "google_apis/gaia/fake_identity_provider.h"
19 #include "google_apis/gaia/google_service_auth_error.h"
20 #include "net/base/ip_endpoint.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 namespace invalidation {
24 namespace {
26 // Implementation of GCMDriver::Register that always succeeds with the same
27 // registrationId.
28 class CustomFakeGCMDriver : public gcm::FakeGCMDriver {
29 public:
30 CustomFakeGCMDriver() {}
31 ~CustomFakeGCMDriver() override {}
33 protected:
34 // FakeGCMDriver override:
35 void RegisterImpl(const std::string& app_id,
36 const std::vector<std::string>& sender_ids) override {
37 base::MessageLoop::current()->PostTask(
38 FROM_HERE,
39 base::Bind(&CustomFakeGCMDriver::RegisterFinished,
40 base::Unretained(this),
41 app_id,
42 std::string("registration.id"),
43 gcm::GCMClient::SUCCESS));
46 private:
47 DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver);
50 class GCMInvalidationBridgeTest : public ::testing::Test {
51 protected:
52 GCMInvalidationBridgeTest()
53 : connection_online_(false) {}
55 ~GCMInvalidationBridgeTest() override {}
57 void SetUp() override {
58 TestingProfile::Builder builder;
59 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
60 &BuildAutoIssuingFakeProfileOAuth2TokenService);
61 builder.AddTestingFactory(AccountTrackerServiceFactory::GetInstance(),
62 FakeAccountTrackerService::Build);
63 builder.AddTestingFactory(ChromeSigninClientFactory::GetInstance(),
64 signin::BuildTestSigninClient);
65 profile_ = builder.Build();
67 FakeProfileOAuth2TokenService* token_service =
68 (FakeProfileOAuth2TokenService*)
69 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get());
70 token_service->IssueRefreshTokenForUser("", "fake_refresh_token");
71 gcm_driver_.reset(new CustomFakeGCMDriver());
73 identity_provider_.reset(new FakeIdentityProvider(token_service));
74 bridge_.reset(new GCMInvalidationBridge(gcm_driver_.get(),
75 identity_provider_.get()));
77 delegate_ = bridge_->CreateDelegate();
78 delegate_->Initialize(
79 base::Bind(&GCMInvalidationBridgeTest::ConnectionStateChanged,
80 base::Unretained(this)));
81 RunLoop();
84 void RunLoop() {
85 base::RunLoop run_loop;
86 run_loop.RunUntilIdle();
89 public:
90 void RegisterFinished(const std::string& registration_id,
91 gcm::GCMClient::Result result) {
92 registration_id_ = registration_id;
95 void RequestTokenFinished(const GoogleServiceAuthError& error,
96 const std::string& token) {
97 issued_tokens_.push_back(token);
98 request_token_errors_.push_back(error);
101 void ConnectionStateChanged(bool online) {
102 connection_online_ = online;
105 content::TestBrowserThreadBundle thread_bundle_;
106 scoped_ptr<Profile> profile_;
107 scoped_ptr<gcm::GCMDriver> gcm_driver_;
108 scoped_ptr<FakeIdentityProvider> identity_provider_;
110 std::vector<std::string> issued_tokens_;
111 std::vector<GoogleServiceAuthError> request_token_errors_;
112 std::string registration_id_;
113 bool connection_online_;
115 scoped_ptr<GCMInvalidationBridge> bridge_;
116 scoped_ptr<syncer::GCMNetworkChannelDelegate> delegate_;
119 TEST_F(GCMInvalidationBridgeTest, RequestToken) {
120 // Make sure that call to RequestToken reaches OAuth2TokenService and gets
121 // back to callback.
122 delegate_->RequestToken(
123 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished,
124 base::Unretained(this)));
125 RunLoop();
126 EXPECT_EQ(1U, issued_tokens_.size());
127 EXPECT_NE("", issued_tokens_[0]);
128 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[0]);
131 TEST_F(GCMInvalidationBridgeTest, RequestTokenTwoConcurrentRequests) {
132 // First call should finish with REQUEST_CANCELLED error.
133 delegate_->RequestToken(
134 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished,
135 base::Unretained(this)));
136 // Second request should succeed.
137 delegate_->RequestToken(
138 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished,
139 base::Unretained(this)));
140 RunLoop();
142 EXPECT_EQ(2U, issued_tokens_.size());
144 EXPECT_EQ("", issued_tokens_[0]);
145 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED,
146 request_token_errors_[0].state());
148 EXPECT_NE("", issued_tokens_[1]);
149 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[1]);
152 TEST_F(GCMInvalidationBridgeTest, Register) {
153 EXPECT_TRUE(registration_id_.empty());
154 delegate_->Register(base::Bind(&GCMInvalidationBridgeTest::RegisterFinished,
155 base::Unretained(this)));
156 RunLoop();
158 EXPECT_FALSE(registration_id_.empty());
161 TEST_F(GCMInvalidationBridgeTest, ConnectionState) {
162 EXPECT_FALSE(connection_online_);
163 bridge_->OnConnected(net::IPEndPoint());
164 RunLoop();
165 EXPECT_TRUE(connection_online_);
166 bridge_->OnDisconnected();
167 RunLoop();
168 EXPECT_FALSE(connection_online_);
171 } // namespace
172 } // namespace invalidation