1// Copyright 2014 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
5#ifndef CHROME_RENDERER_PRINTING_MOCK_PRINTER_H_
6#define CHROME_RENDERER_PRINTING_MOCK_PRINTER_H_
7
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/strings/string16.h"
15#include "printing/image.h"
16#include "third_party/WebKit/public/web/WebPrintScalingOption.h"
17#include "ui/gfx/rect.h"
18#include "ui/gfx/size.h"
19
20struct PrintMsg_Print_Params;
21struct PrintMsg_PrintPages_Params;
22struct PrintHostMsg_DidPrintPage_Params;
23
24// A class which represents an output page used in the MockPrinter class.
25// The MockPrinter class stores output pages in a vector, so, this class
26// inherits the base::RefCounted<> class so that the MockPrinter class can use
27// a smart pointer of this object (i.e. scoped_refptr<>).
28class MockPrinterPage : public base::RefCounted<MockPrinterPage> {
29 public:
30  MockPrinterPage(const void* source_data,
31                  uint32 source_size,
32                  const printing::Image& image);
33
34  int width() const { return image_.size().width(); }
35  int height() const { return image_.size().height(); }
36  const uint8* source_data() const { return source_data_.get(); }
37  uint32 source_size() const { return source_size_; }
38  const printing::Image& image() const { return image_; }
39
40 private:
41  friend class base::RefCounted<MockPrinterPage>;
42  virtual ~MockPrinterPage();
43
44  uint32 source_size_;
45  scoped_ptr<uint8[]> source_data_;
46  printing::Image image_;
47
48  DISALLOW_COPY_AND_ASSIGN(MockPrinterPage);
49};
50
51// A class which implements a pseudo-printer object used by the RenderViewTest
52// class.
53// This class consists of three parts:
54// 1. An IPC-message hanlder sent from the RenderView class;
55// 2. A renderer that creates a printing job into bitmaps, and;
56// 3. A vector which saves the output pages of a printing job.
57// A user who writes RenderViewTest cases only use the functions which
58// retrieve output pages from this vector to verify them with expected results.
59class MockPrinter {
60 public:
61  enum Status {
62    PRINTER_READY,
63    PRINTER_PRINTING,
64    PRINTER_ERROR,
65  };
66
67  MockPrinter();
68  ~MockPrinter();
69
70  // Functions that changes settings of a pseudo printer.
71  void ResetPrinter();
72  void SetDefaultPrintSettings(const PrintMsg_Print_Params& params);
73  void UseInvalidSettings();
74  void UseInvalidPageSize();
75  void UseInvalidContentSize();
76
77  // Functions that handles IPC events.
78  void GetDefaultPrintSettings(PrintMsg_Print_Params* params);
79  void ScriptedPrint(int cookie,
80                     int expected_pages_count,
81                     bool has_selection,
82                     PrintMsg_PrintPages_Params* settings);
83  void UpdateSettings(int cookie, PrintMsg_PrintPages_Params* params,
84                      const std::vector<int>& page_range_array,
85                      int margins_type);
86  void SetPrintedPagesCount(int cookie, int number_pages);
87  void PrintPage(const PrintHostMsg_DidPrintPage_Params& params);
88
89  // Functions that retrieve the output pages.
90  Status GetPrinterStatus() const { return printer_status_; }
91  int GetPrintedPages() const;
92
93  // Get a pointer to the printed page, returns NULL if pageno has not been
94  // printed.  The pointer is for read only view and should not be deleted.
95  const MockPrinterPage* GetPrintedPage(unsigned int pageno) const;
96
97  int GetWidth(unsigned int page) const;
98  int GetHeight(unsigned int page) const;
99  bool GetBitmapChecksum(unsigned int page, std::string* checksum) const;
100  bool GetSource(unsigned int page, const void** data, uint32* size) const;
101  bool GetBitmap(unsigned int page, const void** data, uint32* size) const;
102  bool SaveSource(unsigned int page, const base::FilePath& filepath) const;
103  bool SaveBitmap(unsigned int page, const base::FilePath& filepath) const;
104
105 protected:
106  int CreateDocumentCookie();
107
108 private:
109  // Helper function to fill the fields in |params|.
110  void SetPrintParams(PrintMsg_Print_Params* params);
111
112  // In pixels according to dpi_x and dpi_y.
113  gfx::Size page_size_;
114  gfx::Size content_size_;
115  int margin_left_;
116  int margin_top_;
117  gfx::Rect printable_area_;
118
119  // Specifies dots per inch.
120  double dpi_;
121  double max_shrink_;
122  double min_shrink_;
123
124  // Desired apparent dpi on paper.
125  int desired_dpi_;
126
127  // Print selection.
128  bool selection_only_;
129
130  // Print css backgrounds.
131  bool should_print_backgrounds_;
132
133  // Cookie for the document to ensure correctness.
134  int document_cookie_;
135  int current_document_cookie_;
136
137  // The current status of this printer.
138  Status printer_status_;
139
140  // The output of a printing job.
141  int number_pages_;
142  int page_number_;
143
144  // Used only in the preview sequence.
145  bool is_first_request_;
146  bool print_to_pdf_;
147  int preview_request_id_;
148
149  // Specifies whether to retain/crop/scale source page size to fit the
150  // given printable area.
151  blink::WebPrintScalingOption print_scaling_option_;
152
153  // Used for displaying headers and footers.
154  bool display_header_footer_;
155  base::string16 title_;
156  base::string16 url_;
157
158  // Used for generating invalid settings.
159  bool use_invalid_settings_;
160
161  std::vector<scoped_refptr<MockPrinterPage> > pages_;
162
163  DISALLOW_COPY_AND_ASSIGN(MockPrinter);
164};
165
166#endif  // CHROME_RENDERER_PRINTING_MOCK_PRINTER_H_
167