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

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
42 struct FeatureSessionTypesTestData {
43 std::string api_name;
44 bool expect_available;
45 FeatureSessionType current_session_type;
46 };
47
39 SimpleFeature* CreateAPIFeature() { 48 SimpleFeature* CreateAPIFeature() {
40 return new APIFeature(); 49 return new APIFeature();
41 } 50 }
42 51
52 std::unique_ptr<base::DictionaryValue> GetJSONFeatures() {
53 base::FilePath api_features_path;
54 PathService::Get(chrome::DIR_TEST_DATA, &api_features_path);
55 api_features_path = api_features_path.AppendASCII("extensions")
56 .AppendASCII("extension_api_unittest")
57 .AppendASCII("api_features.json");
58
59 std::string api_features_str;
60 if (!base::ReadFileToString(api_features_path, &api_features_str)) {
61 ADD_FAILURE() << "api_features.json unreadable";
62 return std::unique_ptr<base::DictionaryValue>();
63 }
64
65 return std::unique_ptr<base::DictionaryValue>(
66 static_cast<base::DictionaryValue*>(
67 base::JSONReader::Read(api_features_str).release()));
68 }
69
70 void RegisterAPISchemaResourcesForTest(ExtensionAPI* api,
71 const base::DictionaryValue& features) {
72 for (base::DictionaryValue::Iterator iter(features); !iter.IsAtEnd();
73 iter.Advance()) {
74 if (iter.key().find(".") == std::string::npos)
75 api->RegisterSchemaResource(iter.key(), 0);
76 }
77 }
78
79 } // namespace
80
43 TEST(ExtensionAPITest, Creation) { 81 TEST(ExtensionAPITest, Creation) {
44 ExtensionAPI* shared_instance = ExtensionAPI::GetSharedInstance(); 82 ExtensionAPI* shared_instance = ExtensionAPI::GetSharedInstance();
45 EXPECT_EQ(shared_instance, ExtensionAPI::GetSharedInstance()); 83 EXPECT_EQ(shared_instance, ExtensionAPI::GetSharedInstance());
46 84
47 std::unique_ptr<ExtensionAPI> new_instance( 85 std::unique_ptr<ExtensionAPI> new_instance(
48 ExtensionAPI::CreateWithDefaultConfiguration()); 86 ExtensionAPI::CreateWithDefaultConfiguration());
49 EXPECT_NE(new_instance.get(), 87 EXPECT_NE(new_instance.get(),
50 std::unique_ptr<ExtensionAPI>( 88 std::unique_ptr<ExtensionAPI>(
51 ExtensionAPI::CreateWithDefaultConfiguration()) 89 ExtensionAPI::CreateWithDefaultConfiguration())
52 .get()); 90 .get());
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 { "parent3.noparent", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() }, 195 { "parent3.noparent", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() },
158 { "parent3.noparent", true, Feature::BLESSED_EXTENSION_CONTEXT, GURL() }, 196 { "parent3.noparent", true, Feature::BLESSED_EXTENSION_CONTEXT, GURL() },
159 { "parent3.noparent", true, Feature::UNBLESSED_EXTENSION_CONTEXT, GURL() }, 197 { "parent3.noparent", true, Feature::UNBLESSED_EXTENSION_CONTEXT, GURL() },
160 { "parent3.noparent.child", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() }, 198 { "parent3.noparent.child", true, Feature::CONTENT_SCRIPT_CONTEXT, GURL() },
161 { "parent3.noparent.child", true, Feature::BLESSED_EXTENSION_CONTEXT, 199 { "parent3.noparent.child", true, Feature::BLESSED_EXTENSION_CONTEXT,
162 GURL() }, 200 GURL() },
163 { "parent3.noparent.child", true, Feature::UNBLESSED_EXTENSION_CONTEXT, 201 { "parent3.noparent.child", true, Feature::UNBLESSED_EXTENSION_CONTEXT,
164 GURL() } 202 GURL() }
165 }; 203 };
166 204
167 base::FilePath api_features_path; 205 std::unique_ptr<base::DictionaryValue> value(GetJSONFeatures());
168 PathService::Get(chrome::DIR_TEST_DATA, &api_features_path); 206 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); 207 JSONFeatureProvider api_feature_provider(*value, CreateAPIFeature);
181 208
182 for (size_t i = 0; i < arraysize(test_data); ++i) { 209 for (size_t i = 0; i < arraysize(test_data); ++i) {
183 ExtensionAPI api; 210 ExtensionAPI api;
184 api.RegisterDependencyProvider("api", &api_feature_provider); 211 api.RegisterDependencyProvider("api", &api_feature_provider);
185 for (base::DictionaryValue::Iterator iter(*value); !iter.IsAtEnd(); 212 RegisterAPISchemaResourcesForTest(&api, *value);
186 iter.Advance()) { 213 ExtensionAPI::OverrideSharedInstanceForTest scope(&api);
187 if (iter.key().find(".") == std::string::npos)
188 api.RegisterSchemaResource(iter.key(), 0);
189 }
190 214
191 ExtensionAPI::OverrideSharedInstanceForTest scope(&api);
192 bool expected = test_data[i].expect_is_available; 215 bool expected = test_data[i].expect_is_available;
193 Feature::Availability availability = 216 Feature::Availability availability =
194 api.IsAvailable(test_data[i].api_full_name, 217 api.IsAvailable(test_data[i].api_full_name,
195 NULL, 218 NULL,
196 test_data[i].context, 219 test_data[i].context,
197 test_data[i].url); 220 test_data[i].url);
198 EXPECT_EQ(expected, availability.is_available()) 221 EXPECT_EQ(expected, availability.is_available())
199 << base::StringPrintf("Test %d: Feature '%s' was %s: %s", 222 << base::StringPrintf("Test %d: Feature '%s' was %s: %s",
200 static_cast<int>(i), 223 static_cast<int>(i),
201 test_data[i].api_full_name.c_str(), 224 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") }, 274 GURL("http://google.com") },
252 { "test3", true, Feature::CONTENT_SCRIPT_CONTEXT, NULL, GURL() }, 275 { "test3", true, Feature::CONTENT_SCRIPT_CONTEXT, NULL, GURL() },
253 { "test3", true, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://foo.com") }, 276 { "test3", true, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://foo.com") },
254 { "test4.foo", true, Feature::CONTENT_SCRIPT_CONTEXT, NULL, GURL() }, 277 { "test4.foo", true, Feature::CONTENT_SCRIPT_CONTEXT, NULL, GURL() },
255 { "test7", false, Feature::WEB_PAGE_CONTEXT, NULL, 278 { "test7", false, Feature::WEB_PAGE_CONTEXT, NULL,
256 GURL("http://google.com") }, 279 GURL("http://google.com") },
257 { "test7", true, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://foo.com") }, 280 { "test7", true, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://foo.com") },
258 { "test7", false, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://bar.com") } 281 { "test7", false, Feature::WEB_PAGE_CONTEXT, NULL, GURL("http://bar.com") }
259 }; 282 };
260 283
261 base::FilePath api_features_path; 284 std::unique_ptr<base::DictionaryValue> value(GetJSONFeatures());
262 PathService::Get(chrome::DIR_TEST_DATA, &api_features_path); 285 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); 286 JSONFeatureProvider api_feature_provider(*value, CreateAPIFeature);
275 287
276 for (size_t i = 0; i < arraysize(test_data); ++i) { 288 for (size_t i = 0; i < arraysize(test_data); ++i) {
277 ExtensionAPI api; 289 ExtensionAPI api;
278 api.RegisterDependencyProvider("api", &api_feature_provider); 290 api.RegisterDependencyProvider("api", &api_feature_provider);
279 for (base::DictionaryValue::Iterator iter(*value); !iter.IsAtEnd(); 291 RegisterAPISchemaResourcesForTest(&api, *value);
280 iter.Advance()) { 292 ExtensionAPI::OverrideSharedInstanceForTest scope(&api);
281 if (iter.key().find(".") == std::string::npos)
282 api.RegisterSchemaResource(iter.key(), 0);
283 }
284 293
285 Feature* test_feature = 294 Feature* test_feature =
286 api_feature_provider.GetFeature(test_data[i].api_full_name); 295 api_feature_provider.GetFeature(test_data[i].api_full_name);
287 ASSERT_TRUE(test_feature); 296 ASSERT_TRUE(test_feature);
288 EXPECT_EQ(test_data[i].expect_is_available, 297 EXPECT_EQ(test_data[i].expect_is_available,
289 api.IsAnyFeatureAvailableToContext(*test_feature, 298 api.IsAnyFeatureAvailableToContext(*test_feature,
290 test_data[i].extension, 299 test_data[i].extension,
291 test_data[i].context, 300 test_data[i].context,
292 test_data[i].url)) 301 test_data[i].url))
293 << i; 302 << i;
294 } 303 }
295 } 304 }
296 305
306 TEST(ExtensionAPITest, SessionTypeFeature) {
307 const std::vector<FeatureSessionTypesTestData> kTestData(
308 {{"kiosk_only", true, FeatureSessionType::KIOSK},
309 {"kiosk_only", false, FeatureSessionType::REGULAR},
310 {"kiosk_only", false, FeatureSessionType::UNKNOWN},
311 {"non_kiosk", false, FeatureSessionType::KIOSK},
312 {"non_kiosk", true, FeatureSessionType::REGULAR},
313 {"non_kiosk", false, FeatureSessionType::UNKNOWN},
314 {"all_session_types", true, FeatureSessionType::KIOSK},
315 {"all_session_types", true, FeatureSessionType::REGULAR},
316 {"all_session_types", false, FeatureSessionType::UNKNOWN},
317 {"empty_session_types", false, FeatureSessionType::KIOSK},
318 {"empty_session_types", false, FeatureSessionType::REGULAR},
319 {"empty_session_types", false, FeatureSessionType::UNKNOWN},
320 {"invalid_session_types", false, FeatureSessionType::KIOSK},
Devlin 2016/08/23 20:49:06 Does this feature exist?
tbarzic 2016/09/08 18:48:54 no, removed
321 {"invalid_session_types", false, FeatureSessionType::REGULAR},
322 {"invalid_session_types", false, FeatureSessionType::UNKNOWN},
323 {"test1", true, FeatureSessionType::KIOSK},
324 {"test1", true, FeatureSessionType::REGULAR},
325 {"test1", true, FeatureSessionType::UNKNOWN}});
326
327 std::unique_ptr<base::DictionaryValue> value(GetJSONFeatures());
328 ASSERT_TRUE(value);
329 JSONFeatureProvider api_feature_provider(*value, CreateAPIFeature);
330
331 for (const auto& test : kTestData) {
332 ExtensionAPI api;
333 api.RegisterDependencyProvider("api", &api_feature_provider);
334 RegisterAPISchemaResourcesForTest(&api, *value);
335 ExtensionAPI::OverrideSharedInstanceForTest scope(&api);
336
337 std::unique_ptr<base::AutoReset<FeatureSessionType>> current_session(
338 ScopedCurrentFeatureSessionType(test.current_session_type));
339 EXPECT_EQ(test.expect_available,
340 api.IsAvailable(test.api_name, NULL,
341 Feature::BLESSED_EXTENSION_CONTEXT, GURL())
342 .is_available())
343 << "Test case (" << test.api_name << ", "
344 << static_cast<int>(test.current_session_type) << ").";
345 }
346 }
347
297 TEST(ExtensionAPITest, LazyGetSchema) { 348 TEST(ExtensionAPITest, LazyGetSchema) {
298 std::unique_ptr<ExtensionAPI> apis( 349 std::unique_ptr<ExtensionAPI> apis(
299 ExtensionAPI::CreateWithDefaultConfiguration()); 350 ExtensionAPI::CreateWithDefaultConfiguration());
300 351
301 EXPECT_EQ(NULL, apis->GetSchema(std::string())); 352 EXPECT_EQ(NULL, apis->GetSchema(std::string()));
302 EXPECT_EQ(NULL, apis->GetSchema(std::string())); 353 EXPECT_EQ(NULL, apis->GetSchema(std::string()));
303 EXPECT_EQ(NULL, apis->GetSchema("experimental")); 354 EXPECT_EQ(NULL, apis->GetSchema("experimental"));
304 EXPECT_EQ(NULL, apis->GetSchema("experimental")); 355 EXPECT_EQ(NULL, apis->GetSchema("experimental"));
305 EXPECT_EQ(NULL, apis->GetSchema("foo")); 356 EXPECT_EQ(NULL, apis->GetSchema("foo"));
306 EXPECT_EQ(NULL, apis->GetSchema("foo")); 357 EXPECT_EQ(NULL, apis->GetSchema("foo"));
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after
835 extension.get(), 886 extension.get(),
836 Feature::BLESSED_EXTENSION_CONTEXT, 887 Feature::BLESSED_EXTENSION_CONTEXT,
837 GURL()).is_available()); 888 GURL()).is_available());
838 EXPECT_FALSE(extension_api->IsAvailable("pageAction", 889 EXPECT_FALSE(extension_api->IsAvailable("pageAction",
839 extension.get(), 890 extension.get(),
840 Feature::BLESSED_EXTENSION_CONTEXT, 891 Feature::BLESSED_EXTENSION_CONTEXT,
841 GURL()).is_available()); 892 GURL()).is_available());
842 } 893 }
843 894
844 } // namespace extensions 895 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698