1010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "chrome/browser/apps/app_browsertest_util.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/extensions/api/management/management_api.h"
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/extension_apitest.h"
8b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "chrome/browser/extensions/extension_function_test_utils.h"
90529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "chrome/browser/extensions/test_extension_dir.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/test/base/ui_test_utils.h"
110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "content/public/browser/notification_service.h"
12010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "extensions/browser/api/runtime/runtime_api.h"
130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "extensions/browser/extension_registry.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/test/result_catcher.h"
15eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "net/test/embedded_test_server/embedded_test_server.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests the privileged components of chrome.runtime.
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeRuntimePrivileged) {
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_TRUE(RunExtensionTest("runtime/privileged")) << message_;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests the unprivileged components of chrome.runtime.
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeRuntimeUnprivileged) {
24eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  ASSERT_TRUE(StartEmbeddedTestServer());
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_TRUE(
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LoadExtension(test_data_dir_.AppendASCII("runtime/content_script")));
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The content script runs on webpage.html.
291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  extensions::ResultCatcher catcher;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ui_test_utils::NavigateToURL(browser(),
31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                               embedded_test_server()->GetURL("/webpage.html"));
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(catcher.GetNextResult()) << message_;
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
34b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeRuntimeUninstallURL) {
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Auto-confirm the uninstall dialog.
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  extensions::ManagementUninstallFunction::SetAutoConfirmForTest(true);
38010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("runtime")
39010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                .AppendASCII("uninstall_url")
40010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                .AppendASCII("sets_uninstall_url")));
4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ASSERT_TRUE(RunExtensionTest("runtime/uninstall_url")) << message_;
4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
44b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)namespace extensions {
45b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
46b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeRuntimeGetPlatformInfo) {
47b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  scoped_ptr<base::Value> result(
48b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      extension_function_test_utils::RunFunctionAndReturnSingleResult(
49010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)          new RuntimeGetPlatformInfoFunction(), "[]", browser()));
50b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  ASSERT_TRUE(result.get() != NULL);
51b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  base::DictionaryValue* dict =
52b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      extension_function_test_utils::ToDictionary(result.get());
53b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  ASSERT_TRUE(dict != NULL);
54b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  EXPECT_TRUE(dict->HasKey("os"));
55b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  EXPECT_TRUE(dict->HasKey("arch"));
56b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  EXPECT_TRUE(dict->HasKey("nacl_arch"));
57b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
58b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Tests chrome.runtime.getPackageDirectory with an app.
60424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest,
617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                       ChromeRuntimeGetPackageDirectoryEntryApp) {
62424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  ClearCommandLineArgs();
63424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  ASSERT_TRUE(RunPlatformAppTest("api_test/runtime/get_package_directory/app"))
647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      << message_;
657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Tests chrome.runtime.getPackageDirectory with an extension.
687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(ExtensionApiTest,
697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                       ChromeRuntimeGetPackageDirectoryEntryExtension) {
707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  ASSERT_TRUE(RunExtensionTest("runtime/get_package_directory/extension"))
717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      << message_;
727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// Tests chrome.runtime.reload
755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// This test is flaky: crbug.com/366181
765f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_ChromeRuntimeReload) {
770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  ExtensionRegistry* registry = ExtensionRegistry::Get(profile());
780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  const char kManifest[] =
790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      "{"
800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      "  \"name\": \"reload\","
810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      "  \"version\": \"1.0\","
820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      "  \"background\": {"
830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      "    \"scripts\": [\"background.js\"]"
840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      "  },"
850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      "  \"manifest_version\": 2"
860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      "}";
870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  TestExtensionDir dir;
890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  dir.WriteManifest(kManifest);
900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  dir.WriteFile(FILE_PATH_LITERAL("background.js"), "console.log('loaded');");
910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  const Extension* extension = LoadExtension(dir.unpacked_path());
930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  ASSERT_TRUE(extension);
940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  const std::string extension_id = extension->id();
950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Somewhat arbitrary upper limit of 30 iterations. If the extension manages
970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // to reload itself that often without being terminated, the test fails
980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // anyway.
990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  for (int i = 0; i < 30; i++) {
1000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    content::WindowedNotificationObserver unload_observer(
1015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
1020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        content::NotificationService::AllSources());
1030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    content::WindowedNotificationObserver load_observer(
1045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
1050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        content::NotificationService::AllSources());
1060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    ASSERT_TRUE(ExecuteScriptInBackgroundPageNoWait(
1080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        extension_id, "chrome.runtime.reload();"));
1090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    unload_observer.Wait();
1100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (registry->GetExtensionById(extension_id,
1120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                   ExtensionRegistry::TERMINATED)) {
1130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      break;
1140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    } else {
1150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      load_observer.Wait();
1160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      WaitForExtensionViewsToLoad();
1170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
1180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
1190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  ASSERT_TRUE(
1200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      registry->GetExtensionById(extension_id, ExtensionRegistry::TERMINATED));
1210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
1220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
123b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}  // namespace extensions
124