Implementation of leveldb-backed PrefStore.
[chromium-blink-merge.git] / extensions / browser / api / runtime / runtime_apitest.cc
blob6fd81f1b02be372c8b0eb46a4b71255574b8296b
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/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"
16 // Tests the privileged components of chrome.runtime.
17 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeRuntimePrivileged) {
18 ASSERT_TRUE(RunExtensionTest("runtime/privileged")) << message_;
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")));
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_;
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_;
43 namespace extensions {
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"));
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_;
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_;
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 "}";
92 TestExtensionDir dir;
93 dir.WriteManifest(kManifest);
94 dir.WriteFile(FILE_PATH_LITERAL("background.js"), "console.log('loaded');");
96 const Extension* extension = LoadExtension(dir.unpacked_path());
97 ASSERT_TRUE(extension);
98 const std::string extension_id = extension->id();
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());
111 ASSERT_TRUE(ExecuteScriptInBackgroundPageNoWait(
112 extension_id, "chrome.runtime.reload();"));
113 unload_observer.Wait();
115 if (registry->GetExtensionById(extension_id,
116 ExtensionRegistry::TERMINATED)) {
117 break;
118 } else {
119 load_observer.Wait();
120 WaitForExtensionViewsToLoad();
123 ASSERT_TRUE(
124 registry->GetExtensionById(extension_id, ExtensionRegistry::TERMINATED));
127 } // namespace extensions