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.
5 #include "components/printing/test/mock_printer.h"
7 #include "base/basictypes.h"
8 #include "base/files/file_util.h"
9 #include "base/memory/shared_memory.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "components/printing/common/print_messages.h"
13 #include "ipc/ipc_message_utils.h"
14 #include "printing/pdf_metafile_skia.h"
15 #include "printing/units.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 #if defined(OS_MACOSX)
19 #include "printing/pdf_metafile_cg_mac.h"
24 void UpdateMargins(int margins_type
, int dpi
, PrintMsg_Print_Params
* params
) {
25 if (margins_type
== printing::NO_MARGINS
) {
26 params
->content_size
.SetSize(static_cast<int>((8.5 * dpi
)),
27 static_cast<int>((11.0 * dpi
)));
28 params
->margin_left
= 0;
29 params
->margin_top
= 0;
30 } else if (margins_type
== printing::PRINTABLE_AREA_MARGINS
) {
31 params
->content_size
.SetSize(static_cast<int>((8.0 * dpi
)),
32 static_cast<int>((10.5 * dpi
)));
33 params
->margin_left
= static_cast<int>(0.25 * dpi
);
34 params
->margin_top
= static_cast<int>(0.25 * dpi
);
35 } else if (margins_type
== printing::CUSTOM_MARGINS
) {
36 params
->content_size
.SetSize(static_cast<int>((7.9 * dpi
)),
37 static_cast<int>((10.4 * dpi
)));
38 params
->margin_left
= static_cast<int>(0.30 * dpi
);
39 params
->margin_top
= static_cast<int>(0.30 * dpi
);
45 MockPrinterPage::MockPrinterPage(const void* source_data
,
47 const printing::Image
& image
)
48 : source_size_(source_size
), image_(image
) {
49 // Create copies of the source data
50 source_data_
.reset(new uint8
[source_size
]);
51 if (source_data_
.get())
52 memcpy(source_data_
.get(), source_data
, source_size
);
55 MockPrinterPage::~MockPrinterPage() {
58 MockPrinter::MockPrinter()
59 : dpi_(printing::kPointsPerInch
),
62 desired_dpi_(printing::kPointsPerInch
),
63 selection_only_(false),
64 should_print_backgrounds_(false),
66 current_document_cookie_(0),
67 printer_status_(PRINTER_READY
),
70 is_first_request_(true),
72 preview_request_id_(0),
73 print_scaling_option_(blink::WebPrintScalingOptionSourceSize
),
74 display_header_footer_(false),
75 title_(base::ASCIIToUTF16("title")),
76 url_(base::ASCIIToUTF16("url")),
77 use_invalid_settings_(false) {
78 page_size_
.SetSize(static_cast<int>(8.5 * dpi_
),
79 static_cast<int>(11.0 * dpi_
));
80 content_size_
.SetSize(static_cast<int>((7.5 * dpi_
)),
81 static_cast<int>((10.0 * dpi_
)));
82 margin_left_
= margin_top_
= static_cast<int>(0.5 * dpi_
);
83 printable_area_
.SetRect(
84 static_cast<int>(0.25 * dpi_
), static_cast<int>(0.25 * dpi_
),
85 static_cast<int>(8 * dpi_
), static_cast<int>(10.5 * dpi_
));
88 MockPrinter::~MockPrinter() {
91 void MockPrinter::ResetPrinter() {
92 printer_status_
= PRINTER_READY
;
93 document_cookie_
= -1;
96 void MockPrinter::GetDefaultPrintSettings(PrintMsg_Print_Params
* params
) {
97 // Verify this printer is not processing a job.
98 // Sorry, this mock printer is very fragile.
99 EXPECT_EQ(-1, document_cookie_
);
101 // Assign a unit document cookie and set the print settings.
102 document_cookie_
= CreateDocumentCookie();
104 SetPrintParams(params
);
107 void MockPrinter::SetDefaultPrintSettings(const PrintMsg_Print_Params
& params
) {
109 max_shrink_
= params
.max_shrink
;
110 min_shrink_
= params
.min_shrink
;
111 desired_dpi_
= params
.desired_dpi
;
112 selection_only_
= params
.selection_only
;
113 should_print_backgrounds_
= params
.should_print_backgrounds
;
114 page_size_
= params
.page_size
;
115 content_size_
= params
.content_size
;
116 printable_area_
= params
.printable_area
;
117 margin_left_
= params
.margin_left
;
118 margin_top_
= params
.margin_top
;
119 display_header_footer_
= params
.display_header_footer
;
120 title_
= params
.title
;
124 void MockPrinter::UseInvalidSettings() {
125 use_invalid_settings_
= true;
126 PrintMsg_Print_Params empty_param
;
127 SetDefaultPrintSettings(empty_param
);
130 void MockPrinter::UseInvalidPageSize() {
131 page_size_
.SetSize(0, 0);
134 void MockPrinter::UseInvalidContentSize() {
135 content_size_
.SetSize(0, 0);
138 void MockPrinter::ScriptedPrint(int cookie
,
139 int expected_pages_count
,
141 PrintMsg_PrintPages_Params
* settings
) {
142 // Verify the input parameters.
143 EXPECT_EQ(document_cookie_
, cookie
);
147 settings
->params
.dpi
= dpi_
;
148 settings
->params
.max_shrink
= max_shrink_
;
149 settings
->params
.min_shrink
= min_shrink_
;
150 settings
->params
.desired_dpi
= desired_dpi_
;
151 settings
->params
.selection_only
= selection_only_
;
152 settings
->params
.should_print_backgrounds
= should_print_backgrounds_
;
153 settings
->params
.document_cookie
= document_cookie_
;
154 settings
->params
.page_size
= page_size_
;
155 settings
->params
.content_size
= content_size_
;
156 settings
->params
.printable_area
= printable_area_
;
157 settings
->params
.is_first_request
= is_first_request_
;
158 settings
->params
.print_scaling_option
= print_scaling_option_
;
159 settings
->params
.print_to_pdf
= print_to_pdf_
;
160 settings
->params
.preview_request_id
= preview_request_id_
;
161 settings
->params
.display_header_footer
= display_header_footer_
;
162 settings
->params
.title
= title_
;
163 settings
->params
.url
= url_
;
164 printer_status_
= PRINTER_PRINTING
;
167 void MockPrinter::UpdateSettings(int cookie
,
168 PrintMsg_PrintPages_Params
* params
,
169 const std::vector
<int>& pages
,
171 if (document_cookie_
== -1) {
172 document_cookie_
= CreateDocumentCookie();
175 params
->pages
= pages
;
176 SetPrintParams(&(params
->params
));
177 UpdateMargins(margins_type
, dpi_
, &(params
->params
));
178 printer_status_
= PRINTER_PRINTING
;
181 void MockPrinter::SetPrintedPagesCount(int cookie
, int number_pages
) {
182 // Verify the input parameter and update the printer status so that the
183 // RenderViewTest class can verify the this function finishes without errors.
184 EXPECT_EQ(document_cookie_
, cookie
);
185 EXPECT_EQ(PRINTER_PRINTING
, printer_status_
);
186 EXPECT_EQ(0, number_pages_
);
187 EXPECT_EQ(0, page_number_
);
189 // Initialize the job status.
190 number_pages_
= number_pages
;
195 void MockPrinter::PrintPage(const PrintHostMsg_DidPrintPage_Params
& params
) {
196 // Verify the input parameter and update the printer status so that the
197 // RenderViewTest class can verify the this function finishes without errors.
198 EXPECT_EQ(PRINTER_PRINTING
, printer_status_
);
199 EXPECT_EQ(document_cookie_
, params
.document_cookie
);
200 EXPECT_EQ(page_number_
, params
.page_number
);
201 EXPECT_LE(params
.page_number
, number_pages_
);
203 #if defined(OS_WIN) || defined(OS_MACOSX)
204 // Load the data sent from a RenderView object and create a PageData object.
205 // We duplicate the given file handle when creating a base::SharedMemory
206 // instance so that its destructor closes the copy.
207 EXPECT_GT(params
.data_size
, 0U);
209 base::SharedMemory
metafile_data(params
.metafile_data_handle
, true,
210 GetCurrentProcess());
211 #elif defined(OS_MACOSX)
212 base::SharedMemory
metafile_data(params
.metafile_data_handle
, true);
214 metafile_data
.Map(params
.data_size
);
215 #if defined(OS_MACOSX)
216 printing::PdfMetafileCg metafile
;
218 printing::PdfMetafileSkia metafile
;
220 metafile
.InitFromData(metafile_data
.memory(), params
.data_size
);
221 printing::Image
image(metafile
);
222 MockPrinterPage
* page_data
=
223 new MockPrinterPage(metafile_data
.memory(), params
.data_size
, image
);
224 scoped_refptr
<MockPrinterPage
> page(page_data
);
225 pages_
.push_back(page
);
228 // We finish printing a printing job.
229 // Reset the job status and the printer status.
231 if (number_pages_
== page_number_
)
235 int MockPrinter::GetPrintedPages() const {
236 if (printer_status_
!= PRINTER_READY
)
241 const MockPrinterPage
* MockPrinter::GetPrintedPage(unsigned int pageno
) const {
242 if (pages_
.size() > pageno
)
243 return pages_
[pageno
].get();
248 int MockPrinter::GetWidth(unsigned int page
) const {
249 if (printer_status_
!= PRINTER_READY
|| page
>= pages_
.size())
251 return pages_
[page
]->width();
254 int MockPrinter::GetHeight(unsigned int page
) const {
255 if (printer_status_
!= PRINTER_READY
|| page
>= pages_
.size())
257 return pages_
[page
]->height();
260 bool MockPrinter::GetBitmapChecksum(unsigned int page
,
261 std::string
* checksum
) const {
262 if (printer_status_
!= PRINTER_READY
|| page
>= pages_
.size())
264 *checksum
= pages_
[page
]->image().checksum();
268 bool MockPrinter::SaveSource(unsigned int page
,
269 const base::FilePath
& filepath
) const {
270 if (printer_status_
!= PRINTER_READY
|| page
>= pages_
.size())
272 const uint8
* source_data
= pages_
[page
]->source_data();
273 uint32 source_size
= pages_
[page
]->source_size();
274 base::WriteFile(filepath
, reinterpret_cast<const char*>(source_data
),
279 bool MockPrinter::SaveBitmap(unsigned int page
,
280 const base::FilePath
& filepath
) const {
281 if (printer_status_
!= PRINTER_READY
|| page
>= pages_
.size())
284 pages_
[page
]->image().SaveToPng(filepath
);
288 int MockPrinter::CreateDocumentCookie() {
289 return use_invalid_settings_
? 0 : ++current_document_cookie_
;
292 void MockPrinter::SetPrintParams(PrintMsg_Print_Params
* params
) {
294 params
->max_shrink
= max_shrink_
;
295 params
->min_shrink
= min_shrink_
;
296 params
->desired_dpi
= desired_dpi_
;
297 params
->selection_only
= selection_only_
;
298 params
->should_print_backgrounds
= should_print_backgrounds_
;
299 params
->document_cookie
= document_cookie_
;
300 params
->page_size
= page_size_
;
301 params
->content_size
= content_size_
;
302 params
->printable_area
= printable_area_
;
303 params
->margin_left
= margin_left_
;
304 params
->margin_top
= margin_top_
;
305 params
->is_first_request
= is_first_request_
;
306 params
->print_scaling_option
= print_scaling_option_
;
307 params
->print_to_pdf
= print_to_pdf_
;
308 params
->preview_request_id
= preview_request_id_
;
309 params
->display_header_footer
= display_header_footer_
;
310 params
->title
= title_
;