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

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: lazyboy@'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
37 using test_util::BuildExtension; 34 using test_util::BuildExtension;
38 35
39 SimpleFeature* CreateAPIFeature() {
40 return new APIFeature();
41 }
42
43 TEST(ExtensionAPITest, Creation) { 36 TEST(ExtensionAPITest, Creation) {
44 ExtensionAPI* shared_instance = ExtensionAPI::GetSharedInstance(); 37 ExtensionAPI* shared_instance = ExtensionAPI::GetSharedInstance();
45 EXPECT_EQ(shared_instance, ExtensionAPI::GetSharedInstance()); 38 EXPECT_EQ(shared_instance, ExtensionAPI::GetSharedInstance());
46 39
47 std::unique_ptr<ExtensionAPI> new_instance( 40 std::unique_ptr<ExtensionAPI> new_instance(
48 ExtensionAPI::CreateWithDefaultConfiguration()); 41 ExtensionAPI::CreateWithDefaultConfiguration());
49 EXPECT_NE(new_instance.get(), 42 EXPECT_NE(new_instance.get(),
50 std::unique_ptr<ExtensionAPI>( 43 std::unique_ptr<ExtensionAPI>(
51 ExtensionAPI::CreateWithDefaultConfiguration()) 44 ExtensionAPI::CreateWithDefaultConfiguration())
52 .get()); 45 .get());
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 { "parent3.noparent", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() }, 150 { "parent3.noparent", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() },
158 { "parent3.noparent", true, Feature::BLESSED_EXTENSION_CONTEXT, GURL() }, 151 { "parent3.noparent", true, Feature::BLESSED_EXTENSION_CONTEXT, GURL() },
159 { "parent3.noparent", true, Feature::UNBLESSED_EXTENSION_CONTEXT, GURL() }, 152 { "parent3.noparent", true, Feature::UNBLESSED_EXTENSION_CONTEXT, GURL() },
160 { "parent3.noparent.child", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() }, 153 { "parent3.noparent.child", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() },
161 { "parent3.noparent.child", true, Feature::BLESSED_EXTENSION_CONTEXT, 154 { "parent3.noparent.child", true, Feature::BLESSED_EXTENSION_CONTEXT,
162 GURL() }, 155 GURL() },
163 { "parent3.noparent.child", true, Feature::UNBLESSED_EXTENSION_CONTEXT, 156 { "parent3.noparent.child", true, Feature::UNBLESSED_EXTENSION_CONTEXT,
164 GURL() } 157 GURL() }
165 }; 158 };
166 159
167 base::FilePath api_features_path; 160 const char* test_features[] = {
Lei Zhang 2016/08/16 19:41:57 const char* const kTestFeatures[]
Devlin 2016/08/17 01:10:35 Done.
168 PathService::Get(chrome::DIR_TEST_DATA, &api_features_path); 161 "test1", "test2", "test3", "test4", "test5",
169 api_features_path = api_features_path.AppendASCII("extensions") 162 "test6", "test7", "parent1", "parent2", "parent3",
170 .AppendASCII("extension_api_unittest") 163 };
171 .AppendASCII("api_features.json");
172 164
173 std::string api_features_str; 165 UnittestFeatureProvider api_feature_provider;
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 166
182 for (size_t i = 0; i < arraysize(test_data); ++i) { 167 for (size_t i = 0; i < arraysize(test_data); ++i) {
183 ExtensionAPI api; 168 ExtensionAPI api;
184 api.RegisterDependencyProvider("api", &api_feature_provider); 169 api.RegisterDependencyProvider("api", &api_feature_provider);
185 for (base::DictionaryValue::Iterator iter(*value); !iter.IsAtEnd(); 170 for (const auto& key : test_features)
186 iter.Advance()) { 171 api.RegisterSchemaResource(key, 0);
187 if (iter.key().find(".") == std::string::npos) 172 ExtensionAPI::OverrideSharedInstanceForTest scope(&api);
188 api.RegisterSchemaResource(iter.key(), 0);
189 }
190 173
191 ExtensionAPI::OverrideSharedInstanceForTest scope(&api);
192 bool expected = test_data[i].expect_is_available; 174 bool expected = test_data[i].expect_is_available;
193 Feature::Availability availability = 175 Feature::Availability availability =
194 api.IsAvailable(test_data[i].api_full_name, 176 api.IsAvailable(test_data[i].api_full_name,
195 NULL, 177 NULL,
196 test_data[i].context, 178 test_data[i].context,
197 test_data[i].url); 179 test_data[i].url);
198 EXPECT_EQ(expected, availability.is_available()) 180 EXPECT_EQ(expected, availability.is_available())
199 << base::StringPrintf("Test %d: Feature '%s' was %s: %s", 181 << base::StringPrintf("Test %d: Feature '%s' was %s: %s",
200 static_cast<int>(i), 182 static_cast<int>(i),
201 test_data[i].api_full_name.c_str(), 183 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") }, 233 GURL("http://google.com") },
252 { "test3", true, Feature::CONTENT_SCRIPT_CONTEXT, NULL, GURL() }, 234 { "test3", true, Feature::CONTENT_SCRIPT_CONTEXT, NULL, GURL() },
253 { "test3", true, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://foo.com") }, 235 { "test3", true, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://foo.com") },
254 { "test4.foo", true, Feature::CONTENT_SCRIPT_CONTEXT, NULL, GURL() }, 236 { "test4.foo", true, Feature::CONTENT_SCRIPT_CONTEXT, NULL, GURL() },
255 { "test7", false, Feature::WEB_PAGE_CONTEXT, NULL, 237 { "test7", false, Feature::WEB_PAGE_CONTEXT, NULL,
256 GURL("http://google.com") }, 238 GURL("http://google.com") },
257 { "test7", true, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://foo.com") }, 239 { "test7", true, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://foo.com") },
258 { "test7", false, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://bar.com") } 240 { "test7", false, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://bar.com") }
259 }; 241 };
260 242
261 base::FilePath api_features_path; 243 const char* test_features[] = {
Lei Zhang 2016/08/16 19:41:57 Ditto, or just declare this once in an anonymous n
Devlin 2016/08/17 01:10:35 Done.
262 PathService::Get(chrome::DIR_TEST_DATA, &api_features_path); 244 "test1", "test2", "test3", "test4", "test5",
263 api_features_path = api_features_path.AppendASCII("extensions") 245 "test6", "test7", "parent1", "parent2", "parent3",
264 .AppendASCII("extension_api_unittest") 246 };
265 .AppendASCII("api_features.json");
266 247
267 std::string api_features_str; 248 UnittestFeatureProvider api_feature_provider;
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 : test_features)
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