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

Unified Diff: chrome/common/extensions/api/extension_api_unittest.cc

Issue 9950046: Implement FeatureProvider for ExtensionAPI. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: chrome/common/extensions/api/extension_api_unittest.cc
diff --git a/chrome/common/extensions/api/extension_api_unittest.cc b/chrome/common/extensions/api/extension_api_unittest.cc
index bf240e1c4685b43e65c22eb65fffb64776dc3f44..f48ab925948b5c499961f9c39ebd641fd74a766d 100644
--- a/chrome/common/extensions/api/extension_api_unittest.cc
+++ b/chrome/common/extensions/api/extension_api_unittest.cc
@@ -7,6 +7,7 @@
#include <string>
#include "base/file_path.h"
+#include "base/json/json_writer.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
@@ -16,54 +17,106 @@
namespace extensions {
namespace {
+TEST(ExtensionAPI, Creation) {
+ ExtensionAPI* shared_instance = ExtensionAPI::GetSharedInstance();
+ EXPECT_EQ(shared_instance, ExtensionAPI::GetSharedInstance());
+
+ scoped_ptr<ExtensionAPI> new_instance(
+ ExtensionAPI::CreateWithDefaultConfiguration());
+ EXPECT_NE(new_instance.get(),
+ scoped_ptr<ExtensionAPI>(
+ ExtensionAPI::CreateWithDefaultConfiguration()).get());
+
+ ExtensionAPI empty_instance;
+
+ struct {
+ ExtensionAPI* api;
+ bool expect_populated;
+ } test_data[] = {
+ { shared_instance, true },
+ { new_instance.get(), true },
+ { &empty_instance, false }
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
+ EXPECT_EQ(test_data[i].expect_populated,
+ test_data[i].api->GetSchema("bookmarks.create") != NULL);
+ }
+}
+
+TEST(ExtensionAPI, SplitDependencyName) {
+ struct {
+ std::string input;
+ std::string expected_feature_type;
+ std::string expected_feature_name;
+ } test_data[] = {
+ { "", "api", "" }, // assumes "api" when no type is present
+ { "foo", "api", "foo" },
+ { "foo:", "foo", "" },
+ { ":foo", "", "foo" },
+ { "foo:bar", "foo", "bar" },
+ { "foo:bar.baz", "foo", "bar.baz" }
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
+ std::string feature_type;
+ std::string feature_name;
+ ExtensionAPI::SplitDependencyName(test_data[i].input, &feature_type,
+ &feature_name);
+ EXPECT_EQ(test_data[i].expected_feature_type, feature_type) << i;
+ EXPECT_EQ(test_data[i].expected_feature_name, feature_name) << i;
+ }
+}
+
TEST(ExtensionAPI, IsPrivileged) {
- ExtensionAPI extension_api;
+ scoped_ptr<ExtensionAPI> extension_api(
+ ExtensionAPI::CreateWithDefaultConfiguration());
- EXPECT_FALSE(extension_api.IsPrivileged("extension.connect"));
- EXPECT_FALSE(extension_api.IsPrivileged("extension.onConnect"));
+ EXPECT_FALSE(extension_api->IsPrivileged("extension.connect"));
+ EXPECT_FALSE(extension_api->IsPrivileged("extension.onConnect"));
// Properties are not supported yet.
- EXPECT_TRUE(extension_api.IsPrivileged("extension.lastError"));
+ EXPECT_TRUE(extension_api->IsPrivileged("extension.lastError"));
// Default unknown names to privileged for paranoia's sake.
- EXPECT_TRUE(extension_api.IsPrivileged(""));
- EXPECT_TRUE(extension_api.IsPrivileged("<unknown-namespace>"));
- EXPECT_TRUE(extension_api.IsPrivileged("extension.<unknown-member>"));
+ EXPECT_TRUE(extension_api->IsPrivileged(""));
+ EXPECT_TRUE(extension_api->IsPrivileged("<unknown-namespace>"));
+ EXPECT_TRUE(extension_api->IsPrivileged("extension.<unknown-member>"));
// Exists, but privileged.
- EXPECT_TRUE(extension_api.IsPrivileged("extension.getViews"));
- EXPECT_TRUE(extension_api.IsPrivileged("history.search"));
+ EXPECT_TRUE(extension_api->IsPrivileged("extension.getViews"));
+ EXPECT_TRUE(extension_api->IsPrivileged("history.search"));
// Whole APIs that are unprivileged.
- EXPECT_FALSE(extension_api.IsPrivileged("app.getDetails"));
- EXPECT_FALSE(extension_api.IsPrivileged("app.isInstalled"));
- EXPECT_FALSE(extension_api.IsPrivileged("storage.local"));
- EXPECT_FALSE(extension_api.IsPrivileged("storage.local.onChanged"));
- EXPECT_FALSE(extension_api.IsPrivileged("storage.local.set"));
- EXPECT_FALSE(extension_api.IsPrivileged("storage.local.MAX_ITEMS"));
- EXPECT_FALSE(extension_api.IsPrivileged("storage.set"));
+ EXPECT_FALSE(extension_api->IsPrivileged("app.getDetails"));
+ EXPECT_FALSE(extension_api->IsPrivileged("app.isInstalled"));
+ EXPECT_FALSE(extension_api->IsPrivileged("storage.local"));
+ EXPECT_FALSE(extension_api->IsPrivileged("storage.local.onChanged"));
+ EXPECT_FALSE(extension_api->IsPrivileged("storage.local.set"));
+ EXPECT_FALSE(extension_api->IsPrivileged("storage.local.MAX_ITEMS"));
+ EXPECT_FALSE(extension_api->IsPrivileged("storage.set"));
}
TEST(ExtensionAPI, LazyGetSchema) {
- ExtensionAPI apis;
-
- EXPECT_EQ(NULL, apis.GetSchema(""));
- EXPECT_EQ(NULL, apis.GetSchema(""));
- EXPECT_EQ(NULL, apis.GetSchema("experimental"));
- EXPECT_EQ(NULL, apis.GetSchema("experimental"));
- EXPECT_EQ(NULL, apis.GetSchema("foo"));
- EXPECT_EQ(NULL, apis.GetSchema("foo"));
-
- EXPECT_TRUE(apis.GetSchema("experimental.dns"));
- EXPECT_TRUE(apis.GetSchema("experimental.dns"));
- EXPECT_TRUE(apis.GetSchema("experimental.infobars"));
- EXPECT_TRUE(apis.GetSchema("experimental.infobars"));
- EXPECT_TRUE(apis.GetSchema("extension"));
- EXPECT_TRUE(apis.GetSchema("extension"));
- EXPECT_TRUE(apis.GetSchema("omnibox"));
- EXPECT_TRUE(apis.GetSchema("omnibox"));
- EXPECT_TRUE(apis.GetSchema("storage"));
- EXPECT_TRUE(apis.GetSchema("storage"));
+ scoped_ptr<ExtensionAPI> apis(ExtensionAPI::CreateWithDefaultConfiguration());
+
+ EXPECT_EQ(NULL, apis->GetSchema(""));
+ EXPECT_EQ(NULL, apis->GetSchema(""));
+ EXPECT_EQ(NULL, apis->GetSchema("experimental"));
+ EXPECT_EQ(NULL, apis->GetSchema("experimental"));
+ EXPECT_EQ(NULL, apis->GetSchema("foo"));
+ EXPECT_EQ(NULL, apis->GetSchema("foo"));
+
+ EXPECT_TRUE(apis->GetSchema("experimental.dns"));
+ EXPECT_TRUE(apis->GetSchema("experimental.dns"));
+ EXPECT_TRUE(apis->GetSchema("experimental.infobars"));
+ EXPECT_TRUE(apis->GetSchema("experimental.infobars"));
+ EXPECT_TRUE(apis->GetSchema("extension"));
+ EXPECT_TRUE(apis->GetSchema("extension"));
+ EXPECT_TRUE(apis->GetSchema("omnibox"));
+ EXPECT_TRUE(apis->GetSchema("omnibox"));
+ EXPECT_TRUE(apis->GetSchema("storage"));
+ EXPECT_TRUE(apis->GetSchema("storage"));
}
scoped_refptr<Extension> CreateExtensionWithPermissions(
@@ -106,18 +159,19 @@ TEST(ExtensionAPI, ExtensionWithUnprivilegedAPIs) {
extension = CreateExtensionWithPermissions(permissions);
}
- ExtensionAPI extension_api;
+ scoped_ptr<ExtensionAPI> extension_api(
+ ExtensionAPI::CreateWithDefaultConfiguration());
scoped_ptr<std::set<std::string> > privileged_apis =
- extension_api.GetAPIsForContext(
+ extension_api->GetAPIsForContext(
Feature::BLESSED_EXTENSION_CONTEXT, extension.get(), GURL());
scoped_ptr<std::set<std::string> > unprivileged_apis =
- extension_api.GetAPIsForContext(
+ extension_api->GetAPIsForContext(
Feature::UNBLESSED_EXTENSION_CONTEXT, extension.get(), GURL());
scoped_ptr<std::set<std::string> > content_script_apis =
- extension_api.GetAPIsForContext(
+ extension_api->GetAPIsForContext(
Feature::CONTENT_SCRIPT_CONTEXT, extension.get(), GURL());
// "storage" is completely unprivileged.
@@ -142,9 +196,10 @@ TEST(ExtensionAPI, ExtensionWithDependencies) {
{
scoped_refptr<Extension> extension =
CreateExtensionWithPermission("ttsEngine");
- scoped_ptr<std::set<std::string> > apis =
- ExtensionAPI().GetAPIsForContext(
- Feature::BLESSED_EXTENSION_CONTEXT, extension.get(), GURL());
+ scoped_ptr<ExtensionAPI> api(
+ ExtensionAPI::CreateWithDefaultConfiguration());
+ scoped_ptr<std::set<std::string> > apis = api->GetAPIsForContext(
+ Feature::BLESSED_EXTENSION_CONTEXT, extension.get(), GURL());
EXPECT_EQ(1u, apis->count("ttsEngine"));
EXPECT_EQ(1u, apis->count("tts"));
}
@@ -154,9 +209,9 @@ TEST(ExtensionAPI, ExtensionWithDependencies) {
{
scoped_refptr<Extension> extension =
CreateExtensionWithPermission("tts");
- scoped_ptr<std::set<std::string> > apis =
- ExtensionAPI().GetAPIsForContext(
- Feature::BLESSED_EXTENSION_CONTEXT, extension.get(), GURL());
+ scoped_ptr<ExtensionAPI> api(ExtensionAPI::CreateWithDefaultConfiguration());
+ scoped_ptr<std::set<std::string> > apis = api->GetAPIsForContext(
+ Feature::BLESSED_EXTENSION_CONTEXT, extension.get(), GURL());
EXPECT_EQ(0u, apis->count("ttsEngine"));
EXPECT_EQ(1u, apis->count("tts"));
}
@@ -170,26 +225,123 @@ bool MatchesURL(
}
TEST(ExtensionAPI, URLMatching) {
- ExtensionAPI api;
+ scoped_ptr<ExtensionAPI> api(ExtensionAPI::CreateWithDefaultConfiguration());
// "app" API is available to all URLs that content scripts can be injected.
- EXPECT_TRUE(MatchesURL(&api, "app", "http://example.com/example.html"));
- EXPECT_TRUE(MatchesURL(&api, "app", "https://blah.net"));
- EXPECT_TRUE(MatchesURL(&api, "app", "file://somefile.html"));
+ EXPECT_TRUE(MatchesURL(api.get(), "app", "http://example.com/example.html"));
+ EXPECT_TRUE(MatchesURL(api.get(), "app", "https://blah.net"));
+ EXPECT_TRUE(MatchesURL(api.get(), "app", "file://somefile.html"));
// But not internal URLs (for chrome-extension:// the app API is injected by
// GetSchemasForExtension).
- EXPECT_FALSE(MatchesURL(&api, "app", "about:flags"));
- EXPECT_FALSE(MatchesURL(&api, "app", "chrome://flags"));
- EXPECT_FALSE(MatchesURL(&api, "app", "chrome-extension://fakeextension"));
+ EXPECT_FALSE(MatchesURL(api.get(), "app", "about:flags"));
+ EXPECT_FALSE(MatchesURL(api.get(), "app", "chrome://flags"));
+ EXPECT_FALSE(MatchesURL(api.get(), "app",
+ "chrome-extension://fakeextension"));
// "storage" API (for example) isn't available to any URLs.
- EXPECT_FALSE(MatchesURL(&api, "storage", "http://example.com/example.html"));
- EXPECT_FALSE(MatchesURL(&api, "storage", "https://blah.net"));
- EXPECT_FALSE(MatchesURL(&api, "storage", "file://somefile.html"));
- EXPECT_FALSE(MatchesURL(&api, "storage", "about:flags"));
- EXPECT_FALSE(MatchesURL(&api, "storage", "chrome://flags"));
- EXPECT_FALSE(MatchesURL(&api, "storage", "chrome-extension://fakeextension"));
+ EXPECT_FALSE(MatchesURL(api.get(), "storage",
+ "http://example.com/example.html"));
+ EXPECT_FALSE(MatchesURL(api.get(), "storage", "https://blah.net"));
+ EXPECT_FALSE(MatchesURL(api.get(), "storage", "file://somefile.html"));
+ EXPECT_FALSE(MatchesURL(api.get(), "storage", "about:flags"));
+ EXPECT_FALSE(MatchesURL(api.get(), "storage", "chrome://flags"));
+ EXPECT_FALSE(MatchesURL(api.get(), "storage",
+ "chrome-extension://fakeextension"));
+}
+
+TEST(ExtensionAPI, GetAPINameForFullName) {
+ struct {
+ std::string input;
+ std::string api_name;
+ std::string child_name;
+ } test_data[] = {
+ { "", "", "" },
+ { "unknown", "", "" },
+ { "bookmarks", "bookmarks", "" },
+ { "bookmarks.", "bookmarks", "" },
+ { ".bookmarks", "", "" },
+ { "bookmarks.create", "bookmarks", "create" },
+ { "bookmarks.create.", "bookmarks", "create." },
+ { "bookmarks.create.monkey", "bookmarks", "create.monkey" },
+ { "experimental.bookmarkManager", "experimental.bookmarkManager", "" },
+ { "experimental.bookmarkManager.copy", "experimental.bookmarkManager",
+ "copy" }
+ };
+
+ scoped_ptr<ExtensionAPI> api(ExtensionAPI::CreateWithDefaultConfiguration());
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
+ std::string child_name;
+ std::string api_name = api->GetAPINameForFullName(test_data[i].input,
+ &child_name);
+ EXPECT_EQ(test_data[i].api_name, api_name) << test_data[i].input;
+ EXPECT_EQ(test_data[i].child_name, child_name) << test_data[i].input;
+ }
+}
+
+TEST(ExtensionAPI, Features) {
koz (OOO until 15th September) 2012/04/02 06:39:48 Features -> TestFeatureProperties?
Aaron Boodman 2012/04/02 08:21:54 Done.
+ scoped_ptr<ExtensionAPI> api(ExtensionAPI::CreateWithDefaultConfiguration());
+
+ scoped_ptr<Feature> bookmarks(api->GetFeature("bookmarks"));
+ scoped_ptr<Feature> bookmarks_create(api->GetFeature("bookmarks.create"));
+
+ struct {
+ Feature* feature;
+ // TODO(aa): More stuff to test over time.
+ } test_data[] = {
+ { bookmarks.get() },
+ { bookmarks_create.get() }
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
+ Feature* feature = test_data[i].feature;
+ ASSERT_TRUE(feature) << i;
+
+ EXPECT_TRUE(feature->whitelist()->empty());
+ EXPECT_TRUE(feature->extension_types()->empty());
+
+ EXPECT_EQ(1u, feature->contexts()->size());
+ EXPECT_TRUE(feature->contexts()->count(
+ Feature::BLESSED_EXTENSION_CONTEXT));
+
+ EXPECT_EQ(Feature::UNSPECIFIED_LOCATION, feature->location());
+ EXPECT_EQ(Feature::UNSPECIFIED_PLATFORM, feature->platform());
+ EXPECT_EQ(0, feature->min_manifest_version());
+ EXPECT_EQ(0, feature->max_manifest_version());
+ }
+}
+
+TEST(ExtensionAPI, FeaturesRequireContexts) {
+ scoped_ptr<ListValue> schema1(new ListValue());
+ DictionaryValue* feature_definition = new DictionaryValue();
+ schema1->Append(feature_definition);
+ feature_definition->SetString("namespace", "test");
+ feature_definition->SetBoolean("uses_feature_system", true);
+
+ scoped_ptr<ListValue> schema2(schema1->DeepCopy());
+
+ ListValue* contexts = new ListValue();
+ contexts->Append(Value::CreateStringValue("content_script"));
+ feature_definition->Set("contexts", contexts);
+
+ struct {
+ ListValue* schema;
+ bool expect_success;
+ } test_data[] = {
+ { schema1.get(), true },
+ { schema2.get(), false }
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
+ std::string schema_source;
+ base::JSONWriter::Write(test_data[i].schema, &schema_source);
+
+ ExtensionAPI api;
+ api.RegisterSchema("test", base::StringPiece(schema_source));
+
+ scoped_ptr<Feature> feature(api.GetFeature("test"));
+ EXPECT_EQ(test_data[i].expect_success, feature.get() != NULL) << i;
+ }
}
} // namespace

Powered by Google App Engine
This is Rietveld 408576698