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"
14 TEST(PageSetupTest, Random) {
15 time_t seed = time(NULL);
17 srand(static_cast<unsigned>(seed));
20 printing::PageMargins margins;
21 margins.header = rand() % kMax;
22 margins.footer = rand() % kMax;
23 margins.left = rand() % kMax;
24 margins.top = rand() % kMax;
25 margins.right = rand() % kMax;
26 margins.bottom = rand() % kMax;
27 int kTextHeight = rand() % kMax;
30 gfx::Size page_size(100 + rand() % kMax, 200 + rand() % kMax);
31 gfx::Rect printable_area(rand() % kMax, rand() % kMax, 0, 0);
32 printable_area.set_width(page_size.width() - (rand() % kMax) -
34 printable_area.set_height(page_size.height() - (rand() % kMax) -
37 // Make the calculations.
38 printing::PageSetup setup;
39 setup.SetRequestedMargins(margins);
40 setup.Init(page_size, printable_area, kTextHeight);
42 // Calculate the effective margins.
43 printing::PageMargins effective_margins;
44 effective_margins.header = std::max(margins.header, printable_area.y());
45 effective_margins.left = std::max(margins.left, printable_area.x());
46 effective_margins.top = std::max(margins.top,
47 effective_margins.header + kTextHeight);
48 effective_margins.footer = std::max(margins.footer,
50 printable_area.bottom());
51 effective_margins.right = std::max(margins.right,
53 printable_area.right());
54 effective_margins.bottom = std::max(margins.bottom,
55 effective_margins.footer + kTextHeight);
57 // Calculate the overlay area.
58 gfx::Rect overlay_area(effective_margins.left, effective_margins.header,
59 page_size.width() - effective_margins.right -
60 effective_margins.left,
61 page_size.height() - effective_margins.footer -
62 effective_margins.header);
64 // Calculate the content area.
65 gfx::Rect content_area(overlay_area.x(),
66 effective_margins.top,
68 page_size.height() - effective_margins.bottom -
69 effective_margins.top);
72 EXPECT_EQ(page_size, setup.physical_size()) << seed << " " <<
73 page_size.ToString() << " " << printable_area.ToString() <<
75 EXPECT_EQ(overlay_area, setup.overlay_area()) << seed << " " <<
76 page_size.ToString() << " " << printable_area.ToString() <<
78 EXPECT_EQ(content_area, setup.content_area()) << seed << " " <<
79 page_size.ToString() << " " << printable_area.ToString() <<
82 EXPECT_EQ(effective_margins.header, setup.effective_margins().header) <<
83 seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
85 EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) <<
86 seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
88 EXPECT_EQ(effective_margins.left, setup.effective_margins().left) << seed <<
89 " " << page_size.ToString() << " " << printable_area.ToString() <<
91 EXPECT_EQ(effective_margins.top, setup.effective_margins().top) << seed <<
92 " " << page_size.ToString() << " " << printable_area.ToString() <<
94 EXPECT_EQ(effective_margins.right, setup.effective_margins().right) << seed <<
95 " " << page_size.ToString() << " " << printable_area.ToString() <<
97 EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) <<
98 seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
102 TEST(PageSetupTest, HardCoded) {
104 printing::PageMargins margins;
114 gfx::Size page_size(100, 100);
115 gfx::Rect printable_area(3, 3, 94, 94);
117 // Make the calculations.
118 printing::PageSetup setup;
119 setup.SetRequestedMargins(margins);
120 setup.Init(page_size, printable_area, kTextHeight);
122 // Calculate the effective margins.
123 printing::PageMargins effective_margins;
124 effective_margins.header = 3;
125 effective_margins.left = 4;
126 effective_margins.top = 6;
127 effective_margins.footer = 3;
128 effective_margins.right = 4;
129 effective_margins.bottom = 6;
131 // Calculate the overlay area.
132 gfx::Rect overlay_area(4, 3, 92, 94);
134 // Calculate the content area.
135 gfx::Rect content_area(4, 6, 92, 88);
138 EXPECT_EQ(page_size, setup.physical_size()) << " " << page_size.ToString() <<
139 " " << printable_area.ToString() << " " << kTextHeight;
140 EXPECT_EQ(overlay_area, setup.overlay_area()) << " " <<
141 page_size.ToString() << " " << printable_area.ToString() <<
143 EXPECT_EQ(content_area, setup.content_area()) << " " <<
144 page_size.ToString() << " " << printable_area.ToString() <<
147 EXPECT_EQ(effective_margins.header, setup.effective_margins().header) <<
148 " " << page_size.ToString() << " " <<
149 printable_area.ToString() << " " << kTextHeight;
150 EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) <<
151 " " << page_size.ToString() << " " << printable_area.ToString() <<
153 EXPECT_EQ(effective_margins.left, setup.effective_margins().left) <<
154 " " << page_size.ToString() << " " << printable_area.ToString() <<
156 EXPECT_EQ(effective_margins.top, setup.effective_margins().top) <<
157 " " << page_size.ToString() << " " << printable_area.ToString() <<
159 EXPECT_EQ(effective_margins.right, setup.effective_margins().right) <<
160 " " << page_size.ToString() << " " << printable_area.ToString() <<
162 EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) <<
163 " " << page_size.ToString() << " " << printable_area.ToString() <<
167 TEST(PageSetupTest, OutOfRangeMargins) {
168 printing::PageMargins margins;
174 margins.bottom = -13;
176 gfx::Size page_size(100, 100);
177 gfx::Rect printable_area(1, 2, 96, 94);
179 // Make the calculations.
180 printing::PageSetup setup;
181 setup.SetRequestedMargins(margins);
182 setup.Init(page_size, printable_area, 0);
184 EXPECT_EQ(setup.effective_margins().left, 1);
185 EXPECT_EQ(setup.effective_margins().top, 2);
186 EXPECT_EQ(setup.effective_margins().right, 3);
187 EXPECT_EQ(setup.effective_margins().bottom, 4);
189 setup.ForceRequestedMargins(margins);
190 EXPECT_EQ(setup.effective_margins().left, 0);
191 EXPECT_EQ(setup.effective_margins().top, 0);
192 EXPECT_EQ(setup.effective_margins().right, 0);
193 EXPECT_EQ(setup.effective_margins().bottom, 0);
196 TEST(PageSetupTest, FlipOrientation) {
198 printing::PageMargins margins;
208 gfx::Size page_size(100, 70);
209 gfx::Rect printable_area(8, 9, 92, 50);
211 // Make the calculations.
212 printing::PageSetup setup;
213 setup.SetRequestedMargins(margins);
214 setup.Init(page_size, printable_area, kTextHeight);
216 gfx::Rect overlay_area(8, 9, 86, 50);
217 gfx::Rect content_area(8, 14, 86, 40);
219 EXPECT_EQ(page_size, setup.physical_size());
220 EXPECT_EQ(overlay_area, setup.overlay_area());
221 EXPECT_EQ(content_area, setup.content_area());
223 EXPECT_EQ(setup.effective_margins().left, 8);
224 EXPECT_EQ(setup.effective_margins().top, 14);
225 EXPECT_EQ(setup.effective_margins().right, 6);
226 EXPECT_EQ(setup.effective_margins().bottom, 16);
228 // Flip the orientation
229 setup.FlipOrientation();
232 gfx::Size flipped_page_size(70, 100);
233 gfx::Rect flipped_printable_area(9, 0, 50, 92);
234 gfx::Rect flipped_overlay_area(9, 2, 50, 90);
235 gfx::Rect flipped_content_area(9, 14, 50, 73);
238 EXPECT_EQ(flipped_page_size, setup.physical_size());
239 EXPECT_EQ(flipped_overlay_area, setup.overlay_area());
240 EXPECT_EQ(flipped_content_area, setup.content_area());
241 EXPECT_EQ(flipped_printable_area, setup.printable_area());
243 // Margin values are updated as per the flipped values.
244 EXPECT_EQ(setup.effective_margins().left, 9);
245 EXPECT_EQ(setup.effective_margins().top, 14);
246 EXPECT_EQ(setup.effective_margins().right, 11);
247 EXPECT_EQ(setup.effective_margins().bottom, 13);
249 // Force requested margins and flip the orientation.
250 setup.Init(page_size, printable_area, kTextHeight);
251 setup.ForceRequestedMargins(margins);
252 EXPECT_EQ(setup.effective_margins().left, 4);
253 EXPECT_EQ(setup.effective_margins().top, 14);
254 EXPECT_EQ(setup.effective_margins().right, 6);
255 EXPECT_EQ(setup.effective_margins().bottom, 7);
257 // Flip the orientation
258 setup.FlipOrientation();
261 gfx::Rect new_printable_area(9, 0, 50, 92);
262 gfx::Rect new_overlay_area(4, 2, 60, 95);
263 gfx::Rect new_content_area(4, 14, 60, 79);
266 EXPECT_EQ(flipped_page_size, setup.physical_size());
267 EXPECT_EQ(new_overlay_area, setup.overlay_area());
268 EXPECT_EQ(new_content_area, setup.content_area());
269 EXPECT_EQ(new_printable_area, setup.printable_area());
271 // Margins values are changed respectively.
272 EXPECT_EQ(setup.effective_margins().left, 4);
273 EXPECT_EQ(setup.effective_margins().top, 14);
274 EXPECT_EQ(setup.effective_margins().right, 6);
275 EXPECT_EQ(setup.effective_margins().bottom, 7);