page_setup_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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.
  4. #include "printing/page_setup.h"
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #include <algorithm>
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace printing {
  10. TEST(PageSetupTest, Random) {
  11. time_t seed = time(NULL);
  12. int kMax = 10;
  13. srand(static_cast<unsigned>(seed));
  14. // Margins.
  15. PageMargins margins;
  16. margins.header = rand() % kMax;
  17. margins.footer = rand() % kMax;
  18. margins.left = rand() % kMax;
  19. margins.top = rand() % kMax;
  20. margins.right = rand() % kMax;
  21. margins.bottom = rand() % kMax;
  22. int kTextHeight = rand() % kMax;
  23. // Page description.
  24. gfx::Size page_size(100 + rand() % kMax, 200 + rand() % kMax);
  25. gfx::Rect printable_area(rand() % kMax, rand() % kMax, 0, 0);
  26. printable_area.set_width(page_size.width() - (rand() % kMax) -
  27. printable_area.x());
  28. printable_area.set_height(page_size.height() - (rand() % kMax) -
  29. printable_area.y());
  30. // Make the calculations.
  31. PageSetup setup;
  32. setup.SetRequestedMargins(margins);
  33. setup.Init(page_size, printable_area, kTextHeight);
  34. // Calculate the effective margins.
  35. PageMargins effective_margins;
  36. effective_margins.header = std::max(margins.header, printable_area.y());
  37. effective_margins.left = std::max(margins.left, printable_area.x());
  38. effective_margins.top =
  39. std::max(margins.top, effective_margins.header + kTextHeight);
  40. effective_margins.footer =
  41. std::max(margins.footer, page_size.height() - printable_area.bottom());
  42. effective_margins.right =
  43. std::max(margins.right, page_size.width() - printable_area.right());
  44. effective_margins.bottom =
  45. std::max(margins.bottom, effective_margins.footer + kTextHeight);
  46. // Calculate the overlay area.
  47. gfx::Rect overlay_area(
  48. effective_margins.left, effective_margins.header,
  49. page_size.width() - effective_margins.right - effective_margins.left,
  50. page_size.height() - effective_margins.footer - effective_margins.header);
  51. // Calculate the content area.
  52. gfx::Rect content_area(
  53. overlay_area.x(), effective_margins.top, overlay_area.width(),
  54. page_size.height() - effective_margins.bottom - effective_margins.top);
  55. // Test values.
  56. EXPECT_EQ(page_size, setup.physical_size())
  57. << seed << " " << page_size.ToString() << " " << printable_area.ToString()
  58. << " " << kTextHeight;
  59. EXPECT_EQ(overlay_area, setup.overlay_area())
  60. << seed << " " << page_size.ToString() << " " << printable_area.ToString()
  61. << " " << kTextHeight;
  62. EXPECT_EQ(content_area, setup.content_area())
  63. << seed << " " << page_size.ToString() << " " << printable_area.ToString()
  64. << " " << kTextHeight;
  65. EXPECT_EQ(effective_margins.header, setup.effective_margins().header)
  66. << seed << " " << page_size.ToString() << " " << printable_area.ToString()
  67. << " " << kTextHeight;
  68. EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer)
  69. << seed << " " << page_size.ToString() << " " << printable_area.ToString()
  70. << " " << kTextHeight;
  71. EXPECT_EQ(effective_margins.left, setup.effective_margins().left)
  72. << seed << " " << page_size.ToString() << " " << printable_area.ToString()
  73. << " " << kTextHeight;
  74. EXPECT_EQ(effective_margins.top, setup.effective_margins().top)
  75. << seed << " " << page_size.ToString() << " " << printable_area.ToString()
  76. << " " << kTextHeight;
  77. EXPECT_EQ(effective_margins.right, setup.effective_margins().right)
  78. << seed << " " << page_size.ToString() << " " << printable_area.ToString()
  79. << " " << kTextHeight;
  80. EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom)
  81. << seed << " " << page_size.ToString() << " " << printable_area.ToString()
  82. << " " << kTextHeight;
  83. }
  84. TEST(PageSetupTest, HardCoded) {
  85. // Margins.
  86. PageMargins margins;
  87. margins.header = 2;
  88. margins.footer = 2;
  89. margins.left = 4;
  90. margins.top = 4;
  91. margins.right = 4;
  92. margins.bottom = 4;
  93. int kTextHeight = 3;
  94. // Page description.
  95. gfx::Size page_size(100, 100);
  96. gfx::Rect printable_area(3, 3, 94, 94);
  97. // Make the calculations.
  98. PageSetup setup;
  99. setup.SetRequestedMargins(margins);
  100. setup.Init(page_size, printable_area, kTextHeight);
  101. // Calculate the effective margins.
  102. PageMargins effective_margins;
  103. effective_margins.header = 3;
  104. effective_margins.left = 4;
  105. effective_margins.top = 6;
  106. effective_margins.footer = 3;
  107. effective_margins.right = 4;
  108. effective_margins.bottom = 6;
  109. // Calculate the overlay area.
  110. gfx::Rect overlay_area(4, 3, 92, 94);
  111. // Calculate the content area.
  112. gfx::Rect content_area(4, 6, 92, 88);
  113. // Test values.
  114. EXPECT_EQ(page_size, setup.physical_size())
  115. << " " << page_size.ToString() << " " << printable_area.ToString() << " "
  116. << kTextHeight;
  117. EXPECT_EQ(overlay_area, setup.overlay_area())
  118. << " " << page_size.ToString() << " " << printable_area.ToString() << " "
  119. << kTextHeight;
  120. EXPECT_EQ(content_area, setup.content_area())
  121. << " " << page_size.ToString() << " " << printable_area.ToString() << " "
  122. << kTextHeight;
  123. EXPECT_EQ(effective_margins.header, setup.effective_margins().header)
  124. << " " << page_size.ToString() << " " << printable_area.ToString() << " "
  125. << kTextHeight;
  126. EXPECT_EQ(effective_margins.footer, setup.effective_margins().footer)
  127. << " " << page_size.ToString() << " " << printable_area.ToString() << " "
  128. << kTextHeight;
  129. EXPECT_EQ(effective_margins.left, setup.effective_margins().left)
  130. << " " << page_size.ToString() << " " << printable_area.ToString() << " "
  131. << kTextHeight;
  132. EXPECT_EQ(effective_margins.top, setup.effective_margins().top)
  133. << " " << page_size.ToString() << " " << printable_area.ToString() << " "
  134. << kTextHeight;
  135. EXPECT_EQ(effective_margins.right, setup.effective_margins().right)
  136. << " " << page_size.ToString() << " " << printable_area.ToString() << " "
  137. << kTextHeight;
  138. EXPECT_EQ(effective_margins.bottom, setup.effective_margins().bottom)
  139. << " " << page_size.ToString() << " " << printable_area.ToString() << " "
  140. << kTextHeight;
  141. }
  142. TEST(PageSetupTest, OutOfRangeMargins) {
  143. PageMargins margins;
  144. margins.header = 0;
  145. margins.footer = 0;
  146. margins.left = -10;
  147. margins.top = -11;
  148. margins.right = -12;
  149. margins.bottom = -13;
  150. gfx::Size page_size(100, 100);
  151. gfx::Rect printable_area(1, 2, 96, 94);
  152. // Make the calculations.
  153. PageSetup setup;
  154. setup.SetRequestedMargins(margins);
  155. setup.Init(page_size, printable_area, 0);
  156. EXPECT_EQ(setup.effective_margins().left, 1);
  157. EXPECT_EQ(setup.effective_margins().top, 2);
  158. EXPECT_EQ(setup.effective_margins().right, 3);
  159. EXPECT_EQ(setup.effective_margins().bottom, 4);
  160. setup.ForceRequestedMargins(margins);
  161. EXPECT_EQ(setup.effective_margins().left, 0);
  162. EXPECT_EQ(setup.effective_margins().top, 0);
  163. EXPECT_EQ(setup.effective_margins().right, 0);
  164. EXPECT_EQ(setup.effective_margins().bottom, 0);
  165. }
  166. TEST(PageSetupTest, FlipOrientation) {
  167. // Margins.
  168. PageMargins margins;
  169. margins.header = 2;
  170. margins.footer = 3;
  171. margins.left = 4;
  172. margins.top = 14;
  173. margins.right = 6;
  174. margins.bottom = 7;
  175. int kTextHeight = 5;
  176. // Page description.
  177. gfx::Size page_size(100, 70);
  178. gfx::Rect printable_area(8, 9, 92, 50);
  179. // Make the calculations.
  180. PageSetup setup;
  181. setup.SetRequestedMargins(margins);
  182. setup.Init(page_size, printable_area, kTextHeight);
  183. gfx::Rect overlay_area(8, 9, 86, 50);
  184. gfx::Rect content_area(8, 14, 86, 40);
  185. EXPECT_EQ(page_size, setup.physical_size());
  186. EXPECT_EQ(overlay_area, setup.overlay_area());
  187. EXPECT_EQ(content_area, setup.content_area());
  188. EXPECT_EQ(setup.effective_margins().left, 8);
  189. EXPECT_EQ(setup.effective_margins().top, 14);
  190. EXPECT_EQ(setup.effective_margins().right, 6);
  191. EXPECT_EQ(setup.effective_margins().bottom, 16);
  192. // Flip the orientation
  193. setup.FlipOrientation();
  194. // Expected values.
  195. gfx::Size flipped_page_size(70, 100);
  196. gfx::Rect flipped_printable_area(9, 0, 50, 92);
  197. gfx::Rect flipped_overlay_area(9, 2, 50, 90);
  198. gfx::Rect flipped_content_area(9, 14, 50, 73);
  199. // Test values.
  200. EXPECT_EQ(flipped_page_size, setup.physical_size());
  201. EXPECT_EQ(flipped_overlay_area, setup.overlay_area());
  202. EXPECT_EQ(flipped_content_area, setup.content_area());
  203. EXPECT_EQ(flipped_printable_area, setup.printable_area());
  204. // Margin values are updated as per the flipped values.
  205. EXPECT_EQ(setup.effective_margins().left, 9);
  206. EXPECT_EQ(setup.effective_margins().top, 14);
  207. EXPECT_EQ(setup.effective_margins().right, 11);
  208. EXPECT_EQ(setup.effective_margins().bottom, 13);
  209. // Force requested margins and flip the orientation.
  210. setup.Init(page_size, printable_area, kTextHeight);
  211. setup.ForceRequestedMargins(margins);
  212. EXPECT_EQ(setup.effective_margins().left, 4);
  213. EXPECT_EQ(setup.effective_margins().top, 14);
  214. EXPECT_EQ(setup.effective_margins().right, 6);
  215. EXPECT_EQ(setup.effective_margins().bottom, 7);
  216. // Flip the orientation
  217. setup.FlipOrientation();
  218. // Expected values.
  219. gfx::Rect new_printable_area(9, 0, 50, 92);
  220. gfx::Rect new_overlay_area(4, 2, 60, 95);
  221. gfx::Rect new_content_area(4, 14, 60, 79);
  222. // Test values.
  223. EXPECT_EQ(flipped_page_size, setup.physical_size());
  224. EXPECT_EQ(new_overlay_area, setup.overlay_area());
  225. EXPECT_EQ(new_content_area, setup.content_area());
  226. EXPECT_EQ(new_printable_area, setup.printable_area());
  227. // Margins values are changed respectively.
  228. EXPECT_EQ(setup.effective_margins().left, 4);
  229. EXPECT_EQ(setup.effective_margins().top, 14);
  230. EXPECT_EQ(setup.effective_margins().right, 6);
  231. EXPECT_EQ(setup.effective_margins().bottom, 7);
  232. }
  233. TEST(PageSetupTest, GetSymmetricalPrintableArea) {
  234. gfx::Rect printable_area = PageSetup::GetSymmetricalPrintableArea(
  235. gfx::Size(612, 792), gfx::Rect(0, 0, 560, 750));
  236. EXPECT_EQ(gfx::Rect(52, 42, 508, 708), printable_area);
  237. printable_area = PageSetup::GetSymmetricalPrintableArea(
  238. gfx::Size(612, 792), gfx::Rect(50, 60, 550, 700));
  239. EXPECT_EQ(gfx::Rect(50, 60, 512, 672), printable_area);
  240. printable_area = PageSetup::GetSymmetricalPrintableArea(
  241. gfx::Size(612, 792), gfx::Rect(-1, 60, 520, 700));
  242. EXPECT_EQ(gfx::Rect(), printable_area);
  243. printable_area = PageSetup::GetSymmetricalPrintableArea(
  244. gfx::Size(612, 792), gfx::Rect(50, -1, 520, 700));
  245. EXPECT_EQ(gfx::Rect(), printable_area);
  246. printable_area = PageSetup::GetSymmetricalPrintableArea(
  247. gfx::Size(612, 792), gfx::Rect(100, 60, 520, 700));
  248. EXPECT_EQ(gfx::Rect(), printable_area);
  249. printable_area = PageSetup::GetSymmetricalPrintableArea(
  250. gfx::Size(612, 792), gfx::Rect(50, 100, 520, 700));
  251. EXPECT_EQ(gfx::Rect(), printable_area);
  252. printable_area = PageSetup::GetSymmetricalPrintableArea(
  253. gfx::Size(612, 792), gfx::Rect(400, 60, 212, 700));
  254. EXPECT_EQ(gfx::Rect(), printable_area);
  255. printable_area = PageSetup::GetSymmetricalPrintableArea(
  256. gfx::Size(612, 792), gfx::Rect(40, 600, 212, 192));
  257. EXPECT_EQ(gfx::Rect(), printable_area);
  258. }
  259. } // namespace printing