initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame^] | 1 | // Copyright 2008, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #include "chrome/browser/printing/page_setup.h" |
| 31 | |
| 32 | #include <stdlib.h> |
| 33 | #include <time.h> |
| 34 | |
| 35 | #include "testing/gtest/include/gtest/gtest.h" |
| 36 | |
| 37 | TEST(PageSetupTest, Random) { |
| 38 | time_t seed = time(NULL); |
| 39 | int kMax = 10; |
| 40 | srand(static_cast<unsigned>(seed)); |
| 41 | |
| 42 | // Margins. |
| 43 | printing::PageMargins margins; |
| 44 | margins.header = rand() % kMax; |
| 45 | margins.footer = rand() % kMax; |
| 46 | margins.left = rand() % kMax; |
| 47 | margins.top = rand() % kMax; |
| 48 | margins.right = rand() % kMax; |
| 49 | margins.bottom = rand() % kMax; |
| 50 | int kTextHeight = rand() % kMax; |
| 51 | |
| 52 | // Page description. |
| 53 | gfx::Size page_size(100 + rand() % kMax, 200 + rand() % kMax); |
| 54 | gfx::Rect printable_area(rand() % kMax, rand() % kMax, 0, 0); |
| 55 | printable_area.set_width(page_size.width() - (rand() % kMax) - |
| 56 | printable_area.x()); |
| 57 | printable_area.set_height(page_size.height() - (rand() % kMax) - |
| 58 | printable_area.y()); |
| 59 | |
| 60 | // Make the calculations. |
| 61 | printing::PageSetup setup; |
| 62 | setup.SetRequestedMargins(margins); |
| 63 | setup.Init(page_size, printable_area, kTextHeight); |
| 64 | |
| 65 | // Calculate the effective margins. |
| 66 | printing::PageMargins effective_margins; |
| 67 | effective_margins.header = std::max(margins.header, printable_area.y()); |
| 68 | effective_margins.left = std::max(margins.left, printable_area.x()); |
| 69 | effective_margins.top = std::max(margins.top, |
| 70 | effective_margins.header + kTextHeight); |
| 71 | effective_margins.footer = std::max(margins.footer, |
| 72 | page_size.height() - |
| 73 | printable_area.bottom()); |
| 74 | effective_margins.right = std::max(margins.right, |
| 75 | page_size.width() - |
| 76 | printable_area.right()); |
| 77 | effective_margins.bottom = std::max(margins.bottom, |
| 78 | effective_margins.footer + kTextHeight); |
| 79 | |
| 80 | // Calculate the overlay area. |
| 81 | gfx::Rect overlay_area(effective_margins.left, effective_margins.header, |
| 82 | page_size.width() - effective_margins.right - |
| 83 | effective_margins.left, |
| 84 | page_size.height() - effective_margins.footer - |
| 85 | effective_margins.header); |
| 86 | |
| 87 | // Calculate the content area. |
| 88 | gfx::Rect content_area(overlay_area.x(), |
| 89 | effective_margins.top, |
| 90 | overlay_area.width(), |
| 91 | page_size.height() - effective_margins.bottom - |
| 92 | effective_margins.top); |
| 93 | |
| 94 | // Test values. |
| 95 | EXPECT_EQ(page_size, setup.physical_size()) << seed << " " << page_size << |
| 96 | " " << printable_area << " " << kTextHeight; |
| 97 | EXPECT_EQ(overlay_area, setup.overlay_area()) << seed << " " << page_size << |
| 98 | " " << printable_area << " " << kTextHeight; |
| 99 | EXPECT_EQ(content_area, setup.content_area()) << seed << " " << page_size << |
| 100 | " " << printable_area << " " << kTextHeight; |
| 101 | |
| 102 | EXPECT_EQ(effective_margins.header, setup.effective_margins().header) << |
| 103 | seed << " " << page_size << " " << printable_area << " " << kTextHeight; |
| 104 | EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) << |
| 105 | seed << " " << page_size << " " << printable_area << " " << kTextHeight; |
| 106 | EXPECT_EQ(effective_margins.left, setup.effective_margins().left) << seed << |
| 107 | " " << page_size << " " << printable_area << " " << kTextHeight; |
| 108 | EXPECT_EQ(effective_margins.top, setup.effective_margins().top) << seed << |
| 109 | " " << page_size << " " << printable_area << " " << kTextHeight; |
| 110 | EXPECT_EQ(effective_margins.right, setup.effective_margins().right) << seed << |
| 111 | " " << page_size << " " << printable_area << " " << kTextHeight; |
| 112 | EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) << |
| 113 | seed << " " << page_size << " " << printable_area << " " << kTextHeight; |
| 114 | } |
| 115 | |
| 116 | TEST(PageSetupTest, HardCoded) { |
| 117 | // Margins. |
| 118 | printing::PageMargins margins; |
| 119 | margins.header = 2; |
| 120 | margins.footer = 2; |
| 121 | margins.left = 4; |
| 122 | margins.top = 4; |
| 123 | margins.right = 4; |
| 124 | margins.bottom = 4; |
| 125 | int kTextHeight = 3; |
| 126 | |
| 127 | // Page description. |
| 128 | gfx::Size page_size(100, 100); |
| 129 | gfx::Rect printable_area(3, 3, 94, 94); |
| 130 | |
| 131 | // Make the calculations. |
| 132 | printing::PageSetup setup; |
| 133 | setup.SetRequestedMargins(margins); |
| 134 | setup.Init(page_size, printable_area, kTextHeight); |
| 135 | |
| 136 | // Calculate the effective margins. |
| 137 | printing::PageMargins effective_margins; |
| 138 | effective_margins.header = 3; |
| 139 | effective_margins.left = 4; |
| 140 | effective_margins.top = 6; |
| 141 | effective_margins.footer = 3; |
| 142 | effective_margins.right = 4; |
| 143 | effective_margins.bottom = 6; |
| 144 | |
| 145 | // Calculate the overlay area. |
| 146 | gfx::Rect overlay_area(4, 3, 92, 94); |
| 147 | |
| 148 | // Calculate the content area. |
| 149 | gfx::Rect content_area(4, 6, 92, 88); |
| 150 | |
| 151 | // Test values. |
| 152 | EXPECT_EQ(page_size, setup.physical_size()) << " " << page_size << |
| 153 | " " << printable_area << " " << kTextHeight; |
| 154 | EXPECT_EQ(overlay_area, setup.overlay_area()) << " " << page_size << |
| 155 | " " << printable_area << " " << kTextHeight; |
| 156 | EXPECT_EQ(content_area, setup.content_area()) << " " << page_size << |
| 157 | " " << printable_area << " " << kTextHeight; |
| 158 | |
| 159 | EXPECT_EQ(effective_margins.header, setup.effective_margins().header) << |
| 160 | " " << page_size << " " << printable_area << " " << kTextHeight; |
| 161 | EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) << |
| 162 | " " << page_size << " " << printable_area << " " << kTextHeight; |
| 163 | EXPECT_EQ(effective_margins.left, setup.effective_margins().left) << |
| 164 | " " << page_size << " " << printable_area << " " << kTextHeight; |
| 165 | EXPECT_EQ(effective_margins.top, setup.effective_margins().top) << |
| 166 | " " << page_size << " " << printable_area << " " << kTextHeight; |
| 167 | EXPECT_EQ(effective_margins.right, setup.effective_margins().right) << |
| 168 | " " << page_size << " " << printable_area << " " << kTextHeight; |
| 169 | EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) << |
| 170 | " " << page_size << " " << printable_area << " " << kTextHeight; |
| 171 | } |