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

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

Issue 14494013: Allow API functions and events to have entries in _api_features.json (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixes Created 7 years, 7 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 | Annotate | Revision Log
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 "chrome/common/extensions/api/extension_api.h" 5 #include "chrome/common/extensions/api/extension_api.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 EXPECT_EQ(test_data[i].expected_feature_name, feature_name) << i; 85 EXPECT_EQ(test_data[i].expected_feature_name, feature_name) << i;
86 } 86 }
87 } 87 }
88 88
89 TEST_F(ExtensionAPITest, IsPrivileged) { 89 TEST_F(ExtensionAPITest, IsPrivileged) {
90 scoped_ptr<ExtensionAPI> extension_api( 90 scoped_ptr<ExtensionAPI> extension_api(
91 ExtensionAPI::CreateWithDefaultConfiguration()); 91 ExtensionAPI::CreateWithDefaultConfiguration());
92 92
93 EXPECT_FALSE(extension_api->IsPrivileged("runtime.connect")); 93 EXPECT_FALSE(extension_api->IsPrivileged("runtime.connect"));
94 EXPECT_FALSE(extension_api->IsPrivileged("runtime.onConnect")); 94 EXPECT_FALSE(extension_api->IsPrivileged("runtime.onConnect"));
95 95 EXPECT_FALSE(extension_api->IsPrivileged("runtime.lastError"));
96 // Properties are not supported yet.
97 EXPECT_TRUE(extension_api->IsPrivileged("runtime.lastError"));
98 96
99 // Default unknown names to privileged for paranoia's sake. 97 // Default unknown names to privileged for paranoia's sake.
100 EXPECT_TRUE(extension_api->IsPrivileged(std::string())); 98 EXPECT_TRUE(extension_api->IsPrivileged(std::string()));
101 EXPECT_TRUE(extension_api->IsPrivileged("<unknown-namespace>")); 99 EXPECT_TRUE(extension_api->IsPrivileged("<unknown-namespace>"));
102 EXPECT_TRUE(extension_api->IsPrivileged("extension.<unknown-member>")); 100 EXPECT_TRUE(extension_api->IsPrivileged("extension.<unknown-member>"));
103 101
104 // Exists, but privileged. 102 // Exists, but privileged.
105 EXPECT_TRUE(extension_api->IsPrivileged("extension.getViews")); 103 EXPECT_TRUE(extension_api->IsPrivileged("extension.getViews"));
106 EXPECT_TRUE(extension_api->IsPrivileged("history.search")); 104 EXPECT_TRUE(extension_api->IsPrivileged("history.search"));
107 105
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 } 216 }
219 217
220 EXPECT_EQ(test_data[i].expect_is_available, 218 EXPECT_EQ(test_data[i].expect_is_available,
221 api.IsAvailable(test_data[i].api_full_name, 219 api.IsAvailable(test_data[i].api_full_name,
222 NULL, 220 NULL,
223 test_data[i].context, 221 test_data[i].context,
224 test_data[i].url).is_available()) << i; 222 test_data[i].url).is_available()) << i;
225 } 223 }
226 } 224 }
227 225
226 TEST(ExtensionAPI, IsPartAvailableToContext) {
227 struct {
228 std::string api_full_name;
229 bool expect_is_available;
230 Feature::Context context;
231 GURL url;
232 } test_data[] = {
233 { "test1", false, Feature::WEB_PAGE_CONTEXT, GURL() },
234 { "test1", true, Feature::UNBLESSED_EXTENSION_CONTEXT, GURL() },
235 { "test2", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() },
236 { "test2", true, Feature::WEB_PAGE_CONTEXT, GURL("http://google.com") },
237 { "test2.foo", false, Feature::WEB_PAGE_CONTEXT,
238 GURL("http://google.com") },
239 { "test3", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() },
240 { "test3", true, Feature::WEB_PAGE_CONTEXT, GURL("http://foo.com") },
241 { "test4.foo", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() },
242 { "test7", false, Feature::WEB_PAGE_CONTEXT, GURL("http://google.com") },
243 { "test7", true, Feature::WEB_PAGE_CONTEXT, GURL("http://foo.com") },
244 { "test7", false, Feature::WEB_PAGE_CONTEXT, GURL("http://bar.com") }
245 };
246
247 base::FilePath api_features_path;
248 PathService::Get(chrome::DIR_TEST_DATA, &api_features_path);
249 api_features_path = api_features_path.AppendASCII("extensions")
250 .AppendASCII("extension_api_unittest")
251 .AppendASCII("api_features.json");
252
253 std::string api_features_str;
254 ASSERT_TRUE(file_util::ReadFileToString(
255 api_features_path, &api_features_str)) << "api_features.json";
256
257 scoped_ptr<base::DictionaryValue> value(static_cast<DictionaryValue*>(
258 base::JSONReader::Read(api_features_str)));
259 BaseFeatureProvider api_feature_provider(*value, CreateAPIFeature);
260
261 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
262 ExtensionAPI api;
263 api.RegisterDependencyProvider("api", &api_feature_provider);
264 for (base::DictionaryValue::Iterator iter(*value); !iter.IsAtEnd();
265 iter.Advance()) {
266 if (iter.key().find(".") == std::string::npos)
267 api.RegisterSchema(iter.key(), "");
268 }
269
270 EXPECT_EQ(test_data[i].expect_is_available,
271 api.IsPartAvailableToContext(test_data[i].api_full_name,
272 test_data[i].context,
273 test_data[i].url)) << i;
274 }
275 }
276
228 TEST_F(ExtensionAPITest, LazyGetSchema) { 277 TEST_F(ExtensionAPITest, LazyGetSchema) {
229 scoped_ptr<ExtensionAPI> apis(ExtensionAPI::CreateWithDefaultConfiguration()); 278 scoped_ptr<ExtensionAPI> apis(ExtensionAPI::CreateWithDefaultConfiguration());
230 279
231 EXPECT_EQ(NULL, apis->GetSchema(std::string())); 280 EXPECT_EQ(NULL, apis->GetSchema(std::string()));
232 EXPECT_EQ(NULL, apis->GetSchema(std::string())); 281 EXPECT_EQ(NULL, apis->GetSchema(std::string()));
233 EXPECT_EQ(NULL, apis->GetSchema("experimental")); 282 EXPECT_EQ(NULL, apis->GetSchema("experimental"));
234 EXPECT_EQ(NULL, apis->GetSchema("experimental")); 283 EXPECT_EQ(NULL, apis->GetSchema("experimental"));
235 EXPECT_EQ(NULL, apis->GetSchema("foo")); 284 EXPECT_EQ(NULL, apis->GetSchema("foo"));
236 EXPECT_EQ(NULL, apis->GetSchema("foo")); 285 EXPECT_EQ(NULL, apis->GetSchema("foo"));
237 286
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 GetDictionaryFromList(dict, "parameters", 0, &sub_dict); 606 GetDictionaryFromList(dict, "parameters", 0, &sub_dict);
558 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); 607 EXPECT_TRUE(sub_dict->GetString("$ref", &type));
559 EXPECT_EQ("test.foo.TestType", type); 608 EXPECT_EQ("test.foo.TestType", type);
560 GetDictionaryFromList(dict, "parameters", 1, &sub_dict); 609 GetDictionaryFromList(dict, "parameters", 1, &sub_dict);
561 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); 610 EXPECT_TRUE(sub_dict->GetString("$ref", &type));
562 EXPECT_EQ("fully.qualified.Type", type); 611 EXPECT_EQ("fully.qualified.Type", type);
563 } 612 }
564 613
565 } // namespace 614 } // namespace
566 } // namespace extensions 615 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698