| 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 654 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 665 | 665 |
| 666 static void GetDictionaryFromList(const base::DictionaryValue* schema, | 666 static void GetDictionaryFromList(const base::DictionaryValue* schema, |
| 667 const std::string& list_name, | 667 const std::string& list_name, |
| 668 const int list_index, | 668 const int list_index, |
| 669 const base::DictionaryValue** out) { | 669 const base::DictionaryValue** out) { |
| 670 const base::ListValue* list; | 670 const base::ListValue* list; |
| 671 EXPECT_TRUE(schema->GetList(list_name, &list)); | 671 EXPECT_TRUE(schema->GetList(list_name, &list)); |
| 672 EXPECT_TRUE(list->GetDictionary(list_index, out)); | 672 EXPECT_TRUE(list->GetDictionary(list_index, out)); |
| 673 } | 673 } |
| 674 | 674 |
| 675 static const base::DictionaryValue* GetDictChecked( |
| 676 const base::DictionaryValue* dict, |
| 677 const std::string& key) { |
| 678 const base::DictionaryValue* out = nullptr; |
| 679 CHECK(dict->GetDictionary(key, &out)) << key; |
| 680 return out; |
| 681 } |
| 682 |
| 683 static std::string GetStringChecked(const base::DictionaryValue* dict, |
| 684 const std::string& key) { |
| 685 std::string out; |
| 686 CHECK(dict->GetString(key, &out)) << key; |
| 687 return out; |
| 688 } |
| 689 |
| 675 TEST(ExtensionAPITest, TypesHaveNamespace) { | 690 TEST(ExtensionAPITest, TypesHaveNamespace) { |
| 676 base::FilePath manifest_path; | 691 std::unique_ptr<ExtensionAPI> api( |
| 677 PathService::Get(chrome::DIR_TEST_DATA, &manifest_path); | 692 ExtensionAPI::CreateWithDefaultConfiguration()); |
| 678 manifest_path = manifest_path.AppendASCII("extensions") | |
| 679 .AppendASCII("extension_api_unittest") | |
| 680 .AppendASCII("types_have_namespace.json"); | |
| 681 | 693 |
| 682 std::string manifest_str; | 694 // Returns the dictionary that has |key|: |value|. |
| 683 ASSERT_TRUE(base::ReadFileToString(manifest_path, &manifest_str)) | 695 auto get_dict_from_list = []( |
| 684 << "Failed to load: " << manifest_path.value(); | 696 const base::ListValue* list, const std::string& key, |
| 697 const std::string& value) -> const base::DictionaryValue* { |
| 698 const base::DictionaryValue* ret = nullptr; |
| 699 for (const auto& val : *list) { |
| 700 const base::DictionaryValue* dict = nullptr; |
| 701 if (!val->GetAsDictionary(&dict)) |
| 702 continue; |
| 703 std::string str; |
| 704 if (dict->GetString(key, &str) && str == value) { |
| 705 ret = dict; |
| 706 break; |
| 707 } |
| 708 } |
| 709 return ret; |
| 710 }; |
| 685 | 711 |
| 686 TestExtensionAPI api; | 712 const base::DictionaryValue* schema = api->GetSchema("sessions"); |
| 687 api.add_fake_schema("test.foo"); | 713 ASSERT_TRUE(schema); |
| 688 api.LoadSchema("test.foo", manifest_str); | |
| 689 | 714 |
| 690 const base::DictionaryValue* schema = api.GetSchema("test.foo"); | 715 const base::ListValue* types = nullptr; |
| 716 ASSERT_TRUE(schema->GetList("types", &types)); |
| 717 { |
| 718 const base::DictionaryValue* session_type = |
| 719 get_dict_from_list(types, "id", "sessions.Session"); |
| 720 ASSERT_TRUE(session_type); |
| 721 const base::DictionaryValue* props = |
| 722 GetDictChecked(session_type, "properties"); |
| 723 const base::DictionaryValue* tab = GetDictChecked(props, "tab"); |
| 724 EXPECT_EQ("tabs.Tab", GetStringChecked(tab, "$ref")); |
| 725 const base::DictionaryValue* window = GetDictChecked(props, "window"); |
| 726 EXPECT_EQ("windows.Window", GetStringChecked(window, "$ref")); |
| 727 } |
| 728 { |
| 729 const base::DictionaryValue* device_type = |
| 730 get_dict_from_list(types, "id", "sessions.Device"); |
| 731 ASSERT_TRUE(device_type); |
| 732 const base::DictionaryValue* props = |
| 733 GetDictChecked(device_type, "properties"); |
| 734 const base::DictionaryValue* sessions = GetDictChecked(props, "sessions"); |
| 735 const base::DictionaryValue* items = GetDictChecked(sessions, "items"); |
| 736 EXPECT_EQ("sessions.Session", GetStringChecked(items, "$ref")); |
| 737 } |
| 738 const base::ListValue* functions = nullptr; |
| 739 ASSERT_TRUE(schema->GetList("functions", &functions)); |
| 740 { |
| 741 const base::DictionaryValue* get_recently_closed = |
| 742 get_dict_from_list(functions, "name", "getRecentlyClosed"); |
| 743 ASSERT_TRUE(get_recently_closed); |
| 744 const base::ListValue* parameters = nullptr; |
| 745 ASSERT_TRUE(get_recently_closed->GetList("parameters", ¶meters)); |
| 746 const base::DictionaryValue* filter = |
| 747 get_dict_from_list(parameters, "name", "filter"); |
| 748 ASSERT_TRUE(filter); |
| 749 EXPECT_EQ("sessions.Filter", GetStringChecked(filter, "$ref")); |
| 750 } |
| 691 | 751 |
| 692 const base::DictionaryValue* dict; | 752 schema = api->GetSchema("types"); |
| 693 const base::DictionaryValue* sub_dict; | 753 ASSERT_TRUE(schema); |
| 694 std::string type; | 754 ASSERT_TRUE(schema->GetList("types", &types)); |
| 695 | 755 { |
| 696 GetDictionaryFromList(schema, "types", 0, &dict); | 756 const base::DictionaryValue* chrome_setting = |
| 697 EXPECT_TRUE(dict->GetString("id", &type)); | 757 get_dict_from_list(types, "id", "types.ChromeSetting"); |
| 698 EXPECT_EQ("test.foo.TestType", type); | 758 ASSERT_TRUE(chrome_setting); |
| 699 EXPECT_TRUE(dict->GetString("customBindings", &type)); | 759 EXPECT_EQ("types.ChromeSetting", |
| 700 EXPECT_EQ("test.foo.TestType", type); | 760 GetStringChecked(chrome_setting, "customBindings")); |
| 701 EXPECT_TRUE(dict->GetDictionary("properties", &sub_dict)); | 761 } |
| 702 const base::DictionaryValue* property; | |
| 703 EXPECT_TRUE(sub_dict->GetDictionary("foo", &property)); | |
| 704 EXPECT_TRUE(property->GetString("$ref", &type)); | |
| 705 EXPECT_EQ("test.foo.OtherType", type); | |
| 706 EXPECT_TRUE(sub_dict->GetDictionary("bar", &property)); | |
| 707 EXPECT_TRUE(property->GetString("$ref", &type)); | |
| 708 EXPECT_EQ("fully.qualified.Type", type); | |
| 709 | |
| 710 GetDictionaryFromList(schema, "functions", 0, &dict); | |
| 711 GetDictionaryFromList(dict, "parameters", 0, &sub_dict); | |
| 712 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); | |
| 713 EXPECT_EQ("test.foo.TestType", type); | |
| 714 EXPECT_TRUE(dict->GetDictionary("returns", &sub_dict)); | |
| 715 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); | |
| 716 EXPECT_EQ("fully.qualified.Type", type); | |
| 717 | |
| 718 GetDictionaryFromList(schema, "functions", 1, &dict); | |
| 719 GetDictionaryFromList(dict, "parameters", 0, &sub_dict); | |
| 720 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); | |
| 721 EXPECT_EQ("fully.qualified.Type", type); | |
| 722 EXPECT_TRUE(dict->GetDictionary("returns", &sub_dict)); | |
| 723 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); | |
| 724 EXPECT_EQ("test.foo.TestType", type); | |
| 725 | |
| 726 GetDictionaryFromList(schema, "events", 0, &dict); | |
| 727 GetDictionaryFromList(dict, "parameters", 0, &sub_dict); | |
| 728 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); | |
| 729 EXPECT_EQ("test.foo.TestType", type); | |
| 730 GetDictionaryFromList(dict, "parameters", 1, &sub_dict); | |
| 731 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); | |
| 732 EXPECT_EQ("fully.qualified.Type", type); | |
| 733 } | 762 } |
| 734 | 763 |
| 735 // Tests API availability with an empty manifest. | 764 // Tests API availability with an empty manifest. |
| 736 TEST(ExtensionAPITest, NoPermissions) { | 765 TEST(ExtensionAPITest, NoPermissions) { |
| 737 const struct { | 766 const struct { |
| 738 const char* permission_name; | 767 const char* permission_name; |
| 739 bool expect_success; | 768 bool expect_success; |
| 740 } kTests[] = { | 769 } kTests[] = { |
| 741 // Test default module/package permission. | 770 // Test default module/package permission. |
| 742 { "extension", true }, | 771 { "extension", true }, |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 810 extension.get(), | 839 extension.get(), |
| 811 Feature::BLESSED_EXTENSION_CONTEXT, | 840 Feature::BLESSED_EXTENSION_CONTEXT, |
| 812 GURL()).is_available()); | 841 GURL()).is_available()); |
| 813 EXPECT_FALSE(extension_api->IsAvailable("pageAction", | 842 EXPECT_FALSE(extension_api->IsAvailable("pageAction", |
| 814 extension.get(), | 843 extension.get(), |
| 815 Feature::BLESSED_EXTENSION_CONTEXT, | 844 Feature::BLESSED_EXTENSION_CONTEXT, |
| 816 GURL()).is_available()); | 845 GURL()).is_available()); |
| 817 } | 846 } |
| 818 | 847 |
| 819 } // namespace extensions | 848 } // namespace extensions |
| OLD | NEW |