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/gcm_profile_service_factory.h"
15 #include "chrome/test/base/testing_profile.h"
16 #if defined(OS_CHROMEOS)
17 #include "chromeos/dbus/dbus_thread_manager.h"
19 #include "components/gcm_driver/fake_gcm_app_handler.h"
20 #include "components/gcm_driver/fake_gcm_client.h"
21 #include "components/gcm_driver/fake_gcm_client_factory.h"
22 #include "components/gcm_driver/gcm_client.h"
23 #include "components/gcm_driver/gcm_client_factory.h"
24 #include "components/gcm_driver/gcm_driver.h"
25 #include "components/pref_registry/pref_registry_syncable.h"
26 #include "content/public/browser/browser_context.h"
27 #include "content/public/browser/browser_thread.h"
28 #include "content/public/test/test_browser_thread_bundle.h"
29 #include "testing/gtest/include/gtest/gtest.h"
35 const char kTestAppID
[] = "TestApp";
36 const char kUserID
[] = "user";
38 scoped_ptr
<KeyedService
> BuildGCMProfileService(
39 content::BrowserContext
* context
) {
40 return make_scoped_ptr(new GCMProfileService(
41 Profile::FromBrowserContext(context
),
42 scoped_ptr
<GCMClientFactory
>(new FakeGCMClientFactory(
43 content::BrowserThread::GetMessageLoopProxyForThread(
44 content::BrowserThread::UI
),
45 content::BrowserThread::GetMessageLoopProxyForThread(
46 content::BrowserThread::IO
)))));
51 class GCMProfileServiceTest
: public testing::Test
{
53 GCMProfileServiceTest();
54 ~GCMProfileServiceTest() override
;
57 void SetUp() override
;
58 void TearDown() override
;
60 FakeGCMClient
* GetGCMClient() const;
62 void CreateGCMProfileService();
64 void RegisterAndWaitForCompletion(const std::vector
<std::string
>& sender_ids
);
65 void UnregisterAndWaitForCompletion();
66 void SendAndWaitForCompletion(const OutgoingMessage
& message
);
68 void RegisterCompleted(const base::Closure
& callback
,
69 const std::string
& registration_id
,
70 GCMClient::Result result
);
71 void UnregisterCompleted(const base::Closure
& callback
,
72 GCMClient::Result result
);
73 void SendCompleted(const base::Closure
& callback
,
74 const std::string
& message_id
,
75 GCMClient::Result result
);
77 GCMDriver
* driver() const { return gcm_profile_service_
->driver(); }
78 std::string
registration_id() const { return registration_id_
; }
79 GCMClient::Result
registration_result() const { return registration_result_
; }
80 GCMClient::Result
unregistration_result() const {
81 return unregistration_result_
;
83 std::string
send_message_id() const { return send_message_id_
; }
84 GCMClient::Result
send_result() const { return send_result_
; }
87 content::TestBrowserThreadBundle thread_bundle_
;
88 scoped_ptr
<TestingProfile
> profile_
;
89 GCMProfileService
* gcm_profile_service_
;
90 scoped_ptr
<FakeGCMAppHandler
> gcm_app_handler_
;
92 std::string registration_id_
;
93 GCMClient::Result registration_result_
;
94 GCMClient::Result unregistration_result_
;
95 std::string send_message_id_
;
96 GCMClient::Result send_result_
;
98 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTest
);
101 GCMProfileServiceTest::GCMProfileServiceTest()
102 : gcm_profile_service_(NULL
),
103 gcm_app_handler_(new FakeGCMAppHandler
),
104 registration_result_(GCMClient::UNKNOWN_ERROR
),
105 send_result_(GCMClient::UNKNOWN_ERROR
) {
108 GCMProfileServiceTest::~GCMProfileServiceTest() {
111 FakeGCMClient
* GCMProfileServiceTest::GetGCMClient() const {
112 return static_cast<FakeGCMClient
*>(
113 gcm_profile_service_
->driver()->GetGCMClientForTesting());
116 void GCMProfileServiceTest::SetUp() {
117 #if defined(OS_CHROMEOS)
118 // Create a DBus thread manager setter for its side effect.
119 // Ignore the return value.
120 chromeos::DBusThreadManager::GetSetterForTesting();
122 TestingProfile::Builder builder
;
123 profile_
= builder
.Build();
126 void GCMProfileServiceTest::TearDown() {
127 gcm_profile_service_
->driver()->RemoveAppHandler(kTestAppID
);
130 void GCMProfileServiceTest::CreateGCMProfileService() {
131 gcm_profile_service_
= static_cast<GCMProfileService
*>(
132 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
134 &BuildGCMProfileService
));
135 gcm_profile_service_
->driver()->AddAppHandler(
136 kTestAppID
, gcm_app_handler_
.get());
139 void GCMProfileServiceTest::RegisterAndWaitForCompletion(
140 const std::vector
<std::string
>& sender_ids
) {
141 base::RunLoop run_loop
;
142 gcm_profile_service_
->driver()->Register(
145 base::Bind(&GCMProfileServiceTest::RegisterCompleted
,
146 base::Unretained(this),
147 run_loop
.QuitClosure()));
151 void GCMProfileServiceTest::UnregisterAndWaitForCompletion() {
152 base::RunLoop run_loop
;
153 gcm_profile_service_
->driver()->Unregister(
155 base::Bind(&GCMProfileServiceTest::UnregisterCompleted
,
156 base::Unretained(this),
157 run_loop
.QuitClosure()));
161 void GCMProfileServiceTest::SendAndWaitForCompletion(
162 const OutgoingMessage
& message
) {
163 base::RunLoop run_loop
;
164 gcm_profile_service_
->driver()->Send(
168 base::Bind(&GCMProfileServiceTest::SendCompleted
,
169 base::Unretained(this),
170 run_loop
.QuitClosure()));
174 void GCMProfileServiceTest::RegisterCompleted(
175 const base::Closure
& callback
,
176 const std::string
& registration_id
,
177 GCMClient::Result result
) {
178 registration_id_
= registration_id
;
179 registration_result_
= result
;
183 void GCMProfileServiceTest::UnregisterCompleted(
184 const base::Closure
& callback
,
185 GCMClient::Result result
) {
186 unregistration_result_
= result
;
190 void GCMProfileServiceTest::SendCompleted(
191 const base::Closure
& callback
,
192 const std::string
& message_id
,
193 GCMClient::Result result
) {
194 send_message_id_
= message_id
;
195 send_result_
= result
;
199 TEST_F(GCMProfileServiceTest
, RegisterAndUnregister
) {
200 CreateGCMProfileService();
202 std::vector
<std::string
> sender_ids
;
203 sender_ids
.push_back("sender");
204 RegisterAndWaitForCompletion(sender_ids
);
206 std::string expected_registration_id
=
207 FakeGCMClient::GenerateGCMRegistrationID(sender_ids
);
208 EXPECT_EQ(expected_registration_id
, registration_id());
209 EXPECT_EQ(GCMClient::SUCCESS
, registration_result());
211 UnregisterAndWaitForCompletion();
212 EXPECT_EQ(GCMClient::SUCCESS
, unregistration_result());
215 TEST_F(GCMProfileServiceTest
, Send
) {
216 CreateGCMProfileService();
218 OutgoingMessage message
;
220 message
.data
["key1"] = "value1";
221 SendAndWaitForCompletion(message
);
223 EXPECT_EQ(message
.id
, send_message_id());
224 EXPECT_EQ(GCMClient::SUCCESS
, send_result());