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

Side by Side Diff: chrome/common/extensions/api/common_extension_api_unittest.cc

Issue 2202733003: [Extensions] Remove JSONFeatureProvider, SimpleFeature::Parse (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Lei's Created 4 years, 4 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
« no previous file with comments | « chrome/common/extensions/BUILD.gn ('k') | chrome/common/extensions/chrome_extensions_client.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
11 #include <utility> 11 #include <utility>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/files/file_path.h" 14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h" 15 #include "base/files/file_util.h"
16 #include "base/json/json_reader.h"
17 #include "base/json/json_writer.h"
18 #include "base/macros.h" 16 #include "base/macros.h"
19 #include "base/memory/ref_counted.h" 17 #include "base/memory/ref_counted.h"
20 #include "base/path_service.h" 18 #include "base/path_service.h"
21 #include "base/strings/stringprintf.h" 19 #include "base/strings/stringprintf.h"
22 #include "base/values.h" 20 #include "base/values.h"
23 #include "chrome/common/chrome_paths.h" 21 #include "chrome/common/chrome_paths.h"
22 #include "chrome/common/extensions/extension_features_unittest.h"
24 #include "extensions/common/extension.h" 23 #include "extensions/common/extension.h"
25 #include "extensions/common/extension_builder.h" 24 #include "extensions/common/extension_builder.h"
26 #include "extensions/common/features/api_feature.h"
27 #include "extensions/common/features/json_feature_provider.h"
28 #include "extensions/common/features/simple_feature.h" 25 #include "extensions/common/features/simple_feature.h"
29 #include "extensions/common/manifest.h" 26 #include "extensions/common/manifest.h"
30 #include "extensions/common/manifest_constants.h" 27 #include "extensions/common/manifest_constants.h"
31 #include "extensions/common/test_util.h" 28 #include "extensions/common/test_util.h"
32 #include "extensions/common/value_builder.h" 29 #include "extensions/common/value_builder.h"
33 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
34 31
35 namespace extensions { 32 namespace extensions {
36 33
34 namespace {
35
36 const char* const kTestFeatures[] = {
37 "test1", "test2", "test3", "test4", "test5",
38 "test6", "test7", "parent1", "parent2", "parent3",
39 };
40
41 UnittestFeatureProvider api_feature_provider;
42 }
43
37 using test_util::BuildExtension; 44 using test_util::BuildExtension;
38 45
39 SimpleFeature* CreateAPIFeature() {
40 return new APIFeature();
41 }
42
43 TEST(ExtensionAPITest, Creation) { 46 TEST(ExtensionAPITest, Creation) {
44 ExtensionAPI* shared_instance = ExtensionAPI::GetSharedInstance(); 47 ExtensionAPI* shared_instance = ExtensionAPI::GetSharedInstance();
45 EXPECT_EQ(shared_instance, ExtensionAPI::GetSharedInstance()); 48 EXPECT_EQ(shared_instance, ExtensionAPI::GetSharedInstance());
46 49
47 std::unique_ptr<ExtensionAPI> new_instance( 50 std::unique_ptr<ExtensionAPI> new_instance(
48 ExtensionAPI::CreateWithDefaultConfiguration()); 51 ExtensionAPI::CreateWithDefaultConfiguration());
49 EXPECT_NE(new_instance.get(), 52 EXPECT_NE(new_instance.get(),
50 std::unique_ptr<ExtensionAPI>( 53 std::unique_ptr<ExtensionAPI>(
51 ExtensionAPI::CreateWithDefaultConfiguration()) 54 ExtensionAPI::CreateWithDefaultConfiguration())
52 .get()); 55 .get());
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 { "parent3.noparent", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() }, 160 { "parent3.noparent", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() },
158 { "parent3.noparent", true, Feature::BLESSED_EXTENSION_CONTEXT, GURL() }, 161 { "parent3.noparent", true, Feature::BLESSED_EXTENSION_CONTEXT, GURL() },
159 { "parent3.noparent", true, Feature::UNBLESSED_EXTENSION_CONTEXT, GURL() }, 162 { "parent3.noparent", true, Feature::UNBLESSED_EXTENSION_CONTEXT, GURL() },
160 { "parent3.noparent.child", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() }, 163 { "parent3.noparent.child", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() },
161 { "parent3.noparent.child", true, Feature::BLESSED_EXTENSION_CONTEXT, 164 { "parent3.noparent.child", true, Feature::BLESSED_EXTENSION_CONTEXT,
162 GURL() }, 165 GURL() },
163 { "parent3.noparent.child", true, Feature::UNBLESSED_EXTENSION_CONTEXT, 166 { "parent3.noparent.child", true, Feature::UNBLESSED_EXTENSION_CONTEXT,
164 GURL() } 167 GURL() }
165 }; 168 };
166 169
167 base::FilePath api_features_path; 170 UnittestFeatureProvider api_feature_provider;
168 PathService::Get(chrome::DIR_TEST_DATA, &api_features_path);
169 api_features_path = api_features_path.AppendASCII("extensions")
170 .AppendASCII("extension_api_unittest")
171 .AppendASCII("api_features.json");
172
173 std::string api_features_str;
174 ASSERT_TRUE(base::ReadFileToString(
175 api_features_path, &api_features_str)) << "api_features.json";
176
177 std::unique_ptr<base::DictionaryValue> value(
178 static_cast<base::DictionaryValue*>(
179 base::JSONReader::Read(api_features_str).release()));
180 JSONFeatureProvider api_feature_provider(*value, CreateAPIFeature);
181 171
182 for (size_t i = 0; i < arraysize(test_data); ++i) { 172 for (size_t i = 0; i < arraysize(test_data); ++i) {
183 ExtensionAPI api; 173 ExtensionAPI api;
184 api.RegisterDependencyProvider("api", &api_feature_provider); 174 api.RegisterDependencyProvider("api", &api_feature_provider);
185 for (base::DictionaryValue::Iterator iter(*value); !iter.IsAtEnd(); 175 for (const auto& key : kTestFeatures)
186 iter.Advance()) { 176 api.RegisterSchemaResource(key, 0);
187 if (iter.key().find(".") == std::string::npos) 177 ExtensionAPI::OverrideSharedInstanceForTest scope(&api);
188 api.RegisterSchemaResource(iter.key(), 0);
189 }
190 178
191 ExtensionAPI::OverrideSharedInstanceForTest scope(&api);
192 bool expected = test_data[i].expect_is_available; 179 bool expected = test_data[i].expect_is_available;
193 Feature::Availability availability = 180 Feature::Availability availability =
194 api.IsAvailable(test_data[i].api_full_name, 181 api.IsAvailable(test_data[i].api_full_name,
195 NULL, 182 NULL,
196 test_data[i].context, 183 test_data[i].context,
197 test_data[i].url); 184 test_data[i].url);
198 EXPECT_EQ(expected, availability.is_available()) 185 EXPECT_EQ(expected, availability.is_available())
199 << base::StringPrintf("Test %d: Feature '%s' was %s: %s", 186 << base::StringPrintf("Test %d: Feature '%s' was %s: %s",
200 static_cast<int>(i), 187 static_cast<int>(i),
201 test_data[i].api_full_name.c_str(), 188 test_data[i].api_full_name.c_str(),
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 GURL("http://google.com") }, 238 GURL("http://google.com") },
252 { "test3", true, Feature::CONTENT_SCRIPT_CONTEXT, NULL, GURL() }, 239 { "test3", true, Feature::CONTENT_SCRIPT_CONTEXT, NULL, GURL() },
253 { "test3", true, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://foo.com") }, 240 { "test3", true, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://foo.com") },
254 { "test4.foo", true, Feature::CONTENT_SCRIPT_CONTEXT, NULL, GURL() }, 241 { "test4.foo", true, Feature::CONTENT_SCRIPT_CONTEXT, NULL, GURL() },
255 { "test7", false, Feature::WEB_PAGE_CONTEXT, NULL, 242 { "test7", false, Feature::WEB_PAGE_CONTEXT, NULL,
256 GURL("http://google.com") }, 243 GURL("http://google.com") },
257 { "test7", true, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://foo.com") }, 244 { "test7", true, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://foo.com") },
258 { "test7", false, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://bar.com") } 245 { "test7", false, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://bar.com") }
259 }; 246 };
260 247
261 base::FilePath api_features_path; 248 UnittestFeatureProvider api_feature_provider;
262 PathService::Get(chrome::DIR_TEST_DATA, &api_features_path);
263 api_features_path = api_features_path.AppendASCII("extensions")
264 .AppendASCII("extension_api_unittest")
265 .AppendASCII("api_features.json");
266
267 std::string api_features_str;
268 ASSERT_TRUE(base::ReadFileToString(
269 api_features_path, &api_features_str)) << "api_features.json";
270
271 std::unique_ptr<base::DictionaryValue> value(
272 static_cast<base::DictionaryValue*>(
273 base::JSONReader::Read(api_features_str).release()));
274 JSONFeatureProvider api_feature_provider(*value, CreateAPIFeature);
275 249
276 for (size_t i = 0; i < arraysize(test_data); ++i) { 250 for (size_t i = 0; i < arraysize(test_data); ++i) {
277 ExtensionAPI api; 251 ExtensionAPI api;
278 api.RegisterDependencyProvider("api", &api_feature_provider); 252 api.RegisterDependencyProvider("api", &api_feature_provider);
279 for (base::DictionaryValue::Iterator iter(*value); !iter.IsAtEnd(); 253 for (const auto& key : kTestFeatures)
280 iter.Advance()) { 254 api.RegisterSchemaResource(key, 0);
281 if (iter.key().find(".") == std::string::npos) 255 ExtensionAPI::OverrideSharedInstanceForTest scope(&api);
282 api.RegisterSchemaResource(iter.key(), 0);
283 }
284 256
285 Feature* test_feature = 257 Feature* test_feature =
286 api_feature_provider.GetFeature(test_data[i].api_full_name); 258 api_feature_provider.GetFeature(test_data[i].api_full_name);
287 ASSERT_TRUE(test_feature); 259 ASSERT_TRUE(test_feature);
288 EXPECT_EQ(test_data[i].expect_is_available, 260 EXPECT_EQ(test_data[i].expect_is_available,
289 api.IsAnyFeatureAvailableToContext(*test_feature, 261 api.IsAnyFeatureAvailableToContext(*test_feature,
290 test_data[i].extension, 262 test_data[i].extension,
291 test_data[i].context, 263 test_data[i].context,
292 test_data[i].url)) 264 test_data[i].url))
293 << i; 265 << i;
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after
669 EXPECT_TRUE(feature->whitelist().empty()); 641 EXPECT_TRUE(feature->whitelist().empty());
670 EXPECT_TRUE(feature->extension_types().empty()); 642 EXPECT_TRUE(feature->extension_types().empty());
671 643
672 EXPECT_EQ(SimpleFeature::UNSPECIFIED_LOCATION, feature->location()); 644 EXPECT_EQ(SimpleFeature::UNSPECIFIED_LOCATION, feature->location());
673 EXPECT_TRUE(feature->platforms().empty()); 645 EXPECT_TRUE(feature->platforms().empty());
674 EXPECT_EQ(0, feature->min_manifest_version()); 646 EXPECT_EQ(0, feature->min_manifest_version());
675 EXPECT_EQ(0, feature->max_manifest_version()); 647 EXPECT_EQ(0, feature->max_manifest_version());
676 } 648 }
677 } 649 }
678 650
679 TEST(ExtensionAPITest, JSONFeatureProviderDoesNotStoreInvalidFeatures) {
680 base::DictionaryValue features;
681 std::unique_ptr<base::DictionaryValue> feature_value(
682 new base::DictionaryValue());
683 // This feature is invalid (it needs an extension context), so the
684 // JSONFeatureProvider should not store it.
685 feature_value->SetString("channel", "stable");
686 features.Set("test", std::move(feature_value));
687 JSONFeatureProvider api_feature_provider(features, CreateAPIFeature);
688 EXPECT_FALSE(api_feature_provider.GetFeature("test"));
689 }
690
691 static void GetDictionaryFromList(const base::DictionaryValue* schema, 651 static void GetDictionaryFromList(const base::DictionaryValue* schema,
692 const std::string& list_name, 652 const std::string& list_name,
693 const int list_index, 653 const int list_index,
694 const base::DictionaryValue** out) { 654 const base::DictionaryValue** out) {
695 const base::ListValue* list; 655 const base::ListValue* list;
696 EXPECT_TRUE(schema->GetList(list_name, &list)); 656 EXPECT_TRUE(schema->GetList(list_name, &list));
697 EXPECT_TRUE(list->GetDictionary(list_index, out)); 657 EXPECT_TRUE(list->GetDictionary(list_index, out));
698 } 658 }
699 659
700 TEST(ExtensionAPITest, TypesHaveNamespace) { 660 TEST(ExtensionAPITest, TypesHaveNamespace) {
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
835 extension.get(), 795 extension.get(),
836 Feature::BLESSED_EXTENSION_CONTEXT, 796 Feature::BLESSED_EXTENSION_CONTEXT,
837 GURL()).is_available()); 797 GURL()).is_available());
838 EXPECT_FALSE(extension_api->IsAvailable("pageAction", 798 EXPECT_FALSE(extension_api->IsAvailable("pageAction",
839 extension.get(), 799 extension.get(),
840 Feature::BLESSED_EXTENSION_CONTEXT, 800 Feature::BLESSED_EXTENSION_CONTEXT,
841 GURL()).is_available()); 801 GURL()).is_available());
842 } 802 }
843 803
844 } // namespace extensions 804 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/common/extensions/BUILD.gn ('k') | chrome/common/extensions/chrome_extensions_client.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698