Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / printing / page_setup_unittest.cc
blob50aa639fabf69539ab0adfe87a3c140b72477e03
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"
7 #include <stdlib.h>
8 #include <time.h>
10 #include <algorithm>
12 #include "testing/gtest/include/gtest/gtest.h"
14 TEST(PageSetupTest, Random) {
15 time_t seed = time(NULL);
16 int kMax = 10;
17 srand(static_cast<unsigned>(seed));
19 // Margins.
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;
29 // Page description.
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) -
33 printable_area.x());
34 printable_area.set_height(page_size.height() - (rand() % kMax) -
35 printable_area.y());
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,
49 page_size.height() -
50 printable_area.bottom());
51 effective_margins.right = std::max(margins.right,
52 page_size.width() -
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,
67 overlay_area.width(),
68 page_size.height() - effective_margins.bottom -
69 effective_margins.top);
71 // Test values.
72 EXPECT_EQ(page_size, setup.physical_size()) << seed << " " <<
73 page_size.ToString() << " " << printable_area.ToString() <<
74 " " << kTextHeight;
75 EXPECT_EQ(overlay_area, setup.overlay_area()) << seed << " " <<
76 page_size.ToString() << " " << printable_area.ToString() <<
77 " " << kTextHeight;
78 EXPECT_EQ(content_area, setup.content_area()) << seed << " " <<
79 page_size.ToString() << " " << printable_area.ToString() <<
80 " " << kTextHeight;
82 EXPECT_EQ(effective_margins.header, setup.effective_margins().header) <<
83 seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
84 " " << kTextHeight;
85 EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) <<
86 seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
87 " " << kTextHeight;
88 EXPECT_EQ(effective_margins.left, setup.effective_margins().left) << seed <<
89 " " << page_size.ToString() << " " << printable_area.ToString() <<
90 " " << kTextHeight;
91 EXPECT_EQ(effective_margins.top, setup.effective_margins().top) << seed <<
92 " " << page_size.ToString() << " " << printable_area.ToString() <<
93 " " << kTextHeight;
94 EXPECT_EQ(effective_margins.right, setup.effective_margins().right) << seed <<
95 " " << page_size.ToString() << " " << printable_area.ToString() <<
96 " " << kTextHeight;
97 EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) <<
98 seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
99 " " << kTextHeight;
102 TEST(PageSetupTest, HardCoded) {
103 // Margins.
104 printing::PageMargins margins;
105 margins.header = 2;
106 margins.footer = 2;
107 margins.left = 4;
108 margins.top = 4;
109 margins.right = 4;
110 margins.bottom = 4;
111 int kTextHeight = 3;
113 // Page description.
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);
137 // Test values.
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() <<
142 " " << kTextHeight;
143 EXPECT_EQ(content_area, setup.content_area()) << " " <<
144 page_size.ToString() << " " << printable_area.ToString() <<
145 " " << kTextHeight;
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() <<
152 " " << kTextHeight;
153 EXPECT_EQ(effective_margins.left, setup.effective_margins().left) <<
154 " " << page_size.ToString() << " " << printable_area.ToString() <<
155 " " << kTextHeight;
156 EXPECT_EQ(effective_margins.top, setup.effective_margins().top) <<
157 " " << page_size.ToString() << " " << printable_area.ToString() <<
158 " " << kTextHeight;
159 EXPECT_EQ(effective_margins.right, setup.effective_margins().right) <<
160 " " << page_size.ToString() << " " << printable_area.ToString() <<
161 " " << kTextHeight;
162 EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) <<
163 " " << page_size.ToString() << " " << printable_area.ToString() <<
164 " " << kTextHeight;
167 TEST(PageSetupTest, OutOfRangeMargins) {
168 printing::PageMargins margins;
169 margins.header = 0;
170 margins.footer = 0;
171 margins.left = -10;
172 margins.top = -11;
173 margins.right = -12;
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) {
197 // Margins.
198 printing::PageMargins margins;
199 margins.header = 2;
200 margins.footer = 3;
201 margins.left = 4;
202 margins.top = 14;
203 margins.right = 6;
204 margins.bottom = 7;
205 int kTextHeight = 5;
207 // Page description.
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();
231 // Expected values.
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);
237 // Test values.
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();
260 // Expected values.
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);
265 // Test values.
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);