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"
10 #include "testing/gtest/include/gtest/gtest.h"
12 TEST(PageSetupTest
, Random
) {
13 time_t seed
= time(NULL
);
15 srand(static_cast<unsigned>(seed
));
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
;
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
) -
32 printable_area
.set_height(page_size
.height() - (rand() % kMax
) -
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
,
48 printable_area
.bottom());
49 effective_margins
.right
= std::max(margins
.right
,
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
,
66 page_size
.height() - effective_margins
.bottom
-
67 effective_margins
.top
);
70 EXPECT_EQ(page_size
, setup
.physical_size()) << seed
<< " " <<
71 page_size
.ToString() << " " << printable_area
.ToString() <<
73 EXPECT_EQ(overlay_area
, setup
.overlay_area()) << seed
<< " " <<
74 page_size
.ToString() << " " << printable_area
.ToString() <<
76 EXPECT_EQ(content_area
, setup
.content_area()) << seed
<< " " <<
77 page_size
.ToString() << " " << printable_area
.ToString() <<
80 EXPECT_EQ(effective_margins
.header
, setup
.effective_margins().header
) <<
81 seed
<< " " << page_size
.ToString() << " " << printable_area
.ToString() <<
83 EXPECT_EQ(effective_margins
.footer
, setup
.effective_margins().footer
) <<
84 seed
<< " " << page_size
.ToString() << " " << printable_area
.ToString() <<
86 EXPECT_EQ(effective_margins
.left
, setup
.effective_margins().left
) << seed
<<
87 " " << page_size
.ToString() << " " << printable_area
.ToString() <<
89 EXPECT_EQ(effective_margins
.top
, setup
.effective_margins().top
) << seed
<<
90 " " << page_size
.ToString() << " " << printable_area
.ToString() <<
92 EXPECT_EQ(effective_margins
.right
, setup
.effective_margins().right
) << seed
<<
93 " " << page_size
.ToString() << " " << printable_area
.ToString() <<
95 EXPECT_EQ(effective_margins
.bottom
, setup
.effective_margins().bottom
) <<
96 seed
<< " " << page_size
.ToString() << " " << printable_area
.ToString() <<
100 TEST(PageSetupTest
, HardCoded
) {
102 printing::PageMargins margins
;
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);
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() <<
141 EXPECT_EQ(content_area
, setup
.content_area()) << " " <<
142 page_size
.ToString() << " " << printable_area
.ToString() <<
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() <<
151 EXPECT_EQ(effective_margins
.left
, setup
.effective_margins().left
) <<
152 " " << page_size
.ToString() << " " << printable_area
.ToString() <<
154 EXPECT_EQ(effective_margins
.top
, setup
.effective_margins().top
) <<
155 " " << page_size
.ToString() << " " << printable_area
.ToString() <<
157 EXPECT_EQ(effective_margins
.right
, setup
.effective_margins().right
) <<
158 " " << page_size
.ToString() << " " << printable_area
.ToString() <<
160 EXPECT_EQ(effective_margins
.bottom
, setup
.effective_margins().bottom
) <<
161 " " << page_size
.ToString() << " " << printable_area
.ToString() <<