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 "chrome/browser/services/gcm/gcm_profile_service.h"
10 #include "base/bind_helpers.h"
11 #include "base/callback.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/run_loop.h"
14 #include "chrome/browser/services/gcm/fake_gcm_client_factory.h"
15 #include "chrome/browser/services/gcm/fake_signin_manager.h"
16 #include "chrome/browser/services/gcm/gcm_client_factory.h"
17 #include "chrome/browser/services/gcm/gcm_client_mock.h"
18 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
19 #include "chrome/browser/signin/signin_manager_factory.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "components/user_prefs/pref_registry_syncable.h"
22 #include "content/public/browser/browser_context.h"
23 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "google_apis/gcm/gcm_client.h"
25 #include "testing/gtest/include/gtest/gtest.h"
31 const char kTestAccountID
[] = "user@example.com";
32 const char kTestAppID
[] = "TestApp";
33 const char kUserID
[] = "user";
35 KeyedService
* BuildGCMProfileService(content::BrowserContext
* context
) {
36 return new GCMProfileService(Profile::FromBrowserContext(context
));
41 class GCMProfileServiceTest
: public testing::Test
{
43 GCMProfileServiceTest();
44 virtual ~GCMProfileServiceTest();
47 virtual void SetUp() OVERRIDE
;
49 GCMClientMock
* GetGCMClient() const;
51 void RegisterAndWaitForCompletion(const std::vector
<std::string
>& sender_ids
);
52 void SendAndWaitForCompletion(const GCMClient::OutgoingMessage
& message
);
54 void RegisterCompleted(const base::Closure
& callback
,
55 const std::string
& registration_id
,
56 GCMClient::Result result
);
57 void SendCompleted(const base::Closure
& callback
,
58 const std::string
& message_id
,
59 GCMClient::Result result
);
61 content::TestBrowserThreadBundle thread_bundle_
;
62 scoped_ptr
<TestingProfile
> profile_
;
63 GCMProfileService
* gcm_profile_service_
;
65 std::string registration_id_
;
66 GCMClient::Result registration_result_
;
67 std::string send_message_id_
;
68 GCMClient::Result send_result_
;
71 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTest
);
74 GCMProfileServiceTest::GCMProfileServiceTest()
75 : gcm_profile_service_(NULL
),
76 registration_result_(GCMClient::UNKNOWN_ERROR
),
77 send_result_(GCMClient::UNKNOWN_ERROR
) {
80 GCMProfileServiceTest::~GCMProfileServiceTest() {
83 GCMClientMock
* GCMProfileServiceTest::GetGCMClient() const {
84 return static_cast<GCMClientMock
*>(
85 gcm_profile_service_
->GetGCMClientForTesting());
88 void GCMProfileServiceTest::SetUp() {
89 TestingProfile::Builder builder
;
90 builder
.AddTestingFactory(SigninManagerFactory::GetInstance(),
91 FakeSigninManager::Build
);
92 profile_
= builder
.Build();
94 gcm_profile_service_
= static_cast<GCMProfileService
*>(
95 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
97 &BuildGCMProfileService
));
98 gcm_profile_service_
->Initialize(scoped_ptr
<GCMClientFactory
>(
99 new FakeGCMClientFactory(GCMClientMock::NO_DELAY_LOADING
)));
101 FakeSigninManager
* signin_manager
= static_cast<FakeSigninManager
*>(
102 SigninManagerFactory::GetInstance()->GetForProfile(profile_
.get()));
103 signin_manager
->SignIn(kTestAccountID
);
104 base::RunLoop().RunUntilIdle();
107 void GCMProfileServiceTest::RegisterAndWaitForCompletion(
108 const std::vector
<std::string
>& sender_ids
) {
109 base::RunLoop run_loop
;
110 gcm_profile_service_
->Register(
113 base::Bind(&GCMProfileServiceTest::RegisterCompleted
,
114 base::Unretained(this),
115 run_loop
.QuitClosure()));
119 void GCMProfileServiceTest::SendAndWaitForCompletion(
120 const GCMClient::OutgoingMessage
& message
) {
121 base::RunLoop run_loop
;
122 gcm_profile_service_
->Send(kTestAppID
,
125 base::Bind(&GCMProfileServiceTest::SendCompleted
,
126 base::Unretained(this),
127 run_loop
.QuitClosure()));
131 void GCMProfileServiceTest::RegisterCompleted(
132 const base::Closure
& callback
,
133 const std::string
& registration_id
,
134 GCMClient::Result result
) {
135 registration_id_
= registration_id
;
136 registration_result_
= result
;
140 void GCMProfileServiceTest::SendCompleted(
141 const base::Closure
& callback
,
142 const std::string
& message_id
,
143 GCMClient::Result result
) {
144 send_message_id_
= message_id
;
145 send_result_
= result
;
149 TEST_F(GCMProfileServiceTest
, RegisterUnderNeutralChannelSignal
) {
150 // GCMClient should not be checked in.
151 EXPECT_FALSE(gcm_profile_service_
->IsGCMClientReady());
152 EXPECT_EQ(GCMClientMock::UNINITIALIZED
, GetGCMClient()->status());
154 // Invoking register will make GCMClient checked in.
155 std::vector
<std::string
> sender_ids
;
156 sender_ids
.push_back("sender");
157 RegisterAndWaitForCompletion(sender_ids
);
159 // GCMClient should be checked in.
160 EXPECT_TRUE(gcm_profile_service_
->IsGCMClientReady());
161 EXPECT_EQ(GCMClientMock::LOADED
, GetGCMClient()->status());
163 // Registration should succeed.
164 std::string expected_registration_id
=
165 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids
);
166 EXPECT_EQ(expected_registration_id
, registration_id_
);
167 EXPECT_EQ(GCMClient::SUCCESS
, registration_result_
);
170 TEST_F(GCMProfileServiceTest
, SendUnderNeutralChannelSignal
) {
171 // GCMClient should not be checked in.
172 EXPECT_FALSE(gcm_profile_service_
->IsGCMClientReady());
173 EXPECT_EQ(GCMClientMock::UNINITIALIZED
, GetGCMClient()->status());
175 // Invoking send will make GCMClient checked in.
176 GCMClient::OutgoingMessage message
;
178 message
.data
["key1"] = "value1";
179 SendAndWaitForCompletion( message
);
181 // GCMClient should be checked in.
182 EXPECT_TRUE(gcm_profile_service_
->IsGCMClientReady());
183 EXPECT_EQ(GCMClientMock::LOADED
, GetGCMClient()->status());
185 // Sending should succeed.
186 EXPECT_EQ(message
.id
, send_message_id_
);
187 EXPECT_EQ(GCMClient::SUCCESS
, send_result_
);