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/fake_profile_oauth2_token_service.h"
7 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
8 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "components/gcm_driver/fake_gcm_driver.h"
11 #include "components/gcm_driver/gcm_driver.h"
12 #include "components/invalidation/gcm_invalidation_bridge.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "google_apis/gaia/fake_identity_provider.h"
15 #include "google_apis/gaia/google_service_auth_error.h"
16 #include "net/base/ip_endpoint.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace invalidation {
22 // Implementation of GCMDriver::Register that always succeeds with the same
24 class CustomFakeGCMDriver : public gcm::FakeGCMDriver {
26 CustomFakeGCMDriver() {}
27 ~CustomFakeGCMDriver() override {}
30 // FakeGCMDriver override:
31 void RegisterImpl(const std::string& app_id,
32 const std::vector<std::string>& sender_ids) override {
33 base::MessageLoop::current()->PostTask(
35 base::Bind(&CustomFakeGCMDriver::RegisterFinished,
36 base::Unretained(this),
38 std::string("registration.id"),
39 gcm::GCMClient::SUCCESS));
43 DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver);
46 class GCMInvalidationBridgeTest : public ::testing::Test {
48 GCMInvalidationBridgeTest()
49 : connection_online_(false) {}
51 ~GCMInvalidationBridgeTest() override {}
53 void SetUp() override {
54 TestingProfile::Builder builder;
55 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
56 &BuildAutoIssuingFakeProfileOAuth2TokenService);
57 profile_ = builder.Build();
59 FakeProfileOAuth2TokenService* token_service =
60 (FakeProfileOAuth2TokenService*)
61 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get());
62 token_service->IssueRefreshTokenForUser("", "fake_refresh_token");
63 gcm_driver_.reset(new CustomFakeGCMDriver());
65 identity_provider_.reset(new FakeIdentityProvider(token_service));
66 bridge_.reset(new GCMInvalidationBridge(gcm_driver_.get(),
67 identity_provider_.get()));
69 delegate_ = bridge_->CreateDelegate();
70 delegate_->Initialize(
71 base::Bind(&GCMInvalidationBridgeTest::ConnectionStateChanged,
72 base::Unretained(this)));
77 base::RunLoop run_loop;
78 run_loop.RunUntilIdle();
82 void RegisterFinished(const std::string& registration_id,
83 gcm::GCMClient::Result result) {
84 registration_id_ = registration_id;
87 void RequestTokenFinished(const GoogleServiceAuthError& error,
88 const std::string& token) {
89 issued_tokens_.push_back(token);
90 request_token_errors_.push_back(error);
93 void ConnectionStateChanged(bool online) {
94 connection_online_ = online;
97 content::TestBrowserThreadBundle thread_bundle_;
98 scoped_ptr<Profile> profile_;
99 scoped_ptr<gcm::GCMDriver> gcm_driver_;
100 scoped_ptr<FakeIdentityProvider> identity_provider_;
102 std::vector<std::string> issued_tokens_;
103 std::vector<GoogleServiceAuthError> request_token_errors_;
104 std::string registration_id_;
105 bool connection_online_;
107 scoped_ptr<GCMInvalidationBridge> bridge_;
108 scoped_ptr<syncer::GCMNetworkChannelDelegate> delegate_;
111 TEST_F(GCMInvalidationBridgeTest, RequestToken) {
112 // Make sure that call to RequestToken reaches OAuth2TokenService and gets
114 delegate_->RequestToken(
115 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished,
116 base::Unretained(this)));
118 EXPECT_EQ(1U, issued_tokens_.size());
119 EXPECT_NE("", issued_tokens_[0]);
120 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[0]);
123 TEST_F(GCMInvalidationBridgeTest, RequestTokenTwoConcurrentRequests) {
124 // First call should finish with REQUEST_CANCELLED error.
125 delegate_->RequestToken(
126 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished,
127 base::Unretained(this)));
128 // Second request should succeed.
129 delegate_->RequestToken(
130 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished,
131 base::Unretained(this)));
134 EXPECT_EQ(2U, issued_tokens_.size());
136 EXPECT_EQ("", issued_tokens_[0]);
137 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED,
138 request_token_errors_[0].state());
140 EXPECT_NE("", issued_tokens_[1]);
141 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[1]);
144 TEST_F(GCMInvalidationBridgeTest, Register) {
145 EXPECT_TRUE(registration_id_.empty());
146 delegate_->Register(base::Bind(&GCMInvalidationBridgeTest::RegisterFinished,
147 base::Unretained(this)));
150 EXPECT_FALSE(registration_id_.empty());
153 TEST_F(GCMInvalidationBridgeTest, ConnectionState) {
154 EXPECT_FALSE(connection_online_);
155 bridge_->OnConnected(net::IPEndPoint());
157 EXPECT_TRUE(connection_online_);
158 bridge_->OnDisconnected();
160 EXPECT_FALSE(connection_online_);
164 } // namespace invalidation