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/renderer/mock_printer.h" | 5 #include "chrome/renderer/mock_printer.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/shared_memory.h" | 9 #include "base/shared_memory.h" |
10 #include "base/string16.h" | 10 #include "base/string16.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "chrome/common/print_messages.h" | 12 #include "chrome/common/print_messages.h" |
13 #include "ipc/ipc_message_utils.h" | 13 #include "ipc/ipc_message_utils.h" |
14 #include "printing/metafile_impl.h" | 14 #include "printing/metafile_impl.h" |
15 #include "printing/units.h" | 15 #include "printing/units.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
| 18 namespace { |
| 19 |
| 20 void UpdateMargins(int margins_type, int dpi, PrintMsg_Print_Params* params) { |
| 21 if (margins_type == printing::NO_MARGINS) { |
| 22 params->content_size.SetSize(static_cast<int>((8.5 * dpi)), |
| 23 static_cast<int>((11.0 * dpi))); |
| 24 params->margin_left = 0; |
| 25 params->margin_top = 0; |
| 26 } else if (margins_type == printing::PRINTABLE_AREA_MARGINS) { |
| 27 params->content_size.SetSize(static_cast<int>((8.0 * dpi)), |
| 28 static_cast<int>((10.5 * dpi))); |
| 29 params->margin_left = static_cast<int>(0.25 * dpi); |
| 30 params->margin_top = static_cast<int>(0.25 * dpi); |
| 31 } else if (margins_type == printing::CUSTOM_MARGINS) { |
| 32 params->content_size.SetSize(static_cast<int>((7.9 * dpi)), |
| 33 static_cast<int>((10.4 * dpi))); |
| 34 params->margin_left = static_cast<int>(0.30 * dpi); |
| 35 params->margin_top = static_cast<int>(0.30 * dpi); |
| 36 } |
| 37 } |
| 38 |
| 39 } // end |
| 40 |
18 MockPrinterPage::MockPrinterPage(const void* source_data, | 41 MockPrinterPage::MockPrinterPage(const void* source_data, |
19 uint32 source_size, | 42 uint32 source_size, |
20 const printing::Image& image) | 43 const printing::Image& image) |
21 : source_size_(source_size), | 44 : source_size_(source_size), |
22 image_(image) { | 45 image_(image) { |
23 // Create copies of the source data | 46 // Create copies of the source data |
24 source_data_.reset(new uint8[source_size]); | 47 source_data_.reset(new uint8[source_size]); |
25 if (source_data_.get()) | 48 if (source_data_.get()) |
26 memcpy(source_data_.get(), source_data, source_size); | 49 memcpy(source_data_.get(), source_data, source_size); |
27 } | 50 } |
28 | 51 |
29 MockPrinterPage::~MockPrinterPage() {} | 52 MockPrinterPage::~MockPrinterPage() {} |
30 | 53 |
31 MockPrinter::MockPrinter() | 54 MockPrinter::MockPrinter() |
32 : dpi_(printing::kPointsPerInch), | 55 : dpi_(printing::kPointsPerInch), |
33 max_shrink_(2.0), | 56 max_shrink_(2.0), |
34 min_shrink_(1.25), | 57 min_shrink_(1.25), |
35 desired_dpi_(printing::kPointsPerInch), | 58 desired_dpi_(printing::kPointsPerInch), |
36 selection_only_(false), | 59 selection_only_(false), |
37 document_cookie_(-1), | 60 document_cookie_(-1), |
38 current_document_cookie_(0), | 61 current_document_cookie_(0), |
39 printer_status_(PRINTER_READY), | 62 printer_status_(PRINTER_READY), |
40 number_pages_(0), | 63 number_pages_(0), |
41 page_number_(0), | 64 page_number_(0), |
42 is_first_request_(true), | 65 is_first_request_(true), |
| 66 print_to_pdf_(false), |
43 preview_request_id_(0), | 67 preview_request_id_(0), |
44 display_header_footer_(false), | 68 display_header_footer_(false), |
45 date_(ASCIIToUTF16("date")), | 69 date_(ASCIIToUTF16("date")), |
46 title_(ASCIIToUTF16("title")), | 70 title_(ASCIIToUTF16("title")), |
47 url_(ASCIIToUTF16("url")), | 71 url_(ASCIIToUTF16("url")), |
48 use_invalid_settings_(false) { | 72 use_invalid_settings_(false) { |
49 page_size_.SetSize(static_cast<int>(8.5 * dpi_), | 73 page_size_.SetSize(static_cast<int>(8.5 * dpi_), |
50 static_cast<int>(11.0 * dpi_)); | 74 static_cast<int>(11.0 * dpi_)); |
51 content_size_.SetSize(static_cast<int>((7.5 * dpi_)), | 75 content_size_.SetSize(static_cast<int>((7.5 * dpi_)), |
52 static_cast<int>((10.0 * dpi_))); | 76 static_cast<int>((10.0 * dpi_))); |
53 margin_left_ = margin_top_ = static_cast<int>(0.5 * dpi_); | 77 margin_left_ = margin_top_ = static_cast<int>(0.5 * dpi_); |
| 78 printable_area_.SetRect(static_cast<int>(0.25 * dpi_), |
| 79 static_cast<int>(0.25 *dpi_), |
| 80 static_cast<int>(8 * dpi_), |
| 81 static_cast<int>(10.5 * dpi_)); |
54 } | 82 } |
55 | 83 |
56 MockPrinter::~MockPrinter() { | 84 MockPrinter::~MockPrinter() { |
57 } | 85 } |
58 | 86 |
59 void MockPrinter::ResetPrinter() { | 87 void MockPrinter::ResetPrinter() { |
60 printer_status_ = PRINTER_READY; | 88 printer_status_ = PRINTER_READY; |
61 document_cookie_ = -1; | 89 document_cookie_ = -1; |
62 } | 90 } |
63 | 91 |
64 void MockPrinter::GetDefaultPrintSettings(PrintMsg_Print_Params* params) { | 92 void MockPrinter::GetDefaultPrintSettings(PrintMsg_Print_Params* params) { |
65 // Verify this printer is not processing a job. | 93 // Verify this printer is not processing a job. |
66 // Sorry, this mock printer is very fragile. | 94 // Sorry, this mock printer is very fragile. |
67 EXPECT_EQ(-1, document_cookie_); | 95 EXPECT_EQ(-1, document_cookie_); |
68 | 96 |
69 // Assign a unit document cookie and set the print settings. | 97 // Assign a unit document cookie and set the print settings. |
70 document_cookie_ = CreateDocumentCookie(); | 98 document_cookie_ = CreateDocumentCookie(); |
71 params->Reset(); | 99 params->Reset(); |
72 SetPrintParams(params); | 100 SetPrintParams(params); |
73 } | 101 } |
74 | 102 |
75 void MockPrinter::SetDefaultPrintSettings(const PrintMsg_Print_Params& params) { | 103 void MockPrinter::SetDefaultPrintSettings(const PrintMsg_Print_Params& params) { |
76 dpi_ = params.dpi; | 104 dpi_ = params.dpi; |
77 max_shrink_ = params.max_shrink; | 105 max_shrink_ = params.max_shrink; |
78 min_shrink_ = params.min_shrink; | 106 min_shrink_ = params.min_shrink; |
79 desired_dpi_ = params.desired_dpi; | 107 desired_dpi_ = params.desired_dpi; |
80 selection_only_ = params.selection_only; | 108 selection_only_ = params.selection_only; |
81 page_size_ = params.page_size; | 109 page_size_ = params.page_size; |
82 content_size_ = params.content_size; | 110 content_size_ = params.content_size; |
| 111 printable_area_ = params.printable_area; |
83 margin_left_ = params.margin_left; | 112 margin_left_ = params.margin_left; |
84 margin_top_ = params.margin_top; | 113 margin_top_ = params.margin_top; |
85 display_header_footer_ = params.display_header_footer; | 114 display_header_footer_ = params.display_header_footer; |
86 date_ = params.date; | 115 date_ = params.date; |
87 title_ = params.title; | 116 title_ = params.title; |
88 url_ = params.url; | 117 url_ = params.url; |
89 } | 118 } |
90 | 119 |
91 void MockPrinter::UseInvalidSettings() { | 120 void MockPrinter::UseInvalidSettings() { |
92 use_invalid_settings_ = true; | 121 use_invalid_settings_ = true; |
(...skipping 11 matching lines...) Expand all Loading... |
104 settings->Reset(); | 133 settings->Reset(); |
105 | 134 |
106 settings->params.dpi = dpi_; | 135 settings->params.dpi = dpi_; |
107 settings->params.max_shrink = max_shrink_; | 136 settings->params.max_shrink = max_shrink_; |
108 settings->params.min_shrink = min_shrink_; | 137 settings->params.min_shrink = min_shrink_; |
109 settings->params.desired_dpi = desired_dpi_; | 138 settings->params.desired_dpi = desired_dpi_; |
110 settings->params.selection_only = selection_only_; | 139 settings->params.selection_only = selection_only_; |
111 settings->params.document_cookie = document_cookie_; | 140 settings->params.document_cookie = document_cookie_; |
112 settings->params.page_size = page_size_; | 141 settings->params.page_size = page_size_; |
113 settings->params.content_size = content_size_; | 142 settings->params.content_size = content_size_; |
| 143 settings->params.printable_area = printable_area_; |
114 settings->params.is_first_request = is_first_request_; | 144 settings->params.is_first_request = is_first_request_; |
| 145 settings->params.print_to_pdf = print_to_pdf_; |
115 settings->params.preview_request_id = preview_request_id_; | 146 settings->params.preview_request_id = preview_request_id_; |
116 settings->params.display_header_footer = display_header_footer_; | 147 settings->params.display_header_footer = display_header_footer_; |
117 settings->params.date = date_; | 148 settings->params.date = date_; |
118 settings->params.title = title_; | 149 settings->params.title = title_; |
119 settings->params.url = url_; | 150 settings->params.url = url_; |
120 printer_status_ = PRINTER_PRINTING; | 151 printer_status_ = PRINTER_PRINTING; |
121 } | 152 } |
122 | 153 |
123 void MockPrinter::UpdateSettings(int cookie, | 154 void MockPrinter::UpdateSettings(int cookie, |
124 PrintMsg_PrintPages_Params* params, | 155 PrintMsg_PrintPages_Params* params, |
125 const std::vector<int>& pages) { | 156 const std::vector<int>& pages, |
| 157 int margins_type) { |
126 if (document_cookie_ == -1) { | 158 if (document_cookie_ == -1) { |
127 document_cookie_ = CreateDocumentCookie(); | 159 document_cookie_ = CreateDocumentCookie(); |
128 } | 160 } |
129 params->Reset(); | 161 params->Reset(); |
130 params->pages = pages; | 162 params->pages = pages; |
131 SetPrintParams(&(params->params)); | 163 SetPrintParams(&(params->params)); |
| 164 UpdateMargins(margins_type, dpi_, &(params->params)); |
132 printer_status_ = PRINTER_PRINTING; | 165 printer_status_ = PRINTER_PRINTING; |
133 } | 166 } |
134 | 167 |
135 void MockPrinter::SetPrintedPagesCount(int cookie, int number_pages) { | 168 void MockPrinter::SetPrintedPagesCount(int cookie, int number_pages) { |
136 // Verify the input parameter and update the printer status so that the | 169 // Verify the input parameter and update the printer status so that the |
137 // RenderViewTest class can verify the this function finishes without errors. | 170 // RenderViewTest class can verify the this function finishes without errors. |
138 EXPECT_EQ(document_cookie_, cookie); | 171 EXPECT_EQ(document_cookie_, cookie); |
139 EXPECT_EQ(PRINTER_PRINTING, printer_status_); | 172 EXPECT_EQ(PRINTER_PRINTING, printer_status_); |
140 EXPECT_EQ(0, number_pages_); | 173 EXPECT_EQ(0, number_pages_); |
141 EXPECT_EQ(0, page_number_); | 174 EXPECT_EQ(0, page_number_); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 | 284 |
252 void MockPrinter::SetPrintParams(PrintMsg_Print_Params* params) { | 285 void MockPrinter::SetPrintParams(PrintMsg_Print_Params* params) { |
253 params->dpi = dpi_; | 286 params->dpi = dpi_; |
254 params->max_shrink = max_shrink_; | 287 params->max_shrink = max_shrink_; |
255 params->min_shrink = min_shrink_; | 288 params->min_shrink = min_shrink_; |
256 params->desired_dpi = desired_dpi_; | 289 params->desired_dpi = desired_dpi_; |
257 params->selection_only = selection_only_; | 290 params->selection_only = selection_only_; |
258 params->document_cookie = document_cookie_; | 291 params->document_cookie = document_cookie_; |
259 params->page_size = page_size_; | 292 params->page_size = page_size_; |
260 params->content_size = content_size_; | 293 params->content_size = content_size_; |
| 294 params->printable_area = printable_area_; |
261 params->margin_left = margin_left_; | 295 params->margin_left = margin_left_; |
262 params->margin_top = margin_top_; | 296 params->margin_top = margin_top_; |
263 params->is_first_request = is_first_request_; | 297 params->is_first_request = is_first_request_; |
| 298 params->print_to_pdf = print_to_pdf_; |
264 params->preview_request_id = preview_request_id_; | 299 params->preview_request_id = preview_request_id_; |
265 params->display_header_footer = display_header_footer_; | 300 params->display_header_footer = display_header_footer_; |
266 params->date = date_; | 301 params->date = date_; |
267 params->title = title_; | 302 params->title = title_; |
268 params->url = url_; | 303 params->url = url_; |
269 } | 304 } |
OLD | NEW |