license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame^] | 1 | // Copyright (c) 2006-2008 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. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #include "chrome/browser/printing/page_setup.h" |
| 6 | |
| 7 | #include <stdlib.h> |
| 8 | #include <time.h> |
| 9 | |
| 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
| 12 | TEST(PageSetupTest, Random) { |
| 13 | time_t seed = time(NULL); |
| 14 | int kMax = 10; |
| 15 | srand(static_cast<unsigned>(seed)); |
| 16 | |
| 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; |
| 26 | |
| 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()); |
| 34 | |
| 35 | // Make the calculations. |
| 36 | printing::PageSetup setup; |
| 37 | setup.SetRequestedMargins(margins); |
| 38 | setup.Init(page_size, printable_area, kTextHeight); |
| 39 | |
| 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); |
| 54 | |
| 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); |
| 61 | |
| 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); |
| 68 | |
| 69 | // Test values. |
| 70 | EXPECT_EQ(page_size, setup.physical_size()) << seed << " " << page_size << |
| 71 | " " << printable_area << " " << kTextHeight; |
| 72 | EXPECT_EQ(overlay_area, setup.overlay_area()) << seed << " " << page_size << |
| 73 | " " << printable_area << " " << kTextHeight; |
| 74 | EXPECT_EQ(content_area, setup.content_area()) << seed << " " << page_size << |
| 75 | " " << printable_area << " " << kTextHeight; |
| 76 | |
| 77 | EXPECT_EQ(effective_margins.header, setup.effective_margins().header) << |
| 78 | seed << " " << page_size << " " << printable_area << " " << kTextHeight; |
| 79 | EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) << |
| 80 | seed << " " << page_size << " " << printable_area << " " << kTextHeight; |
| 81 | EXPECT_EQ(effective_margins.left, setup.effective_margins().left) << seed << |
| 82 | " " << page_size << " " << printable_area << " " << kTextHeight; |
| 83 | EXPECT_EQ(effective_margins.top, setup.effective_margins().top) << seed << |
| 84 | " " << page_size << " " << printable_area << " " << kTextHeight; |
| 85 | EXPECT_EQ(effective_margins.right, setup.effective_margins().right) << seed << |
| 86 | " " << page_size << " " << printable_area << " " << kTextHeight; |
| 87 | EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) << |
| 88 | seed << " " << page_size << " " << printable_area << " " << kTextHeight; |
| 89 | } |
| 90 | |
| 91 | TEST(PageSetupTest, HardCoded) { |
| 92 | // Margins. |
| 93 | printing::PageMargins margins; |
| 94 | margins.header = 2; |
| 95 | margins.footer = 2; |
| 96 | margins.left = 4; |
| 97 | margins.top = 4; |
| 98 | margins.right = 4; |
| 99 | margins.bottom = 4; |
| 100 | int kTextHeight = 3; |
| 101 | |
| 102 | // Page description. |
| 103 | gfx::Size page_size(100, 100); |
| 104 | gfx::Rect printable_area(3, 3, 94, 94); |
| 105 | |
| 106 | // Make the calculations. |
| 107 | printing::PageSetup setup; |
| 108 | setup.SetRequestedMargins(margins); |
| 109 | setup.Init(page_size, printable_area, kTextHeight); |
| 110 | |
| 111 | // Calculate the effective margins. |
| 112 | printing::PageMargins effective_margins; |
| 113 | effective_margins.header = 3; |
| 114 | effective_margins.left = 4; |
| 115 | effective_margins.top = 6; |
| 116 | effective_margins.footer = 3; |
| 117 | effective_margins.right = 4; |
| 118 | effective_margins.bottom = 6; |
| 119 | |
| 120 | // Calculate the overlay area. |
| 121 | gfx::Rect overlay_area(4, 3, 92, 94); |
| 122 | |
| 123 | // Calculate the content area. |
| 124 | gfx::Rect content_area(4, 6, 92, 88); |
| 125 | |
| 126 | // Test values. |
| 127 | EXPECT_EQ(page_size, setup.physical_size()) << " " << page_size << |
| 128 | " " << printable_area << " " << kTextHeight; |
| 129 | EXPECT_EQ(overlay_area, setup.overlay_area()) << " " << page_size << |
| 130 | " " << printable_area << " " << kTextHeight; |
| 131 | EXPECT_EQ(content_area, setup.content_area()) << " " << page_size << |
| 132 | " " << printable_area << " " << kTextHeight; |
| 133 | |
| 134 | EXPECT_EQ(effective_margins.header, setup.effective_margins().header) << |
| 135 | " " << page_size << " " << printable_area << " " << kTextHeight; |
| 136 | EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) << |
| 137 | " " << page_size << " " << printable_area << " " << kTextHeight; |
| 138 | EXPECT_EQ(effective_margins.left, setup.effective_margins().left) << |
| 139 | " " << page_size << " " << printable_area << " " << kTextHeight; |
| 140 | EXPECT_EQ(effective_margins.top, setup.effective_margins().top) << |
| 141 | " " << page_size << " " << printable_area << " " << kTextHeight; |
| 142 | EXPECT_EQ(effective_margins.right, setup.effective_margins().right) << |
| 143 | " " << page_size << " " << printable_area << " " << kTextHeight; |
| 144 | EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) << |
| 145 | " " << page_size << " " << printable_area << " " << kTextHeight; |
| 146 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame^] | 147 | |