Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / services / gcm / gcm_profile_service_unittest.cc
blob2b1499a636811678df1916e2f33d7b9eb0057136
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"
7 #include <vector>
9 #include "base/bind.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"
18 #endif
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"
31 namespace gcm {
33 namespace {
35 const char kTestAppID[] = "TestApp";
36 const char kUserID[] = "user";
38 KeyedService* BuildGCMProfileService(content::BrowserContext* context) {
39 return new GCMProfileService(
40 Profile::FromBrowserContext(context),
41 scoped_ptr<GCMClientFactory>(new FakeGCMClientFactory(
42 content::BrowserThread::GetMessageLoopProxyForThread(
43 content::BrowserThread::UI),
44 content::BrowserThread::GetMessageLoopProxyForThread(
45 content::BrowserThread::IO))));
48 } // namespace
50 class GCMProfileServiceTest : public testing::Test {
51 protected:
52 GCMProfileServiceTest();
53 ~GCMProfileServiceTest() override;
55 // testing::Test:
56 void SetUp() override;
57 void TearDown() override;
59 FakeGCMClient* GetGCMClient() const;
61 void CreateGCMProfileService();
63 void RegisterAndWaitForCompletion(const std::vector<std::string>& sender_ids);
64 void UnregisterAndWaitForCompletion();
65 void SendAndWaitForCompletion(const GCMClient::OutgoingMessage& message);
67 void RegisterCompleted(const base::Closure& callback,
68 const std::string& registration_id,
69 GCMClient::Result result);
70 void UnregisterCompleted(const base::Closure& callback,
71 GCMClient::Result result);
72 void SendCompleted(const base::Closure& callback,
73 const std::string& message_id,
74 GCMClient::Result result);
76 GCMDriver* driver() const { return gcm_profile_service_->driver(); }
77 std::string registration_id() const { return registration_id_; }
78 GCMClient::Result registration_result() const { return registration_result_; }
79 GCMClient::Result unregistration_result() const {
80 return unregistration_result_;
82 std::string send_message_id() const { return send_message_id_; }
83 GCMClient::Result send_result() const { return send_result_; }
85 private:
86 content::TestBrowserThreadBundle thread_bundle_;
87 scoped_ptr<TestingProfile> profile_;
88 GCMProfileService* gcm_profile_service_;
89 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_;
91 std::string registration_id_;
92 GCMClient::Result registration_result_;
93 GCMClient::Result unregistration_result_;
94 std::string send_message_id_;
95 GCMClient::Result send_result_;
97 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTest);
100 GCMProfileServiceTest::GCMProfileServiceTest()
101 : gcm_profile_service_(NULL),
102 gcm_app_handler_(new FakeGCMAppHandler),
103 registration_result_(GCMClient::UNKNOWN_ERROR),
104 send_result_(GCMClient::UNKNOWN_ERROR) {
107 GCMProfileServiceTest::~GCMProfileServiceTest() {
110 FakeGCMClient* GCMProfileServiceTest::GetGCMClient() const {
111 return static_cast<FakeGCMClient*>(
112 gcm_profile_service_->driver()->GetGCMClientForTesting());
115 void GCMProfileServiceTest::SetUp() {
116 #if defined(OS_CHROMEOS)
117 // Create a DBus thread manager setter for its side effect.
118 // Ignore the return value.
119 chromeos::DBusThreadManager::GetSetterForTesting();
120 #endif
121 TestingProfile::Builder builder;
122 profile_ = builder.Build();
125 void GCMProfileServiceTest::TearDown() {
126 gcm_profile_service_->driver()->RemoveAppHandler(kTestAppID);
129 void GCMProfileServiceTest::CreateGCMProfileService() {
130 gcm_profile_service_ = static_cast<GCMProfileService*>(
131 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
132 profile_.get(),
133 &BuildGCMProfileService));
134 gcm_profile_service_->driver()->AddAppHandler(
135 kTestAppID, gcm_app_handler_.get());
138 void GCMProfileServiceTest::RegisterAndWaitForCompletion(
139 const std::vector<std::string>& sender_ids) {
140 base::RunLoop run_loop;
141 gcm_profile_service_->driver()->Register(
142 kTestAppID,
143 sender_ids,
144 base::Bind(&GCMProfileServiceTest::RegisterCompleted,
145 base::Unretained(this),
146 run_loop.QuitClosure()));
147 run_loop.Run();
150 void GCMProfileServiceTest::UnregisterAndWaitForCompletion() {
151 base::RunLoop run_loop;
152 gcm_profile_service_->driver()->Unregister(
153 kTestAppID,
154 base::Bind(&GCMProfileServiceTest::UnregisterCompleted,
155 base::Unretained(this),
156 run_loop.QuitClosure()));
157 run_loop.Run();
160 void GCMProfileServiceTest::SendAndWaitForCompletion(
161 const GCMClient::OutgoingMessage& message) {
162 base::RunLoop run_loop;
163 gcm_profile_service_->driver()->Send(
164 kTestAppID,
165 kUserID,
166 message,
167 base::Bind(&GCMProfileServiceTest::SendCompleted,
168 base::Unretained(this),
169 run_loop.QuitClosure()));
170 run_loop.Run();
173 void GCMProfileServiceTest::RegisterCompleted(
174 const base::Closure& callback,
175 const std::string& registration_id,
176 GCMClient::Result result) {
177 registration_id_ = registration_id;
178 registration_result_ = result;
179 callback.Run();
182 void GCMProfileServiceTest::UnregisterCompleted(
183 const base::Closure& callback,
184 GCMClient::Result result) {
185 unregistration_result_ = result;
186 callback.Run();
189 void GCMProfileServiceTest::SendCompleted(
190 const base::Closure& callback,
191 const std::string& message_id,
192 GCMClient::Result result) {
193 send_message_id_ = message_id;
194 send_result_ = result;
195 callback.Run();
198 TEST_F(GCMProfileServiceTest, RegisterAndUnregister) {
199 CreateGCMProfileService();
201 std::vector<std::string> sender_ids;
202 sender_ids.push_back("sender");
203 RegisterAndWaitForCompletion(sender_ids);
205 std::string expected_registration_id =
206 GetGCMClient()->GetRegistrationIdFromSenderIds(sender_ids);
207 EXPECT_EQ(expected_registration_id, registration_id());
208 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
210 UnregisterAndWaitForCompletion();
211 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
214 TEST_F(GCMProfileServiceTest, Send) {
215 CreateGCMProfileService();
217 GCMClient::OutgoingMessage message;
218 message.id = "1";
219 message.data["key1"] = "value1";
220 SendAndWaitForCompletion( message);
222 EXPECT_EQ(message.id, send_message_id());
223 EXPECT_EQ(GCMClient::SUCCESS, send_result());
226 } // namespace gcm