[M120 Migration][VD] Remove accessing oom_score_adj in zygote process
[platform/framework/web/chromium-efl.git] / printing / page_setup_unittest.cc
1 // Copyright 2011 The Chromium Authors
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 =
49       std::max(margins.top, effective_margins.header + kTextHeight);
50   effective_margins.footer =
51       std::max(margins.footer, page_size.height() - printable_area.bottom());
52   effective_margins.right =
53       std::max(margins.right, page_size.width() - printable_area.right());
54   effective_margins.bottom =
55       std::max(margins.bottom, effective_margins.footer + kTextHeight);
56
57   // Calculate the overlay area.
58   gfx::Rect overlay_area(
59       effective_margins.left, effective_margins.header,
60       page_size.width() - effective_margins.right - effective_margins.left,
61       page_size.height() - effective_margins.footer - effective_margins.header);
62
63   // Calculate the content area.
64   gfx::Rect content_area(
65       overlay_area.x(), effective_margins.top, overlay_area.width(),
66       page_size.height() - effective_margins.bottom - effective_margins.top);
67
68   // Test values.
69   EXPECT_EQ(page_size, setup.physical_size())
70       << seed << " " << page_size.ToString() << " " << printable_area.ToString()
71       << " " << kTextHeight;
72   EXPECT_EQ(overlay_area, setup.overlay_area())
73       << seed << " " << page_size.ToString() << " " << printable_area.ToString()
74       << " " << kTextHeight;
75   EXPECT_EQ(content_area, setup.content_area())
76       << seed << " " << page_size.ToString() << " " << printable_area.ToString()
77       << " " << kTextHeight;
78
79   EXPECT_EQ(effective_margins.header, setup.effective_margins().header)
80       << seed << " " << page_size.ToString() << " " << printable_area.ToString()
81       << " " << kTextHeight;
82   EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer)
83       << seed << " " << page_size.ToString() << " " << printable_area.ToString()
84       << " " << kTextHeight;
85   EXPECT_EQ(effective_margins.left, setup.effective_margins().left)
86       << seed << " " << page_size.ToString() << " " << printable_area.ToString()
87       << " " << kTextHeight;
88   EXPECT_EQ(effective_margins.top, setup.effective_margins().top)
89       << seed << " " << page_size.ToString() << " " << printable_area.ToString()
90       << " " << kTextHeight;
91   EXPECT_EQ(effective_margins.right, setup.effective_margins().right)
92       << seed << " " << page_size.ToString() << " " << printable_area.ToString()
93       << " " << kTextHeight;
94   EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom)
95       << seed << " " << page_size.ToString() << " " << printable_area.ToString()
96       << " " << kTextHeight;
97 }
98
99 TEST(PageSetupTest, HardCoded) {
100   // Margins.
101   PageMargins margins;
102   margins.header = 2;
103   margins.footer = 2;
104   margins.left = 4;
105   margins.top = 4;
106   margins.right = 4;
107   margins.bottom = 4;
108   int kTextHeight = 3;
109
110   // Page description.
111   gfx::Size page_size(100, 100);
112   gfx::Rect printable_area(3, 3, 94, 94);
113
114   // Make the calculations.
115   PageSetup setup;
116   setup.SetRequestedMargins(margins);
117   setup.Init(page_size, printable_area, kTextHeight);
118
119   // Calculate the effective margins.
120   PageMargins effective_margins;
121   effective_margins.header = 3;
122   effective_margins.left = 4;
123   effective_margins.top = 6;
124   effective_margins.footer = 3;
125   effective_margins.right = 4;
126   effective_margins.bottom = 6;
127
128   // Calculate the overlay area.
129   gfx::Rect overlay_area(4, 3, 92, 94);
130
131   // Calculate the content area.
132   gfx::Rect content_area(4, 6, 92, 88);
133
134   // Test values.
135   EXPECT_EQ(page_size, setup.physical_size())
136       << " " << page_size.ToString() << " " << printable_area.ToString() << " "
137       << 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;
144
145   EXPECT_EQ(effective_margins.header, setup.effective_margins().header)
146       << " " << page_size.ToString() << " " << printable_area.ToString() << " "
147       << 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;
163 }
164
165 TEST(PageSetupTest, OutOfRangeMargins) {
166   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   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 }
193
194 TEST(PageSetupTest, FlipOrientation) {
195   // Margins.
196   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   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(9, 2, 50, 90);
233   gfx::Rect flipped_content_area(9, 14, 50, 73);
234
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());
240
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);
246
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);
254
255   // Flip the orientation
256   setup.FlipOrientation();
257
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);
262
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());
268
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);
274 }
275
276 TEST(PageSetupTest, GetSymmetricalPrintableArea) {
277   gfx::Rect printable_area = PageSetup::GetSymmetricalPrintableArea(
278       gfx::Size(612, 792), gfx::Rect(0, 0, 560, 750));
279   EXPECT_EQ(gfx::Rect(52, 42, 508, 708), printable_area);
280
281   printable_area = PageSetup::GetSymmetricalPrintableArea(
282       gfx::Size(612, 792), gfx::Rect(50, 60, 550, 700));
283   EXPECT_EQ(gfx::Rect(50, 60, 512, 672), printable_area);
284
285   printable_area = PageSetup::GetSymmetricalPrintableArea(
286       gfx::Size(612, 792), gfx::Rect(-1, 60, 520, 700));
287   EXPECT_EQ(gfx::Rect(), printable_area);
288   printable_area = PageSetup::GetSymmetricalPrintableArea(
289       gfx::Size(612, 792), gfx::Rect(50, -1, 520, 700));
290   EXPECT_EQ(gfx::Rect(), printable_area);
291   printable_area = PageSetup::GetSymmetricalPrintableArea(
292       gfx::Size(612, 792), gfx::Rect(100, 60, 520, 700));
293   EXPECT_EQ(gfx::Rect(), printable_area);
294   printable_area = PageSetup::GetSymmetricalPrintableArea(
295       gfx::Size(612, 792), gfx::Rect(50, 100, 520, 700));
296   EXPECT_EQ(gfx::Rect(), printable_area);
297   printable_area = PageSetup::GetSymmetricalPrintableArea(
298       gfx::Size(612, 792), gfx::Rect(400, 60, 212, 700));
299   EXPECT_EQ(gfx::Rect(), printable_area);
300   printable_area = PageSetup::GetSymmetricalPrintableArea(
301       gfx::Size(612, 792), gfx::Rect(40, 600, 212, 192));
302   EXPECT_EQ(gfx::Rect(), printable_area);
303 }
304
305 }  // namespace printing