OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_gcm_app_handler.h" | 5 #include "chrome/browser/extensions/extension_gcm_app_handler.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 scoped_ptr<base::RunLoop> run_loop_; | 120 scoped_ptr<base::RunLoop> run_loop_; |
121 | 121 |
122 DISALLOW_COPY_AND_ASSIGN(Waiter); | 122 DISALLOW_COPY_AND_ASSIGN(Waiter); |
123 }; | 123 }; |
124 | 124 |
125 class FakeExtensionGCMAppHandler : public ExtensionGCMAppHandler { | 125 class FakeExtensionGCMAppHandler : public ExtensionGCMAppHandler { |
126 public: | 126 public: |
127 FakeExtensionGCMAppHandler(Profile* profile, Waiter* waiter) | 127 FakeExtensionGCMAppHandler(Profile* profile, Waiter* waiter) |
128 : ExtensionGCMAppHandler(profile), | 128 : ExtensionGCMAppHandler(profile), |
129 waiter_(waiter), | 129 waiter_(waiter), |
130 unregistration_result_(gcm::GCMClient::UNKNOWN_ERROR) { | 130 unregistration_result_(gcm::RESULT_UNKNOWN_ERROR) { |
131 } | 131 } |
132 | 132 |
133 virtual ~FakeExtensionGCMAppHandler() { | 133 virtual ~FakeExtensionGCMAppHandler() { |
134 } | 134 } |
135 | 135 |
136 virtual void OnMessage( | 136 virtual void OnMessage( |
137 const std::string& app_id, | 137 const std::string& app_id, |
138 const gcm::GCMClient::IncomingMessage& message) OVERRIDE { | 138 const gcm::IncomingMessage& message) OVERRIDE { |
139 } | 139 } |
140 | 140 |
141 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE { | 141 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE { |
142 } | 142 } |
143 | 143 |
144 virtual void OnSendError( | 144 virtual void OnSendError( |
145 const std::string& app_id, | 145 const std::string& app_id, |
146 const gcm::GCMClient::SendErrorDetails& send_error_details) OVERRIDE { | 146 const gcm::SendErrorDetails& send_error_details) OVERRIDE { |
147 } | 147 } |
148 | 148 |
149 virtual void OnUnregisterCompleted(const std::string& app_id, | 149 virtual void OnUnregisterCompleted(const std::string& app_id, |
150 gcm::GCMClient::Result result) OVERRIDE { | 150 gcm::Result result) OVERRIDE { |
151 unregistration_result_ = result; | 151 unregistration_result_ = result; |
152 waiter_->SignalCompleted(); | 152 waiter_->SignalCompleted(); |
153 } | 153 } |
154 | 154 |
155 gcm::GCMClient::Result unregistration_result() const { | 155 gcm::Result unregistration_result() const { |
156 return unregistration_result_; | 156 return unregistration_result_; |
157 } | 157 } |
158 | 158 |
159 private: | 159 private: |
160 Waiter* waiter_; | 160 Waiter* waiter_; |
161 gcm::GCMClient::Result unregistration_result_; | 161 gcm::Result unregistration_result_; |
162 | 162 |
163 DISALLOW_COPY_AND_ASSIGN(FakeExtensionGCMAppHandler); | 163 DISALLOW_COPY_AND_ASSIGN(FakeExtensionGCMAppHandler); |
164 }; | 164 }; |
165 | 165 |
166 class ExtensionGCMAppHandlerTest : public testing::Test { | 166 class ExtensionGCMAppHandlerTest : public testing::Test { |
167 public: | 167 public: |
168 static KeyedService* BuildGCMProfileService( | 168 static KeyedService* BuildGCMProfileService( |
169 content::BrowserContext* context) { | 169 content::BrowserContext* context) { |
170 return new gcm::GCMProfileService(static_cast<Profile*>(context)); | 170 return new gcm::GCMProfileService(static_cast<Profile*>(context)); |
171 } | 171 } |
172 | 172 |
173 ExtensionGCMAppHandlerTest() | 173 ExtensionGCMAppHandlerTest() |
174 : extension_service_(NULL), | 174 : extension_service_(NULL), |
175 registration_result_(gcm::GCMClient::UNKNOWN_ERROR), | 175 registration_result_(gcm::RESULT_UNKNOWN_ERROR), |
176 unregistration_result_(gcm::GCMClient::UNKNOWN_ERROR) { | 176 unregistration_result_(gcm::RESULT_UNKNOWN_ERROR) { |
177 } | 177 } |
178 | 178 |
179 virtual ~ExtensionGCMAppHandlerTest() { | 179 virtual ~ExtensionGCMAppHandlerTest() { |
180 } | 180 } |
181 | 181 |
182 // Overridden from test::Test: | 182 // Overridden from test::Test: |
183 virtual void SetUp() OVERRIDE { | 183 virtual void SetUp() OVERRIDE { |
184 // Make BrowserThread work in unittest. | 184 // Make BrowserThread work in unittest. |
185 thread_bundle_.reset(new content::TestBrowserThreadBundle( | 185 thread_bundle_.reset(new content::TestBrowserThreadBundle( |
186 content::TestBrowserThreadBundle::REAL_IO_THREAD)); | 186 content::TestBrowserThreadBundle::REAL_IO_THREAD)); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 void Register(const std::string& app_id, | 289 void Register(const std::string& app_id, |
290 const std::vector<std::string>& sender_ids) { | 290 const std::vector<std::string>& sender_ids) { |
291 GetGCMProfileService()->Register( | 291 GetGCMProfileService()->Register( |
292 app_id, | 292 app_id, |
293 sender_ids, | 293 sender_ids, |
294 base::Bind(&ExtensionGCMAppHandlerTest::RegisterCompleted, | 294 base::Bind(&ExtensionGCMAppHandlerTest::RegisterCompleted, |
295 base::Unretained(this))); | 295 base::Unretained(this))); |
296 } | 296 } |
297 | 297 |
298 void RegisterCompleted(const std::string& registration_id, | 298 void RegisterCompleted(const std::string& registration_id, |
299 gcm::GCMClient::Result result) { | 299 gcm::Result result) { |
300 registration_result_ = result; | 300 registration_result_ = result; |
301 waiter_.SignalCompleted(); | 301 waiter_.SignalCompleted(); |
302 } | 302 } |
303 | 303 |
304 gcm::GCMProfileService* GetGCMProfileService() const { | 304 gcm::GCMProfileService* GetGCMProfileService() const { |
305 return gcm::GCMProfileServiceFactory::GetForProfile(profile()); | 305 return gcm::GCMProfileServiceFactory::GetForProfile(profile()); |
306 } | 306 } |
307 | 307 |
308 bool HasAppHandlers(const std::string& app_id) const { | 308 bool HasAppHandlers(const std::string& app_id) const { |
309 return GetGCMProfileService()->app_handlers_.count(app_id); | 309 return GetGCMProfileService()->app_handlers_.count(app_id); |
310 } | 310 } |
311 | 311 |
312 Profile* profile() const { return profile_.get(); } | 312 Profile* profile() const { return profile_.get(); } |
313 Waiter* waiter() { return &waiter_; } | 313 Waiter* waiter() { return &waiter_; } |
314 FakeExtensionGCMAppHandler* gcm_app_handler() const { | 314 FakeExtensionGCMAppHandler* gcm_app_handler() const { |
315 return gcm_app_handler_.get(); | 315 return gcm_app_handler_.get(); |
316 } | 316 } |
317 gcm::GCMClient::Result registration_result() const { | 317 gcm::Result registration_result() const { |
318 return registration_result_; | 318 return registration_result_; |
319 } | 319 } |
320 gcm::GCMClient::Result unregistration_result() const { | 320 gcm::Result unregistration_result() const { |
321 return unregistration_result_; | 321 return unregistration_result_; |
322 } | 322 } |
323 | 323 |
324 private: | 324 private: |
325 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_; | 325 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_; |
326 scoped_ptr<TestingProfile> profile_; | 326 scoped_ptr<TestingProfile> profile_; |
327 ExtensionService* extension_service_; // Not owned. | 327 ExtensionService* extension_service_; // Not owned. |
328 gcm::FakeSigninManager* signin_manager_; // Not owned. | 328 gcm::FakeSigninManager* signin_manager_; // Not owned. |
329 | 329 |
330 // This is needed to create extension service under CrOS. | 330 // This is needed to create extension service under CrOS. |
331 #if defined(OS_CHROMEOS) | 331 #if defined(OS_CHROMEOS) |
332 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | 332 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
333 chromeos::ScopedTestCrosSettings test_cros_settings_; | 333 chromeos::ScopedTestCrosSettings test_cros_settings_; |
334 scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_; | 334 scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_; |
335 #endif | 335 #endif |
336 | 336 |
337 Waiter waiter_; | 337 Waiter waiter_; |
338 scoped_ptr<FakeExtensionGCMAppHandler> gcm_app_handler_; | 338 scoped_ptr<FakeExtensionGCMAppHandler> gcm_app_handler_; |
339 gcm::GCMClient::Result registration_result_; | 339 gcm::Result registration_result_; |
340 gcm::GCMClient::Result unregistration_result_; | 340 gcm::Result unregistration_result_; |
341 | 341 |
342 DISALLOW_COPY_AND_ASSIGN(ExtensionGCMAppHandlerTest); | 342 DISALLOW_COPY_AND_ASSIGN(ExtensionGCMAppHandlerTest); |
343 }; | 343 }; |
344 | 344 |
345 TEST_F(ExtensionGCMAppHandlerTest, AddAndRemoveAppHandler) { | 345 TEST_F(ExtensionGCMAppHandlerTest, AddAndRemoveAppHandler) { |
346 scoped_refptr<Extension> extension(CreateExtension()); | 346 scoped_refptr<Extension> extension(CreateExtension()); |
347 | 347 |
348 // App handler is added when extension is loaded. | 348 // App handler is added when extension is loaded. |
349 LoadExtension(extension); | 349 LoadExtension(extension); |
350 waiter()->PumpUILoop(); | 350 waiter()->PumpUILoop(); |
(...skipping 20 matching lines...) Expand all Loading... |
371 LoadExtension(extension); | 371 LoadExtension(extension); |
372 | 372 |
373 // Sign-in is needed for registration. | 373 // Sign-in is needed for registration. |
374 SignIn(kTestingUsername); | 374 SignIn(kTestingUsername); |
375 | 375 |
376 // Kick off registration. | 376 // Kick off registration. |
377 std::vector<std::string> sender_ids; | 377 std::vector<std::string> sender_ids; |
378 sender_ids.push_back("sender1"); | 378 sender_ids.push_back("sender1"); |
379 Register(extension->id(), sender_ids); | 379 Register(extension->id(), sender_ids); |
380 waiter()->WaitUntilCompleted(); | 380 waiter()->WaitUntilCompleted(); |
381 EXPECT_EQ(gcm::GCMClient::SUCCESS, registration_result()); | 381 EXPECT_EQ(gcm::RESULT_SUCCESS, registration_result()); |
382 | 382 |
383 // Unregistration should be triggered when the extension is uninstalled. | 383 // Unregistration should be triggered when the extension is uninstalled. |
384 UninstallExtension(extension); | 384 UninstallExtension(extension); |
385 waiter()->WaitUntilCompleted(); | 385 waiter()->WaitUntilCompleted(); |
386 EXPECT_EQ(gcm::GCMClient::SUCCESS, | 386 EXPECT_EQ(gcm::RESULT_SUCCESS, |
387 gcm_app_handler()->unregistration_result()); | 387 gcm_app_handler()->unregistration_result()); |
388 } | 388 } |
389 | 389 |
390 } // namespace extensions | 390 } // namespace extensions |
OLD | NEW |