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 "chrome/browser/extensions/api/developer_private/developer_private_api. h" | 6 #include "chrome/browser/extensions/api/developer_private/developer_private_api. h" |
| 7 #include "chrome/browser/extensions/extension_function_test_utils.h" | 7 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/extensions/extension_service_test_base.h" | 9 #include "chrome/browser/extensions/extension_service_test_base.h" |
| 10 #include "chrome/browser/extensions/extension_util.h" | 10 #include "chrome/browser/extensions/extension_util.h" |
| 11 #include "chrome/browser/extensions/test_extension_dir.h" | 11 #include "chrome/browser/extensions/test_extension_dir.h" |
| 12 #include "chrome/browser/extensions/test_extension_system.h" | |
| 12 #include "chrome/browser/extensions/unpacked_installer.h" | 13 #include "chrome/browser/extensions/unpacked_installer.h" |
| 13 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/host_desktop.h" | 15 #include "chrome/browser/ui/host_desktop.h" |
| 15 #include "chrome/common/extensions/api/developer_private.h" | 16 #include "chrome/common/extensions/api/developer_private.h" |
| 16 #include "chrome/test/base/test_browser_window.h" | 17 #include "chrome/test/base/test_browser_window.h" |
| 18 #include "content/public/test/test_web_contents_factory.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/test_extension_registry_observer.h" | 22 #include "extensions/browser/test_extension_registry_observer.h" |
| 21 #include "extensions/common/extension.h" | 23 #include "extensions/common/extension.h" |
| 22 #include "extensions/common/extension_set.h" | 24 #include "extensions/common/extension_set.h" |
| 25 #include "extensions/common/manifest_constants.h" | |
| 23 #include "extensions/common/test_util.h" | 26 #include "extensions/common/test_util.h" |
| 24 | 27 |
| 25 namespace extensions { | 28 namespace extensions { |
| 26 | 29 |
| 30 namespace { | |
| 31 | |
| 32 // Creates a new ExtensionToolbarModel for the given |context|. | |
|
not at google - send to devlin
2015/03/03 22:22:10
Uh what does this comment have to do with the code
Devlin
2015/03/04 17:29:12
Wait, you mean this *isn't* an extension toolbar m
| |
| 33 KeyedService* BuildAPI(content::BrowserContext* context) { | |
| 34 return new DeveloperPrivateAPI(context); | |
| 35 } | |
| 36 | |
| 37 } // namespace | |
| 38 | |
| 27 class DeveloperPrivateApiUnitTest : public ExtensionServiceTestBase { | 39 class DeveloperPrivateApiUnitTest : public ExtensionServiceTestBase { |
| 28 protected: | 40 protected: |
| 29 DeveloperPrivateApiUnitTest() {} | 41 DeveloperPrivateApiUnitTest() {} |
| 30 ~DeveloperPrivateApiUnitTest() override {} | 42 ~DeveloperPrivateApiUnitTest() override {} |
| 31 | 43 |
| 32 // A wrapper around extension_function_test_utils::RunFunction that runs with | 44 // A wrapper around extension_function_test_utils::RunFunction that runs with |
| 33 // the associated browser, no flags, and can take stack-allocated arguments. | 45 // the associated browser, no flags, and can take stack-allocated arguments. |
| 34 bool RunFunction(const scoped_refptr<UIThreadExtensionFunction>& function, | 46 bool RunFunction(const scoped_refptr<UIThreadExtensionFunction>& function, |
| 35 const base::ListValue& args); | 47 const base::ListValue& args); |
| 36 | 48 |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 173 | 185 |
| 174 void DeveloperPrivateApiUnitTest::SetUp() { | 186 void DeveloperPrivateApiUnitTest::SetUp() { |
| 175 ExtensionServiceTestBase::SetUp(); | 187 ExtensionServiceTestBase::SetUp(); |
| 176 InitializeEmptyExtensionService(); | 188 InitializeEmptyExtensionService(); |
| 177 | 189 |
| 178 browser_window_.reset(new TestBrowserWindow()); | 190 browser_window_.reset(new TestBrowserWindow()); |
| 179 Browser::CreateParams params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE); | 191 Browser::CreateParams params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE); |
| 180 params.type = Browser::TYPE_TABBED; | 192 params.type = Browser::TYPE_TABBED; |
| 181 params.window = browser_window_.get(); | 193 params.window = browser_window_.get(); |
| 182 browser_.reset(new Browser(params)); | 194 browser_.reset(new Browser(params)); |
| 195 | |
| 196 // Allow the API to be created. | |
| 197 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile()))-> | |
| 198 SetEventRouter(make_scoped_ptr( | |
| 199 new EventRouter(profile(), ExtensionPrefs::Get(profile())))); | |
| 200 DeveloperPrivateAPI::GetFactoryInstance()->SetTestingFactory( | |
| 201 profile(), &BuildAPI); | |
| 183 } | 202 } |
| 184 | 203 |
| 185 void DeveloperPrivateApiUnitTest::TearDown() { | 204 void DeveloperPrivateApiUnitTest::TearDown() { |
| 186 test_extension_dirs_.clear(); | 205 test_extension_dirs_.clear(); |
| 187 browser_.reset(); | 206 browser_.reset(); |
| 188 browser_window_.reset(); | 207 browser_window_.reset(); |
| 189 ExtensionServiceTestBase::TearDown(); | 208 ExtensionServiceTestBase::TearDown(); |
| 190 } | 209 } |
| 191 | 210 |
| 192 // Test developerPrivate.allowIncognito. | 211 // Test developerPrivate.allowIncognito. |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 base::DeleteFile(crx_path, false); | 276 base::DeleteFile(crx_path, false); |
| 258 EXPECT_TRUE(pack_args.Remove(1u, nullptr)); // Remove the pem key argument. | 277 EXPECT_TRUE(pack_args.Remove(1u, nullptr)); // Remove the pem key argument. |
| 259 EXPECT_TRUE(pack_args.Remove(1u, nullptr)); // Remove the flags argument. | 278 EXPECT_TRUE(pack_args.Remove(1u, nullptr)); // Remove the flags argument. |
| 260 EXPECT_TRUE(TestPackExtensionFunction( | 279 EXPECT_TRUE(TestPackExtensionFunction( |
| 261 pack_args, api::developer_private::PACK_STATUS_ERROR, 0)); | 280 pack_args, api::developer_private::PACK_STATUS_ERROR, 0)); |
| 262 | 281 |
| 263 base::DeleteFile(crx_path, false); | 282 base::DeleteFile(crx_path, false); |
| 264 base::DeleteFile(pem_path, false); | 283 base::DeleteFile(pem_path, false); |
| 265 } | 284 } |
| 266 | 285 |
| 286 // Test developerPrivate.choosePath. | |
| 287 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateChoosePath) { | |
| 288 ResetThreadBundle(content::TestBrowserThreadBundle::DEFAULT); | |
| 289 content::TestWebContentsFactory web_contents_factory; | |
| 290 content::WebContents* web_contents = | |
| 291 web_contents_factory.CreateWebContents(profile()); | |
| 292 | |
| 293 base::FilePath expected_dir_path = data_dir().AppendASCII("good_unpacked"); | |
| 294 api::EntryPicker::SkipPickerAndAlwaysSelectPathForTest(&expected_dir_path); | |
| 295 | |
| 296 // Try selecting a directory. | |
| 297 base::ListValue choose_args; | |
| 298 choose_args.AppendString("FOLDER"); | |
| 299 choose_args.AppendString("LOAD"); | |
| 300 scoped_refptr<UIThreadExtensionFunction> function( | |
| 301 new api::DeveloperPrivateChoosePathFunction()); | |
| 302 function->SetRenderViewHost(web_contents->GetRenderViewHost()); | |
| 303 EXPECT_TRUE(RunFunction(function, choose_args)) << function->GetError(); | |
| 304 std::string path; | |
| 305 EXPECT_TRUE(function->GetResultList() && | |
| 306 function->GetResultList()->GetString(0, &path)); | |
| 307 EXPECT_EQ(path, expected_dir_path.AsUTF8Unsafe()); | |
| 308 | |
| 309 // Try selecting a pem file. | |
| 310 base::FilePath expected_file_path = | |
| 311 data_dir().AppendASCII("good_unpacked.pem"); | |
| 312 api::EntryPicker::SkipPickerAndAlwaysSelectPathForTest(&expected_file_path); | |
| 313 choose_args.Clear(); | |
| 314 choose_args.AppendString("FILE"); | |
| 315 choose_args.AppendString("PEM"); | |
| 316 function = new api::DeveloperPrivateChoosePathFunction(); | |
| 317 function->SetRenderViewHost(web_contents->GetRenderViewHost()); | |
| 318 EXPECT_TRUE(RunFunction(function, choose_args)) << function->GetError(); | |
| 319 EXPECT_TRUE(function->GetResultList() && | |
| 320 function->GetResultList()->GetString(0, &path)); | |
| 321 EXPECT_EQ(path, expected_file_path.AsUTF8Unsafe()); | |
| 322 | |
| 323 // Try canceling the file dialog. | |
| 324 api::EntryPicker::SkipPickerAndAlwaysCancelForTest(); | |
| 325 function = new api::DeveloperPrivateChoosePathFunction(); | |
| 326 function->SetRenderViewHost(web_contents->GetRenderViewHost()); | |
| 327 EXPECT_FALSE(RunFunction(function, choose_args)); | |
| 328 EXPECT_EQ(std::string("File selection was canceled."), function->GetError()); | |
| 329 } | |
| 330 | |
| 331 // Test developerPrivate.loadUnpacked. | |
| 332 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateLoadUnpacked) { | |
| 333 ResetThreadBundle(content::TestBrowserThreadBundle::DEFAULT); | |
| 334 content::TestWebContentsFactory web_contents_factory; | |
| 335 content::WebContents* web_contents = | |
| 336 web_contents_factory.CreateWebContents(profile()); | |
| 337 | |
| 338 base::FilePath path = data_dir().AppendASCII("good_unpacked"); | |
| 339 api::EntryPicker::SkipPickerAndAlwaysSelectPathForTest(&path); | |
| 340 | |
| 341 // Try loading a good extension (it should succeed, and the extension should | |
| 342 // be added). | |
| 343 scoped_refptr<UIThreadExtensionFunction> function( | |
| 344 new api::DeveloperPrivateLoadUnpackedFunction()); | |
| 345 function->SetRenderViewHost(web_contents->GetRenderViewHost()); | |
| 346 ExtensionIdSet current_ids = registry()->enabled_extensions().GetIDs(); | |
| 347 EXPECT_TRUE(RunFunction(function, base::ListValue())) << function->GetError(); | |
| 348 // We should have added one new extension. | |
| 349 ExtensionIdSet id_difference = base::STLSetDifference<ExtensionIdSet>( | |
| 350 registry()->enabled_extensions().GetIDs(), current_ids); | |
| 351 ASSERT_EQ(1u, id_difference.size()); | |
| 352 // The new extension should have the same path. | |
| 353 EXPECT_EQ( | |
| 354 path, | |
| 355 registry()->enabled_extensions().GetByID(*id_difference.begin())->path()); | |
| 356 | |
| 357 path = data_dir().AppendASCII("empty_manifest"); | |
| 358 api::EntryPicker::SkipPickerAndAlwaysSelectPathForTest(&path); | |
| 359 | |
| 360 // Try loading a bad extension (it should fail, and we should get an error). | |
| 361 function = new api::DeveloperPrivateLoadUnpackedFunction(); | |
| 362 function->SetRenderViewHost(web_contents->GetRenderViewHost()); | |
| 363 base::ListValue unpacked_args; | |
| 364 scoped_ptr<base::DictionaryValue> options(new base::DictionaryValue()); | |
| 365 options->SetBoolean("failQuietly", true); | |
| 366 unpacked_args.Append(options.release()); | |
| 367 current_ids = registry()->enabled_extensions().GetIDs(); | |
| 368 EXPECT_FALSE(RunFunction(function, unpacked_args)); | |
| 369 EXPECT_EQ(manifest_errors::kManifestUnreadable, function->GetError()); | |
| 370 // We should have no new extensions installed. | |
| 371 EXPECT_EQ(0u, base::STLSetDifference<ExtensionIdSet>( | |
| 372 registry()->enabled_extensions().GetIDs(), | |
| 373 current_ids).size()); | |
| 374 } | |
| 375 | |
| 267 } // namespace extensions | 376 } // namespace extensions |
| OLD | NEW |