[email protected] | 047470c | 2011-09-27 20:05:30 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 5 | #include "printing/page_setup.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
| 7 | #include <stdlib.h> |
| 8 | #include <time.h> |
| 9 | |
[email protected] | bef17f1 | 2013-10-22 03:02:56 | [diff] [blame] | 10 | #include <algorithm> |
| 11 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
| 14 | TEST(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] | 047470c | 2011-09-27 20:05:30 | [diff] [blame] | 72 | EXPECT_EQ(page_size, setup.physical_size()) << seed << " " << |
[email protected] | e0425ec | 2011-09-28 19:43:48 | [diff] [blame] | 73 | page_size.ToString() << " " << printable_area.ToString() << |
| 74 | " " << kTextHeight; |
[email protected] | 047470c | 2011-09-27 20:05:30 | [diff] [blame] | 75 | EXPECT_EQ(overlay_area, setup.overlay_area()) << seed << " " << |
[email protected] | e0425ec | 2011-09-28 19:43:48 | [diff] [blame] | 76 | page_size.ToString() << " " << printable_area.ToString() << |
| 77 | " " << kTextHeight; |
[email protected] | 047470c | 2011-09-27 20:05:30 | [diff] [blame] | 78 | EXPECT_EQ(content_area, setup.content_area()) << seed << " " << |
[email protected] | e0425ec | 2011-09-28 19:43:48 | [diff] [blame] | 79 | page_size.ToString() << " " << printable_area.ToString() << |
| 80 | " " << kTextHeight; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 81 | |
| 82 | EXPECT_EQ(effective_margins.header, setup.effective_margins().header) << |
[email protected] | e0425ec | 2011-09-28 19:43:48 | [diff] [blame] | 83 | seed << " " << page_size.ToString() << " " << printable_area.ToString() << |
[email protected] | 047470c | 2011-09-27 20:05:30 | [diff] [blame] | 84 | " " << kTextHeight; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 85 | EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) << |
[email protected] | e0425ec | 2011-09-28 19:43:48 | [diff] [blame] | 86 | seed << " " << page_size.ToString() << " " << printable_area.ToString() << |
[email protected] | 047470c | 2011-09-27 20:05:30 | [diff] [blame] | 87 | " " << kTextHeight; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 88 | EXPECT_EQ(effective_margins.left, setup.effective_margins().left) << seed << |
[email protected] | e0425ec | 2011-09-28 19:43:48 | [diff] [blame] | 89 | " " << page_size.ToString() << " " << printable_area.ToString() << |
[email protected] | 047470c | 2011-09-27 20:05:30 | [diff] [blame] | 90 | " " << kTextHeight; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 91 | EXPECT_EQ(effective_margins.top, setup.effective_margins().top) << seed << |
[email protected] | e0425ec | 2011-09-28 19:43:48 | [diff] [blame] | 92 | " " << page_size.ToString() << " " << printable_area.ToString() << |
[email protected] | 047470c | 2011-09-27 20:05:30 | [diff] [blame] | 93 | " " << kTextHeight; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 94 | EXPECT_EQ(effective_margins.right, setup.effective_margins().right) << seed << |
[email protected] | e0425ec | 2011-09-28 19:43:48 | [diff] [blame] | 95 | " " << page_size.ToString() << " " << printable_area.ToString() << |
[email protected] | 047470c | 2011-09-27 20:05:30 | [diff] [blame] | 96 | " " << kTextHeight; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 97 | EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) << |
[email protected] | e0425ec | 2011-09-28 19:43:48 | [diff] [blame] | 98 | seed << " " << page_size.ToString() << " " << printable_area.ToString() << |
[email protected] | 047470c | 2011-09-27 20:05:30 | [diff] [blame] | 99 | " " << kTextHeight; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | TEST(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] | 047470c | 2011-09-27 20:05:30 | [diff] [blame] | 138 | EXPECT_EQ(page_size, setup.physical_size()) << " " << page_size.ToString() << |
[email protected] | e0425ec | 2011-09-28 19:43:48 | [diff] [blame] | 139 | " " << printable_area.ToString() << " " << kTextHeight; |
[email protected] | 047470c | 2011-09-27 20:05:30 | [diff] [blame] | 140 | EXPECT_EQ(overlay_area, setup.overlay_area()) << " " << |
[email protected] | e0425ec | 2011-09-28 19:43:48 | [diff] [blame] | 141 | page_size.ToString() << " " << printable_area.ToString() << |
| 142 | " " << kTextHeight; |
[email protected] | 047470c | 2011-09-27 20:05:30 | [diff] [blame] | 143 | EXPECT_EQ(content_area, setup.content_area()) << " " << |
[email protected] | e0425ec | 2011-09-28 19:43:48 | [diff] [blame] | 144 | page_size.ToString() << " " << printable_area.ToString() << |
| 145 | " " << kTextHeight; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 146 | |
| 147 | EXPECT_EQ(effective_margins.header, setup.effective_margins().header) << |
[email protected] | 047470c | 2011-09-27 20:05:30 | [diff] [blame] | 148 | " " << page_size.ToString() << " " << |
[email protected] | e0425ec | 2011-09-28 19:43:48 | [diff] [blame] | 149 | printable_area.ToString() << " " << kTextHeight; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) << |
[email protected] | e0425ec | 2011-09-28 19:43:48 | [diff] [blame] | 151 | " " << page_size.ToString() << " " << printable_area.ToString() << |
[email protected] | 047470c | 2011-09-27 20:05:30 | [diff] [blame] | 152 | " " << kTextHeight; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 153 | EXPECT_EQ(effective_margins.left, setup.effective_margins().left) << |
[email protected] | e0425ec | 2011-09-28 19:43:48 | [diff] [blame] | 154 | " " << page_size.ToString() << " " << printable_area.ToString() << |
[email protected] | 047470c | 2011-09-27 20:05:30 | [diff] [blame] | 155 | " " << kTextHeight; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | EXPECT_EQ(effective_margins.top, setup.effective_margins().top) << |
[email protected] | e0425ec | 2011-09-28 19:43:48 | [diff] [blame] | 157 | " " << page_size.ToString() << " " << printable_area.ToString() << |
[email protected] | 047470c | 2011-09-27 20:05:30 | [diff] [blame] | 158 | " " << kTextHeight; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 159 | EXPECT_EQ(effective_margins.right, setup.effective_margins().right) << |
[email protected] | e0425ec | 2011-09-28 19:43:48 | [diff] [blame] | 160 | " " << page_size.ToString() << " " << printable_area.ToString() << |
[email protected] | 047470c | 2011-09-27 20:05:30 | [diff] [blame] | 161 | " " << kTextHeight; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) << |
[email protected] | e0425ec | 2011-09-28 19:43:48 | [diff] [blame] | 163 | " " << page_size.ToString() << " " << printable_area.ToString() << |
[email protected] | 047470c | 2011-09-27 20:05:30 | [diff] [blame] | 164 | " " << kTextHeight; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 165 | } |
[email protected] | b076a08 | 2011-10-20 01:26:32 | [diff] [blame] | 166 | |
| 167 | TEST(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] | b89615d | 2011-11-04 00:29:21 | [diff] [blame] | 195 | |
| 196 | TEST(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] | 8d29383c | 2011-11-09 19:43:54 | [diff] [blame] | 234 | gfx::Rect flipped_overlay_area(9, 2, 50, 90); |
| 235 | gfx::Rect flipped_content_area(9, 14, 50, 73); |
[email protected] | b89615d | 2011-11-04 00:29:21 | [diff] [blame] | 236 | |
| 237 | // Test values. |
| 238 | EXPECT_EQ(flipped_page_size, setup.physical_size()); |
[email protected] | 8d29383c | 2011-11-09 19:43:54 | [diff] [blame] | 239 | EXPECT_EQ(flipped_overlay_area, setup.overlay_area()); |
| 240 | EXPECT_EQ(flipped_content_area, setup.content_area()); |
[email protected] | b89615d | 2011-11-04 00:29:21 | [diff] [blame] | 241 | EXPECT_EQ(flipped_printable_area, setup.printable_area()); |
| 242 | |
| 243 | // Margin values are updated as per the flipped values. |
[email protected] | 8d29383c | 2011-11-09 19:43:54 | [diff] [blame] | 244 | EXPECT_EQ(setup.effective_margins().left, 9); |
| 245 | EXPECT_EQ(setup.effective_margins().top, 14); |
[email protected] | b89615d | 2011-11-04 00:29:21 | [diff] [blame] | 246 | 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] | 8d29383c | 2011-11-09 19:43:54 | [diff] [blame] | 262 | gfx::Rect new_overlay_area(4, 2, 60, 95); |
| 263 | gfx::Rect new_content_area(4, 14, 60, 79); |
[email protected] | b89615d | 2011-11-04 00:29:21 | [diff] [blame] | 264 | |
| 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] | 8d29383c | 2011-11-09 19:43:54 | [diff] [blame] | 272 | 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] | b89615d | 2011-11-04 00:29:21 | [diff] [blame] | 276 | } |