blob: 9a19ac4c8fcff688c6b989dc4bb2ac0dabdfa67a [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.commit09911bf2008-07-26 23:55:294
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
11namespace {
12
13const wchar_t kDocRoot[] = L"chrome/test/data";
14
15class ViewSourceTest : public UITest {
16 protected:
[email protected]f72a1cc2010-04-30 07:17:3017 ViewSourceTest() : test_html_("files/viewsource/test.html") {
initial.commit09911bf2008-07-26 23:55:2918 }
19
20 bool IsPageMenuCommandEnabled(int command) {
[email protected]802376e2009-05-28 19:12:4621 scoped_refptr<BrowserProxy> window_proxy(automation()->GetBrowserWindow(0));
[email protected]1a96b432010-03-22 13:26:4622 EXPECT_TRUE(window_proxy.get());
initial.commit09911bf2008-07-26 23:55:2923 if (!window_proxy.get())
24 return false;
25
[email protected]1a96b432010-03-22 13:26:4626 bool enabled;
27 EXPECT_TRUE(window_proxy->IsPageMenuCommandEnabled(command, &enabled));
28 return enabled;
initial.commit09911bf2008-07-26 23:55:2929 }
30
31 protected:
[email protected]f72a1cc2010-04-30 07:17:3032 std::string test_html_;
initial.commit09911bf2008-07-26 23:55:2933};
34
initial.commit09911bf2008-07-26 23:55:2935// 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)
38TEST_F(ViewSourceTest, DoesBrowserRenderInViewSource) {
[email protected]dd265012009-01-08 20:45:2739 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:5540 HTTPTestServer::CreateServer(kDocRoot, NULL);
[email protected]dd265012009-01-08 20:45:2741 ASSERT_TRUE(NULL != server.get());
initial.commit09911bf2008-07-26 23:55:2942 std::string cookie = "viewsource_cookie";
43 std::string cookie_data = "foo";
44
45 // First we navigate to our view-source test page
[email protected]f72a1cc2010-04-30 07:17:3046 GURL url = server->TestServerPage(test_html_);
initial.commit09911bf2008-07-26 23:55:2947 url = GURL("view-source:" + url.spec());
[email protected]802376e2009-05-28 19:12:4648 scoped_refptr<TabProxy> tab(GetActiveTab());
[email protected]5c9431802009-07-07 22:31:4749 ASSERT_TRUE(tab.get());
[email protected]6fd35352010-03-16 08:39:5450 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, tab->NavigateToURL(url));
initial.commit09911bf2008-07-26 23:55:2951
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]6fd35352010-03-16 08:39:5455 ASSERT_TRUE(tab->GetCookieByName(url, cookie, &cookie_found));
initial.commit09911bf2008-07-26 23:55:2956 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)
63TEST_F(ViewSourceTest, DoesBrowserConsumeViewSourcePrefix) {
[email protected]dd265012009-01-08 20:45:2764 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:5565 HTTPTestServer::CreateServer(kDocRoot, NULL);
[email protected]dd265012009-01-08 20:45:2766 ASSERT_TRUE(NULL != server.get());
initial.commit09911bf2008-07-26 23:55:2967
68 // First we navigate to google.html
[email protected]f72a1cc2010-04-30 07:17:3069 GURL url = server->TestServerPage(test_html_);
initial.commit09911bf2008-07-26 23:55:2970 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.
82TEST_F(ViewSourceTest, ViewSourceInPageMenuEnabledOnANormalPage) {
[email protected]dd265012009-01-08 20:45:2783 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:5584 HTTPTestServer::CreateServer(kDocRoot, NULL);
[email protected]dd265012009-01-08 20:45:2785 ASSERT_TRUE(NULL != server.get());
initial.commit09911bf2008-07-26 23:55:2986
87 // First we navigate to google.html
[email protected]f72a1cc2010-04-30 07:17:3088 GURL url = server->TestServerPage(test_html_);
initial.commit09911bf2008-07-26 23:55:2989 NavigateToURL(url);
90
[email protected]f1d306e62008-12-08 20:50:0591 EXPECT_TRUE(IsPageMenuCommandEnabled(IDC_VIEW_SOURCE));
initial.commit09911bf2008-07-26 23:55:2992}
93
94// Make sure that when looking at the page source, we can't select
95// "View Source" from the Page menu.
96TEST_F(ViewSourceTest, ViewSourceInPageMenuDisabledWhileViewingSource) {
[email protected]dd265012009-01-08 20:45:2797 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:5598 HTTPTestServer::CreateServer(kDocRoot, NULL);
[email protected]dd265012009-01-08 20:45:2799 ASSERT_TRUE(NULL != server.get());
initial.commit09911bf2008-07-26 23:55:29100
101 // First we navigate to google.html
[email protected]f72a1cc2010-04-30 07:17:30102 GURL url = server->TestServerPage(test_html_);
initial.commit09911bf2008-07-26 23:55:29103 GURL url_viewsource = GURL("view-source:" + url.spec());
104 NavigateToURL(url_viewsource);
105
[email protected]f1d306e62008-12-08 20:50:05106 EXPECT_FALSE(IsPageMenuCommandEnabled(IDC_VIEW_SOURCE));
initial.commit09911bf2008-07-26 23:55:29107}
[email protected]5d8a05fc2009-04-08 16:14:06108
109} // namespace