| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "base/strings/utf_string_conversions.h" | 5 #include "base/strings/utf_string_conversions.h" |
| 6 #include "chrome/app/chrome_command_ids.h" | 6 #include "chrome/app/chrome_command_ids.h" |
| 7 #include "chrome/browser/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
| 8 #include "chrome/browser/ui/browser.h" | 8 #include "chrome/browser/ui/browser.h" |
| 9 #include "chrome/browser/ui/browser_commands.h" | 9 #include "chrome/browser/ui/browser_commands.h" |
| 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 11 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
| 12 #include "chrome/test/base/in_process_browser_test.h" | 12 #include "chrome/test/base/in_process_browser_test.h" |
| 13 #include "chrome/test/base/ui_test_utils.h" | 13 #include "chrome/test/base/ui_test_utils.h" |
| 14 #include "content/public/browser/navigation_entry.h" | 14 #include "content/public/browser/navigation_entry.h" |
| 15 #include "content/public/browser/notification_service.h" | 15 #include "content/public/browser/notification_service.h" |
| 16 #include "content/public/browser/notification_types.h" | 16 #include "content/public/browser/notification_types.h" |
| 17 #include "content/public/browser/render_frame_host.h" | |
| 18 #include "content/public/browser/render_view_host.h" | |
| 19 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 20 #include "content/public/test/browser_test_utils.h" | 18 #include "content/public/test/browser_test_utils.h" |
| 21 #include "extensions/common/constants.h" | |
| 22 #include "net/test/embedded_test_server/embedded_test_server.h" | 19 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 23 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 24 | 21 |
| 25 namespace { | 22 namespace { |
| 26 const char kTestHtml[] = "/viewsource/test.html"; | 23 const char kTestHtml[] = "/viewsource/test.html"; |
| 27 const char kTestMedia[] = "/media/pink_noise_140ms.wav"; | 24 const char kTestMedia[] = "/media/pink_noise_140ms.wav"; |
| 28 } | 25 } |
| 29 | 26 |
| 30 typedef InProcessBrowserTest ViewSourceTest; | 27 typedef InProcessBrowserTest ViewSourceTest; |
| 31 | 28 |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 // expected element on the page exists or not. In view-source mode it | 176 // expected element on the page exists or not. In view-source mode it |
| 180 // should not be found. | 177 // should not be found. |
| 181 bool result = false; | 178 bool result = false; |
| 182 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | 179 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 183 browser()->tab_strip_model()->GetActiveWebContents(), | 180 browser()->tab_strip_model()->GetActiveWebContents(), |
| 184 "domAutomationController.send(document.getElementById('bar') === null);", | 181 "domAutomationController.send(document.getElementById('bar') === null);", |
| 185 &result)); | 182 &result)); |
| 186 EXPECT_TRUE(result); | 183 EXPECT_TRUE(result); |
| 187 EXPECT_FALSE(chrome::CanViewSource(browser())); | 184 EXPECT_FALSE(chrome::CanViewSource(browser())); |
| 188 } | 185 } |
| 189 | |
| 190 // Verify that restoring a view-source tab for a Chrome extension works | |
| 191 // properly. See https://crbug.com/699428. | |
| 192 IN_PROC_BROWSER_TEST_F(ViewSourceTest, ViewSourceTabRestore) { | |
| 193 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 194 | |
| 195 // Go to the Chrome bookmarks URL. It should redirect to the bookmark | |
| 196 // manager Chrome extension. | |
| 197 GURL bookmarks_url(chrome::kChromeUIBookmarksURL); | |
| 198 ui_test_utils::NavigateToURL(browser(), bookmarks_url); | |
| 199 EXPECT_TRUE(chrome::CanViewSource(browser())); | |
| 200 content::WebContents* bookmarks_tab = | |
| 201 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 202 GURL bookmarks_extension_url = | |
| 203 bookmarks_tab->GetMainFrame()->GetLastCommittedURL(); | |
| 204 EXPECT_TRUE(bookmarks_extension_url.SchemeIs(extensions::kExtensionScheme)); | |
| 205 | |
| 206 // Open a new view-source tab for that URL. | |
| 207 GURL view_source_url(content::kViewSourceScheme + std::string(":") + | |
| 208 bookmarks_extension_url.spec()); | |
| 209 AddTabAtIndex(1, view_source_url, ui::PAGE_TRANSITION_TYPED); | |
| 210 content::WebContents* view_source_tab = | |
| 211 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 212 EXPECT_EQ(view_source_url, view_source_tab->GetVisibleURL()); | |
| 213 EXPECT_EQ(view_source_url, | |
| 214 view_source_tab->GetController().GetActiveEntry()->GetVirtualURL()); | |
| 215 EXPECT_EQ(bookmarks_extension_url, | |
| 216 view_source_tab->GetMainFrame()->GetLastCommittedURL()); | |
| 217 EXPECT_FALSE(chrome::CanViewSource(browser())); | |
| 218 | |
| 219 // Close the view-source tab. | |
| 220 chrome::CloseTab(browser()); | |
| 221 ASSERT_EQ(1, browser()->tab_strip_model()->count()); | |
| 222 | |
| 223 // Restore the tab. In the bug, the restored navigation was blocked, and we | |
| 224 // ended up showing view-source of an about:blank page. | |
| 225 content::WindowedNotificationObserver tab_added_observer( | |
| 226 chrome::NOTIFICATION_TAB_PARENTED, | |
| 227 content::NotificationService::AllSources()); | |
| 228 chrome::RestoreTab(browser()); | |
| 229 tab_added_observer.Wait(); | |
| 230 view_source_tab = browser()->tab_strip_model()->GetActiveWebContents(); | |
| 231 WaitForLoadStop(view_source_tab); | |
| 232 | |
| 233 // Verify the browser-side URLs. Note that without view-source, the | |
| 234 // bookmarks extension visible URL would be rewritten to chrome://bookmarks, | |
| 235 // but with view-source, we should still see it as | |
| 236 // view-source:chrome-extension://.../. | |
| 237 EXPECT_EQ(view_source_url, view_source_tab->GetVisibleURL()); | |
| 238 EXPECT_EQ(view_source_url, | |
| 239 view_source_tab->GetController().GetActiveEntry()->GetVirtualURL()); | |
| 240 EXPECT_EQ(bookmarks_extension_url, | |
| 241 view_source_tab->GetMainFrame()->GetLastCommittedURL()); | |
| 242 EXPECT_FALSE(chrome::CanViewSource(browser())); | |
| 243 | |
| 244 // Verify that the view-source content is not empty, and that the | |
| 245 // renderer-side URL is correct. | |
| 246 int view_source_length; | |
| 247 EXPECT_TRUE(ExecuteScriptAndExtractInt( | |
| 248 view_source_tab, | |
| 249 "domAutomationController.send(document.body.innerText.length)", | |
| 250 &view_source_length)); | |
| 251 EXPECT_GT(view_source_length, 0); | |
| 252 | |
| 253 std::string location; | |
| 254 EXPECT_TRUE(ExecuteScriptAndExtractString( | |
| 255 view_source_tab, "domAutomationController.send(location.href)", | |
| 256 &location)); | |
| 257 EXPECT_EQ(bookmarks_extension_url, location); | |
| 258 } | |
| OLD | NEW |