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_builder.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_account_fetcher_service.h"
20 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "google_apis/gaia/fake_identity_provider.h"
23 #include "google_apis/gaia/google_service_auth_error.h"
24 #include "net/base/ip_endpoint.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 namespace invalidation
{
30 // Implementation of GCMDriver::Register that always succeeds with the same
32 class CustomFakeGCMDriver
: public gcm::FakeGCMDriver
{
34 CustomFakeGCMDriver() {}
35 ~CustomFakeGCMDriver() override
{}
38 // FakeGCMDriver override:
39 void RegisterImpl(const std::string
& app_id
,
40 const std::vector
<std::string
>& sender_ids
) override
{
41 base::ThreadTaskRunnerHandle::Get()->PostTask(
43 base::Bind(&CustomFakeGCMDriver::RegisterFinished
,
44 base::Unretained(this), app_id
,
45 std::string("registration.id"), gcm::GCMClient::SUCCESS
));
49 DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver
);
52 class GCMInvalidationBridgeTest
: public ::testing::Test
{
54 GCMInvalidationBridgeTest()
55 : connection_online_(false) {}
57 ~GCMInvalidationBridgeTest() override
{}
59 void SetUp() override
{
60 TestingProfile::Builder builder
;
61 builder
.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
62 &BuildAutoIssuingFakeProfileOAuth2TokenService
);
63 builder
.AddTestingFactory(AccountFetcherServiceFactory::GetInstance(),
64 FakeAccountFetcherServiceBuilder::BuildForTests
);
65 builder
.AddTestingFactory(ChromeSigninClientFactory::GetInstance(),
66 signin::BuildTestSigninClient
);
67 profile_
= builder
.Build();
69 FakeProfileOAuth2TokenService
* token_service
=
70 (FakeProfileOAuth2TokenService
*)
71 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
.get());
72 token_service
->UpdateCredentials("", "fake_refresh_token");
73 gcm_driver_
.reset(new CustomFakeGCMDriver());
75 identity_provider_
.reset(new FakeIdentityProvider(token_service
));
76 bridge_
.reset(new GCMInvalidationBridge(gcm_driver_
.get(),
77 identity_provider_
.get()));
79 delegate_
= bridge_
->CreateDelegate();
80 delegate_
->Initialize(
81 base::Bind(&GCMInvalidationBridgeTest::ConnectionStateChanged
,
82 base::Unretained(this)));
87 base::RunLoop run_loop
;
88 run_loop
.RunUntilIdle();
92 void RegisterFinished(const std::string
& registration_id
,
93 gcm::GCMClient::Result result
) {
94 registration_id_
= registration_id
;
97 void RequestTokenFinished(const GoogleServiceAuthError
& error
,
98 const std::string
& token
) {
99 issued_tokens_
.push_back(token
);
100 request_token_errors_
.push_back(error
);
103 void ConnectionStateChanged(bool online
) {
104 connection_online_
= online
;
107 content::TestBrowserThreadBundle thread_bundle_
;
108 scoped_ptr
<Profile
> profile_
;
109 scoped_ptr
<gcm::GCMDriver
> gcm_driver_
;
110 scoped_ptr
<FakeIdentityProvider
> identity_provider_
;
112 std::vector
<std::string
> issued_tokens_
;
113 std::vector
<GoogleServiceAuthError
> request_token_errors_
;
114 std::string registration_id_
;
115 bool connection_online_
;
117 scoped_ptr
<GCMInvalidationBridge
> bridge_
;
118 scoped_ptr
<syncer::GCMNetworkChannelDelegate
> delegate_
;
121 TEST_F(GCMInvalidationBridgeTest
, RequestToken
) {
122 // Make sure that call to RequestToken reaches OAuth2TokenService and gets
124 delegate_
->RequestToken(
125 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished
,
126 base::Unretained(this)));
128 EXPECT_EQ(1U, issued_tokens_
.size());
129 EXPECT_NE("", issued_tokens_
[0]);
130 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_
[0]);
133 TEST_F(GCMInvalidationBridgeTest
, RequestTokenTwoConcurrentRequests
) {
134 // First call should finish with REQUEST_CANCELLED error.
135 delegate_
->RequestToken(
136 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished
,
137 base::Unretained(this)));
138 // Second request should succeed.
139 delegate_
->RequestToken(
140 base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished
,
141 base::Unretained(this)));
144 EXPECT_EQ(2U, issued_tokens_
.size());
146 EXPECT_EQ("", issued_tokens_
[0]);
147 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED
,
148 request_token_errors_
[0].state());
150 EXPECT_NE("", issued_tokens_
[1]);
151 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_
[1]);
154 TEST_F(GCMInvalidationBridgeTest
, Register
) {
155 EXPECT_TRUE(registration_id_
.empty());
156 delegate_
->Register(base::Bind(&GCMInvalidationBridgeTest::RegisterFinished
,
157 base::Unretained(this)));
160 EXPECT_FALSE(registration_id_
.empty());
163 TEST_F(GCMInvalidationBridgeTest
, ConnectionState
) {
164 EXPECT_FALSE(connection_online_
);
165 bridge_
->OnConnected(net::IPEndPoint());
167 EXPECT_TRUE(connection_online_
);
168 bridge_
->OnDisconnected();
170 EXPECT_FALSE(connection_online_
);
174 } // namespace invalidation