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
3 // found in the LICENSE file.
5 #include "printing/page_setup.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 TEST(PageSetupTest, Random) {
17 time_t seed = time(NULL);
19 srand(static_cast<unsigned>(seed));
23 margins.header = rand() % kMax;
24 margins.footer = rand() % kMax;
25 margins.left = rand() % kMax;
26 margins.top = rand() % kMax;
27 margins.right = rand() % kMax;
28 margins.bottom = rand() % kMax;
29 int kTextHeight = rand() % kMax;
32 gfx::Size page_size(100 + rand() % kMax, 200 + rand() % kMax);
33 gfx::Rect printable_area(rand() % kMax, rand() % kMax, 0, 0);
34 printable_area.set_width(page_size.width() - (rand() % kMax) -
36 printable_area.set_height(page_size.height() - (rand() % kMax) -
39 // Make the calculations.
41 setup.SetRequestedMargins(margins);
42 setup.Init(page_size, printable_area, kTextHeight);
44 // Calculate the effective margins.
45 PageMargins effective_margins;
46 effective_margins.header = std::max(margins.header, printable_area.y());
47 effective_margins.left = std::max(margins.left, printable_area.x());
48 effective_margins.top = std::max(margins.top,
49 effective_margins.header + kTextHeight);
50 effective_margins.footer = std::max(margins.footer,
52 printable_area.bottom());
53 effective_margins.right = std::max(margins.right,
55 printable_area.right());
56 effective_margins.bottom = std::max(margins.bottom,
57 effective_margins.footer + kTextHeight);
59 // Calculate the overlay area.
60 gfx::Rect overlay_area(effective_margins.left, effective_margins.header,
61 page_size.width() - effective_margins.right -
62 effective_margins.left,
63 page_size.height() - effective_margins.footer -
64 effective_margins.header);
66 // Calculate the content area.
67 gfx::Rect content_area(overlay_area.x(),
68 effective_margins.top,
70 page_size.height() - effective_margins.bottom -
71 effective_margins.top);
74 EXPECT_EQ(page_size, setup.physical_size()) << seed << " " <<
75 page_size.ToString() << " " << printable_area.ToString() <<
77 EXPECT_EQ(overlay_area, setup.overlay_area()) << seed << " " <<
78 page_size.ToString() << " " << printable_area.ToString() <<
80 EXPECT_EQ(content_area, setup.content_area()) << seed << " " <<
81 page_size.ToString() << " " << printable_area.ToString() <<
84 EXPECT_EQ(effective_margins.header, setup.effective_margins().header) <<
85 seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
87 EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) <<
88 seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
90 EXPECT_EQ(effective_margins.left, setup.effective_margins().left) << seed <<
91 " " << page_size.ToString() << " " << printable_area.ToString() <<
93 EXPECT_EQ(effective_margins.top, setup.effective_margins().top) << seed <<
94 " " << page_size.ToString() << " " << printable_area.ToString() <<
96 EXPECT_EQ(effective_margins.right, setup.effective_margins().right) << seed <<
97 " " << page_size.ToString() << " " << printable_area.ToString() <<
99 EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) <<
100 seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
104 TEST(PageSetupTest, HardCoded) {
116 gfx::Size page_size(100, 100);
117 gfx::Rect printable_area(3, 3, 94, 94);
119 // Make the calculations.
121 setup.SetRequestedMargins(margins);
122 setup.Init(page_size, printable_area, kTextHeight);
124 // Calculate the effective margins.
125 PageMargins effective_margins;
126 effective_margins.header = 3;
127 effective_margins.left = 4;
128 effective_margins.top = 6;
129 effective_margins.footer = 3;
130 effective_margins.right = 4;
131 effective_margins.bottom = 6;
133 // Calculate the overlay area.
134 gfx::Rect overlay_area(4, 3, 92, 94);
136 // Calculate the content area.
137 gfx::Rect content_area(4, 6, 92, 88);
140 EXPECT_EQ(page_size, setup.physical_size()) << " " << page_size.ToString() <<
141 " " << printable_area.ToString() << " " << kTextHeight;
142 EXPECT_EQ(overlay_area, setup.overlay_area()) << " " <<
143 page_size.ToString() << " " << printable_area.ToString() <<
145 EXPECT_EQ(content_area, setup.content_area()) << " " <<
146 page_size.ToString() << " " << printable_area.ToString() <<
149 EXPECT_EQ(effective_margins.header, setup.effective_margins().header) <<
150 " " << page_size.ToString() << " " <<
151 printable_area.ToString() << " " << kTextHeight;
152 EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) <<
153 " " << page_size.ToString() << " " << printable_area.ToString() <<
155 EXPECT_EQ(effective_margins.left, setup.effective_margins().left) <<
156 " " << page_size.ToString() << " " << printable_area.ToString() <<
158 EXPECT_EQ(effective_margins.top, setup.effective_margins().top) <<
159 " " << page_size.ToString() << " " << printable_area.ToString() <<
161 EXPECT_EQ(effective_margins.right, setup.effective_margins().right) <<
162 " " << page_size.ToString() << " " << printable_area.ToString() <<
164 EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) <<
165 " " << page_size.ToString() << " " << printable_area.ToString() <<
169 TEST(PageSetupTest, OutOfRangeMargins) {
176 margins.bottom = -13;
178 gfx::Size page_size(100, 100);
179 gfx::Rect printable_area(1, 2, 96, 94);
181 // Make the calculations.
183 setup.SetRequestedMargins(margins);
184 setup.Init(page_size, printable_area, 0);
186 EXPECT_EQ(setup.effective_margins().left, 1);
187 EXPECT_EQ(setup.effective_margins().top, 2);
188 EXPECT_EQ(setup.effective_margins().right, 3);
189 EXPECT_EQ(setup.effective_margins().bottom, 4);
191 setup.ForceRequestedMargins(margins);
192 EXPECT_EQ(setup.effective_margins().left, 0);
193 EXPECT_EQ(setup.effective_margins().top, 0);
194 EXPECT_EQ(setup.effective_margins().right, 0);
195 EXPECT_EQ(setup.effective_margins().bottom, 0);
198 TEST(PageSetupTest, FlipOrientation) {
210 gfx::Size page_size(100, 70);
211 gfx::Rect printable_area(8, 9, 92, 50);
213 // Make the calculations.
215 setup.SetRequestedMargins(margins);
216 setup.Init(page_size, printable_area, kTextHeight);
218 gfx::Rect overlay_area(8, 9, 86, 50);
219 gfx::Rect content_area(8, 14, 86, 40);
221 EXPECT_EQ(page_size, setup.physical_size());
222 EXPECT_EQ(overlay_area, setup.overlay_area());
223 EXPECT_EQ(content_area, setup.content_area());
225 EXPECT_EQ(setup.effective_margins().left, 8);
226 EXPECT_EQ(setup.effective_margins().top, 14);
227 EXPECT_EQ(setup.effective_margins().right, 6);
228 EXPECT_EQ(setup.effective_margins().bottom, 16);
230 // Flip the orientation
231 setup.FlipOrientation();
234 gfx::Size flipped_page_size(70, 100);
235 gfx::Rect flipped_printable_area(9, 0, 50, 92);
236 gfx::Rect flipped_overlay_area(9, 2, 50, 90);
237 gfx::Rect flipped_content_area(9, 14, 50, 73);
240 EXPECT_EQ(flipped_page_size, setup.physical_size());
241 EXPECT_EQ(flipped_overlay_area, setup.overlay_area());
242 EXPECT_EQ(flipped_content_area, setup.content_area());
243 EXPECT_EQ(flipped_printable_area, setup.printable_area());
245 // Margin values are updated as per the flipped values.
246 EXPECT_EQ(setup.effective_margins().left, 9);
247 EXPECT_EQ(setup.effective_margins().top, 14);
248 EXPECT_EQ(setup.effective_margins().right, 11);
249 EXPECT_EQ(setup.effective_margins().bottom, 13);
251 // Force requested margins and flip the orientation.
252 setup.Init(page_size, printable_area, kTextHeight);
253 setup.ForceRequestedMargins(margins);
254 EXPECT_EQ(setup.effective_margins().left, 4);
255 EXPECT_EQ(setup.effective_margins().top, 14);
256 EXPECT_EQ(setup.effective_margins().right, 6);
257 EXPECT_EQ(setup.effective_margins().bottom, 7);
259 // Flip the orientation
260 setup.FlipOrientation();
263 gfx::Rect new_printable_area(9, 0, 50, 92);
264 gfx::Rect new_overlay_area(4, 2, 60, 95);
265 gfx::Rect new_content_area(4, 14, 60, 79);
268 EXPECT_EQ(flipped_page_size, setup.physical_size());
269 EXPECT_EQ(new_overlay_area, setup.overlay_area());
270 EXPECT_EQ(new_content_area, setup.content_area());
271 EXPECT_EQ(new_printable_area, setup.printable_area());
273 // Margins values are changed respectively.
274 EXPECT_EQ(setup.effective_margins().left, 4);
275 EXPECT_EQ(setup.effective_margins().top, 14);
276 EXPECT_EQ(setup.effective_margins().right, 6);
277 EXPECT_EQ(setup.effective_margins().bottom, 7);
280 TEST(PageSetupTest, GetSymmetricalPrintableArea) {
281 gfx::Rect printable_area = PageSetup::GetSymmetricalPrintableArea(
282 gfx::Size(612, 792), gfx::Rect(0, 0, 560, 750));
283 EXPECT_EQ(gfx::Rect(52, 42, 508, 708), printable_area);
285 printable_area = PageSetup::GetSymmetricalPrintableArea(
286 gfx::Size(612, 792), gfx::Rect(50, 60, 550, 700));
287 EXPECT_EQ(gfx::Rect(50, 60, 512, 672), printable_area);
289 printable_area = PageSetup::GetSymmetricalPrintableArea(
290 gfx::Size(612, 792), gfx::Rect(-1, 60, 520, 700));
291 EXPECT_EQ(gfx::Rect(), printable_area);
292 printable_area = PageSetup::GetSymmetricalPrintableArea(
293 gfx::Size(612, 792), gfx::Rect(50, -1, 520, 700));
294 EXPECT_EQ(gfx::Rect(), printable_area);
295 printable_area = PageSetup::GetSymmetricalPrintableArea(
296 gfx::Size(612, 792), gfx::Rect(100, 60, 520, 700));
297 EXPECT_EQ(gfx::Rect(), printable_area);
298 printable_area = PageSetup::GetSymmetricalPrintableArea(
299 gfx::Size(612, 792), gfx::Rect(50, 100, 520, 700));
300 EXPECT_EQ(gfx::Rect(), printable_area);
301 printable_area = PageSetup::GetSymmetricalPrintableArea(
302 gfx::Size(612, 792), gfx::Rect(400, 60, 212, 700));
303 EXPECT_EQ(gfx::Rect(), printable_area);
304 printable_area = PageSetup::GetSymmetricalPrintableArea(
305 gfx::Size(612, 792), gfx::Rect(40, 600, 212, 192));
306 EXPECT_EQ(gfx::Rect(), printable_area);
309 } // namespace printing