Prevent chrome://net-internals/#export from flickering
[chromium-blink-merge.git] / chrome / browser / extensions / extension_disabled_ui_browsertest.cc
blobc3cd1097d536a4d17cab65fa3a7a36831f0e4f8f
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/threading/sequenced_worker_pool.h"
8 #include "chrome/app/chrome_command_ids.h"
9 #include "chrome/browser/extensions/extension_browsertest.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/extension_sync_service.h"
12 #include "chrome/browser/extensions/updater/extension_updater.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/global_error/global_error.h"
16 #include "chrome/browser/ui/global_error/global_error_service.h"
17 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/test/test_utils.h"
21 #include "extensions/browser/extension_prefs.h"
22 #include "extensions/browser/extension_registry.h"
23 #include "extensions/browser/extension_system.h"
24 #include "extensions/common/extension.h"
25 #include "net/url_request/test_url_request_interceptor.h"
26 #include "sync/protocol/extension_specifics.pb.h"
27 #include "sync/protocol/sync.pb.h"
29 using content::BrowserThread;
30 using extensions::Extension;
31 using extensions::ExtensionRegistry;
32 using extensions::ExtensionPrefs;
34 class ExtensionDisabledGlobalErrorTest : public ExtensionBrowserTest {
35 protected:
36 void SetUpCommandLine(base::CommandLine* command_line) override {
37 ExtensionBrowserTest::SetUpCommandLine(command_line);
38 command_line->AppendSwitchASCII(switches::kAppsGalleryUpdateURL,
39 "http://localhost/autoupdate/updates.xml");
42 void SetUpOnMainThread() override {
43 ExtensionBrowserTest::SetUpOnMainThread();
44 EXPECT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
45 service_ = extensions::ExtensionSystem::Get(
46 browser()->profile())->extension_service();
47 registry_ = ExtensionRegistry::Get(browser()->profile());
48 const base::FilePath test_dir =
49 test_data_dir_.AppendASCII("permissions_increase");
50 const base::FilePath pem_path = test_dir.AppendASCII("permissions.pem");
51 path_v1_ = PackExtensionWithOptions(
52 test_dir.AppendASCII("v1"),
53 scoped_temp_dir_.path().AppendASCII("permissions1.crx"),
54 pem_path,
55 base::FilePath());
56 path_v2_ = PackExtensionWithOptions(
57 test_dir.AppendASCII("v2"),
58 scoped_temp_dir_.path().AppendASCII("permissions2.crx"),
59 pem_path,
60 base::FilePath());
61 path_v3_ = PackExtensionWithOptions(
62 test_dir.AppendASCII("v3"),
63 scoped_temp_dir_.path().AppendASCII("permissions3.crx"),
64 pem_path,
65 base::FilePath());
68 // Returns the ExtensionDisabledGlobalError, if present.
69 // Caution: currently only supports one error at a time.
70 GlobalError* GetExtensionDisabledGlobalError() {
71 return GlobalErrorServiceFactory::GetForProfile(browser()->profile())->
72 GetGlobalErrorByMenuItemCommandID(IDC_EXTENSION_DISABLED_FIRST);
75 // Install the initial version, which should happen just fine.
76 const Extension* InstallIncreasingPermissionExtensionV1() {
77 size_t size_before = registry_->enabled_extensions().size();
78 const Extension* extension = InstallExtension(path_v1_, 1);
79 if (!extension)
80 return NULL;
81 if (registry_->enabled_extensions().size() != size_before + 1)
82 return NULL;
83 return extension;
86 // Upgrade to a version that wants more permissions. We should disable the
87 // extension and prompt the user to reenable.
88 const Extension* UpdateIncreasingPermissionExtension(
89 const Extension* extension,
90 const base::FilePath& crx_path,
91 int expected_change) {
92 size_t size_before = registry_->enabled_extensions().size();
93 if (UpdateExtension(extension->id(), crx_path, expected_change))
94 return NULL;
95 content::RunAllBlockingPoolTasksUntilIdle();
96 EXPECT_EQ(size_before + expected_change,
97 registry_->enabled_extensions().size());
98 if (registry_->disabled_extensions().size() != 1u)
99 return NULL;
101 return registry_->disabled_extensions().begin()->get();
104 // Helper function to install an extension and upgrade it to a version
105 // requiring additional permissions. Returns the new disabled Extension.
106 const Extension* InstallAndUpdateIncreasingPermissionsExtension() {
107 const Extension* extension = InstallIncreasingPermissionExtensionV1();
108 extension = UpdateIncreasingPermissionExtension(extension, path_v2_, -1);
109 return extension;
112 ExtensionService* service_;
113 ExtensionRegistry* registry_;
114 base::ScopedTempDir scoped_temp_dir_;
115 base::FilePath path_v1_;
116 base::FilePath path_v2_;
117 base::FilePath path_v3_;
120 // Tests the process of updating an extension to one that requires higher
121 // permissions, and accepting the permissions.
122 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest, AcceptPermissions) {
123 const Extension* extension = InstallAndUpdateIncreasingPermissionsExtension();
124 ASSERT_TRUE(extension);
125 ASSERT_TRUE(GetExtensionDisabledGlobalError());
126 const size_t size_before = registry_->enabled_extensions().size();
128 service_->GrantPermissionsAndEnableExtension(extension);
129 EXPECT_EQ(size_before + 1, registry_->enabled_extensions().size());
130 EXPECT_EQ(0u, registry_->disabled_extensions().size());
131 ASSERT_FALSE(GetExtensionDisabledGlobalError());
134 // Tests uninstalling an extension that was disabled due to higher permissions.
135 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest, Uninstall) {
136 const Extension* extension = InstallAndUpdateIncreasingPermissionsExtension();
137 ASSERT_TRUE(extension);
138 ASSERT_TRUE(GetExtensionDisabledGlobalError());
139 const size_t size_before = registry_->enabled_extensions().size();
141 UninstallExtension(extension->id());
142 EXPECT_EQ(size_before, registry_->enabled_extensions().size());
143 EXPECT_EQ(0u, registry_->disabled_extensions().size());
144 ASSERT_FALSE(GetExtensionDisabledGlobalError());
147 // Tests that no error appears if the user disabled the extension.
148 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest, UserDisabled) {
149 const Extension* extension = InstallIncreasingPermissionExtensionV1();
150 DisableExtension(extension->id());
151 extension = UpdateIncreasingPermissionExtension(extension, path_v2_, 0);
152 ASSERT_FALSE(GetExtensionDisabledGlobalError());
155 // Test that an error appears if the extension gets disabled because a
156 // version with higher permissions was installed by sync.
157 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest,
158 HigherPermissionsFromSync) {
159 // Get data for extension v2 (disabled) into sync.
160 const Extension* extension = InstallAndUpdateIncreasingPermissionsExtension();
161 std::string extension_id = extension->id();
162 ExtensionSyncService* sync_service = ExtensionSyncService::Get(
163 browser()->profile());
164 extensions::ExtensionSyncData sync_data =
165 sync_service->GetExtensionSyncData(*extension);
166 UninstallExtension(extension_id);
167 extension = NULL;
169 // Install extension v1.
170 InstallIncreasingPermissionExtensionV1();
172 // Note: This interceptor gets requests on the IO thread.
173 net::LocalHostTestURLRequestInterceptor interceptor(
174 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
175 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
176 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
177 interceptor.SetResponseIgnoreQuery(
178 GURL("http://localhost/autoupdate/updates.xml"),
179 test_data_dir_.AppendASCII("permissions_increase")
180 .AppendASCII("updates.xml"));
181 interceptor.SetResponseIgnoreQuery(
182 GURL("http://localhost/autoupdate/v2.crx"),
183 scoped_temp_dir_.path().AppendASCII("permissions2.crx"));
185 extensions::ExtensionUpdater::CheckParams params;
186 service_->updater()->set_default_check_params(params);
188 // Sync is replacing an older version, so it pends.
189 EXPECT_FALSE(sync_service->ProcessExtensionSyncData(sync_data));
191 WaitForExtensionInstall();
192 content::RunAllBlockingPoolTasksUntilIdle();
194 extension = service_->GetExtensionById(extension_id, true);
195 ASSERT_TRUE(extension);
196 EXPECT_EQ("2", extension->VersionString());
197 EXPECT_EQ(1u, registry_->disabled_extensions().size());
198 EXPECT_EQ(Extension::DISABLE_PERMISSIONS_INCREASE,
199 ExtensionPrefs::Get(service_->profile())
200 ->GetDisableReasons(extension_id));
201 EXPECT_TRUE(GetExtensionDisabledGlobalError());
204 // Test that an error appears if an extension gets installed server side.
205 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest, RemoteInstall) {
206 static const char extension_id[] = "pgdpcfcocojkjfbgpiianjngphoopgmo";
207 ExtensionSyncService* sync_service =
208 ExtensionSyncService::Get(browser()->profile());
210 // Note: This interceptor gets requests on the IO thread.
211 net::LocalHostTestURLRequestInterceptor interceptor(
212 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
213 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
214 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
215 interceptor.SetResponseIgnoreQuery(
216 GURL("http://localhost/autoupdate/updates.xml"),
217 test_data_dir_.AppendASCII("permissions_increase")
218 .AppendASCII("updates.xml"));
219 interceptor.SetResponseIgnoreQuery(
220 GURL("http://localhost/autoupdate/v2.crx"),
221 scoped_temp_dir_.path().AppendASCII("permissions2.crx"));
223 extensions::ExtensionUpdater::CheckParams params;
224 service_->updater()->set_default_check_params(params);
226 sync_pb::EntitySpecifics specifics;
227 specifics.mutable_extension()->set_id(extension_id);
228 specifics.mutable_extension()->set_enabled(false);
229 specifics.mutable_extension()->set_remote_install(true);
230 specifics.mutable_extension()->set_update_url(
231 "http://localhost/autoupdate/updates.xml");
232 specifics.mutable_extension()->set_version("2");
233 syncer::SyncData sync_data =
234 syncer::SyncData::CreateRemoteData(1234567,
235 specifics,
236 base::Time::Now(),
237 syncer::AttachmentIdList(),
238 syncer::AttachmentServiceProxy());
239 // Sync is installing a new extension, so it pends.
240 EXPECT_FALSE(sync_service->ProcessExtensionSyncData(
241 extensions::ExtensionSyncData(sync_data)));
243 WaitForExtensionInstall();
244 content::RunAllBlockingPoolTasksUntilIdle();
246 const Extension* extension = service_->GetExtensionById(extension_id, true);
247 ASSERT_TRUE(extension);
248 EXPECT_EQ("2", extension->VersionString());
249 EXPECT_EQ(1u, registry_->disabled_extensions().size());
250 EXPECT_EQ(Extension::DISABLE_REMOTE_INSTALL,
251 ExtensionPrefs::Get(service_->profile())
252 ->GetDisableReasons(extension_id));
253 EXPECT_TRUE(GetExtensionDisabledGlobalError());