extension_loading_browsertest.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// This file contains tests for extension loading, reloading, and
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// unloading behavior.
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/run_loop.h"
97dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "base/strings/stringprintf.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/version.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/extensions/extension_browsertest.h"
127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/extensions/test_extension_dir.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/ui/tabs/tab_strip_model.h"
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/test/base/in_process_browser_test.h"
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/test/base/ui_test_utils.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/browser/extension_registry.h"
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "net/test/embedded_test_server/embedded_test_server.h"
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace extensions {
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace {
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class ExtensionLoadingTest : public ExtensionBrowserTest {
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Check the fix for http://crbug.com/178542.
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(ExtensionLoadingTest,
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                       UpgradeAfterNavigatingFromOverriddenNewTabPage) {
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  embedded_test_server()->ServeFilesFromDirectory(
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  TestExtensionDir extension_dir;
357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  const char* manifest_template =
367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      "{"
377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      "  \"name\": \"Overrides New Tab\","
387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      "  \"version\": \"%d\","
397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      "  \"description\": \"Overrides New Tab\","
407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      "  \"manifest_version\": 2,"
417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      "  \"background\": {"
427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      "    \"persistent\": false,"
437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      "    \"scripts\": [\"event.js\"]"
447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      "  },"
457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      "  \"chrome_url_overrides\": {"
467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      "    \"newtab\": \"newtab.html\""
477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      "  }"
487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      "}";
497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  extension_dir.WriteManifest(base::StringPrintf(manifest_template, 1));
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  extension_dir.WriteFile(FILE_PATH_LITERAL("event.js"), "");
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  extension_dir.WriteFile(FILE_PATH_LITERAL("newtab.html"),
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                          "<h1>Overridden New Tab Page</h1>");
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const Extension* new_tab_extension =
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      InstallExtension(extension_dir.Pack(), 1 /*new install*/);
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_TRUE(new_tab_extension);
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Visit the New Tab Page to get a renderer using the extension into history.
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab"));
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Navigate that tab to a non-extension URL to swap out the extension's
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // renderer.
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const GURL test_link_from_NTP =
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      embedded_test_server()->GetURL("/README.chromium");
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_THAT(test_link_from_NTP.spec(), testing::EndsWith("/README.chromium"))
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      << "Check that the test server started.";
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  NavigateInRenderer(browser()->tab_strip_model()->GetActiveWebContents(),
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                     test_link_from_NTP);
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Increase the extension's version.
717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  extension_dir.WriteManifest(base::StringPrintf(manifest_template, 2));
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Upgrade the extension.
74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  new_tab_extension = UpdateExtension(
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      new_tab_extension->id(), extension_dir.Pack(), 0 /*expected upgrade*/);
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_THAT(new_tab_extension->version()->components(),
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)              testing::ElementsAre(2));
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // The extension takes a couple round-trips to the renderer in order
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // to crash, so open a new tab to wait long enough.
81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  AddTabAtIndex(browser()->tab_strip_model()->count(),
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                GURL("http://www.google.com/"),
831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                ui::PAGE_TRANSITION_TYPED);
84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Check that the extension hasn't crashed.
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ExtensionRegistry* registry = ExtensionRegistry::Get(profile());
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(0U, registry->terminated_extensions().size());
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(registry->enabled_extensions().Contains(new_tab_extension->id()));
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace
92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace extensions
93