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

Side by Side Diff: chrome/browser/extensions/api/developer_private/developer_private_api_unittest.cc

Issue 2767173002: [MD Extensions] Respond with a load error when loading unpacked fails (Closed)
Patch Set: . Created 3 years, 9 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 "chrome/browser/extensions/api/developer_private/developer_private_api. h" 5 #include "chrome/browser/extensions/api/developer_private/developer_private_api. h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
(...skipping 15 matching lines...) Expand all
26 #include "chrome/test/base/test_browser_window.h" 26 #include "chrome/test/base/test_browser_window.h"
27 #include "components/crx_file/id_util.h" 27 #include "components/crx_file/id_util.h"
28 #include "components/policy/core/browser/browser_policy_connector.h" 28 #include "components/policy/core/browser/browser_policy_connector.h"
29 #include "components/policy/core/common/mock_configuration_policy_provider.h" 29 #include "components/policy/core/common/mock_configuration_policy_provider.h"
30 #include "components/policy/core/common/policy_map.h" 30 #include "components/policy/core/common/policy_map.h"
31 #include "components/policy/core/common/policy_service_impl.h" 31 #include "components/policy/core/common/policy_service_impl.h"
32 #include "components/policy/core/common/policy_types.h" 32 #include "components/policy/core/common/policy_types.h"
33 #include "components/policy/policy_constants.h" 33 #include "components/policy/policy_constants.h"
34 #include "components/sync_preferences/testing_pref_service_syncable.h" 34 #include "components/sync_preferences/testing_pref_service_syncable.h"
35 #include "content/public/test/web_contents_tester.h" 35 #include "content/public/test/web_contents_tester.h"
36 #include "extensions/browser/api_test_utils.h"
36 #include "extensions/browser/event_router_factory.h" 37 #include "extensions/browser/event_router_factory.h"
37 #include "extensions/browser/extension_error_test_util.h" 38 #include "extensions/browser/extension_error_test_util.h"
38 #include "extensions/browser/extension_prefs.h" 39 #include "extensions/browser/extension_prefs.h"
39 #include "extensions/browser/extension_registry.h" 40 #include "extensions/browser/extension_registry.h"
40 #include "extensions/browser/extension_system.h" 41 #include "extensions/browser/extension_system.h"
41 #include "extensions/browser/test_extension_registry_observer.h" 42 #include "extensions/browser/test_extension_registry_observer.h"
42 #include "extensions/common/extension.h" 43 #include "extensions/common/extension.h"
43 #include "extensions/common/extension_builder.h" 44 #include "extensions/common/extension_builder.h"
44 #include "extensions/common/extension_set.h" 45 #include "extensions/common/extension_set.h"
45 #include "extensions/common/feature_switch.h" 46 #include "extensions/common/feature_switch.h"
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after
494 unpacked_args.Append(std::move(options)); 495 unpacked_args.Append(std::move(options));
495 current_ids = registry()->enabled_extensions().GetIDs(); 496 current_ids = registry()->enabled_extensions().GetIDs();
496 EXPECT_FALSE(RunFunction(function, unpacked_args)); 497 EXPECT_FALSE(RunFunction(function, unpacked_args));
497 EXPECT_EQ(manifest_errors::kManifestUnreadable, function->GetError()); 498 EXPECT_EQ(manifest_errors::kManifestUnreadable, function->GetError());
498 // We should have no new extensions installed. 499 // We should have no new extensions installed.
499 EXPECT_EQ(0u, base::STLSetDifference<ExtensionIdSet>( 500 EXPECT_EQ(0u, base::STLSetDifference<ExtensionIdSet>(
500 registry()->enabled_extensions().GetIDs(), 501 registry()->enabled_extensions().GetIDs(),
501 current_ids).size()); 502 current_ids).size());
502 } 503 }
503 504
505 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateLoadUnpackedLoadError) {
506 std::unique_ptr<content::WebContents> web_contents(
507 content::WebContentsTester::CreateTestWebContents(profile(), nullptr));
508
509 {
510 // Load an extension with a clear manifest error ('version' is invalid).
511 TestExtensionDir dir;
512 dir.WriteManifest(
513 R"({
514 "name": "foo",
515 "description": "bar",
516 "version": 1,
517 "manifest_version": 2
518 })");
519 base::FilePath path = dir.UnpackedPath();
520 api::EntryPicker::SkipPickerAndAlwaysSelectPathForTest(&path);
521
522 scoped_refptr<UIThreadExtensionFunction> function(
523 new api::DeveloperPrivateLoadUnpackedFunction());
524 function->SetRenderFrameHost(web_contents->GetMainFrame());
525 std::unique_ptr<base::Value> result =
526 api_test_utils::RunFunctionAndReturnSingleResult(
527 function.get(),
528 "[{\"failQuietly\": true, \"populateError\": true}]", profile());
529 // The loadError result should be populated.
530 ASSERT_TRUE(result);
531 std::unique_ptr<api::developer_private::LoadError> error =
532 api::developer_private::LoadError::FromValue(*result);
533 ASSERT_TRUE(error);
534 ASSERT_TRUE(error->source);
535 // The source should have *something* (rely on file highlighter tests for
536 // the correct population).
537 EXPECT_FALSE(error->source->before_highlight.empty());
538 // The error should be appropriate (mentioning that version was invalid).
539 EXPECT_TRUE(error->error.find("version") != std::string::npos)
540 << error->error;
541 }
542
543 {
544 // Load an extension with no manifest.
545 TestExtensionDir dir;
546 base::FilePath path = dir.UnpackedPath();
547 api::EntryPicker::SkipPickerAndAlwaysSelectPathForTest(&path);
548
549 scoped_refptr<UIThreadExtensionFunction> function(
550 new api::DeveloperPrivateLoadUnpackedFunction());
551 function->SetRenderFrameHost(web_contents->GetMainFrame());
552 std::unique_ptr<base::Value> result =
553 api_test_utils::RunFunctionAndReturnSingleResult(
554 function.get(),
555 "[{\"failQuietly\": true, \"populateError\": true}]", profile());
556 // The load error should be populated.
557 ASSERT_TRUE(result);
558 std::unique_ptr<api::developer_private::LoadError> error =
559 api::developer_private::LoadError::FromValue(*result);
560 ASSERT_TRUE(error);
561 // The file source should be empty.
562 ASSERT_TRUE(error->source);
563 EXPECT_TRUE(error->source->before_highlight.empty());
564 EXPECT_TRUE(error->source->highlight.empty());
565 EXPECT_TRUE(error->source->after_highlight.empty());
566 }
567
568 {
569 // Load a valid extension.
570 TestExtensionDir dir;
571 dir.WriteManifest(
572 R"({
573 "name": "foo",
574 "description": "bar",
575 "version": "1.0",
576 "manifest_version": 2
577 })");
578 base::FilePath path = dir.UnpackedPath();
579 api::EntryPicker::SkipPickerAndAlwaysSelectPathForTest(&path);
580
581 scoped_refptr<UIThreadExtensionFunction> function(
582 new api::DeveloperPrivateLoadUnpackedFunction());
583 function->SetRenderFrameHost(web_contents->GetMainFrame());
584 std::unique_ptr<base::Value> result =
585 api_test_utils::RunFunctionAndReturnSingleResult(
586 function.get(),
587 "[{\"failQuietly\": true, \"populateError\": true}]", profile());
588 // There should be no load error.
589 ASSERT_FALSE(result);
590 }
591 }
592
504 // Test developerPrivate.requestFileSource. 593 // Test developerPrivate.requestFileSource.
505 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateRequestFileSource) { 594 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateRequestFileSource) {
506 // Testing of this function seems light, but that's because it basically just 595 // Testing of this function seems light, but that's because it basically just
507 // forwards to reading a file to a string, and highlighting it - both of which 596 // forwards to reading a file to a string, and highlighting it - both of which
508 // are already tested separately. 597 // are already tested separately.
509 const Extension* extension = LoadUnpackedExtension(); 598 const Extension* extension = LoadUnpackedExtension();
510 const char kErrorMessage[] = "Something went wrong"; 599 const char kErrorMessage[] = "Something went wrong";
511 api::developer_private::RequestFileSourceProperties properties; 600 api::developer_private::RequestFileSourceProperties properties;
512 properties.extension_id = extension->id(); 601 properties.extension_id = extension->id();
513 properties.path_suffix = "manifest.json"; 602 properties.path_suffix = "manifest.json";
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
661 EXPECT_TRUE( 750 EXPECT_TRUE(
662 profile()->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode)); 751 profile()->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode));
663 752
664 std::unique_ptr<api::developer_private::ProfileInfo> profile_info; 753 std::unique_ptr<api::developer_private::ProfileInfo> profile_info;
665 ASSERT_NO_FATAL_FAILURE(GetProfileConfiguration(&profile_info)); 754 ASSERT_NO_FATAL_FAILURE(GetProfileConfiguration(&profile_info));
666 EXPECT_TRUE(profile_info->in_developer_mode); 755 EXPECT_TRUE(profile_info->in_developer_mode);
667 EXPECT_FALSE(profile_info->is_developer_mode_controlled_by_policy); 756 EXPECT_FALSE(profile_info->is_developer_mode_controlled_by_policy);
668 } 757 }
669 758
670 } // namespace extensions 759 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698