Upload upstream chromium 73.0.3683.0
[platform/framework/web/chromium-efl.git] / printing / page_setup_unittest.cc
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.
4
5 #include "printing/page_setup.h"
6
7 #include <stdlib.h>
8 #include <time.h>
9
10 #include <algorithm>
11
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace printing {
15
16 TEST(PageSetupTest, Random) {
17   time_t seed = time(NULL);
18   int kMax = 10;
19   srand(static_cast<unsigned>(seed));
20
21   // Margins.
22   PageMargins margins;
23   margins.header = rand() % kMax;
24   margins.footer = rand() % kMax;
25   margins.left = rand() % kMax;
26   margins.top = rand() % kMax;
27   margins.right = rand() % kMax;
28   margins.bottom = rand() % kMax;
29   int kTextHeight = rand() % kMax;
30
31   // Page description.
32   gfx::Size page_size(100 + rand() % kMax, 200 + rand() % kMax);
33   gfx::Rect printable_area(rand() % kMax, rand() % kMax, 0, 0);
34   printable_area.set_width(page_size.width() - (rand() % kMax) -
35                            printable_area.x());
36   printable_area.set_height(page_size.height() - (rand() % kMax) -
37                             printable_area.y());
38
39   // Make the calculations.
40   PageSetup setup;
41   setup.SetRequestedMargins(margins);
42   setup.Init(page_size, printable_area, kTextHeight);
43
44   // Calculate the effective margins.
45   PageMargins effective_margins;
46   effective_margins.header = std::max(margins.header, printable_area.y());
47   effective_margins.left = std::max(margins.left, printable_area.x());
48   effective_margins.top = std::max(margins.top,
49                                    effective_margins.header + kTextHeight);
50   effective_margins.footer = std::max(margins.footer,
51                                       page_size.height() -
52                                           printable_area.bottom());
53   effective_margins.right = std::max(margins.right,
54                                       page_size.width() -
55                                           printable_area.right());
56   effective_margins.bottom = std::max(margins.bottom,
57                                       effective_margins.footer  + kTextHeight);
58
59   // Calculate the overlay area.
60   gfx::Rect overlay_area(effective_margins.left, effective_margins.header,
61                          page_size.width() - effective_margins.right -
62                             effective_margins.left,
63                          page_size.height() - effective_margins.footer -
64                             effective_margins.header);
65
66   // Calculate the content area.
67   gfx::Rect content_area(overlay_area.x(),
68                          effective_margins.top,
69                          overlay_area.width(),
70                          page_size.height() - effective_margins.bottom -
71                              effective_margins.top);
72
73   // Test values.
74   EXPECT_EQ(page_size, setup.physical_size()) << seed << " " <<
75       page_size.ToString() << " " << printable_area.ToString() <<
76       " " << kTextHeight;
77   EXPECT_EQ(overlay_area, setup.overlay_area()) << seed << " " <<
78       page_size.ToString() << " " << printable_area.ToString() <<
79       " " << kTextHeight;
80   EXPECT_EQ(content_area, setup.content_area()) << seed << " " <<
81       page_size.ToString() << " " << printable_area.ToString() <<
82       " " << kTextHeight;
83
84   EXPECT_EQ(effective_margins.header, setup.effective_margins().header) <<
85       seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
86       " " << kTextHeight;
87   EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) <<
88       seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
89       " " << kTextHeight;
90   EXPECT_EQ(effective_margins.left, setup.effective_margins().left) << seed <<
91       " " << page_size.ToString() << " " << printable_area.ToString() <<
92       " " << kTextHeight;
93   EXPECT_EQ(effective_margins.top, setup.effective_margins().top) << seed <<
94       " " << page_size.ToString() << " " << printable_area.ToString() <<
95       " " << kTextHeight;
96   EXPECT_EQ(effective_margins.right, setup.effective_margins().right) << seed <<
97       " " << page_size.ToString() << " " << printable_area.ToString() <<
98       " " << kTextHeight;
99   EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) <<
100       seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
101        " " << kTextHeight;
102 }
103
104 TEST(PageSetupTest, HardCoded) {
105   // Margins.
106   PageMargins margins;
107   margins.header = 2;
108   margins.footer = 2;
109   margins.left = 4;
110   margins.top = 4;
111   margins.right = 4;
112   margins.bottom = 4;
113   int kTextHeight = 3;
114
115   // Page description.
116   gfx::Size page_size(100, 100);
117   gfx::Rect printable_area(3, 3, 94, 94);
118
119   // Make the calculations.
120   PageSetup setup;
121   setup.SetRequestedMargins(margins);
122   setup.Init(page_size, printable_area, kTextHeight);
123
124   // Calculate the effective margins.
125   PageMargins effective_margins;
126   effective_margins.header = 3;
127   effective_margins.left = 4;
128   effective_margins.top = 6;
129   effective_margins.footer = 3;
130   effective_margins.right = 4;
131   effective_margins.bottom = 6;
132
133   // Calculate the overlay area.
134   gfx::Rect overlay_area(4, 3, 92, 94);
135
136   // Calculate the content area.
137   gfx::Rect content_area(4, 6, 92, 88);
138
139   // Test values.
140   EXPECT_EQ(page_size, setup.physical_size()) << " " << page_size.ToString() <<
141       " " << printable_area.ToString() << " " << kTextHeight;
142   EXPECT_EQ(overlay_area, setup.overlay_area()) << " " <<
143       page_size.ToString() <<  " " << printable_area.ToString() <<
144       " " << kTextHeight;
145   EXPECT_EQ(content_area, setup.content_area()) << " " <<
146       page_size.ToString() <<  " " << printable_area.ToString() <<
147       " " << kTextHeight;
148
149   EXPECT_EQ(effective_margins.header, setup.effective_margins().header) <<
150       " " << page_size.ToString() << " " <<
151       printable_area.ToString() << " " << kTextHeight;
152   EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) <<
153       " " << page_size.ToString() << " " << printable_area.ToString() <<
154       " " << kTextHeight;
155   EXPECT_EQ(effective_margins.left, setup.effective_margins().left) <<
156       " " << page_size.ToString() << " " << printable_area.ToString() <<
157       " " << kTextHeight;
158   EXPECT_EQ(effective_margins.top, setup.effective_margins().top) <<
159       " " << page_size.ToString() << " " << printable_area.ToString() <<
160       " " << kTextHeight;
161   EXPECT_EQ(effective_margins.right, setup.effective_margins().right) <<
162       " " << page_size.ToString() << " " << printable_area.ToString() <<
163       " " << kTextHeight;
164   EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) <<
165       " " << page_size.ToString() << " " << printable_area.ToString() <<
166       " " << kTextHeight;
167 }
168
169 TEST(PageSetupTest, OutOfRangeMargins) {
170   PageMargins margins;
171   margins.header = 0;
172   margins.footer = 0;
173   margins.left = -10;
174   margins.top = -11;
175   margins.right = -12;
176   margins.bottom = -13;
177
178   gfx::Size page_size(100, 100);
179   gfx::Rect printable_area(1, 2, 96, 94);
180
181   // Make the calculations.
182   PageSetup setup;
183   setup.SetRequestedMargins(margins);
184   setup.Init(page_size, printable_area, 0);
185
186   EXPECT_EQ(setup.effective_margins().left, 1);
187   EXPECT_EQ(setup.effective_margins().top, 2);
188   EXPECT_EQ(setup.effective_margins().right, 3);
189   EXPECT_EQ(setup.effective_margins().bottom, 4);
190
191   setup.ForceRequestedMargins(margins);
192   EXPECT_EQ(setup.effective_margins().left, 0);
193   EXPECT_EQ(setup.effective_margins().top, 0);
194   EXPECT_EQ(setup.effective_margins().right, 0);
195   EXPECT_EQ(setup.effective_margins().bottom, 0);
196 }
197
198 TEST(PageSetupTest, FlipOrientation) {
199   // Margins.
200   PageMargins margins;
201   margins.header = 2;
202   margins.footer = 3;
203   margins.left = 4;
204   margins.top = 14;
205   margins.right = 6;
206   margins.bottom = 7;
207   int kTextHeight = 5;
208
209   // Page description.
210   gfx::Size page_size(100, 70);
211   gfx::Rect printable_area(8, 9, 92, 50);
212
213   // Make the calculations.
214   PageSetup setup;
215   setup.SetRequestedMargins(margins);
216   setup.Init(page_size, printable_area, kTextHeight);
217
218   gfx::Rect overlay_area(8, 9, 86, 50);
219   gfx::Rect content_area(8, 14, 86, 40);
220
221   EXPECT_EQ(page_size, setup.physical_size());
222   EXPECT_EQ(overlay_area, setup.overlay_area());
223   EXPECT_EQ(content_area, setup.content_area());
224
225   EXPECT_EQ(setup.effective_margins().left, 8);
226   EXPECT_EQ(setup.effective_margins().top, 14);
227   EXPECT_EQ(setup.effective_margins().right, 6);
228   EXPECT_EQ(setup.effective_margins().bottom, 16);
229
230   // Flip the orientation
231   setup.FlipOrientation();
232
233   // Expected values.
234   gfx::Size flipped_page_size(70, 100);
235   gfx::Rect flipped_printable_area(9, 0, 50, 92);
236   gfx::Rect flipped_overlay_area(9, 2, 50, 90);
237   gfx::Rect flipped_content_area(9, 14, 50, 73);
238
239   // Test values.
240   EXPECT_EQ(flipped_page_size, setup.physical_size());
241   EXPECT_EQ(flipped_overlay_area, setup.overlay_area());
242   EXPECT_EQ(flipped_content_area, setup.content_area());
243   EXPECT_EQ(flipped_printable_area, setup.printable_area());
244
245   // Margin values are updated as per the flipped values.
246   EXPECT_EQ(setup.effective_margins().left, 9);
247   EXPECT_EQ(setup.effective_margins().top, 14);
248   EXPECT_EQ(setup.effective_margins().right, 11);
249   EXPECT_EQ(setup.effective_margins().bottom, 13);
250
251   // Force requested margins and flip the orientation.
252   setup.Init(page_size, printable_area, kTextHeight);
253   setup.ForceRequestedMargins(margins);
254   EXPECT_EQ(setup.effective_margins().left, 4);
255   EXPECT_EQ(setup.effective_margins().top, 14);
256   EXPECT_EQ(setup.effective_margins().right, 6);
257   EXPECT_EQ(setup.effective_margins().bottom, 7);
258
259   // Flip the orientation
260   setup.FlipOrientation();
261
262   // Expected values.
263   gfx::Rect new_printable_area(9, 0, 50, 92);
264   gfx::Rect new_overlay_area(4, 2, 60, 95);
265   gfx::Rect new_content_area(4, 14, 60, 79);
266
267   // Test values.
268   EXPECT_EQ(flipped_page_size, setup.physical_size());
269   EXPECT_EQ(new_overlay_area, setup.overlay_area());
270   EXPECT_EQ(new_content_area, setup.content_area());
271   EXPECT_EQ(new_printable_area, setup.printable_area());
272
273   // Margins values are changed respectively.
274   EXPECT_EQ(setup.effective_margins().left, 4);
275   EXPECT_EQ(setup.effective_margins().top, 14);
276   EXPECT_EQ(setup.effective_margins().right, 6);
277   EXPECT_EQ(setup.effective_margins().bottom, 7);
278 }
279
280 TEST(PageSetupTest, GetSymmetricalPrintableArea) {
281   gfx::Rect printable_area = PageSetup::GetSymmetricalPrintableArea(
282       gfx::Size(612, 792), gfx::Rect(0, 0, 560, 750));
283   EXPECT_EQ(gfx::Rect(52, 42, 508, 708), printable_area);
284
285   printable_area = PageSetup::GetSymmetricalPrintableArea(
286       gfx::Size(612, 792), gfx::Rect(50, 60, 550, 700));
287   EXPECT_EQ(gfx::Rect(50, 60, 512, 672), printable_area);
288
289   printable_area = PageSetup::GetSymmetricalPrintableArea(
290       gfx::Size(612, 792), gfx::Rect(-1, 60, 520, 700));
291   EXPECT_EQ(gfx::Rect(), printable_area);
292   printable_area = PageSetup::GetSymmetricalPrintableArea(
293       gfx::Size(612, 792), gfx::Rect(50, -1, 520, 700));
294   EXPECT_EQ(gfx::Rect(), printable_area);
295   printable_area = PageSetup::GetSymmetricalPrintableArea(
296       gfx::Size(612, 792), gfx::Rect(100, 60, 520, 700));
297   EXPECT_EQ(gfx::Rect(), printable_area);
298   printable_area = PageSetup::GetSymmetricalPrintableArea(
299       gfx::Size(612, 792), gfx::Rect(50, 100, 520, 700));
300   EXPECT_EQ(gfx::Rect(), printable_area);
301   printable_area = PageSetup::GetSymmetricalPrintableArea(
302       gfx::Size(612, 792), gfx::Rect(400, 60, 212, 700));
303   EXPECT_EQ(gfx::Rect(), printable_area);
304   printable_area = PageSetup::GetSymmetricalPrintableArea(
305       gfx::Size(612, 792), gfx::Rect(40, 600, 212, 192));
306   EXPECT_EQ(gfx::Rect(), printable_area);
307 }
308
309 }  // namespace printing