license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #include "chrome/app/chrome_dll_resource.h" |
| 6 | #include "chrome/test/automation/browser_proxy.h" |
| 7 | #include "chrome/test/automation/tab_proxy.h" |
| 8 | #include "chrome/test/ui/ui_test.h" |
| 9 | #include "net/url_request/url_request_unittest.h" |
| 10 | |
| 11 | namespace { |
| 12 | |
| 13 | const wchar_t kDocRoot[] = L"chrome/test/data"; |
| 14 | |
| 15 | class ViewSourceTest : public UITest { |
| 16 | protected: |
[email protected] | f72a1cc | 2010-04-30 07:17:30 | [diff] [blame] | 17 | ViewSourceTest() : test_html_("files/viewsource/test.html") { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | bool IsPageMenuCommandEnabled(int command) { |
[email protected] | 802376e | 2009-05-28 19:12:46 | [diff] [blame] | 21 | scoped_refptr<BrowserProxy> window_proxy(automation()->GetBrowserWindow(0)); |
[email protected] | 1a96b43 | 2010-03-22 13:26:46 | [diff] [blame] | 22 | EXPECT_TRUE(window_proxy.get()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 23 | if (!window_proxy.get()) |
| 24 | return false; |
| 25 | |
[email protected] | 1a96b43 | 2010-03-22 13:26:46 | [diff] [blame] | 26 | bool enabled; |
| 27 | EXPECT_TRUE(window_proxy->IsPageMenuCommandEnabled(command, &enabled)); |
| 28 | return enabled; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | protected: |
[email protected] | f72a1cc | 2010-04-30 07:17:30 | [diff] [blame] | 32 | std::string test_html_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 33 | }; |
| 34 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | // This test renders a page in view-source and then checks to see if a cookie |
| 36 | // set in the html was set successfully (it shouldn't because we rendered the |
| 37 | // page in view source) |
| 38 | TEST_F(ViewSourceTest, DoesBrowserRenderInViewSource) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 39 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 40 | HTTPTestServer::CreateServer(kDocRoot, NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 41 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | std::string cookie = "viewsource_cookie"; |
| 43 | std::string cookie_data = "foo"; |
| 44 | |
| 45 | // First we navigate to our view-source test page |
[email protected] | f72a1cc | 2010-04-30 07:17:30 | [diff] [blame] | 46 | GURL url = server->TestServerPage(test_html_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | url = GURL("view-source:" + url.spec()); |
[email protected] | 802376e | 2009-05-28 19:12:46 | [diff] [blame] | 48 | scoped_refptr<TabProxy> tab(GetActiveTab()); |
[email protected] | 5c943180 | 2009-07-07 22:31:47 | [diff] [blame] | 49 | ASSERT_TRUE(tab.get()); |
[email protected] | 6fd3535 | 2010-03-16 08:39:54 | [diff] [blame] | 50 | ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, tab->NavigateToURL(url)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | |
| 52 | // Try to retrieve the cookie that the page sets |
| 53 | // It should not be there (because we are in view-source mode |
| 54 | std::string cookie_found; |
[email protected] | 6fd3535 | 2010-03-16 08:39:54 | [diff] [blame] | 55 | ASSERT_TRUE(tab->GetCookieByName(url, cookie, &cookie_found)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | EXPECT_NE(cookie_data, cookie_found); |
| 57 | } |
| 58 | |
| 59 | // This test renders a page normally and then renders the same page in |
| 60 | // view-source mode. This is done since we had a problem at one point during |
| 61 | // implementation of the view-source: prefix being consumed (removed from the |
| 62 | // URL) if the URL was not changed (apart from adding the view-source prefix) |
| 63 | TEST_F(ViewSourceTest, DoesBrowserConsumeViewSourcePrefix) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 64 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 65 | HTTPTestServer::CreateServer(kDocRoot, NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 66 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | |
| 68 | // First we navigate to google.html |
[email protected] | f72a1cc | 2010-04-30 07:17:30 | [diff] [blame] | 69 | GURL url = server->TestServerPage(test_html_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | NavigateToURL(url); |
| 71 | |
| 72 | // Then we navigate to the SAME url but with the view-source: prefix |
| 73 | GURL url_viewsource = GURL("view-source:" + url.spec()); |
| 74 | NavigateToURL(url_viewsource); |
| 75 | |
| 76 | // The URL should still be prefixed with view-source: |
| 77 | EXPECT_EQ(url_viewsource.spec(), GetActiveTabURL().spec()); |
| 78 | } |
| 79 | |
| 80 | // Make sure that when looking at the actual page, we can select |
| 81 | // "View Source" from the Page menu. |
| 82 | TEST_F(ViewSourceTest, ViewSourceInPageMenuEnabledOnANormalPage) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 83 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 84 | HTTPTestServer::CreateServer(kDocRoot, NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 85 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 86 | |
| 87 | // First we navigate to google.html |
[email protected] | f72a1cc | 2010-04-30 07:17:30 | [diff] [blame] | 88 | GURL url = server->TestServerPage(test_html_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 89 | NavigateToURL(url); |
| 90 | |
[email protected] | f1d306e6 | 2008-12-08 20:50:05 | [diff] [blame] | 91 | EXPECT_TRUE(IsPageMenuCommandEnabled(IDC_VIEW_SOURCE)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // Make sure that when looking at the page source, we can't select |
| 95 | // "View Source" from the Page menu. |
| 96 | TEST_F(ViewSourceTest, ViewSourceInPageMenuDisabledWhileViewingSource) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 97 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 98 | HTTPTestServer::CreateServer(kDocRoot, NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 99 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 100 | |
| 101 | // First we navigate to google.html |
[email protected] | f72a1cc | 2010-04-30 07:17:30 | [diff] [blame] | 102 | GURL url = server->TestServerPage(test_html_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 103 | GURL url_viewsource = GURL("view-source:" + url.spec()); |
| 104 | NavigateToURL(url_viewsource); |
| 105 | |
[email protected] | f1d306e6 | 2008-12-08 20:50:05 | [diff] [blame] | 106 | EXPECT_FALSE(IsPageMenuCommandEnabled(IDC_VIEW_SOURCE)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 107 | } |
[email protected] | 5d8a05fc | 2009-04-08 16:14:06 | [diff] [blame] | 108 | |
| 109 | } // namespace |