Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / invalidation / gcm_invalidation_bridge_unittest.cc
blobcd91402a38183d59b254e0020a88aad6b836ac5d
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/location.h"
6 #include "base/run_loop.h"
7 #include "base/single_thread_task_runner.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "chrome/browser/signin/account_fetcher_service_factory.h"
10 #include "chrome/browser/signin/chrome_signin_client_factory.h"
11 #include "chrome/browser/signin/fake_account_fetcher_service.h"
12 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
14 #include "chrome/browser/signin/test_signin_client_builder.h"
15 #include "chrome/test/base/testing_profile.h"
16 #include "components/gcm_driver/fake_gcm_driver.h"
17 #include "components/gcm_driver/gcm_driver.h"
18 #include "components/invalidation/impl/gcm_invalidation_bridge.h"
19 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "google_apis/gaia/fake_identity_provider.h"
22 #include "google_apis/gaia/google_service_auth_error.h"
23 #include "net/base/ip_endpoint.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 namespace invalidation {
27 namespace {
29 // Implementation of GCMDriver::Register that always succeeds with the same
30 // registrationId.
31 class CustomFakeGCMDriver : public gcm::FakeGCMDriver {
32 public:
33 CustomFakeGCMDriver() {}
34 ~CustomFakeGCMDriver() override {}
36 protected:
37 // FakeGCMDriver override:
38 void RegisterImpl(const std::string& app_id,
39 const std::vector<std::string>& sender_ids) override {
40 base::ThreadTaskRunnerHandle::Get()->PostTask(
41 FROM_HERE,
42 base::Bind(&CustomFakeGCMDriver::RegisterFinished,
43 base::Unretained(this), app_id,
44 std::string("registration.id"), gcm::GCMClient::SUCCESS));
47 private:
48 DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver);
51 class GCMInvalidationBridgeTest : public ::testing::Test {
52 protected:
53 GCMInvalidationBridgeTest()
54 : connection_online_(false) {}
56 ~GCMInvalidationBridgeTest() override {}
58 void SetUp() override {
59 TestingProfile::Builder builder;
60 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
61 &BuildAutoIssuingFakeProfileOAuth2TokenService);
62 builder.AddTestingFactory(AccountFetcherServiceFactory::GetInstance(),
63 FakeAccountFetcherService::BuildForTests);
64 builder.AddTestingFactory(ChromeSigninClientFactory::GetInstance(),
65 signin::BuildTestSigninClient);
66 profile_ = builder.Build();
68 FakeProfileOAuth2TokenService* token_service =
69 (FakeProfileOAuth2TokenService*)
70 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get());
71 token_service->UpdateCredentials("", "fake_refresh_token");
72 gcm_driver_.reset(new CustomFakeGCMDriver());
74 identity_provider_.reset(new FakeIdentityProvider(token_service));
75 bridge_.reset(new GCMInvalidationBridge(gcm_driver_.get(),
76 identity_provider_.get()));
78 delegate_ = bridge_->CreateDelegate();
79 delegate_->Initialize(
80 base::Bind(&GCMInvalidationBridgeTest::ConnectionStateChanged,
81 base::Unretained(this)));
82 RunLoop();
85 void RunLoop() {
86 base::RunLoop run_loop;
87 run_loop.RunUntilIdle();
90 public:
91 void RegisterFinished(const std::string& registration_id,
92 gcm::GCMClient::Result result) {
93 registration_id_ = registration_id;
96 void RequestTokenFinished(const GoogleServiceAuthError& error,
97 const std::string& token) {
98 issued_tokens_.push_back(token);
99 request_token_errors_.push_back(error);
102 void ConnectionStateChanged(bool online) {
103 connection_online_ = online;
106 content::TestBrowserThreadBundle thread_bundle_;
107 scoped_ptr<Profile> profile_;
108 scoped_ptr<gcm::GCMDriver> gcm_driver_;
109 scoped_ptr<FakeIdentityProvider> identity_provider_;
111 std::vector<std::string> issued_tokens_;
112 std::vector<GoogleServiceAuthError> request_token_errors_;
113 std::string registration_id_;
114 bool connection_online_;
116 scoped_ptr<GCMInvalidationBridge> bridge_;
117 scoped_ptr<syncer::GCMNetworkChannelDelegate> delegate_;
120 TEST_F(GCMInvalidationBridgeTest, RequestToken) {
121 // Make sure that call to RequestToken reaches OAuth2TokenService and gets
122 // back to callback.
123 delegate_->RequestToken(
124 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished,
125 base::Unretained(this)));
126 RunLoop();
127 EXPECT_EQ(1U, issued_tokens_.size());
128 EXPECT_NE("", issued_tokens_[0]);
129 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[0]);
132 TEST_F(GCMInvalidationBridgeTest, RequestTokenTwoConcurrentRequests) {
133 // First call should finish with REQUEST_CANCELLED error.
134 delegate_->RequestToken(
135 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished,
136 base::Unretained(this)));
137 // Second request should succeed.
138 delegate_->RequestToken(
139 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished,
140 base::Unretained(this)));
141 RunLoop();
143 EXPECT_EQ(2U, issued_tokens_.size());
145 EXPECT_EQ("", issued_tokens_[0]);
146 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED,
147 request_token_errors_[0].state());
149 EXPECT_NE("", issued_tokens_[1]);
150 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[1]);
153 TEST_F(GCMInvalidationBridgeTest, Register) {
154 EXPECT_TRUE(registration_id_.empty());
155 delegate_->Register(base::Bind(&GCMInvalidationBridgeTest::RegisterFinished,
156 base::Unretained(this)));
157 RunLoop();
159 EXPECT_FALSE(registration_id_.empty());
162 TEST_F(GCMInvalidationBridgeTest, ConnectionState) {
163 EXPECT_FALSE(connection_online_);
164 bridge_->OnConnected(net::IPEndPoint());
165 RunLoop();
166 EXPECT_TRUE(connection_online_);
167 bridge_->OnDisconnected();
168 RunLoop();
169 EXPECT_FALSE(connection_online_);
172 } // namespace
173 } // namespace invalidation