| 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 modifying the extension's configuration. |
| 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 const std::string& key); |
| 63 | 62 |
| 64 testing::AssertionResult TestPackExtensionFunction( | 63 testing::AssertionResult TestPackExtensionFunction( |
| 65 const base::ListValue& args, | 64 const base::ListValue& args, |
| 66 api::developer_private::PackStatus expected_status, | 65 api::developer_private::PackStatus expected_status, |
| 67 int expected_flags); | 66 int expected_flags); |
| 68 | 67 |
| 69 Browser* browser() { return browser_.get(); } | 68 Browser* browser() { return browser_.get(); } |
| 70 | 69 |
| 71 private: | 70 private: |
| 72 // ExtensionServiceTestBase: | 71 // ExtensionServiceTestBase: |
| (...skipping 17 matching lines...) Expand all Loading... |
| 90 make_scoped_ptr(args.DeepCopy()), | 89 make_scoped_ptr(args.DeepCopy()), |
| 91 browser(), | 90 browser(), |
| 92 extension_function_test_utils::NONE); | 91 extension_function_test_utils::NONE); |
| 93 } | 92 } |
| 94 | 93 |
| 95 const Extension* DeveloperPrivateApiUnitTest::LoadUnpackedExtension() { | 94 const Extension* DeveloperPrivateApiUnitTest::LoadUnpackedExtension() { |
| 96 const char kManifest[] = | 95 const char kManifest[] = |
| 97 "{" | 96 "{" |
| 98 " \"name\": \"foo\"," | 97 " \"name\": \"foo\"," |
| 99 " \"version\": \"1.0\"," | 98 " \"version\": \"1.0\"," |
| 100 " \"manifest_version\": 2" | 99 " \"manifest_version\": 2," |
| 100 " \"permissions\": [\"*://*/*\"]" |
| 101 "}"; | 101 "}"; |
| 102 | 102 |
| 103 test_extension_dirs_.push_back(new TestExtensionDir); | 103 test_extension_dirs_.push_back(new TestExtensionDir); |
| 104 TestExtensionDir* dir = test_extension_dirs_.back(); | 104 TestExtensionDir* dir = test_extension_dirs_.back(); |
| 105 dir->WriteManifest(kManifest); | 105 dir->WriteManifest(kManifest); |
| 106 | 106 |
| 107 // TODO(devlin): We should extract out methods to load an unpacked extension | 107 // TODO(devlin): We should extract out methods to load an unpacked extension |
| 108 // synchronously. We do it in ExtensionBrowserTest, but that's not helpful | 108 // synchronously. We do it in ExtensionBrowserTest, but that's not helpful |
| 109 // for unittests. | 109 // for unittests. |
| 110 TestExtensionRegistryObserver registry_observer(registry()); | 110 TestExtensionRegistryObserver registry_observer(registry()); |
| 111 scoped_refptr<UnpackedInstaller> installer( | 111 scoped_refptr<UnpackedInstaller> installer( |
| 112 UnpackedInstaller::Create(service())); | 112 UnpackedInstaller::Create(service())); |
| 113 installer->Load(dir->unpacked_path()); | 113 installer->Load(dir->unpacked_path()); |
| 114 base::FilePath extension_path = | 114 base::FilePath extension_path = |
| 115 base::MakeAbsoluteFilePath(dir->unpacked_path()); | 115 base::MakeAbsoluteFilePath(dir->unpacked_path()); |
| 116 const Extension* extension = nullptr; | 116 const Extension* extension = nullptr; |
| 117 do { | 117 do { |
| 118 extension = registry_observer.WaitForExtensionLoaded(); | 118 extension = registry_observer.WaitForExtensionLoaded(); |
| 119 } while (extension->path() != extension_path); | 119 } while (extension->path() != extension_path); |
| 120 // The fact that unpacked extensions get file access by default is an | 120 // The fact that unpacked extensions get file access by default is an |
| 121 // irrelevant detail to these tests. Disable it. | 121 // irrelevant detail to these tests. Disable it. |
| 122 ExtensionPrefs::Get(browser_context())->SetAllowFileAccess(extension->id(), | 122 ExtensionPrefs::Get(browser_context())->SetAllowFileAccess(extension->id(), |
| 123 false); | 123 false); |
| 124 return extension; | 124 return extension; |
| 125 } | 125 } |
| 126 | 126 |
| 127 template<typename T> | |
| 128 void DeveloperPrivateApiUnitTest::TestExtensionPrefSetting( | 127 void DeveloperPrivateApiUnitTest::TestExtensionPrefSetting( |
| 129 bool (*has_pref)(const std::string&, content::BrowserContext*)) { | 128 bool (*has_pref)(const std::string&, content::BrowserContext*), |
| 129 const std::string& key) { |
| 130 // Sadly, we need a "real" directory here, because toggling incognito causes | 130 // Sadly, we need a "real" directory here, because toggling incognito causes |
| 131 // a reload (which needs a path). | 131 // a reload (which needs a path). |
| 132 std::string extension_id = LoadUnpackedExtension()->id(); | 132 std::string extension_id = LoadUnpackedExtension()->id(); |
| 133 | 133 |
| 134 scoped_refptr<UIThreadExtensionFunction> function(new T()); | 134 scoped_refptr<UIThreadExtensionFunction> function( |
| 135 new api::DeveloperPrivateUpdateExtensionConfigurationFunction()); |
| 135 | 136 |
| 136 base::ListValue enable_args; | 137 base::ListValue args; |
| 137 enable_args.AppendString(extension_id); | 138 base::DictionaryValue* parameters = new base::DictionaryValue(); |
| 138 enable_args.AppendBoolean(true); | 139 parameters->SetString("extensionId", extension_id); |
| 140 parameters->SetBoolean(key, true); |
| 141 args.Append(parameters); |
| 139 | 142 |
| 140 EXPECT_FALSE(has_pref(extension_id, profile())); | 143 EXPECT_FALSE(has_pref(extension_id, profile())) << key; |
| 141 | 144 |
| 142 // Pref-setting should require a user action. | 145 EXPECT_FALSE(RunFunction(function, args)) << key; |
| 143 EXPECT_FALSE(RunFunction(function, enable_args)); | |
| 144 EXPECT_EQ(std::string("This action requires a user gesture."), | 146 EXPECT_EQ(std::string("This action requires a user gesture."), |
| 145 function->GetError()); | 147 function->GetError()); |
| 146 | 148 |
| 147 ExtensionFunction::ScopedUserGestureForTests scoped_user_gesture; | 149 ExtensionFunction::ScopedUserGestureForTests scoped_user_gesture; |
| 148 function = new T(); | 150 function = new api::DeveloperPrivateUpdateExtensionConfigurationFunction(); |
| 149 EXPECT_TRUE(RunFunction(function, enable_args)); | 151 EXPECT_TRUE(RunFunction(function, args)) << key; |
| 150 EXPECT_TRUE(has_pref(extension_id, profile())); | 152 EXPECT_TRUE(has_pref(extension_id, profile())) << key; |
| 151 | 153 |
| 152 base::ListValue disable_args; | 154 parameters->SetBoolean(key, false); |
| 153 disable_args.AppendString(extension_id); | 155 function = new api::DeveloperPrivateUpdateExtensionConfigurationFunction(); |
| 154 disable_args.AppendBoolean(false); | 156 EXPECT_TRUE(RunFunction(function, args)) << key; |
| 155 function = new T(); | 157 EXPECT_FALSE(has_pref(extension_id, profile())) << key; |
| 156 EXPECT_TRUE(RunFunction(function, disable_args)); | |
| 157 EXPECT_FALSE(has_pref(extension_id, profile())); | |
| 158 } | 158 } |
| 159 | 159 |
| 160 testing::AssertionResult DeveloperPrivateApiUnitTest::TestPackExtensionFunction( | 160 testing::AssertionResult DeveloperPrivateApiUnitTest::TestPackExtensionFunction( |
| 161 const base::ListValue& args, | 161 const base::ListValue& args, |
| 162 api::developer_private::PackStatus expected_status, | 162 api::developer_private::PackStatus expected_status, |
| 163 int expected_flags) { | 163 int expected_flags) { |
| 164 scoped_refptr<UIThreadExtensionFunction> function( | 164 scoped_refptr<UIThreadExtensionFunction> function( |
| 165 new api::DeveloperPrivatePackDirectoryFunction()); | 165 new api::DeveloperPrivatePackDirectoryFunction()); |
| 166 if (!RunFunction(function, args)) | 166 if (!RunFunction(function, args)) |
| 167 return testing::AssertionFailure() << "Could not run function."; | 167 return testing::AssertionFailure() << "Could not run function."; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 profile(), &BuildAPI); | 206 profile(), &BuildAPI); |
| 207 } | 207 } |
| 208 | 208 |
| 209 void DeveloperPrivateApiUnitTest::TearDown() { | 209 void DeveloperPrivateApiUnitTest::TearDown() { |
| 210 test_extension_dirs_.clear(); | 210 test_extension_dirs_.clear(); |
| 211 browser_.reset(); | 211 browser_.reset(); |
| 212 browser_window_.reset(); | 212 browser_window_.reset(); |
| 213 ExtensionServiceTestBase::TearDown(); | 213 ExtensionServiceTestBase::TearDown(); |
| 214 } | 214 } |
| 215 | 215 |
| 216 // Test developerPrivate.allowIncognito. | 216 // Test developerPrivate.updateExtensionConfiguration. |
| 217 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateAllowIncognito) { | 217 TEST_F(DeveloperPrivateApiUnitTest, |
| 218 TestExtensionPrefSetting<api::DeveloperPrivateAllowIncognitoFunction>( | 218 DeveloperPrivateUpdateExtensionConfiguration) { |
| 219 &util::IsIncognitoEnabled); | 219 FeatureSwitch::ScopedOverride scripts_require_action( |
| 220 FeatureSwitch::scripts_require_action(), true); |
| 221 TestExtensionPrefSetting(&util::IsIncognitoEnabled, "incognitoAccess"); |
| 222 TestExtensionPrefSetting(&util::AllowFileAccess, "fileAccess"); |
| 223 TestExtensionPrefSetting(&util::AllowedScriptingOnAllUrls, |
| 224 "runOnAllUrls"); |
| 220 } | 225 } |
| 221 | 226 |
| 222 // Test developerPrivate.reload. | 227 // Test developerPrivate.reload. |
| 223 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateReload) { | 228 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateReload) { |
| 224 const Extension* extension = LoadUnpackedExtension(); | 229 const Extension* extension = LoadUnpackedExtension(); |
| 225 std::string extension_id = extension->id(); | 230 std::string extension_id = extension->id(); |
| 226 scoped_refptr<UIThreadExtensionFunction> function( | 231 scoped_refptr<UIThreadExtensionFunction> function( |
| 227 new api::DeveloperPrivateReloadFunction()); | 232 new api::DeveloperPrivateReloadFunction()); |
| 228 base::ListValue reload_args; | 233 base::ListValue reload_args; |
| 229 reload_args.AppendString(extension_id); | 234 reload_args.AppendString(extension_id); |
| 230 | 235 |
| 231 TestExtensionRegistryObserver registry_observer(registry()); | 236 TestExtensionRegistryObserver registry_observer(registry()); |
| 232 EXPECT_TRUE(RunFunction(function, reload_args)); | 237 EXPECT_TRUE(RunFunction(function, reload_args)); |
| 233 const Extension* unloaded_extension = | 238 const Extension* unloaded_extension = |
| 234 registry_observer.WaitForExtensionUnloaded(); | 239 registry_observer.WaitForExtensionUnloaded(); |
| 235 EXPECT_EQ(extension, unloaded_extension); | 240 EXPECT_EQ(extension, unloaded_extension); |
| 236 const Extension* reloaded_extension = | 241 const Extension* reloaded_extension = |
| 237 registry_observer.WaitForExtensionLoaded(); | 242 registry_observer.WaitForExtensionLoaded(); |
| 238 EXPECT_EQ(extension_id, reloaded_extension->id()); | 243 EXPECT_EQ(extension_id, reloaded_extension->id()); |
| 239 } | 244 } |
| 240 | 245 |
| 241 // Test developerPrivate.allowFileAccess. | |
| 242 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateAllowFileAccess) { | |
| 243 TestExtensionPrefSetting<api::DeveloperPrivateAllowFileAccessFunction>( | |
| 244 &util::AllowFileAccess); | |
| 245 } | |
| 246 | |
| 247 // Test developerPrivate.packDirectory. | 246 // Test developerPrivate.packDirectory. |
| 248 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivatePackFunction) { | 247 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivatePackFunction) { |
| 249 ResetThreadBundle(content::TestBrowserThreadBundle::DEFAULT); | 248 ResetThreadBundle(content::TestBrowserThreadBundle::DEFAULT); |
| 250 | 249 |
| 251 base::FilePath root_path = data_dir().AppendASCII("good_unpacked"); | 250 base::FilePath root_path = data_dir().AppendASCII("good_unpacked"); |
| 252 base::FilePath crx_path = data_dir().AppendASCII("good_unpacked.crx"); | 251 base::FilePath crx_path = data_dir().AppendASCII("good_unpacked.crx"); |
| 253 base::FilePath pem_path = data_dir().AppendASCII("good_unpacked.pem"); | 252 base::FilePath pem_path = data_dir().AppendASCII("good_unpacked.pem"); |
| 254 | 253 |
| 255 // First, test a directory that should pack properly. | 254 // First, test a directory that should pack properly. |
| 256 base::ListValue pack_args; | 255 base::ListValue pack_args; |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 ASSERT_EQ(1u, results->GetSize()); | 456 ASSERT_EQ(1u, results->GetSize()); |
| 458 ASSERT_TRUE(results->GetList(0u, &list)); | 457 ASSERT_TRUE(results->GetList(0u, &list)); |
| 459 ASSERT_EQ(1u, list->GetSize()); | 458 ASSERT_EQ(1u, list->GetSize()); |
| 460 ASSERT_TRUE(list->Get(0u, &value)); | 459 ASSERT_TRUE(list->Get(0u, &value)); |
| 461 scoped_ptr<api::developer_private::ItemInfo> item_info = | 460 scoped_ptr<api::developer_private::ItemInfo> item_info = |
| 462 api::developer_private::ItemInfo::FromValue(*value); | 461 api::developer_private::ItemInfo::FromValue(*value); |
| 463 ASSERT_TRUE(item_info); | 462 ASSERT_TRUE(item_info); |
| 464 } | 463 } |
| 465 | 464 |
| 466 } // namespace extensions | 465 } // namespace extensions |
| OLD | NEW |