| 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 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 namespace extensions { | 32 namespace extensions { |
| 33 | 33 |
| 34 namespace { | 34 namespace { |
| 35 | 35 |
| 36 const char* const kTestFeatures[] = { | 36 const char* const kTestFeatures[] = { |
| 37 "test1", "test2", "test3", "test4", "test5", | 37 "test1", "test2", "test3", "test4", "test5", |
| 38 "test6", "test7", "parent1", "parent2", "parent3", | 38 "test6", "test7", "parent1", "parent2", "parent3", |
| 39 }; | 39 }; |
| 40 | 40 |
| 41 UnittestFeatureProvider api_feature_provider; | 41 class TestExtensionAPI : public ExtensionAPI { |
| 42 } | 42 public: |
| 43 TestExtensionAPI() {} |
| 44 ~TestExtensionAPI() override {} |
| 45 |
| 46 void add_fake_schema(const std::string& name) { fake_schemas_.insert(name); } |
| 47 |
| 48 private: |
| 49 bool IsKnownAPI(const std::string& name, ExtensionsClient* client) override { |
| 50 return fake_schemas_.count(name) != 0; |
| 51 } |
| 52 |
| 53 std::set<std::string> fake_schemas_; |
| 54 DISALLOW_COPY_AND_ASSIGN(TestExtensionAPI); |
| 55 }; |
| 56 |
| 57 } // namespace |
| 43 | 58 |
| 44 using test_util::BuildExtension; | 59 using test_util::BuildExtension; |
| 45 | 60 |
| 46 TEST(ExtensionAPITest, Creation) { | 61 TEST(ExtensionAPITest, Creation) { |
| 47 ExtensionAPI* shared_instance = ExtensionAPI::GetSharedInstance(); | 62 ExtensionAPI* shared_instance = ExtensionAPI::GetSharedInstance(); |
| 48 EXPECT_EQ(shared_instance, ExtensionAPI::GetSharedInstance()); | 63 EXPECT_EQ(shared_instance, ExtensionAPI::GetSharedInstance()); |
| 49 | 64 |
| 50 std::unique_ptr<ExtensionAPI> new_instance( | 65 std::unique_ptr<ExtensionAPI> new_instance( |
| 51 ExtensionAPI::CreateWithDefaultConfiguration()); | 66 ExtensionAPI::CreateWithDefaultConfiguration()); |
| 52 EXPECT_NE(new_instance.get(), | 67 EXPECT_NE(new_instance.get(), |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 { "parent3.noparent.child", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() }, | 178 { "parent3.noparent.child", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() }, |
| 164 { "parent3.noparent.child", true, Feature::BLESSED_EXTENSION_CONTEXT, | 179 { "parent3.noparent.child", true, Feature::BLESSED_EXTENSION_CONTEXT, |
| 165 GURL() }, | 180 GURL() }, |
| 166 { "parent3.noparent.child", true, Feature::UNBLESSED_EXTENSION_CONTEXT, | 181 { "parent3.noparent.child", true, Feature::UNBLESSED_EXTENSION_CONTEXT, |
| 167 GURL() } | 182 GURL() } |
| 168 }; | 183 }; |
| 169 | 184 |
| 170 UnittestFeatureProvider api_feature_provider; | 185 UnittestFeatureProvider api_feature_provider; |
| 171 | 186 |
| 172 for (size_t i = 0; i < arraysize(test_data); ++i) { | 187 for (size_t i = 0; i < arraysize(test_data); ++i) { |
| 173 ExtensionAPI api; | 188 TestExtensionAPI api; |
| 174 api.RegisterDependencyProvider("api", &api_feature_provider); | 189 api.RegisterDependencyProvider("api", &api_feature_provider); |
| 175 for (const auto& key : kTestFeatures) | 190 for (const auto& key : kTestFeatures) |
| 176 api.RegisterSchemaResource(key, 0); | 191 api.add_fake_schema(key); |
| 177 ExtensionAPI::OverrideSharedInstanceForTest scope(&api); | 192 ExtensionAPI::OverrideSharedInstanceForTest scope(&api); |
| 178 | 193 |
| 179 bool expected = test_data[i].expect_is_available; | 194 bool expected = test_data[i].expect_is_available; |
| 180 Feature::Availability availability = | 195 Feature::Availability availability = |
| 181 api.IsAvailable(test_data[i].api_full_name, | 196 api.IsAvailable(test_data[i].api_full_name, |
| 182 NULL, | 197 NULL, |
| 183 test_data[i].context, | 198 test_data[i].context, |
| 184 test_data[i].url); | 199 test_data[i].url); |
| 185 EXPECT_EQ(expected, availability.is_available()) | 200 EXPECT_EQ(expected, availability.is_available()) |
| 186 << base::StringPrintf("Test %d: Feature '%s' was %s: %s", | 201 << base::StringPrintf("Test %d: Feature '%s' was %s: %s", |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 { "test4.foo", true, Feature::CONTENT_SCRIPT_CONTEXT, NULL, GURL() }, | 256 { "test4.foo", true, Feature::CONTENT_SCRIPT_CONTEXT, NULL, GURL() }, |
| 242 { "test7", false, Feature::WEB_PAGE_CONTEXT, NULL, | 257 { "test7", false, Feature::WEB_PAGE_CONTEXT, NULL, |
| 243 GURL("http://google.com") }, | 258 GURL("http://google.com") }, |
| 244 { "test7", true, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://foo.com") }, | 259 { "test7", true, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://foo.com") }, |
| 245 { "test7", false, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://bar.com") } | 260 { "test7", false, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://bar.com") } |
| 246 }; | 261 }; |
| 247 | 262 |
| 248 UnittestFeatureProvider api_feature_provider; | 263 UnittestFeatureProvider api_feature_provider; |
| 249 | 264 |
| 250 for (size_t i = 0; i < arraysize(test_data); ++i) { | 265 for (size_t i = 0; i < arraysize(test_data); ++i) { |
| 251 ExtensionAPI api; | 266 TestExtensionAPI api; |
| 252 api.RegisterDependencyProvider("api", &api_feature_provider); | 267 api.RegisterDependencyProvider("api", &api_feature_provider); |
| 253 for (const auto& key : kTestFeatures) | 268 for (const auto& key : kTestFeatures) |
| 254 api.RegisterSchemaResource(key, 0); | 269 api.add_fake_schema(key); |
| 255 ExtensionAPI::OverrideSharedInstanceForTest scope(&api); | 270 ExtensionAPI::OverrideSharedInstanceForTest scope(&api); |
| 256 | 271 |
| 257 Feature* test_feature = | 272 Feature* test_feature = |
| 258 api_feature_provider.GetFeature(test_data[i].api_full_name); | 273 api_feature_provider.GetFeature(test_data[i].api_full_name); |
| 259 ASSERT_TRUE(test_feature); | 274 ASSERT_TRUE(test_feature); |
| 260 EXPECT_EQ(test_data[i].expect_is_available, | 275 EXPECT_EQ(test_data[i].expect_is_available, |
| 261 api.IsAnyFeatureAvailableToContext(*test_feature, | 276 api.IsAnyFeatureAvailableToContext(*test_feature, |
| 262 test_data[i].extension, | 277 test_data[i].extension, |
| 263 test_data[i].context, | 278 test_data[i].context, |
| 264 test_data[i].url)) | 279 test_data[i].url)) |
| (...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 661 base::FilePath manifest_path; | 676 base::FilePath manifest_path; |
| 662 PathService::Get(chrome::DIR_TEST_DATA, &manifest_path); | 677 PathService::Get(chrome::DIR_TEST_DATA, &manifest_path); |
| 663 manifest_path = manifest_path.AppendASCII("extensions") | 678 manifest_path = manifest_path.AppendASCII("extensions") |
| 664 .AppendASCII("extension_api_unittest") | 679 .AppendASCII("extension_api_unittest") |
| 665 .AppendASCII("types_have_namespace.json"); | 680 .AppendASCII("types_have_namespace.json"); |
| 666 | 681 |
| 667 std::string manifest_str; | 682 std::string manifest_str; |
| 668 ASSERT_TRUE(base::ReadFileToString(manifest_path, &manifest_str)) | 683 ASSERT_TRUE(base::ReadFileToString(manifest_path, &manifest_str)) |
| 669 << "Failed to load: " << manifest_path.value(); | 684 << "Failed to load: " << manifest_path.value(); |
| 670 | 685 |
| 671 ExtensionAPI api; | 686 TestExtensionAPI api; |
| 672 api.RegisterSchemaResource("test.foo", 0); | 687 api.add_fake_schema("test.foo"); |
| 673 api.LoadSchema("test.foo", manifest_str); | 688 api.LoadSchema("test.foo", manifest_str); |
| 674 | 689 |
| 675 const base::DictionaryValue* schema = api.GetSchema("test.foo"); | 690 const base::DictionaryValue* schema = api.GetSchema("test.foo"); |
| 676 | 691 |
| 677 const base::DictionaryValue* dict; | 692 const base::DictionaryValue* dict; |
| 678 const base::DictionaryValue* sub_dict; | 693 const base::DictionaryValue* sub_dict; |
| 679 std::string type; | 694 std::string type; |
| 680 | 695 |
| 681 GetDictionaryFromList(schema, "types", 0, &dict); | 696 GetDictionaryFromList(schema, "types", 0, &dict); |
| 682 EXPECT_TRUE(dict->GetString("id", &type)); | 697 EXPECT_TRUE(dict->GetString("id", &type)); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 795 extension.get(), | 810 extension.get(), |
| 796 Feature::BLESSED_EXTENSION_CONTEXT, | 811 Feature::BLESSED_EXTENSION_CONTEXT, |
| 797 GURL()).is_available()); | 812 GURL()).is_available()); |
| 798 EXPECT_FALSE(extension_api->IsAvailable("pageAction", | 813 EXPECT_FALSE(extension_api->IsAvailable("pageAction", |
| 799 extension.get(), | 814 extension.get(), |
| 800 Feature::BLESSED_EXTENSION_CONTEXT, | 815 Feature::BLESSED_EXTENSION_CONTEXT, |
| 801 GURL()).is_available()); | 816 GURL()).is_available()); |
| 802 } | 817 } |
| 803 | 818 |
| 804 } // namespace extensions | 819 } // namespace extensions |
| OLD | NEW |