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.
5 #include "chrome/browser/apps/app_browsertest_util.h"
6 #include "chrome/browser/extensions/extension_apitest.h"
7 #include "chrome/browser/extensions/extension_function_test_utils.h"
8 #include "chrome/browser/extensions/test_extension_dir.h"
9 #include "chrome/test/base/ui_test_utils.h"
10 #include "content/public/browser/notification_service.h"
11 #include "extensions/browser/api/runtime/runtime_api.h"
12 #include "extensions/browser/extension_dialog_auto_confirm.h"
13 #include "extensions/browser/extension_registry.h"
14 #include "extensions/test/result_catcher.h"
15 #include "net/test/embedded_test_server/embedded_test_server.h"
17 // Tests the privileged components of chrome.runtime.
18 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, ChromeRuntimePrivileged
) {
19 ASSERT_TRUE(RunExtensionTest("runtime/privileged")) << message_
;
22 // Tests the unprivileged components of chrome.runtime.
23 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, ChromeRuntimeUnprivileged
) {
24 ASSERT_TRUE(StartEmbeddedTestServer());
26 LoadExtension(test_data_dir_
.AppendASCII("runtime/content_script")));
28 // The content script runs on webpage.html.
29 extensions::ResultCatcher catcher
;
30 ui_test_utils::NavigateToURL(browser(),
31 embedded_test_server()->GetURL("/webpage.html"));
32 EXPECT_TRUE(catcher
.GetNextResult()) << message_
;
35 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, ChromeRuntimeUninstallURL
) {
36 // Auto-confirm the uninstall dialog.
37 extensions::ScopedTestDialogAutoConfirm
auto_confirm(
38 extensions::ScopedTestDialogAutoConfirm::ACCEPT
);
39 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("runtime")
40 .AppendASCII("uninstall_url")
41 .AppendASCII("sets_uninstall_url")));
42 ASSERT_TRUE(RunExtensionTest("runtime/uninstall_url")) << message_
;
45 namespace extensions
{
47 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, ChromeRuntimeOpenOptionsPage
) {
48 ASSERT_TRUE(RunExtensionTest("runtime/open_options_page"));
51 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, ChromeRuntimeOpenOptionsPageError
) {
52 ASSERT_TRUE(RunExtensionTest("runtime/open_options_page_error"));
55 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, ChromeRuntimeGetPlatformInfo
) {
56 scoped_ptr
<base::Value
> result(
57 extension_function_test_utils::RunFunctionAndReturnSingleResult(
58 new RuntimeGetPlatformInfoFunction(), "[]", browser()));
59 ASSERT_TRUE(result
.get() != NULL
);
60 base::DictionaryValue
* dict
=
61 extension_function_test_utils::ToDictionary(result
.get());
62 ASSERT_TRUE(dict
!= NULL
);
63 EXPECT_TRUE(dict
->HasKey("os"));
64 EXPECT_TRUE(dict
->HasKey("arch"));
65 EXPECT_TRUE(dict
->HasKey("nacl_arch"));
68 // Tests chrome.runtime.getPackageDirectory with an app.
69 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest
,
70 ChromeRuntimeGetPackageDirectoryEntryApp
) {
71 ASSERT_TRUE(RunPlatformAppTest("api_test/runtime/get_package_directory/app"))
75 // Tests chrome.runtime.getPackageDirectory with an extension.
76 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
,
77 ChromeRuntimeGetPackageDirectoryEntryExtension
) {
78 ASSERT_TRUE(RunExtensionTest("runtime/get_package_directory/extension"))
82 // Tests chrome.runtime.reload
83 // This test is flaky: crbug.com/366181
84 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, DISABLED_ChromeRuntimeReload
) {
85 ExtensionRegistry
* registry
= ExtensionRegistry::Get(profile());
86 const char kManifest
[] =
88 " \"name\": \"reload\","
89 " \"version\": \"1.0\","
91 " \"scripts\": [\"background.js\"]"
93 " \"manifest_version\": 2"
97 dir
.WriteManifest(kManifest
);
98 dir
.WriteFile(FILE_PATH_LITERAL("background.js"), "console.log('loaded');");
100 const Extension
* extension
= LoadExtension(dir
.unpacked_path());
101 ASSERT_TRUE(extension
);
102 const std::string extension_id
= extension
->id();
104 // Somewhat arbitrary upper limit of 30 iterations. If the extension manages
105 // to reload itself that often without being terminated, the test fails
107 for (int i
= 0; i
< 30; i
++) {
108 content::WindowedNotificationObserver
unload_observer(
109 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
,
110 content::NotificationService::AllSources());
111 content::WindowedNotificationObserver
load_observer(
112 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
,
113 content::NotificationService::AllSources());
115 ASSERT_TRUE(ExecuteScriptInBackgroundPageNoWait(
116 extension_id
, "chrome.runtime.reload();"));
117 unload_observer
.Wait();
119 if (registry
->GetExtensionById(extension_id
,
120 ExtensionRegistry::TERMINATED
)) {
123 load_observer
.Wait();
124 WaitForExtensionViewsToLoad();
128 registry
->GetExtensionById(extension_id
, ExtensionRegistry::TERMINATED
));
131 } // namespace extensions