| OLD | NEW | 
|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/files/file_util.h" | 5 #include "base/files/file_util.h" | 
|  | 6 #include "base/strings/utf_string_conversions.h" | 
| 6 #include "chrome/browser/extensions/api/developer_private/developer_private_api.
     h" | 7 #include "chrome/browser/extensions/api/developer_private/developer_private_api.
     h" | 
| 7 #include "chrome/browser/extensions/extension_function_test_utils.h" | 8 #include "chrome/browser/extensions/extension_function_test_utils.h" | 
| 8 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" | 
| 9 #include "chrome/browser/extensions/extension_service_test_base.h" | 10 #include "chrome/browser/extensions/extension_service_test_base.h" | 
| 10 #include "chrome/browser/extensions/extension_util.h" | 11 #include "chrome/browser/extensions/extension_util.h" | 
| 11 #include "chrome/browser/extensions/test_extension_dir.h" | 12 #include "chrome/browser/extensions/test_extension_dir.h" | 
| 12 #include "chrome/browser/extensions/test_extension_system.h" | 13 #include "chrome/browser/extensions/test_extension_system.h" | 
| 13 #include "chrome/browser/extensions/unpacked_installer.h" | 14 #include "chrome/browser/extensions/unpacked_installer.h" | 
| 14 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" | 
| 15 #include "chrome/browser/ui/host_desktop.h" | 16 #include "chrome/browser/ui/host_desktop.h" | 
| 16 #include "chrome/common/extensions/api/developer_private.h" | 17 #include "chrome/common/extensions/api/developer_private.h" | 
|  | 18 #include "chrome/common/pref_names.h" | 
| 17 #include "chrome/test/base/test_browser_window.h" | 19 #include "chrome/test/base/test_browser_window.h" | 
|  | 20 #include "components/crx_file/id_util.h" | 
| 18 #include "content/public/test/test_web_contents_factory.h" | 21 #include "content/public/test/test_web_contents_factory.h" | 
| 19 #include "extensions/browser/extension_prefs.h" | 22 #include "extensions/browser/extension_prefs.h" | 
| 20 #include "extensions/browser/extension_registry.h" | 23 #include "extensions/browser/extension_registry.h" | 
| 21 #include "extensions/browser/extension_system.h" | 24 #include "extensions/browser/extension_system.h" | 
| 22 #include "extensions/browser/test_extension_registry_observer.h" | 25 #include "extensions/browser/test_extension_registry_observer.h" | 
| 23 #include "extensions/common/extension.h" | 26 #include "extensions/common/extension.h" | 
|  | 27 #include "extensions/common/extension_builder.h" | 
| 24 #include "extensions/common/extension_set.h" | 28 #include "extensions/common/extension_set.h" | 
|  | 29 #include "extensions/common/feature_switch.h" | 
| 25 #include "extensions/common/manifest_constants.h" | 30 #include "extensions/common/manifest_constants.h" | 
| 26 #include "extensions/common/test_util.h" | 31 #include "extensions/common/test_util.h" | 
|  | 32 #include "extensions/common/value_builder.h" | 
| 27 | 33 | 
| 28 namespace extensions { | 34 namespace extensions { | 
| 29 | 35 | 
| 30 namespace { | 36 namespace { | 
| 31 | 37 | 
| 32 KeyedService* BuildAPI(content::BrowserContext* context) { | 38 KeyedService* BuildAPI(content::BrowserContext* context) { | 
| 33   return new DeveloperPrivateAPI(context); | 39   return new DeveloperPrivateAPI(context); | 
| 34 } | 40 } | 
| 35 | 41 | 
| 36 }  // namespace | 42 }  // namespace | 
| (...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 397   scoped_ptr<api::developer_private::RequestFileSourceResponse> response = | 403   scoped_ptr<api::developer_private::RequestFileSourceResponse> response = | 
| 398       api::developer_private::RequestFileSourceResponse::FromValue( | 404       api::developer_private::RequestFileSourceResponse::FromValue( | 
| 399           *response_value); | 405           *response_value); | 
| 400   EXPECT_FALSE(response->before_highlight.empty()); | 406   EXPECT_FALSE(response->before_highlight.empty()); | 
| 401   EXPECT_EQ("\"name\": \"foo\"", response->highlight); | 407   EXPECT_EQ("\"name\": \"foo\"", response->highlight); | 
| 402   EXPECT_FALSE(response->after_highlight.empty()); | 408   EXPECT_FALSE(response->after_highlight.empty()); | 
| 403   EXPECT_EQ("foo: manifest.json", response->title); | 409   EXPECT_EQ("foo: manifest.json", response->title); | 
| 404   EXPECT_EQ(kErrorMessage, response->message); | 410   EXPECT_EQ(kErrorMessage, response->message); | 
| 405 } | 411 } | 
| 406 | 412 | 
|  | 413 // Test developerPrivate.getExtensionsInfo. | 
|  | 414 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateGetExtensionsInfo) { | 
|  | 415   // Enable error console for testing. | 
|  | 416   ResetThreadBundle(content::TestBrowserThreadBundle::DEFAULT); | 
|  | 417   FeatureSwitch::ScopedOverride error_console_override( | 
|  | 418       FeatureSwitch::error_console(), true); | 
|  | 419   profile()->GetPrefs()->SetBoolean(prefs::kExtensionsUIDeveloperMode, true); | 
|  | 420 | 
|  | 421   const char kName[] = "extension name"; | 
|  | 422   const char kVersion[] = "1.0.0.1"; | 
|  | 423   std::string id = crx_file::id_util::GenerateId(kName); | 
|  | 424   DictionaryBuilder manifest; | 
|  | 425   manifest.Set("name", kName) | 
|  | 426           .Set("version", kVersion) | 
|  | 427           .Set("manifest_version", 2) | 
|  | 428           .Set("description", "an extension") | 
|  | 429           .Set("permissions", ListBuilder().Append("file://*/*")); | 
|  | 430   scoped_refptr<const Extension> extension = | 
|  | 431       ExtensionBuilder().SetManifest(manifest) | 
|  | 432                         .SetLocation(Manifest::UNPACKED) | 
|  | 433                         .SetPath(data_dir()) | 
|  | 434                         .SetID(id) | 
|  | 435                         .Build(); | 
|  | 436   service()->AddExtension(extension.get()); | 
|  | 437   ErrorConsole* error_console = ErrorConsole::Get(profile()); | 
|  | 438   error_console->ReportError( | 
|  | 439       make_scoped_ptr(new RuntimeError( | 
|  | 440           extension->id(), | 
|  | 441           false, | 
|  | 442           base::UTF8ToUTF16("source"), | 
|  | 443           base::UTF8ToUTF16("message"), | 
|  | 444           StackTrace(1, StackFrame(1, | 
|  | 445                                    1, | 
|  | 446                                    base::UTF8ToUTF16("source"), | 
|  | 447                                    base::UTF8ToUTF16("function"))), | 
|  | 448           GURL("url"), | 
|  | 449           logging::LOG_ERROR, | 
|  | 450           1, | 
|  | 451           1))); | 
|  | 452   error_console->ReportError( | 
|  | 453       make_scoped_ptr(new ManifestError(extension->id(), | 
|  | 454                                         base::UTF8ToUTF16("message"), | 
|  | 455                                         base::UTF8ToUTF16("key"), | 
|  | 456                                         base::string16()))); | 
|  | 457 | 
|  | 458   // It's not feasible to validate every field here, because that would be | 
|  | 459   // a duplication of the logic in the method itself. Instead, test a handful | 
|  | 460   // of fields, and, more importantly, check that we return something reasonable | 
|  | 461   // (which is done through the serialization/deserialization that happens). | 
|  | 462   scoped_refptr<UIThreadExtensionFunction> function( | 
|  | 463       new api::DeveloperPrivateGetExtensionsInfoFunction()); | 
|  | 464   EXPECT_TRUE(RunFunction(function, base::ListValue())) << function->GetError(); | 
|  | 465   const base::ListValue* results = function->GetResultList(); | 
|  | 466   ASSERT_EQ(1u, results->GetSize()); | 
|  | 467   const base::ListValue* list = nullptr; | 
|  | 468   ASSERT_TRUE(results->GetList(0u, &list)); | 
|  | 469   ASSERT_EQ(1u, list->GetSize()); | 
|  | 470   const base::Value* value = nullptr; | 
|  | 471   ASSERT_TRUE(list->Get(0u, &value)); | 
|  | 472   scoped_ptr<api::developer_private::ExtensionInfo> info = | 
|  | 473       api::developer_private::ExtensionInfo::FromValue(*value); | 
|  | 474   ASSERT_TRUE(info); | 
|  | 475   EXPECT_EQ(kName, info->name); | 
|  | 476   EXPECT_EQ(id, info->id); | 
|  | 477   EXPECT_EQ(kVersion, info->version); | 
|  | 478   EXPECT_EQ(api::developer_private::EXTENSION_STATE_ENABLED, info->state); | 
|  | 479   EXPECT_EQ(api::developer_private::EXTENSION_TYPE_EXTENSION, info->type); | 
|  | 480   EXPECT_TRUE(info->file_access.is_enabled); | 
|  | 481   EXPECT_FALSE(info->file_access.is_active); | 
|  | 482   EXPECT_TRUE(info->incognito_access.is_enabled); | 
|  | 483   EXPECT_FALSE(info->incognito_access.is_active); | 
|  | 484   ASSERT_EQ(1u, info->runtime_errors.size()); | 
|  | 485   const api::developer_private::RuntimeError& runtime_error = | 
|  | 486       *info->runtime_errors[0]; | 
|  | 487   EXPECT_EQ(extension->id(), runtime_error.extension_id); | 
|  | 488   EXPECT_EQ(api::developer_private::ERROR_TYPE_RUNTIME, runtime_error.type); | 
|  | 489   EXPECT_EQ(api::developer_private::ERROR_LEVEL_ERROR, | 
|  | 490             runtime_error.severity); | 
|  | 491   EXPECT_EQ(1u, runtime_error.stack_trace.size()); | 
|  | 492   ASSERT_EQ(1u, info->manifest_errors.size()); | 
|  | 493   const api::developer_private::ManifestError& manifest_error = | 
|  | 494       *info->manifest_errors[0]; | 
|  | 495   EXPECT_EQ(extension->id(), manifest_error.extension_id); | 
|  | 496 | 
|  | 497   // As a sanity check, also run the GetItemsInfo and make sure it returns a | 
|  | 498   // sane value. | 
|  | 499   function = new api::DeveloperPrivateGetItemsInfoFunction(); | 
|  | 500   base::ListValue args; | 
|  | 501   args.AppendBoolean(false); | 
|  | 502   args.AppendBoolean(false); | 
|  | 503   EXPECT_TRUE(RunFunction(function, args)) << function->GetError(); | 
|  | 504   results = function->GetResultList(); | 
|  | 505   ASSERT_EQ(1u, results->GetSize()); | 
|  | 506   ASSERT_TRUE(results->GetList(0u, &list)); | 
|  | 507   ASSERT_EQ(1u, list->GetSize()); | 
|  | 508   ASSERT_TRUE(list->Get(0u, &value)); | 
|  | 509   scoped_ptr<api::developer_private::ItemInfo> item_info = | 
|  | 510       api::developer_private::ItemInfo::FromValue(*value); | 
|  | 511   ASSERT_TRUE(item_info); | 
|  | 512 } | 
|  | 513 | 
| 407 }  // namespace extensions | 514 }  // namespace extensions | 
| OLD | NEW | 
|---|