| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/common/chrome_switches.h" | 5 #include "chrome/common/chrome_switches.h" |
| 6 #include "chrome/common/print_messages.h" | 6 #include "chrome/common/print_messages.h" |
| 7 #include "chrome/renderer/print_web_view_helper.h" | 7 #include "chrome/renderer/print_web_view_helper.h" |
| 8 #include "chrome/test/base/render_view_test.h" | 8 #include "chrome/test/base/render_view_test.h" |
| 9 #include "printing/print_job_constants.h" | 9 #include "printing/print_job_constants.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 | 35 |
| 36 void CreatePrintSettingsDictionary(DictionaryValue* dict) { | 36 void CreatePrintSettingsDictionary(DictionaryValue* dict) { |
| 37 dict->SetBoolean(printing::kSettingLandscape, false); | 37 dict->SetBoolean(printing::kSettingLandscape, false); |
| 38 dict->SetBoolean(printing::kSettingCollate, false); | 38 dict->SetBoolean(printing::kSettingCollate, false); |
| 39 dict->SetBoolean(printing::kSettingColor, false); | 39 dict->SetBoolean(printing::kSettingColor, false); |
| 40 dict->SetBoolean(printing::kSettingPrintToPDF, true); | 40 dict->SetBoolean(printing::kSettingPrintToPDF, true); |
| 41 dict->SetInteger(printing::kSettingDuplexMode, printing::SIMPLEX); | 41 dict->SetInteger(printing::kSettingDuplexMode, printing::SIMPLEX); |
| 42 dict->SetInteger(printing::kSettingCopies, 1); | 42 dict->SetInteger(printing::kSettingCopies, 1); |
| 43 dict->SetString(printing::kSettingDeviceName, "dummy"); | 43 dict->SetString(printing::kSettingDeviceName, "dummy"); |
| 44 dict->SetInteger(printing::kPreviewRequestID, 12345); | 44 dict->SetInteger(printing::kPreviewRequestID, 12345); |
| 45 dict->SetBoolean(printing::kSettingGenerateDraftData, true); | |
| 46 } | 45 } |
| 47 | 46 |
| 48 } // namespace | 47 } // namespace |
| 49 | 48 |
| 50 class PrintWebViewHelperTestBase : public RenderViewTest { | 49 class PrintWebViewHelperTestBase : public RenderViewTest { |
| 51 public: | 50 public: |
| 52 PrintWebViewHelperTestBase() {} | 51 PrintWebViewHelperTestBase() {} |
| 53 ~PrintWebViewHelperTestBase() {} | 52 ~PrintWebViewHelperTestBase() {} |
| 54 | 53 |
| 55 protected: | 54 protected: |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 protected: | 313 protected: |
| 315 void VerifyPrintPreviewFailed(bool did_fail) { | 314 void VerifyPrintPreviewFailed(bool did_fail) { |
| 316 bool print_preview_failed = (render_thread_.sink().GetUniqueMessageMatching( | 315 bool print_preview_failed = (render_thread_.sink().GetUniqueMessageMatching( |
| 317 PrintHostMsg_PrintPreviewFailed::ID) != NULL); | 316 PrintHostMsg_PrintPreviewFailed::ID) != NULL); |
| 318 EXPECT_EQ(did_fail, print_preview_failed); | 317 EXPECT_EQ(did_fail, print_preview_failed); |
| 319 } | 318 } |
| 320 | 319 |
| 321 void VerifyPrintPreviewGenerated(bool generated_preview) { | 320 void VerifyPrintPreviewGenerated(bool generated_preview) { |
| 322 const IPC::Message* preview_msg = | 321 const IPC::Message* preview_msg = |
| 323 render_thread_.sink().GetUniqueMessageMatching( | 322 render_thread_.sink().GetUniqueMessageMatching( |
| 324 PrintHostMsg_MetafileReadyForPrinting::ID); | 323 PrintHostMsg_PagesReadyForPreview::ID); |
| 325 bool did_get_preview_msg = (NULL != preview_msg); | 324 bool did_get_preview_msg = (NULL != preview_msg); |
| 326 ASSERT_EQ(generated_preview, did_get_preview_msg); | 325 ASSERT_EQ(generated_preview, did_get_preview_msg); |
| 327 if (did_get_preview_msg) { | 326 if (did_get_preview_msg) { |
| 328 PrintHostMsg_MetafileReadyForPrinting::Param preview_param; | 327 PrintHostMsg_PagesReadyForPreview::Param preview_param; |
| 329 PrintHostMsg_MetafileReadyForPrinting::Read(preview_msg, &preview_param); | 328 PrintHostMsg_PagesReadyForPreview::Read(preview_msg, &preview_param); |
| 330 EXPECT_NE(0, preview_param.a.document_cookie); | 329 EXPECT_NE(0, preview_param.a.document_cookie); |
| 331 EXPECT_NE(0, preview_param.a.expected_pages_count); | 330 EXPECT_NE(0, preview_param.a.expected_pages_count); |
| 332 EXPECT_NE(0U, preview_param.a.data_size); | 331 EXPECT_NE(0U, preview_param.a.data_size); |
| 333 } | 332 } |
| 334 } | 333 } |
| 335 | 334 |
| 336 void VerifyPrintFailed(bool did_fail) { | 335 void VerifyPrintFailed(bool did_fail) { |
| 337 bool print_failed = (render_thread_.sink().GetUniqueMessageMatching( | 336 bool print_failed = (render_thread_.sink().GetUniqueMessageMatching( |
| 338 PrintHostMsg_PrintingFailed::ID) != NULL); | 337 PrintHostMsg_PrintingFailed::ID) != NULL); |
| 339 EXPECT_EQ(did_fail, print_failed); | 338 EXPECT_EQ(did_fail, print_failed); |
| 340 } | 339 } |
| 341 | 340 |
| 342 // |page_number| is 0-based. | |
| 343 void VerifyDidPreviewPage(bool generate_draft_pages, int page_number) { | |
| 344 bool msg_found = false; | |
| 345 size_t msg_count = render_thread_.sink().message_count(); | |
| 346 for (size_t i = 0; i < msg_count; ++i) { | |
| 347 const IPC::Message* msg = render_thread_.sink().GetMessageAt(i); | |
| 348 if (msg->type() == PrintHostMsg_DidPreviewPage::ID) { | |
| 349 PrintHostMsg_DidPreviewPage::Param page_param; | |
| 350 PrintHostMsg_DidPreviewPage::Read(msg, &page_param); | |
| 351 if (page_param.a.page_number == page_number) { | |
| 352 msg_found = true; | |
| 353 if (generate_draft_pages) | |
| 354 EXPECT_NE(0U, page_param.a.data_size); | |
| 355 else | |
| 356 EXPECT_EQ(0U, page_param.a.data_size); | |
| 357 break; | |
| 358 } | |
| 359 } | |
| 360 } | |
| 361 ASSERT_EQ(true, msg_found); | |
| 362 } | |
| 363 | |
| 364 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperPreviewTest); | 341 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperPreviewTest); |
| 365 }; | 342 }; |
| 366 | 343 |
| 367 // Tests that print preview work and sending and receiving messages through | 344 // Tests that print preview work and sending and receiving messages through |
| 368 // that channel all works. | 345 // that channel all works. |
| 369 TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreview) { | 346 TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreview) { |
| 370 LoadHTML(kHelloWorldHTML); | 347 LoadHTML(kHelloWorldHTML); |
| 371 | 348 |
| 372 PrintWebViewHelper::Get(view_)->OnInitiatePrintPreview(); | 349 PrintWebViewHelper::Get(view_)->OnInitiatePrintPreview(); |
| 373 // Fill in some dummy values. | 350 // Fill in some dummy values. |
| 374 DictionaryValue dict; | 351 DictionaryValue dict; |
| 375 CreatePrintSettingsDictionary(&dict); | 352 CreatePrintSettingsDictionary(&dict); |
| 376 PrintWebViewHelper::Get(view_)->OnPrintPreview(dict); | 353 PrintWebViewHelper::Get(view_)->OnPrintPreview(dict); |
| 377 | 354 |
| 378 // Need to finish simulating print preview. | 355 // Need to finish simulating print preview. |
| 379 // Generate the page and finalize it. | 356 // Generate the page and finalize it. |
| 380 PrintWebViewHelper::Get(view_)->OnContinuePreview(); | 357 PrintWebViewHelper::Get(view_)->OnContinuePreview( |
| 381 | 358 printing::INVALID_PAGE_INDEX); |
| 382 // Verify that we did create the draft metafile for the first page. | 359 PrintWebViewHelper::Get(view_)->OnContinuePreview( |
| 383 VerifyDidPreviewPage(true, 0); | 360 printing::INVALID_PAGE_INDEX); |
| 384 PrintWebViewHelper::Get(view_)->OnContinuePreview(); | |
| 385 | |
| 386 EXPECT_EQ(0, render_thread_.print_preview_pages_remaining()); | |
| 387 VerifyPrintPreviewFailed(false); | |
| 388 VerifyPrintPreviewGenerated(true); | |
| 389 VerifyPagesPrinted(false); | |
| 390 } | |
| 391 | |
| 392 // Test to verify that complete metafile is generated for a subset of pages | |
| 393 // without creating draft pages. | |
| 394 TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreviewForSelectedPages) { | |
| 395 LoadHTML(kHelloWorldHTML); | |
| 396 | |
| 397 PrintWebViewHelper::Get(view_)->OnInitiatePrintPreview(); | |
| 398 // Fill in some dummy values. | |
| 399 DictionaryValue dict; | |
| 400 CreatePrintSettingsDictionary(&dict); | |
| 401 | |
| 402 // Set a page range and update the dictionary to generate only the complete | |
| 403 // metafile with the selected pages. Page numbers used in the dictionary | |
| 404 // are 1-based. | |
| 405 DictionaryValue* page_range = new DictionaryValue(); | |
| 406 page_range->SetInteger(printing::kSettingPageRangeFrom, 1); | |
| 407 page_range->SetInteger(printing::kSettingPageRangeTo, 1); | |
| 408 | |
| 409 ListValue* page_range_array = new ListValue(); | |
| 410 page_range_array->Append(page_range); | |
| 411 | |
| 412 dict.Set(printing::kSettingPageRange, page_range_array); | |
| 413 dict.SetBoolean(printing::kSettingGenerateDraftData, false); | |
| 414 | |
| 415 PrintWebViewHelper::Get(view_)->OnPrintPreview(dict); | |
| 416 | |
| 417 // Need to finish simulating print preview. | |
| 418 // Generate the page and finalize it. | |
| 419 PrintWebViewHelper::Get(view_)->OnContinuePreview(); | |
| 420 | |
| 421 // Verify that we did not create the draft metafile for the first page. | |
| 422 VerifyDidPreviewPage(false, 0); | |
| 423 PrintWebViewHelper::Get(view_)->OnContinuePreview(); | |
| 424 | 361 |
| 425 EXPECT_EQ(0, render_thread_.print_preview_pages_remaining()); | 362 EXPECT_EQ(0, render_thread_.print_preview_pages_remaining()); |
| 426 VerifyPrintPreviewFailed(false); | 363 VerifyPrintPreviewFailed(false); |
| 427 VerifyPrintPreviewGenerated(true); | 364 VerifyPrintPreviewGenerated(true); |
| 428 VerifyPagesPrinted(false); | 365 VerifyPagesPrinted(false); |
| 429 } | 366 } |
| 430 | 367 |
| 431 // Tests that print preview fails and receiving error messages through | 368 // Tests that print preview fails and receiving error messages through |
| 432 // that channel all works. | 369 // that channel all works. |
| 433 TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreviewFail) { | 370 TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreviewFail) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 464 LoadHTML(kPrintPreviewHTML); | 401 LoadHTML(kPrintPreviewHTML); |
| 465 | 402 |
| 466 // An empty dictionary should fail. | 403 // An empty dictionary should fail. |
| 467 DictionaryValue empty_dict; | 404 DictionaryValue empty_dict; |
| 468 PrintWebViewHelper::Get(view_)->OnPrintForPrintPreview(empty_dict); | 405 PrintWebViewHelper::Get(view_)->OnPrintForPrintPreview(empty_dict); |
| 469 | 406 |
| 470 VerifyPrintFailed(true); | 407 VerifyPrintFailed(true); |
| 471 VerifyPagesPrinted(false); | 408 VerifyPagesPrinted(false); |
| 472 } | 409 } |
| 473 #endif // !defined(OS_CHROMEOS) | 410 #endif // !defined(OS_CHROMEOS) |
| OLD | NEW |