OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 #include <algorithm> |
6 #include <fstream> | 6 #include <fstream> |
7 #include <iostream> | 7 #include <iostream> |
8 #include <iterator> | 8 #include <iterator> |
9 #include <limits> | 9 #include <limits> |
10 #include <string> | 10 #include <string> |
11 #include <utility> | 11 #include <utility> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/bind.h" | 14 #include "base/bind.h" |
15 #include "base/callback.h" | 15 #include "base/callback.h" |
16 #include "base/files/file.h" | 16 #include "base/files/file.h" |
17 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
18 #include "base/files/file_util.h" | 18 #include "base/files/file_util.h" |
19 #include "base/files/scoped_temp_dir.h" | 19 #include "base/files/scoped_temp_dir.h" |
20 #include "base/logging.h" | 20 #include "base/logging.h" |
21 #include "base/md5.h" | 21 #include "base/md5.h" |
22 #include "base/memory/scoped_ptr.h" | 22 #include "base/memory/scoped_ptr.h" |
23 #include "base/path_service.h" | 23 #include "base/path_service.h" |
24 #include "base/run_loop.h" | 24 #include "base/run_loop.h" |
| 25 #include "base/scoped_native_library.h" |
25 #include "base/strings/string_split.h" | 26 #include "base/strings/string_split.h" |
26 #include "base/strings/utf_string_conversions.h" | 27 #include "base/strings/utf_string_conversions.h" |
27 #include "chrome/browser/printing/print_preview_dialog_controller.h" | 28 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
28 #include "chrome/browser/ui/browser.h" | 29 #include "chrome/browser/ui/browser.h" |
29 #include "chrome/browser/ui/browser_commands.h" | 30 #include "chrome/browser/ui/browser_commands.h" |
30 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 31 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
31 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" | 32 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" |
32 #include "chrome/common/chrome_paths.h" | 33 #include "chrome/common/chrome_paths.h" |
33 #include "chrome/test/base/in_process_browser_test.h" | 34 #include "chrome/test/base/in_process_browser_test.h" |
34 #include "chrome/test/base/ui_test_utils.h" | 35 #include "chrome/test/base/ui_test_utils.h" |
35 #include "components/printing/common/print_messages.h" | 36 #include "components/printing/common/print_messages.h" |
36 #include "content/public/browser/web_contents.h" | 37 #include "content/public/browser/web_contents.h" |
37 #include "content/public/browser/web_ui_message_handler.h" | 38 #include "content/public/browser/web_ui_message_handler.h" |
38 #include "content/public/test/browser_test_utils.h" | 39 #include "content/public/test/browser_test_utils.h" |
39 #include "ipc/ipc_message_macros.h" | 40 #include "ipc/ipc_message_macros.h" |
40 #include "net/base/filename_util.h" | 41 #include "net/base/filename_util.h" |
41 #include "pdf/pdf.h" | |
42 #include "printing/pdf_render_settings.h" | 42 #include "printing/pdf_render_settings.h" |
43 #include "printing/units.h" | 43 #include "printing/units.h" |
44 #include "ui/gfx/codec/png_codec.h" | 44 #include "ui/gfx/codec/png_codec.h" |
45 #include "ui/gfx/geometry/rect.h" | 45 #include "ui/gfx/geometry/rect.h" |
46 #include "url/gurl.h" | 46 #include "url/gurl.h" |
47 | 47 |
48 #if defined(OS_WIN) | 48 #if defined(OS_WIN) |
49 #include <fcntl.h> | 49 #include <fcntl.h> |
50 #include <io.h> | 50 #include <io.h> |
51 #endif | 51 #endif |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 chrome::Print(browser()); | 316 chrome::Print(browser()); |
317 loop.Run(); | 317 loop.Run(); |
318 | 318 |
319 // Need to check whether the save was successful. Ending the loop only | 319 // Need to check whether the save was successful. Ending the loop only |
320 // means the save was attempted. | 320 // means the save was attempted. |
321 base::File pdf_file( | 321 base::File pdf_file( |
322 pdf_file_save_path_, base::File::FLAG_OPEN | base::File::FLAG_READ); | 322 pdf_file_save_path_, base::File::FLAG_OPEN | base::File::FLAG_READ); |
323 ASSERT_TRUE(pdf_file.IsValid()); | 323 ASSERT_TRUE(pdf_file.IsValid()); |
324 } | 324 } |
325 | 325 |
| 326 // Initializes function pointers from the PDF library. Called once when the |
| 327 // test starts. The library is closed when the browser test ends. |
| 328 void InitPdfFunctions() { |
| 329 base::FilePath pdf_module_path; |
| 330 |
| 331 ASSERT_TRUE(PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_module_path)); |
| 332 ASSERT_TRUE(base::PathExists(pdf_module_path)); |
| 333 pdf_lib_.Reset(base::LoadNativeLibrary(pdf_module_path, NULL)); |
| 334 |
| 335 ASSERT_TRUE(pdf_lib_.is_valid()); |
| 336 pdf_to_bitmap_func_ = |
| 337 reinterpret_cast<PDFPageToBitmapProc>( |
| 338 pdf_lib_.GetFunctionPointer("RenderPDFPageToBitmap")); |
| 339 |
| 340 pdf_doc_info_func_ = |
| 341 reinterpret_cast<GetPDFDocInfoProc>( |
| 342 pdf_lib_.GetFunctionPointer("GetPDFDocInfo")); |
| 343 |
| 344 pdf_page_size_func_ = |
| 345 reinterpret_cast<GetPDFPageSizeByIndexProc>( |
| 346 pdf_lib_.GetFunctionPointer("GetPDFPageSizeByIndex")); |
| 347 |
| 348 ASSERT_TRUE(pdf_to_bitmap_func_); |
| 349 ASSERT_TRUE(pdf_doc_info_func_); |
| 350 ASSERT_TRUE(pdf_page_size_func_); |
| 351 } |
| 352 |
326 // Converts the PDF to a PNG file so that the layout test can do an image | 353 // Converts the PDF to a PNG file so that the layout test can do an image |
327 // diff on this image and a reference image. | 354 // diff on this image and a reference image. |
328 void PdfToPng() { | 355 void PdfToPng() { |
329 int num_pages; | 356 int num_pages; |
330 double max_width_in_points = 0; | 357 double max_width_in_points = 0; |
331 std::vector<uint8_t> bitmap_data; | 358 std::vector<uint8_t> bitmap_data; |
332 double total_height_in_pixels = 0; | 359 double total_height_in_pixels = 0; |
333 std::string pdf_data; | 360 std::string pdf_data; |
334 | 361 |
335 ASSERT_TRUE(base::ReadFileToString(pdf_file_save_path_, &pdf_data)); | 362 ASSERT_TRUE(base::ReadFileToString(pdf_file_save_path_, &pdf_data)); |
336 ASSERT_TRUE(chrome_pdf::GetPDFDocInfo(pdf_data.data(), | 363 ASSERT_TRUE(pdf_doc_info_func_(pdf_data.data(), |
337 pdf_data.size(), | 364 pdf_data.size(), |
338 &num_pages, | 365 &num_pages, |
339 &max_width_in_points)); | 366 &max_width_in_points)); |
340 | 367 |
341 ASSERT_GT(num_pages, 0); | 368 ASSERT_GT(num_pages, 0); |
342 double max_width_in_pixels = | 369 double max_width_in_pixels = |
343 ConvertUnitDouble(max_width_in_points, kPointsPerInch, kDpi); | 370 ConvertUnitDouble(max_width_in_points, kPointsPerInch, kDpi); |
344 | 371 |
345 for (int i = 0; i < num_pages; ++i) { | 372 for (int i = 0; i < num_pages; ++i) { |
346 double width_in_points, height_in_points; | 373 double width_in_points, height_in_points; |
347 ASSERT_TRUE(chrome_pdf::GetPDFPageSizeByIndex(pdf_data.data(), | 374 ASSERT_TRUE(pdf_page_size_func_(pdf_data.data(), |
348 pdf_data.size(), | 375 pdf_data.size(), |
349 i, | 376 i, |
350 &width_in_points, | 377 &width_in_points, |
351 &height_in_points)); | 378 &height_in_points)); |
352 | 379 |
353 double width_in_pixels = ConvertUnitDouble( | 380 double width_in_pixels = ConvertUnitDouble( |
354 width_in_points, kPointsPerInch, kDpi); | 381 width_in_points, kPointsPerInch, kDpi); |
355 double height_in_pixels = ConvertUnitDouble( | 382 double height_in_pixels = ConvertUnitDouble( |
356 height_in_points, kPointsPerInch, kDpi); | 383 height_in_points, kPointsPerInch, kDpi); |
357 | 384 |
358 // The image will be rotated if |width_in_pixels| is greater than | 385 // The image will be rotated if |width_in_pixels| is greater than |
359 // |height_in_pixels|. This is because the page will be rotated to fit | 386 // |height_in_pixels|. This is because the page will be rotated to fit |
360 // within a piece of paper. Therefore, |width_in_pixels| and | 387 // within a piece of paper. Therefore, |width_in_pixels| and |
361 // |height_in_pixels| have to be swapped or else they won't reflect the | 388 // |height_in_pixels| have to be swapped or else they won't reflect the |
362 // dimensions of the rotated page. | 389 // dimensions of the rotated page. |
363 if (width_in_pixels > height_in_pixels) | 390 if (width_in_pixels > height_in_pixels) |
364 std::swap(width_in_pixels, height_in_pixels); | 391 std::swap(width_in_pixels, height_in_pixels); |
365 | 392 |
366 total_height_in_pixels += height_in_pixels; | 393 total_height_in_pixels += height_in_pixels; |
367 gfx::Rect rect(width_in_pixels, height_in_pixels); | 394 gfx::Rect rect(width_in_pixels, height_in_pixels); |
368 PdfRenderSettings settings(rect, kDpi, true); | 395 PdfRenderSettings settings(rect, kDpi, true); |
369 | 396 |
370 int int_max = std::numeric_limits<int>::max(); | 397 int int_max = std::numeric_limits<int>::max(); |
371 if (settings.area().width() > int_max / kColorChannels || | 398 if (settings.area().width() > int_max / kColorChannels || |
372 settings.area().height() > int_max / (kColorChannels * | 399 settings.area().height() > int_max / (kColorChannels * |
373 settings.area().width())) { | 400 settings.area().width())) { |
374 FAIL() << "The dimensions of the image are too large." | 401 FAIL() << "The dimensions of the image are too large." |
375 << "Decrease the DPI or the dimensions of the image."; | 402 << "Decrease the DPI or the dimensions of the image."; |
376 } | 403 } |
377 | 404 |
378 std::vector<uint8_t> page_bitmap_data( | 405 std::vector<uint8_t> page_bitmap_data( |
379 kColorChannels * settings.area().size().GetArea()); | 406 kColorChannels * settings.area().size().GetArea()); |
380 | 407 |
381 ASSERT_TRUE(chrome_pdf::RenderPDFPageToBitmap( | 408 ASSERT_TRUE(pdf_to_bitmap_func_(pdf_data.data(), |
382 pdf_data.data(), | 409 pdf_data.size(), |
383 pdf_data.size(), | 410 i, |
384 i, | 411 page_bitmap_data.data(), |
385 page_bitmap_data.data(), | 412 settings.area().size().width(), |
386 settings.area().size().width(), | 413 settings.area().size().height(), |
387 settings.area().size().height(), | 414 settings.dpi(), |
388 settings.dpi(), | 415 settings.dpi(), |
389 true)); | 416 true)); |
390 FillPng(&page_bitmap_data, | 417 FillPng(&page_bitmap_data, |
391 width_in_pixels, | 418 width_in_pixels, |
392 max_width_in_pixels, | 419 max_width_in_pixels, |
393 settings.area().size().height()); | 420 settings.area().size().height()); |
394 bitmap_data.insert(bitmap_data.end(), | 421 bitmap_data.insert(bitmap_data.end(), |
395 page_bitmap_data.begin(), | 422 page_bitmap_data.begin(), |
396 page_bitmap_data.end()); | 423 page_bitmap_data.end()); |
397 } | 424 } |
398 | 425 |
399 CreatePng(bitmap_data, max_width_in_pixels, total_height_in_pixels); | 426 CreatePng(bitmap_data, max_width_in_pixels, total_height_in_pixels); |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 png_rect.size(), | 565 png_rect.size(), |
539 png_rect.size().width() * kColorChannels, | 566 png_rect.size().width() * kColorChannels, |
540 false, | 567 false, |
541 comments, | 568 comments, |
542 &png_output_)); | 569 &png_output_)); |
543 } | 570 } |
544 | 571 |
545 scoped_ptr<PrintPreviewObserver> print_preview_observer_; | 572 scoped_ptr<PrintPreviewObserver> print_preview_observer_; |
546 base::FilePath pdf_file_save_path_; | 573 base::FilePath pdf_file_save_path_; |
547 | 574 |
| 575 // These typedefs are function pointers to pdflib functions that give |
| 576 // information about the PDF as a whole and about specific pages. |
| 577 |
| 578 // Converts the PDF to a bitmap. |
| 579 typedef bool (*PDFPageToBitmapProc)(const void* pdf_buffer, |
| 580 int pdf_buffer_size, |
| 581 int page_number, |
| 582 void* bitmap_buffer, |
| 583 int bitmap_width, |
| 584 int bitmap_height, |
| 585 int dpi_x, |
| 586 int dpi_y, |
| 587 bool autorotate); |
| 588 |
| 589 // Gets the page count and maximum page width of the PDF in points. |
| 590 typedef bool (*GetPDFDocInfoProc)(const void* pdf_buffer, |
| 591 int buffer_size, |
| 592 int* pages_count, |
| 593 double* max_page_width); |
| 594 |
| 595 // Gets the dimensions of a specific page within a PDF. |
| 596 typedef bool (*GetPDFPageSizeByIndexProc)(const void* pdf_buffer, |
| 597 int buffer_size, |
| 598 int index, |
| 599 double* width, |
| 600 double* height); |
| 601 |
| 602 // Instantiations of the function pointers described above. |
| 603 PDFPageToBitmapProc pdf_to_bitmap_func_; |
| 604 GetPDFDocInfoProc pdf_doc_info_func_; |
| 605 GetPDFPageSizeByIndexProc pdf_page_size_func_; |
| 606 |
| 607 // Used to open up the pdf plugin, which contains the functions above. |
| 608 base::ScopedNativeLibrary pdf_lib_; |
| 609 |
548 // Vector for storing the PNG to be sent to the layout test framework. | 610 // Vector for storing the PNG to be sent to the layout test framework. |
549 // TODO(ivandavid): Eventually change this to uint32_t and make everything | 611 // TODO(ivandavid): Eventually change this to uint32_t and make everything |
550 // work with that. It might be a bit tricky to fix everything to work with | 612 // work with that. It might be a bit tricky to fix everything to work with |
551 // uint32_t, but not too tricky. | 613 // uint32_t, but not too tricky. |
552 std::vector<unsigned char> png_output_; | 614 std::vector<unsigned char> png_output_; |
553 | 615 |
554 // Image hash of the bitmap that is turned into a PNG. The hash is put into | 616 // Image hash of the bitmap that is turned into a PNG. The hash is put into |
555 // the PNG as a comment, as it is needed by the layout test framework. | 617 // the PNG as a comment, as it is needed by the layout test framework. |
556 base::MD5Digest hash_; | 618 base::MD5Digest hash_; |
557 | 619 |
(...skipping 14 matching lines...) Expand all Loading... |
572 // - Print webpage to a pdf | 634 // - Print webpage to a pdf |
573 // - Convert pdf to a png | 635 // - Convert pdf to a png |
574 // - Send png to layout test framework, where it doesn an image diff | 636 // - Send png to layout test framework, where it doesn an image diff |
575 // on the image sent by this test and a reference image. | 637 // on the image sent by this test and a reference image. |
576 // | 638 // |
577 // Throughout this code, there will be |std::cout| statements. The layout test | 639 // Throughout this code, there will be |std::cout| statements. The layout test |
578 // framework uses stdout to get data from the browser test and uses stdin | 640 // framework uses stdout to get data from the browser test and uses stdin |
579 // to send data to the browser test. Writing "EOF\n" to |std::cout| indicates | 641 // to send data to the browser test. Writing "EOF\n" to |std::cout| indicates |
580 // that whatever block of data that the test was expecting has been completely | 642 // that whatever block of data that the test was expecting has been completely |
581 // sent. Sometimes EOF is printed to stderr because the test will expect it | 643 // sent. Sometimes EOF is printed to stderr because the test will expect it |
582 // from stderr in addition to stdout for certain blocks of data.= | 644 // from stderr in addition to stdout for certain blocks of data. |
| 645 InitPdfFunctions(); |
583 SetupStdinAndSavePath(); | 646 SetupStdinAndSavePath(); |
584 | 647 |
585 while (true) { | 648 while (true) { |
586 std::string input; | 649 std::string input; |
587 while (input.empty()) { | 650 while (input.empty()) { |
588 std::getline(std::cin, input); | 651 std::getline(std::cin, input); |
589 if (std::cin.eof()) | 652 if (std::cin.eof()) |
590 std::cin.clear(); | 653 std::cin.clear(); |
591 } | 654 } |
592 | 655 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
630 // waiting for this message and start waiting for the image data. | 693 // waiting for this message and start waiting for the image data. |
631 std::cout << "#EOF\n"; | 694 std::cout << "#EOF\n"; |
632 std::cout.flush(); | 695 std::cout.flush(); |
633 | 696 |
634 SendPng(); | 697 SendPng(); |
635 Reset(); | 698 Reset(); |
636 } | 699 } |
637 } | 700 } |
638 | 701 |
639 } // namespace printing | 702 } // namespace printing |
OLD | NEW |