| 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" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 18 #include "base/values.h" | 18 #include "base/values.h" |
| 19 #include "chrome/browser/extensions/extension_service.h" | 19 #include "chrome/browser/extensions/extension_service.h" |
| 20 #include "chrome/browser/extensions/test_extension_service.h" | 20 #include "chrome/browser/extensions/test_extension_service.h" |
| 21 #include "chrome/browser/extensions/test_extension_system.h" | 21 #include "chrome/browser/extensions/test_extension_system.h" |
| 22 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/browser/services/gcm/fake_gcm_client.h" | 23 #include "chrome/browser/services/gcm/fake_gcm_client.h" |
| 24 #include "chrome/browser/services/gcm/fake_gcm_client_factory.h" | 24 #include "chrome/browser/services/gcm/fake_gcm_client_factory.h" |
| 25 #include "chrome/browser/services/gcm/fake_signin_manager.h" | 25 #include "chrome/browser/services/gcm/fake_signin_manager.h" |
| 26 #include "chrome/browser/services/gcm/gcm_driver.h" |
| 26 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 27 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
| 27 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 28 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| 28 #include "chrome/browser/signin/signin_manager_factory.h" | 29 #include "chrome/browser/signin/signin_manager_factory.h" |
| 29 #include "chrome/common/pref_names.h" | 30 #include "chrome/common/pref_names.h" |
| 30 #include "chrome/test/base/testing_profile.h" | 31 #include "chrome/test/base/testing_profile.h" |
| 31 #include "components/gcm_driver/gcm_client_factory.h" | 32 #include "components/gcm_driver/gcm_client_factory.h" |
| 32 #include "components/keyed_service/core/keyed_service.h" | 33 #include "components/keyed_service/core/keyed_service.h" |
| 33 #include "content/public/browser/browser_context.h" | 34 #include "content/public/browser/browser_context.h" |
| 34 #include "content/public/browser/browser_thread.h" | 35 #include "content/public/browser/browser_thread.h" |
| 35 #include "content/public/test/test_browser_thread_bundle.h" | 36 #include "content/public/test/test_browser_thread_bundle.h" |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 Waiter* waiter_; | 161 Waiter* waiter_; |
| 161 gcm::GCMClient::Result unregistration_result_; | 162 gcm::GCMClient::Result unregistration_result_; |
| 162 | 163 |
| 163 DISALLOW_COPY_AND_ASSIGN(FakeExtensionGCMAppHandler); | 164 DISALLOW_COPY_AND_ASSIGN(FakeExtensionGCMAppHandler); |
| 164 }; | 165 }; |
| 165 | 166 |
| 166 class ExtensionGCMAppHandlerTest : public testing::Test { | 167 class ExtensionGCMAppHandlerTest : public testing::Test { |
| 167 public: | 168 public: |
| 168 static KeyedService* BuildGCMProfileService( | 169 static KeyedService* BuildGCMProfileService( |
| 169 content::BrowserContext* context) { | 170 content::BrowserContext* context) { |
| 170 return new gcm::GCMProfileService(static_cast<Profile*>(context)); | 171 return new gcm::GCMProfileService( |
| 172 Profile::FromBrowserContext(context), |
| 173 scoped_ptr<gcm::GCMClientFactory>( |
| 174 new gcm::FakeGCMClientFactory(gcm::FakeGCMClient::NO_DELAY_START))); |
| 171 } | 175 } |
| 172 | 176 |
| 173 ExtensionGCMAppHandlerTest() | 177 ExtensionGCMAppHandlerTest() |
| 174 : extension_service_(NULL), | 178 : extension_service_(NULL), |
| 175 registration_result_(gcm::GCMClient::UNKNOWN_ERROR), | 179 registration_result_(gcm::GCMClient::UNKNOWN_ERROR), |
| 176 unregistration_result_(gcm::GCMClient::UNKNOWN_ERROR) { | 180 unregistration_result_(gcm::GCMClient::UNKNOWN_ERROR) { |
| 177 } | 181 } |
| 178 | 182 |
| 179 virtual ~ExtensionGCMAppHandlerTest() { | 183 virtual ~ExtensionGCMAppHandlerTest() { |
| 180 } | 184 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 202 TestExtensionSystem* extension_system( | 206 TestExtensionSystem* extension_system( |
| 203 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile()))); | 207 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile()))); |
| 204 extension_system->CreateExtensionService( | 208 extension_system->CreateExtensionService( |
| 205 CommandLine::ForCurrentProcess(), base::FilePath(), false); | 209 CommandLine::ForCurrentProcess(), base::FilePath(), false); |
| 206 extension_service_ = extension_system->Get(profile())->extension_service(); | 210 extension_service_ = extension_system->Get(profile())->extension_service(); |
| 207 | 211 |
| 208 // Enable GCM such that tests could be run on all channels. | 212 // Enable GCM such that tests could be run on all channels. |
| 209 profile()->GetPrefs()->SetBoolean(prefs::kGCMChannelEnabled, true); | 213 profile()->GetPrefs()->SetBoolean(prefs::kGCMChannelEnabled, true); |
| 210 | 214 |
| 211 // Create GCMProfileService that talks with fake GCMClient. | 215 // Create GCMProfileService that talks with fake GCMClient. |
| 212 gcm::GCMProfileService* gcm_profile_service = | 216 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 213 static_cast<gcm::GCMProfileService*>( | 217 profile(), &ExtensionGCMAppHandlerTest::BuildGCMProfileService); |
| 214 gcm::GCMProfileServiceFactory::GetInstance()-> | |
| 215 SetTestingFactoryAndUse( | |
| 216 profile(), | |
| 217 &ExtensionGCMAppHandlerTest::BuildGCMProfileService)); | |
| 218 scoped_ptr<gcm::GCMClientFactory> gcm_client_factory( | |
| 219 new gcm::FakeGCMClientFactory(gcm::FakeGCMClient::NO_DELAY_START)); | |
| 220 gcm_profile_service->Initialize(gcm_client_factory.Pass()); | |
| 221 | 218 |
| 222 // Create a fake version of ExtensionGCMAppHandler. | 219 // Create a fake version of ExtensionGCMAppHandler. |
| 223 gcm_app_handler_.reset(new FakeExtensionGCMAppHandler(profile(), &waiter_)); | 220 gcm_app_handler_.reset(new FakeExtensionGCMAppHandler(profile(), &waiter_)); |
| 224 } | 221 } |
| 225 | 222 |
| 226 virtual void TearDown() OVERRIDE { | 223 virtual void TearDown() OVERRIDE { |
| 227 #if defined(OS_CHROMEOS) | 224 #if defined(OS_CHROMEOS) |
| 228 test_user_manager_.reset(); | 225 test_user_manager_.reset(); |
| 229 #endif | 226 #endif |
| 230 | 227 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 waiter_.PumpIOLoop(); | 278 waiter_.PumpIOLoop(); |
| 282 } | 279 } |
| 283 | 280 |
| 284 void SignOut() { | 281 void SignOut() { |
| 285 signin_manager_->SignOut(); | 282 signin_manager_->SignOut(); |
| 286 waiter_.PumpIOLoop(); | 283 waiter_.PumpIOLoop(); |
| 287 } | 284 } |
| 288 | 285 |
| 289 void Register(const std::string& app_id, | 286 void Register(const std::string& app_id, |
| 290 const std::vector<std::string>& sender_ids) { | 287 const std::vector<std::string>& sender_ids) { |
| 291 GetGCMProfileService()->Register( | 288 GetGCMDriver()->Register( |
| 292 app_id, | 289 app_id, |
| 293 sender_ids, | 290 sender_ids, |
| 294 base::Bind(&ExtensionGCMAppHandlerTest::RegisterCompleted, | 291 base::Bind(&ExtensionGCMAppHandlerTest::RegisterCompleted, |
| 295 base::Unretained(this))); | 292 base::Unretained(this))); |
| 296 } | 293 } |
| 297 | 294 |
| 298 void RegisterCompleted(const std::string& registration_id, | 295 void RegisterCompleted(const std::string& registration_id, |
| 299 gcm::GCMClient::Result result) { | 296 gcm::GCMClient::Result result) { |
| 300 registration_result_ = result; | 297 registration_result_ = result; |
| 301 waiter_.SignalCompleted(); | 298 waiter_.SignalCompleted(); |
| 302 } | 299 } |
| 303 | 300 |
| 304 gcm::GCMProfileService* GetGCMProfileService() const { | 301 gcm::GCMDriver* GetGCMDriver() const { |
| 305 return gcm::GCMProfileServiceFactory::GetForProfile(profile()); | 302 return gcm::GCMProfileServiceFactory::GetForProfile(profile())->driver(); |
| 306 } | 303 } |
| 307 | 304 |
| 308 bool HasAppHandlers(const std::string& app_id) const { | 305 bool HasAppHandlers(const std::string& app_id) const { |
| 309 return GetGCMProfileService()->app_handlers().count(app_id); | 306 return GetGCMDriver()->app_handlers().count(app_id); |
| 310 } | 307 } |
| 311 | 308 |
| 312 Profile* profile() const { return profile_.get(); } | 309 Profile* profile() const { return profile_.get(); } |
| 313 Waiter* waiter() { return &waiter_; } | 310 Waiter* waiter() { return &waiter_; } |
| 314 FakeExtensionGCMAppHandler* gcm_app_handler() const { | 311 FakeExtensionGCMAppHandler* gcm_app_handler() const { |
| 315 return gcm_app_handler_.get(); | 312 return gcm_app_handler_.get(); |
| 316 } | 313 } |
| 317 gcm::GCMClient::Result registration_result() const { | 314 gcm::GCMClient::Result registration_result() const { |
| 318 return registration_result_; | 315 return registration_result_; |
| 319 } | 316 } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 EXPECT_EQ(gcm::GCMClient::SUCCESS, registration_result()); | 378 EXPECT_EQ(gcm::GCMClient::SUCCESS, registration_result()); |
| 382 | 379 |
| 383 // Unregistration should be triggered when the extension is uninstalled. | 380 // Unregistration should be triggered when the extension is uninstalled. |
| 384 UninstallExtension(extension); | 381 UninstallExtension(extension); |
| 385 waiter()->WaitUntilCompleted(); | 382 waiter()->WaitUntilCompleted(); |
| 386 EXPECT_EQ(gcm::GCMClient::SUCCESS, | 383 EXPECT_EQ(gcm::GCMClient::SUCCESS, |
| 387 gcm_app_handler()->unregistration_result()); | 384 gcm_app_handler()->unregistration_result()); |
| 388 } | 385 } |
| 389 | 386 |
| 390 } // namespace extensions | 387 } // namespace extensions |
| OLD | NEW |