Chromium Code Reviews| 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 "base/files/file_util.h" | 5 #include "base/files/file_util.h" |
| 6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
| 7 #include "chrome/browser/extensions/api/developer_private/developer_private_api. h" | 7 #include "chrome/browser/extensions/api/developer_private/developer_private_api. h" |
| 8 #include "chrome/browser/extensions/extension_function_test_utils.h" | 8 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/extensions/extension_service_test_base.h" | 10 #include "chrome/browser/extensions/extension_service_test_base.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 48 | 48 |
| 49 // A wrapper around extension_function_test_utils::RunFunction that runs with | 49 // A wrapper around extension_function_test_utils::RunFunction that runs with |
| 50 // the associated browser, no flags, and can take stack-allocated arguments. | 50 // the associated browser, no flags, and can take stack-allocated arguments. |
| 51 bool RunFunction(const scoped_refptr<UIThreadExtensionFunction>& function, | 51 bool RunFunction(const scoped_refptr<UIThreadExtensionFunction>& function, |
| 52 const base::ListValue& args); | 52 const base::ListValue& args); |
| 53 | 53 |
| 54 // Loads an unpacked extension that is backed by a real directory, allowing | 54 // Loads an unpacked extension that is backed by a real directory, allowing |
| 55 // it to be reloaded. | 55 // it to be reloaded. |
| 56 const Extension* LoadUnpackedExtension(); | 56 const Extension* LoadUnpackedExtension(); |
| 57 | 57 |
| 58 // Tests a developer private function (T) that sets an extension pref, and | 58 // Tests setting various extension prefs. |
| 59 // verifies it with |has_pref|. | |
| 60 template<typename T> | |
| 61 void TestExtensionPrefSetting( | 59 void TestExtensionPrefSetting( |
| 62 bool (*has_pref)(const std::string&, content::BrowserContext*)); | 60 bool (*has_pref)(const std::string&, content::BrowserContext*), |
| 61 api::developer_private::PermissionType permission, | |
| 62 bool require_user_gesture); | |
| 63 | 63 |
| 64 testing::AssertionResult TestPackExtensionFunction( | 64 testing::AssertionResult TestPackExtensionFunction( |
| 65 const base::ListValue& args, | 65 const base::ListValue& args, |
| 66 api::developer_private::PackStatus expected_status, | 66 api::developer_private::PackStatus expected_status, |
| 67 int expected_flags); | 67 int expected_flags); |
| 68 | 68 |
| 69 Browser* browser() { return browser_.get(); } | 69 Browser* browser() { return browser_.get(); } |
| 70 | 70 |
| 71 private: | 71 private: |
| 72 // ExtensionServiceTestBase: | 72 // ExtensionServiceTestBase: |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 90 make_scoped_ptr(args.DeepCopy()), | 90 make_scoped_ptr(args.DeepCopy()), |
| 91 browser(), | 91 browser(), |
| 92 extension_function_test_utils::NONE); | 92 extension_function_test_utils::NONE); |
| 93 } | 93 } |
| 94 | 94 |
| 95 const Extension* DeveloperPrivateApiUnitTest::LoadUnpackedExtension() { | 95 const Extension* DeveloperPrivateApiUnitTest::LoadUnpackedExtension() { |
| 96 const char kManifest[] = | 96 const char kManifest[] = |
| 97 "{" | 97 "{" |
| 98 " \"name\": \"foo\"," | 98 " \"name\": \"foo\"," |
| 99 " \"version\": \"1.0\"," | 99 " \"version\": \"1.0\"," |
| 100 " \"manifest_version\": 2" | 100 " \"manifest_version\": 2," |
| 101 " \"permissions\": [\"*://*/*\"]" | |
| 101 "}"; | 102 "}"; |
| 102 | 103 |
| 103 test_extension_dirs_.push_back(new TestExtensionDir); | 104 test_extension_dirs_.push_back(new TestExtensionDir); |
| 104 TestExtensionDir* dir = test_extension_dirs_.back(); | 105 TestExtensionDir* dir = test_extension_dirs_.back(); |
| 105 dir->WriteManifest(kManifest); | 106 dir->WriteManifest(kManifest); |
| 106 | 107 |
| 107 // TODO(devlin): We should extract out methods to load an unpacked extension | 108 // TODO(devlin): We should extract out methods to load an unpacked extension |
| 108 // synchronously. We do it in ExtensionBrowserTest, but that's not helpful | 109 // synchronously. We do it in ExtensionBrowserTest, but that's not helpful |
| 109 // for unittests. | 110 // for unittests. |
| 110 TestExtensionRegistryObserver registry_observer(registry()); | 111 TestExtensionRegistryObserver registry_observer(registry()); |
| 111 scoped_refptr<UnpackedInstaller> installer( | 112 scoped_refptr<UnpackedInstaller> installer( |
| 112 UnpackedInstaller::Create(service())); | 113 UnpackedInstaller::Create(service())); |
| 113 installer->Load(dir->unpacked_path()); | 114 installer->Load(dir->unpacked_path()); |
| 114 base::FilePath extension_path = | 115 base::FilePath extension_path = |
| 115 base::MakeAbsoluteFilePath(dir->unpacked_path()); | 116 base::MakeAbsoluteFilePath(dir->unpacked_path()); |
| 116 const Extension* extension = nullptr; | 117 const Extension* extension = nullptr; |
| 117 do { | 118 do { |
| 118 extension = registry_observer.WaitForExtensionLoaded(); | 119 extension = registry_observer.WaitForExtensionLoaded(); |
| 119 } while (extension->path() != extension_path); | 120 } while (extension->path() != extension_path); |
| 120 // The fact that unpacked extensions get file access by default is an | 121 // The fact that unpacked extensions get file access by default is an |
| 121 // irrelevant detail to these tests. Disable it. | 122 // irrelevant detail to these tests. Disable it. |
| 122 ExtensionPrefs::Get(browser_context())->SetAllowFileAccess(extension->id(), | 123 ExtensionPrefs::Get(browser_context())->SetAllowFileAccess(extension->id(), |
| 123 false); | 124 false); |
| 124 return extension; | 125 return extension; |
| 125 } | 126 } |
| 126 | 127 |
| 127 template<typename T> | |
| 128 void DeveloperPrivateApiUnitTest::TestExtensionPrefSetting( | 128 void DeveloperPrivateApiUnitTest::TestExtensionPrefSetting( |
| 129 bool (*has_pref)(const std::string&, content::BrowserContext*)) { | 129 bool (*has_pref)(const std::string&, content::BrowserContext*), |
| 130 api::developer_private::PermissionType permission, | |
| 131 bool require_user_gesture) { | |
| 130 // Sadly, we need a "real" directory here, because toggling incognito causes | 132 // Sadly, we need a "real" directory here, because toggling incognito causes |
| 131 // a reload (which needs a path). | 133 // a reload (which needs a path). |
| 132 std::string extension_id = LoadUnpackedExtension()->id(); | 134 std::string extension_id = LoadUnpackedExtension()->id(); |
| 133 | 135 |
| 134 scoped_refptr<UIThreadExtensionFunction> function(new T()); | 136 scoped_refptr<UIThreadExtensionFunction> function( |
| 137 new api::DeveloperPrivateSetExtensionPermissionFunction()); | |
| 135 | 138 |
| 136 base::ListValue enable_args; | 139 std::string permission_string = api::developer_private::ToString(permission); |
| 137 enable_args.AppendString(extension_id); | |
| 138 enable_args.AppendBoolean(true); | |
| 139 | 140 |
| 140 EXPECT_FALSE(has_pref(extension_id, profile())); | 141 base::ListValue args; |
| 142 base::DictionaryValue* parameters = new base::DictionaryValue(); | |
| 143 parameters->SetString("extensionId", extension_id); | |
| 144 parameters->SetString("permission", permission_string); | |
| 145 parameters->SetBoolean("isActive", true); | |
| 146 args.Append(parameters); | |
|
not at google - send to devlin
2015/03/16 21:48:08
consider using DictionaryBuilder
Devlin
2015/03/16 22:41:37
I did consider it, but this is shorter when we wan
| |
| 141 | 147 |
| 142 // Pref-setting should require a user action. | 148 EXPECT_FALSE(has_pref(extension_id, profile())) << permission_string; |
| 143 EXPECT_FALSE(RunFunction(function, enable_args)); | |
| 144 EXPECT_EQ(std::string("This action requires a user gesture."), | |
| 145 function->GetError()); | |
| 146 | 149 |
| 147 ExtensionFunction::ScopedUserGestureForTests scoped_user_gesture; | 150 scoped_ptr<ExtensionFunction::ScopedUserGestureForTests> scoped_user_gesture; |
| 148 function = new T(); | 151 if (require_user_gesture) { |
| 149 EXPECT_TRUE(RunFunction(function, enable_args)); | 152 EXPECT_FALSE(RunFunction(function, args)) << permission_string; |
| 150 EXPECT_TRUE(has_pref(extension_id, profile())); | 153 EXPECT_EQ(std::string("This action requires a user gesture."), |
| 154 function->GetError()); | |
| 155 scoped_user_gesture.reset( | |
| 156 new ExtensionFunction::ScopedUserGestureForTests()); | |
| 157 function = new api::DeveloperPrivateSetExtensionPermissionFunction(); | |
| 158 } | |
| 151 | 159 |
| 152 base::ListValue disable_args; | 160 EXPECT_TRUE(RunFunction(function, args)) << permission_string; |
| 153 disable_args.AppendString(extension_id); | 161 EXPECT_TRUE(has_pref(extension_id, profile())) << permission_string; |
| 154 disable_args.AppendBoolean(false); | 162 |
| 155 function = new T(); | 163 parameters->SetBoolean("isActive", false); |
| 156 EXPECT_TRUE(RunFunction(function, disable_args)); | 164 function = new api::DeveloperPrivateSetExtensionPermissionFunction(); |
| 157 EXPECT_FALSE(has_pref(extension_id, profile())); | 165 EXPECT_TRUE(RunFunction(function, args)) << permission_string; |
| 166 EXPECT_FALSE(has_pref(extension_id, profile())) << permission_string; | |
| 158 } | 167 } |
| 159 | 168 |
| 160 testing::AssertionResult DeveloperPrivateApiUnitTest::TestPackExtensionFunction( | 169 testing::AssertionResult DeveloperPrivateApiUnitTest::TestPackExtensionFunction( |
| 161 const base::ListValue& args, | 170 const base::ListValue& args, |
| 162 api::developer_private::PackStatus expected_status, | 171 api::developer_private::PackStatus expected_status, |
| 163 int expected_flags) { | 172 int expected_flags) { |
| 164 scoped_refptr<UIThreadExtensionFunction> function( | 173 scoped_refptr<UIThreadExtensionFunction> function( |
| 165 new api::DeveloperPrivatePackDirectoryFunction()); | 174 new api::DeveloperPrivatePackDirectoryFunction()); |
| 166 if (!RunFunction(function, args)) | 175 if (!RunFunction(function, args)) |
| 167 return testing::AssertionFailure() << "Could not run function."; | 176 return testing::AssertionFailure() << "Could not run function."; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 206 profile(), &BuildAPI); | 215 profile(), &BuildAPI); |
| 207 } | 216 } |
| 208 | 217 |
| 209 void DeveloperPrivateApiUnitTest::TearDown() { | 218 void DeveloperPrivateApiUnitTest::TearDown() { |
| 210 test_extension_dirs_.clear(); | 219 test_extension_dirs_.clear(); |
| 211 browser_.reset(); | 220 browser_.reset(); |
| 212 browser_window_.reset(); | 221 browser_window_.reset(); |
| 213 ExtensionServiceTestBase::TearDown(); | 222 ExtensionServiceTestBase::TearDown(); |
| 214 } | 223 } |
| 215 | 224 |
| 216 // Test developerPrivate.allowIncognito. | 225 // Test developerPrivate.setExtensionPermission. |
| 217 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateAllowIncognito) { | 226 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateSetExtensionPermission) { |
| 218 TestExtensionPrefSetting<api::DeveloperPrivateAllowIncognitoFunction>( | 227 FeatureSwitch::ScopedOverride scripts_require_action( |
| 219 &util::IsIncognitoEnabled); | 228 FeatureSwitch::scripts_require_action(), true); |
| 229 TestExtensionPrefSetting( | |
| 230 &util::IsIncognitoEnabled, | |
| 231 api::developer_private::PERMISSION_TYPE_INCOGNITO_ACCESS, | |
| 232 true); | |
| 233 TestExtensionPrefSetting( | |
| 234 &util::AllowFileAccess, | |
| 235 api::developer_private::PERMISSION_TYPE_FILE_ACCESS, | |
| 236 true); | |
| 237 TestExtensionPrefSetting( | |
| 238 &util::AllowedScriptingOnAllUrls, | |
| 239 api::developer_private::PERMISSION_TYPE_RUN_ON_ALL_URLS, | |
| 240 true); | |
| 220 } | 241 } |
| 221 | 242 |
| 222 // Test developerPrivate.reload. | 243 // Test developerPrivate.reload. |
| 223 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateReload) { | 244 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateReload) { |
| 224 const Extension* extension = LoadUnpackedExtension(); | 245 const Extension* extension = LoadUnpackedExtension(); |
| 225 std::string extension_id = extension->id(); | 246 std::string extension_id = extension->id(); |
| 226 scoped_refptr<UIThreadExtensionFunction> function( | 247 scoped_refptr<UIThreadExtensionFunction> function( |
| 227 new api::DeveloperPrivateReloadFunction()); | 248 new api::DeveloperPrivateReloadFunction()); |
| 228 base::ListValue reload_args; | 249 base::ListValue reload_args; |
| 229 reload_args.AppendString(extension_id); | 250 reload_args.AppendString(extension_id); |
| 230 | 251 |
| 231 TestExtensionRegistryObserver registry_observer(registry()); | 252 TestExtensionRegistryObserver registry_observer(registry()); |
| 232 EXPECT_TRUE(RunFunction(function, reload_args)); | 253 EXPECT_TRUE(RunFunction(function, reload_args)); |
| 233 const Extension* unloaded_extension = | 254 const Extension* unloaded_extension = |
| 234 registry_observer.WaitForExtensionUnloaded(); | 255 registry_observer.WaitForExtensionUnloaded(); |
| 235 EXPECT_EQ(extension, unloaded_extension); | 256 EXPECT_EQ(extension, unloaded_extension); |
| 236 const Extension* reloaded_extension = | 257 const Extension* reloaded_extension = |
| 237 registry_observer.WaitForExtensionLoaded(); | 258 registry_observer.WaitForExtensionLoaded(); |
| 238 EXPECT_EQ(extension_id, reloaded_extension->id()); | 259 EXPECT_EQ(extension_id, reloaded_extension->id()); |
| 239 } | 260 } |
| 240 | 261 |
| 241 // Test developerPrivate.allowFileAccess. | |
| 242 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateAllowFileAccess) { | |
| 243 TestExtensionPrefSetting<api::DeveloperPrivateAllowFileAccessFunction>( | |
| 244 &util::AllowFileAccess); | |
| 245 } | |
| 246 | |
| 247 // Test developerPrivate.packDirectory. | 262 // Test developerPrivate.packDirectory. |
| 248 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivatePackFunction) { | 263 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivatePackFunction) { |
| 249 ResetThreadBundle(content::TestBrowserThreadBundle::DEFAULT); | 264 ResetThreadBundle(content::TestBrowserThreadBundle::DEFAULT); |
| 250 | 265 |
| 251 base::FilePath root_path = data_dir().AppendASCII("good_unpacked"); | 266 base::FilePath root_path = data_dir().AppendASCII("good_unpacked"); |
| 252 base::FilePath crx_path = data_dir().AppendASCII("good_unpacked.crx"); | 267 base::FilePath crx_path = data_dir().AppendASCII("good_unpacked.crx"); |
| 253 base::FilePath pem_path = data_dir().AppendASCII("good_unpacked.pem"); | 268 base::FilePath pem_path = data_dir().AppendASCII("good_unpacked.pem"); |
| 254 | 269 |
| 255 // First, test a directory that should pack properly. | 270 // First, test a directory that should pack properly. |
| 256 base::ListValue pack_args; | 271 base::ListValue pack_args; |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 457 ASSERT_EQ(1u, results->GetSize()); | 472 ASSERT_EQ(1u, results->GetSize()); |
| 458 ASSERT_TRUE(results->GetList(0u, &list)); | 473 ASSERT_TRUE(results->GetList(0u, &list)); |
| 459 ASSERT_EQ(1u, list->GetSize()); | 474 ASSERT_EQ(1u, list->GetSize()); |
| 460 ASSERT_TRUE(list->Get(0u, &value)); | 475 ASSERT_TRUE(list->Get(0u, &value)); |
| 461 scoped_ptr<api::developer_private::ItemInfo> item_info = | 476 scoped_ptr<api::developer_private::ItemInfo> item_info = |
| 462 api::developer_private::ItemInfo::FromValue(*value); | 477 api::developer_private::ItemInfo::FromValue(*value); |
| 463 ASSERT_TRUE(item_info); | 478 ASSERT_TRUE(item_info); |
| 464 } | 479 } |
| 465 | 480 |
| 466 } // namespace extensions | 481 } // namespace extensions |
| OLD | NEW |