view_source_uitest.cc revision 513209b27ff55e2841eac0e4120199c23acce758
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) 41TEST_F(ViewSourceTest, DoesBrowserRenderInViewSource) { 42 ASSERT_TRUE(test_server_.Start()); 43 44 std::string cookie = "viewsource_cookie"; 45 std::string cookie_data = "foo"; 46 47 // First we navigate to our view-source test page. 48 GURL url(chrome::kViewSourceScheme + std::string(":") + 49 test_server_.GetURL(kTestHtml).spec()); 50 scoped_refptr<TabProxy> tab(GetActiveTab()); 51 ASSERT_TRUE(tab.get()); 52 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, tab->NavigateToURL(url)); 53 54 // Try to retrieve the cookie that the page sets. It should not be there 55 // (because we are in view-source mode). 56 std::string cookie_found; 57 ASSERT_TRUE(tab->GetCookieByName(url, cookie, &cookie_found)); 58 EXPECT_NE(cookie_data, cookie_found); 59} 60 61// This test renders a page normally and then renders the same page in 62// view-source mode. This is done since we had a problem at one point during 63// implementation of the view-source: prefix being consumed (removed from the 64// URL) if the URL was not changed (apart from adding the view-source prefix) 65TEST_F(ViewSourceTest, DoesBrowserConsumeViewSourcePrefix) { 66 ASSERT_TRUE(test_server_.Start()); 67 68 // First we navigate to google.html. 69 GURL url(test_server_.GetURL(kTestHtml)); 70 NavigateToURL(url); 71 72 // Then we navigate to the same url but with the "view-source:" prefix. 73 GURL url_viewsource(chrome::kViewSourceScheme + std::string(":") + 74 url.spec()); 75 NavigateToURL(url_viewsource); 76 77 // The URL should still be prefixed with "view-source:". 78 EXPECT_EQ(url_viewsource.spec(), GetActiveTabURL().spec()); 79} 80 81// Make sure that when looking at the actual page, we can select "View Source" 82// from the menu. 83TEST_F(ViewSourceTest, ViewSourceInMenuEnabledOnANormalPage) { 84 ASSERT_TRUE(test_server_.Start()); 85 86 GURL url(test_server_.GetURL(kTestHtml)); 87 NavigateToURL(url); 88 89 EXPECT_TRUE(IsMenuCommandEnabled(IDC_VIEW_SOURCE)); 90} 91 92// Make sure that when looking at the page source, we can't select "View Source" 93// from the menu. 94TEST_F(ViewSourceTest, ViewSourceInMenuDisabledWhileViewingSource) { 95 ASSERT_TRUE(test_server_.Start()); 96 97 GURL url_viewsource(chrome::kViewSourceScheme + std::string(":") + 98 test_server_.GetURL(kTestHtml).spec()); 99 NavigateToURL(url_viewsource); 100 101 EXPECT_FALSE(IsMenuCommandEnabled(IDC_VIEW_SOURCE)); 102} 103 104} // namespace 105