blob: 542462cf699b36d2b4ff05adbc97ea8f396fa208 [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"
magjedc3a0a602015-11-24 12:41:1411#include "base/win/windows_version.h"
avia2f4804a2015-12-24 23:11:1312#include "build/build_config.h"
rockota34fed222015-09-25 21:56:3113#include "chrome/browser/extensions/devtools_util.h"
[email protected]bcabac762013-05-29 23:33:2414#include "chrome/browser/extensions/extension_browsertest.h"
rockota34fed222015-09-25 21:56:3115#include "chrome/browser/extensions/extension_service.h"
[email protected]44580842013-07-09 21:36:5316#include "chrome/browser/extensions/test_extension_dir.h"
[email protected]ca975942014-01-07 12:06:4717#include "chrome/browser/profiles/profile.h"
[email protected]bcabac762013-05-29 23:33:2418#include "chrome/browser/ui/tabs/tab_strip_model.h"
19#include "chrome/test/base/in_process_browser_test.h"
20#include "chrome/test/base/ui_test_utils.h"
rockota34fed222015-09-25 21:56:3121#include "content/public/browser/devtools_agent_host.h"
[email protected]ca975942014-01-07 12:06:4722#include "extensions/browser/extension_registry.h"
rockota34fed222015-09-25 21:56:3123#include "extensions/browser/process_manager.h"
[email protected]bcabac762013-05-29 23:33:2424#include "net/test/embedded_test_server/embedded_test_server.h"
25#include "testing/gmock/include/gmock/gmock.h"
26
27namespace extensions {
28namespace {
29
[email protected]bcabac762013-05-29 23:33:2430class ExtensionLoadingTest : public ExtensionBrowserTest {
31};
32
33// Check the fix for http://crbug.com/178542.
34IN_PROC_BROWSER_TEST_F(ExtensionLoadingTest,
35 UpgradeAfterNavigatingFromOverriddenNewTabPage) {
36 embedded_test_server()->ServeFilesFromDirectory(
37 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
svaldeza01f7d92015-11-18 17:47:5638 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]bcabac762013-05-29 23:33:2439
40 TestExtensionDir extension_dir;
thestig4b36dd32014-10-31 20:30:1941 const char manifest_template[] =
[email protected]44580842013-07-09 21:36:5342 "{"
rockota34fed222015-09-25 21:56:3143 " 'name': 'Overrides New Tab',"
44 " 'version': '%d',"
45 " 'description': 'Overrides New Tab',"
46 " 'manifest_version': 2,"
47 " 'background': {"
48 " 'persistent': false,"
49 " 'scripts': ['event.js']"
[email protected]44580842013-07-09 21:36:5350 " },"
rockota34fed222015-09-25 21:56:3151 " 'chrome_url_overrides': {"
52 " 'newtab': 'newtab.html'"
[email protected]44580842013-07-09 21:36:5353 " }"
54 "}";
rockota34fed222015-09-25 21:56:3155 extension_dir.WriteManifestWithSingleQuotes(
56 base::StringPrintf(manifest_template, 1));
[email protected]bcabac762013-05-29 23:33:2457 extension_dir.WriteFile(FILE_PATH_LITERAL("event.js"), "");
58 extension_dir.WriteFile(FILE_PATH_LITERAL("newtab.html"),
59 "<h1>Overridden New Tab Page</h1>");
60
61 const Extension* new_tab_extension =
62 InstallExtension(extension_dir.Pack(), 1 /*new install*/);
63 ASSERT_TRUE(new_tab_extension);
64
65 // Visit the New Tab Page to get a renderer using the extension into history.
66 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab"));
67
68 // Navigate that tab to a non-extension URL to swap out the extension's
69 // renderer.
70 const GURL test_link_from_NTP =
71 embedded_test_server()->GetURL("/README.chromium");
72 EXPECT_THAT(test_link_from_NTP.spec(), testing::EndsWith("/README.chromium"))
73 << "Check that the test server started.";
74 NavigateInRenderer(browser()->tab_strip_model()->GetActiveWebContents(),
75 test_link_from_NTP);
76
77 // Increase the extension's version.
rockota34fed222015-09-25 21:56:3178 extension_dir.WriteManifestWithSingleQuotes(
79 base::StringPrintf(manifest_template, 2));
[email protected]bcabac762013-05-29 23:33:2480
81 // Upgrade the extension.
82 new_tab_extension = UpdateExtension(
83 new_tab_extension->id(), extension_dir.Pack(), 0 /*expected upgrade*/);
84 EXPECT_THAT(new_tab_extension->version()->components(),
85 testing::ElementsAre(2));
86
87 // The extension takes a couple round-trips to the renderer in order
88 // to crash, so open a new tab to wait long enough.
89 AddTabAtIndex(browser()->tab_strip_model()->count(),
90 GURL("http://www.google.com/"),
Sylvain Defresnec6ccc77d2014-09-19 10:19:3591 ui::PAGE_TRANSITION_TYPED);
[email protected]bcabac762013-05-29 23:33:2492
93 // Check that the extension hasn't crashed.
[email protected]ca975942014-01-07 12:06:4794 ExtensionRegistry* registry = ExtensionRegistry::Get(profile());
95 EXPECT_EQ(0U, registry->terminated_extensions().size());
96 EXPECT_TRUE(registry->enabled_extensions().Contains(new_tab_extension->id()));
[email protected]bcabac762013-05-29 23:33:2497}
98
rockota34fed222015-09-25 21:56:3199// Tests the behavior described in http://crbug.com/532088.
100IN_PROC_BROWSER_TEST_F(ExtensionLoadingTest,
101 KeepAliveWithDevToolsOpenOnReload) {
magjedc3a0a602015-11-24 12:41:14102#if defined(OS_WIN)
103 // Flaky on Win XP SP3. http://crbug.com/560716.
104 if (base::win::GetVersion() <= base::win::VERSION_SERVER_2003)
105 return;
106#endif
107
rockota34fed222015-09-25 21:56:31108 embedded_test_server()->ServeFilesFromDirectory(
109 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
svaldeza01f7d92015-11-18 17:47:56110 ASSERT_TRUE(embedded_test_server()->Start());
rockota34fed222015-09-25 21:56:31111
112 TestExtensionDir extension_dir;
113 const char manifest_contents[] =
114 "{"
115 " 'name': 'Test With Lazy Background Page',"
116 " 'version': '0',"
117 " 'manifest_version': 2,"
118 " 'app': {"
119 " 'background': {"
120 " 'scripts': ['event.js']"
121 " }"
122 " }"
123 "}";
124 extension_dir.WriteManifestWithSingleQuotes(manifest_contents);
125 extension_dir.WriteFile(FILE_PATH_LITERAL("event.js"), "");
126
127 const Extension* extension =
128 InstallExtension(extension_dir.Pack(), 1 /*new install*/);
129 ASSERT_TRUE(extension);
130 std::string extension_id = extension->id();
131
132 ProcessManager* process_manager = ProcessManager::Get(profile());
133 EXPECT_EQ(0, process_manager->GetLazyKeepaliveCount(extension));
134
135 devtools_util::InspectBackgroundPage(extension, profile());
136 EXPECT_EQ(1, process_manager->GetLazyKeepaliveCount(extension));
137
138 // Opening DevTools will cause the background page to load. Wait for it.
139 WaitForExtensionViewsToLoad();
140
141 ReloadExtension(extension_id);
142
143 // Flush the MessageLoop to ensure that DevTools has a chance to be reattached
144 // and the background page has a chance to begin reloading.
145 base::RunLoop().RunUntilIdle();
146
147 // And wait for the background page to finish loading again.
148 WaitForExtensionViewsToLoad();
149
150 // Ensure that our DevtoolsAgentHost is actually connected to the new
151 // background WebContents.
152 content::WebContents* background_contents =
153 process_manager->GetBackgroundHostForExtension(extension_id)
154 ->host_contents();
155 EXPECT_TRUE(content::DevToolsAgentHost::HasFor(background_contents));
156
157 // The old Extension object is no longer valid.
158 extension = ExtensionRegistry::Get(profile())
159 ->enabled_extensions().GetByID(extension_id);
160
161 // Keepalive count should stabilize back to 1, because DevTools is still open.
162 EXPECT_EQ(1, process_manager->GetLazyKeepaliveCount(extension));
163}
164
[email protected]bcabac762013-05-29 23:33:24165} // namespace
166} // namespace extensions