| 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); |
| 156 } | 166 } |
| 157 | 167 |
| 168 void TearDown() override { chrome::DisableShutdownForTesting(false); } |
| 169 |
| 158 protected: | 170 protected: |
| 159 scoped_refptr<extensions::Extension> CreateExtension( | 171 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_; | 172 scoped_ptr<base::CommandLine> command_line_; |
| 179 | 173 |
| 180 scoped_ptr<TestingProfileManager> profile_manager_; | 174 scoped_ptr<TestingProfileManager> profile_manager_; |
| 181 // Test profile used by all tests - this is owned by profile_manager_. | 175 // Test profile used by all tests - this is owned by profile_manager_. |
| 182 TestingProfile* profile_; | 176 TestingProfile* profile_; |
| 183 | 177 |
| 184 private: | 178 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); | 179 DISALLOW_COPY_AND_ASSIGN(BackgroundModeManagerTest); |
| 193 }; | 180 }; |
| 194 | 181 |
| 195 class BackgroundModeManagerWithExtensionsTest | 182 class BackgroundModeManagerWithExtensionsTest : public testing::Test { |
| 196 : public BackgroundModeManagerTest { | |
| 197 public: | 183 public: |
| 198 BackgroundModeManagerWithExtensionsTest() {} | 184 BackgroundModeManagerWithExtensionsTest() {} |
| 199 ~BackgroundModeManagerWithExtensionsTest() override {} | 185 ~BackgroundModeManagerWithExtensionsTest() override {} |
| 200 | 186 |
| 201 void SetUp() override { | 187 void SetUp() override { |
| 202 BackgroundModeManagerTest::SetUp(); | 188 command_line_.reset(new base::CommandLine(base::CommandLine::NO_PROGRAM)); |
| 189 profile_manager_ = CreateTestingProfileManager(); |
| 190 profile_ = profile_manager_->CreateTestingProfile("p1"); |
| 203 | 191 |
| 204 // Aura clears notifications from the message center at shutdown. | 192 // Aura clears notifications from the message center at shutdown. |
| 205 message_center::MessageCenter::Initialize(); | 193 message_center::MessageCenter::Initialize(); |
| 206 | 194 |
| 207 // BackgroundModeManager actually affects Chrome start/stop state, | 195 // BackgroundModeManager actually affects Chrome start/stop state, |
| 208 // tearing down our thread bundle before we've had chance to clean | 196 // tearing down our thread bundle before we've had chance to clean |
| 209 // everything up. Keeping Chrome alive prevents this. | 197 // everything up. Keeping Chrome alive prevents this. |
| 210 // We aren't interested in if the keep alive works correctly in this test. | 198 // We aren't interested in if the keep alive works correctly in this test. |
| 211 chrome::IncrementKeepAliveCount(); | 199 chrome::IncrementKeepAliveCount(); |
| 212 | 200 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 // Message Center shutdown must occur after the DecrementKeepAliveCount | 247 // Message Center shutdown must occur after the DecrementKeepAliveCount |
| 260 // because DecrementKeepAliveCount will end up referencing the message | 248 // because DecrementKeepAliveCount will end up referencing the message |
| 261 // center during cleanup. | 249 // center during cleanup. |
| 262 message_center::MessageCenter::Shutdown(); | 250 message_center::MessageCenter::Shutdown(); |
| 263 | 251 |
| 264 // Clear the shutdown flag to isolate the remaining effect of this test. | 252 // Clear the shutdown flag to isolate the remaining effect of this test. |
| 265 browser_shutdown::SetTryingToQuit(false); | 253 browser_shutdown::SetTryingToQuit(false); |
| 266 } | 254 } |
| 267 | 255 |
| 268 protected: | 256 protected: |
| 257 scoped_refptr<extensions::Extension> CreateExtension( |
| 258 extensions::Manifest::Location location, |
| 259 const std::string& data, |
| 260 const std::string& id) { |
| 261 scoped_ptr<base::DictionaryValue> parsed_manifest( |
| 262 extensions::api_test_utils::ParseDictionary(data)); |
| 263 return extensions::api_test_utils::CreateExtension( |
| 264 location, parsed_manifest.get(), id); |
| 265 } |
| 266 |
| 267 // From views::MenuModelAdapter::IsCommandEnabled with modification. |
| 268 bool IsCommandEnabled(ui::MenuModel* model, int id) const { |
| 269 int index = 0; |
| 270 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) |
| 271 return model->IsEnabledAt(index); |
| 272 |
| 273 return false; |
| 274 } |
| 275 |
| 269 void AddEphemeralApp(const extensions::Extension* extension, | 276 void AddEphemeralApp(const extensions::Extension* extension, |
| 270 ExtensionService* service) { | 277 ExtensionService* service) { |
| 271 extensions::ExtensionPrefs* prefs = | 278 extensions::ExtensionPrefs* prefs = |
| 272 extensions::ExtensionPrefs::Get(service->profile()); | 279 extensions::ExtensionPrefs::Get(service->profile()); |
| 273 ASSERT_TRUE(prefs); | 280 ASSERT_TRUE(prefs); |
| 274 prefs->OnExtensionInstalled(extension, | 281 prefs->OnExtensionInstalled(extension, |
| 275 extensions::Extension::ENABLED, | 282 extensions::Extension::ENABLED, |
| 276 syncer::StringOrdinal(), | 283 syncer::StringOrdinal(), |
| 277 extensions::kInstallFlagIsEphemeral, | 284 extensions::kInstallFlagIsEphemeral, |
| 278 std::string()); | 285 std::string()); |
| 279 | 286 |
| 280 service->AddExtension(extension); | 287 service->AddExtension(extension); |
| 281 } | 288 } |
| 282 | 289 |
| 283 scoped_ptr<TestBackgroundModeManager> manager_; | 290 scoped_ptr<TestBackgroundModeManager> manager_; |
| 284 | 291 |
| 292 scoped_ptr<base::CommandLine> command_line_; |
| 293 |
| 294 scoped_ptr<TestingProfileManager> profile_manager_; |
| 295 // Test profile used by all tests - this is owned by profile_manager_. |
| 296 TestingProfile* profile_; |
| 297 |
| 285 private: | 298 private: |
| 286 // Required for extension service. | 299 // Required for extension service. |
| 287 content::TestBrowserThreadBundle thread_bundle_; | 300 content::TestBrowserThreadBundle thread_bundle_; |
| 288 | 301 |
| 289 #if defined(OS_CHROMEOS) | 302 #if defined(OS_CHROMEOS) |
| 290 // ChromeOS needs extra services to run in the following order. | 303 // ChromeOS needs extra services to run in the following order. |
| 291 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | 304 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
| 292 chromeos::ScopedTestCrosSettings test_cros_settings_; | 305 chromeos::ScopedTestCrosSettings test_cros_settings_; |
| 293 chromeos::ScopedTestUserManager test_user_manager_; | 306 chromeos::ScopedTestUserManager test_user_manager_; |
| 294 #endif | 307 #endif |
| (...skipping 613 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 908 } | 921 } |
| 909 service->AddExtension(upgraded_bg_ext.get()); | 922 service->AddExtension(upgraded_bg_ext.get()); |
| 910 Mock::VerifyAndClearExpectations(manager_.get()); | 923 Mock::VerifyAndClearExpectations(manager_.get()); |
| 911 EXPECT_FALSE(manager_->HasShownBalloon()); | 924 EXPECT_FALSE(manager_->HasShownBalloon()); |
| 912 | 925 |
| 913 // Upgrading an extension that didn't have background to one that does should | 926 // Upgrading an extension that didn't have background to one that does should |
| 914 // show the balloon. | 927 // show the balloon. |
| 915 service->AddExtension(upgraded_no_bg_ext_has_bg.get()); | 928 service->AddExtension(upgraded_no_bg_ext_has_bg.get()); |
| 916 EXPECT_TRUE(manager_->HasShownBalloon()); | 929 EXPECT_TRUE(manager_->HasShownBalloon()); |
| 917 } | 930 } |
| OLD | NEW |