| OLD | NEW |
| 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 <memory> |
| 6 |
| 5 #include "base/macros.h" | 7 #include "base/macros.h" |
| 6 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/ptr_util.h" |
| 7 #include "chrome/browser/extensions/extension_function_test_utils.h" | 9 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/extensions/extension_service_test_base.h" | 11 #include "chrome/browser/extensions/extension_service_test_base.h" |
| 10 #include "chrome/browser/extensions/test_extension_system.h" | 12 #include "chrome/browser/extensions/test_extension_system.h" |
| 11 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/test/base/test_browser_window.h" | 14 #include "chrome/test/base/test_browser_window.h" |
| 13 #include "extensions/browser/api/management/management_api.h" | 15 #include "extensions/browser/api/management/management_api.h" |
| 14 #include "extensions/browser/api/management/management_api_constants.h" | 16 #include "extensions/browser/api/management/management_api_constants.h" |
| 15 #include "extensions/browser/event_router_factory.h" | 17 #include "extensions/browser/event_router_factory.h" |
| 16 #include "extensions/browser/extension_dialog_auto_confirm.h" | 18 #include "extensions/browser/extension_dialog_auto_confirm.h" |
| 17 #include "extensions/browser/extension_prefs.h" | 19 #include "extensions/browser/extension_prefs.h" |
| 18 #include "extensions/browser/extension_registry.h" | 20 #include "extensions/browser/extension_registry.h" |
| 19 #include "extensions/browser/extension_system.h" | 21 #include "extensions/browser/extension_system.h" |
| 20 #include "extensions/browser/management_policy.h" | 22 #include "extensions/browser/management_policy.h" |
| 21 #include "extensions/browser/test_management_policy.h" | 23 #include "extensions/browser/test_management_policy.h" |
| 22 #include "extensions/common/error_utils.h" | 24 #include "extensions/common/error_utils.h" |
| 23 #include "extensions/common/extension.h" | 25 #include "extensions/common/extension.h" |
| 24 #include "extensions/common/extension_set.h" | 26 #include "extensions/common/extension_set.h" |
| 25 #include "extensions/common/test_util.h" | 27 #include "extensions/common/test_util.h" |
| 26 | 28 |
| 27 namespace extensions { | 29 namespace extensions { |
| 28 | 30 |
| 29 namespace { | 31 namespace { |
| 30 | 32 |
| 31 scoped_ptr<KeyedService> BuildManagementApi(content::BrowserContext* context) { | 33 std::unique_ptr<KeyedService> BuildManagementApi( |
| 32 return make_scoped_ptr(new ManagementAPI(context)); | 34 content::BrowserContext* context) { |
| 35 return base::WrapUnique(new ManagementAPI(context)); |
| 33 } | 36 } |
| 34 | 37 |
| 35 scoped_ptr<KeyedService> BuildEventRouter(content::BrowserContext* profile) { | 38 std::unique_ptr<KeyedService> BuildEventRouter( |
| 36 return make_scoped_ptr( | 39 content::BrowserContext* profile) { |
| 40 return base::WrapUnique( |
| 37 new extensions::EventRouter(profile, ExtensionPrefs::Get(profile))); | 41 new extensions::EventRouter(profile, ExtensionPrefs::Get(profile))); |
| 38 } | 42 } |
| 39 | 43 |
| 40 } // namespace | 44 } // namespace |
| 41 | 45 |
| 42 namespace constants = extension_management_api_constants; | 46 namespace constants = extension_management_api_constants; |
| 43 | 47 |
| 44 // TODO(devlin): Unittests are awesome. Test more with unittests and less with | 48 // TODO(devlin): Unittests are awesome. Test more with unittests and less with |
| 45 // heavy api/browser tests. | 49 // heavy api/browser tests. |
| 46 class ManagementApiUnitTest : public ExtensionServiceTestBase { | 50 class ManagementApiUnitTest : public ExtensionServiceTestBase { |
| 47 protected: | 51 protected: |
| 48 ManagementApiUnitTest() {} | 52 ManagementApiUnitTest() {} |
| 49 ~ManagementApiUnitTest() override {} | 53 ~ManagementApiUnitTest() override {} |
| 50 | 54 |
| 51 // A wrapper around extension_function_test_utils::RunFunction that runs with | 55 // A wrapper around extension_function_test_utils::RunFunction that runs with |
| 52 // the associated browser, no flags, and can take stack-allocated arguments. | 56 // the associated browser, no flags, and can take stack-allocated arguments. |
| 53 bool RunFunction(const scoped_refptr<UIThreadExtensionFunction>& function, | 57 bool RunFunction(const scoped_refptr<UIThreadExtensionFunction>& function, |
| 54 const base::ListValue& args); | 58 const base::ListValue& args); |
| 55 | 59 |
| 56 Browser* browser() { return browser_.get(); } | 60 Browser* browser() { return browser_.get(); } |
| 57 | 61 |
| 58 private: | 62 private: |
| 59 // ExtensionServiceTestBase: | 63 // ExtensionServiceTestBase: |
| 60 void SetUp() override; | 64 void SetUp() override; |
| 61 void TearDown() override; | 65 void TearDown() override; |
| 62 | 66 |
| 63 // The browser (and accompanying window). | 67 // The browser (and accompanying window). |
| 64 scoped_ptr<TestBrowserWindow> browser_window_; | 68 std::unique_ptr<TestBrowserWindow> browser_window_; |
| 65 scoped_ptr<Browser> browser_; | 69 std::unique_ptr<Browser> browser_; |
| 66 | 70 |
| 67 DISALLOW_COPY_AND_ASSIGN(ManagementApiUnitTest); | 71 DISALLOW_COPY_AND_ASSIGN(ManagementApiUnitTest); |
| 68 }; | 72 }; |
| 69 | 73 |
| 70 bool ManagementApiUnitTest::RunFunction( | 74 bool ManagementApiUnitTest::RunFunction( |
| 71 const scoped_refptr<UIThreadExtensionFunction>& function, | 75 const scoped_refptr<UIThreadExtensionFunction>& function, |
| 72 const base::ListValue& args) { | 76 const base::ListValue& args) { |
| 73 return extension_function_test_utils::RunFunction( | 77 return extension_function_test_utils::RunFunction( |
| 74 function.get(), | 78 function.get(), base::WrapUnique(args.DeepCopy()), browser(), |
| 75 make_scoped_ptr(args.DeepCopy()), | |
| 76 browser(), | |
| 77 extension_function_test_utils::NONE); | 79 extension_function_test_utils::NONE); |
| 78 } | 80 } |
| 79 | 81 |
| 80 void ManagementApiUnitTest::SetUp() { | 82 void ManagementApiUnitTest::SetUp() { |
| 81 ExtensionServiceTestBase::SetUp(); | 83 ExtensionServiceTestBase::SetUp(); |
| 82 InitializeEmptyExtensionService(); | 84 InitializeEmptyExtensionService(); |
| 83 ManagementAPI::GetFactoryInstance()->SetTestingFactory(profile(), | 85 ManagementAPI::GetFactoryInstance()->SetTestingFactory(profile(), |
| 84 &BuildManagementApi); | 86 &BuildManagementApi); |
| 85 | 87 |
| 86 EventRouterFactory::GetInstance()->SetTestingFactory(profile(), | 88 EventRouterFactory::GetInstance()->SetTestingFactory(profile(), |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 new ManagementUninstallFunction(); | 193 new ManagementUninstallFunction(); |
| 192 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id)); | 194 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id)); |
| 193 EXPECT_FALSE(RunFunction(function, uninstall_args)); | 195 EXPECT_FALSE(RunFunction(function, uninstall_args)); |
| 194 // The uninstall should have failed. | 196 // The uninstall should have failed. |
| 195 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id)); | 197 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id)); |
| 196 EXPECT_EQ(ErrorUtils::FormatErrorMessage(constants::kUninstallCanceledError, | 198 EXPECT_EQ(ErrorUtils::FormatErrorMessage(constants::kUninstallCanceledError, |
| 197 extension_id), | 199 extension_id), |
| 198 function->GetError()); | 200 function->GetError()); |
| 199 | 201 |
| 200 // Try again, using showConfirmDialog: false. | 202 // Try again, using showConfirmDialog: false. |
| 201 scoped_ptr<base::DictionaryValue> options(new base::DictionaryValue()); | 203 std::unique_ptr<base::DictionaryValue> options(new base::DictionaryValue()); |
| 202 options->SetBoolean("showConfirmDialog", false); | 204 options->SetBoolean("showConfirmDialog", false); |
| 203 uninstall_args.Append(options.release()); | 205 uninstall_args.Append(options.release()); |
| 204 function = new ManagementUninstallFunction(); | 206 function = new ManagementUninstallFunction(); |
| 205 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id)); | 207 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id)); |
| 206 EXPECT_FALSE(RunFunction(function, uninstall_args)); | 208 EXPECT_FALSE(RunFunction(function, uninstall_args)); |
| 207 // This should still fail, since extensions can only suppress the dialog for | 209 // This should still fail, since extensions can only suppress the dialog for |
| 208 // uninstalling themselves. | 210 // uninstalling themselves. |
| 209 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id)); | 211 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id)); |
| 210 EXPECT_EQ(ErrorUtils::FormatErrorMessage(constants::kUninstallCanceledError, | 212 EXPECT_EQ(ErrorUtils::FormatErrorMessage(constants::kUninstallCanceledError, |
| 211 extension_id), | 213 extension_id), |
| 212 function->GetError()); | 214 function->GetError()); |
| 213 | 215 |
| 214 // If we try uninstall the extension itself, the uninstall should succeed | 216 // If we try uninstall the extension itself, the uninstall should succeed |
| 215 // (even though we auto-cancel any dialog), because the dialog is never | 217 // (even though we auto-cancel any dialog), because the dialog is never |
| 216 // shown. | 218 // shown. |
| 217 uninstall_args.Remove(0u, nullptr); | 219 uninstall_args.Remove(0u, nullptr); |
| 218 function = new ManagementUninstallSelfFunction(); | 220 function = new ManagementUninstallSelfFunction(); |
| 219 function->set_extension(extension); | 221 function->set_extension(extension); |
| 220 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id)); | 222 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_id)); |
| 221 EXPECT_TRUE(RunFunction(function, uninstall_args)) << function->GetError(); | 223 EXPECT_TRUE(RunFunction(function, uninstall_args)) << function->GetError(); |
| 222 EXPECT_FALSE(registry()->GetExtensionById(extension_id, | 224 EXPECT_FALSE(registry()->GetExtensionById(extension_id, |
| 223 ExtensionRegistry::EVERYTHING)); | 225 ExtensionRegistry::EVERYTHING)); |
| 224 } | 226 } |
| 225 } | 227 } |
| 226 | 228 |
| 227 } // namespace extensions | 229 } // namespace extensions |
| OLD | NEW |