Chromium Code Reviews| 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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 139 | 148 |
| 140 private: | 149 private: |
| 141 bool enabled_; | 150 bool enabled_; |
| 142 std::map<Profile*, int> profile_app_counts_; | 151 std::map<Profile*, int> profile_app_counts_; |
| 143 | 152 |
| 144 DISALLOW_COPY_AND_ASSIGN(AdvancedTestBackgroundModeManager); | 153 DISALLOW_COPY_AND_ASSIGN(AdvancedTestBackgroundModeManager); |
| 145 }; | 154 }; |
| 146 | 155 |
| 147 class BackgroundModeManagerTest : public testing::Test { | 156 class BackgroundModeManagerTest : public testing::Test { |
| 148 public: | 157 public: |
| 149 BackgroundModeManagerTest() {} | 158 BackgroundModeManagerTest() |
| 159 : dummy_task_runner_(new base::TestSimpleTaskRunner()) {} | |
| 150 ~BackgroundModeManagerTest() override {} | 160 ~BackgroundModeManagerTest() override {} |
| 151 | 161 |
| 152 void SetUp() override { | 162 void SetUp() override { |
| 153 command_line_.reset(new base::CommandLine(base::CommandLine::NO_PROGRAM)); | 163 command_line_.reset(new base::CommandLine(base::CommandLine::NO_PROGRAM)); |
| 154 profile_manager_ = CreateTestingProfileManager(); | 164 profile_manager_ = CreateTestingProfileManager(); |
| 155 profile_ = profile_manager_->CreateTestingProfile("p1"); | 165 profile_ = profile_manager_->CreateTestingProfile("p1"); |
| 156 } | 166 } |
| 157 | 167 |
| 158 protected: | 168 protected: |
| 159 scoped_refptr<extensions::Extension> CreateExtension( | 169 // We use a dummy task runner instead of the full test browser thread bundle |
| 160 extensions::Manifest::Location location, | 170 // to avoid having the browser's shutdown logic try to access a non-existent |
|
Andrew T Wilson (Slow)
2015/04/22 14:48:03
I guess I'm not sure I understand - are you saying
Sami
2015/04/22 18:06:45
Disclaimer: that was just the first thing I manage
| |
| 161 const std::string& data, | 171 // objects such as the MessageCenter during teardown. |
| 162 const std::string& id) { | 172 base::ThreadTaskRunnerHandle dummy_task_runner_; |
| 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_; | 173 scoped_ptr<base::CommandLine> command_line_; |
| 179 | 174 |
| 180 scoped_ptr<TestingProfileManager> profile_manager_; | 175 scoped_ptr<TestingProfileManager> profile_manager_; |
| 181 // Test profile used by all tests - this is owned by profile_manager_. | 176 // Test profile used by all tests - this is owned by profile_manager_. |
| 182 TestingProfile* profile_; | 177 TestingProfile* profile_; |
| 183 | 178 |
| 184 private: | 179 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); | 180 DISALLOW_COPY_AND_ASSIGN(BackgroundModeManagerTest); |
| 193 }; | 181 }; |
| 194 | 182 |
| 195 class BackgroundModeManagerWithExtensionsTest | 183 class BackgroundModeManagerWithExtensionsTest : public testing::Test { |
| 196 : public BackgroundModeManagerTest { | |
| 197 public: | 184 public: |
| 198 BackgroundModeManagerWithExtensionsTest() {} | 185 BackgroundModeManagerWithExtensionsTest() {} |
| 199 ~BackgroundModeManagerWithExtensionsTest() override {} | 186 ~BackgroundModeManagerWithExtensionsTest() override {} |
| 200 | 187 |
| 201 void SetUp() override { | 188 void SetUp() override { |
| 202 BackgroundModeManagerTest::SetUp(); | 189 command_line_.reset(new base::CommandLine(base::CommandLine::NO_PROGRAM)); |
| 190 profile_manager_ = CreateTestingProfileManager(); | |
| 191 profile_ = profile_manager_->CreateTestingProfile("p1"); | |
| 203 | 192 |
| 204 // Aura clears notifications from the message center at shutdown. | 193 // Aura clears notifications from the message center at shutdown. |
| 205 message_center::MessageCenter::Initialize(); | 194 message_center::MessageCenter::Initialize(); |
| 206 | 195 |
| 207 // BackgroundModeManager actually affects Chrome start/stop state, | 196 // BackgroundModeManager actually affects Chrome start/stop state, |
| 208 // tearing down our thread bundle before we've had chance to clean | 197 // tearing down our thread bundle before we've had chance to clean |
| 209 // everything up. Keeping Chrome alive prevents this. | 198 // everything up. Keeping Chrome alive prevents this. |
| 210 // We aren't interested in if the keep alive works correctly in this test. | 199 // We aren't interested in if the keep alive works correctly in this test. |
| 211 chrome::IncrementKeepAliveCount(); | 200 chrome::IncrementKeepAliveCount(); |
| 212 | 201 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 259 // Message Center shutdown must occur after the DecrementKeepAliveCount | 248 // Message Center shutdown must occur after the DecrementKeepAliveCount |
| 260 // because DecrementKeepAliveCount will end up referencing the message | 249 // because DecrementKeepAliveCount will end up referencing the message |
| 261 // center during cleanup. | 250 // center during cleanup. |
| 262 message_center::MessageCenter::Shutdown(); | 251 message_center::MessageCenter::Shutdown(); |
| 263 | 252 |
| 264 // Clear the shutdown flag to isolate the remaining effect of this test. | 253 // Clear the shutdown flag to isolate the remaining effect of this test. |
| 265 browser_shutdown::SetTryingToQuit(false); | 254 browser_shutdown::SetTryingToQuit(false); |
| 266 } | 255 } |
| 267 | 256 |
| 268 protected: | 257 protected: |
| 258 scoped_refptr<extensions::Extension> CreateExtension( | |
| 259 extensions::Manifest::Location location, | |
| 260 const std::string& data, | |
| 261 const std::string& id) { | |
| 262 scoped_ptr<base::DictionaryValue> parsed_manifest( | |
| 263 extensions::api_test_utils::ParseDictionary(data)); | |
| 264 return extensions::api_test_utils::CreateExtension( | |
| 265 location, parsed_manifest.get(), id); | |
| 266 } | |
| 267 | |
| 268 // From views::MenuModelAdapter::IsCommandEnabled with modification. | |
| 269 bool IsCommandEnabled(ui::MenuModel* model, int id) const { | |
| 270 int index = 0; | |
| 271 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) | |
| 272 return model->IsEnabledAt(index); | |
| 273 | |
| 274 return false; | |
| 275 } | |
| 276 | |
| 269 void AddEphemeralApp(const extensions::Extension* extension, | 277 void AddEphemeralApp(const extensions::Extension* extension, |
| 270 ExtensionService* service) { | 278 ExtensionService* service) { |
| 271 extensions::ExtensionPrefs* prefs = | 279 extensions::ExtensionPrefs* prefs = |
| 272 extensions::ExtensionPrefs::Get(service->profile()); | 280 extensions::ExtensionPrefs::Get(service->profile()); |
| 273 ASSERT_TRUE(prefs); | 281 ASSERT_TRUE(prefs); |
| 274 prefs->OnExtensionInstalled(extension, | 282 prefs->OnExtensionInstalled(extension, |
| 275 extensions::Extension::ENABLED, | 283 extensions::Extension::ENABLED, |
| 276 syncer::StringOrdinal(), | 284 syncer::StringOrdinal(), |
| 277 extensions::kInstallFlagIsEphemeral, | 285 extensions::kInstallFlagIsEphemeral, |
| 278 std::string()); | 286 std::string()); |
| 279 | 287 |
| 280 service->AddExtension(extension); | 288 service->AddExtension(extension); |
| 281 } | 289 } |
| 282 | 290 |
| 283 scoped_ptr<TestBackgroundModeManager> manager_; | 291 scoped_ptr<TestBackgroundModeManager> manager_; |
| 284 | 292 |
| 293 scoped_ptr<base::CommandLine> command_line_; | |
| 294 | |
| 295 scoped_ptr<TestingProfileManager> profile_manager_; | |
| 296 // Test profile used by all tests - this is owned by profile_manager_. | |
| 297 TestingProfile* profile_; | |
| 298 | |
| 285 private: | 299 private: |
| 286 // Required for extension service. | 300 // Required for extension service. |
| 287 content::TestBrowserThreadBundle thread_bundle_; | 301 content::TestBrowserThreadBundle thread_bundle_; |
| 288 | 302 |
| 289 #if defined(OS_CHROMEOS) | 303 #if defined(OS_CHROMEOS) |
| 290 // ChromeOS needs extra services to run in the following order. | 304 // ChromeOS needs extra services to run in the following order. |
| 291 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | 305 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
| 292 chromeos::ScopedTestCrosSettings test_cros_settings_; | 306 chromeos::ScopedTestCrosSettings test_cros_settings_; |
| 293 chromeos::ScopedTestUserManager test_user_manager_; | 307 chromeos::ScopedTestUserManager test_user_manager_; |
| 294 #endif | 308 #endif |
| (...skipping 613 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 908 } | 922 } |
| 909 service->AddExtension(upgraded_bg_ext.get()); | 923 service->AddExtension(upgraded_bg_ext.get()); |
| 910 Mock::VerifyAndClearExpectations(manager_.get()); | 924 Mock::VerifyAndClearExpectations(manager_.get()); |
| 911 EXPECT_FALSE(manager_->HasShownBalloon()); | 925 EXPECT_FALSE(manager_->HasShownBalloon()); |
| 912 | 926 |
| 913 // Upgrading an extension that didn't have background to one that does should | 927 // Upgrading an extension that didn't have background to one that does should |
| 914 // show the balloon. | 928 // show the balloon. |
| 915 service->AddExtension(upgraded_no_bg_ext_has_bg.get()); | 929 service->AddExtension(upgraded_no_bg_ext_has_bg.get()); |
| 916 EXPECT_TRUE(manager_->HasShownBalloon()); | 930 EXPECT_TRUE(manager_->HasShownBalloon()); |
| 917 } | 931 } |
| OLD | NEW |