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

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

Issue 2255613003: Introduce session type parameter to extension features (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Introduce session type parameter to extension features 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
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" 16 #include "base/json/json_reader.h"
17 #include "base/json/json_writer.h" 17 #include "base/json/json_writer.h"
18 #include "base/macros.h" 18 #include "base/macros.h"
19 #include "base/memory/ref_counted.h" 19 #include "base/memory/ref_counted.h"
20 #include "base/path_service.h" 20 #include "base/path_service.h"
21 #include "base/strings/stringprintf.h" 21 #include "base/strings/stringprintf.h"
22 #include "base/values.h" 22 #include "base/values.h"
23 #include "chrome/common/chrome_paths.h" 23 #include "chrome/common/chrome_paths.h"
24 #include "extensions/common/extension.h" 24 #include "extensions/common/extension.h"
25 #include "extensions/common/extension_builder.h" 25 #include "extensions/common/extension_builder.h"
26 #include "extensions/common/features/api_feature.h" 26 #include "extensions/common/features/api_feature.h"
27 #include "extensions/common/features/feature_session_type.h"
27 #include "extensions/common/features/json_feature_provider.h" 28 #include "extensions/common/features/json_feature_provider.h"
28 #include "extensions/common/features/simple_feature.h" 29 #include "extensions/common/features/simple_feature.h"
29 #include "extensions/common/manifest.h" 30 #include "extensions/common/manifest.h"
30 #include "extensions/common/manifest_constants.h" 31 #include "extensions/common/manifest_constants.h"
31 #include "extensions/common/test_util.h" 32 #include "extensions/common/test_util.h"
32 #include "extensions/common/value_builder.h" 33 #include "extensions/common/value_builder.h"
33 #include "testing/gtest/include/gtest/gtest.h" 34 #include "testing/gtest/include/gtest/gtest.h"
34 35
35 namespace extensions { 36 namespace extensions {
36 37
37 using test_util::BuildExtension; 38 using test_util::BuildExtension;
38 39
40 namespace {
41
39 SimpleFeature* CreateAPIFeature() { 42 SimpleFeature* CreateAPIFeature() {
40 return new APIFeature(); 43 return new APIFeature();
41 } 44 }
42 45
46 std::unique_ptr<base::DictionaryValue> GetJSONFeatures() {
47 base::FilePath api_features_path;
48 PathService::Get(chrome::DIR_TEST_DATA, &api_features_path);
49 api_features_path = api_features_path.AppendASCII("extensions")
50 .AppendASCII("extension_api_unittest")
51 .AppendASCII("api_features.json");
52
53 std::string api_features_str;
54 if (!base::ReadFileToString(api_features_path, &api_features_str)) {
55 ADD_FAILURE() << "api_features.json unreadable";
56 return std::unique_ptr<base::DictionaryValue>();
57 }
58
59 return std::unique_ptr<base::DictionaryValue>(
60 static_cast<base::DictionaryValue*>(
61 base::JSONReader::Read(api_features_str).release()));
62 }
63
64 void RegisterAPISchemaResourcesForTest(ExtensionAPI* api,
65 const base::DictionaryValue& features) {
66 for (base::DictionaryValue::Iterator iter(features); !iter.IsAtEnd();
67 iter.Advance()) {
68 if (iter.key().find(".") == std::string::npos)
69 api->RegisterSchemaResource(iter.key(), 0);
70 }
71 }
72
73 } // namespace
74
43 TEST(ExtensionAPITest, Creation) { 75 TEST(ExtensionAPITest, Creation) {
44 ExtensionAPI* shared_instance = ExtensionAPI::GetSharedInstance(); 76 ExtensionAPI* shared_instance = ExtensionAPI::GetSharedInstance();
45 EXPECT_EQ(shared_instance, ExtensionAPI::GetSharedInstance()); 77 EXPECT_EQ(shared_instance, ExtensionAPI::GetSharedInstance());
46 78
47 std::unique_ptr<ExtensionAPI> new_instance( 79 std::unique_ptr<ExtensionAPI> new_instance(
48 ExtensionAPI::CreateWithDefaultConfiguration()); 80 ExtensionAPI::CreateWithDefaultConfiguration());
49 EXPECT_NE(new_instance.get(), 81 EXPECT_NE(new_instance.get(),
50 std::unique_ptr<ExtensionAPI>( 82 std::unique_ptr<ExtensionAPI>(
51 ExtensionAPI::CreateWithDefaultConfiguration()) 83 ExtensionAPI::CreateWithDefaultConfiguration())
52 .get()); 84 .get());
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 { "parent3.noparent", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() }, 189 { "parent3.noparent", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() },
158 { "parent3.noparent", true, Feature::BLESSED_EXTENSION_CONTEXT, GURL() }, 190 { "parent3.noparent", true, Feature::BLESSED_EXTENSION_CONTEXT, GURL() },
159 { "parent3.noparent", true, Feature::UNBLESSED_EXTENSION_CONTEXT, GURL() }, 191 { "parent3.noparent", true, Feature::UNBLESSED_EXTENSION_CONTEXT, GURL() },
160 { "parent3.noparent.child", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() }, 192 { "parent3.noparent.child", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() },
161 { "parent3.noparent.child", true, Feature::BLESSED_EXTENSION_CONTEXT, 193 { "parent3.noparent.child", true, Feature::BLESSED_EXTENSION_CONTEXT,
162 GURL() }, 194 GURL() },
163 { "parent3.noparent.child", true, Feature::UNBLESSED_EXTENSION_CONTEXT, 195 { "parent3.noparent.child", true, Feature::UNBLESSED_EXTENSION_CONTEXT,
164 GURL() } 196 GURL() }
165 }; 197 };
166 198
167 base::FilePath api_features_path; 199 std::unique_ptr<base::DictionaryValue> value(GetJSONFeatures());
168 PathService::Get(chrome::DIR_TEST_DATA, &api_features_path); 200 ASSERT_TRUE(value);
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); 201 JSONFeatureProvider api_feature_provider(*value, CreateAPIFeature);
181 202
182 for (size_t i = 0; i < arraysize(test_data); ++i) { 203 for (size_t i = 0; i < arraysize(test_data); ++i) {
183 ExtensionAPI api; 204 ExtensionAPI api;
184 api.RegisterDependencyProvider("api", &api_feature_provider); 205 api.RegisterDependencyProvider("api", &api_feature_provider);
185 for (base::DictionaryValue::Iterator iter(*value); !iter.IsAtEnd(); 206 RegisterAPISchemaResourcesForTest(&api, *value);
186 iter.Advance()) { 207 ExtensionAPI::OverrideSharedInstanceForTest scope(&api);
187 if (iter.key().find(".") == std::string::npos)
188 api.RegisterSchemaResource(iter.key(), 0);
189 }
190 208
191 ExtensionAPI::OverrideSharedInstanceForTest scope(&api);
192 bool expected = test_data[i].expect_is_available; 209 bool expected = test_data[i].expect_is_available;
193 Feature::Availability availability = 210 Feature::Availability availability =
194 api.IsAvailable(test_data[i].api_full_name, 211 api.IsAvailable(test_data[i].api_full_name,
195 NULL, 212 NULL,
196 test_data[i].context, 213 test_data[i].context,
197 test_data[i].url); 214 test_data[i].url);
198 EXPECT_EQ(expected, availability.is_available()) 215 EXPECT_EQ(expected, availability.is_available())
199 << base::StringPrintf("Test %d: Feature '%s' was %s: %s", 216 << base::StringPrintf("Test %d: Feature '%s' was %s: %s",
200 static_cast<int>(i), 217 static_cast<int>(i),
201 test_data[i].api_full_name.c_str(), 218 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") }, 268 GURL("http://google.com") },
252 { "test3", true, Feature::CONTENT_SCRIPT_CONTEXT, NULL, GURL() }, 269 { "test3", true, Feature::CONTENT_SCRIPT_CONTEXT, NULL, GURL() },
253 { "test3", true, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://foo.com") }, 270 { "test3", true, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://foo.com") },
254 { "test4.foo", true, Feature::CONTENT_SCRIPT_CONTEXT, NULL, GURL() }, 271 { "test4.foo", true, Feature::CONTENT_SCRIPT_CONTEXT, NULL, GURL() },
255 { "test7", false, Feature::WEB_PAGE_CONTEXT, NULL, 272 { "test7", false, Feature::WEB_PAGE_CONTEXT, NULL,
256 GURL("http://google.com") }, 273 GURL("http://google.com") },
257 { "test7", true, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://foo.com") }, 274 { "test7", true, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://foo.com") },
258 { "test7", false, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://bar.com") } 275 { "test7", false, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://bar.com") }
259 }; 276 };
260 277
261 base::FilePath api_features_path; 278 std::unique_ptr<base::DictionaryValue> value(GetJSONFeatures());
262 PathService::Get(chrome::DIR_TEST_DATA, &api_features_path); 279 ASSERT_TRUE(value);
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); 280 JSONFeatureProvider api_feature_provider(*value, CreateAPIFeature);
275 281
276 for (size_t i = 0; i < arraysize(test_data); ++i) { 282 for (size_t i = 0; i < arraysize(test_data); ++i) {
277 ExtensionAPI api; 283 ExtensionAPI api;
278 api.RegisterDependencyProvider("api", &api_feature_provider); 284 api.RegisterDependencyProvider("api", &api_feature_provider);
279 for (base::DictionaryValue::Iterator iter(*value); !iter.IsAtEnd(); 285 RegisterAPISchemaResourcesForTest(&api, *value);
280 iter.Advance()) { 286 ExtensionAPI::OverrideSharedInstanceForTest scope(&api);
281 if (iter.key().find(".") == std::string::npos)
282 api.RegisterSchemaResource(iter.key(), 0);
283 }
284 287
285 Feature* test_feature = 288 Feature* test_feature =
286 api_feature_provider.GetFeature(test_data[i].api_full_name); 289 api_feature_provider.GetFeature(test_data[i].api_full_name);
287 ASSERT_TRUE(test_feature); 290 ASSERT_TRUE(test_feature);
288 EXPECT_EQ(test_data[i].expect_is_available, 291 EXPECT_EQ(test_data[i].expect_is_available,
289 api.IsAnyFeatureAvailableToContext(*test_feature, 292 api.IsAnyFeatureAvailableToContext(*test_feature,
290 test_data[i].extension, 293 test_data[i].extension,
291 test_data[i].context, 294 test_data[i].context,
292 test_data[i].url)) 295 test_data[i].url))
293 << i; 296 << i;
294 } 297 }
295 } 298 }
296 299
300 TEST(ExtensionAPITest, SessionTypeFeature) {
301 struct {
xiyuan 2016/08/18 21:24:00 nit: const
tbarzic 2016/08/18 22:18:56 Done
302 std::string api_name;
303 bool expect_available;
304 FeatureSessionType current_session_type;
305 } test_data[]{{"kiosk_only", true, FeatureSessionType::KIOSK},
xiyuan 2016/08/18 21:24:00 nit: } kTestData[] = {...
tbarzic 2016/08/18 22:18:55 Done.
306 {"kiosk_only", false, FeatureSessionType::REGULAR},
307 {"kiosk_only", false, FeatureSessionType::UNKNOWN},
308 {"non_kiosk", false, FeatureSessionType::KIOSK},
309 {"non_kiosk", true, FeatureSessionType::REGULAR},
310 {"non_kiosk", false, FeatureSessionType::UNKNOWN}};
311
312 std::unique_ptr<base::DictionaryValue> value(GetJSONFeatures());
313 ASSERT_TRUE(value);
314 JSONFeatureProvider api_feature_provider(*value, CreateAPIFeature);
315
316 for (size_t i = 0; i < arraysize(test_data); ++i) {
rkc 2016/08/18 20:35:28 Change test data to a vector (or std::array) then
xiyuan 2016/08/18 21:24:00 Think this is fine as many tests are using test da
tbarzic 2016/08/18 22:18:55 Done
317 ExtensionAPI api;
318 api.RegisterDependencyProvider("api", &api_feature_provider);
319 RegisterAPISchemaResourcesForTest(&api, *value);
320 ExtensionAPI::OverrideSharedInstanceForTest scope(&api);
321
322 std::unique_ptr<base::AutoReset<FeatureSessionType>> current_session(
323 ScopedCurrentFeatureSessionType(test_data[i].current_session_type));
324 EXPECT_EQ(test_data[i].expect_available,
325 api.IsAvailable(test_data[i].api_name, NULL,
326 Feature::BLESSED_EXTENSION_CONTEXT, GURL())
327 .is_available())
328 << "Test case " << i;
329 }
330 }
331
297 TEST(ExtensionAPITest, LazyGetSchema) { 332 TEST(ExtensionAPITest, LazyGetSchema) {
298 std::unique_ptr<ExtensionAPI> apis( 333 std::unique_ptr<ExtensionAPI> apis(
299 ExtensionAPI::CreateWithDefaultConfiguration()); 334 ExtensionAPI::CreateWithDefaultConfiguration());
300 335
301 EXPECT_EQ(NULL, apis->GetSchema(std::string())); 336 EXPECT_EQ(NULL, apis->GetSchema(std::string()));
302 EXPECT_EQ(NULL, apis->GetSchema(std::string())); 337 EXPECT_EQ(NULL, apis->GetSchema(std::string()));
303 EXPECT_EQ(NULL, apis->GetSchema("experimental")); 338 EXPECT_EQ(NULL, apis->GetSchema("experimental"));
304 EXPECT_EQ(NULL, apis->GetSchema("experimental")); 339 EXPECT_EQ(NULL, apis->GetSchema("experimental"));
305 EXPECT_EQ(NULL, apis->GetSchema("foo")); 340 EXPECT_EQ(NULL, apis->GetSchema("foo"));
306 EXPECT_EQ(NULL, apis->GetSchema("foo")); 341 EXPECT_EQ(NULL, apis->GetSchema("foo"));
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after
835 extension.get(), 870 extension.get(),
836 Feature::BLESSED_EXTENSION_CONTEXT, 871 Feature::BLESSED_EXTENSION_CONTEXT,
837 GURL()).is_available()); 872 GURL()).is_available());
838 EXPECT_FALSE(extension_api->IsAvailable("pageAction", 873 EXPECT_FALSE(extension_api->IsAvailable("pageAction",
839 extension.get(), 874 extension.get(),
840 Feature::BLESSED_EXTENSION_CONTEXT, 875 Feature::BLESSED_EXTENSION_CONTEXT,
841 GURL()).is_available()); 876 GURL()).is_available());
842 } 877 }
843 878
844 } // namespace extensions 879 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698