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

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

Issue 9460002: Convert app_bindings.js to the schema_generated_bindings.js infrastructure. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 10 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 8
9 #include "base/file_path.h" 9 #include "base/file_path.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/values.h" 12 #include "base/values.h"
13 #include "chrome/common/extensions/extension.h" 13 #include "chrome/common/extensions/extension.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 15
16 namespace {
17
16 using extensions::ExtensionAPI; 18 using extensions::ExtensionAPI;
17 19
20 bool GetSchemasForURLContains(
21 const std::string& api_name, const std::string& url) {
22 ExtensionAPI::SchemaMap schemas;
23 ExtensionAPI::GetInstance()->GetSchemasForURL(GURL(url), &schemas);
24 return schemas.find(api_name) != schemas.end();
25 }
26
27 bool MatchesURL(
28 const std::string& api_name, const std::string& url) {
29 return ExtensionAPI::GetInstance()->MatchesURL(api_name, GURL(url));
30 }
31
18 TEST(ExtensionAPI, IsPrivileged) { 32 TEST(ExtensionAPI, IsPrivileged) {
19 ExtensionAPI* extension_api = ExtensionAPI::GetInstance(); 33 ExtensionAPI* extension_api = ExtensionAPI::GetInstance();
20 EXPECT_FALSE(extension_api->IsPrivileged("extension.connect")); 34 EXPECT_FALSE(extension_api->IsPrivileged("extension.connect"));
21 EXPECT_FALSE(extension_api->IsPrivileged("extension.onConnect")); 35 EXPECT_FALSE(extension_api->IsPrivileged("extension.onConnect"));
22 36
23 // Properties are not supported yet. 37 // Properties are not supported yet.
24 EXPECT_TRUE(extension_api->IsPrivileged("extension.lastError")); 38 EXPECT_TRUE(extension_api->IsPrivileged("extension.lastError"));
25 39
26 // Default unknown names to privileged for paranoia's sake. 40 // Default unknown names to privileged for paranoia's sake.
27 EXPECT_TRUE(extension_api->IsPrivileged("")); 41 EXPECT_TRUE(extension_api->IsPrivileged(""));
28 EXPECT_TRUE(extension_api->IsPrivileged("<unknown-namespace>")); 42 EXPECT_TRUE(extension_api->IsPrivileged("<unknown-namespace>"));
29 EXPECT_TRUE(extension_api->IsPrivileged("extension.<unknown-member>")); 43 EXPECT_TRUE(extension_api->IsPrivileged("extension.<unknown-member>"));
30 44
31 // Exists, but privileged. 45 // Exists, but privileged.
32 EXPECT_TRUE(extension_api->IsPrivileged("extension.getViews")); 46 EXPECT_TRUE(extension_api->IsPrivileged("extension.getViews"));
33 EXPECT_TRUE(extension_api->IsPrivileged("history.search")); 47 EXPECT_TRUE(extension_api->IsPrivileged("history.search"));
34 48
35 // Whole APIs that are unprivileged. 49 // Whole APIs that are unprivileged.
50 EXPECT_FALSE(extension_api->IsPrivileged("app.getDetails"));
51 EXPECT_FALSE(extension_api->IsPrivileged("app.isInstalled"));
36 EXPECT_FALSE(extension_api->IsPrivileged("storage.local")); 52 EXPECT_FALSE(extension_api->IsPrivileged("storage.local"));
37 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.onChanged")); 53 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.onChanged"));
38 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.set")); 54 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.set"));
39 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.MAX_ITEMS")); 55 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.MAX_ITEMS"));
40 EXPECT_FALSE(extension_api->IsPrivileged("storage.set")); 56 EXPECT_FALSE(extension_api->IsPrivileged("storage.set"));
41 } 57 }
42 58
43 TEST(ExtensionAPI, IsWholeAPIPrivileged) { 59 TEST(ExtensionAPI, IsWholeAPIPrivileged) {
44 ExtensionAPI* extension_api = ExtensionAPI::GetInstance(); 60 ExtensionAPI* extension_api = ExtensionAPI::GetInstance();
45 61
(...skipping 28 matching lines...) Expand all
74 scoped_refptr<Extension> extension(Extension::Create( 90 scoped_refptr<Extension> extension(Extension::Create(
75 FilePath(), Extension::LOAD, manifest, Extension::NO_FLAGS, &error)); 91 FilePath(), Extension::LOAD, manifest, Extension::NO_FLAGS, &error));
76 CHECK(extension.get()); 92 CHECK(extension.get());
77 CHECK(error.empty()); 93 CHECK(error.empty());
78 94
79 ExtensionAPI::SchemaMap schemas; 95 ExtensionAPI::SchemaMap schemas;
80 ExtensionAPI::GetInstance()->GetSchemasForExtension( 96 ExtensionAPI::GetInstance()->GetSchemasForExtension(
81 *extension, ExtensionAPI::ALL, &schemas); 97 *extension, ExtensionAPI::ALL, &schemas);
82 EXPECT_EQ(1u, schemas.count("tts")); 98 EXPECT_EQ(1u, schemas.count("tts"));
83 } 99 }
100
101 TEST(ExtensionAPI, GetSchemasForURL) {
102 // "app" API is available to all URLs that content scripts can be injected.
103 EXPECT_TRUE(GetSchemasForURLContains("app",
104 "http://example.com/example.html"));
105 EXPECT_TRUE(GetSchemasForURLContains("app", "https://blah.net"));
106 EXPECT_TRUE(GetSchemasForURLContains("app", "file://somefile.html"));
107
108 // But not internal URLs (for chrome-extension:// the app API is injected by
109 // GetSchemasForExtension).
110 EXPECT_FALSE(GetSchemasForURLContains("app", "about:flags"));
111 EXPECT_FALSE(GetSchemasForURLContains("app", "chrome://flags"));
112 EXPECT_FALSE(GetSchemasForURLContains("app",
113 "chrome-extension://fakeextension"));
114
115 // "storage" API (for example) isn't available to any URLs.
116 EXPECT_FALSE(GetSchemasForURLContains("storage",
117 "http://example.com/example.html"));
118 EXPECT_FALSE(GetSchemasForURLContains("storage", "https://blah.net"));
119 EXPECT_FALSE(GetSchemasForURLContains("storage", "file://somefile.html"));
120 EXPECT_FALSE(GetSchemasForURLContains("storage", "about:flags"));
121 EXPECT_FALSE(GetSchemasForURLContains("storage", "chrome://flags"));
122 EXPECT_FALSE(GetSchemasForURLContains("storage",
123 "chrome-extension://fakeextension"));
koz (OOO until 15th September) 2012/02/27 03:04:11 nit: This seems redundant to have the same data pa
not at google - send to devlin 2012/02/27 04:44:24 Yeah, it's ok, you didn't need to soften that with
124 }
125
126 TEST(ExtensionAPI, MatchesURL) {
127 // "app" API is available to all URLs that content scripts can be injected.
128 EXPECT_TRUE(MatchesURL("app", "http://example.com/example.html"));
129 EXPECT_TRUE(MatchesURL("app", "https://blah.net"));
130 EXPECT_TRUE(MatchesURL("app", "file://somefile.html"));
131
132 // But not internal URLs (for chrome-extension:// the app API is injected by
133 // GetSchemasForExtension).
134 EXPECT_FALSE(MatchesURL("app", "about:flags"));
135 EXPECT_FALSE(MatchesURL("app", "chrome://flags"));
136 EXPECT_FALSE(MatchesURL("app", "chrome-extension://fakeextension"));
137
138 // "storage" API (for example) isn't available to any URLs.
139 EXPECT_FALSE(MatchesURL("storage", "http://example.com/example.html"));
140 EXPECT_FALSE(MatchesURL("storage", "https://blah.net"));
141 EXPECT_FALSE(MatchesURL("storage", "file://somefile.html"));
142 EXPECT_FALSE(MatchesURL("storage", "about:flags"));
143 EXPECT_FALSE(MatchesURL("storage", "chrome://flags"));
144 EXPECT_FALSE(MatchesURL("storage", "chrome-extension://fakeextension"));
145 }
146
147 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698