File: mock_printer.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (143 lines) | stat: -rw-r--r-- 4,629 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_PRINTING_TEST_MOCK_PRINTER_H_
#define COMPONENTS_PRINTING_TEST_MOCK_PRINTER_H_

#include <stdint.h>

#include <memory>
#include <string>
#include <vector>

#include "base/memory/ref_counted.h"
#include "components/printing/common/print.mojom.h"
#include "pdf/buildflags.h"
#include "printing/image.h"
#include "printing/mojom/print.mojom.h"
#include "printing/units.h"

#if BUILDFLAG(ENABLE_PDF)
#define MOCK_PRINTER_SUPPORTS_PAGE_IMAGES
#endif

namespace base {
class ReadOnlySharedMemoryMapping;
}  // namespace base

namespace printing {

// A class which represents an output page used in the MockPrinter class.
// The MockPrinter class stores output pages in a vector, so, this class
// inherits the base::RefCounted<> class so that the MockPrinter class can use
// a smart pointer of this object (i.e. scoped_refptr<>).
class MockPrinterPage : public base::RefCounted<MockPrinterPage> {
 public:
  explicit MockPrinterPage(const Image& image);
  MockPrinterPage(const MockPrinterPage&) = delete;
  MockPrinterPage& operator=(const MockPrinterPage&) = delete;

  int width() const { return image_.size().width(); }
  int height() const { return image_.size().height(); }
  const Image& image() const { return image_; }

 private:
  friend class base::RefCounted<MockPrinterPage>;
  virtual ~MockPrinterPage();

  Image image_;
};

// A class which implements a pseudo-printer object used by the RenderViewTest
// class.
// This class consists of three parts:
// 1. An IPC-message hanlder sent from the RenderView class;
// 2. A renderer that creates a printing job into bitmaps, and;
// 3. A vector which saves the output pages of a printing job.
// A user who writes RenderViewTest cases only use the functions which
// retrieve output pages from this vector to verify them with expected results.
class MockPrinter {
 public:
  enum Status {
    PRINTER_READY,
    PRINTER_PRINTING,
    PRINTER_ERROR,
  };

  MockPrinter();
  MockPrinter(const MockPrinter&) = delete;
  MockPrinter& operator=(const MockPrinter&) = delete;
  ~MockPrinter();

#if defined(MOCK_PRINTER_SUPPORTS_PAGE_IMAGES)
  void set_should_generate_page_images(bool val) {
    should_generate_page_images_ = val;
  }
#endif  // MOCK_PRINTER_SUPPORTS_PAGE_IMAGES

  // Reset the printer, to prepare for another print job.
  void Reset();

  mojom::PrintParams& Params() { return params_; }

  // Functions that handle mojo messages.
  mojom::PrintParamsPtr GetDefaultPrintSettings();
  void SetPrintedPagesCount(int cookie, uint32_t number_pages);

  // Functions that handles IPC events.
  void ScriptedPrint(int cookie,
                     uint32_t expected_pages_count,
                     bool has_selection,
                     mojom::PrintPagesParams* settings);
  void OnDocumentPrinted(mojom::DidPrintDocumentParamsPtr params);

  // Functions that retrieve the output pages.
  Status GetPrinterStatus() const { return printer_status_; }
  int GetPageCount() const;

#if defined(MOCK_PRINTER_SUPPORTS_PAGE_IMAGES)
  // Generate MockPrinterPage objects from the printed metafile.
  void GeneratePageImages(const base::ReadOnlySharedMemoryMapping& mapping);

  // Get a pointer to the printed page, returns NULL if pageno has not been
  // printed.  The pointer is for read only view and should not be deleted.
  const MockPrinterPage* GetPrinterPage(unsigned int pageno) const;

  int GetWidth(unsigned int page) const;
  int GetHeight(unsigned int page) const;
#endif  // MOCK_PRINTER_SUPPORTS_PAGE_IMAGES

 private:
  // Sets `document_cookie_` based on `use_invalid_settings_`.
  void CreateDocumentCookie();

  // Set the printer in a finished state after printing.
  void Finish();

  mojom::PrintParams params_;

  bool document_cookie_set_ = false;

  // The current status of this printer.
  Status printer_status_ = PRINTER_READY;

  // The number of pages printed.
  int page_count_ = 0;

  // Used for generating invalid settings.
  bool use_invalid_settings_ = false;

#if defined(MOCK_PRINTER_SUPPORTS_PAGE_IMAGES)
  // If true, one MockPrinterPage object (including an Image) will be generated
  // for each page, so that tests that want to look at pixels can do that. This
  // operation is surprisingly expensive, so it's false by default.
  bool should_generate_page_images_ = false;
#endif  // MOCK_PRINTER_SUPPORTS_PAGE_IMAGES

  std::vector<scoped_refptr<MockPrinterPage>> pages_;
};

}  // namespace printing

#endif  // COMPONENTS_PRINTING_TEST_MOCK_PRINTER_H_