Chromium Code Reviews| Index: chrome/browser/printing/print_preview_pdf_generated_browsertest.cc |
| diff --git a/chrome/browser/printing/print_preview_pdf_generated_browsertest.cc b/chrome/browser/printing/print_preview_pdf_generated_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4dfb7056cd78e523dd83662a44b3fcb76833e8b9 |
| --- /dev/null |
| +++ b/chrome/browser/printing/print_preview_pdf_generated_browsertest.cc |
| @@ -0,0 +1,672 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <cstdio> |
| +#include <iostream> |
| +#include <iterator> |
| +#include <limits> |
| +#include <string> |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "base/file_util.h" |
| +#include "base/files/file.h" |
| +#include "base/files/file_path.h" |
| +#include "base/files/scoped_temp_dir.h" |
| +#include "base/format_macros.h" |
| +#include "base/logging.h" |
| +#include "base/md5.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/numerics/safe_conversions.h" |
| +#include "base/path_service.h" |
| +#include "base/run_loop.h" |
| +#include "base/scoped_native_library.h" |
| +#include "base/strings/string16.h" |
| +#include "base/strings/string_split.h" |
| +#include "base/strings/string_util.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/app/chrome_command_ids.h" |
| +#include "chrome/browser/net/referrer.h" |
| +#include "chrome/browser/printing/print_preview_dialog_controller.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_commands.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| +#include "chrome/browser/ui/webui/print_preview/print_preview_handler.h" |
| +#include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" |
| +#include "chrome/browser/ui/webui/print_preview/sticky_settings.h" |
| +#include "chrome/common/chrome_paths.h" |
| +#include "chrome/common/print_messages.h" |
| +#include "chrome/common/url_constants.h" |
| +#include "chrome/test/base/in_process_browser_test.h" |
| +#include "chrome/test/base/ui_test_utils.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/browser/web_ui_message_handler.h" |
| +#include "content/public/common/page_transition_types.h" |
| +#include "content/public/test/browser_test_utils.h" |
| +#include "content/public/test/test_navigation_observer.h" |
| +#include "content/public/test/test_utils.h" |
| +#include "net/base/filename_util.h" |
| +#include "printing/pdf_render_settings.h" |
| +#include "printing/units.h" |
| +#include "ui/events/keycodes/keyboard_codes.h" |
| +#include "ui/gfx/codec/png_codec.h" |
| +#include "ui/gfx/geometry/rect.h" |
| +#include "url/gurl.h" |
| +#include "ipc/ipc_message_macros.h" |
|
Lei Zhang
2014/07/11 04:15:07
this should be in alphabetical order
ivandavid
2014/07/16 20:06:33
Done.
|
| + |
| +#if defined(OS_POSIX) |
| +#define STDIN_STREAM std::cin |
| +#define STRING_TYPE std::string |
|
Lei Zhang
2014/07/11 04:15:07
Drop this and use base::FilePath::StringType.
ivandavid
2014/07/16 20:06:32
Done.
|
| +#elif defined(OS_WIN) |
| +#define STDIN_STREAM std::wcin |
|
Lei Zhang
2014/07/11 04:15:07
Have you actually tried this on Windows? I don't k
ivandavid
2014/07/16 20:06:33
I removed this code and just use std::cin. Then I
|
| +#define STRING_TYPE std::wstring |
| +#endif |
| + |
| +using content::WebContents; |
| +using content::WebContentsObserver; |
| + |
| +// Number of color channels in a BGRA bitmap. |
| +const int kColorChannels = 4; |
| +const int kDpi = 300; |
| + |
| +// Message refers to the 'UILoaded' message from print_preview.js. |
| +// It gets sent either from onPreviewGenerationDone or from |
| +// onManipulateSettings_() in print_preview.js |
| +enum State { |
|
Lei Zhang
2014/07/11 04:15:06
Now that you added |is_already_pdf|, can you docum
ivandavid
2014/07/16 20:06:32
Done.
|
| + // Waiting for the first message so the program can select Save as PDF |
| + kWaitingToSendSaveAsPdf = 0, |
| + // Waiting for the second message so the test can set the layout |
| + kWaitingToSendLayoutSettings = 1, |
| + // Waiting for the third message so the test can set the page numbers |
| + kWaitingToSendPageNumbers = 2, |
| + // Waiting for the forth message so the test can set the headers checkbox |
| + kWaitingToSendHeadersAndFooters = 3, |
| + // Waiting for the fifth message so the test can set the background checkbox |
| + kWaitingToSendBackgroundColorsAndImages = 4, |
| + // Waiting for the sixth message so the test can set the margins combobox |
| + kWaitingToSendMargins = 5, |
| + // Waiting for the final message so the program can save to PDF. |
| + kWaitingForFinalMessage = 6, |
| +}; |
| + |
| +// Settings for print preview. It reflects the current options provided by |
| +// print preview. If more options are added, more states should be added and |
| +// there should be more settings added to this struct. |
| +struct PrintPreviewSettings { |
| + PrintPreviewSettings(bool is_portrait, |
| + std::string page_numbers, |
| + bool headers_and_footers, |
| + bool background_colors_and_images, |
| + printing::MarginType margins, |
|
Lei Zhang
2014/07/11 04:15:06
I'd put the entire file starting from line 70 in n
ivandavid
2014/07/16 20:06:33
Done.
|
| + bool is_already_pdf) |
| + : is_portrait(is_portrait), |
| + page_numbers(page_numbers), |
| + headers_and_footers(headers_and_footers), |
| + background_colors_and_images(background_colors_and_images), |
| + margins(margins), |
| + is_already_pdf(is_already_pdf) {} |
| + |
| + bool is_portrait; |
| + std::string page_numbers; |
| + bool headers_and_footers; |
| + bool background_colors_and_images; |
| + printing::MarginType margins; |
| + bool is_already_pdf; |
| +}; |
| + |
| +// Observes the print preview webpage. Once it observes the PreviewPageCount |
| +// message, will send a sequence of commands to the print preview dialog and |
| +// change the settings of the preview dialog. |
| +class PrintPreviewObserver : public WebContentsObserver { |
| + public: |
| + PrintPreviewObserver(WebContents* dialog, Browser* browser) |
|
Lei Zhang
2014/07/11 04:15:07
Can you make the parameters Browser* and then WebC
ivandavid
2014/07/16 20:06:32
Done.
|
| + : WebContentsObserver(dialog), |
| + browser_(browser), |
| + state_(kWaitingToSendSaveAsPdf) {} |
| + |
| + virtual ~PrintPreviewObserver() {} |
| + |
| + // Sets closure for the observer so that it can end the loop. |
| + void set_quit_closure(const base::Closure &closure) { |
| + closure_ = closure; |
| + } |
| + |
| + // Actually stops the message loop so that the test can proceed. |
| + void EndLoop() { |
| + base::MessageLoop::current()->PostTask(FROM_HERE, closure_); |
| + } |
| + |
| + bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
| + IPC_BEGIN_MESSAGE_MAP(PrintPreviewObserver, message) |
| + IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount, |
| + OnDidGetPreviewPageCount) |
| + IPC_END_MESSAGE_MAP(); |
| + return false; |
| + } |
| + |
| + // Gets the web contents for the print preview dialog so that the UI and |
| + // other elements can be accessed. |
| + WebContents* GetDialog() { |
| + WebContents* tab = browser_->tab_strip_model()->GetActiveWebContents(); |
| + printing::PrintPreviewDialogController* dialog_controller = |
| + printing::PrintPreviewDialogController::GetInstance(); |
| + WebContents* web_contents = |
| + dialog_controller->GetPrintPreviewForContents(tab); |
| + return web_contents; |
| + } |
| + |
| + // Gets the PrintPreviewUI so that certain elements can be accessed. |
| + PrintPreviewUI* GetUI() { |
| + return static_cast<PrintPreviewUI*>( |
| + GetDialog()->GetWebUI()->GetController()); |
| + } |
| + |
| + // Calls native_layer.onManipulateSettingsForTest() and sends a dictionary |
| + // value containing the type of setting and the value to set that settings |
| + // to. |
| + void ManipulatePreviewSettings() { |
| + base::DictionaryValue script_argument; |
| + |
| + if (state_ == kWaitingToSendSaveAsPdf) { |
| + script_argument.SetBoolean("selectSaveAsPdfDestination", true); |
| + state_ = settings_->is_already_pdf ? |
| + kWaitingToSendPageNumbers : kWaitingToSendLayoutSettings; |
| + } else if (state_ == kWaitingToSendLayoutSettings) { |
| + script_argument.SetBoolean("layoutSettings.portrait", |
| + settings_->is_portrait); |
| + state_ = kWaitingToSendPageNumbers; |
| + } else if (state_ == kWaitingToSendPageNumbers) { |
| + script_argument.SetString("pageRange", settings_->page_numbers); |
| + state_ = settings_->is_already_pdf ? |
| + kWaitingForFinalMessage : kWaitingToSendHeadersAndFooters; |
| + } else if (state_ == kWaitingToSendHeadersAndFooters) { |
| + script_argument.SetBoolean("headersAndFooters", |
| + settings_->headers_and_footers); |
| + state_ = kWaitingToSendBackgroundColorsAndImages; |
| + } else if (state_ == kWaitingToSendBackgroundColorsAndImages) { |
| + script_argument.SetBoolean("backgroundColorsAndImages", |
| + settings_->background_colors_and_images); |
| + state_ = kWaitingToSendMargins; |
| + } else if (state_ == kWaitingToSendMargins) { |
| + script_argument.SetInteger("margins", settings_->margins); |
| + state_ = kWaitingForFinalMessage; |
| + } else if (state_ == kWaitingForFinalMessage) { |
| + EndLoop(); |
| + return; |
| + } |
| + |
| + ASSERT_TRUE(!script_argument.empty()); |
| + GetUI()->web_ui()->CallJavascriptFunction( |
| + "onManipulateSettingsForTest", script_argument); |
| + } |
| + |
| + // Saves the print preview settings to be sent to the print preview dialog. |
| + void SetPrintPreviewSettings(const PrintPreviewSettings& settings) { |
| + settings_.reset(new PrintPreviewSettings(settings)); |
| + } |
| + |
| + private: |
| + // Listens for messages from the print preview dialog. Specifically, it |
| + // listens for 'UILoadedForTest' and 'UIFailedLoadingForTest.' |
| + class UIDoneLoadingMessageHandler : public content::WebUIMessageHandler { |
| + public: |
| + explicit UIDoneLoadingMessageHandler(PrintPreviewObserver* observer) |
| + : observer_(observer) {} |
| + |
| + virtual ~UIDoneLoadingMessageHandler() {} |
| + |
| + // When a setting has been set succesfully, this is called and the observer |
| + // is told to send the next setting to be set. |
| + void HandleDone(const base::ListValue* /* args */) { |
| + ASSERT_TRUE(observer_); |
| + observer_->ManipulatePreviewSettings(); |
| + } |
| + |
| + // Ends the test because a setting was not set successfully. Called when |
| + // this class hears 'UIFailedLoadingForTest.' |
| + void HandleFailure(const base::ListValue* /* args */) { |
| + FAIL(); |
| + } |
| + |
| + // Sets up this class to listen for the UILoadedForTest and |
| + // UIFailedLoadingForTest messages. These messages are sent by |
| + // print_preview.js. On UILoadedForTest, a settings has been successfully |
| + // set and its effects on the pdf have been finalized. On |
| + // UIFailedLoadingForTest a setting has not been successfully set and the |
| + // test should fail. |
| + void RegisterMessages() OVERRIDE { |
| + web_ui()->RegisterMessageCallback( |
| + "UILoadedForTest", |
| + base::Bind(&UIDoneLoadingMessageHandler::HandleDone, |
| + base::Unretained(this))); |
| + |
| + web_ui()->RegisterMessageCallback( |
| + "UIFailedLoadingForTest", |
| + base::Bind(&UIDoneLoadingMessageHandler::HandleFailure, |
| + base::Unretained(this))); |
| + } |
| + |
| + private: |
| + PrintPreviewObserver * const observer_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(UIDoneLoadingMessageHandler); |
| + }; |
| + |
| + // Called when the observer gets the IPC message stating that the page count |
| + // is ready. |
| + void OnDidGetPreviewPageCount( |
| + const PrintHostMsg_DidGetPreviewPageCount_Params ¶ms) { |
| + WebContents* web_contents = GetDialog(); |
| + PrintPreviewUI* ui = GetUI(); |
| + ASSERT_TRUE(ui); |
| + ASSERT_TRUE(ui->web_ui()); |
| + Observe(web_contents); |
| + ASSERT_TRUE(web_contents); |
| + |
| + // The web ui deallocates the memory for the handler when the web ui is |
| + // destroyed so we don't have to worry about it. |
| + ui->web_ui()->AddMessageHandler(new UIDoneLoadingMessageHandler(this)); |
| + ui->web_ui()->CallJavascriptFunction("onEnableManipulateSettingsForTest"); |
| + } |
| + |
| + void DidCloneToNewWebContents(WebContents* old_web_contents, |
| + WebContents* new_web_contents) OVERRIDE { |
| + Observe(new_web_contents); |
| + } |
| + |
| + void WebContentsDestroyed() OVERRIDE { |
| + EndLoop(); |
| + } |
| + |
| + Browser* browser_; |
| + base::Closure closure_; |
|
Lei Zhang
2014/07/11 04:15:07
quit_closure_
ivandavid
2014/07/16 20:06:33
Done.
|
| + scoped_ptr<PrintPreviewSettings> settings_; |
| + |
| + // State of the observer. The state indicates what message it is to send |
|
Lei Zhang
2014/07/11 04:15:07
s/it is//
ivandavid
2014/07/16 20:06:32
Done.
|
| + // next. The state advances whenever the message handler calls |
| + // ManipulatePreviewSettings() on the observer. |
| + State state_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PrintPreviewObserver); |
| +}; |
| + |
| +class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest { |
| + public: |
| + PrintPreviewPdfGeneratedBrowserTest() {} |
| + virtual ~PrintPreviewPdfGeneratedBrowserTest() {} |
| + |
| + // Navigates to the given web page, then initiates print preview and waits |
| + // for all the settings to be set. |
| + void NavigateAndPreview(const STRING_TYPE& file_name, |
| + PrintPreviewSettings settings) { |
|
Lei Zhang
2014/07/11 04:15:07
const ref
ivandavid
2014/07/16 20:06:33
Done.
|
| + print_preview_observer_->SetPrintPreviewSettings(settings); |
| + base::FilePath path(file_name); |
| + GURL gurl = net::FilePathToFileURL(path); |
| + |
| + ui_test_utils::NavigateToURL(browser(), gurl); |
| + |
| + base::RunLoop loop; |
| + print_preview_observer_->set_quit_closure(loop.QuitClosure()); |
| + chrome::Print(browser()); |
| + loop.Run(); |
| + } |
| + |
| + // Prints the web page to a PDF. NavigateAndPreview must be called first. |
| + void Print() { |
| + ASSERT_FALSE(pdf_file_save_path_.empty()); |
| + base::RunLoop loop; |
| + print_preview_observer_->set_quit_closure(loop.QuitClosure()); |
| + print_preview_observer_->GetUI()->handler_->FileSelected( |
| + pdf_file_save_path_, 0, NULL); |
| + loop.Run(); |
| + } |
| + |
| + // Initializes the pdf lib functions. Called once when browser test starts. |
| + // Library is closed when the browser test is killed. |
|
Lei Zhang
2014/07/11 04:15:06
s/is killed/ends/
ivandavid
2014/07/16 20:06:32
Done.
|
| + void InitPdfFunctions() { |
| + base::FilePath pdf_module_path; |
| + |
| + ASSERT_TRUE(PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_module_path)); |
| + ASSERT_TRUE(base::PathExists(pdf_module_path)); |
| + pdf_lib_.Reset(base::LoadNativeLibrary(pdf_module_path, NULL)); |
| + |
| + ASSERT_TRUE(pdf_lib_.is_valid()); |
| + pdf_to_bitmap_func_ = |
| + reinterpret_cast<PDFPageToBitmapProc>( |
| + pdf_lib_.GetFunctionPointer("RenderPDFPageToBitmap")); |
| + |
| + pdf_doc_info_func_ = |
| + reinterpret_cast<GetPDFDocInfoProc>( |
| + pdf_lib_.GetFunctionPointer("GetPDFDocInfo")); |
| + |
| + pdf_page_size_func_ = |
| + reinterpret_cast<GetPDFPageSizeByIndexProc>( |
| + pdf_lib_.GetFunctionPointer("GetPDFPageSizeByIndex")); |
| + |
| + ASSERT_TRUE(pdf_to_bitmap_func_); |
| + ASSERT_TRUE(pdf_doc_info_func_); |
| + ASSERT_TRUE(pdf_page_size_func_); |
| + } |
| + |
| + // Converts the PDF to a PNG file so that the layout test can do an image |
| + // diff on this image and a reference image. |
| + void PdfToPng() { |
| + std::string data; |
| + int num_pages; |
| + double max_width = 0; |
| + std::vector<uint8_t> bitmap_data; |
| + double total_height = 0; |
| + |
| + ASSERT_TRUE(base::ReadFileToString(pdf_file_save_path_, &data)); |
| + ASSERT_TRUE(pdf_doc_info_func_(data.data(), |
| + data.size(), |
| + &num_pages, |
|
Lei Zhang
2014/07/11 04:15:06
Weren't we checking this is not negative?
ivandavid
2014/07/16 20:06:32
Done.
ivandavid
2014/07/16 20:06:33
Accidentally deleted that.
|
| + &max_width)); |
| + |
| + max_width = printing::ConvertPointsToPixelDouble(max_width); |
|
Lei Zhang
2014/07/11 04:15:07
I would make a new variable here named |max_width_
ivandavid
2014/07/16 20:06:33
Done.
|
| + |
| + for (int i = 0; i < num_pages; ++i) { |
| + double width, height; |
| + bool autorotate = false; |
|
Lei Zhang
2014/07/11 04:15:07
Shouldn't this always be true? If width < height,
ivandavid
2014/07/16 20:06:32
Done.
ivandavid
2014/07/16 20:06:33
I didn't realize that the |autorotate| parameter d
|
| + ASSERT_TRUE(pdf_page_size_func_( |
| + data.data(), data.size(), i, &width, &height)); |
| + |
| + width = printing::ConvertPointsToPixelDouble(width); |
|
Lei Zhang
2014/07/11 04:15:07
ditto for new variables.
ivandavid
2014/07/16 20:06:33
Done.
|
| + height = printing::ConvertPointsToPixelDouble(height); |
| + |
| + // The image needs to be rotated if the width is greater than the height. |
| + // This is because when the image is printed to a pdf, it it is in |
| + // landscape mode (or the document is just wide), it will be printed |
| + // horizontally, not vertically. However, if it is printed onto a piece of |
| + // paper, it would be rotated to be vertical. Therefore, the image has to |
| + // be rotated here to reflect what will truly be printed. |
| + if (width > height) { |
| + std::swap<double>(width, height); |
| + autorotate = true; |
| + } |
| + |
| + total_height += height; |
| + gfx::Rect rect(width, height); |
| + printing::PdfRenderSettings settings(rect, kDpi, true); |
| + |
| + // TODO(ivandavid): Implement correct overflow detection. Need to check |
| + // for signed multiplication overflow. Can't use size_t or any unsigned |
| + // stuff, b/c internally everything in PdfRenderSettings is signed it |
| + // seems. |
| + |
| + int max_int = std::numeric_limits<int>::max(); |
| + if (settings.area().height() > max_int / settings.area().width() || |
|
Lei Zhang
2014/07/11 04:15:06
The two comparisons in the if statement seem redun
ivandavid
2014/07/16 20:06:32
Done.
ivandavid
2014/07/16 20:06:32
I rewrote the overflow conditional. I first check
|
| + settings.area().height() > |
| + max_int / (kColorChannels * settings.area().width())) { |
|
Lei Zhang
2014/07/11 04:15:07
The multiplication can also overflow here.
ivandavid
2014/07/16 20:06:33
Done.
|
| + FAIL() << "IMAGE SIZE IS TOO LARGE, DECREASE DIMENSIONS OR DPI."; |
|
Lei Zhang
2014/07/11 04:15:06
No need to be all caps.
ivandavid
2014/07/16 20:06:33
Done.
|
| + } |
| + |
| + std::vector<uint8_t> page_bitmap_data( |
| + kColorChannels * settings.area().size().GetArea()); |
| + |
| + ASSERT_TRUE(pdf_to_bitmap_func_(data.data(), |
| + data.size(), |
| + i, |
| + page_bitmap_data.data(), |
| + settings.area().size().width(), |
| + settings.area().size().height(), |
| + settings.dpi(), |
| + settings.dpi(), |
| + autorotate)); |
| + |
| + // Must multiply width & max_width by number of color channels because |
|
Lei Zhang
2014/07/11 04:15:06
Refer to variables in comments as |foo|.
Lei Zhang
2014/07/11 04:15:07
Shouldn't this be an internal detail to FillPng()
ivandavid
2014/07/16 20:06:33
Done.
ivandavid
2014/07/16 20:06:33
Done.
|
| + // the bitmap stores individual bytes, not int32's. |
| + if (width < max_width) { |
| + FillPng(&page_bitmap_data, |
| + width * kColorChannels, |
| + max_width * kColorChannels, |
| + settings.area().size().height()); |
| + } |
| + |
| + bitmap_data.insert(bitmap_data.end(), |
| + page_bitmap_data.begin(), |
| + page_bitmap_data.end()); |
| + } |
| + |
| + CreatePng(bitmap_data, max_width, total_height); |
| + } |
| + |
| + // Fills out a bitmap with whitespace so that the image will correctly fit |
| + // within a PNG that is wider than the bitmap itself. |
| + void FillPng(std::vector<uint8_t>* bitmap, |
| + int current_width, |
| + int desired_width, |
| + int height) { |
| + // Need to separate the iterator and the index. This is because the iterator |
| + // will be rendered invalid whenever there is a resize, and since the bitmap |
| + // might have to be widened significantly, this is likely to happen. Since |
| + // vectors are being used, std::advance is really fast, so its ok. |
| + ASSERT_TRUE(bitmap); |
| + ASSERT_LE(current_width, desired_width); |
| + const uint8_t kColorByte = 255; |
| + size_t index = 0; |
| + for (int i = 0; i < height; ++i, index = i * desired_width) { |
|
Lei Zhang
2014/07/11 04:15:07
The complexity of this algorithm using std::vector
ivandavid
2014/07/16 20:06:32
I think I came up with a better solution. I pre-al
ivandavid
2014/07/16 20:06:33
Done.
Lei Zhang
2014/07/17 00:30:36
Sure, this uses 2*M memory, but you can get that d
|
| + index += current_width - 1; |
| + std::vector<uint8_t>::iterator iterator = bitmap->begin(); |
| + std::advance(iterator, index); |
| + // 255,255,255,255 is white in BGRA |
| + bitmap->insert(iterator, desired_width - current_width, kColorByte); |
| + } |
| + } |
| + |
| + // Sends the PNG image to the layout test framework for comparison. |
| + void SendPng() { |
| + // Send image header and |hash_| to the layout test framework. |
| + printf("Content-Type: image/png\n"); |
| + printf("ActualHash: %s\n", base::MD5DigestToBase16(hash_).data()); |
|
Lei Zhang
2014/07/11 04:15:07
data() -> c_str();
http://www.cplusplus.com/refer
ivandavid
2014/07/16 20:06:33
Done.
|
| + printf("Content-Length: %" PRIuS "\n", output_.size()); |
| + |
| + for (size_t i = 0; i < output_.size(); ++i) |
| + printf("%c", output_[i]); |
| + |
| + printf("#EOF\n"); |
| + fflush(stdout); |
| + fprintf(stderr, "#EOF\n"); |
| + fflush(stderr); |
| + } |
| + |
| + // Duplicates the tab that was created when the browser opened. This is done |
| + // so that the observer can listen to the duplicated tab as soon as possible |
| + // and start listening for messages related to print preview. |
| + void DuplicateTab() { |
| + WebContents* tab = |
| + browser()->tab_strip_model()->GetActiveWebContents(); |
| + ASSERT_TRUE(tab); |
| + |
| + print_preview_observer_.reset(new PrintPreviewObserver(tab, browser())); |
| + chrome::DuplicateTab(browser()); |
| + |
| + WebContents* initiator = |
| + browser()->tab_strip_model()->GetActiveWebContents(); |
| + ASSERT_TRUE(initiator); |
| + ASSERT_NE(tab, initiator); |
| + } |
| + |
| + // Resets the test so that another web page can be printed. It also deletes |
| + // the duplicated tab as it isn't needed anymore. |
| + void Reset() { |
| + output_.clear(); |
| + ASSERT_EQ(browser()->tab_strip_model()->count(), 2); |
| + chrome::CloseTab(browser()); |
| + ASSERT_EQ(browser()->tab_strip_model()->count(), 1); |
| + } |
| + |
| + // Creates a temporary directory to store a text file that will be used for |
| + // stdin to accept input from the layout test framework. A path for the pdf |
| + // file is also created. The directory and files within it are automatically |
| + // cleaned up once the test is killed. |
| + void SetupStdinAndSavePath() { |
| + // Sends a message to the layout test framework indicating indicating |
| + // that the browser test has completed setting itself up. The layout |
| + // test will then expect the file path for stdin. |
| + base::FilePath tmp_path; |
| + printf("#READY\n"); |
| + fflush(stdout); |
| + |
| + ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir()); |
| + ASSERT_TRUE(base::CreateTemporaryFileInDir(tmp_dir_.path(), &tmp_path)); |
| + ASSERT_TRUE(freopen(tmp_path.value().c_str(), "r", stdin)); |
| + |
| + pdf_file_save_path_ = tmp_dir_.path().AppendASCII("dummy.pdf"); |
| + |
| + // Send the file path to the layout test framework so that it can |
| + // communicate with this browsertest. |
|
Lei Zhang
2014/07/11 04:15:07
browser test
ivandavid
2014/07/16 20:06:32
Done.
|
| + printf("StdinPath: %s\n#EOF\n", tmp_path.value().c_str()); |
| + fflush(stdout); |
| + } |
| + |
| + private: |
| + // Generates a png from bitmap data and stores it in |output_|. |
| + void CreatePng( |
| + const std::vector<uint8_t>& bitmap_data, int width, int height) { |
| + std::string hash_data(bitmap_data.begin(), bitmap_data.end()); |
| + base::MD5Sum( |
| + static_cast<const void*>(hash_data.data()), hash_data.size(), &hash_); |
| + |
| + gfx::Rect png_rect(width, height); |
| + |
| + // tEXtchecksum looks funny, but that's what the layout test framework |
| + // expects. |
| + std::string comment_title("tEXtchecksum\x00"); |
| + gfx::PNGCodec::Comment hash_comment(comment_title, |
| + base::MD5DigestToBase16(hash_)); |
| + |
| + std::vector<gfx::PNGCodec::Comment> comments; |
| + comments.push_back(hash_comment); |
| + |
| + ASSERT_TRUE(gfx::PNGCodec::Encode(bitmap_data.data(), |
| + gfx::PNGCodec::FORMAT_BGRA, |
| + png_rect.size(), |
| + png_rect.size().width() * kColorChannels, |
| + false, |
| + comments, |
| + &output_)); |
| + } |
| + |
| + scoped_ptr<PrintPreviewObserver> print_preview_observer_; |
| + base::FilePath pdf_file_save_path_; |
| + |
| + // These typedefs are function pointers to pdflib functions that give |
| + // information about the PDF as a whole and about specific pages. |
| + |
| + // Converts the PDF to a bitmap. |
| + typedef bool (*PDFPageToBitmapProc)(const void* pdf_buffer, |
| + int pdf_buffer_size, |
| + int page_number, |
| + void* bitmap_buffer, |
| + int bitmap_width, |
| + int bitmap_height, |
| + int dpi_x, |
| + int dpi_y, |
| + bool autorotate); |
| + |
| + // Gets the page count and maximum page width of the PDF in points. |
| + typedef bool (*GetPDFDocInfoProc)(const void* pdf_buffer, |
| + int buffer_size, |
| + int* pages_count, |
| + double* max_page_width); |
| + |
| + // Gets the dimensions of a specific page within a PDF. |
| + typedef bool (*GetPDFPageSizeByIndexProc)(const void* pdf_buffer, |
| + int buffer_size, |
| + int index, |
| + double* width, |
| + double* height); |
| + |
| + // Instantiations of the function pointers described above. |
| + PDFPageToBitmapProc pdf_to_bitmap_func_; |
| + GetPDFDocInfoProc pdf_doc_info_func_; |
| + GetPDFPageSizeByIndexProc pdf_page_size_func_; |
| + |
| + // Used to open up the libpdf.so, which contains the functions above. |
| + base::ScopedNativeLibrary pdf_lib_; |
| + |
| + // Vector for storing the PNG to be sent to the layout test framework. |
| + std::vector<unsigned char> output_; |
| + |
| + // Image hash of the bitmap that is turned into a PNG. The hash is put into |
| + // the PNG as a comment, as it is needed by the layout test framework. |
| + base::MD5Digest hash_; |
| + |
| + // Temporary directory for storing the pdf and the file for stdin. |
| + base::ScopedTempDir tmp_dir_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PrintPreviewPdfGeneratedBrowserTest); |
| +}; |
| + |
| +IN_PROC_BROWSER_TEST_F(PrintPreviewPdfGeneratedBrowserTest, |
| + MANUAL_DummyTest) { |
| + // What this code is supposed to do: |
| + // -Setup communication with the layout test framework |
|
Lei Zhang
2014/07/11 04:15:06
Put a space after -
ivandavid
2014/07/16 20:06:32
Done.
|
| + // -Print webpage to a pdf |
| + // -Convert pdf to a png |
| + // -Send png to layout test framework, where it doesn an image diff |
| + // on the image sent by this test and a reference image. |
| + // |
| + // Throughout this code, there will be printf statements. The layout test |
| + // framework uses stdout to get data from the browser test and uses stdin |
| + // to send data to the browser test. Calling printf("#EOF\n") indicates that |
| + // whatever block of data that the test was expecting has been completely |
| + // sent. Sometimes EOF is printed to stderr because the test will expect it |
| + // from stderr in addition to stdout for certain blocks of data. |
| + InitPdfFunctions(); |
| + SetupStdinAndSavePath(); |
| + |
| + // There is now way to determine how many tests are to be run ahead of time |
|
Lei Zhang
2014/07/11 04:15:07
now -> no
ivandavid
2014/07/16 20:06:32
Done.
|
| + // without undesirable changes to the layout test framework. However, that is |
| + // ok since whenever all the tests have been run, the layout test framework |
| + // calls SIGKILL on this process, ending the test. This will end the while |
| + // loop and cause the test to clean up after itself. For this to work, the |
| + // browsertest must be run with '--single_process' not '--single-process.' |
| + while (true) { |
| + STRING_TYPE cmd; |
| + std::getline(STDIN_STREAM, cmd); |
| + if (cmd.empty()) { |
| + while (STDIN_STREAM.eof()) { |
| + STDIN_STREAM.clear(); |
| + std::getline(STDIN_STREAM, cmd); |
| + if (!cmd.empty()) { |
| + break; |
| + } |
| + } |
| + } |
| + |
| + DuplicateTab(); |
| + |
| + PrintPreviewSettings settings( |
| + true, |
| + "", |
| + false, |
| + false, |
| + printing::DEFAULT_MARGINS, |
| + cmd.find(".pdf") != STRING_TYPE::npos); |
| + |
| + // Splits the command sent by the layout test framework. The first command |
| + // is always the file path to use for the test. The rest isn't relevant, |
| + // so it can be ignored. The separator for the commands is an apostrophe. |
| + std::vector<STRING_TYPE> cmd_arguments; |
| + base::SplitString(cmd, '\'', &cmd_arguments); |
| + STRING_TYPE test_name(cmd_arguments[0]); |
|
Lei Zhang
2014/07/11 04:15:06
Assert the |cmd_arguments| size >= 1 before access
ivandavid
2014/07/16 20:06:33
Done.
|
| + NavigateAndPreview(test_name, settings); |
| + Print(); |
| + PdfToPng(); |
| + |
| + // Message to the layout test framework indicating that it should start |
| + // waiting for the image data, as there is no more text data to be read. |
| + // There actually isn't any text data at all, however because the layout |
| + // test framework requires it, a message has to be sent to stop it from |
| + // waiting for this message and start waiting for the image data. |
| + printf("#EOF\n"); |
| + fflush(stdout); |
| + |
| + SendPng(); |
| + Reset(); |
| + } |
| +} |