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" |
| 12 #include "base/files/file_path.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/observer_list.h" |
7 #include "base/prefs/pref_service.h" | 18 #include "base/prefs/pref_service.h" |
8 #include "chrome/browser/extensions/extension_gcm_app_handler.h" | 19 #include "base/run_loop.h" |
| 20 #include "base/values.h" |
| 21 #include "chrome/browser/extensions/extension_service.h" |
9 #include "chrome/browser/extensions/test_extension_service.h" | 22 #include "chrome/browser/extensions/test_extension_service.h" |
10 #include "chrome/browser/extensions/test_extension_system.h" | 23 #include "chrome/browser/extensions/test_extension_system.h" |
| 24 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/browser/services/gcm/fake_gcm_client_factory.h" |
| 26 #include "chrome/browser/services/gcm/gcm_client_factory.h" |
11 #include "chrome/browser/services/gcm/gcm_client_mock.h" | 27 #include "chrome/browser/services/gcm/gcm_client_mock.h" |
12 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 28 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
13 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 29 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
14 #include "chrome/browser/services/gcm/gcm_profile_service_test_helper.h" | 30 #include "chrome/browser/signin/chrome_signin_client_factory.h" |
15 #include "chrome/browser/signin/signin_manager_factory.h" | 31 #include "chrome/browser/signin/signin_manager_factory.h" |
16 #include "chrome/common/pref_names.h" | 32 #include "chrome/common/pref_names.h" |
17 #include "chrome/test/base/testing_profile.h" | 33 #include "chrome/test/base/testing_profile.h" |
| 34 #include "components/keyed_service/core/keyed_service.h" |
| 35 #include "components/signin/core/common/signin_pref_names.h" |
| 36 #include "content/public/browser/browser_context.h" |
18 #include "content/public/browser/browser_thread.h" | 37 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/test/test_browser_thread_bundle.h" | 38 #include "content/public/test/test_browser_thread_bundle.h" |
| 39 #include "extensions/browser/extension_system.h" |
20 #include "extensions/common/extension.h" | 40 #include "extensions/common/extension.h" |
| 41 #include "extensions/common/manifest.h" |
21 #include "extensions/common/manifest_constants.h" | 42 #include "extensions/common/manifest_constants.h" |
| 43 #include "extensions/common/permissions/api_permission.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 44 #include "testing/gtest/include/gtest/gtest.h" |
23 | 45 |
| 46 #if !defined(OS_ANDROID) |
| 47 #include "chrome/browser/extensions/api/gcm/gcm_api.h" |
| 48 #endif |
| 49 |
24 #if defined(OS_CHROMEOS) | 50 #if defined(OS_CHROMEOS) |
25 #include "chrome/browser/chromeos/login/user_manager.h" | 51 #include "chrome/browser/chromeos/login/user_manager.h" |
26 #include "chrome/browser/chromeos/settings/cros_settings.h" | 52 #include "chrome/browser/chromeos/settings/cros_settings.h" |
27 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 53 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 54 #include "components/signin/core/browser/signin_manager_base.h" |
28 #else | 55 #else |
| 56 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
29 #include "components/signin/core/browser/signin_manager.h" | 57 #include "components/signin/core/browser/signin_manager.h" |
30 #endif | 58 #endif |
31 | 59 |
32 using namespace gcm; | 60 using namespace gcm; |
33 | 61 |
34 namespace extensions { | 62 namespace extensions { |
35 | 63 |
36 namespace { | 64 namespace { |
37 | 65 |
38 const char kTestExtensionName[] = "FooBar"; | 66 const char kTestExtensionName[] = "FooBar"; |
39 const char kTestingUsername[] = "user1@example.com"; | 67 const char kTestingUsername[] = "user1@example.com"; |
40 | 68 |
41 } // namespace | 69 } // namespace |
42 | 70 |
| 71 #if defined(OS_CHROMEOS) |
| 72 class FakeSigninManager : public SigninManagerBase { |
| 73 #else |
| 74 class FakeSigninManager : public SigninManager { |
| 75 #endif |
| 76 public: |
| 77 static KeyedService* Build(content::BrowserContext* context) { |
| 78 return new FakeSigninManager(static_cast<Profile*>(context)); |
| 79 } |
| 80 |
| 81 explicit FakeSigninManager(Profile* profile) |
| 82 #if defined(OS_CHROMEOS) |
| 83 : SigninManagerBase( |
| 84 ChromeSigninClientFactory::GetInstance()->GetForProfile(profile)), |
| 85 #else |
| 86 : SigninManager( |
| 87 ChromeSigninClientFactory::GetInstance()->GetForProfile(profile), |
| 88 ProfileOAuth2TokenServiceFactory::GetForProfile(profile)), |
| 89 #endif |
| 90 profile_(profile) { |
| 91 Initialize(NULL); |
| 92 } |
| 93 |
| 94 virtual ~FakeSigninManager() {} |
| 95 |
| 96 void SignIn(const std::string& username) { |
| 97 SetAuthenticatedUsername(username); |
| 98 FOR_EACH_OBSERVER(Observer, |
| 99 observer_list_, |
| 100 GoogleSigninSucceeded(username, std::string())); |
| 101 } |
| 102 |
| 103 #if defined(OS_CHROMEOS) |
| 104 void SignOut() { |
| 105 #else |
| 106 virtual void SignOut() OVERRIDE { |
| 107 #endif |
| 108 std::string username = GetAuthenticatedUsername(); |
| 109 clear_authenticated_username(); |
| 110 profile_->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); |
| 111 FOR_EACH_OBSERVER(Observer, observer_list_, GoogleSignedOut(username)); |
| 112 } |
| 113 |
| 114 private: |
| 115 Profile* profile_; |
| 116 |
| 117 DISALLOW_COPY_AND_ASSIGN(FakeSigninManager); |
| 118 }; |
| 119 |
| 120 // Helper class for asynchronous waiting. |
| 121 class Waiter { |
| 122 public: |
| 123 Waiter() {} |
| 124 ~Waiter() {} |
| 125 |
| 126 // Waits until the asynchronous operation finishes. |
| 127 void WaitUntilCompleted() { |
| 128 run_loop_.reset(new base::RunLoop); |
| 129 run_loop_->Run(); |
| 130 } |
| 131 |
| 132 // Signals that the asynchronous operation finishes. |
| 133 void SignalCompleted() { |
| 134 if (run_loop_ && run_loop_->running()) |
| 135 run_loop_->Quit(); |
| 136 } |
| 137 |
| 138 // Runs until UI loop becomes idle. |
| 139 void PumpUILoop() { |
| 140 base::MessageLoop::current()->RunUntilIdle(); |
| 141 } |
| 142 |
| 143 // Runs until IO loop becomes idle. |
| 144 void PumpIOLoop() { |
| 145 content::BrowserThread::PostTask( |
| 146 content::BrowserThread::IO, |
| 147 FROM_HERE, |
| 148 base::Bind(&Waiter::OnIOLoopPump, base::Unretained(this))); |
| 149 |
| 150 WaitUntilCompleted(); |
| 151 } |
| 152 |
| 153 private: |
| 154 void PumpIOLoopCompleted() { |
| 155 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 156 |
| 157 SignalCompleted(); |
| 158 } |
| 159 |
| 160 void OnIOLoopPump() { |
| 161 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 162 |
| 163 content::BrowserThread::PostTask( |
| 164 content::BrowserThread::IO, |
| 165 FROM_HERE, |
| 166 base::Bind(&Waiter::OnIOLoopPumpCompleted, base::Unretained(this))); |
| 167 } |
| 168 |
| 169 void OnIOLoopPumpCompleted() { |
| 170 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 171 |
| 172 content::BrowserThread::PostTask( |
| 173 content::BrowserThread::UI, |
| 174 FROM_HERE, |
| 175 base::Bind(&Waiter::PumpIOLoopCompleted, base::Unretained(this))); |
| 176 } |
| 177 |
| 178 scoped_ptr<base::RunLoop> run_loop_; |
| 179 |
| 180 DISALLOW_COPY_AND_ASSIGN(Waiter); |
| 181 }; |
| 182 |
43 class FakeExtensionGCMAppHandler : public ExtensionGCMAppHandler { | 183 class FakeExtensionGCMAppHandler : public ExtensionGCMAppHandler { |
44 public: | 184 public: |
45 FakeExtensionGCMAppHandler(Profile* profile, Waiter* waiter) | 185 FakeExtensionGCMAppHandler(Profile* profile, Waiter* waiter) |
46 : ExtensionGCMAppHandler(profile), | 186 : ExtensionGCMAppHandler(profile), |
47 waiter_(waiter), | 187 waiter_(waiter), |
48 unregistration_result_(GCMClient::UNKNOWN_ERROR) { | 188 unregistration_result_(GCMClient::UNKNOWN_ERROR) { |
49 } | 189 } |
50 | 190 |
51 virtual ~FakeExtensionGCMAppHandler() { | 191 virtual ~FakeExtensionGCMAppHandler() { |
52 } | 192 } |
53 | 193 |
54 virtual void OnMessage( | 194 virtual void OnMessage( |
55 const std::string& app_id, | 195 const std::string& app_id, |
56 const GCMClient::IncomingMessage& message)OVERRIDE { | 196 const GCMClient::IncomingMessage& message) OVERRIDE { |
57 } | 197 } |
58 | 198 |
59 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE { | 199 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE { |
60 } | 200 } |
61 | 201 |
62 virtual void OnSendError( | 202 virtual void OnSendError( |
63 const std::string& app_id, | 203 const std::string& app_id, |
64 const GCMClient::SendErrorDetails& send_error_details) OVERRIDE { | 204 const GCMClient::SendErrorDetails& send_error_details) OVERRIDE { |
65 } | 205 } |
66 | 206 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 content::TestBrowserThreadBundle::REAL_IO_THREAD)); | 244 content::TestBrowserThreadBundle::REAL_IO_THREAD)); |
105 | 245 |
106 // This is needed to create extension service under CrOS. | 246 // This is needed to create extension service under CrOS. |
107 #if defined(OS_CHROMEOS) | 247 #if defined(OS_CHROMEOS) |
108 test_user_manager_.reset(new chromeos::ScopedTestUserManager()); | 248 test_user_manager_.reset(new chromeos::ScopedTestUserManager()); |
109 #endif | 249 #endif |
110 | 250 |
111 // Create a new profile. | 251 // Create a new profile. |
112 TestingProfile::Builder builder; | 252 TestingProfile::Builder builder; |
113 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), | 253 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), |
114 gcm::FakeSigninManager::Build); | 254 FakeSigninManager::Build); |
115 profile_ = builder.Build(); | 255 profile_ = builder.Build(); |
116 signin_manager_ = static_cast<gcm::FakeSigninManager*>( | 256 signin_manager_ = static_cast<FakeSigninManager*>( |
117 SigninManagerFactory::GetInstance()->GetForProfile(profile_.get())); | 257 SigninManagerFactory::GetInstance()->GetForProfile(profile_.get())); |
118 | 258 |
119 // Create extension service in order to uninstall the extension. | 259 // Create extension service in order to uninstall the extension. |
120 TestExtensionSystem* extension_system( | 260 TestExtensionSystem* extension_system( |
121 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile()))); | 261 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile()))); |
122 extension_system->CreateExtensionService( | 262 extension_system->CreateExtensionService( |
123 CommandLine::ForCurrentProcess(), base::FilePath(), false); | 263 CommandLine::ForCurrentProcess(), base::FilePath(), false); |
124 extension_service_ = extension_system->Get(profile())->extension_service(); | 264 extension_service_ = extension_system->Get(profile())->extension_service(); |
125 | 265 |
126 // Enable GCM such that tests could be run on all channels. | 266 // 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 } | 372 } |
233 GCMClient::Result registration_result() const { return registration_result_; } | 373 GCMClient::Result registration_result() const { return registration_result_; } |
234 GCMClient::Result unregistration_result() const { | 374 GCMClient::Result unregistration_result() const { |
235 return unregistration_result_; | 375 return unregistration_result_; |
236 } | 376 } |
237 | 377 |
238 private: | 378 private: |
239 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_; | 379 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_; |
240 scoped_ptr<TestingProfile> profile_; | 380 scoped_ptr<TestingProfile> profile_; |
241 ExtensionService* extension_service_; // Not owned. | 381 ExtensionService* extension_service_; // Not owned. |
242 gcm::FakeSigninManager* signin_manager_; // Not owned. | 382 FakeSigninManager* signin_manager_; // Not owned. |
243 | 383 |
244 // This is needed to create extension service under CrOS. | 384 // This is needed to create extension service under CrOS. |
245 #if defined(OS_CHROMEOS) | 385 #if defined(OS_CHROMEOS) |
246 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | 386 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
247 chromeos::ScopedTestCrosSettings test_cros_settings_; | 387 chromeos::ScopedTestCrosSettings test_cros_settings_; |
248 scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_; | 388 scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_; |
249 #endif | 389 #endif |
250 | 390 |
251 Waiter waiter_; | 391 Waiter waiter_; |
252 scoped_ptr<FakeExtensionGCMAppHandler> gcm_app_handler_; | 392 scoped_ptr<FakeExtensionGCMAppHandler> gcm_app_handler_; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 waiter()->WaitUntilCompleted(); | 434 waiter()->WaitUntilCompleted(); |
295 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | 435 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); |
296 | 436 |
297 // Unregistration should be triggered when the extension is uninstalled. | 437 // Unregistration should be triggered when the extension is uninstalled. |
298 UninstallExtension(extension); | 438 UninstallExtension(extension); |
299 waiter()->WaitUntilCompleted(); | 439 waiter()->WaitUntilCompleted(); |
300 EXPECT_EQ(GCMClient::SUCCESS, gcm_app_handler()->unregistration_result()); | 440 EXPECT_EQ(GCMClient::SUCCESS, gcm_app_handler()->unregistration_result()); |
301 } | 441 } |
302 | 442 |
303 } // namespace extensions | 443 } // namespace extensions |
OLD | NEW |