| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/update_install_gate.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/command_line.h" |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/extensions/test_extension_system.h" |
| 14 #include "chrome/test/base/testing_browser_process.h" |
| 15 #include "chrome/test/base/testing_profile.h" |
| 16 #include "chrome/test/base/testing_profile_manager.h" |
| 17 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "content/public/test/test_renderer_host.h" |
| 19 #include "extensions/browser/event_router.h" |
| 20 #include "extensions/browser/event_router_factory.h" |
| 21 #include "extensions/browser/extension_host.h" |
| 22 #include "extensions/browser/extension_prefs.h" |
| 23 #include "extensions/browser/extension_registry.h" |
| 24 #include "extensions/common/extension_builder.h" |
| 25 #include "extensions/common/manifest_handlers/background_info.h" |
| 26 #include "extensions/common/value_builder.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" |
| 28 |
| 29 #if defined(OS_CHROMEOS) |
| 30 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" |
| 31 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
| 32 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 33 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 34 #endif |
| 35 |
| 36 namespace extensions { |
| 37 |
| 38 namespace { |
| 39 |
| 40 const char kAppId[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
| 41 const char kPersistentExtensionId[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; |
| 42 const char kNonPersistentExtensionId[] = "cccccccccccccccccccccccccccccccc"; |
| 43 |
| 44 std::unique_ptr<KeyedService> BuildEventRouter( |
| 45 content::BrowserContext* profile) { |
| 46 return base::WrapUnique(new extensions::EventRouter(profile, nullptr)); |
| 47 } |
| 48 |
| 49 scoped_refptr<Extension> CreateApp(const std::string& extension_id, |
| 50 const std::string& version) { |
| 51 scoped_refptr<Extension> app = |
| 52 ExtensionBuilder() |
| 53 .SetManifest( |
| 54 DictionaryBuilder() |
| 55 .Set("name", "Test app") |
| 56 .Set("version", version) |
| 57 .Set("manifest_version", 2) |
| 58 .Set("app", |
| 59 DictionaryBuilder() |
| 60 .Set("background", |
| 61 DictionaryBuilder() |
| 62 .Set("scripts", ListBuilder() |
| 63 .Append("background.js") |
| 64 .Build()) |
| 65 .Build()) |
| 66 .Build()) |
| 67 .Build()) |
| 68 .SetID(extension_id) |
| 69 .Build(); |
| 70 return app; |
| 71 } |
| 72 |
| 73 scoped_refptr<Extension> CreateExtension(const std::string& extension_id, |
| 74 const std::string& version, |
| 75 bool persistent) { |
| 76 scoped_refptr<Extension> extension = |
| 77 ExtensionBuilder() |
| 78 .SetManifest( |
| 79 DictionaryBuilder() |
| 80 .Set("name", "Test extension") |
| 81 .Set("version", version) |
| 82 .Set("manifest_version", 2) |
| 83 .Set("background", DictionaryBuilder() |
| 84 .Set("page", "background.html") |
| 85 .SetBoolean("persistent", persistent) |
| 86 .Build()) |
| 87 .Build()) |
| 88 .SetID(extension_id) |
| 89 .Build(); |
| 90 return extension; |
| 91 } |
| 92 |
| 93 ExtensionHost* CreateHost(Profile* profile, const Extension* app) { |
| 94 ProcessManager::Get(profile)->CreateBackgroundHost( |
| 95 app, BackgroundInfo::GetBackgroundURL(app)); |
| 96 base::RunLoop().RunUntilIdle(); |
| 97 |
| 98 return ProcessManager::Get(profile)->GetBackgroundHostForExtension(app->id()); |
| 99 } |
| 100 |
| 101 } // namespace |
| 102 |
| 103 class UpdateInstallGateTest : public testing::Test { |
| 104 public: |
| 105 UpdateInstallGateTest() { |
| 106 profile_manager_.reset( |
| 107 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); |
| 108 } |
| 109 |
| 110 // testing::Test |
| 111 void SetUp() override { |
| 112 // Must be called from ::testing::Test::SetUp. |
| 113 ASSERT_TRUE(profile_manager_->SetUp()); |
| 114 |
| 115 const char kUserProfile[] = "profile1@example.com"; |
| 116 #if defined(OS_CHROMEOS) |
| 117 const AccountId account_id(AccountId::FromUserEmail(kUserProfile)); |
| 118 // Needed to allow ChromeProcessManagerDelegate to allow background pages. |
| 119 fake_user_manager_ = new chromeos::FakeChromeUserManager(); |
| 120 // Takes ownership of fake_user_manager_. |
| 121 scoped_user_manager_enabler_.reset( |
| 122 new chromeos::ScopedUserManagerEnabler(fake_user_manager_)); |
| 123 fake_user_manager_->AddUser(account_id); |
| 124 fake_user_manager_->LoginUser(account_id); |
| 125 #endif |
| 126 profile_ = profile_manager_->CreateTestingProfile(kUserProfile); |
| 127 profile_manager_->SetLoggedIn(true); |
| 128 |
| 129 TestExtensionSystem* test_extension_system = |
| 130 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile_)); |
| 131 service_ = test_extension_system->CreateExtensionService( |
| 132 base::CommandLine::ForCurrentProcess(), |
| 133 base::FilePath() /* install_directory */, |
| 134 false /* autoupdate_enabled */); |
| 135 registry_ = ExtensionRegistry::Get(profile_); |
| 136 |
| 137 event_router_ = static_cast<EventRouter*>( |
| 138 EventRouterFactory::GetInstance()->SetTestingFactoryAndUse( |
| 139 profile_, &BuildEventRouter)); |
| 140 |
| 141 delayer_.reset(new UpdateInstallGate(service_)); |
| 142 |
| 143 new_app_ = CreateApp(kAppId, "2.0"); |
| 144 new_persistent_ = CreateExtension(kPersistentExtensionId, "2.0", true); |
| 145 new_none_persistent_ = |
| 146 CreateExtension(kNonPersistentExtensionId, "2.0", false); |
| 147 } |
| 148 |
| 149 void TearDown() override { profile_manager_->DeleteAllTestingProfiles(); } |
| 150 |
| 151 void AddExistingExtensions() { |
| 152 scoped_refptr<Extension> app = CreateApp(kAppId, "1.0"); |
| 153 registry_->AddEnabled(app); |
| 154 |
| 155 scoped_refptr<Extension> persistent = |
| 156 CreateExtension(kPersistentExtensionId, "1.0", true); |
| 157 registry_->AddEnabled(persistent); |
| 158 |
| 159 scoped_refptr<Extension> none_persistent = |
| 160 CreateExtension(kNonPersistentExtensionId, "1.0", false); |
| 161 registry_->AddEnabled(none_persistent); |
| 162 } |
| 163 |
| 164 void MakeExtensionInUse(const std::string& extension_id) { |
| 165 const Extension* const extension = |
| 166 registry_->GetInstalledExtension(extension_id); |
| 167 ASSERT_TRUE(!!extension); |
| 168 ASSERT_TRUE(!!CreateHost(profile_, extension)); |
| 169 } |
| 170 |
| 171 void MakeExtensionListenForOnUpdateAvailable( |
| 172 const std::string& extension_id) { |
| 173 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable"; |
| 174 event_router_->AddEventListener(kOnUpdateAvailableEvent, NULL, |
| 175 extension_id); |
| 176 } |
| 177 |
| 178 void Check(const Extension* extension, |
| 179 bool is_in_use, |
| 180 bool has_listener, |
| 181 bool install_immediately, |
| 182 InstallGate::Action expected_action) { |
| 183 if (is_in_use) |
| 184 MakeExtensionInUse(extension->id()); |
| 185 if (has_listener) |
| 186 MakeExtensionListenForOnUpdateAvailable(extension->id()); |
| 187 |
| 188 EXPECT_EQ(expected_action, |
| 189 delayer()->ShouldDelay(extension, install_immediately)); |
| 190 } |
| 191 |
| 192 UpdateInstallGate* delayer() { return delayer_.get(); } |
| 193 ExtensionService* service() { return service_; } |
| 194 |
| 195 const Extension* new_app() const { return new_app_.get(); } |
| 196 const Extension* new_persistent() const { return new_persistent_.get(); } |
| 197 const Extension* new_none_persistent() const { |
| 198 return new_none_persistent_.get(); |
| 199 } |
| 200 |
| 201 private: |
| 202 // Needed by extension system. |
| 203 content::TestBrowserThreadBundle thread_bundle_; |
| 204 |
| 205 // Needed to ensure we don't end up creating actual RenderViewHosts |
| 206 // and RenderProcessHosts. |
| 207 content::RenderViewHostTestEnabler render_view_host_test_enabler_; |
| 208 |
| 209 TestingProfile* profile_ = nullptr; |
| 210 std::unique_ptr<TestingProfileManager> profile_manager_; |
| 211 |
| 212 ExtensionService* service_ = nullptr; |
| 213 ExtensionRegistry* registry_ = nullptr; |
| 214 EventRouter* event_router_ = nullptr; |
| 215 |
| 216 #if defined(OS_CHROMEOS) |
| 217 // Needed for creating ExtensionService. |
| 218 chromeos::FakeChromeUserManager* fake_user_manager_ = nullptr; |
| 219 std::unique_ptr<chromeos::ScopedUserManagerEnabler> |
| 220 scoped_user_manager_enabler_; |
| 221 #endif |
| 222 |
| 223 std::unique_ptr<UpdateInstallGate> delayer_; |
| 224 |
| 225 scoped_refptr<Extension> new_app_; |
| 226 scoped_refptr<Extension> new_persistent_; |
| 227 scoped_refptr<Extension> new_none_persistent_; |
| 228 |
| 229 DISALLOW_COPY_AND_ASSIGN(UpdateInstallGateTest); |
| 230 }; |
| 231 |
| 232 TEST_F(UpdateInstallGateTest, InstallOnServiceNotReady) { |
| 233 ASSERT_FALSE(service()->is_ready()); |
| 234 Check(new_app(), false, false, false, InstallGate::INSTALL); |
| 235 Check(new_persistent(), false, false, false, InstallGate::INSTALL); |
| 236 Check(new_none_persistent(), false, false, false, InstallGate::INSTALL); |
| 237 } |
| 238 |
| 239 TEST_F(UpdateInstallGateTest, InstallOnFirstInstall) { |
| 240 service()->Init(); |
| 241 Check(new_app(), false, false, false, InstallGate::INSTALL); |
| 242 Check(new_persistent(), false, false, false, InstallGate::INSTALL); |
| 243 Check(new_none_persistent(), false, false, false, InstallGate::INSTALL); |
| 244 } |
| 245 |
| 246 TEST_F(UpdateInstallGateTest, InstallOnInstallImmediately) { |
| 247 service()->Init(); |
| 248 AddExistingExtensions(); |
| 249 |
| 250 const bool kInstallImmediately = true; |
| 251 for (bool in_use : {false, true}) { |
| 252 for (bool has_listener : {false, true}) { |
| 253 Check(new_app(), in_use, has_listener, kInstallImmediately, |
| 254 InstallGate::INSTALL); |
| 255 Check(new_persistent(), in_use, has_listener, kInstallImmediately, |
| 256 InstallGate::INSTALL); |
| 257 Check(new_none_persistent(), in_use, has_listener, kInstallImmediately, |
| 258 InstallGate::INSTALL); |
| 259 } |
| 260 } |
| 261 } |
| 262 |
| 263 TEST_F(UpdateInstallGateTest, DelayInstallWhenInUse) { |
| 264 service()->Init(); |
| 265 AddExistingExtensions(); |
| 266 |
| 267 const bool kInUse = true; |
| 268 const bool kDontInstallImmediately = false; |
| 269 for (bool has_listener : {false, true}) { |
| 270 Check(new_app(), kInUse, has_listener, kDontInstallImmediately, |
| 271 InstallGate::DELAY); |
| 272 Check(new_persistent(), kInUse, has_listener, kDontInstallImmediately, |
| 273 has_listener ? InstallGate::DELAY : InstallGate::INSTALL); |
| 274 Check(new_none_persistent(), kInUse, has_listener, kDontInstallImmediately, |
| 275 InstallGate::DELAY); |
| 276 } |
| 277 } |
| 278 |
| 279 } // namespace extensions |
| OLD | NEW |