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