1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/apps/app_browsertest_util.h" 6 #include "chrome/browser/extensions/api/management/management_api.h" 7 #include "chrome/browser/extensions/extension_apitest.h" 8 #include "chrome/browser/extensions/extension_function_test_utils.h" 9 #include "chrome/browser/extensions/test_extension_dir.h" 10 #include "chrome/test/base/ui_test_utils.h" 11 #include "content/public/browser/notification_service.h" 12 #include "extensions/browser/api/runtime/runtime_api.h" 13 #include "extensions/browser/extension_registry.h" 14 #include "net/test/embedded_test_server/embedded_test_server.h" 15 16 // Tests the privileged components of chrome.runtime. 17 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeRuntimePrivileged) { 18 ASSERT_TRUE(RunExtensionTest("runtime/privileged")) << message_; 19 } 20 21 // Tests the unprivileged components of chrome.runtime. 22 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeRuntimeUnprivileged) { 23 ASSERT_TRUE(StartEmbeddedTestServer()); 24 ASSERT_TRUE( 25 LoadExtension(test_data_dir_.AppendASCII("runtime/content_script"))); 26 27 // The content script runs on webpage.html. 28 ResultCatcher catcher; 29 ui_test_utils::NavigateToURL(browser(), 30 embedded_test_server()->GetURL("/webpage.html")); 31 EXPECT_TRUE(catcher.GetNextResult()) << message_; 32 } 33 34 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeRuntimeUninstallURL) { 35 // Auto-confirm the uninstall dialog. 36 extensions::ManagementUninstallFunction::SetAutoConfirmForTest(true); 37 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("runtime") 38 .AppendASCII("uninstall_url") 39 .AppendASCII("sets_uninstall_url"))); 40 ASSERT_TRUE(RunExtensionTest("runtime/uninstall_url")) << message_; 41 } 42 43 namespace extensions { 44 45 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeRuntimeGetPlatformInfo) { 46 scoped_ptr<base::Value> result( 47 extension_function_test_utils::RunFunctionAndReturnSingleResult( 48 new RuntimeGetPlatformInfoFunction(), "[]", browser())); 49 ASSERT_TRUE(result.get() != NULL); 50 base::DictionaryValue* dict = 51 extension_function_test_utils::ToDictionary(result.get()); 52 ASSERT_TRUE(dict != NULL); 53 EXPECT_TRUE(dict->HasKey("os")); 54 EXPECT_TRUE(dict->HasKey("arch")); 55 EXPECT_TRUE(dict->HasKey("nacl_arch")); 56 } 57 58 // Tests chrome.runtime.getPackageDirectory with an app. 59 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, 60 ChromeRuntimeGetPackageDirectoryEntryApp) { 61 ClearCommandLineArgs(); 62 ASSERT_TRUE(RunPlatformAppTest("api_test/runtime/get_package_directory/app")) 63 << message_; 64 } 65 66 // Tests chrome.runtime.getPackageDirectory with an extension. 67 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, 68 ChromeRuntimeGetPackageDirectoryEntryExtension) { 69 ASSERT_TRUE(RunExtensionTest("runtime/get_package_directory/extension")) 70 << message_; 71 } 72 73 // Tests chrome.runtime.reload 74 // This test is flaky on Linux: crbug.com/366181 75 #if defined(OS_LINUX) || defined(OS_CHROMEOS) 76 #define MAYBE_ChromeRuntimeReload DISABLED_ChromeRuntimeReload 77 #else 78 #define MAYBE_ChromeRuntimeReload ChromeRuntimeReload 79 #endif 80 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_ChromeRuntimeReload) { 81 ExtensionRegistry* registry = ExtensionRegistry::Get(profile()); 82 const char kManifest[] = 83 "{" 84 " \"name\": \"reload\"," 85 " \"version\": \"1.0\"," 86 " \"background\": {" 87 " \"scripts\": [\"background.js\"]" 88 " }," 89 " \"manifest_version\": 2" 90 "}"; 91 92 TestExtensionDir dir; 93 dir.WriteManifest(kManifest); 94 dir.WriteFile(FILE_PATH_LITERAL("background.js"), "console.log('loaded');"); 95 96 const Extension* extension = LoadExtension(dir.unpacked_path()); 97 ASSERT_TRUE(extension); 98 const std::string extension_id = extension->id(); 99 100 // Somewhat arbitrary upper limit of 30 iterations. If the extension manages 101 // to reload itself that often without being terminated, the test fails 102 // anyway. 103 for (int i = 0; i < 30; i++) { 104 content::WindowedNotificationObserver unload_observer( 105 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, 106 content::NotificationService::AllSources()); 107 content::WindowedNotificationObserver load_observer( 108 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, 109 content::NotificationService::AllSources()); 110 111 ASSERT_TRUE(ExecuteScriptInBackgroundPageNoWait( 112 extension_id, "chrome.runtime.reload();")); 113 unload_observer.Wait(); 114 115 if (registry->GetExtensionById(extension_id, 116 ExtensionRegistry::TERMINATED)) { 117 break; 118 } else { 119 load_observer.Wait(); 120 WaitForExtensionViewsToLoad(); 121 } 122 } 123 ASSERT_TRUE( 124 registry->GetExtensionById(extension_id, ExtensionRegistry::TERMINATED)); 125 } 126 127 } // namespace extensions 128