[email protected] | bcabac76 | 2013-05-29 23:33:24 | [diff] [blame] | 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 | |
[email protected] | bcabac76 | 2013-05-29 23:33:24 | [diff] [blame] | 8 | #include "base/run_loop.h" |
[email protected] | 4458084 | 2013-07-09 21:36:53 | [diff] [blame] | 9 | #include "base/strings/stringprintf.h" |
[email protected] | ca97594 | 2014-01-07 12:06:47 | [diff] [blame] | 10 | #include "base/version.h" |
magjed | c3a0a60 | 2015-11-24 12:41:14 | [diff] [blame] | 11 | #include "base/win/windows_version.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame^] | 12 | #include "build/build_config.h" |
rockot | a34fed22 | 2015-09-25 21:56:31 | [diff] [blame] | 13 | #include "chrome/browser/extensions/devtools_util.h" |
[email protected] | bcabac76 | 2013-05-29 23:33:24 | [diff] [blame] | 14 | #include "chrome/browser/extensions/extension_browsertest.h" |
rockot | a34fed22 | 2015-09-25 21:56:31 | [diff] [blame] | 15 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 4458084 | 2013-07-09 21:36:53 | [diff] [blame] | 16 | #include "chrome/browser/extensions/test_extension_dir.h" |
[email protected] | ca97594 | 2014-01-07 12:06:47 | [diff] [blame] | 17 | #include "chrome/browser/profiles/profile.h" |
[email protected] | bcabac76 | 2013-05-29 23:33:24 | [diff] [blame] | 18 | #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" |
rockot | a34fed22 | 2015-09-25 21:56:31 | [diff] [blame] | 21 | #include "content/public/browser/devtools_agent_host.h" |
[email protected] | ca97594 | 2014-01-07 12:06:47 | [diff] [blame] | 22 | #include "extensions/browser/extension_registry.h" |
rockot | a34fed22 | 2015-09-25 21:56:31 | [diff] [blame] | 23 | #include "extensions/browser/process_manager.h" |
[email protected] | bcabac76 | 2013-05-29 23:33:24 | [diff] [blame] | 24 | #include "net/test/embedded_test_server/embedded_test_server.h" |
| 25 | #include "testing/gmock/include/gmock/gmock.h" |
| 26 | |
| 27 | namespace extensions { |
| 28 | namespace { |
| 29 | |
[email protected] | bcabac76 | 2013-05-29 23:33:24 | [diff] [blame] | 30 | class ExtensionLoadingTest : public ExtensionBrowserTest { |
| 31 | }; |
| 32 | |
| 33 | // Check the fix for http://crbug.com/178542. |
| 34 | IN_PROC_BROWSER_TEST_F(ExtensionLoadingTest, |
| 35 | UpgradeAfterNavigatingFromOverriddenNewTabPage) { |
| 36 | embedded_test_server()->ServeFilesFromDirectory( |
| 37 | base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 38 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | bcabac76 | 2013-05-29 23:33:24 | [diff] [blame] | 39 | |
| 40 | TestExtensionDir extension_dir; |
thestig | 4b36dd3 | 2014-10-31 20:30:19 | [diff] [blame] | 41 | const char manifest_template[] = |
[email protected] | 4458084 | 2013-07-09 21:36:53 | [diff] [blame] | 42 | "{" |
rockot | a34fed22 | 2015-09-25 21:56:31 | [diff] [blame] | 43 | " '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] | 4458084 | 2013-07-09 21:36:53 | [diff] [blame] | 50 | " }," |
rockot | a34fed22 | 2015-09-25 21:56:31 | [diff] [blame] | 51 | " 'chrome_url_overrides': {" |
| 52 | " 'newtab': 'newtab.html'" |
[email protected] | 4458084 | 2013-07-09 21:36:53 | [diff] [blame] | 53 | " }" |
| 54 | "}"; |
rockot | a34fed22 | 2015-09-25 21:56:31 | [diff] [blame] | 55 | extension_dir.WriteManifestWithSingleQuotes( |
| 56 | base::StringPrintf(manifest_template, 1)); |
[email protected] | bcabac76 | 2013-05-29 23:33:24 | [diff] [blame] | 57 | 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. |
rockot | a34fed22 | 2015-09-25 21:56:31 | [diff] [blame] | 78 | extension_dir.WriteManifestWithSingleQuotes( |
| 79 | base::StringPrintf(manifest_template, 2)); |
[email protected] | bcabac76 | 2013-05-29 23:33:24 | [diff] [blame] | 80 | |
| 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 Defresne | c6ccc77d | 2014-09-19 10:19:35 | [diff] [blame] | 91 | ui::PAGE_TRANSITION_TYPED); |
[email protected] | bcabac76 | 2013-05-29 23:33:24 | [diff] [blame] | 92 | |
| 93 | // Check that the extension hasn't crashed. |
[email protected] | ca97594 | 2014-01-07 12:06:47 | [diff] [blame] | 94 | 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] | bcabac76 | 2013-05-29 23:33:24 | [diff] [blame] | 97 | } |
| 98 | |
rockot | a34fed22 | 2015-09-25 21:56:31 | [diff] [blame] | 99 | // Tests the behavior described in http://crbug.com/532088. |
| 100 | IN_PROC_BROWSER_TEST_F(ExtensionLoadingTest, |
| 101 | KeepAliveWithDevToolsOpenOnReload) { |
magjed | c3a0a60 | 2015-11-24 12:41:14 | [diff] [blame] | 102 | #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 | |
rockot | a34fed22 | 2015-09-25 21:56:31 | [diff] [blame] | 108 | embedded_test_server()->ServeFilesFromDirectory( |
| 109 | base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 110 | ASSERT_TRUE(embedded_test_server()->Start()); |
rockot | a34fed22 | 2015-09-25 21:56:31 | [diff] [blame] | 111 | |
| 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] | bcabac76 | 2013-05-29 23:33:24 | [diff] [blame] | 165 | } // namespace |
| 166 | } // namespace extensions |