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.
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"
23 const char kTestHtml
[] = "/viewsource/test.html";
24 const char kTestMedia
[] = "files/media/pink_noise_140ms.wav";
27 typedef InProcessBrowserTest ViewSourceTest
;
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.
33 IN_PROC_BROWSER_TEST_F(ViewSourceTest
, DoesBrowserRenderInViewSource
) {
34 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
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
);
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());
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)
51 IN_PROC_BROWSER_TEST_F(ViewSourceTest
, DoesBrowserConsumeViewSourcePrefix
) {
52 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
54 // First we navigate to google.html.
55 GURL
url(embedded_test_server()->GetURL(kTestHtml
));
56 ui_test_utils::NavigateToURL(browser(), url
);
58 // Then we navigate to the same url but with the "view-source:" prefix.
59 GURL
url_viewsource(content::kViewSourceScheme
+ std::string(":") +
61 ui_test_utils::NavigateToURL(browser(), url_viewsource
);
63 // The URL should still be prefixed with "view-source:".
64 EXPECT_EQ(url_viewsource
.spec(),
65 browser()->tab_strip_model()->GetActiveWebContents()->
69 // Make sure that when looking at the actual page, we can select "View Source"
71 IN_PROC_BROWSER_TEST_F(ViewSourceTest
, ViewSourceInMenuEnabledOnANormalPage
) {
72 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
74 GURL
url(embedded_test_server()->GetURL(kTestHtml
));
75 ui_test_utils::NavigateToURL(browser(), url
);
77 EXPECT_TRUE(chrome::CanViewSource(browser()));
80 // For page that is media content, make sure that we cannot select "View Source"
81 // See http://crbug.com/83714
82 IN_PROC_BROWSER_TEST_F(ViewSourceTest
, ViewSourceInMenuDisabledOnAMediaPage
) {
83 ASSERT_TRUE(test_server()->Start());
85 GURL
url(test_server()->GetURL(kTestMedia
));
86 ui_test_utils::NavigateToURL(browser(), url
);
88 const char* mime_type
= browser()->tab_strip_model()->GetActiveWebContents()->
89 GetContentsMimeType().c_str();
91 EXPECT_STREQ("audio/wav", mime_type
);
92 EXPECT_FALSE(chrome::CanViewSource(browser()));
95 // Make sure that when looking at the page source, we can't select "View Source"
97 IN_PROC_BROWSER_TEST_F(ViewSourceTest
,
98 ViewSourceInMenuDisabledWhileViewingSource
) {
99 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
101 GURL
url_viewsource(content::kViewSourceScheme
+ std::string(":") +
102 embedded_test_server()->GetURL(kTestHtml
).spec());
103 ui_test_utils::NavigateToURL(browser(), url_viewsource
);
105 EXPECT_FALSE(chrome::CanViewSource(browser()));
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
111 IN_PROC_BROWSER_TEST_F(ViewSourceTest
, DISABLED_TestViewSourceReload
) {
112 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
114 GURL
url_viewsource(content::kViewSourceScheme
+ std::string(":") +
115 embedded_test_server()->GetURL(kTestHtml
).spec());
117 content::WindowedNotificationObserver
observer(
118 content::NOTIFICATION_LOAD_STOP
,
119 content::NotificationService::AllSources());
120 ui_test_utils::NavigateToURL(browser(), url_viewsource
);
124 content::ExecuteScript(browser()->tab_strip_model()->GetWebContentsAt(0),
125 "window.location.reload();"));
127 content::WindowedNotificationObserver
observer2(
128 content::NOTIFICATION_LOAD_STOP
,
129 content::NotificationService::AllSources());
131 ASSERT_TRUE(browser()->tab_strip_model()->GetWebContentsAt(0)->
132 GetController().GetActiveEntry()->IsViewSourceMode());