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

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

Issue 1880143002: Convert chrome/common to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 8 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 <string> 10 #include <string>
10 #include <utility> 11 #include <utility>
11 #include <vector> 12 #include <vector>
12 13
13 #include "base/files/file_path.h" 14 #include "base/files/file_path.h"
14 #include "base/files/file_util.h" 15 #include "base/files/file_util.h"
15 #include "base/json/json_reader.h" 16 #include "base/json/json_reader.h"
16 #include "base/json/json_writer.h" 17 #include "base/json/json_writer.h"
17 #include "base/macros.h" 18 #include "base/macros.h"
18 #include "base/memory/ref_counted.h" 19 #include "base/memory/ref_counted.h"
19 #include "base/memory/scoped_ptr.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/base_feature_provider.h" 27 #include "extensions/common/features/base_feature_provider.h"
28 #include "extensions/common/features/simple_feature.h" 28 #include "extensions/common/features/simple_feature.h"
29 #include "extensions/common/manifest.h" 29 #include "extensions/common/manifest.h"
30 #include "extensions/common/manifest_constants.h" 30 #include "extensions/common/manifest_constants.h"
31 #include "extensions/common/test_util.h" 31 #include "extensions/common/test_util.h"
32 #include "extensions/common/value_builder.h" 32 #include "extensions/common/value_builder.h"
33 #include "testing/gtest/include/gtest/gtest.h" 33 #include "testing/gtest/include/gtest/gtest.h"
34 34
35 namespace extensions { 35 namespace extensions {
36 36
37 using test_util::BuildExtension; 37 using test_util::BuildExtension;
38 38
39 SimpleFeature* CreateAPIFeature() { 39 SimpleFeature* CreateAPIFeature() {
40 return new APIFeature(); 40 return new APIFeature();
41 } 41 }
42 42
43 TEST(ExtensionAPITest, Creation) { 43 TEST(ExtensionAPITest, Creation) {
44 ExtensionAPI* shared_instance = ExtensionAPI::GetSharedInstance(); 44 ExtensionAPI* shared_instance = ExtensionAPI::GetSharedInstance();
45 EXPECT_EQ(shared_instance, ExtensionAPI::GetSharedInstance()); 45 EXPECT_EQ(shared_instance, ExtensionAPI::GetSharedInstance());
46 46
47 scoped_ptr<ExtensionAPI> new_instance( 47 std::unique_ptr<ExtensionAPI> new_instance(
48 ExtensionAPI::CreateWithDefaultConfiguration()); 48 ExtensionAPI::CreateWithDefaultConfiguration());
49 EXPECT_NE(new_instance.get(), 49 EXPECT_NE(new_instance.get(),
50 scoped_ptr<ExtensionAPI>( 50 std::unique_ptr<ExtensionAPI>(
51 ExtensionAPI::CreateWithDefaultConfiguration()).get()); 51 ExtensionAPI::CreateWithDefaultConfiguration())
52 .get());
52 53
53 ExtensionAPI empty_instance; 54 ExtensionAPI empty_instance;
54 55
55 struct { 56 struct {
56 ExtensionAPI* api; 57 ExtensionAPI* api;
57 bool expect_populated; 58 bool expect_populated;
58 } test_data[] = { 59 } test_data[] = {
59 { shared_instance, true }, 60 { shared_instance, true },
60 { new_instance.get(), true }, 61 { new_instance.get(), true },
61 { &empty_instance, false } 62 { &empty_instance, false }
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 base::FilePath api_features_path; 167 base::FilePath api_features_path;
167 PathService::Get(chrome::DIR_TEST_DATA, &api_features_path); 168 PathService::Get(chrome::DIR_TEST_DATA, &api_features_path);
168 api_features_path = api_features_path.AppendASCII("extensions") 169 api_features_path = api_features_path.AppendASCII("extensions")
169 .AppendASCII("extension_api_unittest") 170 .AppendASCII("extension_api_unittest")
170 .AppendASCII("api_features.json"); 171 .AppendASCII("api_features.json");
171 172
172 std::string api_features_str; 173 std::string api_features_str;
173 ASSERT_TRUE(base::ReadFileToString( 174 ASSERT_TRUE(base::ReadFileToString(
174 api_features_path, &api_features_str)) << "api_features.json"; 175 api_features_path, &api_features_str)) << "api_features.json";
175 176
176 scoped_ptr<base::DictionaryValue> value(static_cast<base::DictionaryValue*>( 177 std::unique_ptr<base::DictionaryValue> value(
177 base::JSONReader::Read(api_features_str).release())); 178 static_cast<base::DictionaryValue*>(
179 base::JSONReader::Read(api_features_str).release()));
178 BaseFeatureProvider api_feature_provider(*value, CreateAPIFeature); 180 BaseFeatureProvider api_feature_provider(*value, CreateAPIFeature);
179 181
180 for (size_t i = 0; i < arraysize(test_data); ++i) { 182 for (size_t i = 0; i < arraysize(test_data); ++i) {
181 ExtensionAPI api; 183 ExtensionAPI api;
182 api.RegisterDependencyProvider("api", &api_feature_provider); 184 api.RegisterDependencyProvider("api", &api_feature_provider);
183 for (base::DictionaryValue::Iterator iter(*value); !iter.IsAtEnd(); 185 for (base::DictionaryValue::Iterator iter(*value); !iter.IsAtEnd();
184 iter.Advance()) { 186 iter.Advance()) {
185 if (iter.key().find(".") == std::string::npos) 187 if (iter.key().find(".") == std::string::npos)
186 api.RegisterSchemaResource(iter.key(), 0); 188 api.RegisterSchemaResource(iter.key(), 0);
187 } 189 }
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 base::FilePath api_features_path; 261 base::FilePath api_features_path;
260 PathService::Get(chrome::DIR_TEST_DATA, &api_features_path); 262 PathService::Get(chrome::DIR_TEST_DATA, &api_features_path);
261 api_features_path = api_features_path.AppendASCII("extensions") 263 api_features_path = api_features_path.AppendASCII("extensions")
262 .AppendASCII("extension_api_unittest") 264 .AppendASCII("extension_api_unittest")
263 .AppendASCII("api_features.json"); 265 .AppendASCII("api_features.json");
264 266
265 std::string api_features_str; 267 std::string api_features_str;
266 ASSERT_TRUE(base::ReadFileToString( 268 ASSERT_TRUE(base::ReadFileToString(
267 api_features_path, &api_features_str)) << "api_features.json"; 269 api_features_path, &api_features_str)) << "api_features.json";
268 270
269 scoped_ptr<base::DictionaryValue> value(static_cast<base::DictionaryValue*>( 271 std::unique_ptr<base::DictionaryValue> value(
270 base::JSONReader::Read(api_features_str).release())); 272 static_cast<base::DictionaryValue*>(
273 base::JSONReader::Read(api_features_str).release()));
271 BaseFeatureProvider api_feature_provider(*value, CreateAPIFeature); 274 BaseFeatureProvider api_feature_provider(*value, CreateAPIFeature);
272 275
273 for (size_t i = 0; i < arraysize(test_data); ++i) { 276 for (size_t i = 0; i < arraysize(test_data); ++i) {
274 ExtensionAPI api; 277 ExtensionAPI api;
275 api.RegisterDependencyProvider("api", &api_feature_provider); 278 api.RegisterDependencyProvider("api", &api_feature_provider);
276 for (base::DictionaryValue::Iterator iter(*value); !iter.IsAtEnd(); 279 for (base::DictionaryValue::Iterator iter(*value); !iter.IsAtEnd();
277 iter.Advance()) { 280 iter.Advance()) {
278 if (iter.key().find(".") == std::string::npos) 281 if (iter.key().find(".") == std::string::npos)
279 api.RegisterSchemaResource(iter.key(), 0); 282 api.RegisterSchemaResource(iter.key(), 0);
280 } 283 }
281 284
282 Feature* test_feature = 285 Feature* test_feature =
283 api_feature_provider.GetFeature(test_data[i].api_full_name); 286 api_feature_provider.GetFeature(test_data[i].api_full_name);
284 ASSERT_TRUE(test_feature); 287 ASSERT_TRUE(test_feature);
285 EXPECT_EQ(test_data[i].expect_is_available, 288 EXPECT_EQ(test_data[i].expect_is_available,
286 api.IsAnyFeatureAvailableToContext(*test_feature, 289 api.IsAnyFeatureAvailableToContext(*test_feature,
287 test_data[i].extension, 290 test_data[i].extension,
288 test_data[i].context, 291 test_data[i].context,
289 test_data[i].url)) 292 test_data[i].url))
290 << i; 293 << i;
291 } 294 }
292 } 295 }
293 296
294 TEST(ExtensionAPITest, LazyGetSchema) { 297 TEST(ExtensionAPITest, LazyGetSchema) {
295 scoped_ptr<ExtensionAPI> apis(ExtensionAPI::CreateWithDefaultConfiguration()); 298 std::unique_ptr<ExtensionAPI> apis(
299 ExtensionAPI::CreateWithDefaultConfiguration());
296 300
297 EXPECT_EQ(NULL, apis->GetSchema(std::string())); 301 EXPECT_EQ(NULL, apis->GetSchema(std::string()));
298 EXPECT_EQ(NULL, apis->GetSchema(std::string())); 302 EXPECT_EQ(NULL, apis->GetSchema(std::string()));
299 EXPECT_EQ(NULL, apis->GetSchema("experimental")); 303 EXPECT_EQ(NULL, apis->GetSchema("experimental"));
300 EXPECT_EQ(NULL, apis->GetSchema("experimental")); 304 EXPECT_EQ(NULL, apis->GetSchema("experimental"));
301 EXPECT_EQ(NULL, apis->GetSchema("foo")); 305 EXPECT_EQ(NULL, apis->GetSchema("foo"));
302 EXPECT_EQ(NULL, apis->GetSchema("foo")); 306 EXPECT_EQ(NULL, apis->GetSchema("foo"));
303 307
304 EXPECT_TRUE(apis->GetSchema("dns")); 308 EXPECT_TRUE(apis->GetSchema("dns"));
305 EXPECT_TRUE(apis->GetSchema("dns")); 309 EXPECT_TRUE(apis->GetSchema("dns"));
306 EXPECT_TRUE(apis->GetSchema("extension")); 310 EXPECT_TRUE(apis->GetSchema("extension"));
307 EXPECT_TRUE(apis->GetSchema("extension")); 311 EXPECT_TRUE(apis->GetSchema("extension"));
308 EXPECT_TRUE(apis->GetSchema("omnibox")); 312 EXPECT_TRUE(apis->GetSchema("omnibox"));
309 EXPECT_TRUE(apis->GetSchema("omnibox")); 313 EXPECT_TRUE(apis->GetSchema("omnibox"));
310 EXPECT_TRUE(apis->GetSchema("storage")); 314 EXPECT_TRUE(apis->GetSchema("storage"));
311 EXPECT_TRUE(apis->GetSchema("storage")); 315 EXPECT_TRUE(apis->GetSchema("storage"));
312 } 316 }
313 317
314 scoped_refptr<Extension> CreateExtensionWithPermissions( 318 scoped_refptr<Extension> CreateExtensionWithPermissions(
315 const std::set<std::string>& permissions) { 319 const std::set<std::string>& permissions) {
316 base::DictionaryValue manifest; 320 base::DictionaryValue manifest;
317 manifest.SetString("name", "extension"); 321 manifest.SetString("name", "extension");
318 manifest.SetString("version", "1.0"); 322 manifest.SetString("version", "1.0");
319 manifest.SetInteger("manifest_version", 2); 323 manifest.SetInteger("manifest_version", 2);
320 { 324 {
321 scoped_ptr<base::ListValue> permissions_list(new base::ListValue()); 325 std::unique_ptr<base::ListValue> permissions_list(new base::ListValue());
322 for (std::set<std::string>::const_iterator i = permissions.begin(); 326 for (std::set<std::string>::const_iterator i = permissions.begin();
323 i != permissions.end(); ++i) { 327 i != permissions.end(); ++i) {
324 permissions_list->Append(new base::StringValue(*i)); 328 permissions_list->Append(new base::StringValue(*i));
325 } 329 }
326 manifest.Set("permissions", permissions_list.release()); 330 manifest.Set("permissions", permissions_list.release());
327 } 331 }
328 332
329 std::string error; 333 std::string error;
330 scoped_refptr<Extension> extension(Extension::Create( 334 scoped_refptr<Extension> extension(Extension::Create(
331 base::FilePath(), Manifest::UNPACKED, 335 base::FilePath(), Manifest::UNPACKED,
(...skipping 13 matching lines...) Expand all
345 349
346 TEST(ExtensionAPITest, ExtensionWithUnprivilegedAPIs) { 350 TEST(ExtensionAPITest, ExtensionWithUnprivilegedAPIs) {
347 scoped_refptr<Extension> extension; 351 scoped_refptr<Extension> extension;
348 { 352 {
349 std::set<std::string> permissions; 353 std::set<std::string> permissions;
350 permissions.insert("storage"); 354 permissions.insert("storage");
351 permissions.insert("history"); 355 permissions.insert("history");
352 extension = CreateExtensionWithPermissions(permissions); 356 extension = CreateExtensionWithPermissions(permissions);
353 } 357 }
354 358
355 scoped_ptr<ExtensionAPI> extension_api( 359 std::unique_ptr<ExtensionAPI> extension_api(
356 ExtensionAPI::CreateWithDefaultConfiguration()); 360 ExtensionAPI::CreateWithDefaultConfiguration());
357 361
358 const FeatureProvider& api_features = *FeatureProvider::GetAPIFeatures(); 362 const FeatureProvider& api_features = *FeatureProvider::GetAPIFeatures();
359 363
360 // "storage" is completely unprivileged. 364 // "storage" is completely unprivileged.
361 EXPECT_TRUE(extension_api->IsAnyFeatureAvailableToContext( 365 EXPECT_TRUE(extension_api->IsAnyFeatureAvailableToContext(
362 *api_features.GetFeature("storage"), 366 *api_features.GetFeature("storage"),
363 NULL, 367 NULL,
364 Feature::BLESSED_EXTENSION_CONTEXT, 368 Feature::BLESSED_EXTENSION_CONTEXT,
365 GURL())); 369 GURL()));
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 "http://www.example.com"); 442 "http://www.example.com");
439 443
440 base::DictionaryValue* app = new base::DictionaryValue(); 444 base::DictionaryValue* app = new base::DictionaryValue();
441 base::DictionaryValue* background = new base::DictionaryValue(); 445 base::DictionaryValue* background = new base::DictionaryValue();
442 base::ListValue* scripts = new base::ListValue(); 446 base::ListValue* scripts = new base::ListValue();
443 scripts->Append(new base::StringValue("test.js")); 447 scripts->Append(new base::StringValue("test.js"));
444 background->Set("scripts", scripts); 448 background->Set("scripts", scripts);
445 app->Set("background", background); 449 app->Set("background", background);
446 values.Set(manifest_keys::kApp, app); 450 values.Set(manifest_keys::kApp, app);
447 { 451 {
448 scoped_ptr<base::ListValue> permissions_list(new base::ListValue()); 452 std::unique_ptr<base::ListValue> permissions_list(new base::ListValue());
449 for (std::set<std::string>::const_iterator i = permissions.begin(); 453 for (std::set<std::string>::const_iterator i = permissions.begin();
450 i != permissions.end(); ++i) { 454 i != permissions.end(); ++i) {
451 permissions_list->Append(new base::StringValue(*i)); 455 permissions_list->Append(new base::StringValue(*i));
452 } 456 }
453 values.Set("permissions", permissions_list.release()); 457 values.Set("permissions", permissions_list.release());
454 } 458 }
455 459
456 std::string error; 460 std::string error;
457 scoped_refptr<Extension> extension(Extension::Create( 461 scoped_refptr<Extension> extension(Extension::Create(
458 base::FilePath(), Manifest::INTERNAL, values, Extension::NO_FLAGS, 462 base::FilePath(), Manifest::INTERNAL, values, Extension::NO_FLAGS,
459 &error)); 463 &error));
460 CHECK(extension.get()) << error; 464 CHECK(extension.get()) << error;
461 return extension; 465 return extension;
462 } 466 }
463 467
464 TEST(ExtensionAPITest, HostedAppPermissions) { 468 TEST(ExtensionAPITest, HostedAppPermissions) {
465 scoped_refptr<Extension> extension = CreateHostedApp(); 469 scoped_refptr<Extension> extension = CreateHostedApp();
466 470
467 scoped_ptr<ExtensionAPI> extension_api( 471 std::unique_ptr<ExtensionAPI> extension_api(
468 ExtensionAPI::CreateWithDefaultConfiguration()); 472 ExtensionAPI::CreateWithDefaultConfiguration());
469 473
470 // "runtime" and "tabs" should not be available in hosted apps. 474 // "runtime" and "tabs" should not be available in hosted apps.
471 EXPECT_FALSE(extension_api->IsAvailable("runtime", 475 EXPECT_FALSE(extension_api->IsAvailable("runtime",
472 extension.get(), 476 extension.get(),
473 Feature::BLESSED_EXTENSION_CONTEXT, 477 Feature::BLESSED_EXTENSION_CONTEXT,
474 GURL()).is_available()); 478 GURL()).is_available());
475 EXPECT_FALSE(extension_api->IsAvailable("runtime.id", 479 EXPECT_FALSE(extension_api->IsAvailable("runtime.id",
476 extension.get(), 480 extension.get(),
477 Feature::BLESSED_EXTENSION_CONTEXT, 481 Feature::BLESSED_EXTENSION_CONTEXT,
478 GURL()).is_available()); 482 GURL()).is_available());
479 EXPECT_FALSE(extension_api->IsAvailable("runtime.sendMessage", 483 EXPECT_FALSE(extension_api->IsAvailable("runtime.sendMessage",
480 extension.get(), 484 extension.get(),
481 Feature::BLESSED_EXTENSION_CONTEXT, 485 Feature::BLESSED_EXTENSION_CONTEXT,
482 GURL()).is_available()); 486 GURL()).is_available());
483 EXPECT_FALSE(extension_api->IsAvailable("runtime.sendNativeMessage", 487 EXPECT_FALSE(extension_api->IsAvailable("runtime.sendNativeMessage",
484 extension.get(), 488 extension.get(),
485 Feature::BLESSED_EXTENSION_CONTEXT, 489 Feature::BLESSED_EXTENSION_CONTEXT,
486 GURL()).is_available()); 490 GURL()).is_available());
487 EXPECT_FALSE(extension_api->IsAvailable("tabs.create", 491 EXPECT_FALSE(extension_api->IsAvailable("tabs.create",
488 extension.get(), 492 extension.get(),
489 Feature::BLESSED_EXTENSION_CONTEXT, 493 Feature::BLESSED_EXTENSION_CONTEXT,
490 GURL()).is_available()); 494 GURL()).is_available());
491 } 495 }
492 496
493 TEST(ExtensionAPITest, AppAndFriendsAvailability) { 497 TEST(ExtensionAPITest, AppAndFriendsAvailability) {
494 498 std::unique_ptr<ExtensionAPI> extension_api(
495 scoped_ptr<ExtensionAPI> extension_api(
496 ExtensionAPI::CreateWithDefaultConfiguration()); 499 ExtensionAPI::CreateWithDefaultConfiguration());
497 500
498 // Make sure chrome.app.runtime and chrome.app.window are available to apps, 501 // Make sure chrome.app.runtime and chrome.app.window are available to apps,
499 // and chrome.app is not. 502 // and chrome.app is not.
500 { 503 {
501 std::set<std::string> permissions; 504 std::set<std::string> permissions;
502 permissions.insert("app.runtime"); 505 permissions.insert("app.runtime");
503 permissions.insert("app.window"); 506 permissions.insert("app.window");
504 scoped_refptr<Extension> extension = 507 scoped_refptr<Extension> extension =
505 CreatePackagedAppWithPermissions(permissions); 508 CreatePackagedAppWithPermissions(permissions);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 GURL("http://foo.com")).is_available()); 545 GURL("http://foo.com")).is_available());
543 } 546 }
544 } 547 }
545 548
546 TEST(ExtensionAPITest, ExtensionWithDependencies) { 549 TEST(ExtensionAPITest, ExtensionWithDependencies) {
547 // Extension with the "ttsEngine" permission but not the "tts" permission; it 550 // Extension with the "ttsEngine" permission but not the "tts" permission; it
548 // should not automatically get "tts" permission. 551 // should not automatically get "tts" permission.
549 { 552 {
550 scoped_refptr<Extension> extension = 553 scoped_refptr<Extension> extension =
551 CreateExtensionWithPermission("ttsEngine"); 554 CreateExtensionWithPermission("ttsEngine");
552 scoped_ptr<ExtensionAPI> api( 555 std::unique_ptr<ExtensionAPI> api(
553 ExtensionAPI::CreateWithDefaultConfiguration()); 556 ExtensionAPI::CreateWithDefaultConfiguration());
554 EXPECT_TRUE(api->IsAvailable("ttsEngine", 557 EXPECT_TRUE(api->IsAvailable("ttsEngine",
555 extension.get(), 558 extension.get(),
556 Feature::BLESSED_EXTENSION_CONTEXT, 559 Feature::BLESSED_EXTENSION_CONTEXT,
557 GURL()).is_available()); 560 GURL()).is_available());
558 EXPECT_FALSE(api->IsAvailable("tts", 561 EXPECT_FALSE(api->IsAvailable("tts",
559 extension.get(), 562 extension.get(),
560 Feature::BLESSED_EXTENSION_CONTEXT, 563 Feature::BLESSED_EXTENSION_CONTEXT,
561 GURL()).is_available()); 564 GURL()).is_available());
562 } 565 }
563 566
564 // Conversely, extension with the "tts" permission but not the "ttsEngine" 567 // Conversely, extension with the "tts" permission but not the "ttsEngine"
565 // permission shouldn't get the "ttsEngine" permission. 568 // permission shouldn't get the "ttsEngine" permission.
566 { 569 {
567 scoped_refptr<Extension> extension = 570 scoped_refptr<Extension> extension =
568 CreateExtensionWithPermission("tts"); 571 CreateExtensionWithPermission("tts");
569 scoped_ptr<ExtensionAPI> api( 572 std::unique_ptr<ExtensionAPI> api(
570 ExtensionAPI::CreateWithDefaultConfiguration()); 573 ExtensionAPI::CreateWithDefaultConfiguration());
571 EXPECT_FALSE(api->IsAvailable("ttsEngine", 574 EXPECT_FALSE(api->IsAvailable("ttsEngine",
572 extension.get(), 575 extension.get(),
573 Feature::BLESSED_EXTENSION_CONTEXT, 576 Feature::BLESSED_EXTENSION_CONTEXT,
574 GURL()).is_available()); 577 GURL()).is_available());
575 EXPECT_TRUE(api->IsAvailable("tts", 578 EXPECT_TRUE(api->IsAvailable("tts",
576 extension.get(), 579 extension.get(),
577 Feature::BLESSED_EXTENSION_CONTEXT, 580 Feature::BLESSED_EXTENSION_CONTEXT,
578 GURL()).is_available()); 581 GURL()).is_available());
579 } 582 }
580 } 583 }
581 584
582 bool MatchesURL( 585 bool MatchesURL(
583 ExtensionAPI* api, const std::string& api_name, const std::string& url) { 586 ExtensionAPI* api, const std::string& api_name, const std::string& url) {
584 return api->IsAvailable( 587 return api->IsAvailable(
585 api_name, NULL, Feature::WEB_PAGE_CONTEXT, GURL(url)).is_available(); 588 api_name, NULL, Feature::WEB_PAGE_CONTEXT, GURL(url)).is_available();
586 } 589 }
587 590
588 TEST(ExtensionAPITest, URLMatching) { 591 TEST(ExtensionAPITest, URLMatching) {
589 scoped_ptr<ExtensionAPI> api(ExtensionAPI::CreateWithDefaultConfiguration()); 592 std::unique_ptr<ExtensionAPI> api(
593 ExtensionAPI::CreateWithDefaultConfiguration());
590 594
591 // "app" API is available to all URLs that content scripts can be injected. 595 // "app" API is available to all URLs that content scripts can be injected.
592 EXPECT_TRUE(MatchesURL(api.get(), "app", "http://example.com/example.html")); 596 EXPECT_TRUE(MatchesURL(api.get(), "app", "http://example.com/example.html"));
593 EXPECT_TRUE(MatchesURL(api.get(), "app", "https://blah.net")); 597 EXPECT_TRUE(MatchesURL(api.get(), "app", "https://blah.net"));
594 EXPECT_TRUE(MatchesURL(api.get(), "app", "file://somefile.html")); 598 EXPECT_TRUE(MatchesURL(api.get(), "app", "file://somefile.html"));
595 599
596 // Also to internal URLs. 600 // Also to internal URLs.
597 EXPECT_TRUE(MatchesURL(api.get(), "app", "about:flags")); 601 EXPECT_TRUE(MatchesURL(api.get(), "app", "about:flags"));
598 EXPECT_TRUE(MatchesURL(api.get(), "app", "chrome://flags")); 602 EXPECT_TRUE(MatchesURL(api.get(), "app", "chrome://flags"));
599 603
(...skipping 23 matching lines...) Expand all
623 { "bookmarks", "bookmarks", "" }, 627 { "bookmarks", "bookmarks", "" },
624 { "bookmarks.", "bookmarks", "" }, 628 { "bookmarks.", "bookmarks", "" },
625 { ".bookmarks", "", "" }, 629 { ".bookmarks", "", "" },
626 { "bookmarks.create", "bookmarks", "create" }, 630 { "bookmarks.create", "bookmarks", "create" },
627 { "bookmarks.create.", "bookmarks", "create." }, 631 { "bookmarks.create.", "bookmarks", "create." },
628 { "bookmarks.create.monkey", "bookmarks", "create.monkey" }, 632 { "bookmarks.create.monkey", "bookmarks", "create.monkey" },
629 { "bookmarkManagerPrivate", "bookmarkManagerPrivate", "" }, 633 { "bookmarkManagerPrivate", "bookmarkManagerPrivate", "" },
630 { "bookmarkManagerPrivate.copy", "bookmarkManagerPrivate", "copy" } 634 { "bookmarkManagerPrivate.copy", "bookmarkManagerPrivate", "copy" }
631 }; 635 };
632 636
633 scoped_ptr<ExtensionAPI> api(ExtensionAPI::CreateWithDefaultConfiguration()); 637 std::unique_ptr<ExtensionAPI> api(
638 ExtensionAPI::CreateWithDefaultConfiguration());
634 for (size_t i = 0; i < arraysize(test_data); ++i) { 639 for (size_t i = 0; i < arraysize(test_data); ++i) {
635 std::string child_name; 640 std::string child_name;
636 std::string api_name = api->GetAPINameFromFullName(test_data[i].input, 641 std::string api_name = api->GetAPINameFromFullName(test_data[i].input,
637 &child_name); 642 &child_name);
638 EXPECT_EQ(test_data[i].api_name, api_name) << test_data[i].input; 643 EXPECT_EQ(test_data[i].api_name, api_name) << test_data[i].input;
639 EXPECT_EQ(test_data[i].child_name, child_name) << test_data[i].input; 644 EXPECT_EQ(test_data[i].child_name, child_name) << test_data[i].input;
640 } 645 }
641 } 646 }
642 647
643 TEST(ExtensionAPITest, DefaultConfigurationFeatures) { 648 TEST(ExtensionAPITest, DefaultConfigurationFeatures) {
644 scoped_ptr<ExtensionAPI> api(ExtensionAPI::CreateWithDefaultConfiguration()); 649 std::unique_ptr<ExtensionAPI> api(
650 ExtensionAPI::CreateWithDefaultConfiguration());
645 651
646 SimpleFeature* bookmarks = static_cast<SimpleFeature*>( 652 SimpleFeature* bookmarks = static_cast<SimpleFeature*>(
647 api->GetFeatureDependency("api:bookmarks")); 653 api->GetFeatureDependency("api:bookmarks"));
648 SimpleFeature* bookmarks_create = static_cast<SimpleFeature*>( 654 SimpleFeature* bookmarks_create = static_cast<SimpleFeature*>(
649 api->GetFeatureDependency("api:bookmarks.create")); 655 api->GetFeatureDependency("api:bookmarks.create"));
650 656
651 struct { 657 struct {
652 SimpleFeature* feature; 658 SimpleFeature* feature;
653 // TODO(aa): More stuff to test over time. 659 // TODO(aa): More stuff to test over time.
654 } test_data[] = { 660 } test_data[] = {
(...skipping 10 matching lines...) Expand all
665 671
666 EXPECT_EQ(SimpleFeature::UNSPECIFIED_LOCATION, feature->location()); 672 EXPECT_EQ(SimpleFeature::UNSPECIFIED_LOCATION, feature->location());
667 EXPECT_TRUE(feature->platforms()->empty()); 673 EXPECT_TRUE(feature->platforms()->empty());
668 EXPECT_EQ(0, feature->min_manifest_version()); 674 EXPECT_EQ(0, feature->min_manifest_version());
669 EXPECT_EQ(0, feature->max_manifest_version()); 675 EXPECT_EQ(0, feature->max_manifest_version());
670 } 676 }
671 } 677 }
672 678
673 TEST(ExtensionAPITest, FeaturesRequireContexts) { 679 TEST(ExtensionAPITest, FeaturesRequireContexts) {
674 // TODO(cduvall): Make this check API featues. 680 // TODO(cduvall): Make this check API featues.
675 scoped_ptr<base::DictionaryValue> api_features1(new base::DictionaryValue()); 681 std::unique_ptr<base::DictionaryValue> api_features1(
676 scoped_ptr<base::DictionaryValue> api_features2(new base::DictionaryValue()); 682 new base::DictionaryValue());
683 std::unique_ptr<base::DictionaryValue> api_features2(
684 new base::DictionaryValue());
677 base::DictionaryValue* test1 = new base::DictionaryValue(); 685 base::DictionaryValue* test1 = new base::DictionaryValue();
678 base::DictionaryValue* test2 = new base::DictionaryValue(); 686 base::DictionaryValue* test2 = new base::DictionaryValue();
679 base::ListValue* contexts = new base::ListValue(); 687 base::ListValue* contexts = new base::ListValue();
680 contexts->Append(new base::StringValue("content_script")); 688 contexts->Append(new base::StringValue("content_script"));
681 test1->Set("contexts", contexts); 689 test1->Set("contexts", contexts);
682 test1->SetString("channel", "stable"); 690 test1->SetString("channel", "stable");
683 test2->SetString("channel", "stable"); 691 test2->SetString("channel", "stable");
684 api_features1->Set("test", test1); 692 api_features1->Set("test", test1);
685 api_features2->Set("test", test2); 693 api_features2->Set("test", test2);
686 694
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 { "app.getIsInstalled", true }, 816 { "app.getIsInstalled", true },
809 { "app.installState", true }, 817 { "app.installState", true },
810 { "app.runningState", true }, 818 { "app.runningState", true },
811 { "management.getPermissionWarningsByManifest", true }, 819 { "management.getPermissionWarningsByManifest", true },
812 { "management.uninstallSelf", true }, 820 { "management.uninstallSelf", true },
813 // But other functions in those modules do. 821 // But other functions in those modules do.
814 { "management.getPermissionWarningsById", false }, 822 { "management.getPermissionWarningsById", false },
815 { "runtime.connectNative", false }, 823 { "runtime.connectNative", false },
816 }; 824 };
817 825
818 scoped_ptr<ExtensionAPI> extension_api( 826 std::unique_ptr<ExtensionAPI> extension_api(
819 ExtensionAPI::CreateWithDefaultConfiguration()); 827 ExtensionAPI::CreateWithDefaultConfiguration());
820 scoped_refptr<Extension> extension = 828 scoped_refptr<Extension> extension =
821 BuildExtension(ExtensionBuilder()).Build(); 829 BuildExtension(ExtensionBuilder()).Build();
822 830
823 for (size_t i = 0; i < arraysize(kTests); ++i) { 831 for (size_t i = 0; i < arraysize(kTests); ++i) {
824 EXPECT_EQ(kTests[i].expect_success, 832 EXPECT_EQ(kTests[i].expect_success,
825 extension_api->IsAvailable(kTests[i].permission_name, 833 extension_api->IsAvailable(kTests[i].permission_name,
826 extension.get(), 834 extension.get(),
827 Feature::BLESSED_EXTENSION_CONTEXT, 835 Feature::BLESSED_EXTENSION_CONTEXT,
828 GURL()).is_available()) 836 GURL()).is_available())
829 << "Permission being tested: " << kTests[i].permission_name; 837 << "Permission being tested: " << kTests[i].permission_name;
830 } 838 }
831 } 839 }
832 840
833 // Tests that permissions that require manifest keys are available when those 841 // Tests that permissions that require manifest keys are available when those
834 // keys are present. 842 // keys are present.
835 TEST(ExtensionAPITest, ManifestKeys) { 843 TEST(ExtensionAPITest, ManifestKeys) {
836 scoped_ptr<ExtensionAPI> extension_api( 844 std::unique_ptr<ExtensionAPI> extension_api(
837 ExtensionAPI::CreateWithDefaultConfiguration()); 845 ExtensionAPI::CreateWithDefaultConfiguration());
838 846
839 scoped_refptr<Extension> extension = 847 scoped_refptr<Extension> extension =
840 BuildExtension(ExtensionBuilder()) 848 BuildExtension(ExtensionBuilder())
841 .MergeManifest(DictionaryBuilder() 849 .MergeManifest(DictionaryBuilder()
842 .Set("browser_action", DictionaryBuilder().Build()) 850 .Set("browser_action", DictionaryBuilder().Build())
843 .Build()) 851 .Build())
844 .Build(); 852 .Build();
845 853
846 EXPECT_TRUE(extension_api->IsAvailable("browserAction", 854 EXPECT_TRUE(extension_api->IsAvailable("browserAction",
847 extension.get(), 855 extension.get(),
848 Feature::BLESSED_EXTENSION_CONTEXT, 856 Feature::BLESSED_EXTENSION_CONTEXT,
849 GURL()).is_available()); 857 GURL()).is_available());
850 EXPECT_FALSE(extension_api->IsAvailable("pageAction", 858 EXPECT_FALSE(extension_api->IsAvailable("pageAction",
851 extension.get(), 859 extension.get(),
852 Feature::BLESSED_EXTENSION_CONTEXT, 860 Feature::BLESSED_EXTENSION_CONTEXT,
853 GURL()).is_available()); 861 GURL()).is_available());
854 } 862 }
855 863
856 } // namespace extensions 864 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/common/extensions/api/commands/commands_handler.cc ('k') | chrome/common/extensions/api/extension_action/action_info.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698