| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "extensions/common/extension_api.h" | 5 #include "extensions/common/extension_api.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 639 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 650 | 650 |
| 651 static void GetDictionaryFromList(const base::DictionaryValue* schema, | 651 static void GetDictionaryFromList(const base::DictionaryValue* schema, |
| 652 const std::string& list_name, | 652 const std::string& list_name, |
| 653 const int list_index, | 653 const int list_index, |
| 654 const base::DictionaryValue** out) { | 654 const base::DictionaryValue** out) { |
| 655 const base::ListValue* list; | 655 const base::ListValue* list; |
| 656 EXPECT_TRUE(schema->GetList(list_name, &list)); | 656 EXPECT_TRUE(schema->GetList(list_name, &list)); |
| 657 EXPECT_TRUE(list->GetDictionary(list_index, out)); | 657 EXPECT_TRUE(list->GetDictionary(list_index, out)); |
| 658 } | 658 } |
| 659 | 659 |
| 660 static const base::DictionaryValue* GetDictChecked( |
| 661 const base::DictionaryValue* dict, |
| 662 const std::string& key) { |
| 663 const base::DictionaryValue* out = nullptr; |
| 664 CHECK(dict->GetDictionary(key, &out)) << key; |
| 665 return out; |
| 666 } |
| 667 |
| 668 static std::string GetStringChecked(const base::DictionaryValue* dict, |
| 669 const std::string& key) { |
| 670 std::string out; |
| 671 CHECK(dict->GetString(key, &out)) << key; |
| 672 return out; |
| 673 } |
| 674 |
| 660 TEST(ExtensionAPITest, TypesHaveNamespace) { | 675 TEST(ExtensionAPITest, TypesHaveNamespace) { |
| 661 base::FilePath manifest_path; | 676 std::unique_ptr<ExtensionAPI> api( |
| 662 PathService::Get(chrome::DIR_TEST_DATA, &manifest_path); | 677 ExtensionAPI::CreateWithDefaultConfiguration()); |
| 663 manifest_path = manifest_path.AppendASCII("extensions") | |
| 664 .AppendASCII("extension_api_unittest") | |
| 665 .AppendASCII("types_have_namespace.json"); | |
| 666 | 678 |
| 667 std::string manifest_str; | 679 // Returns the dictionary that has |key|: |value|. |
| 668 ASSERT_TRUE(base::ReadFileToString(manifest_path, &manifest_str)) | 680 auto get_dict_from_list = []( |
| 669 << "Failed to load: " << manifest_path.value(); | 681 const base::ListValue* list, const std::string& key, |
| 682 const std::string& value) -> const base::DictionaryValue* { |
| 683 const base::DictionaryValue* ret = nullptr; |
| 684 for (const auto& val : *list) { |
| 685 const base::DictionaryValue* dict = nullptr; |
| 686 if (!val->GetAsDictionary(&dict)) |
| 687 continue; |
| 688 std::string str; |
| 689 if (dict->GetString(key, &str) && str == value) { |
| 690 ret = dict; |
| 691 break; |
| 692 } |
| 693 } |
| 694 return ret; |
| 695 }; |
| 670 | 696 |
| 671 ExtensionAPI api; | 697 const base::DictionaryValue* schema = api->GetSchema("sessions"); |
| 672 api.RegisterSchemaResource("test.foo", 0); | 698 ASSERT_TRUE(schema); |
| 673 api.LoadSchema("test.foo", manifest_str); | |
| 674 | 699 |
| 675 const base::DictionaryValue* schema = api.GetSchema("test.foo"); | 700 const base::ListValue* types = nullptr; |
| 701 ASSERT_TRUE(schema->GetList("types", &types)); |
| 702 { |
| 703 const base::DictionaryValue* session_type = |
| 704 get_dict_from_list(types, "id", "sessions.Session"); |
| 705 ASSERT_TRUE(session_type); |
| 706 const base::DictionaryValue* props = |
| 707 GetDictChecked(session_type, "properties"); |
| 708 const base::DictionaryValue* tab = GetDictChecked(props, "tab"); |
| 709 EXPECT_EQ("tabs.Tab", GetStringChecked(tab, "$ref")); |
| 710 const base::DictionaryValue* window = GetDictChecked(props, "window"); |
| 711 EXPECT_EQ("windows.Window", GetStringChecked(window, "$ref")); |
| 712 } |
| 713 { |
| 714 const base::DictionaryValue* device_type = |
| 715 get_dict_from_list(types, "id", "sessions.Device"); |
| 716 ASSERT_TRUE(device_type); |
| 717 const base::DictionaryValue* props = |
| 718 GetDictChecked(device_type, "properties"); |
| 719 const base::DictionaryValue* sessions = GetDictChecked(props, "sessions"); |
| 720 const base::DictionaryValue* items = GetDictChecked(sessions, "items"); |
| 721 EXPECT_EQ("sessions.Session", GetStringChecked(items, "$ref")); |
| 722 } |
| 723 const base::ListValue* functions = nullptr; |
| 724 ASSERT_TRUE(schema->GetList("functions", &functions)); |
| 725 { |
| 726 const base::DictionaryValue* get_recently_closed = |
| 727 get_dict_from_list(functions, "name", "getRecentlyClosed"); |
| 728 ASSERT_TRUE(get_recently_closed); |
| 729 const base::ListValue* parameters = nullptr; |
| 730 ASSERT_TRUE(get_recently_closed->GetList("parameters", ¶meters)); |
| 731 const base::DictionaryValue* filter = |
| 732 get_dict_from_list(parameters, "name", "filter"); |
| 733 ASSERT_TRUE(filter); |
| 734 EXPECT_EQ("sessions.Filter", GetStringChecked(filter, "$ref")); |
| 735 } |
| 676 | 736 |
| 677 const base::DictionaryValue* dict; | 737 schema = api->GetSchema("types"); |
| 678 const base::DictionaryValue* sub_dict; | 738 ASSERT_TRUE(schema); |
| 679 std::string type; | 739 ASSERT_TRUE(schema->GetList("types", &types)); |
| 680 | 740 { |
| 681 GetDictionaryFromList(schema, "types", 0, &dict); | 741 const base::DictionaryValue* chrome_setting = |
| 682 EXPECT_TRUE(dict->GetString("id", &type)); | 742 get_dict_from_list(types, "id", "types.ChromeSetting"); |
| 683 EXPECT_EQ("test.foo.TestType", type); | 743 ASSERT_TRUE(chrome_setting); |
| 684 EXPECT_TRUE(dict->GetString("customBindings", &type)); | 744 EXPECT_EQ("types.ChromeSetting", |
| 685 EXPECT_EQ("test.foo.TestType", type); | 745 GetStringChecked(chrome_setting, "customBindings")); |
| 686 EXPECT_TRUE(dict->GetDictionary("properties", &sub_dict)); | 746 } |
| 687 const base::DictionaryValue* property; | |
| 688 EXPECT_TRUE(sub_dict->GetDictionary("foo", &property)); | |
| 689 EXPECT_TRUE(property->GetString("$ref", &type)); | |
| 690 EXPECT_EQ("test.foo.OtherType", type); | |
| 691 EXPECT_TRUE(sub_dict->GetDictionary("bar", &property)); | |
| 692 EXPECT_TRUE(property->GetString("$ref", &type)); | |
| 693 EXPECT_EQ("fully.qualified.Type", type); | |
| 694 | |
| 695 GetDictionaryFromList(schema, "functions", 0, &dict); | |
| 696 GetDictionaryFromList(dict, "parameters", 0, &sub_dict); | |
| 697 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); | |
| 698 EXPECT_EQ("test.foo.TestType", type); | |
| 699 EXPECT_TRUE(dict->GetDictionary("returns", &sub_dict)); | |
| 700 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); | |
| 701 EXPECT_EQ("fully.qualified.Type", type); | |
| 702 | |
| 703 GetDictionaryFromList(schema, "functions", 1, &dict); | |
| 704 GetDictionaryFromList(dict, "parameters", 0, &sub_dict); | |
| 705 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); | |
| 706 EXPECT_EQ("fully.qualified.Type", type); | |
| 707 EXPECT_TRUE(dict->GetDictionary("returns", &sub_dict)); | |
| 708 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); | |
| 709 EXPECT_EQ("test.foo.TestType", type); | |
| 710 | |
| 711 GetDictionaryFromList(schema, "events", 0, &dict); | |
| 712 GetDictionaryFromList(dict, "parameters", 0, &sub_dict); | |
| 713 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); | |
| 714 EXPECT_EQ("test.foo.TestType", type); | |
| 715 GetDictionaryFromList(dict, "parameters", 1, &sub_dict); | |
| 716 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); | |
| 717 EXPECT_EQ("fully.qualified.Type", type); | |
| 718 } | 747 } |
| 719 | 748 |
| 720 // Tests API availability with an empty manifest. | 749 // Tests API availability with an empty manifest. |
| 721 TEST(ExtensionAPITest, NoPermissions) { | 750 TEST(ExtensionAPITest, NoPermissions) { |
| 722 const struct { | 751 const struct { |
| 723 const char* permission_name; | 752 const char* permission_name; |
| 724 bool expect_success; | 753 bool expect_success; |
| 725 } kTests[] = { | 754 } kTests[] = { |
| 726 // Test default module/package permission. | 755 // Test default module/package permission. |
| 727 { "extension", true }, | 756 { "extension", true }, |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 795 extension.get(), | 824 extension.get(), |
| 796 Feature::BLESSED_EXTENSION_CONTEXT, | 825 Feature::BLESSED_EXTENSION_CONTEXT, |
| 797 GURL()).is_available()); | 826 GURL()).is_available()); |
| 798 EXPECT_FALSE(extension_api->IsAvailable("pageAction", | 827 EXPECT_FALSE(extension_api->IsAvailable("pageAction", |
| 799 extension.get(), | 828 extension.get(), |
| 800 Feature::BLESSED_EXTENSION_CONTEXT, | 829 Feature::BLESSED_EXTENSION_CONTEXT, |
| 801 GURL()).is_available()); | 830 GURL()).is_available()); |
| 802 } | 831 } |
| 803 | 832 |
| 804 } // namespace extensions | 833 } // namespace extensions |
| OLD | NEW |