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 11 matching lines...) Expand all Loading... | |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 // A simple web page. | 25 // A simple web page. |
26 const char kHelloWorldHTML[] = "<body><p>Hello World!</p></body>"; | 26 const char kHelloWorldHTML[] = "<body><p>Hello World!</p></body>"; |
27 | 27 |
28 // A simple webpage that prints itself. | 28 // A simple webpage that prints itself. |
29 const char kPrintWithJSHTML[] = | 29 const char kPrintWithJSHTML[] = |
30 "<body>Hello<script>window.print()</script>World</body>"; | 30 "<body>Hello<script>window.print()</script>World</body>"; |
31 | 31 |
32 // A longer web page. | |
33 const char kLongPageHTML[] = | |
34 "<body><img src=\"\" width=10 height=10000 /></body>"; | |
35 | |
32 // A web page to simulate the print preview page. | 36 // A web page to simulate the print preview page. |
33 const char kPrintPreviewHTML[] = | 37 const char kPrintPreviewHTML[] = |
34 "<body><p id=\"pdf-viewer\">Hello World!</p></body>"; | 38 "<body><p id=\"pdf-viewer\">Hello World!</p></body>"; |
35 | 39 |
36 void CreatePrintSettingsDictionary(DictionaryValue* dict) { | 40 void CreatePrintSettingsDictionary(DictionaryValue* dict) { |
37 dict->SetBoolean(printing::kSettingLandscape, false); | 41 dict->SetBoolean(printing::kSettingLandscape, false); |
38 dict->SetBoolean(printing::kSettingCollate, false); | 42 dict->SetBoolean(printing::kSettingCollate, false); |
39 dict->SetBoolean(printing::kSettingColor, false); | 43 dict->SetBoolean(printing::kSettingColor, false); |
40 dict->SetBoolean(printing::kSettingPrintToPDF, true); | 44 dict->SetBoolean(printing::kSettingPrintToPDF, true); |
41 dict->SetInteger(printing::kSettingDuplexMode, printing::SIMPLEX); | 45 dict->SetInteger(printing::kSettingDuplexMode, printing::SIMPLEX); |
42 dict->SetInteger(printing::kSettingCopies, 1); | 46 dict->SetInteger(printing::kSettingCopies, 1); |
43 dict->SetString(printing::kSettingDeviceName, "dummy"); | 47 dict->SetString(printing::kSettingDeviceName, "dummy"); |
48 dict->SetString(printing::kPreviewUIAddr, "0xb33fbeef"); | |
44 dict->SetInteger(printing::kPreviewRequestID, 12345); | 49 dict->SetInteger(printing::kPreviewRequestID, 12345); |
45 dict->SetBoolean(printing::kIsFirstRequest, true); | 50 dict->SetBoolean(printing::kIsFirstRequest, true); |
46 dict->SetBoolean(printing::kSettingHeaderFooterEnabled, false); | 51 dict->SetBoolean(printing::kSettingHeaderFooterEnabled, false); |
47 } | 52 } |
48 | 53 |
49 } // namespace | 54 } // namespace |
50 | 55 |
51 class PrintWebViewHelperTestBase : public RenderViewTest { | 56 class PrintWebViewHelperTestBase : public RenderViewTest { |
52 public: | 57 public: |
53 PrintWebViewHelperTestBase() {} | 58 PrintWebViewHelperTestBase() {} |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
298 | 303 |
299 virtual void SetUp() { | 304 virtual void SetUp() { |
300 // Append the print preview switch before creating the PrintWebViewHelper. | 305 // Append the print preview switch before creating the PrintWebViewHelper. |
301 CommandLine::ForCurrentProcess()->AppendSwitch( | 306 CommandLine::ForCurrentProcess()->AppendSwitch( |
302 switches::kEnablePrintPreview); | 307 switches::kEnablePrintPreview); |
303 | 308 |
304 RenderViewTest::SetUp(); | 309 RenderViewTest::SetUp(); |
305 } | 310 } |
306 | 311 |
307 protected: | 312 protected: |
313 void VerifyPrintPreviewCancelled(bool did_cancel) { | |
314 bool print_preview_cancelled = | |
315 (render_thread_.sink().GetUniqueMessageMatching( | |
316 PrintHostMsg_PrintPreviewCancelled::ID) != NULL); | |
317 EXPECT_EQ(did_cancel, print_preview_cancelled); | |
318 } | |
319 | |
308 void VerifyPrintPreviewFailed(bool did_fail) { | 320 void VerifyPrintPreviewFailed(bool did_fail) { |
309 bool print_preview_failed = (render_thread_.sink().GetUniqueMessageMatching( | 321 bool print_preview_failed = (render_thread_.sink().GetUniqueMessageMatching( |
310 PrintHostMsg_PrintPreviewFailed::ID) != NULL); | 322 PrintHostMsg_PrintPreviewFailed::ID) != NULL); |
311 EXPECT_EQ(did_fail, print_preview_failed); | 323 EXPECT_EQ(did_fail, print_preview_failed); |
312 } | 324 } |
313 | 325 |
314 void VerifyPrintPreviewGenerated(bool generated_preview) { | 326 void VerifyPrintPreviewGenerated(bool generated_preview) { |
315 const IPC::Message* preview_msg = | 327 const IPC::Message* preview_msg = |
316 render_thread_.sink().GetUniqueMessageMatching( | 328 render_thread_.sink().GetUniqueMessageMatching( |
317 PrintHostMsg_PagesReadyForPreview::ID); | 329 PrintHostMsg_PagesReadyForPreview::ID); |
(...skipping 21 matching lines...) Expand all Loading... | |
339 // that channel all works. | 351 // that channel all works. |
340 TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreview) { | 352 TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreview) { |
341 LoadHTML(kHelloWorldHTML); | 353 LoadHTML(kHelloWorldHTML); |
342 | 354 |
343 PrintWebViewHelper::Get(view_)->OnInitiatePrintPreview(); | 355 PrintWebViewHelper::Get(view_)->OnInitiatePrintPreview(); |
344 // Fill in some dummy values. | 356 // Fill in some dummy values. |
345 DictionaryValue dict; | 357 DictionaryValue dict; |
346 CreatePrintSettingsDictionary(&dict); | 358 CreatePrintSettingsDictionary(&dict); |
347 PrintWebViewHelper::Get(view_)->OnPrintPreview(dict); | 359 PrintWebViewHelper::Get(view_)->OnPrintPreview(dict); |
348 | 360 |
349 // Need to finish simulating print preview. | |
350 // Generate the page and finalize it. | |
351 PrintWebViewHelper::Get(view_)->OnContinuePreview( | |
352 printing::INVALID_PAGE_INDEX); | |
353 PrintWebViewHelper::Get(view_)->OnContinuePreview( | |
354 printing::INVALID_PAGE_INDEX); | |
355 | |
356 EXPECT_EQ(0, render_thread_.print_preview_pages_remaining()); | 361 EXPECT_EQ(0, render_thread_.print_preview_pages_remaining()); |
362 VerifyPrintPreviewCancelled(false); | |
357 VerifyPrintPreviewFailed(false); | 363 VerifyPrintPreviewFailed(false); |
358 VerifyPrintPreviewGenerated(true); | 364 VerifyPrintPreviewGenerated(true); |
359 VerifyPagesPrinted(false); | 365 VerifyPagesPrinted(false); |
360 } | 366 } |
361 | 367 |
362 // Tests that print preview fails and receiving error messages through | 368 // Tests that print preview fails and receiving error messages through |
363 // that channel all works. | 369 // that channel all works. |
364 TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreviewFail) { | 370 TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreviewFail) { |
365 LoadHTML(kHelloWorldHTML); | 371 LoadHTML(kHelloWorldHTML); |
366 | 372 |
367 PrintWebViewHelper::Get(view_)->OnInitiatePrintPreview(); | 373 PrintWebViewHelper::Get(view_)->OnInitiatePrintPreview(); |
368 // An empty dictionary should fail. | 374 // An empty dictionary should fail. |
369 DictionaryValue empty_dict; | 375 DictionaryValue empty_dict; |
370 PrintWebViewHelper::Get(view_)->OnPrintPreview(empty_dict); | 376 PrintWebViewHelper::Get(view_)->OnPrintPreview(empty_dict); |
371 | 377 |
372 EXPECT_EQ(0, render_thread_.print_preview_pages_remaining()); | 378 EXPECT_EQ(0, render_thread_.print_preview_pages_remaining()); |
379 VerifyPrintPreviewCancelled(false); | |
373 VerifyPrintPreviewFailed(true); | 380 VerifyPrintPreviewFailed(true); |
374 VerifyPrintPreviewGenerated(false); | 381 VerifyPrintPreviewGenerated(false); |
375 VerifyPagesPrinted(false); | 382 VerifyPagesPrinted(false); |
376 } | 383 } |
377 | 384 |
385 // Tests that print preview work and sending and receiving messages through | |
kmadhusu
2011/08/22 22:51:20
nit: Update comment.
Lei Zhang
2011/08/22 22:56:22
Done.
| |
386 // that channel all works. | |
387 TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreviewCancel) { | |
388 LoadHTML(kLongPageHTML); | |
389 | |
390 const int kCancelPage = 3; | |
391 render_thread_.set_print_preview_cancel_page_number(kCancelPage); | |
392 PrintWebViewHelper::Get(view_)->OnInitiatePrintPreview(); | |
393 // Fill in some dummy values. | |
394 DictionaryValue dict; | |
395 CreatePrintSettingsDictionary(&dict); | |
396 PrintWebViewHelper::Get(view_)->OnPrintPreview(dict); | |
397 | |
398 EXPECT_EQ(kCancelPage, render_thread_.print_preview_pages_remaining()); | |
399 VerifyPrintPreviewCancelled(true); | |
400 VerifyPrintPreviewFailed(false); | |
401 VerifyPrintPreviewGenerated(false); | |
402 VerifyPagesPrinted(false); | |
403 } | |
404 | |
378 // Tests that printing from print preview works and sending and receiving | 405 // Tests that printing from print preview works and sending and receiving |
379 // messages through that channel all works. | 406 // messages through that channel all works. |
380 TEST_F(PrintWebViewHelperPreviewTest, OnPrintForPrintPreview) { | 407 TEST_F(PrintWebViewHelperPreviewTest, OnPrintForPrintPreview) { |
381 LoadHTML(kPrintPreviewHTML); | 408 LoadHTML(kPrintPreviewHTML); |
382 | 409 |
383 // Fill in some dummy values. | 410 // Fill in some dummy values. |
384 DictionaryValue dict; | 411 DictionaryValue dict; |
385 CreatePrintSettingsDictionary(&dict); | 412 CreatePrintSettingsDictionary(&dict); |
386 PrintWebViewHelper::Get(view_)->OnPrintForPrintPreview(dict); | 413 PrintWebViewHelper::Get(view_)->OnPrintForPrintPreview(dict); |
387 | 414 |
388 VerifyPrintFailed(false); | 415 VerifyPrintFailed(false); |
389 VerifyPagesPrinted(true); | 416 VerifyPagesPrinted(true); |
390 } | 417 } |
391 | 418 |
392 // Tests that printing from print preview fails and receiving error messages | 419 // Tests that printing from print preview fails and receiving error messages |
393 // through that channel all works. | 420 // through that channel all works. |
394 TEST_F(PrintWebViewHelperPreviewTest, OnPrintForPrintPreviewFail) { | 421 TEST_F(PrintWebViewHelperPreviewTest, OnPrintForPrintPreviewFail) { |
395 LoadHTML(kPrintPreviewHTML); | 422 LoadHTML(kPrintPreviewHTML); |
396 | 423 |
397 // An empty dictionary should fail. | 424 // An empty dictionary should fail. |
398 DictionaryValue empty_dict; | 425 DictionaryValue empty_dict; |
399 PrintWebViewHelper::Get(view_)->OnPrintForPrintPreview(empty_dict); | 426 PrintWebViewHelper::Get(view_)->OnPrintForPrintPreview(empty_dict); |
400 | 427 |
401 VerifyPrintFailed(true); | 428 VerifyPrintFailed(true); |
402 VerifyPagesPrinted(false); | 429 VerifyPagesPrinted(false); |
403 } | 430 } |
404 #endif // !defined(OS_CHROMEOS) | 431 #endif // !defined(OS_CHROMEOS) |
OLD | NEW |