blob: 00c8f2a0c9f6b0f19a4c37260510631b24a3328b [file] [log] [blame]
[email protected]047470c2011-09-27 20:05:301// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]8ff1d422009-07-07 21:31:395#include "printing/page_setup.h"
initial.commit09911bf2008-07-26 23:55:296
7#include <stdlib.h>
8#include <time.h>
9
10#include "testing/gtest/include/gtest/gtest.h"
11
12TEST(PageSetupTest, Random) {
13 time_t seed = time(NULL);
14 int kMax = 10;
15 srand(static_cast<unsigned>(seed));
16
17 // Margins.
18 printing::PageMargins margins;
19 margins.header = rand() % kMax;
20 margins.footer = rand() % kMax;
21 margins.left = rand() % kMax;
22 margins.top = rand() % kMax;
23 margins.right = rand() % kMax;
24 margins.bottom = rand() % kMax;
25 int kTextHeight = rand() % kMax;
26
27 // Page description.
28 gfx::Size page_size(100 + rand() % kMax, 200 + rand() % kMax);
29 gfx::Rect printable_area(rand() % kMax, rand() % kMax, 0, 0);
30 printable_area.set_width(page_size.width() - (rand() % kMax) -
31 printable_area.x());
32 printable_area.set_height(page_size.height() - (rand() % kMax) -
33 printable_area.y());
34
35 // Make the calculations.
36 printing::PageSetup setup;
37 setup.SetRequestedMargins(margins);
38 setup.Init(page_size, printable_area, kTextHeight);
39
40 // Calculate the effective margins.
41 printing::PageMargins effective_margins;
42 effective_margins.header = std::max(margins.header, printable_area.y());
43 effective_margins.left = std::max(margins.left, printable_area.x());
44 effective_margins.top = std::max(margins.top,
45 effective_margins.header + kTextHeight);
46 effective_margins.footer = std::max(margins.footer,
47 page_size.height() -
48 printable_area.bottom());
49 effective_margins.right = std::max(margins.right,
50 page_size.width() -
51 printable_area.right());
52 effective_margins.bottom = std::max(margins.bottom,
53 effective_margins.footer + kTextHeight);
54
55 // Calculate the overlay area.
56 gfx::Rect overlay_area(effective_margins.left, effective_margins.header,
57 page_size.width() - effective_margins.right -
58 effective_margins.left,
59 page_size.height() - effective_margins.footer -
60 effective_margins.header);
61
62 // Calculate the content area.
63 gfx::Rect content_area(overlay_area.x(),
64 effective_margins.top,
65 overlay_area.width(),
66 page_size.height() - effective_margins.bottom -
67 effective_margins.top);
68
69 // Test values.
[email protected]047470c2011-09-27 20:05:3070 EXPECT_EQ(page_size, setup.physical_size()) << seed << " " <<
[email protected]e0425ec2011-09-28 19:43:4871 page_size.ToString() << " " << printable_area.ToString() <<
72 " " << kTextHeight;
[email protected]047470c2011-09-27 20:05:3073 EXPECT_EQ(overlay_area, setup.overlay_area()) << seed << " " <<
[email protected]e0425ec2011-09-28 19:43:4874 page_size.ToString() << " " << printable_area.ToString() <<
75 " " << kTextHeight;
[email protected]047470c2011-09-27 20:05:3076 EXPECT_EQ(content_area, setup.content_area()) << seed << " " <<
[email protected]e0425ec2011-09-28 19:43:4877 page_size.ToString() << " " << printable_area.ToString() <<
78 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:2979
80 EXPECT_EQ(effective_margins.header, setup.effective_margins().header) <<
[email protected]e0425ec2011-09-28 19:43:4881 seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:3082 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:2983 EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) <<
[email protected]e0425ec2011-09-28 19:43:4884 seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:3085 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:2986 EXPECT_EQ(effective_margins.left, setup.effective_margins().left) << seed <<
[email protected]e0425ec2011-09-28 19:43:4887 " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:3088 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:2989 EXPECT_EQ(effective_margins.top, setup.effective_margins().top) << seed <<
[email protected]e0425ec2011-09-28 19:43:4890 " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:3091 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:2992 EXPECT_EQ(effective_margins.right, setup.effective_margins().right) << seed <<
[email protected]e0425ec2011-09-28 19:43:4893 " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:3094 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:2995 EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) <<
[email protected]e0425ec2011-09-28 19:43:4896 seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:3097 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:2998}
99
100TEST(PageSetupTest, HardCoded) {
101 // Margins.
102 printing::PageMargins margins;
103 margins.header = 2;
104 margins.footer = 2;
105 margins.left = 4;
106 margins.top = 4;
107 margins.right = 4;
108 margins.bottom = 4;
109 int kTextHeight = 3;
110
111 // Page description.
112 gfx::Size page_size(100, 100);
113 gfx::Rect printable_area(3, 3, 94, 94);
114
115 // Make the calculations.
116 printing::PageSetup setup;
117 setup.SetRequestedMargins(margins);
118 setup.Init(page_size, printable_area, kTextHeight);
119
120 // Calculate the effective margins.
121 printing::PageMargins effective_margins;
122 effective_margins.header = 3;
123 effective_margins.left = 4;
124 effective_margins.top = 6;
125 effective_margins.footer = 3;
126 effective_margins.right = 4;
127 effective_margins.bottom = 6;
128
129 // Calculate the overlay area.
130 gfx::Rect overlay_area(4, 3, 92, 94);
131
132 // Calculate the content area.
133 gfx::Rect content_area(4, 6, 92, 88);
134
135 // Test values.
[email protected]047470c2011-09-27 20:05:30136 EXPECT_EQ(page_size, setup.physical_size()) << " " << page_size.ToString() <<
[email protected]e0425ec2011-09-28 19:43:48137 " " << printable_area.ToString() << " " << kTextHeight;
[email protected]047470c2011-09-27 20:05:30138 EXPECT_EQ(overlay_area, setup.overlay_area()) << " " <<
[email protected]e0425ec2011-09-28 19:43:48139 page_size.ToString() << " " << printable_area.ToString() <<
140 " " << kTextHeight;
[email protected]047470c2011-09-27 20:05:30141 EXPECT_EQ(content_area, setup.content_area()) << " " <<
[email protected]e0425ec2011-09-28 19:43:48142 page_size.ToString() << " " << printable_area.ToString() <<
143 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:29144
145 EXPECT_EQ(effective_margins.header, setup.effective_margins().header) <<
[email protected]047470c2011-09-27 20:05:30146 " " << page_size.ToString() << " " <<
[email protected]e0425ec2011-09-28 19:43:48147 printable_area.ToString() << " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:29148 EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) <<
[email protected]e0425ec2011-09-28 19:43:48149 " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:30150 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:29151 EXPECT_EQ(effective_margins.left, setup.effective_margins().left) <<
[email protected]e0425ec2011-09-28 19:43:48152 " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:30153 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:29154 EXPECT_EQ(effective_margins.top, setup.effective_margins().top) <<
[email protected]e0425ec2011-09-28 19:43:48155 " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:30156 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:29157 EXPECT_EQ(effective_margins.right, setup.effective_margins().right) <<
[email protected]e0425ec2011-09-28 19:43:48158 " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:30159 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:29160 EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) <<
[email protected]e0425ec2011-09-28 19:43:48161 " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:30162 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:29163}
[email protected]b076a082011-10-20 01:26:32164
165TEST(PageSetupTest, OutOfRangeMargins) {
166 printing::PageMargins margins;
167 margins.header = 0;
168 margins.footer = 0;
169 margins.left = -10;
170 margins.top = -11;
171 margins.right = -12;
172 margins.bottom = -13;
173
174 gfx::Size page_size(100, 100);
175 gfx::Rect printable_area(1, 2, 96, 94);
176
177 // Make the calculations.
178 printing::PageSetup setup;
179 setup.SetRequestedMargins(margins);
180 setup.Init(page_size, printable_area, 0);
181
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);
186
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);
192}
[email protected]b89615d2011-11-04 00:29:21193
194TEST(PageSetupTest, FlipOrientation) {
195 // Margins.
196 printing::PageMargins margins;
197 margins.header = 2;
198 margins.footer = 3;
199 margins.left = 4;
200 margins.top = 14;
201 margins.right = 6;
202 margins.bottom = 7;
203 int kTextHeight = 5;
204
205 // Page description.
206 gfx::Size page_size(100, 70);
207 gfx::Rect printable_area(8, 9, 92, 50);
208
209 // Make the calculations.
210 printing::PageSetup setup;
211 setup.SetRequestedMargins(margins);
212 setup.Init(page_size, printable_area, kTextHeight);
213
214 gfx::Rect overlay_area(8, 9, 86, 50);
215 gfx::Rect content_area(8, 14, 86, 40);
216
217 EXPECT_EQ(page_size, setup.physical_size());
218 EXPECT_EQ(overlay_area, setup.overlay_area());
219 EXPECT_EQ(content_area, setup.content_area());
220
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);
225
226 // Flip the orientation
227 setup.FlipOrientation();
228
229 // Expected values.
230 gfx::Size flipped_page_size(70, 100);
231 gfx::Rect flipped_printable_area(9, 0, 50, 92);
232 gfx::Rect flipped_overlay_area(14, 2, 45, 90);
233 gfx::Rect flipped_content_area(14, 7, 45, 80);
234
235 // Test values.
236 EXPECT_EQ(flipped_page_size, setup.physical_size());
237 EXPECT_EQ(flipped_overlay_area, setup.overlay_area()) << " " <<
238 flipped_page_size.ToString() << " " << flipped_printable_area.ToString();
239 EXPECT_EQ(flipped_content_area, setup.content_area()) << " " <<
240 flipped_page_size.ToString() << " " << flipped_printable_area.ToString();
241 EXPECT_EQ(flipped_printable_area, setup.printable_area());
242
243 // Margin values are updated as per the flipped values.
244 EXPECT_EQ(setup.effective_margins().left, 14);
245 EXPECT_EQ(setup.effective_margins().top, 7);
246 EXPECT_EQ(setup.effective_margins().right, 11);
247 EXPECT_EQ(setup.effective_margins().bottom, 13);
248
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);
256
257 // Flip the orientation
258 setup.FlipOrientation();
259
260 // Expected values.
261 gfx::Rect new_printable_area(9, 0, 50, 92);
262 gfx::Rect new_overlay_area(14, 2, 49, 95);
263 gfx::Rect new_content_area(14, 6, 49, 90);
264
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());
270
271 // Margins values are changed respectively.
272 EXPECT_EQ(setup.effective_margins().left,14);
273 EXPECT_EQ(setup.effective_margins().top, 6);
274 EXPECT_EQ(setup.effective_margins().right, 7);
275 EXPECT_EQ(setup.effective_margins().bottom, 4);
276}