15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "printing/page_setup.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdlib.h>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <time.h>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include <algorithm>
118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(PageSetupTest, Random) {
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  time_t seed = time(NULL);
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int kMax = 10;
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  srand(static_cast<unsigned>(seed));
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Margins.
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  printing::PageMargins margins;
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.header = rand() % kMax;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.footer = rand() % kMax;
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.left = rand() % kMax;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.top = rand() % kMax;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.right = rand() % kMax;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.bottom = rand() % kMax;
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int kTextHeight = rand() % kMax;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Page description.
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Size page_size(100 + rand() % kMax, 200 + rand() % kMax);
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Rect printable_area(rand() % kMax, rand() % kMax, 0, 0);
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  printable_area.set_width(page_size.width() - (rand() % kMax) -
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           printable_area.x());
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  printable_area.set_height(page_size.height() - (rand() % kMax) -
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            printable_area.y());
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Make the calculations.
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  printing::PageSetup setup;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  setup.SetRequestedMargins(margins);
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  setup.Init(page_size, printable_area, kTextHeight);
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Calculate the effective margins.
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  printing::PageMargins effective_margins;
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  effective_margins.header = std::max(margins.header, printable_area.y());
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  effective_margins.left = std::max(margins.left, printable_area.x());
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  effective_margins.top = std::max(margins.top,
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   effective_margins.header + kTextHeight);
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  effective_margins.footer = std::max(margins.footer,
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      page_size.height() -
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          printable_area.bottom());
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  effective_margins.right = std::max(margins.right,
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      page_size.width() -
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          printable_area.right());
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  effective_margins.bottom = std::max(margins.bottom,
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      effective_margins.footer  + kTextHeight);
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Calculate the overlay area.
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Rect overlay_area(effective_margins.left, effective_margins.header,
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         page_size.width() - effective_margins.right -
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            effective_margins.left,
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         page_size.height() - effective_margins.footer -
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            effective_margins.header);
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Calculate the content area.
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Rect content_area(overlay_area.x(),
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         effective_margins.top,
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         overlay_area.width(),
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         page_size.height() - effective_margins.bottom -
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             effective_margins.top);
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Test values.
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(page_size, setup.physical_size()) << seed << " " <<
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      page_size.ToString() << " " << printable_area.ToString() <<
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << kTextHeight;
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(overlay_area, setup.overlay_area()) << seed << " " <<
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      page_size.ToString() << " " << printable_area.ToString() <<
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << kTextHeight;
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(content_area, setup.content_area()) << seed << " " <<
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      page_size.ToString() << " " << printable_area.ToString() <<
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << kTextHeight;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(effective_margins.header, setup.effective_margins().header) <<
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << kTextHeight;
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) <<
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << kTextHeight;
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(effective_margins.left, setup.effective_margins().left) << seed <<
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << page_size.ToString() << " " << printable_area.ToString() <<
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << kTextHeight;
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(effective_margins.top, setup.effective_margins().top) << seed <<
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << page_size.ToString() << " " << printable_area.ToString() <<
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << kTextHeight;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(effective_margins.right, setup.effective_margins().right) << seed <<
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << page_size.ToString() << " " << printable_area.ToString() <<
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << kTextHeight;
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) <<
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      seed << " " << page_size.ToString() << " " << printable_area.ToString() <<
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       " " << kTextHeight;
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(PageSetupTest, HardCoded) {
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Margins.
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  printing::PageMargins margins;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.header = 2;
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.footer = 2;
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.left = 4;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.top = 4;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.right = 4;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.bottom = 4;
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int kTextHeight = 3;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Page description.
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Size page_size(100, 100);
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Rect printable_area(3, 3, 94, 94);
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Make the calculations.
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  printing::PageSetup setup;
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  setup.SetRequestedMargins(margins);
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  setup.Init(page_size, printable_area, kTextHeight);
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Calculate the effective margins.
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  printing::PageMargins effective_margins;
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  effective_margins.header = 3;
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  effective_margins.left = 4;
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  effective_margins.top = 6;
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  effective_margins.footer = 3;
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  effective_margins.right = 4;
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  effective_margins.bottom = 6;
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Calculate the overlay area.
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Rect overlay_area(4, 3, 92, 94);
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Calculate the content area.
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Rect content_area(4, 6, 92, 88);
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Test values.
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(page_size, setup.physical_size()) << " " << page_size.ToString() <<
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << printable_area.ToString() << " " << kTextHeight;
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(overlay_area, setup.overlay_area()) << " " <<
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      page_size.ToString() <<  " " << printable_area.ToString() <<
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << kTextHeight;
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(content_area, setup.content_area()) << " " <<
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      page_size.ToString() <<  " " << printable_area.ToString() <<
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << kTextHeight;
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(effective_margins.header, setup.effective_margins().header) <<
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << page_size.ToString() << " " <<
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      printable_area.ToString() << " " << kTextHeight;
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer) <<
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << page_size.ToString() << " " << printable_area.ToString() <<
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << kTextHeight;
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(effective_margins.left, setup.effective_margins().left) <<
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << page_size.ToString() << " " << printable_area.ToString() <<
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << kTextHeight;
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(effective_margins.top, setup.effective_margins().top) <<
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << page_size.ToString() << " " << printable_area.ToString() <<
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << kTextHeight;
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(effective_margins.right, setup.effective_margins().right) <<
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << page_size.ToString() << " " << printable_area.ToString() <<
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << kTextHeight;
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom) <<
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << page_size.ToString() << " " << printable_area.ToString() <<
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      " " << kTextHeight;
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(PageSetupTest, OutOfRangeMargins) {
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  printing::PageMargins margins;
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.header = 0;
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.footer = 0;
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.left = -10;
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.top = -11;
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.right = -12;
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.bottom = -13;
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Size page_size(100, 100);
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Rect printable_area(1, 2, 96, 94);
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Make the calculations.
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  printing::PageSetup setup;
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  setup.SetRequestedMargins(margins);
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  setup.Init(page_size, printable_area, 0);
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().left, 1);
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().top, 2);
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().right, 3);
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().bottom, 4);
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  setup.ForceRequestedMargins(margins);
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().left, 0);
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().top, 0);
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().right, 0);
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().bottom, 0);
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(PageSetupTest, FlipOrientation) {
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Margins.
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  printing::PageMargins margins;
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.header = 2;
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.footer = 3;
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.left = 4;
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.top = 14;
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.right = 6;
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  margins.bottom = 7;
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int kTextHeight = 5;
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Page description.
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Size page_size(100, 70);
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Rect printable_area(8, 9, 92, 50);
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Make the calculations.
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  printing::PageSetup setup;
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  setup.SetRequestedMargins(margins);
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  setup.Init(page_size, printable_area, kTextHeight);
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Rect overlay_area(8, 9, 86, 50);
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Rect content_area(8, 14, 86, 40);
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(page_size, setup.physical_size());
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(overlay_area, setup.overlay_area());
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(content_area, setup.content_area());
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().left, 8);
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().top, 14);
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().right, 6);
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().bottom, 16);
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Flip the orientation
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  setup.FlipOrientation();
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Expected values.
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Size flipped_page_size(70, 100);
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Rect flipped_printable_area(9, 0, 50, 92);
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Rect flipped_overlay_area(9, 2, 50, 90);
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Rect flipped_content_area(9, 14, 50, 73);
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Test values.
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(flipped_page_size, setup.physical_size());
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(flipped_overlay_area, setup.overlay_area());
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(flipped_content_area, setup.content_area());
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(flipped_printable_area, setup.printable_area());
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Margin values are updated as per the flipped values.
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().left, 9);
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().top, 14);
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().right, 11);
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().bottom, 13);
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Force requested margins and flip the orientation.
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  setup.Init(page_size, printable_area, kTextHeight);
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  setup.ForceRequestedMargins(margins);
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().left, 4);
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().top, 14);
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().right, 6);
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().bottom, 7);
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Flip the orientation
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  setup.FlipOrientation();
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Expected values.
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Rect new_printable_area(9, 0, 50, 92);
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Rect new_overlay_area(4, 2, 60, 95);
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Rect new_content_area(4, 14, 60, 79);
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Test values.
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(flipped_page_size, setup.physical_size());
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(new_overlay_area, setup.overlay_area());
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(new_content_area, setup.content_area());
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(new_printable_area, setup.printable_area());
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Margins values are changed respectively.
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().left, 4);
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().top, 14);
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().right, 6);
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(setup.effective_margins().bottom, 7);
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
277