[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" |
[email protected] | bcabac76 | 2013-05-29 23:33:24 | [diff] [blame] | 11 | #include "chrome/browser/extensions/extension_browsertest.h" |
[email protected] | 4458084 | 2013-07-09 21:36:53 | [diff] [blame] | 12 | #include "chrome/browser/extensions/test_extension_dir.h" |
[email protected] | ca97594 | 2014-01-07 12:06:47 | [diff] [blame] | 13 | #include "chrome/browser/profiles/profile.h" |
[email protected] | bcabac76 | 2013-05-29 23:33:24 | [diff] [blame] | 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" |
[email protected] | ca97594 | 2014-01-07 12:06:47 | [diff] [blame] | 17 | #include "extensions/browser/extension_registry.h" |
[email protected] | bcabac76 | 2013-05-29 23:33:24 | [diff] [blame] | 18 | #include "net/test/embedded_test_server/embedded_test_server.h" |
| 19 | #include "testing/gmock/include/gmock/gmock.h" |
| 20 | |
| 21 | namespace extensions { |
| 22 | namespace { |
| 23 | |
[email protected] | bcabac76 | 2013-05-29 23:33:24 | [diff] [blame] | 24 | class ExtensionLoadingTest : public ExtensionBrowserTest { |
| 25 | }; |
| 26 | |
| 27 | // Check the fix for http://crbug.com/178542. |
| 28 | IN_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; |
thestig | 4b36dd3 | 2014-10-31 20:30:19 | [diff] [blame] | 35 | const char manifest_template[] = |
[email protected] | 4458084 | 2013-07-09 21:36:53 | [diff] [blame] | 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)); |
[email protected] | bcabac76 | 2013-05-29 23:33:24 | [diff] [blame] | 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. |
[email protected] | 4458084 | 2013-07-09 21:36:53 | [diff] [blame] | 71 | extension_dir.WriteManifest(base::StringPrintf(manifest_template, 2)); |
[email protected] | bcabac76 | 2013-05-29 23:33:24 | [diff] [blame] | 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/"), |
Sylvain Defresne | c6ccc77d | 2014-09-19 10:19:35 | [diff] [blame] | 83 | ui::PAGE_TRANSITION_TYPED); |
[email protected] | bcabac76 | 2013-05-29 23:33:24 | [diff] [blame] | 84 | |
| 85 | // Check that the extension hasn't crashed. |
[email protected] | ca97594 | 2014-01-07 12:06:47 | [diff] [blame] | 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())); |
[email protected] | bcabac76 | 2013-05-29 23:33:24 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | } // namespace |
| 92 | } // namespace extensions |