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" | |
8 #include "base/file_util.h" | 7 #include "base/file_util.h" |
9 #include "base/shared_memory.h" | 8 #include "base/shared_memory.h" |
10 #include "base/string16.h" | |
11 #include "base/utf_string_conversions.h" | |
12 #include "chrome/common/print_messages.h" | 9 #include "chrome/common/print_messages.h" |
13 #include "ipc/ipc_message_utils.h" | 10 #include "ipc/ipc_message_utils.h" |
14 #include "printing/metafile_impl.h" | 11 #include "printing/metafile_impl.h" |
15 #include "printing/units.h" | 12 #include "printing/units.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
17 | 14 |
18 PrintMsg_Print_Params_Clone::PrintMsg_Print_Params_Clone() | |
19 : page_size_(), | |
20 printable_size_(), | |
21 margin_top_(0), | |
22 margin_left_(0), | |
23 dpi_(0), | |
24 min_shrink_(0), | |
25 max_shrink_(0), | |
26 desired_dpi_(0), | |
27 document_cookie_(0), | |
28 selection_only_(0), | |
29 supports_alpha_blend_(0), | |
30 preview_request_id_(0), | |
31 is_first_request_(0), | |
32 display_header_footer_(0), | |
33 date_(), | |
34 title_(), | |
35 url_() { | |
36 } | |
37 | |
38 PrintMsg_Print_Params_Clone::~PrintMsg_Print_Params_Clone(){} | |
39 | |
40 void PrintMsg_Print_Params_Clone::ResetParams(PrintMsg_Print_Params* params) { | |
41 params->dpi = dpi_; | |
42 params->max_shrink = max_shrink_; | |
43 params->min_shrink = min_shrink_; | |
44 params->desired_dpi = desired_dpi_; | |
45 params->selection_only = selection_only_; | |
46 params->document_cookie = document_cookie_; | |
47 params->page_size = page_size_; | |
48 params->printable_size = printable_size_; | |
49 params->margin_left = margin_left_; | |
50 params->margin_top = margin_top_; | |
51 params->is_first_request = is_first_request_; | |
52 params->preview_request_id = preview_request_id_; | |
53 params->display_header_footer = display_header_footer_; | |
54 params->date = date_; | |
55 params->title = title_; | |
56 params->url = url_; | |
57 | |
58 COMPILE_ASSERT(sizeof(PrintMsg_Print_Params_Clone) == | |
59 sizeof(PrintMsg_Print_Params), | |
60 PrintMsg_Print_Params); | |
61 } | |
62 | |
63 PrintMsg_PrintPages_Params_Clone::PrintMsg_PrintPages_Params_Clone() | |
64 : pages_(0) { | |
65 } | |
66 | |
67 PrintMsg_PrintPages_Params_Clone::~PrintMsg_PrintPages_Params_Clone(){} | |
68 | |
69 void PrintMsg_PrintPages_Params_Clone::ResetParams( | |
70 PrintMsg_PrintPages_Params* params) { | |
71 params_.ResetParams(¶ms->params); | |
72 params->pages = pages_; | |
73 | |
74 COMPILE_ASSERT(sizeof(PrintMsg_PrintPages_Params_Clone) == | |
75 sizeof(PrintMsg_PrintPages_Params_Clone), | |
76 PrintMsg_PrintPages_Params); | |
77 } | |
78 | |
79 MockPrinterPage::MockPrinterPage(const void* source_data, | 15 MockPrinterPage::MockPrinterPage(const void* source_data, |
80 uint32 source_size, | 16 uint32 source_size, |
81 const printing::Image& image) | 17 const printing::Image& image) |
82 : source_size_(source_size), | 18 : source_size_(source_size), |
83 image_(image) { | 19 image_(image) { |
84 // Create copies of the source data | 20 // Create copies of the source data |
85 source_data_.reset(new uint8[source_size]); | 21 source_data_.reset(new uint8[source_size]); |
86 if (source_data_.get()) | 22 if (source_data_.get()) |
87 memcpy(source_data_.get(), source_data, source_size); | 23 memcpy(source_data_.get(), source_data, source_size); |
88 } | 24 } |
89 | 25 |
90 MockPrinterPage::~MockPrinterPage() {} | 26 MockPrinterPage::~MockPrinterPage() {} |
91 | 27 |
92 MockPrinter::MockPrinter() | 28 MockPrinter::MockPrinter() |
93 : dpi_(printing::kPointsPerInch), | 29 : dpi_(printing::kPointsPerInch), |
94 max_shrink_(2.0), | 30 max_shrink_(2.0), |
95 min_shrink_(1.25), | 31 min_shrink_(1.25), |
96 desired_dpi_(printing::kPointsPerInch), | 32 desired_dpi_(printing::kPointsPerInch), |
97 selection_only_(false), | 33 selection_only_(false), |
98 document_cookie_(-1), | 34 document_cookie_(-1), |
99 current_document_cookie_(0), | 35 current_document_cookie_(0), |
100 printer_status_(PRINTER_READY), | 36 printer_status_(PRINTER_READY), |
101 number_pages_(0), | 37 number_pages_(0), |
102 page_number_(0), | 38 page_number_(0), |
103 is_first_request_(true), | 39 is_first_request_(true), |
104 preview_request_id_(0), | 40 preview_request_id_(0) { |
105 display_header_footer_(false), | |
106 date_(ASCIIToUTF16("date")), | |
107 title_(ASCIIToUTF16("title")), | |
108 url_(ASCIIToUTF16("url")) { | |
109 page_size_.SetSize(static_cast<int>(8.5 * dpi_), | 41 page_size_.SetSize(static_cast<int>(8.5 * dpi_), |
110 static_cast<int>(11.0 * dpi_)); | 42 static_cast<int>(11.0 * dpi_)); |
111 printable_size_.SetSize(static_cast<int>((7.5 * dpi_)), | 43 printable_size_.SetSize(static_cast<int>((7.5 * dpi_)), |
112 static_cast<int>((10.0 * dpi_))); | 44 static_cast<int>((10.0 * dpi_))); |
113 margin_left_ = margin_top_ = static_cast<int>(0.5 * dpi_); | 45 margin_left_ = margin_top_ = static_cast<int>(0.5 * dpi_); |
114 } | 46 } |
115 | 47 |
116 MockPrinter::~MockPrinter() { | 48 MockPrinter::~MockPrinter() { |
117 } | 49 } |
118 | 50 |
119 void MockPrinter::ResetPrinter() { | 51 void MockPrinter::ResetPrinter() { |
120 printer_status_ = PRINTER_READY; | 52 printer_status_ = PRINTER_READY; |
121 document_cookie_ = -1; | 53 document_cookie_ = -1; |
122 } | 54 } |
123 | 55 |
124 void MockPrinter::GetDefaultPrintSettings(PrintMsg_Print_Params* params) { | 56 void MockPrinter::GetDefaultPrintSettings(PrintMsg_Print_Params* params) { |
125 // Verify this printer is not processing a job. | 57 // Verify this printer is not processing a job. |
126 // Sorry, this mock printer is very fragile. | 58 // Sorry, this mock printer is very fragile. |
127 EXPECT_EQ(-1, document_cookie_); | 59 EXPECT_EQ(-1, document_cookie_); |
128 | 60 |
129 // Assign a unit document cookie and set the print settings. | 61 // Assign a unit document cookie and set the print settings. |
130 document_cookie_ = CreateDocumentCookie(); | 62 document_cookie_ = CreateDocumentCookie(); |
131 PrintMsg_Print_Params_Clone params_clone; | 63 memset(params, 0, sizeof(PrintMsg_Print_Params)); |
132 params_clone.ResetParams(params); | |
133 SetPrintParams(params); | 64 SetPrintParams(params); |
134 } | 65 } |
135 | 66 |
136 void MockPrinter::SetDefaultPrintSettings(const PrintMsg_Print_Params& params) { | 67 void MockPrinter::SetDefaultPrintSettings(const PrintMsg_Print_Params& params) { |
137 dpi_ = params.dpi; | 68 dpi_ = params.dpi; |
138 max_shrink_ = params.max_shrink; | 69 max_shrink_ = params.max_shrink; |
139 min_shrink_ = params.min_shrink; | 70 min_shrink_ = params.min_shrink; |
140 desired_dpi_ = params.desired_dpi; | 71 desired_dpi_ = params.desired_dpi; |
141 selection_only_ = params.selection_only; | 72 selection_only_ = params.selection_only; |
142 page_size_ = params.page_size; | 73 page_size_ = params.page_size; |
143 printable_size_ = params.printable_size; | 74 printable_size_ = params.printable_size; |
144 margin_left_ = params.margin_left; | 75 margin_left_ = params.margin_left; |
145 margin_top_ = params.margin_top; | 76 margin_top_ = params.margin_top; |
146 display_header_footer_ = params.display_header_footer; | |
147 date_ = params.date; | |
148 title_ = params.title; | |
149 url_ = params.url; | |
150 } | 77 } |
151 | 78 |
152 void MockPrinter::ScriptedPrint(int cookie, | 79 void MockPrinter::ScriptedPrint(int cookie, |
153 int expected_pages_count, | 80 int expected_pages_count, |
154 bool has_selection, | 81 bool has_selection, |
155 PrintMsg_PrintPages_Params* settings) { | 82 PrintMsg_PrintPages_Params* settings) { |
156 // Verify the input parameters. | 83 // Verify the input parameters. |
157 EXPECT_EQ(document_cookie_, cookie); | 84 EXPECT_EQ(document_cookie_, cookie); |
158 | 85 |
159 PrintMsg_PrintPages_Params_Clone params_clone; | 86 memset(settings, 0, sizeof(PrintMsg_PrintPages_Params)); |
160 params_clone.ResetParams(settings); | |
161 | |
162 settings->params.dpi = dpi_; | 87 settings->params.dpi = dpi_; |
163 settings->params.max_shrink = max_shrink_; | 88 settings->params.max_shrink = max_shrink_; |
164 settings->params.min_shrink = min_shrink_; | 89 settings->params.min_shrink = min_shrink_; |
165 settings->params.desired_dpi = desired_dpi_; | 90 settings->params.desired_dpi = desired_dpi_; |
166 settings->params.selection_only = selection_only_; | 91 settings->params.selection_only = selection_only_; |
167 settings->params.document_cookie = document_cookie_; | 92 settings->params.document_cookie = document_cookie_; |
168 settings->params.page_size = page_size_; | 93 settings->params.page_size = page_size_; |
169 settings->params.printable_size = printable_size_; | 94 settings->params.printable_size = printable_size_; |
170 settings->params.is_first_request = is_first_request_; | 95 settings->params.is_first_request = is_first_request_; |
171 settings->params.preview_request_id = preview_request_id_; | 96 settings->params.preview_request_id = preview_request_id_; |
172 settings->params.display_header_footer = display_header_footer_; | |
173 settings->params.date = date_; | |
174 settings->params.title = title_; | |
175 settings->params.url = url_; | |
176 printer_status_ = PRINTER_PRINTING; | 97 printer_status_ = PRINTER_PRINTING; |
177 } | 98 } |
178 | 99 |
179 void MockPrinter::UpdateSettings(int cookie, | 100 void MockPrinter::UpdateSettings(int cookie, |
180 PrintMsg_PrintPages_Params* params) { | 101 PrintMsg_PrintPages_Params* params) { |
181 EXPECT_EQ(document_cookie_, cookie); | 102 EXPECT_EQ(document_cookie_, cookie); |
182 | 103 |
183 PrintMsg_PrintPages_Params_Clone params_clone; | 104 memset(params, 0, sizeof(PrintMsg_PrintPages_Params)); |
184 params_clone.ResetParams(params); | |
185 SetPrintParams(&(params->params)); | 105 SetPrintParams(&(params->params)); |
186 printer_status_ = PRINTER_PRINTING; | 106 printer_status_ = PRINTER_PRINTING; |
187 } | 107 } |
188 | 108 |
189 void MockPrinter::SetPrintedPagesCount(int cookie, int number_pages) { | 109 void MockPrinter::SetPrintedPagesCount(int cookie, int number_pages) { |
190 // Verify the input parameter and update the printer status so that the | 110 // Verify the input parameter and update the printer status so that the |
191 // RenderViewTest class can verify the this function finishes without errors. | 111 // RenderViewTest class can verify the this function finishes without errors. |
192 EXPECT_EQ(document_cookie_, cookie); | 112 EXPECT_EQ(document_cookie_, cookie); |
193 EXPECT_EQ(PRINTER_PRINTING, printer_status_); | 113 EXPECT_EQ(PRINTER_PRINTING, printer_status_); |
194 EXPECT_EQ(0, number_pages_); | 114 EXPECT_EQ(0, number_pages_); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 params->min_shrink = min_shrink_; | 229 params->min_shrink = min_shrink_; |
310 params->desired_dpi = desired_dpi_; | 230 params->desired_dpi = desired_dpi_; |
311 params->selection_only = selection_only_; | 231 params->selection_only = selection_only_; |
312 params->document_cookie = document_cookie_; | 232 params->document_cookie = document_cookie_; |
313 params->page_size = page_size_; | 233 params->page_size = page_size_; |
314 params->printable_size = printable_size_; | 234 params->printable_size = printable_size_; |
315 params->margin_left = margin_left_; | 235 params->margin_left = margin_left_; |
316 params->margin_top = margin_top_; | 236 params->margin_top = margin_top_; |
317 params->is_first_request = is_first_request_; | 237 params->is_first_request = is_first_request_; |
318 params->preview_request_id = preview_request_id_; | 238 params->preview_request_id = preview_request_id_; |
319 params->display_header_footer = display_header_footer_; | |
320 params->date = date_; | |
321 params->title = title_; | |
322 params->url = url_; | |
323 } | 239 } |
OLD | NEW |