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/files/file_enumerator.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/pdf/pdf_browsertest_base.h"
10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11 #include "chrome/test/base/ui_test_utils.h"
12 #include "content/public/browser/navigation_controller.h"
13 #include "content/public/browser/notification_source.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/test/browser_test_utils.h"
17 #include "third_party/WebKit/public/web/WebInputEvent.h"
19 using content::NavigationController
;
20 using content::WebContents
;
22 // Note: All tests in here require the internal PDF plugin, so they're disabled
23 // in non-official builds. We still compile them though, to prevent bitrot.
27 // Tests basic PDF rendering. This can be broken depending on bad merges with
28 // the vendor, so it's important that we have basic sanity checking.
29 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_LINUX)
30 #define MAYBE_Basic Basic
32 #define MAYBE_Basic DISABLED_Basic
34 IN_PROC_BROWSER_TEST_F(PDFBrowserTest
, MAYBE_Basic
) {
35 ASSERT_NO_FATAL_FAILURE(Load());
36 ASSERT_NO_FATAL_FAILURE(WaitForResponse());
37 // OS X uses CoreText, and FreeType renders slightly different on Linux and
39 #if defined(OS_MACOSX)
40 // The bots render differently than locally, see http://crbug.com/142531.
41 ASSERT_TRUE(VerifySnapshot("pdf_browsertest_mac.png") ||
42 VerifySnapshot("pdf_browsertest_mac2.png"));
43 #elif defined(OS_LINUX)
44 ASSERT_TRUE(VerifySnapshot("pdf_browsertest_linux.png"));
46 ASSERT_TRUE(VerifySnapshot("pdf_browsertest.png"));
50 #if defined(GOOGLE_CHROME_BUILD) && (defined(OS_WIN) || defined(OS_LINUX))
51 #define MAYBE_Scroll Scroll
53 // TODO(thestig): http://crbug.com/79837, http://crbug.com/332778
54 #define MAYBE_Scroll DISABLED_Scroll
56 // Tests that scrolling works.
57 IN_PROC_BROWSER_TEST_F(PDFBrowserTest
, MAYBE_Scroll
) {
58 ASSERT_NO_FATAL_FAILURE(Load());
60 // We use wheel mouse event since that's the only one we can easily push to
61 // the renderer. There's no way to push a cross-platform keyboard event at
63 blink::WebMouseWheelEvent wheel_event
;
64 wheel_event
.type
= blink::WebInputEvent::MouseWheel
;
65 wheel_event
.deltaY
= -200;
66 wheel_event
.wheelTicksY
= -2;
67 WebContents
* web_contents
=
68 browser()->tab_strip_model()->GetActiveWebContents();
69 web_contents
->GetRenderViewHost()->ForwardWheelEvent(wheel_event
);
70 ASSERT_NO_FATAL_FAILURE(WaitForResponse());
73 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
74 browser()->tab_strip_model()->GetActiveWebContents(),
75 "window.domAutomationController.send(plugin.pageYOffset())",
77 ASSERT_GT(y_offset
, 0);
80 const int kLoadingNumberOfParts
= 10;
82 // Tests that loading async pdfs works correctly (i.e. document fully loads).
83 // This also loads all documents that used to crash, to ensure we don't have
85 // If it flakes, reopen http://crbug.com/74548.
86 #if defined(GOOGLE_CHROME_BUILD)
87 #define MAYBE_Loading Loading
89 #define MAYBE_Loading DISABLED_Loading
91 IN_PROC_BROWSER_TEST_P(PDFBrowserTest
, MAYBE_Loading
) {
92 ASSERT_TRUE(pdf_test_server()->InitializeAndWaitUntilReady());
94 NavigationController
* controller
=
95 &(browser()->tab_strip_model()->GetActiveWebContents()->GetController());
96 content::NotificationRegistrar registrar
;
98 content::NOTIFICATION_LOAD_STOP
,
99 content::Source
<NavigationController
>(controller
));
100 std::string base_url
= std::string("/");
102 base::FileEnumerator
file_enumerator(
103 ui_test_utils::GetTestFilePath(
104 base::FilePath(FILE_PATH_LITERAL("pdf_private")), base::FilePath()),
106 base::FileEnumerator::FILES
,
107 FILE_PATH_LITERAL("*.pdf"));
108 for (base::FilePath file_path
= file_enumerator
.Next();
110 file_path
= file_enumerator
.Next()) {
111 std::string filename
= file_path
.BaseName().MaybeAsASCII();
112 ASSERT_FALSE(filename
.empty());
114 #if defined(OS_POSIX)
115 if (filename
== "sample.pdf")
116 continue; // Crashes on Mac and Linux. http://crbug.com/63549
119 // Split the test into smaller sub-tests. Each one only loads
121 if (static_cast<int>(base::Hash(filename
) % kLoadingNumberOfParts
) !=
126 LOG(WARNING
) << "PDFBrowserTest.Loading: " << filename
;
128 GURL url
= pdf_test_server()->GetURL(base_url
+ filename
);
129 ui_test_utils::NavigateToURL(browser(), url
);
132 int last_count
= load_stop_notification_count();
133 // We might get extraneous chrome::LOAD_STOP notifications when
134 // doing async loading. This happens when the first loader is cancelled
135 // and before creating a byte-range request loader.
136 bool complete
= false;
137 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
138 browser()->tab_strip_model()->GetActiveWebContents(),
139 "window.domAutomationController.send(plugin.documentLoadComplete())",
144 // Check if the LOAD_STOP notification could have come while we run a
145 // nested message loop for the JS call.
146 if (last_count
!= load_stop_notification_count())
148 content::WaitForLoadStop(
149 browser()->tab_strip_model()->GetActiveWebContents());
154 INSTANTIATE_TEST_CASE_P(PDFTestFiles
,
156 testing::Range(0, kLoadingNumberOfParts
));
158 #if defined(GOOGLE_CHROME_BUILD) && (defined(OS_WIN) || defined(OS_LINUX))
159 #define MAYBE_Action Action
161 // http://crbug.com/315160
162 #define MAYBE_Action DISABLED_Action
164 IN_PROC_BROWSER_TEST_F(PDFBrowserTest
, MAYBE_Action
) {
165 ASSERT_NO_FATAL_FAILURE(Load());
167 ASSERT_TRUE(content::ExecuteScript(
168 browser()->tab_strip_model()->GetActiveWebContents(),
169 "document.getElementsByName('plugin')[0].fitToHeight();"));
171 std::string zoom1
, zoom2
;
172 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
173 browser()->tab_strip_model()->GetActiveWebContents(),
174 "window.domAutomationController.send("
175 " document.getElementsByName('plugin')[0].getZoomLevel().toString())",
178 ASSERT_TRUE(content::ExecuteScript(
179 browser()->tab_strip_model()->GetActiveWebContents(),
180 "document.getElementsByName('plugin')[0].fitToWidth();"));
182 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
183 browser()->tab_strip_model()->GetActiveWebContents(),
184 "window.domAutomationController.send("
185 " document.getElementsByName('plugin')[0].getZoomLevel().toString())",
187 ASSERT_NE(zoom1
, zoom2
);
190 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_LINUX)
191 #define MAYBE_OnLoadAndReload OnLoadAndReload
193 // Flaky as per http://crbug.com/74549.
194 #define MAYBE_OnLoadAndReload DISABLED_OnLoadAndReload
196 IN_PROC_BROWSER_TEST_F(PDFBrowserTest
, MAYBE_OnLoadAndReload
) {
197 ASSERT_TRUE(pdf_test_server()->InitializeAndWaitUntilReady());
199 GURL url
= pdf_test_server()->GetURL("/onload_reload.html");
200 ui_test_utils::NavigateToURL(browser(), url
);
201 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
203 content::WindowedNotificationObserver
observer(
204 content::NOTIFICATION_LOAD_STOP
,
205 content::Source
<NavigationController
>(
206 &contents
->GetController()));
207 ASSERT_TRUE(content::ExecuteScript(
208 browser()->tab_strip_model()->GetActiveWebContents(),
212 ASSERT_EQ("success", contents
->GetURL().query());