Fixed a small bug, causing a crash (DCHECK hit) when x-webkit-speech input tags are...
[chromium-blink-merge.git] / printing / page_setup_unittest.cc
blob4915a6c0ddb655831c91dc04f8b84acb4f9d5aab
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 "testing/gtest/include/gtest/gtest.h"
12 TEST(PageSetupTest, Random) {
13 time_t seed = time(NULL);
14 int kMax = 10;
15 srand(static_cast<unsigned>(seed));
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;
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());
35 // Make the calculations.
36 printing::PageSetup setup;
37 setup.SetRequestedMargins(margins);
38 setup.Init(page_size, printable_area, kTextHeight);
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);
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);
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);
69 // Test values.
70 EXPECT_EQ(page_size, setup.physical_size()) << seed << " " <<
71 page_size.ToString() << " " << printable_area.ToString() <<
72 " " << kTextHeight;
73 EXPECT_EQ(overlay_area, setup.overlay_area()) << seed << " " <<
74 page_size.ToString() << " " << printable_area.ToString() <<
75 " " << kTextHeight;
76 EXPECT_EQ(content_area, setup.content_area()) << seed << " " <<
77 page_size.ToString() << " " << printable_area.ToString() <<
78 " " << kTextHeight;
80 EXPECT_EQ(effective_margins.header, setup.effective_margins().header) <<
81 seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
82 " " << kTextHeight;
83 EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) <<
84 seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
85 " " << kTextHeight;
86 EXPECT_EQ(effective_margins.left, setup.effective_margins().left) << seed <<
87 " " << page_size.ToString() << " " << printable_area.ToString() <<
88 " " << kTextHeight;
89 EXPECT_EQ(effective_margins.top, setup.effective_margins().top) << seed <<
90 " " << page_size.ToString() << " " << printable_area.ToString() <<
91 " " << kTextHeight;
92 EXPECT_EQ(effective_margins.right, setup.effective_margins().right) << seed <<
93 " " << page_size.ToString() << " " << printable_area.ToString() <<
94 " " << kTextHeight;
95 EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) <<
96 seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
97 " " << kTextHeight;
100 TEST(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;
111 // Page description.
112 gfx::Size page_size(100, 100);
113 gfx::Rect printable_area(3, 3, 94, 94);
115 // Make the calculations.
116 printing::PageSetup setup;
117 setup.SetRequestedMargins(margins);
118 setup.Init(page_size, printable_area, kTextHeight);
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;
129 // Calculate the overlay area.
130 gfx::Rect overlay_area(4, 3, 92, 94);
132 // Calculate the content area.
133 gfx::Rect content_area(4, 6, 92, 88);
135 // Test values.
136 EXPECT_EQ(page_size, setup.physical_size()) << " " << page_size.ToString() <<
137 " " << printable_area.ToString() << " " << kTextHeight;
138 EXPECT_EQ(overlay_area, setup.overlay_area()) << " " <<
139 page_size.ToString() << " " << printable_area.ToString() <<
140 " " << kTextHeight;
141 EXPECT_EQ(content_area, setup.content_area()) << " " <<
142 page_size.ToString() << " " << printable_area.ToString() <<
143 " " << kTextHeight;
145 EXPECT_EQ(effective_margins.header, setup.effective_margins().header) <<
146 " " << page_size.ToString() << " " <<
147 printable_area.ToString() << " " << kTextHeight;
148 EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) <<
149 " " << page_size.ToString() << " " << printable_area.ToString() <<
150 " " << kTextHeight;
151 EXPECT_EQ(effective_margins.left, setup.effective_margins().left) <<
152 " " << page_size.ToString() << " " << printable_area.ToString() <<
153 " " << kTextHeight;
154 EXPECT_EQ(effective_margins.top, setup.effective_margins().top) <<
155 " " << page_size.ToString() << " " << printable_area.ToString() <<
156 " " << kTextHeight;
157 EXPECT_EQ(effective_margins.right, setup.effective_margins().right) <<
158 " " << page_size.ToString() << " " << printable_area.ToString() <<
159 " " << kTextHeight;
160 EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) <<
161 " " << page_size.ToString() << " " << printable_area.ToString() <<
162 " " << kTextHeight;
165 TEST(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;
174 gfx::Size page_size(100, 100);
175 gfx::Rect printable_area(1, 2, 96, 94);
177 // Make the calculations.
178 printing::PageSetup setup;
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) {
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;
205 // Page description.
206 gfx::Size page_size(100, 70);
207 gfx::Rect printable_area(8, 9, 92, 50);
209 // Make the calculations.
210 printing::PageSetup setup;
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();
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(9, 2, 50, 90);
233 gfx::Rect flipped_content_area(9, 14, 50, 73);
235 // Test values.
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();
258 // Expected values.
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);
263 // Test values.
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);