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_command_ids.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/test/test_server.h"
11
12namespace {
13
14const char kTestHtml[] = "files/viewsource/test.html";
15
16class ViewSourceTest : public UITest {
17 protected:
18  ViewSourceTest()
19      : test_server_(net::TestServer::TYPE_HTTP,
20                     FilePath(FILE_PATH_LITERAL("chrome/test/data"))) {
21  }
22
23  bool IsMenuCommandEnabled(int command) {
24    scoped_refptr<BrowserProxy> window_proxy(automation()->GetBrowserWindow(0));
25    EXPECT_TRUE(window_proxy.get());
26    if (!window_proxy.get())
27      return false;
28
29    bool enabled;
30    EXPECT_TRUE(window_proxy->IsMenuCommandEnabled(command, &enabled));
31    return enabled;
32  }
33
34 protected:
35  net::TestServer test_server_;
36};
37
38// This test renders a page in view-source and then checks to see if a cookie
39// set in the html was set successfully (it shouldn't because we rendered the
40// page in view source).
41// Flaky; see http://crbug.com/72201.
42TEST_F(ViewSourceTest, FLAKY_DoesBrowserRenderInViewSource) {
43  ASSERT_TRUE(test_server_.Start());
44
45  std::string cookie = "viewsource_cookie";
46  std::string cookie_data = "foo";
47
48  // First we navigate to our view-source test page.
49  GURL url(chrome::kViewSourceScheme + std::string(":") +
50      test_server_.GetURL(kTestHtml).spec());
51  scoped_refptr<TabProxy> tab(GetActiveTab());
52  ASSERT_TRUE(tab.get());
53  ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, tab->NavigateToURL(url));
54
55  // Try to retrieve the cookie that the page sets.  It should not be there
56  // (because we are in view-source mode).
57  std::string cookie_found;
58  ASSERT_TRUE(tab->GetCookieByName(url, cookie, &cookie_found));
59  EXPECT_NE(cookie_data, cookie_found);
60}
61
62// This test renders a page normally and then renders the same page in
63// view-source mode. This is done since we had a problem at one point during
64// implementation of the view-source: prefix being consumed (removed from the
65// URL) if the URL was not changed (apart from adding the view-source prefix)
66TEST_F(ViewSourceTest, DoesBrowserConsumeViewSourcePrefix) {
67  ASSERT_TRUE(test_server_.Start());
68
69  // First we navigate to google.html.
70  GURL url(test_server_.GetURL(kTestHtml));
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  ASSERT_TRUE(test_server_.Start());
86
87  GURL url(test_server_.GetURL(kTestHtml));
88  NavigateToURL(url);
89
90  EXPECT_TRUE(IsMenuCommandEnabled(IDC_VIEW_SOURCE));
91}
92
93// Make sure that when looking at the page source, we can't select "View Source"
94// from the menu.
95//
96// Occasionally crashes on all platforms, see http://crbug.com/69249
97TEST_F(ViewSourceTest, FLAKY_ViewSourceInMenuDisabledWhileViewingSource) {
98  ASSERT_TRUE(test_server_.Start());
99
100  GURL url_viewsource(chrome::kViewSourceScheme + std::string(":") +
101      test_server_.GetURL(kTestHtml).spec());
102  NavigateToURL(url_viewsource);
103
104  EXPECT_FALSE(IsMenuCommandEnabled(IDC_VIEW_SOURCE));
105}
106
107}  // namespace
108