| 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" |
| 6 |
| 7 #include <vector> |
| 8 |
| 5 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 6 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 7 #include "base/prefs/pref_service.h" | 12 #include "base/files/file_path.h" |
| 8 #include "chrome/browser/extensions/extension_gcm_app_handler.h" | 13 #include "base/location.h" |
| 14 #include "base/logging.h" |
| 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/run_loop.h" |
| 18 #include "base/values.h" |
| 19 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/extensions/test_extension_service.h" | 20 #include "chrome/browser/extensions/test_extension_service.h" |
| 10 #include "chrome/browser/extensions/test_extension_system.h" | 21 #include "chrome/browser/extensions/test_extension_system.h" |
| 22 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/browser/services/gcm/fake_gcm_client_factory.h" |
| 24 #include "chrome/browser/services/gcm/fake_signin_manager.h" |
| 25 #include "chrome/browser/services/gcm/gcm_client_factory.h" |
| 11 #include "chrome/browser/services/gcm/gcm_client_mock.h" | 26 #include "chrome/browser/services/gcm/gcm_client_mock.h" |
| 12 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 27 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
| 13 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 28 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| 14 #include "chrome/browser/services/gcm/gcm_profile_service_test_helper.h" | |
| 15 #include "chrome/browser/signin/signin_manager_factory.h" | 29 #include "chrome/browser/signin/signin_manager_factory.h" |
| 16 #include "chrome/common/pref_names.h" | 30 #include "chrome/common/pref_names.h" |
| 17 #include "chrome/test/base/testing_profile.h" | 31 #include "chrome/test/base/testing_profile.h" |
| 32 #include "components/keyed_service/core/keyed_service.h" |
| 33 #include "content/public/browser/browser_context.h" |
| 18 #include "content/public/browser/browser_thread.h" | 34 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/test/test_browser_thread_bundle.h" | 35 #include "content/public/test/test_browser_thread_bundle.h" |
| 36 #include "extensions/browser/extension_system.h" |
| 20 #include "extensions/common/extension.h" | 37 #include "extensions/common/extension.h" |
| 38 #include "extensions/common/manifest.h" |
| 21 #include "extensions/common/manifest_constants.h" | 39 #include "extensions/common/manifest_constants.h" |
| 40 #include "extensions/common/permissions/api_permission.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 42 |
| 43 #if !defined(OS_ANDROID) |
| 44 #include "chrome/browser/extensions/api/gcm/gcm_api.h" |
| 45 #endif |
| 46 |
| 24 #if defined(OS_CHROMEOS) | 47 #if defined(OS_CHROMEOS) |
| 25 #include "chrome/browser/chromeos/login/user_manager.h" | 48 #include "chrome/browser/chromeos/login/user_manager.h" |
| 26 #include "chrome/browser/chromeos/settings/cros_settings.h" | 49 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 27 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 50 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 28 #else | |
| 29 #include "components/signin/core/browser/signin_manager.h" | |
| 30 #endif | 51 #endif |
| 31 | 52 |
| 32 using namespace gcm; | 53 using namespace gcm; |
| 33 | 54 |
| 34 namespace extensions { | 55 namespace extensions { |
| 35 | 56 |
| 36 namespace { | 57 namespace { |
| 37 | 58 |
| 38 const char kTestExtensionName[] = "FooBar"; | 59 const char kTestExtensionName[] = "FooBar"; |
| 39 const char kTestingUsername[] = "user1@example.com"; | 60 const char kTestingUsername[] = "user1@example.com"; |
| 40 | 61 |
| 41 } // namespace | 62 } // namespace |
| 42 | 63 |
| 64 // Helper class for asynchronous waiting. |
| 65 class Waiter { |
| 66 public: |
| 67 Waiter() {} |
| 68 ~Waiter() {} |
| 69 |
| 70 // Waits until the asynchronous operation finishes. |
| 71 void WaitUntilCompleted() { |
| 72 run_loop_.reset(new base::RunLoop); |
| 73 run_loop_->Run(); |
| 74 } |
| 75 |
| 76 // Signals that the asynchronous operation finishes. |
| 77 void SignalCompleted() { |
| 78 if (run_loop_ && run_loop_->running()) |
| 79 run_loop_->Quit(); |
| 80 } |
| 81 |
| 82 // Runs until UI loop becomes idle. |
| 83 void PumpUILoop() { |
| 84 base::MessageLoop::current()->RunUntilIdle(); |
| 85 } |
| 86 |
| 87 // Runs until IO loop becomes idle. |
| 88 void PumpIOLoop() { |
| 89 content::BrowserThread::PostTask( |
| 90 content::BrowserThread::IO, |
| 91 FROM_HERE, |
| 92 base::Bind(&Waiter::OnIOLoopPump, base::Unretained(this))); |
| 93 |
| 94 WaitUntilCompleted(); |
| 95 } |
| 96 |
| 97 private: |
| 98 void PumpIOLoopCompleted() { |
| 99 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 100 |
| 101 SignalCompleted(); |
| 102 } |
| 103 |
| 104 void OnIOLoopPump() { |
| 105 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 106 |
| 107 content::BrowserThread::PostTask( |
| 108 content::BrowserThread::IO, |
| 109 FROM_HERE, |
| 110 base::Bind(&Waiter::OnIOLoopPumpCompleted, base::Unretained(this))); |
| 111 } |
| 112 |
| 113 void OnIOLoopPumpCompleted() { |
| 114 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 115 |
| 116 content::BrowserThread::PostTask( |
| 117 content::BrowserThread::UI, |
| 118 FROM_HERE, |
| 119 base::Bind(&Waiter::PumpIOLoopCompleted, base::Unretained(this))); |
| 120 } |
| 121 |
| 122 scoped_ptr<base::RunLoop> run_loop_; |
| 123 |
| 124 DISALLOW_COPY_AND_ASSIGN(Waiter); |
| 125 }; |
| 126 |
| 43 class FakeExtensionGCMAppHandler : public ExtensionGCMAppHandler { | 127 class FakeExtensionGCMAppHandler : public ExtensionGCMAppHandler { |
| 44 public: | 128 public: |
| 45 FakeExtensionGCMAppHandler(Profile* profile, Waiter* waiter) | 129 FakeExtensionGCMAppHandler(Profile* profile, Waiter* waiter) |
| 46 : ExtensionGCMAppHandler(profile), | 130 : ExtensionGCMAppHandler(profile), |
| 47 waiter_(waiter), | 131 waiter_(waiter), |
| 48 unregistration_result_(GCMClient::UNKNOWN_ERROR) { | 132 unregistration_result_(GCMClient::UNKNOWN_ERROR) { |
| 49 } | 133 } |
| 50 | 134 |
| 51 virtual ~FakeExtensionGCMAppHandler() { | 135 virtual ~FakeExtensionGCMAppHandler() { |
| 52 } | 136 } |
| 53 | 137 |
| 54 virtual void OnMessage( | 138 virtual void OnMessage( |
| 55 const std::string& app_id, | 139 const std::string& app_id, |
| 56 const GCMClient::IncomingMessage& message)OVERRIDE { | 140 const GCMClient::IncomingMessage& message) OVERRIDE { |
| 57 } | 141 } |
| 58 | 142 |
| 59 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE { | 143 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE { |
| 60 } | 144 } |
| 61 | 145 |
| 62 virtual void OnSendError( | 146 virtual void OnSendError( |
| 63 const std::string& app_id, | 147 const std::string& app_id, |
| 64 const GCMClient::SendErrorDetails& send_error_details) OVERRIDE { | 148 const GCMClient::SendErrorDetails& send_error_details) OVERRIDE { |
| 65 } | 149 } |
| 66 | 150 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 content::TestBrowserThreadBundle::REAL_IO_THREAD)); | 188 content::TestBrowserThreadBundle::REAL_IO_THREAD)); |
| 105 | 189 |
| 106 // This is needed to create extension service under CrOS. | 190 // This is needed to create extension service under CrOS. |
| 107 #if defined(OS_CHROMEOS) | 191 #if defined(OS_CHROMEOS) |
| 108 test_user_manager_.reset(new chromeos::ScopedTestUserManager()); | 192 test_user_manager_.reset(new chromeos::ScopedTestUserManager()); |
| 109 #endif | 193 #endif |
| 110 | 194 |
| 111 // Create a new profile. | 195 // Create a new profile. |
| 112 TestingProfile::Builder builder; | 196 TestingProfile::Builder builder; |
| 113 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), | 197 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), |
| 114 gcm::FakeSigninManager::Build); | 198 FakeSigninManager::Build); |
| 115 profile_ = builder.Build(); | 199 profile_ = builder.Build(); |
| 116 signin_manager_ = static_cast<gcm::FakeSigninManager*>( | 200 signin_manager_ = static_cast<FakeSigninManager*>( |
| 117 SigninManagerFactory::GetInstance()->GetForProfile(profile_.get())); | 201 SigninManagerFactory::GetInstance()->GetForProfile(profile_.get())); |
| 118 | 202 |
| 119 // Create extension service in order to uninstall the extension. | 203 // Create extension service in order to uninstall the extension. |
| 120 TestExtensionSystem* extension_system( | 204 TestExtensionSystem* extension_system( |
| 121 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile()))); | 205 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile()))); |
| 122 extension_system->CreateExtensionService( | 206 extension_system->CreateExtensionService( |
| 123 CommandLine::ForCurrentProcess(), base::FilePath(), false); | 207 CommandLine::ForCurrentProcess(), base::FilePath(), false); |
| 124 extension_service_ = extension_system->Get(profile())->extension_service(); | 208 extension_service_ = extension_system->Get(profile())->extension_service(); |
| 125 | 209 |
| 126 // Enable GCM such that tests could be run on all channels. | 210 // Enable GCM such that tests could be run on all channels. |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 } | 316 } |
| 233 GCMClient::Result registration_result() const { return registration_result_; } | 317 GCMClient::Result registration_result() const { return registration_result_; } |
| 234 GCMClient::Result unregistration_result() const { | 318 GCMClient::Result unregistration_result() const { |
| 235 return unregistration_result_; | 319 return unregistration_result_; |
| 236 } | 320 } |
| 237 | 321 |
| 238 private: | 322 private: |
| 239 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_; | 323 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_; |
| 240 scoped_ptr<TestingProfile> profile_; | 324 scoped_ptr<TestingProfile> profile_; |
| 241 ExtensionService* extension_service_; // Not owned. | 325 ExtensionService* extension_service_; // Not owned. |
| 242 gcm::FakeSigninManager* signin_manager_; // Not owned. | 326 FakeSigninManager* signin_manager_; // Not owned. |
| 243 | 327 |
| 244 // This is needed to create extension service under CrOS. | 328 // This is needed to create extension service under CrOS. |
| 245 #if defined(OS_CHROMEOS) | 329 #if defined(OS_CHROMEOS) |
| 246 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | 330 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
| 247 chromeos::ScopedTestCrosSettings test_cros_settings_; | 331 chromeos::ScopedTestCrosSettings test_cros_settings_; |
| 248 scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_; | 332 scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_; |
| 249 #endif | 333 #endif |
| 250 | 334 |
| 251 Waiter waiter_; | 335 Waiter waiter_; |
| 252 scoped_ptr<FakeExtensionGCMAppHandler> gcm_app_handler_; | 336 scoped_ptr<FakeExtensionGCMAppHandler> gcm_app_handler_; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 waiter()->WaitUntilCompleted(); | 378 waiter()->WaitUntilCompleted(); |
| 295 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | 379 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); |
| 296 | 380 |
| 297 // Unregistration should be triggered when the extension is uninstalled. | 381 // Unregistration should be triggered when the extension is uninstalled. |
| 298 UninstallExtension(extension); | 382 UninstallExtension(extension); |
| 299 waiter()->WaitUntilCompleted(); | 383 waiter()->WaitUntilCompleted(); |
| 300 EXPECT_EQ(GCMClient::SUCCESS, gcm_app_handler()->unregistration_result()); | 384 EXPECT_EQ(GCMClient::SUCCESS, gcm_app_handler()->unregistration_result()); |
| 301 } | 385 } |
| 302 | 386 |
| 303 } // namespace extensions | 387 } // namespace extensions |
| OLD | NEW |