1 // Copyright (c) 2012 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.
6 #include "base/bind_helpers.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/prefs/scoped_user_pref_update.h"
10 #include "base/stl_util.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/extensions/extension_browsertest.h"
13 #include "chrome/browser/extensions/extension_host.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extension_system.h"
16 #include "chrome/browser/extensions/extension_test_message_listener.h"
17 #include "chrome/browser/extensions/external_policy_loader.h"
18 #include "chrome/browser/extensions/updater/extension_downloader.h"
19 #include "chrome/browser/extensions/updater/extension_updater.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/ui/browser.h"
22 #include "chrome/common/pref_names.h"
23 #include "chrome/common/url_constants.h"
24 #include "chrome/test/base/ui_test_utils.h"
25 #include "content/public/browser/notification_service.h"
26 #include "content/public/browser/render_view_host.h"
27 #include "content/public/test/browser_test_utils.h"
28 #include "content/test/net/url_request_prepackaged_interceptor.h"
29 #include "extensions/browser/pref_names.h"
30 #include "net/url_request/url_fetcher.h"
32 using extensions::Extension;
33 using extensions::Manifest;
35 class ExtensionManagementTest : public ExtensionBrowserTest {
37 // Helper method that returns whether the extension is at the given version.
38 // This calls version(), which must be defined in the extension's bg page,
39 // as well as asking the extension itself.
41 // Note that 'version' here means something different than the version field
42 // in the extension's manifest. We use the version as reported by the
43 // background page to test how overinstalling crx files with the same
44 // manifest version works.
45 bool IsExtensionAtVersion(const Extension* extension,
46 const std::string& expected_version) {
47 // Test that the extension's version from the manifest and reported by the
48 // background page is correct. This is to ensure that the processes are in
49 // sync with the Extension.
50 extensions::ProcessManager* manager =
51 extensions::ExtensionSystem::Get(browser()->profile())->
53 extensions::ExtensionHost* ext_host =
54 manager->GetBackgroundHostForExtension(extension->id());
55 EXPECT_TRUE(ext_host);
59 std::string version_from_bg;
60 bool exec = content::ExecuteScriptAndExtractString(
61 ext_host->render_view_host(), "version()", &version_from_bg);
66 if (version_from_bg != expected_version ||
67 extension->VersionString() != expected_version)
74 // Times out sometimes on Linux. http://crbug.com/89727
75 #define MAYBE_InstallSameVersion DISABLED_InstallSameVersion
77 #define MAYBE_InstallSameVersion InstallSameVersion
80 // Tests that installing the same version overwrites.
81 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, MAYBE_InstallSameVersion) {
82 const Extension* extension = InstallExtension(
83 test_data_dir_.AppendASCII("install/install.crx"), 1);
84 ASSERT_TRUE(extension);
85 base::FilePath old_path = extension->path();
87 // Install an extension with the same version. The previous install should be
89 extension = InstallExtension(
90 test_data_dir_.AppendASCII("install/install_same_version.crx"), 0);
91 ASSERT_TRUE(extension);
92 base::FilePath new_path = extension->path();
94 EXPECT_FALSE(IsExtensionAtVersion(extension, "1.0"));
95 EXPECT_NE(old_path.value(), new_path.value());
98 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, InstallOlderVersion) {
99 const Extension* extension = InstallExtension(
100 test_data_dir_.AppendASCII("install/install.crx"), 1);
101 ASSERT_TRUE(extension);
102 ASSERT_FALSE(InstallExtension(
103 test_data_dir_.AppendASCII("install/install_older_version.crx"), 0));
104 EXPECT_TRUE(IsExtensionAtVersion(extension, "1.0"));
107 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, InstallThenCancel) {
108 const Extension* extension = InstallExtension(
109 test_data_dir_.AppendASCII("install/install.crx"), 1);
110 ASSERT_TRUE(extension);
112 // Cancel this install.
113 ASSERT_FALSE(StartInstallButCancel(
114 test_data_dir_.AppendASCII("install/install_v2.crx")));
115 EXPECT_TRUE(IsExtensionAtVersion(extension, "1.0"));
119 // http://crbug.com/141913
120 #define MAYBE_InstallRequiresConfirm DISABLED_InstallRequiresConfirm
122 #define MAYBE_InstallRequiresConfirm InstallRequiresConfirm
124 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, MAYBE_InstallRequiresConfirm) {
125 // Installing the extension without an auto confirming UI should result in
126 // it being disabled, since good.crx has permissions that require approval.
127 ExtensionService* service = extensions::ExtensionSystem::Get(
128 browser()->profile())->extension_service();
129 std::string id = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
130 ASSERT_FALSE(InstallExtension(test_data_dir_.AppendASCII("good.crx"), 0));
131 ASSERT_TRUE(service->GetExtensionById(id, true));
132 UninstallExtension(id);
134 // And the install should succeed when the permissions are accepted.
135 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(
136 test_data_dir_.AppendASCII("good.crx"), 1, browser()));
137 UninstallExtension(id);
140 // Tests that disabling and re-enabling an extension works.
141 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, DisableEnable) {
142 extensions::ProcessManager* manager =
143 extensions::ExtensionSystem::Get(browser()->profile())->process_manager();
144 ExtensionService* service = extensions::ExtensionSystem::Get(
145 browser()->profile())->extension_service();
146 const size_t size_before = service->extensions()->size();
148 // Load an extension, expect the background page to be available.
149 std::string extension_id = "bjafgdebaacbbbecmhlhpofkepfkgcpa";
150 ASSERT_TRUE(LoadExtension(
151 test_data_dir_.AppendASCII("good").AppendASCII("Extensions")
152 .AppendASCII(extension_id)
153 .AppendASCII("1.0")));
154 ASSERT_EQ(size_before + 1, service->extensions()->size());
155 EXPECT_EQ(0u, service->disabled_extensions()->size());
156 EXPECT_TRUE(manager->GetBackgroundHostForExtension(extension_id));
158 // After disabling, the background page should go away.
159 DisableExtension(extension_id);
160 EXPECT_EQ(size_before, service->extensions()->size());
161 EXPECT_EQ(1u, service->disabled_extensions()->size());
162 EXPECT_FALSE(manager->GetBackgroundHostForExtension(extension_id));
164 // And bring it back.
165 EnableExtension(extension_id);
166 EXPECT_EQ(size_before + 1, service->extensions()->size());
167 EXPECT_EQ(0u, service->disabled_extensions()->size());
168 EXPECT_TRUE(manager->GetBackgroundHostForExtension(extension_id));
171 // Used for testing notifications sent during extension updates.
172 class NotificationListener : public content::NotificationObserver {
174 NotificationListener() : started_(false), finished_(false) {
176 chrome::NOTIFICATION_EXTENSION_UPDATING_STARTED,
177 chrome::NOTIFICATION_EXTENSION_UPDATE_FOUND
179 for (size_t i = 0; i < arraysize(types); i++) {
181 this, types[i], content::NotificationService::AllSources());
184 virtual ~NotificationListener() {}
186 bool started() { return started_; }
188 bool finished() { return finished_; }
190 const std::set<std::string>& updates() { return updates_; }
198 // Implements content::NotificationObserver interface.
199 virtual void Observe(int type,
200 const content::NotificationSource& source,
201 const content::NotificationDetails& details) OVERRIDE {
203 case chrome::NOTIFICATION_EXTENSION_UPDATING_STARTED: {
204 EXPECT_FALSE(started_);
208 case chrome::NOTIFICATION_EXTENSION_UPDATE_FOUND: {
209 const std::string& id =
210 content::Details<extensions::UpdateDetails>(details)->id;
220 EXPECT_FALSE(finished_);
225 content::NotificationRegistrar registrar_;
227 // Did we see EXTENSION_UPDATING_STARTED?
230 // Did we see EXTENSION_UPDATING_FINISHED?
233 // The set of extension id's we've seen via EXTENSION_UPDATE_FOUND.
234 std::set<std::string> updates_;
238 // Fails consistently on Windows XP, see: http://crbug.com/120640.
239 #define MAYBE_AutoUpdate DISABLED_AutoUpdate
241 // See http://crbug.com/103371 and http://crbug.com/120640.
242 #if defined(ADDRESS_SANITIZER)
243 #define MAYBE_AutoUpdate DISABLED_AutoUpdate
245 #define MAYBE_AutoUpdate AutoUpdate
249 // Tests extension autoupdate.
250 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, MAYBE_AutoUpdate) {
251 NotificationListener notification_listener;
252 base::FilePath basedir = test_data_dir_.AppendASCII("autoupdate");
253 // Note: This interceptor gets requests on the IO thread.
254 content::URLLocalHostRequestPrepackagedInterceptor interceptor;
255 net::URLFetcher::SetEnableInterceptionForTests(true);
257 interceptor.SetResponseIgnoreQuery(
258 GURL("http://localhost/autoupdate/manifest"),
259 basedir.AppendASCII("manifest_v2.xml"));
260 interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
261 basedir.AppendASCII("v2.crx"));
263 // Install version 1 of the extension.
264 ExtensionTestMessageListener listener1("v1 installed", false);
265 ExtensionService* service = extensions::ExtensionSystem::Get(
266 browser()->profile())->extension_service();
267 const size_t size_before = service->extensions()->size();
268 ASSERT_TRUE(service->disabled_extensions()->is_empty());
269 const Extension* extension =
270 InstallExtension(basedir.AppendASCII("v1.crx"), 1);
271 ASSERT_TRUE(extension);
272 listener1.WaitUntilSatisfied();
273 ASSERT_EQ(size_before + 1, service->extensions()->size());
274 ASSERT_EQ("ogjcoiohnmldgjemafoockdghcjciccf", extension->id());
275 ASSERT_EQ("1.0", extension->VersionString());
277 extensions::ExtensionUpdater::CheckParams params;
279 base::Bind(&NotificationListener::OnFinished,
280 base::Unretained(¬ification_listener));
282 // Run autoupdate and make sure version 2 of the extension was installed.
283 ExtensionTestMessageListener listener2("v2 installed", false);
284 service->updater()->CheckNow(params);
285 ASSERT_TRUE(WaitForExtensionInstall());
286 listener2.WaitUntilSatisfied();
287 ASSERT_EQ(size_before + 1, service->extensions()->size());
288 extension = service->GetExtensionById(
289 "ogjcoiohnmldgjemafoockdghcjciccf", false);
290 ASSERT_TRUE(extension);
291 ASSERT_EQ("2.0", extension->VersionString());
292 ASSERT_TRUE(notification_listener.started());
293 ASSERT_TRUE(notification_listener.finished());
294 ASSERT_TRUE(ContainsKey(notification_listener.updates(),
295 "ogjcoiohnmldgjemafoockdghcjciccf"));
296 notification_listener.Reset();
298 // Now try doing an update to version 3, which has been incorrectly
299 // signed. This should fail.
300 interceptor.SetResponseIgnoreQuery(
301 GURL("http://localhost/autoupdate/manifest"),
302 basedir.AppendASCII("manifest_v3.xml"));
303 interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v3.crx"),
304 basedir.AppendASCII("v3.crx"));
306 service->updater()->CheckNow(params);
307 ASSERT_TRUE(WaitForExtensionInstallError());
308 ASSERT_TRUE(notification_listener.started());
309 ASSERT_TRUE(notification_listener.finished());
310 ASSERT_TRUE(ContainsKey(notification_listener.updates(),
311 "ogjcoiohnmldgjemafoockdghcjciccf"));
313 // Make sure the extension state is the same as before.
314 ASSERT_EQ(size_before + 1, service->extensions()->size());
315 extension = service->GetExtensionById(
316 "ogjcoiohnmldgjemafoockdghcjciccf", false);
317 ASSERT_TRUE(extension);
318 ASSERT_EQ("2.0", extension->VersionString());
322 // Fails consistently on Windows XP, see: http://crbug.com/120640.
323 #define MAYBE_AutoUpdateDisabledExtensions DISABLED_AutoUpdateDisabledExtensions
325 #if defined(ADDRESS_SANITIZER)
326 #define MAYBE_AutoUpdateDisabledExtensions DISABLED_AutoUpdateDisabledExtensions
328 #define MAYBE_AutoUpdateDisabledExtensions AutoUpdateDisabledExtensions
332 // Tests extension autoupdate.
333 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest,
334 MAYBE_AutoUpdateDisabledExtensions) {
335 NotificationListener notification_listener;
336 base::FilePath basedir = test_data_dir_.AppendASCII("autoupdate");
337 // Note: This interceptor gets requests on the IO thread.
338 content::URLLocalHostRequestPrepackagedInterceptor interceptor;
339 net::URLFetcher::SetEnableInterceptionForTests(true);
341 interceptor.SetResponseIgnoreQuery(
342 GURL("http://localhost/autoupdate/manifest"),
343 basedir.AppendASCII("manifest_v2.xml"));
344 interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
345 basedir.AppendASCII("v2.crx"));
347 // Install version 1 of the extension.
348 ExtensionTestMessageListener listener1("v1 installed", false);
349 ExtensionService* service = extensions::ExtensionSystem::Get(
350 browser()->profile())->extension_service();
351 const size_t enabled_size_before = service->extensions()->size();
352 const size_t disabled_size_before = service->disabled_extensions()->size();
353 const Extension* extension =
354 InstallExtension(basedir.AppendASCII("v1.crx"), 1);
355 ASSERT_TRUE(extension);
356 listener1.WaitUntilSatisfied();
357 DisableExtension(extension->id());
358 ASSERT_EQ(disabled_size_before + 1, service->disabled_extensions()->size());
359 ASSERT_EQ(enabled_size_before, service->extensions()->size());
360 ASSERT_EQ("ogjcoiohnmldgjemafoockdghcjciccf", extension->id());
361 ASSERT_EQ("1.0", extension->VersionString());
363 extensions::ExtensionUpdater::CheckParams params;
365 base::Bind(&NotificationListener::OnFinished,
366 base::Unretained(¬ification_listener));
368 ExtensionTestMessageListener listener2("v2 installed", false);
369 // Run autoupdate and make sure version 2 of the extension was installed but
370 // is still disabled.
371 service->updater()->CheckNow(params);
372 ASSERT_TRUE(WaitForExtensionInstall());
373 ASSERT_EQ(disabled_size_before + 1, service->disabled_extensions()->size());
374 ASSERT_EQ(enabled_size_before, service->extensions()->size());
375 extension = service->GetExtensionById(
376 "ogjcoiohnmldgjemafoockdghcjciccf", true);
377 ASSERT_TRUE(extension);
378 ASSERT_FALSE(service->GetExtensionById(
379 "ogjcoiohnmldgjemafoockdghcjciccf", false));
380 ASSERT_EQ("2.0", extension->VersionString());
382 // The extension should have not made the callback because it is disabled.
383 // When we enabled it, it should then make the callback.
384 ASSERT_FALSE(listener2.was_satisfied());
385 EnableExtension(extension->id());
386 listener2.WaitUntilSatisfied();
387 ASSERT_TRUE(notification_listener.started());
388 ASSERT_TRUE(notification_listener.finished());
389 ASSERT_TRUE(ContainsKey(notification_listener.updates(),
390 "ogjcoiohnmldgjemafoockdghcjciccf"));
391 notification_listener.Reset();
394 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, ExternalUrlUpdate) {
395 ExtensionService* service = extensions::ExtensionSystem::Get(
396 browser()->profile())->extension_service();
397 const char* kExtensionId = "ogjcoiohnmldgjemafoockdghcjciccf";
398 extensions::ExtensionUpdater::CheckParams params;
400 base::FilePath basedir = test_data_dir_.AppendASCII("autoupdate");
402 // Note: This interceptor gets requests on the IO thread.
403 content::URLLocalHostRequestPrepackagedInterceptor interceptor;
404 net::URLFetcher::SetEnableInterceptionForTests(true);
406 interceptor.SetResponseIgnoreQuery(
407 GURL("http://localhost/autoupdate/manifest"),
408 basedir.AppendASCII("manifest_v2.xml"));
409 interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
410 basedir.AppendASCII("v2.crx"));
412 const size_t size_before = service->extensions()->size();
413 ASSERT_TRUE(service->disabled_extensions()->is_empty());
415 extensions::PendingExtensionManager* pending_extension_manager =
416 service->pending_extension_manager();
418 // The code that reads external_extensions.json uses this method to inform
419 // the ExtensionService of an extension to download. Using the real code
420 // is race-prone, because instantating the ExtensionService starts a read
421 // of external_extensions.json before this test function starts.
423 EXPECT_TRUE(pending_extension_manager->AddFromExternalUpdateUrl(
424 kExtensionId, GURL("http://localhost/autoupdate/manifest"),
425 Manifest::EXTERNAL_PREF_DOWNLOAD, Extension::NO_FLAGS, false));
427 // Run autoupdate and make sure version 2 of the extension was installed.
428 service->updater()->CheckNow(params);
429 ASSERT_TRUE(WaitForExtensionInstall());
430 ASSERT_EQ(size_before + 1, service->extensions()->size());
431 const Extension* extension = service->GetExtensionById(kExtensionId, false);
432 ASSERT_TRUE(extension);
433 ASSERT_EQ("2.0", extension->VersionString());
435 // Uninstalling the extension should set a pref that keeps the extension from
436 // being installed again the next time external_extensions.json is read.
438 UninstallExtension(kExtensionId);
440 extensions::ExtensionPrefs* extension_prefs = service->extension_prefs();
441 EXPECT_TRUE(extension_prefs->IsExternalExtensionUninstalled(kExtensionId))
442 << "Uninstalling should set kill bit on externaly installed extension.";
444 // Try to install the extension again from an external source. It should fail
445 // because of the killbit.
446 EXPECT_FALSE(pending_extension_manager->AddFromExternalUpdateUrl(
447 kExtensionId, GURL("http://localhost/autoupdate/manifest"),
448 Manifest::EXTERNAL_PREF_DOWNLOAD, Extension::NO_FLAGS, false));
449 EXPECT_FALSE(pending_extension_manager->IsIdPending(kExtensionId))
450 << "External reinstall of a killed extension shouldn't work.";
451 EXPECT_TRUE(extension_prefs->IsExternalExtensionUninstalled(kExtensionId))
452 << "External reinstall of a killed extension should leave it killed.";
454 // Installing from non-external source.
455 ASSERT_TRUE(InstallExtension(basedir.AppendASCII("v2.crx"), 1));
457 EXPECT_FALSE(extension_prefs->IsExternalExtensionUninstalled(kExtensionId))
458 << "Reinstalling should clear the kill bit.";
460 // Uninstalling from a non-external source should not set the kill bit.
461 UninstallExtension(kExtensionId);
463 EXPECT_FALSE(extension_prefs->IsExternalExtensionUninstalled(kExtensionId))
464 << "Uninstalling non-external extension should not set kill bit.";
469 const char* kForceInstallNotEmptyHelp =
470 "A policy may already be controlling the list of force-installed "
471 "extensions. Please remove all policy settings from your computer "
472 "before running tests. E.g. from /etc/chromium/policies Linux or "
473 "from the registry on Windows, etc.";
477 // See http://crbug.com/57378 for flakiness details.
478 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, ExternalPolicyRefresh) {
479 ExtensionService* service = extensions::ExtensionSystem::Get(
480 browser()->profile())->extension_service();
481 const char* kExtensionId = "ogjcoiohnmldgjemafoockdghcjciccf";
483 base::FilePath basedir = test_data_dir_.AppendASCII("autoupdate");
485 // Note: This interceptor gets requests on the IO thread.
486 content::URLLocalHostRequestPrepackagedInterceptor interceptor;
487 net::URLFetcher::SetEnableInterceptionForTests(true);
489 interceptor.SetResponseIgnoreQuery(
490 GURL("http://localhost/autoupdate/manifest"),
491 basedir.AppendASCII("manifest_v2.xml"));
492 interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
493 basedir.AppendASCII("v2.crx"));
495 const size_t size_before = service->extensions()->size();
496 ASSERT_TRUE(service->disabled_extensions()->is_empty());
498 PrefService* prefs = browser()->profile()->GetPrefs();
499 const base::DictionaryValue* forcelist =
500 prefs->GetDictionary(extensions::pref_names::kInstallForceList);
501 ASSERT_TRUE(forcelist->empty()) << kForceInstallNotEmptyHelp;
504 // Set the policy as a user preference and fire notification observers.
505 DictionaryPrefUpdate pref_update(prefs,
506 extensions::pref_names::kInstallForceList);
507 base::DictionaryValue* forcelist = pref_update.Get();
508 extensions::ExternalPolicyLoader::AddExtension(
509 forcelist, kExtensionId, "http://localhost/autoupdate/manifest");
512 // Check if the extension got installed.
513 ASSERT_TRUE(WaitForExtensionInstall());
514 ASSERT_EQ(size_before + 1, service->extensions()->size());
515 const Extension* extension = service->GetExtensionById(kExtensionId, false);
516 ASSERT_TRUE(extension);
517 ASSERT_EQ("2.0", extension->VersionString());
518 EXPECT_EQ(Manifest::EXTERNAL_POLICY_DOWNLOAD, extension->location());
520 // Try to disable and uninstall the extension which should fail.
521 DisableExtension(kExtensionId);
522 EXPECT_EQ(size_before + 1, service->extensions()->size());
523 EXPECT_EQ(0u, service->disabled_extensions()->size());
524 UninstallExtension(kExtensionId);
525 EXPECT_EQ(size_before + 1, service->extensions()->size());
526 EXPECT_EQ(0u, service->disabled_extensions()->size());
528 // Now try to disable it through the management api, again failing.
529 ExtensionTestMessageListener listener1("ready", false);
530 ASSERT_TRUE(LoadExtension(
531 test_data_dir_.AppendASCII("management/uninstall_extension")));
532 ASSERT_TRUE(listener1.WaitUntilSatisfied());
533 EXPECT_EQ(size_before + 2, service->extensions()->size());
534 EXPECT_EQ(0u, service->disabled_extensions()->size());
536 // Check that emptying the list triggers uninstall.
537 prefs->ClearPref(extensions::pref_names::kInstallForceList);
538 EXPECT_EQ(size_before + 1, service->extensions()->size());
539 EXPECT_FALSE(service->GetExtensionById(kExtensionId, true));
542 // See http://crbug.com/103371 and http://crbug.com/120640.
543 #if defined(ADDRESS_SANITIZER) || defined(OS_WIN)
544 #define MAYBE_PolicyOverridesUserInstall DISABLED_PolicyOverridesUserInstall
546 #define MAYBE_PolicyOverridesUserInstall PolicyOverridesUserInstall
549 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest,
550 MAYBE_PolicyOverridesUserInstall) {
551 ExtensionService* service = extensions::ExtensionSystem::Get(
552 browser()->profile())->extension_service();
553 const char* kExtensionId = "ogjcoiohnmldgjemafoockdghcjciccf";
554 extensions::ExtensionUpdater::CheckParams params;
555 service->updater()->set_default_check_params(params);
556 const size_t size_before = service->extensions()->size();
557 base::FilePath basedir = test_data_dir_.AppendASCII("autoupdate");
558 ASSERT_TRUE(service->disabled_extensions()->is_empty());
560 // Note: This interceptor gets requests on the IO thread.
561 content::URLLocalHostRequestPrepackagedInterceptor interceptor;
562 net::URLFetcher::SetEnableInterceptionForTests(true);
564 interceptor.SetResponseIgnoreQuery(
565 GURL("http://localhost/autoupdate/manifest"),
566 basedir.AppendASCII("manifest_v2.xml"));
567 interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
568 basedir.AppendASCII("v2.crx"));
570 // Check that the policy is initially empty.
571 PrefService* prefs = browser()->profile()->GetPrefs();
572 const base::DictionaryValue* forcelist =
573 prefs->GetDictionary(extensions::pref_names::kInstallForceList);
574 ASSERT_TRUE(forcelist->empty()) << kForceInstallNotEmptyHelp;
576 // User install of the extension.
577 ASSERT_TRUE(InstallExtension(basedir.AppendASCII("v2.crx"), 1));
578 ASSERT_EQ(size_before + 1, service->extensions()->size());
579 const Extension* extension = service->GetExtensionById(kExtensionId, false);
580 ASSERT_TRUE(extension);
581 EXPECT_EQ(Manifest::INTERNAL, extension->location());
582 EXPECT_TRUE(service->IsExtensionEnabled(kExtensionId));
584 // Setup the force install policy. It should override the location.
586 DictionaryPrefUpdate pref_update(prefs,
587 extensions::pref_names::kInstallForceList);
588 extensions::ExternalPolicyLoader::AddExtension(
589 pref_update.Get(), kExtensionId,
590 "http://localhost/autoupdate/manifest");
592 ASSERT_TRUE(WaitForExtensionInstall());
593 ASSERT_EQ(size_before + 1, service->extensions()->size());
594 extension = service->GetExtensionById(kExtensionId, false);
595 ASSERT_TRUE(extension);
596 EXPECT_EQ(Manifest::EXTERNAL_POLICY_DOWNLOAD, extension->location());
597 EXPECT_TRUE(service->IsExtensionEnabled(kExtensionId));
599 // Remove the policy, and verify that the extension was uninstalled.
600 // TODO(joaodasilva): it would be nicer if the extension was kept instead,
601 // and reverted location to INTERNAL or whatever it was before the policy
603 prefs->ClearPref(extensions::pref_names::kInstallForceList);
604 ASSERT_EQ(size_before, service->extensions()->size());
605 extension = service->GetExtensionById(kExtensionId, true);
606 EXPECT_FALSE(extension);
608 // User install again, but have it disabled too before setting the policy.
609 ASSERT_TRUE(InstallExtension(basedir.AppendASCII("v2.crx"), 1));
610 ASSERT_EQ(size_before + 1, service->extensions()->size());
611 extension = service->GetExtensionById(kExtensionId, false);
612 ASSERT_TRUE(extension);
613 EXPECT_EQ(Manifest::INTERNAL, extension->location());
614 EXPECT_TRUE(service->IsExtensionEnabled(kExtensionId));
615 EXPECT_TRUE(service->disabled_extensions()->is_empty());
617 DisableExtension(kExtensionId);
618 EXPECT_EQ(1u, service->disabled_extensions()->size());
619 extension = service->GetExtensionById(kExtensionId, true);
620 EXPECT_TRUE(extension);
621 EXPECT_FALSE(service->IsExtensionEnabled(kExtensionId));
623 // Install the policy again. It should overwrite the extension's location,
624 // and force enable it too.
626 DictionaryPrefUpdate pref_update(prefs,
627 extensions::pref_names::kInstallForceList);
628 base::DictionaryValue* forcelist = pref_update.Get();
629 extensions::ExternalPolicyLoader::AddExtension(
630 forcelist, kExtensionId, "http://localhost/autoupdate/manifest");
632 ASSERT_TRUE(WaitForExtensionInstall());
633 ASSERT_EQ(size_before + 1, service->extensions()->size());
634 extension = service->GetExtensionById(kExtensionId, false);
635 ASSERT_TRUE(extension);
636 EXPECT_EQ(Manifest::EXTERNAL_POLICY_DOWNLOAD, extension->location());
637 EXPECT_TRUE(service->IsExtensionEnabled(kExtensionId));
638 EXPECT_TRUE(service->disabled_extensions()->is_empty());