OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // This file contains tests for extension loading, reloading, and | 5 // This file contains tests for extension loading, reloading, and |
6 // unloading behavior. | 6 // unloading behavior. |
7 | 7 |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/version.h" | 10 #include "base/version.h" |
11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
12 #include "chrome/browser/extensions/devtools_util.h" | 12 #include "chrome/browser/extensions/devtools_util.h" |
13 #include "chrome/browser/extensions/extension_browsertest.h" | 13 #include "chrome/browser/extensions/extension_browsertest.h" |
14 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
15 #include "chrome/browser/extensions/test_extension_dir.h" | 15 #include "chrome/browser/extensions/test_extension_dir.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
18 #include "chrome/test/base/in_process_browser_test.h" | 18 #include "chrome/test/base/in_process_browser_test.h" |
19 #include "chrome/test/base/ui_test_utils.h" | 19 #include "chrome/test/base/ui_test_utils.h" |
20 #include "content/public/browser/devtools_agent_host.h" | 20 #include "content/public/browser/devtools_agent_host.h" |
21 #include "content/public/test/browser_test_utils.h" | 21 #include "content/public/test/browser_test_utils.h" |
22 #include "extensions/browser/extension_registry.h" | 22 #include "extensions/browser/extension_registry.h" |
23 #include "extensions/browser/process_manager.h" | 23 #include "extensions/browser/process_manager.h" |
| 24 #include "extensions/common/permissions/permissions_data.h" |
24 #include "extensions/test/extension_test_message_listener.h" | 25 #include "extensions/test/extension_test_message_listener.h" |
25 #include "net/test/embedded_test_server/embedded_test_server.h" | 26 #include "net/test/embedded_test_server/embedded_test_server.h" |
26 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
27 | 28 |
28 #if defined(OS_WIN) | 29 #if defined(OS_WIN) |
29 #include "base/win/windows_version.h" | 30 #include "base/win/windows_version.h" |
30 #endif | 31 #endif |
31 | 32 |
32 namespace extensions { | 33 namespace extensions { |
33 namespace { | 34 namespace { |
34 | 35 |
35 class ExtensionLoadingTest : public ExtensionBrowserTest { | 36 class ExtensionLoadingTest : public ExtensionBrowserTest { |
36 }; | 37 }; |
37 | 38 |
38 // Check the fix for http://crbug.com/178542. | 39 // Check the fix for http://crbug.com/178542. |
39 IN_PROC_BROWSER_TEST_F(ExtensionLoadingTest, | 40 IN_PROC_BROWSER_TEST_F(ExtensionLoadingTest, |
40 UpgradeAfterNavigatingFromOverriddenNewTabPage) { | 41 UpgradeAfterNavigatingFromOverriddenNewTabPage) { |
41 embedded_test_server()->ServeFilesFromDirectory( | 42 embedded_test_server()->ServeFilesFromDirectory( |
42 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | 43 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); |
43 ASSERT_TRUE(embedded_test_server()->Start()); | 44 ASSERT_TRUE(embedded_test_server()->Start()); |
44 | 45 |
45 TestExtensionDir extension_dir; | 46 TestExtensionDir extension_dir; |
46 const char manifest_template[] = | 47 const char kManifestTemplate[] = |
47 "{" | 48 "{" |
48 " 'name': 'Overrides New Tab'," | 49 " 'name': 'Overrides New Tab'," |
49 " 'version': '%d'," | 50 " 'version': '%d'," |
50 " 'description': 'Overrides New Tab'," | 51 " 'description': 'Overrides New Tab'," |
51 " 'manifest_version': 2," | 52 " 'manifest_version': 2," |
52 " 'background': {" | 53 " 'background': {" |
53 " 'persistent': false," | 54 " 'persistent': false," |
54 " 'scripts': ['event.js']" | 55 " 'scripts': ['event.js']" |
55 " }," | 56 " }," |
56 " 'chrome_url_overrides': {" | 57 " 'chrome_url_overrides': {" |
57 " 'newtab': 'newtab.html'" | 58 " 'newtab': 'newtab.html'" |
58 " }" | 59 " }" |
59 "}"; | 60 "}"; |
60 extension_dir.WriteManifestWithSingleQuotes( | 61 extension_dir.WriteManifestWithSingleQuotes( |
61 base::StringPrintf(manifest_template, 1)); | 62 base::StringPrintf(kManifestTemplate, 1)); |
62 extension_dir.WriteFile(FILE_PATH_LITERAL("event.js"), ""); | 63 extension_dir.WriteFile(FILE_PATH_LITERAL("event.js"), ""); |
63 extension_dir.WriteFile(FILE_PATH_LITERAL("newtab.html"), | 64 extension_dir.WriteFile(FILE_PATH_LITERAL("newtab.html"), |
64 "<h1>Overridden New Tab Page</h1>"); | 65 "<h1>Overridden New Tab Page</h1>"); |
65 | 66 |
66 const Extension* new_tab_extension = | 67 const Extension* new_tab_extension = |
67 InstallExtension(extension_dir.Pack(), 1 /*new install*/); | 68 InstallExtension(extension_dir.Pack(), 1 /*new install*/); |
68 ASSERT_TRUE(new_tab_extension); | 69 ASSERT_TRUE(new_tab_extension); |
69 | 70 |
70 // Visit the New Tab Page to get a renderer using the extension into history. | 71 // Visit the New Tab Page to get a renderer using the extension into history. |
71 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab")); | 72 ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab")); |
72 | 73 |
73 // Navigate that tab to a non-extension URL to swap out the extension's | 74 // Navigate that tab to a non-extension URL to swap out the extension's |
74 // renderer. | 75 // renderer. |
75 const GURL test_link_from_NTP = | 76 const GURL test_link_from_NTP = |
76 embedded_test_server()->GetURL("/README.chromium"); | 77 embedded_test_server()->GetURL("/README.chromium"); |
77 EXPECT_THAT(test_link_from_NTP.spec(), testing::EndsWith("/README.chromium")) | 78 EXPECT_THAT(test_link_from_NTP.spec(), testing::EndsWith("/README.chromium")) |
78 << "Check that the test server started."; | 79 << "Check that the test server started."; |
79 NavigateInRenderer(browser()->tab_strip_model()->GetActiveWebContents(), | 80 NavigateInRenderer(browser()->tab_strip_model()->GetActiveWebContents(), |
80 test_link_from_NTP); | 81 test_link_from_NTP); |
81 | 82 |
82 // Increase the extension's version. | 83 // Increase the extension's version. |
83 extension_dir.WriteManifestWithSingleQuotes( | 84 extension_dir.WriteManifestWithSingleQuotes( |
84 base::StringPrintf(manifest_template, 2)); | 85 base::StringPrintf(kManifestTemplate, 2)); |
85 | 86 |
86 // Upgrade the extension. | 87 // Upgrade the extension. |
87 new_tab_extension = UpdateExtension( | 88 new_tab_extension = UpdateExtension( |
88 new_tab_extension->id(), extension_dir.Pack(), 0 /*expected upgrade*/); | 89 new_tab_extension->id(), extension_dir.Pack(), 0 /*expected upgrade*/); |
89 EXPECT_THAT(new_tab_extension->version()->components(), | 90 EXPECT_THAT(new_tab_extension->version()->components(), |
90 testing::ElementsAre(2)); | 91 testing::ElementsAre(2)); |
91 | 92 |
92 // The extension takes a couple round-trips to the renderer in order | 93 // The extension takes a couple round-trips to the renderer in order |
93 // to crash, so open a new tab to wait long enough. | 94 // to crash, so open a new tab to wait long enough. |
94 AddTabAtIndex(browser()->tab_strip_model()->count(), | 95 AddTabAtIndex(browser()->tab_strip_model()->count(), |
95 GURL("http://www.google.com/"), | 96 GURL("http://www.google.com/"), |
96 ui::PAGE_TRANSITION_TYPED); | 97 ui::PAGE_TRANSITION_TYPED); |
97 | 98 |
98 // Check that the extension hasn't crashed. | 99 // Check that the extension hasn't crashed. |
99 ExtensionRegistry* registry = ExtensionRegistry::Get(profile()); | 100 ExtensionRegistry* registry = ExtensionRegistry::Get(profile()); |
100 EXPECT_EQ(0U, registry->terminated_extensions().size()); | 101 EXPECT_EQ(0U, registry->terminated_extensions().size()); |
101 EXPECT_TRUE(registry->enabled_extensions().Contains(new_tab_extension->id())); | 102 EXPECT_TRUE(registry->enabled_extensions().Contains(new_tab_extension->id())); |
102 } | 103 } |
103 | 104 |
| 105 IN_PROC_BROWSER_TEST_F(ExtensionLoadingTest, |
| 106 UpgradeAddingNewTabPagePermissionNoPrompt) { |
| 107 embedded_test_server()->ServeFilesFromDirectory( |
| 108 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); |
| 109 ASSERT_TRUE(embedded_test_server()->Start()); |
| 110 |
| 111 TestExtensionDir extension_dir; |
| 112 const char kManifestTemplate[] = |
| 113 "{" |
| 114 " 'name': 'Overrides New Tab'," |
| 115 " 'version': '%d'," |
| 116 " 'description': 'Will override New Tab soon'," |
| 117 " %s" // Placeholder for future NTP url override block. |
| 118 " 'manifest_version': 2" |
| 119 "}"; |
| 120 extension_dir.WriteManifestWithSingleQuotes( |
| 121 base::StringPrintf(kManifestTemplate, 1, "")); |
| 122 extension_dir.WriteFile(FILE_PATH_LITERAL("event.js"), ""); |
| 123 extension_dir.WriteFile(FILE_PATH_LITERAL("newtab.html"), |
| 124 "<h1>Overridden New Tab Page</h1>"); |
| 125 |
| 126 const Extension* new_tab_extension = |
| 127 InstallExtension(extension_dir.Pack(), 1 /*new install*/); |
| 128 ASSERT_TRUE(new_tab_extension); |
| 129 |
| 130 EXPECT_FALSE(new_tab_extension->permissions_data()->HasAPIPermission( |
| 131 APIPermission::kNewTabPageOverride)); |
| 132 |
| 133 // Navigate that tab to a non-extension URL to swap out the extension's |
| 134 // renderer. |
| 135 const GURL test_link_from_ntp = |
| 136 embedded_test_server()->GetURL("/README.chromium"); |
| 137 EXPECT_THAT(test_link_from_ntp.spec(), testing::EndsWith("/README.chromium")) |
| 138 << "Check that the test server started."; |
| 139 NavigateInRenderer(browser()->tab_strip_model()->GetActiveWebContents(), |
| 140 test_link_from_ntp); |
| 141 |
| 142 // Increase the extension's version and add the NTP url override which will |
| 143 // add the kNewTabPageOverride permission. |
| 144 const char ntp_override_string[] = |
| 145 " 'chrome_url_overrides': {" |
| 146 " 'newtab': 'newtab.html'" |
| 147 " },"; |
| 148 extension_dir.WriteManifestWithSingleQuotes( |
| 149 base::StringPrintf(kManifestTemplate, 2, ntp_override_string)); |
| 150 |
| 151 // Upgrade the extension, ensure that the upgrade 'worked' in the sense that |
| 152 // the extension is still present and not disabled and that it now has the |
| 153 // new API permission. |
| 154 // TODO(robertshield): Update this once most of the population is on M62+ |
| 155 // and adding NTP permissions implies a permission upgrade. |
| 156 new_tab_extension = UpdateExtension( |
| 157 new_tab_extension->id(), extension_dir.Pack(), 0 /*expected upgrade*/); |
| 158 ASSERT_NE(nullptr, new_tab_extension); |
| 159 |
| 160 EXPECT_TRUE(new_tab_extension->permissions_data()->HasAPIPermission( |
| 161 APIPermission::kNewTabPageOverride)); |
| 162 EXPECT_THAT(new_tab_extension->version()->components(), |
| 163 testing::ElementsAre(2)); |
| 164 } |
| 165 |
104 // Tests the behavior described in http://crbug.com/532088. | 166 // Tests the behavior described in http://crbug.com/532088. |
105 IN_PROC_BROWSER_TEST_F(ExtensionLoadingTest, | 167 IN_PROC_BROWSER_TEST_F(ExtensionLoadingTest, |
106 KeepAliveWithDevToolsOpenOnReload) { | 168 KeepAliveWithDevToolsOpenOnReload) { |
107 embedded_test_server()->ServeFilesFromDirectory( | 169 embedded_test_server()->ServeFilesFromDirectory( |
108 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | 170 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); |
109 ASSERT_TRUE(embedded_test_server()->Start()); | 171 ASSERT_TRUE(embedded_test_server()->Start()); |
110 | 172 |
111 TestExtensionDir extension_dir; | 173 TestExtensionDir extension_dir; |
112 const char manifest_contents[] = | 174 const char manifest_contents[] = |
113 "{" | 175 "{" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 | 292 |
231 // Now check whether the extension runtime is valid (see kTargetJs). | 293 // Now check whether the extension runtime is valid (see kTargetJs). |
232 bool is_valid = false; | 294 bool is_valid = false; |
233 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | 295 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
234 bg_contents, "domAutomationController.send(is_valid);", &is_valid)); | 296 bg_contents, "domAutomationController.send(is_valid);", &is_valid)); |
235 EXPECT_TRUE(is_valid); | 297 EXPECT_TRUE(is_valid); |
236 } | 298 } |
237 | 299 |
238 } // namespace | 300 } // namespace |
239 } // namespace extensions | 301 } // namespace extensions |
OLD | NEW |