Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(589)

Side by Side Diff: chrome/browser/extensions/api/management/management_api_unittest.cc

Issue 1148323007: [Extensions] Introduce a ScopedExtensionDialogAutoConfirm (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Latest master Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 #include "chrome/browser/extensions/extension_function_test_utils.h" 6 #include "chrome/browser/extensions/extension_function_test_utils.h"
7 #include "chrome/browser/extensions/extension_service.h" 7 #include "chrome/browser/extensions/extension_service.h"
8 #include "chrome/browser/extensions/extension_service_test_base.h" 8 #include "chrome/browser/extensions/extension_service_test_base.h"
9 #include "chrome/browser/extensions/test_extension_system.h" 9 #include "chrome/browser/extensions/test_extension_system.h"
10 #include "chrome/browser/ui/browser.h" 10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/host_desktop.h" 11 #include "chrome/browser/ui/host_desktop.h"
12 #include "chrome/test/base/test_browser_window.h" 12 #include "chrome/test/base/test_browser_window.h"
13 #include "extensions/browser/api/management/management_api.h" 13 #include "extensions/browser/api/management/management_api.h"
14 #include "extensions/browser/api/management/management_api_constants.h" 14 #include "extensions/browser/api/management/management_api_constants.h"
15 #include "extensions/browser/event_router_factory.h" 15 #include "extensions/browser/event_router_factory.h"
16 #include "extensions/browser/extension_dialog_auto_confirm.h"
16 #include "extensions/browser/extension_prefs.h" 17 #include "extensions/browser/extension_prefs.h"
17 #include "extensions/browser/extension_registry.h" 18 #include "extensions/browser/extension_registry.h"
18 #include "extensions/browser/extension_system.h" 19 #include "extensions/browser/extension_system.h"
19 #include "extensions/browser/management_policy.h" 20 #include "extensions/browser/management_policy.h"
20 #include "extensions/browser/test_management_policy.h" 21 #include "extensions/browser/test_management_policy.h"
21 #include "extensions/common/error_utils.h" 22 #include "extensions/common/error_utils.h"
22 #include "extensions/common/extension.h" 23 #include "extensions/common/extension.h"
23 #include "extensions/common/extension_set.h" 24 #include "extensions/common/extension_set.h"
24 #include "extensions/common/test_util.h" 25 #include "extensions/common/test_util.h"
25 26
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 // We need to be on the UI thread for this. 150 // We need to be on the UI thread for this.
150 ResetThreadBundle(content::TestBrowserThreadBundle::DEFAULT); 151 ResetThreadBundle(content::TestBrowserThreadBundle::DEFAULT);
151 scoped_refptr<const Extension> extension = test_util::CreateEmptyExtension(); 152 scoped_refptr<const Extension> extension = test_util::CreateEmptyExtension();
152 service()->AddExtension(extension.get()); 153 service()->AddExtension(extension.get());
153 std::string extension_id = extension->id(); 154 std::string extension_id = extension->id();
154 155
155 base::ListValue uninstall_args; 156 base::ListValue uninstall_args;
156 uninstall_args.AppendString(extension->id()); 157 uninstall_args.AppendString(extension->id());
157 158
158 // Auto-accept any uninstalls. 159 // Auto-accept any uninstalls.
159 ManagementUninstallFunctionBase::SetAutoConfirmForTest(true); 160 {
161 ScopedTestDialogAutoConfirm auto_confirm(
162 ScopedTestDialogAutoConfirm::ACCEPT);
160 163
161 // Uninstall requires a user gesture, so this should fail. 164 // Uninstall requires a user gesture, so this should fail.
162 scoped_refptr<UIThreadExtensionFunction> function( 165 scoped_refptr<UIThreadExtensionFunction> function(
163 new ManagementUninstallFunction()); 166 new ManagementUninstallFunction());
164 EXPECT_FALSE(RunFunction(function, uninstall_args)); 167 EXPECT_FALSE(RunFunction(function, uninstall_args));
165 EXPECT_EQ(std::string(constants::kGestureNeededForUninstallError), 168 EXPECT_EQ(std::string(constants::kGestureNeededForUninstallError),
166 function->GetError()); 169 function->GetError());
167 170
168 ExtensionFunction::ScopedUserGestureForTests scoped_user_gesture; 171 ExtensionFunction::ScopedUserGestureForTests scoped_user_gesture;
169 172
170 function = new ManagementUninstallFunction(); 173 function = new ManagementUninstallFunction();
171 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id)); 174 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id));
172 EXPECT_TRUE(RunFunction(function, uninstall_args)) << function->GetError(); 175 EXPECT_TRUE(RunFunction(function, uninstall_args)) << function->GetError();
173 // The extension should be uninstalled. 176 // The extension should be uninstalled.
174 EXPECT_FALSE(registry()->GetExtensionById( 177 EXPECT_FALSE(registry()->GetExtensionById(extension_id,
175 extension_id, ExtensionRegistry::EVERYTHING)); 178 ExtensionRegistry::EVERYTHING));
179 }
176 180
177 // Install the extension again, and try uninstalling, auto-canceling the 181 // Install the extension again, and try uninstalling, auto-canceling the
178 // dialog. 182 // dialog.
179 service()->AddExtension(extension.get()); 183 {
180 function = new ManagementUninstallFunction(); 184 ScopedTestDialogAutoConfirm auto_confirm(
181 ManagementUninstallFunctionBase::SetAutoConfirmForTest(false); 185 ScopedTestDialogAutoConfirm::CANCEL);
182 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id)); 186 ExtensionFunction::ScopedUserGestureForTests scoped_user_gesture;
183 EXPECT_FALSE(RunFunction(function, uninstall_args));
184 // The uninstall should have failed.
185 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id));
186 EXPECT_EQ(ErrorUtils::FormatErrorMessage(constants::kUninstallCanceledError,
187 extension_id),
188 function->GetError());
189 187
190 // Try again, using showConfirmDialog: false. 188 service()->AddExtension(extension.get());
191 scoped_ptr<base::DictionaryValue> options(new base::DictionaryValue()); 189 scoped_refptr<UIThreadExtensionFunction> function =
192 options->SetBoolean("showConfirmDialog", false); 190 new ManagementUninstallFunction();
193 uninstall_args.Append(options.release()); 191 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id));
194 function = new ManagementUninstallFunction(); 192 EXPECT_FALSE(RunFunction(function, uninstall_args));
195 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id)); 193 // The uninstall should have failed.
196 EXPECT_FALSE(RunFunction(function, uninstall_args)); 194 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id));
197 // This should still fail, since extensions can only suppress the dialog for 195 EXPECT_EQ(ErrorUtils::FormatErrorMessage(constants::kUninstallCanceledError,
198 // uninstalling themselves. 196 extension_id),
199 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id)); 197 function->GetError());
200 EXPECT_EQ(ErrorUtils::FormatErrorMessage(constants::kUninstallCanceledError,
201 extension_id),
202 function->GetError());
203 198
204 // If we try uninstall the extension itself, the uninstall should succeed 199 // Try again, using showConfirmDialog: false.
205 // (even though we auto-cancel any dialog), because the dialog is never shown. 200 scoped_ptr<base::DictionaryValue> options(new base::DictionaryValue());
206 uninstall_args.Remove(0u, nullptr); 201 options->SetBoolean("showConfirmDialog", false);
207 function = new ManagementUninstallSelfFunction(); 202 uninstall_args.Append(options.release());
208 function->set_extension(extension); 203 function = new ManagementUninstallFunction();
209 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id)); 204 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id));
210 EXPECT_TRUE(RunFunction(function, uninstall_args)) << function->GetError(); 205 EXPECT_FALSE(RunFunction(function, uninstall_args));
211 EXPECT_FALSE(registry()->GetExtensionById( 206 // This should still fail, since extensions can only suppress the dialog for
212 extension_id, ExtensionRegistry::EVERYTHING)); 207 // uninstalling themselves.
208 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id));
209 EXPECT_EQ(ErrorUtils::FormatErrorMessage(constants::kUninstallCanceledError,
210 extension_id),
211 function->GetError());
212
213 // If we try uninstall the extension itself, the uninstall should succeed
214 // (even though we auto-cancel any dialog), because the dialog is never
215 // shown.
216 uninstall_args.Remove(0u, nullptr);
217 function = new ManagementUninstallSelfFunction();
218 function->set_extension(extension);
219 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id));
220 EXPECT_TRUE(RunFunction(function, uninstall_args)) << function->GetError();
221 EXPECT_FALSE(registry()->GetExtensionById(extension_id,
222 ExtensionRegistry::EVERYTHING));
223 }
213 } 224 }
214 225
215 } // namespace extensions 226 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698