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
{
26 // Implementation of GCMDriver::Register that always succeeds with the same
28 class CustomFakeGCMDriver
: public gcm::FakeGCMDriver
{
30 CustomFakeGCMDriver() {}
31 ~CustomFakeGCMDriver() override
{}
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(
39 base::Bind(&CustomFakeGCMDriver::RegisterFinished
,
40 base::Unretained(this),
42 std::string("registration.id"),
43 gcm::GCMClient::SUCCESS
));
47 DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver
);
50 class GCMInvalidationBridgeTest
: public ::testing::Test
{
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)));
85 base::RunLoop run_loop
;
86 run_loop
.RunUntilIdle();
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
122 delegate_
->RequestToken(
123 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished
,
124 base::Unretained(this)));
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)));
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)));
158 EXPECT_FALSE(registration_id_
.empty());
161 TEST_F(GCMInvalidationBridgeTest
, ConnectionState
) {
162 EXPECT_FALSE(connection_online_
);
163 bridge_
->OnConnected(net::IPEndPoint());
165 EXPECT_TRUE(connection_online_
);
166 bridge_
->OnDisconnected();
168 EXPECT_FALSE(connection_online_
);
172 } // namespace invalidation