blob: 4bcf96854f14f7afbbb1735ae22a750f44d9d59e [file] [log] [blame]
[email protected]bcabac762013-05-29 23:33:241// 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
[email protected]bcabac762013-05-29 23:33:248#include "base/run_loop.h"
[email protected]44580842013-07-09 21:36:539#include "base/strings/stringprintf.h"
[email protected]ca975942014-01-07 12:06:4710#include "base/version.h"
[email protected]bcabac762013-05-29 23:33:2411#include "chrome/browser/extensions/extension_browsertest.h"
[email protected]44580842013-07-09 21:36:5312#include "chrome/browser/extensions/test_extension_dir.h"
[email protected]ca975942014-01-07 12:06:4713#include "chrome/browser/profiles/profile.h"
[email protected]bcabac762013-05-29 23:33:2414#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"
[email protected]ca975942014-01-07 12:06:4717#include "extensions/browser/extension_registry.h"
[email protected]bcabac762013-05-29 23:33:2418#include "net/test/embedded_test_server/embedded_test_server.h"
19#include "testing/gmock/include/gmock/gmock.h"
20
21namespace extensions {
22namespace {
23
[email protected]bcabac762013-05-29 23:33:2424class 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;
thestig4b36dd32014-10-31 20:30:1935 const char manifest_template[] =
[email protected]44580842013-07-09 21:36:5336 "{"
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));
[email protected]bcabac762013-05-29 23:33:2450 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.
[email protected]44580842013-07-09 21:36:5371 extension_dir.WriteManifest(base::StringPrintf(manifest_template, 2));
[email protected]bcabac762013-05-29 23:33:2472
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/"),
Sylvain Defresnec6ccc77d2014-09-19 10:19:3583 ui::PAGE_TRANSITION_TYPED);
[email protected]bcabac762013-05-29 23:33:2484
85 // Check that the extension hasn't crashed.
[email protected]ca975942014-01-07 12:06:4786 ExtensionRegistry* registry = ExtensionRegistry::Get(profile());
87 EXPECT_EQ(0U, registry->terminated_extensions().size());
88 EXPECT_TRUE(registry->enabled_extensions().Contains(new_tab_extension->id()));
[email protected]bcabac762013-05-29 23:33:2489}
90
91} // namespace
92} // namespace extensions