Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <cstdio> | |
| 6 #include <iostream> | |
| 7 #include <limits> | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/file_util.h" | |
| 12 #include "base/files/file.h" | |
| 13 #include "base/files/file_path.h" | |
| 14 #include "base/files/scoped_temp_dir.h" | |
| 15 #include "base/format_macros.h" | |
| 16 #include "base/logging.h" | |
| 17 #include "base/md5.h" | |
| 18 #include "base/memory/scoped_ptr.h" | |
| 19 #include "base/numerics/safe_conversions.h" | |
| 20 #include "base/path_service.h" | |
| 21 #include "base/run_loop.h" | |
| 22 #include "base/scoped_native_library.h" | |
| 23 #include "base/strings/string16.h" | |
| 24 #include "base/strings/string_split.h" | |
| 25 #include "base/strings/string_util.h" | |
| 26 #include "base/strings/utf_string_conversions.h" | |
| 27 #include "chrome/app/chrome_command_ids.h" | |
| 28 #include "chrome/browser/net/referrer.h" | |
| 29 #include "chrome/browser/printing/print_preview_dialog_controller.h" | |
| 30 #include "chrome/browser/profiles/profile.h" | |
| 31 #include "chrome/browser/ui/browser.h" | |
| 32 #include "chrome/browser/ui/browser_commands.h" | |
| 33 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 34 #include "chrome/browser/ui/webui/print_preview/print_preview_handler.h" | |
| 35 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" | |
| 36 #include "chrome/browser/ui/webui/print_preview/sticky_settings.h" | |
| 37 #include "chrome/common/chrome_paths.h" | |
| 38 #include "chrome/common/print_messages.h" | |
| 39 #include "chrome/common/url_constants.h" | |
| 40 #include "chrome/test/base/in_process_browser_test.h" | |
| 41 #include "chrome/test/base/ui_test_utils.h" | |
| 42 #include "content/public/browser/web_contents.h" | |
| 43 #include "content/public/browser/web_ui_message_handler.h" | |
| 44 #include "content/public/common/page_transition_types.h" | |
| 45 #include "content/public/test/browser_test_utils.h" | |
| 46 #include "content/public/test/test_navigation_observer.h" | |
| 47 #include "content/public/test/test_utils.h" | |
| 48 #include "net/base/filename_util.h" | |
| 49 #include "printing/pdf_render_settings.h" | |
| 50 #include "printing/units.h" | |
| 51 #include "ui/events/keycodes/keyboard_codes.h" | |
| 52 #include "ui/gfx/codec/png_codec.h" | |
| 53 #include "ui/gfx/geometry/rect.h" | |
| 54 #include "url/gurl.h" | |
| 55 #include "ipc/ipc_message_macros.h" | |
| 56 | |
| 57 #if defined(OS_POSIX) | |
|
ivandavid
2014/07/03 03:12:03
Is this an OK solution to the problem of getline n
Lei Zhang
2014/07/08 01:11:36
Can you just use the same pattern as the blink lay
ivandavid
2014/07/08 23:24:19
I'm going to keep this implementation for now unti
| |
| 58 #define STDIN_STREAM std::cin | |
| 59 #elif defined(OS_WIN) | |
| 60 #define STDIN_STREAM std::wcin | |
| 61 #endif | |
| 62 | |
| 63 using content::WebContents; | |
| 64 using content::WebContentsObserver; | |
| 65 | |
| 66 // Message refers to the 'UILoaded' message from print_preview.js. | |
| 67 // It gets sent either from onPreviewGenerationDone or from | |
| 68 // onManipulateSettings_() in print_preview.js | |
| 69 enum State { | |
| 70 // Waiting for the first message so the program can select Save as PDF | |
| 71 kWaitingToSendSaveAsPdf = 0, | |
| 72 // Waiting for the second message so the test can set the layout | |
| 73 kWaitingToSendLayoutSettings = 1, | |
| 74 // Waiting for the third message so the test can set the page numbers | |
| 75 kWaitingToSendPageNumbers = 2, | |
| 76 // Waiting for the forth message so the test can set the headers checkbox | |
| 77 kWaitingToSendHeadersAndFooters = 3, | |
| 78 // Waiting for the fifth message so the test can set the background checkbox | |
| 79 kWaitingToSendBackgroundColorsAndImages = 4, | |
| 80 // Waiting for the sixth message so the test can set the margins combobox | |
| 81 kWaitingToSendMargins = 5, | |
| 82 // Waiting for the final message so the program can save to PDF. | |
| 83 kWaitingForFinalMessage = 6, | |
| 84 }; | |
| 85 | |
| 86 struct PrintPreviewSettings { | |
| 87 PrintPreviewSettings(bool is_portrait, | |
| 88 std::string page_numbers, | |
| 89 bool headers_and_footers, | |
| 90 bool background_colors_and_images, | |
| 91 printing::MarginType margins, | |
| 92 bool is_already_pdf) | |
| 93 : is_portrait(is_portrait), | |
| 94 page_numbers(page_numbers), | |
| 95 headers_and_footers(headers_and_footers), | |
| 96 background_colors_and_images(background_colors_and_images), | |
| 97 margins(margins), | |
| 98 is_already_pdf(is_already_pdf) {} | |
| 99 | |
| 100 PrintPreviewSettings(const PrintPreviewSettings& settings) | |
| 101 : is_portrait(settings.is_portrait), | |
| 102 page_numbers(settings.page_numbers), | |
| 103 headers_and_footers(settings.headers_and_footers), | |
| 104 background_colors_and_images(settings.background_colors_and_images), | |
| 105 margins(settings.margins), | |
| 106 is_already_pdf(settings.is_already_pdf) {} | |
|
Dan Beam
2014/07/07 23:08:11
do you need to explicitly declare these constructo
ivandavid
2014/07/08 01:07:41
The first one does, however the copy constructor d
ivandavid
2014/07/08 01:07:41
Done.
| |
| 107 | |
| 108 bool is_portrait; | |
| 109 std::string page_numbers; | |
| 110 bool headers_and_footers; | |
| 111 bool background_colors_and_images; | |
| 112 printing::MarginType margins; | |
| 113 bool is_already_pdf; | |
| 114 }; | |
| 115 | |
| 116 // Observes the print preview webpage. Once it observes the | |
| 117 // PreviewPageCount message, will send a sequence of commands | |
| 118 // to the print preview dialog and change the settings of the | |
| 119 // preview dialog. | |
| 120 class PrintPreviewObserver : public WebContentsObserver { | |
| 121 public: | |
| 122 PrintPreviewObserver(WebContents* dialog, | |
| 123 Browser* browser) | |
|
Lei Zhang
2014/07/08 01:11:37
Fits on the previous line. Not sure why you moved
ivandavid
2014/07/08 23:24:20
Done.
ivandavid
2014/07/08 23:24:21
I added an extra parameter, but then removed it, t
| |
| 124 : WebContentsObserver(dialog), | |
| 125 browser_(browser), | |
| 126 state_(kWaitingToSendSaveAsPdf) {} | |
| 127 | |
| 128 virtual ~PrintPreviewObserver() {} | |
| 129 | |
| 130 // Sets closure for the observer so that it can end the loop. | |
| 131 void set_quit_closure(const base::Closure &closure) { | |
| 132 closure_ = closure; | |
| 133 } | |
| 134 | |
| 135 // Actually stops the message_loop so that the test can proceed. | |
| 136 void EndLoop() { | |
| 137 base::MessageLoop::current()->PostTask(FROM_HERE, closure_); | |
| 138 } | |
| 139 | |
| 140 // This method must always return false. If it ever returns true, | |
| 141 // it will cause the test to hang. This is because the | |
| 142 // PrintPreviewMessageHandler should still handle all of the messages to | |
| 143 // progress the print preview process. | |
| 144 bool OnMessageReceived(const IPC::Message& message) OVERRIDE { | |
| 145 IPC_BEGIN_MESSAGE_MAP(PrintPreviewObserver, message) | |
| 146 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount, | |
| 147 OnDidGetPreviewPageCount) | |
| 148 IPC_MESSAGE_UNHANDLED(break;) | |
| 149 IPC_END_MESSAGE_MAP(); | |
| 150 return false; | |
| 151 } | |
| 152 | |
| 153 // Gets the web contents for the print preview dialog so that | |
| 154 // the UI and other elements can be accessed. | |
| 155 WebContents* GetDialog() { | |
| 156 WebContents* tab = browser_->tab_strip_model()->GetActiveWebContents(); | |
| 157 printing::PrintPreviewDialogController* dialog_controller = | |
| 158 printing::PrintPreviewDialogController::GetInstance(); | |
| 159 WebContents* web_contents = | |
| 160 dialog_controller->GetPrintPreviewForContents(tab); | |
| 161 return web_contents; | |
| 162 } | |
| 163 | |
| 164 // Gets the PrintPreviewUI so that certain elements can be accessed. | |
| 165 PrintPreviewUI* GetUI() { | |
| 166 return static_cast<PrintPreviewUI*>( | |
| 167 GetDialog()->GetWebUI()->GetController()); | |
| 168 } | |
| 169 | |
| 170 // Calls a javascript function that will change the print preview settings, | |
| 171 // such as the layout, the margins, page numbers, etc. | |
| 172 void ManipulatePreviewSettings() { | |
| 173 base::DictionaryValue script_argument; | |
| 174 | |
| 175 if (state_ == kWaitingToSendSaveAsPdf) { | |
| 176 script_argument.SetBoolean("selectSaveAsPdfDestination", true); | |
| 177 state_ = settings_->is_already_pdf ? | |
| 178 kWaitingToSendPageNumbers : | |
| 179 kWaitingToSendLayoutSettings; | |
| 180 } else if (state_ == kWaitingToSendLayoutSettings) { | |
| 181 script_argument.SetBoolean("layoutSettings.portrait", | |
| 182 settings_->is_portrait); | |
| 183 state_ = kWaitingToSendPageNumbers; | |
| 184 } else if (state_ == kWaitingToSendPageNumbers) { | |
| 185 script_argument.SetString("pageRange", settings_->page_numbers); | |
| 186 state_ = settings_->is_already_pdf ? | |
| 187 kWaitingForFinalMessage : | |
| 188 kWaitingToSendHeadersAndFooters; | |
| 189 } else if (state_ == kWaitingToSendHeadersAndFooters) { | |
| 190 script_argument.SetBoolean("headersAndFooters", | |
| 191 settings_->headers_and_footers); | |
| 192 state_ = kWaitingToSendBackgroundColorsAndImages; | |
| 193 } else if (state_ == kWaitingToSendBackgroundColorsAndImages) { | |
| 194 script_argument.SetBoolean("backgroundColorsAndImages", | |
| 195 settings_->background_colors_and_images); | |
| 196 state_ = kWaitingToSendMargins; | |
| 197 } else if (state_ == kWaitingToSendMargins) { | |
| 198 script_argument.SetInteger("margins", settings_->margins); | |
| 199 state_ = kWaitingForFinalMessage; | |
| 200 } else if (state_ == kWaitingForFinalMessage) { | |
| 201 EndLoop(); | |
|
Lei Zhang
2014/07/08 01:11:36
Return after this? Your code should have failed th
ivandavid
2014/07/08 23:24:20
Done.
| |
| 202 } | |
| 203 | |
| 204 DCHECK(!script_argument.empty()); | |
| 205 GetUI()->web_ui()->CallJavascriptFunction( | |
| 206 "onManipulateSettingsForTest", script_argument); | |
| 207 } | |
| 208 | |
| 209 // Function to set the print preview settings and save them so they | |
| 210 // can be sent later. Currently only used in the constructor. Will be | |
| 211 // used when creating a test and take command line arguments. | |
| 212 // |page_numbers| is a comma separated page range. | |
| 213 // Example: "1-5,9" will print pages 1 through 5 and page 9. | |
| 214 // The pages specified must be less than or equal to the maximum | |
| 215 // page number. An empty string seems to be valid input, however | |
| 216 // further testing will be required to see if that is actually | |
| 217 // true. | |
| 218 void SetPrintPreviewSettings(const PrintPreviewSettings& settings) { | |
| 219 settings_.reset(new PrintPreviewSettings(settings)); | |
| 220 } | |
| 221 | |
| 222 private: | |
| 223 // Called when the observer gets the IPC message stating that the | |
| 224 // page count is ready. | |
| 225 // Due to forward declaration problem, the definition of the function | |
| 226 // must be separated from the declaration. | |
| 227 void OnDidGetPreviewPageCount( | |
| 228 const PrintHostMsg_DidGetPreviewPageCount_Params ¶ms); | |
| 229 | |
| 230 void DidCloneToNewWebContents(WebContents* old_web_contents, | |
| 231 WebContents* new_web_contents) | |
| 232 OVERRIDE { | |
|
Dan Beam
2014/07/07 23:08:11
indent off on OVERRIDE
ivandavid
2014/07/08 01:07:41
Done.
Lei Zhang
2014/07/08 01:11:36
Or just put it on the previous line...
ivandavid
2014/07/08 23:24:20
Done.
| |
| 233 Observe(new_web_contents); | |
| 234 } | |
| 235 | |
| 236 void WebContentsDestroyed() OVERRIDE { | |
| 237 EndLoop(); | |
| 238 } | |
| 239 | |
| 240 Browser* browser_; | |
| 241 base::Closure closure_; | |
| 242 scoped_ptr<PrintPreviewSettings> settings_; | |
| 243 State state_; | |
| 244 | |
| 245 DISALLOW_COPY_AND_ASSIGN(PrintPreviewObserver); | |
| 246 }; | |
| 247 | |
| 248 // Listens for messages from the print preview ui. Its a different | |
| 249 // set of messages, which is why two different classes are needed for this. | |
| 250 // When it gets the "UILoadedForTest" message, it prompts the observer to | |
| 251 // send a new message. When it gets the "UIFailedLoadingForTest" it just | |
| 252 // calls FAIL() and the test stops. | |
| 253 class UIDoneLoadingMessageHandler : public content::WebUIMessageHandler { | |
| 254 public: | |
| 255 explicit UIDoneLoadingMessageHandler(PrintPreviewObserver* observer) : | |
|
Dan Beam
2014/07/07 23:08:11
: on next line
ivandavid
2014/07/08 01:07:41
Done.
| |
| 256 observer_(observer) {} | |
| 257 | |
| 258 virtual ~UIDoneLoadingMessageHandler() {} | |
| 259 | |
| 260 // When a setting has been set succesfully, this is called and | |
| 261 // the observer_ is told to send the next setting to be set. | |
| 262 void HandleDone(const base::ListValue* /* args */) { | |
| 263 observer_->ManipulatePreviewSettings(); | |
|
Dan Beam
2014/07/07 23:08:12
can |observer_| be NULL?
ivandavid
2014/07/08 01:07:41
No it shouldn't. I added an ASSERT_TRUE to make su
ivandavid
2014/07/08 01:07:41
Done.
| |
| 264 } | |
| 265 | |
| 266 // Ends the test because a setting was not set successfully, | |
| 267 // therefore, the test shouldn't continue. | |
| 268 void HandleFailure(const base::ListValue* /* args */) { | |
| 269 FAIL(); | |
| 270 } | |
| 271 | |
| 272 // Sets up this class to listen for the UILoadedForTest and | |
| 273 // UIFailedLoadingForTest messages. These messages are sent | |
| 274 // by print_preview.js. On UILoadedForTest, a settings has | |
| 275 // been successfully set and its effects on the pdf have been finalized. | |
| 276 // On UIFaieldLoadingForTest a setting has not been successfully set | |
| 277 // and the test should fail. | |
| 278 void RegisterMessages() OVERRIDE { | |
| 279 web_ui()->RegisterMessageCallback( | |
| 280 "UILoadedForTest", | |
| 281 base::Bind(&UIDoneLoadingMessageHandler::HandleDone, | |
| 282 base::Unretained(this))); | |
|
Dan Beam
2014/07/07 23:08:11
indent off
ivandavid
2014/07/08 01:07:41
Done.
Lei Zhang
2014/07/08 01:11:36
That is, indentation is off on line 280 and downwa
ivandavid
2014/07/08 23:24:20
Done.
| |
| 283 | |
| 284 web_ui()->RegisterMessageCallback( | |
| 285 "UIFailedLoadingForTest", | |
| 286 base::Bind(&UIDoneLoadingMessageHandler::HandleFailure, | |
| 287 base::Unretained(this))); | |
|
Dan Beam
2014/07/07 23:08:12
indent off
ivandavid
2014/07/08 01:07:41
Done.
| |
| 288 } | |
| 289 | |
| 290 private: | |
| 291 PrintPreviewObserver* observer_; | |
| 292 | |
| 293 DISALLOW_COPY_AND_ASSIGN(UIDoneLoadingMessageHandler); | |
| 294 }; | |
| 295 | |
| 296 // This function needs to be forward declared. | |
|
Dan Beam
2014/07/07 23:08:11
I don't think you need this forward if you move UI
ivandavid
2014/07/08 01:07:41
If I did that, I would have had another forward de
ivandavid
2014/07/08 01:07:42
Done.
| |
| 297 void PrintPreviewObserver::OnDidGetPreviewPageCount( | |
| 298 const PrintHostMsg_DidGetPreviewPageCount_Params ¶ms) { | |
| 299 WebContents* web_contents = GetDialog(); | |
| 300 PrintPreviewUI* ui = GetUI(); | |
| 301 ASSERT_TRUE(ui); | |
| 302 ASSERT_TRUE(ui->web_ui()); | |
| 303 Observe(web_contents); | |
| 304 ASSERT_TRUE(web_contents); | |
| 305 ui->web_ui()->AddMessageHandler(new UIDoneLoadingMessageHandler(this)); | |
| 306 ui->web_ui()->CallJavascriptFunction("onEnableManipulateSettingsForTest"); | |
| 307 } | |
| 308 | |
| 309 class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest { | |
| 310 public: | |
| 311 PrintPreviewPdfGeneratedBrowserTest() {} | |
| 312 virtual ~PrintPreviewPdfGeneratedBrowserTest() {} | |
| 313 | |
| 314 // Navigates to the web page given, then initiates print preview | |
| 315 // and waits for all the settings to be set. | |
| 316 void NavigateAndPreview(const base::FilePath::StringType& file_name, | |
| 317 PrintPreviewSettings settings) { | |
| 318 print_preview_observer_->SetPrintPreviewSettings(settings); | |
| 319 base::FilePath path(file_name); | |
| 320 GURL gurl = net::FilePathToFileURL(path); | |
| 321 | |
| 322 ui_test_utils::NavigateToURL(browser(), gurl); | |
| 323 | |
| 324 base::RunLoop loop; | |
| 325 print_preview_observer_->set_quit_closure(loop.QuitClosure()); | |
| 326 chrome::Print(browser()); | |
| 327 loop.Run(); | |
| 328 } | |
| 329 | |
| 330 // Prints the webpage to pdf, after the settings have been set. | |
| 331 void Print() { | |
| 332 ASSERT_FALSE(pdf_file_save_path_.empty()); | |
| 333 base::RunLoop loop; | |
| 334 print_preview_observer_->set_quit_closure(loop.QuitClosure()); | |
| 335 print_preview_observer_->GetUI()->handler_->FileSelected( | |
| 336 pdf_file_save_path_, 0, NULL); | |
| 337 loop.Run(); | |
| 338 } | |
| 339 | |
| 340 // Converts the pdf to a png file, so that the layout test can | |
| 341 // do an image diff on this image and a reference image. | |
| 342 void PdfToPng() { | |
|
Lei Zhang
2014/07/08 01:11:36
PdfToPng() is over 100 lines long. Consider breaki
ivandavid
2014/07/08 23:24:20
Done.
ivandavid
2014/07/08 23:24:21
I turned lines 344-366 into a different setup func
| |
| 343 // Get the library functions to get the pdf info. | |
| 344 base::ScopedNativeLibrary pdf_lib; | |
| 345 base::FilePath pdf_module_path; | |
| 346 | |
| 347 ASSERT_TRUE(PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_module_path)); | |
| 348 ASSERT_TRUE(base::PathExists(pdf_module_path)); | |
| 349 pdf_lib.Reset(base::LoadNativeLibrary(pdf_module_path, NULL)); | |
| 350 | |
| 351 ASSERT_TRUE(pdf_lib.is_valid()); | |
| 352 pdf_to_bitmap_func_ = | |
| 353 reinterpret_cast<PDFPageToBitmap>( | |
| 354 pdf_lib.GetFunctionPointer("RenderPDFPageToBitmap")); | |
| 355 | |
| 356 pdf_doc_info_func_ = | |
| 357 reinterpret_cast<GetPDFDocInfoProc>( | |
| 358 pdf_lib.GetFunctionPointer("GetPDFDocInfo")); | |
| 359 | |
| 360 pdf_page_size_func_ = | |
| 361 reinterpret_cast<GetPDFPageSizeByIndexProc>( | |
| 362 pdf_lib.GetFunctionPointer("GetPDFPageSizeByIndex")); | |
| 363 | |
| 364 ASSERT_TRUE(pdf_to_bitmap_func_); | |
| 365 ASSERT_TRUE(pdf_doc_info_func_); | |
| 366 ASSERT_TRUE(pdf_page_size_func_); | |
| 367 | |
| 368 std::string data; | |
| 369 int num_pages; | |
| 370 ASSERT_TRUE(base::ReadFileToString(pdf_file_save_path_, &data)); | |
| 371 ASSERT_TRUE(pdf_doc_info_func_(data.data(), | |
| 372 data.size(), | |
| 373 &num_pages, | |
|
Lei Zhang
2014/07/08 01:11:36
ASSERT |num_pages| is positive just in case.
ivandavid
2014/07/08 23:24:20
Done.
| |
| 374 NULL)); | |
| 375 std::vector<uint8_t> bitmap_data; | |
| 376 double min_width = std::numeric_limits<double>::max(); | |
| 377 double total_height = 0; | |
| 378 | |
| 379 for (int i = 0; i < num_pages; i++) { | |
| 380 double height, width; | |
|
Lei Zhang
2014/07/08 01:11:36
Since we generally think of sizes as WxH, maybe de
ivandavid
2014/07/08 23:24:21
Done. I didn't realize how annoying this was until
| |
| 381 ASSERT_TRUE(pdf_page_size_func_( | |
| 382 data.data(), data.size(), i, &width, &height)); | |
| 383 | |
| 384 // Can't seem to access printing/units.h due to linking difficulties. | |
| 385 // This will do for now. | |
|
Dan Beam
2014/07/07 23:08:11
i think you simply need to add PRINTING_EXPORT to
ivandavid
2014/07/08 01:07:41
I actually had problems accessing the function I n
ivandavid
2014/07/08 01:07:41
Done.
| |
| 386 | |
| 387 const double kPointsPerInch = 72.0; | |
| 388 const double kPixelsPerInch = 96.0; | |
| 389 | |
| 390 height = height * kPixelsPerInch / kPointsPerInch; | |
|
Lei Zhang
2014/07/08 01:11:36
Use printing::ConvertUnit() ?
ivandavid
2014/07/08 23:24:20
Done.
| |
| 391 width = width * kPixelsPerInch / kPointsPerInch; | |
| 392 bool autorotate = false; | |
|
Lei Zhang
2014/07/08 01:11:36
Can you explain why we need to rotate landscape im
ivandavid
2014/07/08 23:24:20
Added a comment.
| |
| 393 | |
| 394 if (height < width) { | |
| 395 double temp = height; | |
|
Lei Zhang
2014/07/08 01:11:36
use std::swap()
ivandavid
2014/07/08 23:24:20
Done.
| |
| 396 height = width; | |
| 397 width = temp; | |
| 398 autorotate = true; | |
| 399 } | |
| 400 | |
| 401 total_height += height; | |
| 402 | |
| 403 if (width < min_width) | |
| 404 min_width = width; | |
| 405 | |
| 406 gfx::Rect rect(width, height); | |
| 407 printing::PdfRenderSettings settings(rect, 300, true); | |
|
Lei Zhang
2014/07/08 01:11:36
What is 300? Is it the DPI? Make it a constant ple
ivandavid
2014/07/08 23:24:21
Done.
| |
| 408 | |
| 409 // Detecting overflow. If a * b > max_size then there is overflow. | |
| 410 // So just check if a > max_size / b. | |
| 411 // Need to check both the cases in which GetArea() overflows and where | |
| 412 // GetArea() * 4 overflows. If the latter is just checked, then it is | |
| 413 // possible that if GetArea() overflows, the latter case will pass by | |
| 414 // virtue of it overflowing to a value of 1 or something like that. | |
| 415 // Also, for the first comparison, height is promoted to size_t. | |
| 416 size_t max_size = std::numeric_limits<size_t>::max(); | |
|
ivandavid
2014/07/03 03:12:03
I think I did this correctly. I feel like my reaso
| |
| 417 if (static_cast<size_t>(settings.area().size().width()) > | |
|
Lei Zhang
2014/07/08 01:11:36
Can't you just call area().width() and skip the si
Lei Zhang
2014/07/08 01:11:36
Why do you need to cast this to a size_t?
ivandavid
2014/07/08 23:24:20
settings.area().width() is a signed type, however
ivandavid
2014/07/08 23:24:21
Done.
| |
| 418 max_size / settings.area().size().height() || | |
| 419 static_cast<size_t>(settings.area().size().GetArea()) > | |
| 420 max_size / 4) { | |
| 421 LOG(ERROR) << "OVERFLOW DETECTED: IMAGE WIDTH OR HEIGHT IS TOO LARGE!"; | |
|
Lei Zhang
2014/07/08 01:11:36
You can just FAIL() << "oh noes!";
ivandavid
2014/07/08 23:24:20
Done.
| |
| 422 FAIL(); | |
| 423 } | |
| 424 | |
| 425 std::vector<uint8_t> page_bitmap_data( | |
| 426 4 * settings.area().size().GetArea()); | |
| 427 | |
| 428 ASSERT_TRUE(pdf_to_bitmap_func_(data.data(), | |
| 429 data.size(), | |
| 430 i, | |
| 431 page_bitmap_data.data(), | |
| 432 settings.area().size().width(), | |
| 433 settings.area().size().height(), | |
| 434 settings.dpi(), | |
| 435 settings.dpi(), | |
| 436 autorotate)); | |
| 437 | |
| 438 bitmap_data.insert(bitmap_data.end(), | |
| 439 page_bitmap_data.begin(), | |
| 440 page_bitmap_data.end()); | |
| 441 } | |
| 442 | |
| 443 std::string hash_input(bitmap_data.begin(), bitmap_data.end()); | |
| 444 base::MD5Sum(static_cast<void*>(const_cast<char*>(hash_input.data())), | |
|
Lei Zhang
2014/07/08 01:11:37
Do you need the const_cast? base::MD5Sum() takes a
ivandavid
2014/07/08 23:24:20
Done.
| |
| 445 hash_input.size(), | |
| 446 &hash_); | |
| 447 | |
| 448 gfx::Rect png_rect(min_width, total_height); | |
|
Lei Zhang
2014/07/08 01:11:36
If you start with a 2 page PDF file source, and th
ivandavid
2014/07/08 23:24:20
Done. I made it so that the bitmap gets filled in
| |
| 449 | |
| 450 std::string comment_title("tEXtchecksum\x00"); | |
| 451 gfx::PNGCodec::Comment hash_comment( | |
| 452 comment_title, | |
| 453 base::MD5DigestToBase16(hash_)); | |
| 454 | |
| 455 std::vector<gfx::PNGCodec::Comment> comments; | |
| 456 comments.push_back(hash_comment); | |
| 457 | |
| 458 ASSERT_TRUE( | |
| 459 gfx::PNGCodec::Encode(static_cast<unsigned char*>( | |
| 460 bitmap_data.data()), | |
| 461 gfx::PNGCodec::FORMAT_BGRA, | |
| 462 png_rect.size(), | |
| 463 png_rect.size().width() * sizeof(uint32_t), | |
| 464 false, | |
| 465 comments, | |
| 466 &output_)); | |
| 467 } | |
| 468 | |
| 469 // Sends the png image to the layout test framework for comparison. | |
| 470 void SendPng() { | |
| 471 // Send image header & hash_ | |
| 472 printf("Content-Type: image/png\n"); | |
| 473 printf("ActualHash: %s\n", base::MD5DigestToBase16(hash_).data()); | |
| 474 printf("Content-Length: %" PRIuS "\n", output_.size()); | |
| 475 | |
| 476 for (size_t i = 0; i < output_.size(); ++i) | |
| 477 printf("%c", output_[i]); | |
| 478 | |
| 479 printf("#EOF\n"); | |
| 480 fflush(stdout); | |
| 481 fprintf(stderr, "#EOF\n"); | |
| 482 fflush(stderr); | |
| 483 } | |
| 484 | |
| 485 // Duplicates the tab that was created when the browser opened. | |
| 486 // This is done, so that the observer can listen to the duplicated | |
| 487 // tab as soon as possible and start listening for messages related to | |
| 488 // print preview. | |
| 489 void DuplicateTab() { | |
| 490 WebContents* tab = | |
| 491 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 492 ASSERT_TRUE(tab); | |
| 493 | |
| 494 print_preview_observer_.reset( | |
| 495 new PrintPreviewObserver(tab, browser())); | |
|
Lei Zhang
2014/07/08 01:11:36
This fits on the previous line just fine before...
ivandavid
2014/07/08 23:24:20
Done.
| |
| 496 chrome::DuplicateTab(browser()); | |
| 497 | |
| 498 WebContents* initiator = | |
| 499 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 500 ASSERT_TRUE(initiator); | |
| 501 ASSERT_NE(tab, initiator); | |
| 502 } | |
| 503 | |
| 504 // Resets the test so that another web page can be printing. | |
| 505 // Deletes the duplicate tab as it isn't needed anymore. | |
| 506 void Clean() { | |
|
Dan Beam
2014/07/07 23:08:11
nit: Reset()
ivandavid
2014/07/08 01:07:41
Done.
| |
| 507 output_.clear(); | |
| 508 ASSERT_EQ(browser()->tab_strip_model()->count(), 2); | |
| 509 chrome::CloseTab(browser()); | |
| 510 ASSERT_EQ(browser()->tab_strip_model()->count(), 1); | |
| 511 } | |
| 512 | |
| 513 // Creates a temporary directory to store the file that will be used for | |
| 514 // stdin to accept input. Also sets up the path to save the pdf file | |
| 515 // that will be printed. Everything is cleaned up automatically once | |
| 516 // the test ends. | |
| 517 void SetupStdinAndSavePath() { | |
| 518 printf("#READY\n"); | |
|
Dan Beam
2014/07/07 23:08:11
what is this doing?
ivandavid
2014/07/08 01:07:41
See comment on line 615.
| |
| 519 fflush(stdout); | |
| 520 | |
| 521 ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir()); | |
| 522 ASSERT_TRUE(base::CreateTemporaryFileInDir(tmp_dir_.path(), &tmp_path_)); | |
| 523 ASSERT_TRUE(freopen(tmp_path_.value().c_str(), "r", stdin)); | |
| 524 | |
| 525 pdf_file_save_path_ = tmp_dir_.path().AppendASCII("dummy.pdf"); | |
| 526 | |
| 527 printf("StdinPath: %s\n#EOF\n", tmp_path_.value().c_str()); | |
| 528 fflush(stdout); | |
| 529 } | |
| 530 | |
| 531 private: | |
| 532 scoped_ptr<PrintPreviewObserver> print_preview_observer_; | |
| 533 base::FilePath pdf_file_save_path_; | |
| 534 | |
| 535 typedef bool (*PDFPageToBitmap) (const void * pdf_buffer, | |
|
Dan Beam
2014/07/07 23:08:10
nit: ) ( -> )(
Lei Zhang
2014/07/08 01:11:36
and void * -> void*
Lei Zhang
2014/07/08 01:11:36
And name it PDFPageToBitmapProc to be consistent w
ivandavid
2014/07/08 23:24:21
Done.
| |
| 536 int pdf_buffer_size, | |
| 537 int page_number, | |
| 538 void* bitmap_buffer, | |
| 539 int bitmap_width, | |
| 540 int bitmap_height, | |
| 541 int dpi_x, | |
| 542 int dpi_y, | |
| 543 bool autorotate); | |
| 544 | |
| 545 typedef bool (*GetPDFDocInfoProc)(const void* pdf_buffer, | |
| 546 int buffer_size, | |
| 547 int* pages_count, | |
| 548 double* max_page_width); | |
| 549 | |
| 550 typedef bool (*GetPDFPageSizeByIndexProc)(const void* pdf_buffer, | |
| 551 int buffer_size, | |
| 552 int index, | |
| 553 double* width, | |
| 554 double* height); | |
| 555 | |
| 556 PDFPageToBitmap pdf_to_bitmap_func_; | |
|
Lei Zhang
2014/07/08 01:11:36
Document your member variables unless they are com
ivandavid
2014/07/08 23:24:20
Done.
| |
| 557 GetPDFDocInfoProc pdf_doc_info_func_; | |
| 558 GetPDFPageSizeByIndexProc pdf_page_size_func_; | |
| 559 std::vector<unsigned char> output_; | |
| 560 base::MD5Digest hash_; | |
| 561 base::ScopedTempDir tmp_dir_; | |
| 562 base::FilePath tmp_path_; | |
| 563 | |
| 564 DISALLOW_COPY_AND_ASSIGN(PrintPreviewPdfGeneratedBrowserTest); | |
| 565 }; | |
| 566 | |
| 567 IN_PROC_BROWSER_TEST_F(PrintPreviewPdfGeneratedBrowserTest, | |
| 568 MANUAL_DummyTest) { | |
|
ivandavid
2014/07/03 03:12:03
Now a manual test since it should only be run with
Dan Beam
2014/07/07 23:08:11
what's the point of a manual test like this?
ivandavid
2014/07/08 01:07:41
It was a suggestion by Dirk Pranke that he made in
| |
| 569 // What this code is supposed to do: Setup communication with the | |
|
Dan Beam
2014/07/07 23:08:10
make into an ordered list:
- much organized
- so p
ivandavid
2014/07/08 01:07:41
Done.
| |
| 570 // layout test framework, print webpage to a pdf, convert that | |
| 571 // pdf to a png, then send that png file to the layout test framework, | |
| 572 // where the framework will do an image diff. | |
| 573 | |
|
Dan Beam
2014/07/07 23:08:11
remove \n
ivandavid
2014/07/08 01:07:41
Done.
| |
| 574 SetupStdinAndSavePath(); | |
| 575 | |
| 576 // There is no way to determine how many tests are to be run | |
| 577 // ahead of time without a ton of modifications to the layout | |
| 578 // test framework. However, whenever it is done with a set of tests, | |
| 579 // it calls SIGKILL on the browser test that ran those set of tests. | |
| 580 // Thus, this while loop will end once the layout test framework | |
| 581 // decides to actually kill this process. For this to work, | |
| 582 // the test must be run with '--single_process'. There must be an | |
| 583 // underscore, not a dash, as that is something different. | |
| 584 while (true) { | |
| 585 DuplicateTab(); | |
| 586 | |
| 587 base::FilePath::StringType cmd; | |
| 588 std::getline(STDIN_STREAM, cmd); | |
| 589 if (cmd.empty()) { | |
| 590 while (STDIN_STREAM.eof()) { | |
| 591 STDIN_STREAM.clear(); | |
| 592 std::getline(STDIN_STREAM, cmd); | |
| 593 if (!cmd.empty()) { | |
| 594 break; | |
| 595 } | |
| 596 } | |
| 597 } | |
| 598 | |
| 599 // TODO(ivandavid): Get settings from file in resources. | |
|
ivandavid
2014/07/03 03:12:03
I will do this once I start actually writing tests
| |
| 600 PrintPreviewSettings settings( | |
| 601 true, | |
| 602 "", | |
| 603 false, | |
| 604 false, | |
| 605 printing::DEFAULT_MARGINS, | |
| 606 cmd.find(".pdf") != base::FilePath::StringType::npos); | |
| 607 | |
| 608 std::vector<base::FilePath::StringType> cmd_arguments; | |
| 609 base::SplitString(cmd, '\'', &cmd_arguments); | |
| 610 base::FilePath::StringType test_name(cmd_arguments[0]); | |
| 611 NavigateAndPreview(test_name, settings); | |
| 612 Print(); | |
| 613 PdfToPng(); | |
| 614 | |
| 615 printf("#EOF\n"); | |
|
Dan Beam
2014/07/07 23:08:12
what is this doing?
ivandavid
2014/07/08 01:07:41
All of the printf("#EOF\n") statements are for lay
| |
| 616 fflush(stdout); | |
| 617 | |
| 618 SendPng(); | |
| 619 Clean(); | |
| 620 } | |
| 621 } | |
| OLD | NEW |