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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
43 dict->SetBoolean(printing::kSettingColor, false); | 43 dict->SetBoolean(printing::kSettingColor, false); |
44 dict->SetBoolean(printing::kSettingPrintToPDF, true); | 44 dict->SetBoolean(printing::kSettingPrintToPDF, true); |
45 dict->SetInteger(printing::kSettingDuplexMode, printing::SIMPLEX); | 45 dict->SetInteger(printing::kSettingDuplexMode, printing::SIMPLEX); |
46 dict->SetInteger(printing::kSettingCopies, 1); | 46 dict->SetInteger(printing::kSettingCopies, 1); |
47 dict->SetString(printing::kSettingDeviceName, "dummy"); | 47 dict->SetString(printing::kSettingDeviceName, "dummy"); |
48 dict->SetString(printing::kPreviewUIAddr, "0xb33fbeef"); | 48 dict->SetString(printing::kPreviewUIAddr, "0xb33fbeef"); |
49 dict->SetInteger(printing::kPreviewRequestID, 12345); | 49 dict->SetInteger(printing::kPreviewRequestID, 12345); |
50 dict->SetBoolean(printing::kIsFirstRequest, true); | 50 dict->SetBoolean(printing::kIsFirstRequest, true); |
51 dict->SetBoolean(printing::kSettingDefaultMarginsSelected, true); | 51 dict->SetBoolean(printing::kSettingDefaultMarginsSelected, true); |
52 dict->SetBoolean(printing::kSettingHeaderFooterEnabled, false); | 52 dict->SetBoolean(printing::kSettingHeaderFooterEnabled, false); |
53 dict->SetBoolean(printing::kSettingGenerateDraftData, true); | |
53 } | 54 } |
54 | 55 |
55 } // namespace | 56 } // namespace |
56 | 57 |
57 class PrintWebViewHelperTestBase : public RenderViewTest { | 58 class PrintWebViewHelperTestBase : public RenderViewTest { |
58 public: | 59 public: |
59 PrintWebViewHelperTestBase() {} | 60 PrintWebViewHelperTestBase() {} |
60 ~PrintWebViewHelperTestBase() {} | 61 ~PrintWebViewHelperTestBase() {} |
61 | 62 |
62 protected: | 63 protected: |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
320 | 321 |
321 void VerifyPrintPreviewFailed(bool did_fail) { | 322 void VerifyPrintPreviewFailed(bool did_fail) { |
322 bool print_preview_failed = (render_thread_.sink().GetUniqueMessageMatching( | 323 bool print_preview_failed = (render_thread_.sink().GetUniqueMessageMatching( |
323 PrintHostMsg_PrintPreviewFailed::ID) != NULL); | 324 PrintHostMsg_PrintPreviewFailed::ID) != NULL); |
324 EXPECT_EQ(did_fail, print_preview_failed); | 325 EXPECT_EQ(did_fail, print_preview_failed); |
325 } | 326 } |
326 | 327 |
327 void VerifyPrintPreviewGenerated(bool generated_preview) { | 328 void VerifyPrintPreviewGenerated(bool generated_preview) { |
328 const IPC::Message* preview_msg = | 329 const IPC::Message* preview_msg = |
329 render_thread_.sink().GetUniqueMessageMatching( | 330 render_thread_.sink().GetUniqueMessageMatching( |
330 PrintHostMsg_PagesReadyForPreview::ID); | 331 PrintHostMsg_MetafileReadyForPrinting::ID); |
331 bool did_get_preview_msg = (NULL != preview_msg); | 332 bool did_get_preview_msg = (NULL != preview_msg); |
332 ASSERT_EQ(generated_preview, did_get_preview_msg); | 333 ASSERT_EQ(generated_preview, did_get_preview_msg); |
333 if (did_get_preview_msg) { | 334 if (did_get_preview_msg) { |
334 PrintHostMsg_PagesReadyForPreview::Param preview_param; | 335 PrintHostMsg_MetafileReadyForPrinting::Param preview_param; |
335 PrintHostMsg_PagesReadyForPreview::Read(preview_msg, &preview_param); | 336 PrintHostMsg_MetafileReadyForPrinting::Read(preview_msg, &preview_param); |
336 EXPECT_NE(0, preview_param.a.document_cookie); | 337 EXPECT_NE(0, preview_param.a.document_cookie); |
337 EXPECT_NE(0, preview_param.a.expected_pages_count); | 338 EXPECT_NE(0, preview_param.a.expected_pages_count); |
338 EXPECT_NE(0U, preview_param.a.data_size); | 339 EXPECT_NE(0U, preview_param.a.data_size); |
339 } | 340 } |
340 } | 341 } |
341 | 342 |
342 void VerifyPrintFailed(bool did_fail) { | 343 void VerifyPrintFailed(bool did_fail) { |
343 bool print_failed = (render_thread_.sink().GetUniqueMessageMatching( | 344 bool print_failed = (render_thread_.sink().GetUniqueMessageMatching( |
344 PrintHostMsg_PrintingFailed::ID) != NULL); | 345 PrintHostMsg_PrintingFailed::ID) != NULL); |
345 EXPECT_EQ(did_fail, print_failed); | 346 EXPECT_EQ(did_fail, print_failed); |
346 } | 347 } |
347 | 348 |
349 // |page_number| is 0-based. | |
350 void VerifyDidPreviewPage(bool generate_draft_pages, int page_number) { | |
351 bool msg_found = false; | |
352 size_t msg_count = render_thread_.sink().message_count(); | |
353 for (size_t i = 0; i < msg_count; ++i) { | |
354 const IPC::Message* msg = render_thread_.sink().GetMessageAt(i); | |
355 if (msg->type() == PrintHostMsg_DidPreviewPage::ID) { | |
356 PrintHostMsg_DidPreviewPage::Param page_param; | |
357 PrintHostMsg_DidPreviewPage::Read(msg, &page_param); | |
358 if (page_param.a.page_number == page_number) { | |
359 msg_found = true; | |
360 if (generate_draft_pages) | |
361 EXPECT_NE(0U, page_param.a.data_size); | |
362 else | |
363 EXPECT_EQ(0U, page_param.a.data_size); | |
364 break; | |
365 } | |
366 } | |
367 } | |
368 ASSERT_EQ(generate_draft_pages, msg_found); | |
369 } | |
370 | |
348 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperPreviewTest); | 371 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperPreviewTest); |
349 }; | 372 }; |
350 | 373 |
351 // Tests that print preview work and sending and receiving messages through | 374 // Tests that print preview work and sending and receiving messages through |
352 // that channel all works. | 375 // that channel all works. |
353 TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreview) { | 376 TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreview) { |
354 LoadHTML(kHelloWorldHTML); | 377 LoadHTML(kHelloWorldHTML); |
355 | 378 |
356 PrintWebViewHelper::Get(view_)->OnInitiatePrintPreview(); | 379 PrintWebViewHelper::Get(view_)->OnInitiatePrintPreview(); |
357 // Fill in some dummy values. | 380 // Fill in some dummy values. |
358 DictionaryValue dict; | 381 DictionaryValue dict; |
359 CreatePrintSettingsDictionary(&dict); | 382 CreatePrintSettingsDictionary(&dict); |
360 PrintWebViewHelper::Get(view_)->OnPrintPreview(dict); | 383 PrintWebViewHelper::Get(view_)->OnPrintPreview(dict); |
361 | 384 |
362 EXPECT_EQ(0, render_thread_.print_preview_pages_remaining()); | 385 EXPECT_EQ(0, render_thread_.print_preview_pages_remaining()); |
363 VerifyPrintPreviewCancelled(false); | 386 VerifyPrintPreviewCancelled(false); |
364 VerifyPrintPreviewFailed(false); | 387 VerifyPrintPreviewFailed(false); |
365 VerifyPrintPreviewGenerated(true); | 388 VerifyPrintPreviewGenerated(true); |
366 VerifyPagesPrinted(false); | 389 VerifyPagesPrinted(false); |
367 } | 390 } |
368 | 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) { | |
Lei Zhang
2011/09/01 05:16:51
You should add a test case using kLongPageHTML. Te
kmadhusu
2011/09/01 20:21:16
This test is to verify that we generate only the c
| |
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 // Verify that we did not create the draft metafile for the first page. | |
418 VerifyDidPreviewPage(false, 0); | |
419 | |
420 VerifyPrintPreviewCancelled(false); | |
421 VerifyPrintPreviewFailed(false); | |
422 VerifyPrintPreviewGenerated(true); | |
423 VerifyPagesPrinted(false); | |
424 } | |
425 | |
369 // Tests that print preview fails and receiving error messages through | 426 // Tests that print preview fails and receiving error messages through |
370 // that channel all works. | 427 // that channel all works. |
371 TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreviewFail) { | 428 TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreviewFail) { |
372 LoadHTML(kHelloWorldHTML); | 429 LoadHTML(kHelloWorldHTML); |
373 | 430 |
374 PrintWebViewHelper::Get(view_)->OnInitiatePrintPreview(); | 431 PrintWebViewHelper::Get(view_)->OnInitiatePrintPreview(); |
375 // An empty dictionary should fail. | 432 // An empty dictionary should fail. |
376 DictionaryValue empty_dict; | 433 DictionaryValue empty_dict; |
377 PrintWebViewHelper::Get(view_)->OnPrintPreview(empty_dict); | 434 PrintWebViewHelper::Get(view_)->OnPrintPreview(empty_dict); |
378 | 435 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
422 LoadHTML(kPrintPreviewHTML); | 479 LoadHTML(kPrintPreviewHTML); |
423 | 480 |
424 // An empty dictionary should fail. | 481 // An empty dictionary should fail. |
425 DictionaryValue empty_dict; | 482 DictionaryValue empty_dict; |
426 PrintWebViewHelper::Get(view_)->OnPrintForPrintPreview(empty_dict); | 483 PrintWebViewHelper::Get(view_)->OnPrintForPrintPreview(empty_dict); |
427 | 484 |
428 VerifyPrintFailed(true); | 485 VerifyPrintFailed(true); |
429 VerifyPagesPrinted(false); | 486 VerifyPagesPrinted(false); |
430 } | 487 } |
431 #endif // !defined(OS_CHROMEOS) | 488 #endif // !defined(OS_CHROMEOS) |
OLD | NEW |