NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / pdf / pdf_browsertest.cc
blobaeb968381bcb203dff8e67560373aa24160737ef
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/file_util.h"
6 #include "base/files/file.h"
7 #include "base/files/file_enumerator.h"
8 #include "base/hash.h"
9 #include "base/path_service.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chrome/test/base/ui_test_utils.h"
20 #include "content/public/browser/navigation_controller.h"
21 #include "content/public/browser/notification_observer.h"
22 #include "content/public/browser/render_view_host.h"
23 #include "content/public/browser/web_contents.h"
24 #include "content/public/test/browser_test_utils.h"
25 #include "net/test/embedded_test_server/embedded_test_server.h"
26 #include "third_party/skia/include/core/SkBitmap.h"
27 #include "ui/base/clipboard/clipboard.h"
28 #include "ui/gfx/codec/png_codec.h"
29 #include "ui/gfx/screen.h"
31 using content::NavigationController;
32 using content::WebContents;
34 // Note: All tests in here require the internal PDF plugin, so they're disabled
35 // in non-official builds. We still compile them though, to prevent bitrot.
37 namespace {
39 // Include things like browser frame and scrollbar and make sure we're bigger
40 // than the test pdf document.
41 static const int kBrowserWidth = 1000;
42 static const int kBrowserHeight = 600;
44 class PDFBrowserTest : public InProcessBrowserTest,
45 public testing::WithParamInterface<int>,
46 public content::NotificationObserver {
47 public:
48 PDFBrowserTest()
49 : snapshot_different_(true),
50 next_dummy_search_value_(0),
51 load_stop_notification_count_(0) {
52 base::FilePath src_dir;
53 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &src_dir));
54 pdf_test_server_.ServeFilesFromDirectory(src_dir.AppendASCII("pdf/test"));
57 protected:
58 // Use our own TestServer so that we can serve files from the pdf directory.
59 net::test_server::EmbeddedTestServer* pdf_test_server() {
60 return &pdf_test_server_;
63 int load_stop_notification_count() const {
64 return load_stop_notification_count_;
67 base::FilePath GetPDFTestDir() {
68 return base::FilePath(base::FilePath::kCurrentDirectory).AppendASCII("..").
69 AppendASCII("..").AppendASCII("..").AppendASCII("pdf").
70 AppendASCII("test");
73 void Load() {
74 // Make sure to set the window size before rendering, as otherwise rendering
75 // to a smaller window and then expanding leads to slight anti-aliasing
76 // differences of the text and the pixel comparison fails.
77 gfx::Rect bounds(gfx::Rect(0, 0, kBrowserWidth, kBrowserHeight));
78 gfx::Rect screen_bounds =
79 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().bounds();
80 ASSERT_GT(screen_bounds.width(), kBrowserWidth);
81 ASSERT_GT(screen_bounds.height(), kBrowserHeight);
82 browser()->window()->SetBounds(bounds);
84 GURL url(ui_test_utils::GetTestUrl(
85 GetPDFTestDir(),
86 base::FilePath(FILE_PATH_LITERAL("pdf_browsertest.pdf"))));
87 ui_test_utils::NavigateToURL(browser(), url);
90 bool VerifySnapshot(const std::string& expected_filename) {
91 snapshot_different_ = true;
92 expected_filename_ = expected_filename;
93 WebContents* web_contents =
94 browser()->tab_strip_model()->GetActiveWebContents();
95 DCHECK(web_contents);
97 content::RenderWidgetHost* rwh = web_contents->GetRenderViewHost();
98 rwh->GetSnapshotFromRenderer(gfx::Rect(), base::Bind(
99 &PDFBrowserTest::GetSnapshotFromRendererCallback, this));
101 content::RunMessageLoop();
103 if (snapshot_different_) {
104 LOG(INFO) << "Rendering didn't match, see result " <<
105 snapshot_filename_.value().c_str();
107 return !snapshot_different_;
110 void WaitForResponse() {
111 // Even if the plugin has loaded the data or scrolled, because of how
112 // pepper painting works, we might not have the data. One way to force this
113 // to be flushed is to do a find operation, since on this two-page test
114 // document, it'll wait for us to flush the renderer message loop twice and
115 // also the browser's once, at which point we're guaranteed to have updated
116 // the backingstore. Hacky, but it works.
117 // Note that we need to change the text each time, because if we don't the
118 // renderer code will think the second message is to go to next result, but
119 // there are none so the plugin will assert.
121 base::string16 query = base::UTF8ToUTF16(
122 std::string("xyzxyz" + base::IntToString(next_dummy_search_value_++)));
123 ASSERT_EQ(0, ui_test_utils::FindInPage(
124 browser()->tab_strip_model()->GetActiveWebContents(),
125 query, true, false, NULL, NULL));
128 private:
129 void GetSnapshotFromRendererCallback(bool success,
130 const SkBitmap& bitmap) {
131 base::MessageLoopForUI::current()->Quit();
132 ASSERT_EQ(success, true);
133 base::FilePath reference = ui_test_utils::GetTestFilePath(
134 GetPDFTestDir(),
135 base::FilePath().AppendASCII(expected_filename_));
136 base::File::Info info;
137 ASSERT_TRUE(base::GetFileInfo(reference, &info));
138 int size = static_cast<size_t>(info.size);
139 scoped_ptr<char[]> data(new char[size]);
140 ASSERT_EQ(size, base::ReadFile(reference, data.get(), size));
142 int w, h;
143 std::vector<unsigned char> decoded;
144 ASSERT_TRUE(gfx::PNGCodec::Decode(
145 reinterpret_cast<unsigned char*>(data.get()), size,
146 gfx::PNGCodec::FORMAT_BGRA, &decoded, &w, &h));
147 int32* ref_pixels = reinterpret_cast<int32*>(&decoded[0]);
149 int32* pixels = static_cast<int32*>(bitmap.getPixels());
151 // Get the background color, and use it to figure out the x-offsets in
152 // each image. The reason is that depending on the theme in the OS, the
153 // same browser width can lead to slightly different plugin sizes, so the
154 // pdf content will start at different x offsets.
155 // Also note that the images we saved are cut off before the scrollbar, as
156 // that'll change depending on the theme, and also cut off vertically so
157 // that the ui controls don't show up, as those fade-in and so the timing
158 // will affect their transparency.
159 int32 bg_color = ref_pixels[0];
160 int ref_x_offset, snapshot_x_offset;
161 for (ref_x_offset = 0; ref_x_offset < w; ++ref_x_offset) {
162 if (ref_pixels[ref_x_offset] != bg_color)
163 break;
166 for (snapshot_x_offset = 0; snapshot_x_offset < bitmap.width();
167 ++snapshot_x_offset) {
168 if (pixels[snapshot_x_offset] != bg_color)
169 break;
172 int x_max = std::min(
173 w - ref_x_offset, bitmap.width() - snapshot_x_offset);
174 int y_max = std::min(h, bitmap.height());
175 int stride = bitmap.rowBytes();
176 snapshot_different_ = false;
177 for (int y = 0; y < y_max && !snapshot_different_; ++y) {
178 for (int x = 0; x < x_max && !snapshot_different_; ++x) {
179 if (pixels[y * stride / sizeof(int32) + x + snapshot_x_offset] !=
180 ref_pixels[y * w + x + ref_x_offset])
181 snapshot_different_ = true;
185 if (snapshot_different_) {
186 std::vector<unsigned char> png_data;
187 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_data);
188 if (base::CreateTemporaryFile(&snapshot_filename_)) {
189 file_util::WriteFile(snapshot_filename_,
190 reinterpret_cast<char*>(&png_data[0]), png_data.size());
195 // content::NotificationObserver
196 virtual void Observe(int type,
197 const content::NotificationSource& source,
198 const content::NotificationDetails& details) {
199 if (type == content::NOTIFICATION_LOAD_STOP) {
200 load_stop_notification_count_++;
204 // True if the snapshot differed from the expected value.
205 bool snapshot_different_;
206 // Internal variable used to synchronize to the renderer.
207 int next_dummy_search_value_;
208 // The filename of the bitmap to compare the snapshot to.
209 std::string expected_filename_;
210 // If the snapshot is different, holds the location where it's saved.
211 base::FilePath snapshot_filename_;
212 // How many times we've seen chrome::LOAD_STOP.
213 int load_stop_notification_count_;
215 net::test_server::EmbeddedTestServer pdf_test_server_;
219 // TODO(thestig): http://crbug.com/79837
220 // Tests basic PDF rendering. This can be broken depending on bad merges with
221 // the vendor, so it's important that we have basic sanity checking.
222 IN_PROC_BROWSER_TEST_F(PDFBrowserTest, DISABLED_Basic) {
223 ASSERT_NO_FATAL_FAILURE(Load());
224 ASSERT_NO_FATAL_FAILURE(WaitForResponse());
225 // OS X uses CoreText, and FreeType renders slightly different on Linux and
226 // Win.
227 #if defined(OS_MACOSX)
228 // The bots render differently than locally, see http://crbug.com/142531.
229 ASSERT_TRUE(VerifySnapshot("pdf_browsertest_mac.png") ||
230 VerifySnapshot("pdf_browsertest_mac2.png"));
231 #elif defined(OS_LINUX)
232 ASSERT_TRUE(VerifySnapshot("pdf_browsertest_linux.png"));
233 #else
234 ASSERT_TRUE(VerifySnapshot("pdf_browsertest.png"));
235 #endif
238 #if (!defined(GOOGLE_CHROME_BUILD) || defined(OS_CHROMEOS)) || \
239 (defined(OS_LINUX) || defined(OS_MACOSX))
240 // TODO(sanjeevr): http://crbug.com/79837, http://crbug.com/332778
241 #define MAYBE_Scroll DISABLED_Scroll
242 #else
243 #define MAYBE_Scroll Scroll
244 #endif
245 // Tests that scrolling works.
246 IN_PROC_BROWSER_TEST_F(PDFBrowserTest, MAYBE_Scroll) {
247 ASSERT_NO_FATAL_FAILURE(Load());
249 // We use wheel mouse event since that's the only one we can easily push to
250 // the renderer. There's no way to push a cross-platform keyboard event at
251 // the moment.
252 blink::WebMouseWheelEvent wheel_event;
253 wheel_event.type = blink::WebInputEvent::MouseWheel;
254 wheel_event.deltaY = -200;
255 wheel_event.wheelTicksY = -2;
256 WebContents* web_contents =
257 browser()->tab_strip_model()->GetActiveWebContents();
258 web_contents->GetRenderViewHost()->ForwardWheelEvent(wheel_event);
259 ASSERT_NO_FATAL_FAILURE(WaitForResponse());
261 int y_offset = 0;
262 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
263 browser()->tab_strip_model()->GetActiveWebContents(),
264 "window.domAutomationController.send(plugin.pageYOffset())",
265 &y_offset));
266 ASSERT_GT(y_offset, 0);
269 #if (!defined(GOOGLE_CHROME_BUILD) || defined(OS_CHROMEOS)) || \
270 (defined(OS_LINUX) || defined(OS_MACOSX))
271 // TODO(thestig): http://crbug.com/79837, http://crbug.com/329912
272 #define MAYBE_FindAndCopy DISABLED_FindAndCopy
273 #else
274 #define MAYBE_FindAndCopy FindAndCopy
275 #endif
276 IN_PROC_BROWSER_TEST_F(PDFBrowserTest, MAYBE_FindAndCopy) {
277 ASSERT_NO_FATAL_FAILURE(Load());
278 // Verifies that find in page works.
279 ASSERT_EQ(3, ui_test_utils::FindInPage(
280 browser()->tab_strip_model()->GetActiveWebContents(),
281 base::UTF8ToUTF16("adipiscing"),
282 true, false, NULL, NULL));
284 // Verify that copying selected text works.
285 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
286 // Reset the clipboard first.
287 ui::Clipboard::ObjectMap objects;
288 ui::Clipboard::ObjectMapParams params;
289 params.push_back(std::vector<char>());
290 objects[ui::Clipboard::CBF_TEXT] = params;
291 clipboard->WriteObjects(ui::CLIPBOARD_TYPE_COPY_PASTE, objects);
293 browser()->tab_strip_model()->GetActiveWebContents()->
294 GetRenderViewHost()->Copy();
295 ASSERT_NO_FATAL_FAILURE(WaitForResponse());
297 std::string text;
298 clipboard->ReadAsciiText(ui::CLIPBOARD_TYPE_COPY_PASTE, &text);
299 ASSERT_EQ("adipiscing", text);
302 const int kLoadingNumberOfParts = 10;
304 // Tests that loading async pdfs works correctly (i.e. document fully loads).
305 // This also loads all documents that used to crash, to ensure we don't have
306 // regressions.
307 // If it flakes, reopen http://crbug.com/74548.
308 #if !defined(GOOGLE_CHROME_BUILD)
309 #define MAYBE_Loading DISABLED_Loading
310 #else
311 #define MAYBE_Loading Loading
312 #endif
313 IN_PROC_BROWSER_TEST_P(PDFBrowserTest, MAYBE_Loading) {
314 ASSERT_TRUE(pdf_test_server()->InitializeAndWaitUntilReady());
316 NavigationController* controller =
317 &(browser()->tab_strip_model()->GetActiveWebContents()->GetController());
318 content::NotificationRegistrar registrar;
319 registrar.Add(this,
320 content::NOTIFICATION_LOAD_STOP,
321 content::Source<NavigationController>(controller));
322 std::string base_url = std::string("/");
324 base::FileEnumerator file_enumerator(
325 ui_test_utils::GetTestFilePath(GetPDFTestDir(), base::FilePath()),
326 false,
327 base::FileEnumerator::FILES,
328 FILE_PATH_LITERAL("*.pdf"));
329 for (base::FilePath file_path = file_enumerator.Next();
330 !file_path.empty();
331 file_path = file_enumerator.Next()) {
332 std::string filename = file_path.BaseName().MaybeAsASCII();
333 ASSERT_FALSE(filename.empty());
335 #if defined(OS_POSIX)
336 if (filename == "sample.pdf")
337 continue; // Crashes on Mac and Linux. http://crbug.com/63549
338 #endif
340 // Split the test into smaller sub-tests. Each one only loads
341 // every k-th file.
342 if (static_cast<int>(base::Hash(filename) % kLoadingNumberOfParts) !=
343 GetParam()) {
344 continue;
347 LOG(WARNING) << "PDFBrowserTest.Loading: " << filename;
349 GURL url = pdf_test_server()->GetURL(base_url + filename);
350 ui_test_utils::NavigateToURL(browser(), url);
352 while (true) {
353 int last_count = load_stop_notification_count();
354 // We might get extraneous chrome::LOAD_STOP notifications when
355 // doing async loading. This happens when the first loader is cancelled
356 // and before creating a byte-range request loader.
357 bool complete = false;
358 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
359 browser()->tab_strip_model()->GetActiveWebContents(),
360 "window.domAutomationController.send(plugin.documentLoadComplete())",
361 &complete));
362 if (complete)
363 break;
365 // Check if the LOAD_STOP notification could have come while we run a
366 // nested message loop for the JS call.
367 if (last_count != load_stop_notification_count())
368 continue;
369 content::WaitForLoadStop(
370 browser()->tab_strip_model()->GetActiveWebContents());
375 INSTANTIATE_TEST_CASE_P(PDFTestFiles,
376 PDFBrowserTest,
377 testing::Range(0, kLoadingNumberOfParts));
379 #if !defined(GOOGLE_CHROME_BUILD) || defined(OS_MACOSX)
380 // http://crbug.com/315160
381 #define MAYBE_Action DISABLED_Action
382 #else
383 #define MAYBE_Action Action
384 #endif
385 IN_PROC_BROWSER_TEST_F(PDFBrowserTest, MAYBE_Action) {
386 ASSERT_NO_FATAL_FAILURE(Load());
388 ASSERT_TRUE(content::ExecuteScript(
389 browser()->tab_strip_model()->GetActiveWebContents(),
390 "document.getElementsByName('plugin')[0].fitToHeight();"));
392 std::string zoom1, zoom2;
393 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
394 browser()->tab_strip_model()->GetActiveWebContents(),
395 "window.domAutomationController.send("
396 " document.getElementsByName('plugin')[0].getZoomLevel().toString())",
397 &zoom1));
399 ASSERT_TRUE(content::ExecuteScript(
400 browser()->tab_strip_model()->GetActiveWebContents(),
401 "document.getElementsByName('plugin')[0].fitToWidth();"));
403 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
404 browser()->tab_strip_model()->GetActiveWebContents(),
405 "window.domAutomationController.send("
406 " document.getElementsByName('plugin')[0].getZoomLevel().toString())",
407 &zoom2));
408 ASSERT_NE(zoom1, zoom2);
411 // Flaky as per http://crbug.com/74549.
412 IN_PROC_BROWSER_TEST_F(PDFBrowserTest, DISABLED_OnLoadAndReload) {
413 ASSERT_TRUE(pdf_test_server()->InitializeAndWaitUntilReady());
415 GURL url = pdf_test_server()->GetURL("/onload_reload.html");
416 ui_test_utils::NavigateToURL(browser(), url);
418 content::WindowedNotificationObserver observer(
419 content::NOTIFICATION_LOAD_STOP,
420 content::Source<NavigationController>(
421 &browser()->tab_strip_model()->GetActiveWebContents()->
422 GetController()));
423 ASSERT_TRUE(content::ExecuteScript(
424 browser()->tab_strip_model()->GetActiveWebContents(),
425 "reloadPDF();"));
426 observer.Wait();
428 ASSERT_EQ("success",
429 browser()->tab_strip_model()->GetActiveWebContents()->
430 GetURL().query());
433 } // namespace