test_printing_context.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2021 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. #ifndef PRINTING_TEST_PRINTING_CONTEXT_H_
  5. #define PRINTING_TEST_PRINTING_CONTEXT_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/containers/flat_map.h"
  9. #include "build/build_config.h"
  10. #include "printing/mojom/print.mojom.h"
  11. #include "printing/print_settings.h"
  12. #include "printing/printing_context.h"
  13. namespace printing {
  14. class TestPrintingContextDelegate : public PrintingContext::Delegate {
  15. public:
  16. TestPrintingContextDelegate();
  17. TestPrintingContextDelegate(const TestPrintingContextDelegate&) = delete;
  18. TestPrintingContextDelegate& operator=(const TestPrintingContextDelegate&) =
  19. delete;
  20. ~TestPrintingContextDelegate() override;
  21. // PrintingContext::Delegate overrides:
  22. gfx::NativeView GetParentView() override;
  23. std::string GetAppLocale() override;
  24. };
  25. class TestPrintingContext : public PrintingContext {
  26. public:
  27. TestPrintingContext(Delegate* delegate, bool skip_system_calls);
  28. TestPrintingContext(const TestPrintingContext&) = delete;
  29. TestPrintingContext& operator=(const TestPrintingContext&) = delete;
  30. ~TestPrintingContext() override;
  31. // Methods for test setup:
  32. // Provide settings that will be used as the current settings for the
  33. // indicated device.
  34. void SetDeviceSettings(const std::string& device_name,
  35. std::unique_ptr<PrintSettings> settings);
  36. // Enables tests to fail with an access-denied error.
  37. void SetNewDocumentBlockedByPermissions() {
  38. new_document_blocked_by_permissions_ = true;
  39. }
  40. #if BUILDFLAG(IS_WIN)
  41. void SetOnRenderPageBlockedByPermissions() {
  42. render_page_blocked_by_permissions_ = true;
  43. }
  44. #endif
  45. void SetOnRenderDocumentBlockedByPermissions() {
  46. render_document_blocked_by_permissions_ = true;
  47. }
  48. void SetDocumentDoneBlockedByPermissions() {
  49. document_done_blocked_by_permissions_ = true;
  50. }
  51. // Enables tests to fail with a failed error.
  52. void SetUseDefaultSettingsFails() { use_default_settings_fails_ = true; }
  53. // Enables tests to fail with a canceled error.
  54. void SetAskUserForSettingsCanceled() { ask_user_for_settings_cancel_ = true; }
  55. // PrintingContext overrides:
  56. void AskUserForSettings(int max_pages,
  57. bool has_selection,
  58. bool is_scripted,
  59. PrintSettingsCallback callback) override;
  60. mojom::ResultCode UseDefaultSettings() override;
  61. gfx::Size GetPdfPaperSizeDeviceUnits() override;
  62. mojom::ResultCode UpdatePrinterSettings(
  63. const PrinterSettings& printer_settings) override;
  64. mojom::ResultCode NewDocument(const std::u16string& document_name) override;
  65. #if BUILDFLAG(IS_WIN)
  66. mojom::ResultCode RenderPage(const PrintedPage& page,
  67. const PageSetup& page_setup) override;
  68. #endif
  69. mojom::ResultCode PrintDocument(const MetafilePlayer& metafile,
  70. const PrintSettings& settings,
  71. uint32_t num_pages) override;
  72. mojom::ResultCode DocumentDone() override;
  73. void Cancel() override;
  74. void ReleaseContext() override;
  75. NativeDrawingContext context() const override;
  76. #if BUILDFLAG(IS_WIN)
  77. mojom::ResultCode InitWithSettingsForTest(
  78. std::unique_ptr<PrintSettings> settings) override;
  79. #endif
  80. private:
  81. base::flat_map<std::string, std::unique_ptr<PrintSettings>> device_settings_;
  82. bool use_default_settings_fails_ = false;
  83. bool ask_user_for_settings_cancel_ = false;
  84. bool new_document_blocked_by_permissions_ = false;
  85. #if BUILDFLAG(IS_WIN)
  86. bool render_page_blocked_by_permissions_ = false;
  87. #endif
  88. bool render_document_blocked_by_permissions_ = false;
  89. bool document_done_blocked_by_permissions_ = false;
  90. };
  91. } // namespace printing
  92. #endif // PRINTING_TEST_PRINTING_CONTEXT_H_