1 // Copyright 2011 The Chromium Authors
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 =
49 std::max(margins.top, effective_margins.header + kTextHeight);
50 effective_margins.footer =
51 std::max(margins.footer, page_size.height() - printable_area.bottom());
52 effective_margins.right =
53 std::max(margins.right, page_size.width() - printable_area.right());
54 effective_margins.bottom =
55 std::max(margins.bottom, effective_margins.footer + kTextHeight);
57 // Calculate the overlay area.
58 gfx::Rect overlay_area(
59 effective_margins.left, effective_margins.header,
60 page_size.width() - effective_margins.right - effective_margins.left,
61 page_size.height() - effective_margins.footer - effective_margins.header);
63 // Calculate the content area.
64 gfx::Rect content_area(
65 overlay_area.x(), effective_margins.top, overlay_area.width(),
66 page_size.height() - effective_margins.bottom - effective_margins.top);
69 EXPECT_EQ(page_size, setup.physical_size())
70 << seed << " " << page_size.ToString() << " " << printable_area.ToString()
71 << " " << kTextHeight;
72 EXPECT_EQ(overlay_area, setup.overlay_area())
73 << seed << " " << page_size.ToString() << " " << printable_area.ToString()
74 << " " << kTextHeight;
75 EXPECT_EQ(content_area, setup.content_area())
76 << seed << " " << page_size.ToString() << " " << printable_area.ToString()
77 << " " << kTextHeight;
79 EXPECT_EQ(effective_margins.header, setup.effective_margins().header)
80 << seed << " " << page_size.ToString() << " " << printable_area.ToString()
81 << " " << kTextHeight;
82 EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer)
83 << seed << " " << page_size.ToString() << " " << printable_area.ToString()
84 << " " << kTextHeight;
85 EXPECT_EQ(effective_margins.left, setup.effective_margins().left)
86 << seed << " " << page_size.ToString() << " " << printable_area.ToString()
87 << " " << kTextHeight;
88 EXPECT_EQ(effective_margins.top, setup.effective_margins().top)
89 << seed << " " << page_size.ToString() << " " << printable_area.ToString()
90 << " " << kTextHeight;
91 EXPECT_EQ(effective_margins.right, setup.effective_margins().right)
92 << seed << " " << page_size.ToString() << " " << printable_area.ToString()
93 << " " << kTextHeight;
94 EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom)
95 << seed << " " << page_size.ToString() << " " << printable_area.ToString()
96 << " " << kTextHeight;
99 TEST(PageSetupTest, HardCoded) {
111 gfx::Size page_size(100, 100);
112 gfx::Rect printable_area(3, 3, 94, 94);
114 // Make the calculations.
116 setup.SetRequestedMargins(margins);
117 setup.Init(page_size, printable_area, kTextHeight);
119 // Calculate the effective margins.
120 PageMargins effective_margins;
121 effective_margins.header = 3;
122 effective_margins.left = 4;
123 effective_margins.top = 6;
124 effective_margins.footer = 3;
125 effective_margins.right = 4;
126 effective_margins.bottom = 6;
128 // Calculate the overlay area.
129 gfx::Rect overlay_area(4, 3, 92, 94);
131 // Calculate the content area.
132 gfx::Rect content_area(4, 6, 92, 88);
135 EXPECT_EQ(page_size, setup.physical_size())
136 << " " << page_size.ToString() << " " << printable_area.ToString() << " "
138 EXPECT_EQ(overlay_area, setup.overlay_area())
139 << " " << page_size.ToString() << " " << printable_area.ToString() << " "
141 EXPECT_EQ(content_area, setup.content_area())
142 << " " << page_size.ToString() << " " << printable_area.ToString() << " "
145 EXPECT_EQ(effective_margins.header, setup.effective_margins().header)
146 << " " << page_size.ToString() << " " << printable_area.ToString() << " "
148 EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer)
149 << " " << page_size.ToString() << " " << printable_area.ToString() << " "
151 EXPECT_EQ(effective_margins.left, setup.effective_margins().left)
152 << " " << page_size.ToString() << " " << printable_area.ToString() << " "
154 EXPECT_EQ(effective_margins.top, setup.effective_margins().top)
155 << " " << page_size.ToString() << " " << printable_area.ToString() << " "
157 EXPECT_EQ(effective_margins.right, setup.effective_margins().right)
158 << " " << page_size.ToString() << " " << printable_area.ToString() << " "
160 EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom)
161 << " " << page_size.ToString() << " " << printable_area.ToString() << " "
165 TEST(PageSetupTest, OutOfRangeMargins) {
172 margins.bottom = -13;
174 gfx::Size page_size(100, 100);
175 gfx::Rect printable_area(1, 2, 96, 94);
177 // Make the calculations.
179 setup.SetRequestedMargins(margins);
180 setup.Init(page_size, printable_area, 0);
182 EXPECT_EQ(setup.effective_margins().left, 1);
183 EXPECT_EQ(setup.effective_margins().top, 2);
184 EXPECT_EQ(setup.effective_margins().right, 3);
185 EXPECT_EQ(setup.effective_margins().bottom, 4);
187 setup.ForceRequestedMargins(margins);
188 EXPECT_EQ(setup.effective_margins().left, 0);
189 EXPECT_EQ(setup.effective_margins().top, 0);
190 EXPECT_EQ(setup.effective_margins().right, 0);
191 EXPECT_EQ(setup.effective_margins().bottom, 0);
194 TEST(PageSetupTest, FlipOrientation) {
206 gfx::Size page_size(100, 70);
207 gfx::Rect printable_area(8, 9, 92, 50);
209 // Make the calculations.
211 setup.SetRequestedMargins(margins);
212 setup.Init(page_size, printable_area, kTextHeight);
214 gfx::Rect overlay_area(8, 9, 86, 50);
215 gfx::Rect content_area(8, 14, 86, 40);
217 EXPECT_EQ(page_size, setup.physical_size());
218 EXPECT_EQ(overlay_area, setup.overlay_area());
219 EXPECT_EQ(content_area, setup.content_area());
221 EXPECT_EQ(setup.effective_margins().left, 8);
222 EXPECT_EQ(setup.effective_margins().top, 14);
223 EXPECT_EQ(setup.effective_margins().right, 6);
224 EXPECT_EQ(setup.effective_margins().bottom, 16);
226 // Flip the orientation
227 setup.FlipOrientation();
230 gfx::Size flipped_page_size(70, 100);
231 gfx::Rect flipped_printable_area(9, 0, 50, 92);
232 gfx::Rect flipped_overlay_area(9, 2, 50, 90);
233 gfx::Rect flipped_content_area(9, 14, 50, 73);
236 EXPECT_EQ(flipped_page_size, setup.physical_size());
237 EXPECT_EQ(flipped_overlay_area, setup.overlay_area());
238 EXPECT_EQ(flipped_content_area, setup.content_area());
239 EXPECT_EQ(flipped_printable_area, setup.printable_area());
241 // Margin values are updated as per the flipped values.
242 EXPECT_EQ(setup.effective_margins().left, 9);
243 EXPECT_EQ(setup.effective_margins().top, 14);
244 EXPECT_EQ(setup.effective_margins().right, 11);
245 EXPECT_EQ(setup.effective_margins().bottom, 13);
247 // Force requested margins and flip the orientation.
248 setup.Init(page_size, printable_area, kTextHeight);
249 setup.ForceRequestedMargins(margins);
250 EXPECT_EQ(setup.effective_margins().left, 4);
251 EXPECT_EQ(setup.effective_margins().top, 14);
252 EXPECT_EQ(setup.effective_margins().right, 6);
253 EXPECT_EQ(setup.effective_margins().bottom, 7);
255 // Flip the orientation
256 setup.FlipOrientation();
259 gfx::Rect new_printable_area(9, 0, 50, 92);
260 gfx::Rect new_overlay_area(4, 2, 60, 95);
261 gfx::Rect new_content_area(4, 14, 60, 79);
264 EXPECT_EQ(flipped_page_size, setup.physical_size());
265 EXPECT_EQ(new_overlay_area, setup.overlay_area());
266 EXPECT_EQ(new_content_area, setup.content_area());
267 EXPECT_EQ(new_printable_area, setup.printable_area());
269 // Margins values are changed respectively.
270 EXPECT_EQ(setup.effective_margins().left, 4);
271 EXPECT_EQ(setup.effective_margins().top, 14);
272 EXPECT_EQ(setup.effective_margins().right, 6);
273 EXPECT_EQ(setup.effective_margins().bottom, 7);
276 TEST(PageSetupTest, GetSymmetricalPrintableArea) {
277 gfx::Rect printable_area = PageSetup::GetSymmetricalPrintableArea(
278 gfx::Size(612, 792), gfx::Rect(0, 0, 560, 750));
279 EXPECT_EQ(gfx::Rect(52, 42, 508, 708), printable_area);
281 printable_area = PageSetup::GetSymmetricalPrintableArea(
282 gfx::Size(612, 792), gfx::Rect(50, 60, 550, 700));
283 EXPECT_EQ(gfx::Rect(50, 60, 512, 672), printable_area);
285 printable_area = PageSetup::GetSymmetricalPrintableArea(
286 gfx::Size(612, 792), gfx::Rect(-1, 60, 520, 700));
287 EXPECT_EQ(gfx::Rect(), printable_area);
288 printable_area = PageSetup::GetSymmetricalPrintableArea(
289 gfx::Size(612, 792), gfx::Rect(50, -1, 520, 700));
290 EXPECT_EQ(gfx::Rect(), printable_area);
291 printable_area = PageSetup::GetSymmetricalPrintableArea(
292 gfx::Size(612, 792), gfx::Rect(100, 60, 520, 700));
293 EXPECT_EQ(gfx::Rect(), printable_area);
294 printable_area = PageSetup::GetSymmetricalPrintableArea(
295 gfx::Size(612, 792), gfx::Rect(50, 100, 520, 700));
296 EXPECT_EQ(gfx::Rect(), printable_area);
297 printable_area = PageSetup::GetSymmetricalPrintableArea(
298 gfx::Size(612, 792), gfx::Rect(400, 60, 212, 700));
299 EXPECT_EQ(gfx::Rect(), printable_area);
300 printable_area = PageSetup::GetSymmetricalPrintableArea(
301 gfx::Size(612, 792), gfx::Rect(40, 600, 212, 192));
302 EXPECT_EQ(gfx::Rect(), printable_area);
305 } // namespace printing