1// Copyright (c) 2012 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 "base/strings/utf_string_conversions.h"
6#include "chrome/app/chrome_command_ids.h"
7#include "chrome/browser/ui/browser.h"
8#include "chrome/browser/ui/browser_commands.h"
9#include "chrome/browser/ui/tabs/tab_strip_model.h"
10#include "chrome/common/url_constants.h"
11#include "chrome/test/base/in_process_browser_test.h"
12#include "chrome/test/base/ui_test_utils.h"
13#include "content/public/browser/navigation_entry.h"
14#include "content/public/browser/notification_service.h"
15#include "content/public/browser/notification_types.h"
16#include "content/public/browser/render_view_host.h"
17#include "content/public/browser/web_contents.h"
18#include "content/public/test/browser_test_utils.h"
19#include "net/test/embedded_test_server/embedded_test_server.h"
20#include "url/gurl.h"
21
22namespace {
23const char kTestHtml[] = "/viewsource/test.html";
24const char kTestMedia[] = "files/media/pink_noise_140ms.wav";
25}
26
27typedef InProcessBrowserTest ViewSourceTest;
28
29// This test renders a page in view-source and then checks to see if the title
30// set in the html was set successfully (it shouldn't because we rendered the
31// page in view source).
32// Flaky; see http://crbug.com/72201.
33IN_PROC_BROWSER_TEST_F(ViewSourceTest, DoesBrowserRenderInViewSource) {
34  ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
35
36  // First we navigate to our view-source test page.
37  GURL url(content::kViewSourceScheme + std::string(":") +
38           embedded_test_server()->GetURL(kTestHtml).spec());
39  ui_test_utils::NavigateToURL(browser(), url);
40
41  // Check that the title didn't get set.  It should not be there (because we
42  // are in view-source mode).
43  EXPECT_NE(base::ASCIIToUTF16("foo"),
44            browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
45}
46
47// This test renders a page normally and then renders the same page in
48// view-source mode. This is done since we had a problem at one point during
49// implementation of the view-source: prefix being consumed (removed from the
50// URL) if the URL was not changed (apart from adding the view-source prefix)
51IN_PROC_BROWSER_TEST_F(ViewSourceTest, DoesBrowserConsumeViewSourcePrefix) {
52  ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
53
54  // First we navigate to google.html.
55  GURL url(embedded_test_server()->GetURL(kTestHtml));
56  ui_test_utils::NavigateToURL(browser(), url);
57
58  // Then we navigate to the same url but with the "view-source:" prefix.
59  GURL url_viewsource(content::kViewSourceScheme + std::string(":") +
60                      url.spec());
61  ui_test_utils::NavigateToURL(browser(), url_viewsource);
62
63  // The URL should still be prefixed with "view-source:".
64  EXPECT_EQ(url_viewsource.spec(),
65            browser()->tab_strip_model()->GetActiveWebContents()->
66                GetURL().spec());
67}
68
69// Make sure that when looking at the actual page, we can select "View Source"
70// from the menu.
71IN_PROC_BROWSER_TEST_F(ViewSourceTest, ViewSourceInMenuEnabledOnANormalPage) {
72  ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
73
74  GURL url(embedded_test_server()->GetURL(kTestHtml));
75  ui_test_utils::NavigateToURL(browser(), url);
76
77  EXPECT_TRUE(chrome::CanViewSource(browser()));
78}
79
80// For page that is media content, make sure that we cannot select "View Source"
81// See http://crbug.com/83714
82IN_PROC_BROWSER_TEST_F(ViewSourceTest, ViewSourceInMenuDisabledOnAMediaPage) {
83  ASSERT_TRUE(test_server()->Start());
84
85  GURL url(test_server()->GetURL(kTestMedia));
86  ui_test_utils::NavigateToURL(browser(), url);
87
88  const char* mime_type = browser()->tab_strip_model()->GetActiveWebContents()->
89      GetContentsMimeType().c_str();
90
91  EXPECT_STREQ("audio/wav", mime_type);
92  EXPECT_FALSE(chrome::CanViewSource(browser()));
93}
94
95// Make sure that when looking at the page source, we can't select "View Source"
96// from the menu.
97IN_PROC_BROWSER_TEST_F(ViewSourceTest,
98                       ViewSourceInMenuDisabledWhileViewingSource) {
99  ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
100
101  GURL url_viewsource(content::kViewSourceScheme + std::string(":") +
102                      embedded_test_server()->GetURL(kTestHtml).spec());
103  ui_test_utils::NavigateToURL(browser(), url_viewsource);
104
105  EXPECT_FALSE(chrome::CanViewSource(browser()));
106}
107
108// Tests that reload initiated by the script on the view-source page leaves
109// the page in view-source mode.
110// Times out on Mac, Windows, ChromeOS Linux: crbug.com/162080
111IN_PROC_BROWSER_TEST_F(ViewSourceTest, DISABLED_TestViewSourceReload) {
112  ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
113
114  GURL url_viewsource(content::kViewSourceScheme + std::string(":") +
115                      embedded_test_server()->GetURL(kTestHtml).spec());
116
117  content::WindowedNotificationObserver observer(
118      content::NOTIFICATION_LOAD_STOP,
119      content::NotificationService::AllSources());
120  ui_test_utils::NavigateToURL(browser(), url_viewsource);
121  observer.Wait();
122
123  ASSERT_TRUE(
124      content::ExecuteScript(browser()->tab_strip_model()->GetWebContentsAt(0),
125                             "window.location.reload();"));
126
127  content::WindowedNotificationObserver observer2(
128      content::NOTIFICATION_LOAD_STOP,
129      content::NotificationService::AllSources());
130  observer2.Wait();
131  ASSERT_TRUE(browser()->tab_strip_model()->GetWebContentsAt(0)->
132                  GetController().GetActiveEntry()->IsViewSourceMode());
133}
134