extension_loading_browsertest.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
1// Copyright 2013 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// This file contains tests for extension loading, reloading, and
6// unloading behavior.
7
8#include "base/run_loop.h"
9#include "base/strings/stringprintf.h"
10#include "base/version.h"
11#include "chrome/browser/extensions/extension_browsertest.h"
12#include "chrome/browser/extensions/test_extension_dir.h"
13#include "chrome/browser/profiles/profile.h"
14#include "chrome/browser/ui/tabs/tab_strip_model.h"
15#include "chrome/test/base/in_process_browser_test.h"
16#include "chrome/test/base/ui_test_utils.h"
17#include "extensions/browser/extension_registry.h"
18#include "net/test/embedded_test_server/embedded_test_server.h"
19#include "testing/gmock/include/gmock/gmock.h"
20
21namespace extensions {
22namespace {
23
24class ExtensionLoadingTest : public ExtensionBrowserTest {
25};
26
27// Check the fix for http://crbug.com/178542.
28IN_PROC_BROWSER_TEST_F(ExtensionLoadingTest,
29                       UpgradeAfterNavigatingFromOverriddenNewTabPage) {
30  embedded_test_server()->ServeFilesFromDirectory(
31      base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
32  ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
33
34  TestExtensionDir extension_dir;
35  const char* manifest_template =
36      "{"
37      "  \"name\": \"Overrides New Tab\","
38      "  \"version\": \"%d\","
39      "  \"description\": \"Overrides New Tab\","
40      "  \"manifest_version\": 2,"
41      "  \"background\": {"
42      "    \"persistent\": false,"
43      "    \"scripts\": [\"event.js\"]"
44      "  },"
45      "  \"chrome_url_overrides\": {"
46      "    \"newtab\": \"newtab.html\""
47      "  }"
48      "}";
49  extension_dir.WriteManifest(base::StringPrintf(manifest_template, 1));
50  extension_dir.WriteFile(FILE_PATH_LITERAL("event.js"), "");
51  extension_dir.WriteFile(FILE_PATH_LITERAL("newtab.html"),
52                          "<h1>Overridden New Tab Page</h1>");
53
54  const Extension* new_tab_extension =
55      InstallExtension(extension_dir.Pack(), 1 /*new install*/);
56  ASSERT_TRUE(new_tab_extension);
57
58  // Visit the New Tab Page to get a renderer using the extension into history.
59  ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab"));
60
61  // Navigate that tab to a non-extension URL to swap out the extension's
62  // renderer.
63  const GURL test_link_from_NTP =
64      embedded_test_server()->GetURL("/README.chromium");
65  EXPECT_THAT(test_link_from_NTP.spec(), testing::EndsWith("/README.chromium"))
66      << "Check that the test server started.";
67  NavigateInRenderer(browser()->tab_strip_model()->GetActiveWebContents(),
68                     test_link_from_NTP);
69
70  // Increase the extension's version.
71  extension_dir.WriteManifest(base::StringPrintf(manifest_template, 2));
72
73  // Upgrade the extension.
74  new_tab_extension = UpdateExtension(
75      new_tab_extension->id(), extension_dir.Pack(), 0 /*expected upgrade*/);
76  EXPECT_THAT(new_tab_extension->version()->components(),
77              testing::ElementsAre(2));
78
79  // The extension takes a couple round-trips to the renderer in order
80  // to crash, so open a new tab to wait long enough.
81  AddTabAtIndex(browser()->tab_strip_model()->count(),
82                GURL("http://www.google.com/"),
83                ui::PAGE_TRANSITION_TYPED);
84
85  // Check that the extension hasn't crashed.
86  ExtensionRegistry* registry = ExtensionRegistry::Get(profile());
87  EXPECT_EQ(0U, registry->terminated_extensions().size());
88  EXPECT_TRUE(registry->enabled_extensions().Contains(new_tab_extension->id()));
89}
90
91}  // namespace
92}  // namespace extensions
93