Remove unused field from extension_sorting.cc
[chromium-blink-merge.git] / chrome / browser / extensions / extension_disabled_ui_browsertest.cc
blob8eb5228f34263b7e4121a77f7d5c78944a9abc54
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.
5 #include "base/files/file_path.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "base/run_loop.h"
8 #include "base/threading/sequenced_worker_pool.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/extensions/extension_browsertest.h"
11 #include "chrome/browser/extensions/extension_prefs.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/updater/extension_updater.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/global_error/global_error.h"
17 #include "chrome/browser/ui/global_error/global_error_service.h"
18 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/extensions/extension.h"
21 #include "content/public/test/test_utils.h"
22 #include "content/test/net/url_request_prepackaged_interceptor.h"
23 #include "net/url_request/url_fetcher.h"
25 using extensions::Extension;
27 class ExtensionDisabledGlobalErrorTest : public ExtensionBrowserTest {
28 protected:
29 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
30 ExtensionBrowserTest::SetUpCommandLine(command_line);
31 command_line->AppendSwitchASCII(switches::kAppsGalleryUpdateURL,
32 "http://localhost/autoupdate/updates.xml");
35 virtual void SetUpOnMainThread() OVERRIDE {
36 EXPECT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
37 service_ = browser()->profile()->GetExtensionService();
38 base::FilePath pem_path = test_data_dir_.
39 AppendASCII("permissions_increase").AppendASCII("permissions.pem");
40 path_v1_ = PackExtensionWithOptions(
41 test_data_dir_.AppendASCII("permissions_increase").AppendASCII("v1"),
42 scoped_temp_dir_.path().AppendASCII("permissions1.crx"),
43 pem_path,
44 base::FilePath());
45 path_v2_ = PackExtensionWithOptions(
46 test_data_dir_.AppendASCII("permissions_increase").AppendASCII("v2"),
47 scoped_temp_dir_.path().AppendASCII("permissions2.crx"),
48 pem_path,
49 base::FilePath());
50 path_v3_ = PackExtensionWithOptions(
51 test_data_dir_.AppendASCII("permissions_increase").AppendASCII("v3"),
52 scoped_temp_dir_.path().AppendASCII("permissions3.crx"),
53 pem_path,
54 base::FilePath());
57 // Returns the ExtensionDisabledGlobalError, if present.
58 // Caution: currently only supports one error at a time.
59 GlobalError* GetExtensionDisabledGlobalError() {
60 return GlobalErrorServiceFactory::GetForProfile(browser()->profile())->
61 GetGlobalErrorByMenuItemCommandID(IDC_EXTENSION_DISABLED_FIRST);
64 // Install the initial version, which should happen just fine.
65 const Extension* InstallIncreasingPermissionExtensionV1() {
66 size_t size_before = service_->extensions()->size();
67 const Extension* extension = InstallExtension(path_v1_, 1);
68 if (!extension)
69 return NULL;
70 if (service_->extensions()->size() != size_before + 1)
71 return NULL;
72 return extension;
75 // Upgrade to a version that wants more permissions. We should disable the
76 // extension and prompt the user to reenable.
77 const Extension* UpdateIncreasingPermissionExtension(
78 const Extension* extension,
79 const base::FilePath& crx_path,
80 int expected_change) {
81 size_t size_before = service_->extensions()->size();
82 if (UpdateExtension(extension->id(), crx_path, expected_change))
83 return NULL;
84 BrowserThread::GetBlockingPool()->FlushForTesting();
85 base::RunLoop().RunUntilIdle();
86 EXPECT_EQ(size_before + expected_change, service_->extensions()->size());
87 if (service_->disabled_extensions()->size() != 1u)
88 return NULL;
90 return service_->disabled_extensions()->begin()->get();
93 // Helper function to install an extension and upgrade it to a version
94 // requiring additional permissions. Returns the new disabled Extension.
95 const Extension* InstallAndUpdateIncreasingPermissionsExtension() {
96 const Extension* extension = InstallIncreasingPermissionExtensionV1();
97 extension = UpdateIncreasingPermissionExtension(extension, path_v2_, -1);
98 return extension;
101 ExtensionService* service_;
102 base::ScopedTempDir scoped_temp_dir_;
103 base::FilePath path_v1_;
104 base::FilePath path_v2_;
105 base::FilePath path_v3_;
108 // Tests the process of updating an extension to one that requires higher
109 // permissions, and accepting the permissions.
110 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest, AcceptPermissions) {
111 const Extension* extension = InstallAndUpdateIncreasingPermissionsExtension();
112 ASSERT_TRUE(extension);
113 ASSERT_TRUE(GetExtensionDisabledGlobalError());
114 const size_t size_before = service_->extensions()->size();
116 service_->GrantPermissionsAndEnableExtension(extension);
117 EXPECT_EQ(size_before + 1, service_->extensions()->size());
118 EXPECT_EQ(0u, service_->disabled_extensions()->size());
119 ASSERT_FALSE(GetExtensionDisabledGlobalError());
122 // Tests uninstalling an extension that was disabled due to higher permissions.
123 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest, Uninstall) {
124 const Extension* extension = InstallAndUpdateIncreasingPermissionsExtension();
125 ASSERT_TRUE(extension);
126 ASSERT_TRUE(GetExtensionDisabledGlobalError());
127 const size_t size_before = service_->extensions()->size();
129 UninstallExtension(extension->id());
130 EXPECT_EQ(size_before, service_->extensions()->size());
131 EXPECT_EQ(0u, service_->disabled_extensions()->size());
132 ASSERT_FALSE(GetExtensionDisabledGlobalError());
135 // Tests that no error appears if the user disabled the extension.
136 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest, UserDisabled) {
137 const Extension* extension = InstallIncreasingPermissionExtensionV1();
138 DisableExtension(extension->id());
139 extension = UpdateIncreasingPermissionExtension(extension, path_v2_, 0);
140 ASSERT_FALSE(GetExtensionDisabledGlobalError());
143 // Test that no error appears if the disable reason is unknown
144 // (but probably was by the user).
145 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest,
146 UnknownReasonSamePermissions) {
147 const Extension* extension = InstallIncreasingPermissionExtensionV1();
148 DisableExtension(extension->id());
149 // Clear disable reason to simulate legacy disables.
150 service_->extension_prefs()->ClearDisableReasons(extension->id());
151 // Upgrade to version 2. Infer from version 1 having the same permissions
152 // granted by the user that it was disabled by the user.
153 extension = UpdateIncreasingPermissionExtension(extension, path_v2_, 0);
154 ASSERT_TRUE(extension);
155 ASSERT_FALSE(GetExtensionDisabledGlobalError());
158 // Test that an error appears if the disable reason is unknown
159 // (but probably was for increased permissions).
160 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest,
161 UnknownReasonHigherPermissions) {
162 const Extension* extension = InstallAndUpdateIncreasingPermissionsExtension();
163 // Clear disable reason to simulate legacy disables.
164 service_->extension_prefs()->ClearDisableReasons(extension->id());
165 // We now have version 2 but only accepted permissions for version 1.
166 GlobalError* error = GetExtensionDisabledGlobalError();
167 ASSERT_TRUE(error);
168 // Also, remove the upgrade error for version 2.
169 GlobalErrorServiceFactory::GetForProfile(browser()->profile())->
170 RemoveGlobalError(error);
171 delete error;
172 // Upgrade to version 3, with even higher permissions. Infer from
173 // version 2 having higher-than-granted permissions that it was disabled
174 // for permissions increase.
175 extension = UpdateIncreasingPermissionExtension(extension, path_v3_, 0);
176 ASSERT_TRUE(extension);
177 ASSERT_TRUE(GetExtensionDisabledGlobalError());
180 // Test that an error appears if the extension gets disabled because a
181 // version with higher permissions was installed by sync.
182 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest,
183 HigherPermissionsFromSync) {
184 // Get data for extension v2 (disabled) into sync.
185 const Extension* extension = InstallAndUpdateIncreasingPermissionsExtension();
186 std::string extension_id = extension->id();
187 extensions::ExtensionSyncData sync_data =
188 service_->GetExtensionSyncData(*extension);
189 UninstallExtension(extension_id);
190 extension = NULL;
192 // Install extension v1.
193 InstallIncreasingPermissionExtensionV1();
195 // Note: This interceptor gets requests on the IO thread.
196 content::URLLocalHostRequestPrepackagedInterceptor interceptor;
197 net::URLFetcher::SetEnableInterceptionForTests(true);
198 interceptor.SetResponseIgnoreQuery(
199 GURL("http://localhost/autoupdate/updates.xml"),
200 test_data_dir_.AppendASCII("permissions_increase")
201 .AppendASCII("updates.xml"));
202 interceptor.SetResponseIgnoreQuery(
203 GURL("http://localhost/autoupdate/v2.crx"),
204 scoped_temp_dir_.path().AppendASCII("permissions2.crx"));
206 extensions::ExtensionUpdater::CheckParams params;
207 params.check_blacklist = false;
208 service_->updater()->set_default_check_params(params);
210 // Sync is replacing an older version, so it pends.
211 EXPECT_FALSE(service_->ProcessExtensionSyncData(sync_data));
213 WaitForExtensionInstall();
214 BrowserThread::GetBlockingPool()->FlushForTesting();
215 base::RunLoop().RunUntilIdle();
217 extension = service_->GetExtensionById(extension_id, true);
218 ASSERT_TRUE(extension);
219 EXPECT_EQ("2", extension->VersionString());
220 EXPECT_EQ(1u, service_->disabled_extensions()->size());
221 EXPECT_EQ(Extension::DISABLE_PERMISSIONS_INCREASE,
222 service_->extension_prefs()->GetDisableReasons(extension_id));
223 EXPECT_TRUE(GetExtensionDisabledGlobalError());