OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/test/test_simple_task_runner.h" |
| 11 #include "base/thread_task_runner_handle.h" |
10 #include "chrome/browser/background/background_mode_manager.h" | 12 #include "chrome/browser/background/background_mode_manager.h" |
11 #include "chrome/browser/browser_shutdown.h" | 13 #include "chrome/browser/browser_shutdown.h" |
12 #include "chrome/browser/extensions/extension_function_test_utils.h" | 14 #include "chrome/browser/extensions/extension_function_test_utils.h" |
13 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
14 #include "chrome/browser/extensions/test_extension_system.h" | 16 #include "chrome/browser/extensions/test_extension_system.h" |
15 #include "chrome/browser/lifetime/application_lifetime.h" | 17 #include "chrome/browser/lifetime/application_lifetime.h" |
16 #include "chrome/browser/profiles/profile_info_cache.h" | 18 #include "chrome/browser/profiles/profile_info_cache.h" |
17 #include "chrome/browser/status_icons/status_icon_menu_model.h" | 19 #include "chrome/browser/status_icons/status_icon_menu_model.h" |
18 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
19 #include "chrome/test/base/testing_browser_process.h" | 21 #include "chrome/test/base/testing_browser_process.h" |
(...skipping 17 matching lines...) Expand all Loading... |
37 | 39 |
38 using testing::_; | 40 using testing::_; |
39 using testing::AtMost; | 41 using testing::AtMost; |
40 using testing::Exactly; | 42 using testing::Exactly; |
41 using testing::InSequence; | 43 using testing::InSequence; |
42 using testing::Mock; | 44 using testing::Mock; |
43 using testing::StrictMock; | 45 using testing::StrictMock; |
44 | 46 |
45 namespace { | 47 namespace { |
46 | 48 |
| 49 scoped_ptr<TestingProfileManager> CreateTestingProfileManager() { |
| 50 scoped_ptr<TestingProfileManager> profile_manager( |
| 51 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); |
| 52 EXPECT_TRUE(profile_manager->SetUp()); |
| 53 return profile_manager.Pass(); |
| 54 } |
| 55 |
47 // Helper class that tracks state transitions in BackgroundModeManager and | 56 // Helper class that tracks state transitions in BackgroundModeManager and |
48 // exposes them via getters (or gmock for EnableLaunchOnStartup). | 57 // exposes them via getters (or gmock for EnableLaunchOnStartup). |
49 class TestBackgroundModeManager : public StrictMock<BackgroundModeManager> { | 58 class TestBackgroundModeManager : public StrictMock<BackgroundModeManager> { |
50 public: | 59 public: |
51 TestBackgroundModeManager(const base::CommandLine& command_line, | 60 TestBackgroundModeManager(const base::CommandLine& command_line, |
52 ProfileInfoCache* cache) | 61 ProfileInfoCache* cache) |
53 : StrictMock<BackgroundModeManager>(command_line, cache), | 62 : StrictMock<BackgroundModeManager>(command_line, cache), |
54 have_status_tray_(false), | 63 have_status_tray_(false), |
55 has_shown_balloon_(false) { | 64 has_shown_balloon_(false) { |
56 ResumeBackgroundMode(); | 65 ResumeBackgroundMode(); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 | 155 |
147 class BackgroundModeManagerTest : public testing::Test { | 156 class BackgroundModeManagerTest : public testing::Test { |
148 public: | 157 public: |
149 BackgroundModeManagerTest() {} | 158 BackgroundModeManagerTest() {} |
150 ~BackgroundModeManagerTest() override {} | 159 ~BackgroundModeManagerTest() override {} |
151 | 160 |
152 void SetUp() override { | 161 void SetUp() override { |
153 command_line_.reset(new base::CommandLine(base::CommandLine::NO_PROGRAM)); | 162 command_line_.reset(new base::CommandLine(base::CommandLine::NO_PROGRAM)); |
154 profile_manager_ = CreateTestingProfileManager(); | 163 profile_manager_ = CreateTestingProfileManager(); |
155 profile_ = profile_manager_->CreateTestingProfile("p1"); | 164 profile_ = profile_manager_->CreateTestingProfile("p1"); |
| 165 chrome::DisableShutdownForTesting(true); |
| 166 } |
| 167 |
| 168 void TearDown() override { |
| 169 // Don't allow the browser to be closed because the shutdown procedure will |
| 170 // attempt to access objects that we haven't created (e.g., MessageCenter). |
| 171 browser_shutdown::SetTryingToQuit(true); |
| 172 chrome::DisableShutdownForTesting(false); |
| 173 browser_shutdown::SetTryingToQuit(false); |
156 } | 174 } |
157 | 175 |
158 protected: | 176 protected: |
159 scoped_refptr<extensions::Extension> CreateExtension( | 177 content::TestBrowserThreadBundle thread_bundle_; |
160 extensions::Manifest::Location location, | |
161 const std::string& data, | |
162 const std::string& id) { | |
163 scoped_ptr<base::DictionaryValue> parsed_manifest( | |
164 extensions::api_test_utils::ParseDictionary(data)); | |
165 return extensions::api_test_utils::CreateExtension( | |
166 location, parsed_manifest.get(), id); | |
167 } | |
168 | |
169 // From views::MenuModelAdapter::IsCommandEnabled with modification. | |
170 bool IsCommandEnabled(ui::MenuModel* model, int id) const { | |
171 int index = 0; | |
172 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) | |
173 return model->IsEnabledAt(index); | |
174 | |
175 return false; | |
176 } | |
177 | |
178 scoped_ptr<base::CommandLine> command_line_; | 178 scoped_ptr<base::CommandLine> command_line_; |
179 | 179 |
180 scoped_ptr<TestingProfileManager> profile_manager_; | 180 scoped_ptr<TestingProfileManager> profile_manager_; |
181 // Test profile used by all tests - this is owned by profile_manager_. | 181 // Test profile used by all tests - this is owned by profile_manager_. |
182 TestingProfile* profile_; | 182 TestingProfile* profile_; |
183 | 183 |
184 private: | 184 private: |
185 scoped_ptr<TestingProfileManager> CreateTestingProfileManager() { | |
186 scoped_ptr<TestingProfileManager> profile_manager | |
187 (new TestingProfileManager(TestingBrowserProcess::GetGlobal())); | |
188 EXPECT_TRUE(profile_manager->SetUp()); | |
189 return profile_manager.Pass(); | |
190 } | |
191 | |
192 DISALLOW_COPY_AND_ASSIGN(BackgroundModeManagerTest); | 185 DISALLOW_COPY_AND_ASSIGN(BackgroundModeManagerTest); |
193 }; | 186 }; |
194 | 187 |
195 class BackgroundModeManagerWithExtensionsTest | 188 class BackgroundModeManagerWithExtensionsTest : public testing::Test { |
196 : public BackgroundModeManagerTest { | |
197 public: | 189 public: |
198 BackgroundModeManagerWithExtensionsTest() {} | 190 BackgroundModeManagerWithExtensionsTest() {} |
199 ~BackgroundModeManagerWithExtensionsTest() override {} | 191 ~BackgroundModeManagerWithExtensionsTest() override {} |
200 | 192 |
201 void SetUp() override { | 193 void SetUp() override { |
202 BackgroundModeManagerTest::SetUp(); | 194 command_line_.reset(new base::CommandLine(base::CommandLine::NO_PROGRAM)); |
| 195 profile_manager_ = CreateTestingProfileManager(); |
| 196 profile_ = profile_manager_->CreateTestingProfile("p1"); |
203 | 197 |
204 // Aura clears notifications from the message center at shutdown. | 198 // Aura clears notifications from the message center at shutdown. |
205 message_center::MessageCenter::Initialize(); | 199 message_center::MessageCenter::Initialize(); |
206 | 200 |
207 // BackgroundModeManager actually affects Chrome start/stop state, | 201 // BackgroundModeManager actually affects Chrome start/stop state, |
208 // tearing down our thread bundle before we've had chance to clean | 202 // tearing down our thread bundle before we've had chance to clean |
209 // everything up. Keeping Chrome alive prevents this. | 203 // everything up. Keeping Chrome alive prevents this. |
210 // We aren't interested in if the keep alive works correctly in this test. | 204 // We aren't interested in if the keep alive works correctly in this test. |
211 chrome::IncrementKeepAliveCount(); | 205 chrome::IncrementKeepAliveCount(); |
212 | 206 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 // Message Center shutdown must occur after the DecrementKeepAliveCount | 253 // Message Center shutdown must occur after the DecrementKeepAliveCount |
260 // because DecrementKeepAliveCount will end up referencing the message | 254 // because DecrementKeepAliveCount will end up referencing the message |
261 // center during cleanup. | 255 // center during cleanup. |
262 message_center::MessageCenter::Shutdown(); | 256 message_center::MessageCenter::Shutdown(); |
263 | 257 |
264 // Clear the shutdown flag to isolate the remaining effect of this test. | 258 // Clear the shutdown flag to isolate the remaining effect of this test. |
265 browser_shutdown::SetTryingToQuit(false); | 259 browser_shutdown::SetTryingToQuit(false); |
266 } | 260 } |
267 | 261 |
268 protected: | 262 protected: |
| 263 scoped_refptr<extensions::Extension> CreateExtension( |
| 264 extensions::Manifest::Location location, |
| 265 const std::string& data, |
| 266 const std::string& id) { |
| 267 scoped_ptr<base::DictionaryValue> parsed_manifest( |
| 268 extensions::api_test_utils::ParseDictionary(data)); |
| 269 return extensions::api_test_utils::CreateExtension( |
| 270 location, parsed_manifest.get(), id); |
| 271 } |
| 272 |
| 273 // From views::MenuModelAdapter::IsCommandEnabled with modification. |
| 274 bool IsCommandEnabled(ui::MenuModel* model, int id) const { |
| 275 int index = 0; |
| 276 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) |
| 277 return model->IsEnabledAt(index); |
| 278 |
| 279 return false; |
| 280 } |
| 281 |
269 void AddEphemeralApp(const extensions::Extension* extension, | 282 void AddEphemeralApp(const extensions::Extension* extension, |
270 ExtensionService* service) { | 283 ExtensionService* service) { |
271 extensions::ExtensionPrefs* prefs = | 284 extensions::ExtensionPrefs* prefs = |
272 extensions::ExtensionPrefs::Get(service->profile()); | 285 extensions::ExtensionPrefs::Get(service->profile()); |
273 ASSERT_TRUE(prefs); | 286 ASSERT_TRUE(prefs); |
274 prefs->OnExtensionInstalled(extension, | 287 prefs->OnExtensionInstalled(extension, |
275 extensions::Extension::ENABLED, | 288 extensions::Extension::ENABLED, |
276 syncer::StringOrdinal(), | 289 syncer::StringOrdinal(), |
277 extensions::kInstallFlagIsEphemeral, | 290 extensions::kInstallFlagIsEphemeral, |
278 std::string()); | 291 std::string()); |
279 | 292 |
280 service->AddExtension(extension); | 293 service->AddExtension(extension); |
281 } | 294 } |
282 | 295 |
283 scoped_ptr<TestBackgroundModeManager> manager_; | 296 scoped_ptr<TestBackgroundModeManager> manager_; |
284 | 297 |
| 298 scoped_ptr<base::CommandLine> command_line_; |
| 299 |
| 300 scoped_ptr<TestingProfileManager> profile_manager_; |
| 301 // Test profile used by all tests - this is owned by profile_manager_. |
| 302 TestingProfile* profile_; |
| 303 |
285 private: | 304 private: |
286 // Required for extension service. | 305 // Required for extension service. |
287 content::TestBrowserThreadBundle thread_bundle_; | 306 content::TestBrowserThreadBundle thread_bundle_; |
288 | 307 |
289 #if defined(OS_CHROMEOS) | 308 #if defined(OS_CHROMEOS) |
290 // ChromeOS needs extra services to run in the following order. | 309 // ChromeOS needs extra services to run in the following order. |
291 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | 310 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
292 chromeos::ScopedTestCrosSettings test_cros_settings_; | 311 chromeos::ScopedTestCrosSettings test_cros_settings_; |
293 chromeos::ScopedTestUserManager test_user_manager_; | 312 chromeos::ScopedTestUserManager test_user_manager_; |
294 #endif | 313 #endif |
(...skipping 613 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
908 } | 927 } |
909 service->AddExtension(upgraded_bg_ext.get()); | 928 service->AddExtension(upgraded_bg_ext.get()); |
910 Mock::VerifyAndClearExpectations(manager_.get()); | 929 Mock::VerifyAndClearExpectations(manager_.get()); |
911 EXPECT_FALSE(manager_->HasShownBalloon()); | 930 EXPECT_FALSE(manager_->HasShownBalloon()); |
912 | 931 |
913 // Upgrading an extension that didn't have background to one that does should | 932 // Upgrading an extension that didn't have background to one that does should |
914 // show the balloon. | 933 // show the balloon. |
915 service->AddExtension(upgraded_no_bg_ext_has_bg.get()); | 934 service->AddExtension(upgraded_no_bg_ext_has_bg.get()); |
916 EXPECT_TRUE(manager_->HasShownBalloon()); | 935 EXPECT_TRUE(manager_->HasShownBalloon()); |
917 } | 936 } |
OLD | NEW |