blob: 50aa639fabf69539ab0adfe87a3c140b72477e03 [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
[email protected]bef17f12013-10-22 03:02:5610#include <algorithm>
11
initial.commit09911bf2008-07-26 23:55:2912#include "testing/gtest/include/gtest/gtest.h"
13
14TEST(PageSetupTest, Random) {
15 time_t seed = time(NULL);
16 int kMax = 10;
17 srand(static_cast<unsigned>(seed));
18
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;
28
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());
36
37 // Make the calculations.
38 printing::PageSetup setup;
39 setup.SetRequestedMargins(margins);
40 setup.Init(page_size, printable_area, kTextHeight);
41
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);
56
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);
63
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);
70
71 // Test values.
[email protected]047470c2011-09-27 20:05:3072 EXPECT_EQ(page_size, setup.physical_size()) << seed << " " <<
[email protected]e0425ec2011-09-28 19:43:4873 page_size.ToString() << " " << printable_area.ToString() <<
74 " " << kTextHeight;
[email protected]047470c2011-09-27 20:05:3075 EXPECT_EQ(overlay_area, setup.overlay_area()) << seed << " " <<
[email protected]e0425ec2011-09-28 19:43:4876 page_size.ToString() << " " << printable_area.ToString() <<
77 " " << kTextHeight;
[email protected]047470c2011-09-27 20:05:3078 EXPECT_EQ(content_area, setup.content_area()) << seed << " " <<
[email protected]e0425ec2011-09-28 19:43:4879 page_size.ToString() << " " << printable_area.ToString() <<
80 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:2981
82 EXPECT_EQ(effective_margins.header, setup.effective_margins().header) <<
[email protected]e0425ec2011-09-28 19:43:4883 seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:3084 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:2985 EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) <<
[email protected]e0425ec2011-09-28 19:43:4886 seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:3087 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:2988 EXPECT_EQ(effective_margins.left, setup.effective_margins().left) << seed <<
[email protected]e0425ec2011-09-28 19:43:4889 " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:3090 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:2991 EXPECT_EQ(effective_margins.top, setup.effective_margins().top) << seed <<
[email protected]e0425ec2011-09-28 19:43:4892 " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:3093 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:2994 EXPECT_EQ(effective_margins.right, setup.effective_margins().right) << seed <<
[email protected]e0425ec2011-09-28 19:43:4895 " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:3096 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:2997 EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) <<
[email protected]e0425ec2011-09-28 19:43:4898 seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:3099 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:29100}
101
102TEST(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;
112
113 // Page description.
114 gfx::Size page_size(100, 100);
115 gfx::Rect printable_area(3, 3, 94, 94);
116
117 // Make the calculations.
118 printing::PageSetup setup;
119 setup.SetRequestedMargins(margins);
120 setup.Init(page_size, printable_area, kTextHeight);
121
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;
130
131 // Calculate the overlay area.
132 gfx::Rect overlay_area(4, 3, 92, 94);
133
134 // Calculate the content area.
135 gfx::Rect content_area(4, 6, 92, 88);
136
137 // Test values.
[email protected]047470c2011-09-27 20:05:30138 EXPECT_EQ(page_size, setup.physical_size()) << " " << page_size.ToString() <<
[email protected]e0425ec2011-09-28 19:43:48139 " " << printable_area.ToString() << " " << kTextHeight;
[email protected]047470c2011-09-27 20:05:30140 EXPECT_EQ(overlay_area, setup.overlay_area()) << " " <<
[email protected]e0425ec2011-09-28 19:43:48141 page_size.ToString() << " " << printable_area.ToString() <<
142 " " << kTextHeight;
[email protected]047470c2011-09-27 20:05:30143 EXPECT_EQ(content_area, setup.content_area()) << " " <<
[email protected]e0425ec2011-09-28 19:43:48144 page_size.ToString() << " " << printable_area.ToString() <<
145 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:29146
147 EXPECT_EQ(effective_margins.header, setup.effective_margins().header) <<
[email protected]047470c2011-09-27 20:05:30148 " " << page_size.ToString() << " " <<
[email protected]e0425ec2011-09-28 19:43:48149 printable_area.ToString() << " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:29150 EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) <<
[email protected]e0425ec2011-09-28 19:43:48151 " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:30152 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:29153 EXPECT_EQ(effective_margins.left, setup.effective_margins().left) <<
[email protected]e0425ec2011-09-28 19:43:48154 " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:30155 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:29156 EXPECT_EQ(effective_margins.top, setup.effective_margins().top) <<
[email protected]e0425ec2011-09-28 19:43:48157 " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:30158 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:29159 EXPECT_EQ(effective_margins.right, setup.effective_margins().right) <<
[email protected]e0425ec2011-09-28 19:43:48160 " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:30161 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:29162 EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) <<
[email protected]e0425ec2011-09-28 19:43:48163 " " << page_size.ToString() << " " << printable_area.ToString() <<
[email protected]047470c2011-09-27 20:05:30164 " " << kTextHeight;
initial.commit09911bf2008-07-26 23:55:29165}
[email protected]b076a082011-10-20 01:26:32166
167TEST(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;
175
176 gfx::Size page_size(100, 100);
177 gfx::Rect printable_area(1, 2, 96, 94);
178
179 // Make the calculations.
180 printing::PageSetup setup;
181 setup.SetRequestedMargins(margins);
182 setup.Init(page_size, printable_area, 0);
183
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);
188
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);
194}
[email protected]b89615d2011-11-04 00:29:21195
196TEST(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;
206
207 // Page description.
208 gfx::Size page_size(100, 70);
209 gfx::Rect printable_area(8, 9, 92, 50);
210
211 // Make the calculations.
212 printing::PageSetup setup;
213 setup.SetRequestedMargins(margins);
214 setup.Init(page_size, printable_area, kTextHeight);
215
216 gfx::Rect overlay_area(8, 9, 86, 50);
217 gfx::Rect content_area(8, 14, 86, 40);
218
219 EXPECT_EQ(page_size, setup.physical_size());
220 EXPECT_EQ(overlay_area, setup.overlay_area());
221 EXPECT_EQ(content_area, setup.content_area());
222
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);
227
228 // Flip the orientation
229 setup.FlipOrientation();
230
231 // Expected values.
232 gfx::Size flipped_page_size(70, 100);
233 gfx::Rect flipped_printable_area(9, 0, 50, 92);
[email protected]8d29383c2011-11-09 19:43:54234 gfx::Rect flipped_overlay_area(9, 2, 50, 90);
235 gfx::Rect flipped_content_area(9, 14, 50, 73);
[email protected]b89615d2011-11-04 00:29:21236
237 // Test values.
238 EXPECT_EQ(flipped_page_size, setup.physical_size());
[email protected]8d29383c2011-11-09 19:43:54239 EXPECT_EQ(flipped_overlay_area, setup.overlay_area());
240 EXPECT_EQ(flipped_content_area, setup.content_area());
[email protected]b89615d2011-11-04 00:29:21241 EXPECT_EQ(flipped_printable_area, setup.printable_area());
242
243 // Margin values are updated as per the flipped values.
[email protected]8d29383c2011-11-09 19:43:54244 EXPECT_EQ(setup.effective_margins().left, 9);
245 EXPECT_EQ(setup.effective_margins().top, 14);
[email protected]b89615d2011-11-04 00:29:21246 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);
[email protected]8d29383c2011-11-09 19:43:54262 gfx::Rect new_overlay_area(4, 2, 60, 95);
263 gfx::Rect new_content_area(4, 14, 60, 79);
[email protected]b89615d2011-11-04 00:29:21264
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.
[email protected]8d29383c2011-11-09 19:43:54272 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);
[email protected]b89615d2011-11-04 00:29:21276}