rect_unittest.cc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. // Copyright (c) 2013 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 "ui/gfx/geometry/rect.h"
  5. #include <stddef.h>
  6. #include <limits>
  7. #include "build/build_config.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. #include "ui/gfx/geometry/insets.h"
  10. #include "ui/gfx/geometry/rect_conversions.h"
  11. #include "ui/gfx/geometry/test/geometry_util.h"
  12. #if BUILDFLAG(IS_WIN)
  13. #include <windows.h>
  14. #endif
  15. namespace gfx {
  16. constexpr int kMaxInt = std::numeric_limits<int>::max();
  17. constexpr int kMinInt = std::numeric_limits<int>::min();
  18. TEST(RectTest, Contains) {
  19. static const struct ContainsCase {
  20. int rect_x;
  21. int rect_y;
  22. int rect_width;
  23. int rect_height;
  24. int point_x;
  25. int point_y;
  26. bool contained;
  27. } contains_cases[] = {
  28. {0, 0, 10, 10, 0, 0, true},
  29. {0, 0, 10, 10, 5, 5, true},
  30. {0, 0, 10, 10, 9, 9, true},
  31. {0, 0, 10, 10, 5, 10, false},
  32. {0, 0, 10, 10, 10, 5, false},
  33. {0, 0, 10, 10, -1, -1, false},
  34. {0, 0, 10, 10, 50, 50, false},
  35. #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
  36. {0, 0, -10, -10, 0, 0, false},
  37. #endif
  38. };
  39. for (size_t i = 0; i < std::size(contains_cases); ++i) {
  40. const ContainsCase& value = contains_cases[i];
  41. Rect rect(value.rect_x, value.rect_y, value.rect_width, value.rect_height);
  42. EXPECT_EQ(value.contained, rect.Contains(value.point_x, value.point_y));
  43. }
  44. }
  45. TEST(RectTest, Intersects) {
  46. static const struct {
  47. int x1; // rect 1
  48. int y1;
  49. int w1;
  50. int h1;
  51. int x2; // rect 2
  52. int y2;
  53. int w2;
  54. int h2;
  55. bool intersects;
  56. } tests[] = {
  57. { 0, 0, 0, 0, 0, 0, 0, 0, false },
  58. { 0, 0, 0, 0, -10, -10, 20, 20, false },
  59. { -10, 0, 0, 20, 0, -10, 20, 0, false },
  60. { 0, 0, 10, 10, 0, 0, 10, 10, true },
  61. { 0, 0, 10, 10, 10, 10, 10, 10, false },
  62. { 10, 10, 10, 10, 0, 0, 10, 10, false },
  63. { 10, 10, 10, 10, 5, 5, 10, 10, true },
  64. { 10, 10, 10, 10, 15, 15, 10, 10, true },
  65. { 10, 10, 10, 10, 20, 15, 10, 10, false },
  66. { 10, 10, 10, 10, 21, 15, 10, 10, false }
  67. };
  68. for (size_t i = 0; i < std::size(tests); ++i) {
  69. Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
  70. Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
  71. EXPECT_EQ(tests[i].intersects, r1.Intersects(r2));
  72. EXPECT_EQ(tests[i].intersects, r2.Intersects(r1));
  73. }
  74. }
  75. TEST(RectTest, Intersect) {
  76. static const struct {
  77. int x1; // rect 1
  78. int y1;
  79. int w1;
  80. int h1;
  81. int x2; // rect 2
  82. int y2;
  83. int w2;
  84. int h2;
  85. int x3; // rect 3: the union of rects 1 and 2
  86. int y3;
  87. int w3;
  88. int h3;
  89. } tests[] = {
  90. { 0, 0, 0, 0, // zeros
  91. 0, 0, 0, 0,
  92. 0, 0, 0, 0 },
  93. { 0, 0, 4, 4, // equal
  94. 0, 0, 4, 4,
  95. 0, 0, 4, 4 },
  96. { 0, 0, 4, 4, // neighboring
  97. 4, 4, 4, 4,
  98. 0, 0, 0, 0 },
  99. { 0, 0, 4, 4, // overlapping corners
  100. 2, 2, 4, 4,
  101. 2, 2, 2, 2 },
  102. { 0, 0, 4, 4, // T junction
  103. 3, 1, 4, 2,
  104. 3, 1, 1, 2 },
  105. { 3, 0, 2, 2, // gap
  106. 0, 0, 2, 2,
  107. 0, 0, 0, 0 }
  108. };
  109. for (size_t i = 0; i < std::size(tests); ++i) {
  110. Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
  111. Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
  112. Rect r3(tests[i].x3, tests[i].y3, tests[i].w3, tests[i].h3);
  113. EXPECT_EQ(r3, IntersectRects(r1, r2));
  114. }
  115. }
  116. TEST(RectTest, InclusiveIntersect) {
  117. Rect rect(11, 12, 0, 0);
  118. EXPECT_TRUE(rect.InclusiveIntersect(Rect(11, 12, 13, 14)));
  119. EXPECT_EQ(Rect(11, 12, 0, 0), rect);
  120. rect = Rect(11, 12, 13, 14);
  121. EXPECT_TRUE(rect.InclusiveIntersect(Rect(24, 8, 0, 7)));
  122. EXPECT_EQ(Rect(24, 12, 0, 3), rect);
  123. rect = Rect(11, 12, 13, 14);
  124. EXPECT_TRUE(rect.InclusiveIntersect(Rect(9, 15, 4, 0)));
  125. EXPECT_EQ(Rect(11, 15, 2, 0), rect);
  126. rect = Rect(11, 12, 0, 14);
  127. EXPECT_FALSE(rect.InclusiveIntersect(Rect(12, 13, 15, 16)));
  128. EXPECT_EQ(Rect(), rect);
  129. }
  130. TEST(RectTest, Union) {
  131. EXPECT_EQ(Rect(), UnionRects(Rect(), Rect()));
  132. EXPECT_EQ(Rect(1, 2, 3, 4), UnionRects(Rect(1, 2, 3, 4), Rect(1, 2, 3, 4)));
  133. EXPECT_EQ(Rect(0, 0, 8, 10), UnionRects(Rect(0, 0, 3, 4), Rect(3, 4, 5, 6)));
  134. EXPECT_EQ(Rect(0, 0, 8, 10), UnionRects(Rect(3, 4, 5, 6), Rect(0, 0, 3, 4)));
  135. EXPECT_EQ(Rect(0, 1, 3, 8), UnionRects(Rect(0, 1, 3, 4), Rect(0, 5, 3, 4)));
  136. EXPECT_EQ(Rect(0, 1, 10, 11), UnionRects(Rect(0, 1, 3, 4), Rect(4, 5, 6, 7)));
  137. EXPECT_EQ(Rect(0, 1, 10, 11), UnionRects(Rect(4, 5, 6, 7), Rect(0, 1, 3, 4)));
  138. EXPECT_EQ(Rect(2, 3, 4, 5), UnionRects(Rect(8, 9, 0, 2), Rect(2, 3, 4, 5)));
  139. EXPECT_EQ(Rect(2, 3, 4, 5), UnionRects(Rect(2, 3, 4, 5), Rect(8, 9, 2, 0)));
  140. }
  141. TEST(RectTest, UnionEvenIfEmpty) {
  142. EXPECT_EQ(Rect(), UnionRectsEvenIfEmpty(Rect(), Rect()));
  143. EXPECT_EQ(Rect(0, 0, 3, 4), UnionRectsEvenIfEmpty(Rect(), Rect(3, 4, 0, 0)));
  144. EXPECT_EQ(Rect(0, 0, 8, 10),
  145. UnionRectsEvenIfEmpty(Rect(0, 0, 3, 4), Rect(3, 4, 5, 6)));
  146. EXPECT_EQ(Rect(0, 0, 8, 10),
  147. UnionRectsEvenIfEmpty(Rect(3, 4, 5, 6), Rect(0, 0, 3, 4)));
  148. EXPECT_EQ(Rect(2, 3, 6, 8),
  149. UnionRectsEvenIfEmpty(Rect(8, 9, 0, 2), Rect(2, 3, 4, 5)));
  150. EXPECT_EQ(Rect(2, 3, 8, 6),
  151. UnionRectsEvenIfEmpty(Rect(2, 3, 4, 5), Rect(8, 9, 2, 0)));
  152. }
  153. TEST(RectTest, Equals) {
  154. ASSERT_TRUE(Rect(0, 0, 0, 0) == Rect(0, 0, 0, 0));
  155. ASSERT_TRUE(Rect(1, 2, 3, 4) == Rect(1, 2, 3, 4));
  156. ASSERT_FALSE(Rect(0, 0, 0, 0) == Rect(0, 0, 0, 1));
  157. ASSERT_FALSE(Rect(0, 0, 0, 0) == Rect(0, 0, 1, 0));
  158. ASSERT_FALSE(Rect(0, 0, 0, 0) == Rect(0, 1, 0, 0));
  159. ASSERT_FALSE(Rect(0, 0, 0, 0) == Rect(1, 0, 0, 0));
  160. }
  161. TEST(RectTest, AdjustToFit) {
  162. static const struct Test {
  163. int x1; // source
  164. int y1;
  165. int w1;
  166. int h1;
  167. int x2; // target
  168. int y2;
  169. int w2;
  170. int h2;
  171. int x3; // rect 3: results of invoking AdjustToFit
  172. int y3;
  173. int w3;
  174. int h3;
  175. } tests[] = {
  176. { 0, 0, 2, 2,
  177. 0, 0, 2, 2,
  178. 0, 0, 2, 2 },
  179. { 2, 2, 3, 3,
  180. 0, 0, 4, 4,
  181. 1, 1, 3, 3 },
  182. { -1, -1, 5, 5,
  183. 0, 0, 4, 4,
  184. 0, 0, 4, 4 },
  185. { 2, 2, 4, 4,
  186. 0, 0, 3, 3,
  187. 0, 0, 3, 3 },
  188. { 2, 2, 1, 1,
  189. 0, 0, 3, 3,
  190. 2, 2, 1, 1 }
  191. };
  192. for (size_t i = 0; i < std::size(tests); ++i) {
  193. Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
  194. Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
  195. Rect r3(tests[i].x3, tests[i].y3, tests[i].w3, tests[i].h3);
  196. Rect u = r1;
  197. u.AdjustToFit(r2);
  198. EXPECT_EQ(r3, u);
  199. }
  200. }
  201. TEST(RectTest, Subtract) {
  202. Rect result;
  203. // Matching
  204. result = Rect(10, 10, 20, 20);
  205. result.Subtract(Rect(10, 10, 20, 20));
  206. EXPECT_EQ(Rect(0, 0, 0, 0), result);
  207. // Contains
  208. result = Rect(10, 10, 20, 20);
  209. result.Subtract(Rect(5, 5, 30, 30));
  210. EXPECT_EQ(Rect(0, 0, 0, 0), result);
  211. // No intersection
  212. result = Rect(10, 10, 20, 20);
  213. result.Subtract(Rect(30, 30, 30, 30));
  214. EXPECT_EQ(Rect(10, 10, 20, 20), result);
  215. // Not a complete intersection in either direction
  216. result = Rect(10, 10, 20, 20);
  217. result.Subtract(Rect(15, 15, 20, 20));
  218. EXPECT_EQ(Rect(10, 10, 20, 20), result);
  219. // Complete intersection in the x-direction, top edge is fully covered.
  220. result = Rect(10, 10, 20, 20);
  221. result.Subtract(Rect(10, 15, 20, 20));
  222. EXPECT_EQ(Rect(10, 10, 20, 5), result);
  223. // Complete intersection in the x-direction, top edge is fully covered.
  224. result = Rect(10, 10, 20, 20);
  225. result.Subtract(Rect(5, 15, 30, 20));
  226. EXPECT_EQ(Rect(10, 10, 20, 5), result);
  227. // Complete intersection in the x-direction, bottom edge is fully covered.
  228. result = Rect(10, 10, 20, 20);
  229. result.Subtract(Rect(5, 5, 30, 20));
  230. EXPECT_EQ(Rect(10, 25, 20, 5), result);
  231. // Complete intersection in the x-direction, none of the edges is fully
  232. // covered.
  233. result = Rect(10, 10, 20, 20);
  234. result.Subtract(Rect(5, 15, 30, 1));
  235. EXPECT_EQ(Rect(10, 10, 20, 20), result);
  236. // Complete intersection in the y-direction, left edge is fully covered.
  237. result = Rect(10, 10, 20, 20);
  238. result.Subtract(Rect(10, 10, 10, 30));
  239. EXPECT_EQ(Rect(20, 10, 10, 20), result);
  240. // Complete intersection in the y-direction, left edge is fully covered.
  241. result = Rect(10, 10, 20, 20);
  242. result.Subtract(Rect(5, 5, 20, 30));
  243. EXPECT_EQ(Rect(25, 10, 5, 20), result);
  244. // Complete intersection in the y-direction, right edge is fully covered.
  245. result = Rect(10, 10, 20, 20);
  246. result.Subtract(Rect(20, 5, 20, 30));
  247. EXPECT_EQ(Rect(10, 10, 10, 20), result);
  248. // Complete intersection in the y-direction, none of the edges is fully
  249. // covered.
  250. result = Rect(10, 10, 20, 20);
  251. result.Subtract(Rect(15, 5, 1, 30));
  252. EXPECT_EQ(Rect(10, 10, 20, 20), result);
  253. }
  254. TEST(RectTest, IsEmpty) {
  255. EXPECT_TRUE(Rect(0, 0, 0, 0).IsEmpty());
  256. EXPECT_TRUE(Rect(0, 0, 0, 0).size().IsEmpty());
  257. EXPECT_TRUE(Rect(0, 0, 10, 0).IsEmpty());
  258. EXPECT_TRUE(Rect(0, 0, 10, 0).size().IsEmpty());
  259. EXPECT_TRUE(Rect(0, 0, 0, 10).IsEmpty());
  260. EXPECT_TRUE(Rect(0, 0, 0, 10).size().IsEmpty());
  261. EXPECT_FALSE(Rect(0, 0, 10, 10).IsEmpty());
  262. EXPECT_FALSE(Rect(0, 0, 10, 10).size().IsEmpty());
  263. }
  264. TEST(RectTest, SplitVertically) {
  265. Rect left_half, right_half;
  266. // Splitting when origin is (0, 0).
  267. Rect(0, 0, 20, 20).SplitVertically(&left_half, &right_half);
  268. EXPECT_TRUE(left_half == Rect(0, 0, 10, 20));
  269. EXPECT_TRUE(right_half == Rect(10, 0, 10, 20));
  270. // Splitting when origin is arbitrary.
  271. Rect(10, 10, 20, 10).SplitVertically(&left_half, &right_half);
  272. EXPECT_TRUE(left_half == Rect(10, 10, 10, 10));
  273. EXPECT_TRUE(right_half == Rect(20, 10, 10, 10));
  274. // Splitting a rectangle of zero width.
  275. Rect(10, 10, 0, 10).SplitVertically(&left_half, &right_half);
  276. EXPECT_TRUE(left_half == Rect(10, 10, 0, 10));
  277. EXPECT_TRUE(right_half == Rect(10, 10, 0, 10));
  278. // Splitting a rectangle of odd width.
  279. Rect(10, 10, 5, 10).SplitVertically(&left_half, &right_half);
  280. EXPECT_TRUE(left_half == Rect(10, 10, 2, 10));
  281. EXPECT_TRUE(right_half == Rect(12, 10, 3, 10));
  282. }
  283. TEST(RectTest, CenterPoint) {
  284. Point center;
  285. // When origin is (0, 0).
  286. center = Rect(0, 0, 20, 20).CenterPoint();
  287. EXPECT_TRUE(center == Point(10, 10));
  288. // When origin is even.
  289. center = Rect(10, 10, 20, 20).CenterPoint();
  290. EXPECT_TRUE(center == Point(20, 20));
  291. // When origin is odd.
  292. center = Rect(11, 11, 20, 20).CenterPoint();
  293. EXPECT_TRUE(center == Point(21, 21));
  294. // When 0 width or height.
  295. center = Rect(10, 10, 0, 20).CenterPoint();
  296. EXPECT_TRUE(center == Point(10, 20));
  297. center = Rect(10, 10, 20, 0).CenterPoint();
  298. EXPECT_TRUE(center == Point(20, 10));
  299. // When an odd size.
  300. center = Rect(10, 10, 21, 21).CenterPoint();
  301. EXPECT_TRUE(center == Point(20, 20));
  302. // When an odd size and position.
  303. center = Rect(11, 11, 21, 21).CenterPoint();
  304. EXPECT_TRUE(center == Point(21, 21));
  305. }
  306. TEST(RectTest, SharesEdgeWith) {
  307. Rect r(2, 3, 4, 5);
  308. // Must be non-overlapping
  309. EXPECT_FALSE(r.SharesEdgeWith(r));
  310. Rect just_above(2, 1, 4, 2);
  311. Rect just_below(2, 8, 4, 2);
  312. Rect just_left(0, 3, 2, 5);
  313. Rect just_right(6, 3, 2, 5);
  314. EXPECT_TRUE(r.SharesEdgeWith(just_above));
  315. EXPECT_TRUE(r.SharesEdgeWith(just_below));
  316. EXPECT_TRUE(r.SharesEdgeWith(just_left));
  317. EXPECT_TRUE(r.SharesEdgeWith(just_right));
  318. // Wrong placement
  319. Rect same_height_no_edge(0, 0, 1, 5);
  320. Rect same_width_no_edge(0, 0, 4, 1);
  321. EXPECT_FALSE(r.SharesEdgeWith(same_height_no_edge));
  322. EXPECT_FALSE(r.SharesEdgeWith(same_width_no_edge));
  323. Rect just_above_no_edge(2, 1, 5, 2); // too wide
  324. Rect just_below_no_edge(2, 8, 3, 2); // too narrow
  325. Rect just_left_no_edge(0, 3, 2, 6); // too tall
  326. Rect just_right_no_edge(6, 3, 2, 4); // too short
  327. EXPECT_FALSE(r.SharesEdgeWith(just_above_no_edge));
  328. EXPECT_FALSE(r.SharesEdgeWith(just_below_no_edge));
  329. EXPECT_FALSE(r.SharesEdgeWith(just_left_no_edge));
  330. EXPECT_FALSE(r.SharesEdgeWith(just_right_no_edge));
  331. }
  332. static void TestScaleRectOverflowClamp(Rect (*function)(const Rect&,
  333. float,
  334. float)) {
  335. // The whole rect is scaled out of kMinInt.
  336. Rect xy_underflow1(-100000, -123456, 10, 20);
  337. EXPECT_EQ(Rect(kMinInt, kMinInt, 0, 0),
  338. function(xy_underflow1, 100000, 100000));
  339. // This rect's right/bottom is 0. The origin overflows, and is clamped to
  340. // -kMaxInt (instead of kMinInt) to keep width/height not overflowing.
  341. Rect xy_underflow2(-100000, -123456, 100000, 123456);
  342. EXPECT_EQ(Rect(-kMaxInt, -kMaxInt, kMaxInt, kMaxInt),
  343. function(xy_underflow2, 100000, 100000));
  344. // A location overflow means that width/right and bottom/top also
  345. // overflow so need to be clamped.
  346. Rect xy_overflow(100000, 123456, 10, 20);
  347. EXPECT_EQ(Rect(kMaxInt, kMaxInt, 0, 0),
  348. function(xy_overflow, 100000, 100000));
  349. // In practice all rects are clamped to 0 width / 0 height so
  350. // negative sizes don't matter, but try this for the sake of testing.
  351. Rect size_underflow(-1, -2, 100000, 100000);
  352. EXPECT_EQ(Rect(100000, 200000, 0, 0),
  353. function(size_underflow, -100000, -100000));
  354. Rect size_overflow(-1, -2, 123456, 234567);
  355. EXPECT_EQ(Rect(-100000, -200000, kMaxInt, kMaxInt),
  356. function(size_overflow, 100000, 100000));
  357. // Verify width/right gets clamped properly too if x/y positive.
  358. Rect size_overflow2(1, 2, 123456, 234567);
  359. EXPECT_EQ(Rect(100000, 200000, kMaxInt - 100000, kMaxInt - 200000),
  360. function(size_overflow2, 100000, 100000));
  361. constexpr float kMaxIntAsFloat = static_cast<float>(kMaxInt);
  362. Rect max_origin_rect(kMaxInt, kMaxInt, kMaxInt, kMaxInt);
  363. // width/height of max_origin_rect has already been clamped to 0.
  364. EXPECT_EQ(Rect(kMaxInt, kMaxInt, 0, 0), max_origin_rect);
  365. EXPECT_EQ(Rect(kMaxInt, kMaxInt, 0, 0),
  366. function(max_origin_rect, kMaxIntAsFloat, kMaxIntAsFloat));
  367. Rect max_size_rect1(0, 0, kMaxInt, kMaxInt);
  368. // Max sized rect can't be scaled up any further in any dimension.
  369. EXPECT_EQ(max_size_rect1, function(max_size_rect1, 2, 3.5));
  370. EXPECT_EQ(max_size_rect1,
  371. function(max_size_rect1, kMaxIntAsFloat, kMaxIntAsFloat));
  372. // Max sized ret scaled by negative scale is an empty rect.
  373. EXPECT_EQ(Rect(), function(max_size_rect1, kMinInt, kMinInt));
  374. Rect max_size_rect2(-kMaxInt, -kMaxInt, kMaxInt, kMaxInt);
  375. EXPECT_EQ(max_size_rect2, function(max_size_rect2, 2, 3.5));
  376. EXPECT_EQ(max_size_rect2,
  377. function(max_size_rect2, kMaxIntAsFloat, kMaxIntAsFloat));
  378. EXPECT_EQ(Rect(kMaxInt, kMaxInt, 0, 0),
  379. function(max_size_rect2, kMinInt, kMinInt));
  380. }
  381. TEST(RectTest, ScaleToEnclosedRect) {
  382. EXPECT_EQ(Rect(), ScaleToEnclosedRect(Rect(), 5.f));
  383. EXPECT_EQ(Rect(5, 5, 5, 5), ScaleToEnclosedRect(Rect(1, 1, 1, 1), 5.f));
  384. EXPECT_EQ(Rect(-5, -5, 0, 0), ScaleToEnclosedRect(Rect(-1, -1, 0, 0), 5.f));
  385. EXPECT_EQ(Rect(5, -5, 0, 5), ScaleToEnclosedRect(Rect(1, -1, 0, 1), 5.f));
  386. EXPECT_EQ(Rect(-5, 5, 5, 0), ScaleToEnclosedRect(Rect(-1, 1, 1, 0), 5.f));
  387. EXPECT_EQ(Rect(2, 3, 4, 6), ScaleToEnclosedRect(Rect(1, 2, 3, 4), 1.5f));
  388. EXPECT_EQ(Rect(-1, -3, 0, 0), ScaleToEnclosedRect(Rect(-1, -2, 0, 0), 1.5f));
  389. EXPECT_EQ(Rect(1, 2, 2, 1), ScaleToEnclosedRect(Rect(2, 4, 9, 8), 0.3f));
  390. TestScaleRectOverflowClamp(ScaleToEnclosedRect);
  391. }
  392. TEST(RectTest, ScaleToEnclosingRect) {
  393. EXPECT_EQ(Rect(), ScaleToEnclosingRect(Rect(), 5.f));
  394. EXPECT_EQ(Rect(5, 5, 5, 5), ScaleToEnclosingRect(Rect(1, 1, 1, 1), 5.f));
  395. EXPECT_EQ(Rect(-5, -5, 0, 0), ScaleToEnclosingRect(Rect(-1, -1, 0, 0), 5.f));
  396. EXPECT_EQ(Rect(5, -5, 0, 5), ScaleToEnclosingRect(Rect(1, -1, 0, 1), 5.f));
  397. EXPECT_EQ(Rect(-5, 5, 5, 0), ScaleToEnclosingRect(Rect(-1, 1, 1, 0), 5.f));
  398. EXPECT_EQ(Rect(1, 3, 5, 6), ScaleToEnclosingRect(Rect(1, 2, 3, 4), 1.5f));
  399. EXPECT_EQ(Rect(-2, -3, 0, 0), ScaleToEnclosingRect(Rect(-1, -2, 0, 0), 1.5f));
  400. EXPECT_EQ(Rect(0, 1, 4, 3), ScaleToEnclosingRect(Rect(2, 4, 9, 8), 0.3f));
  401. TestScaleRectOverflowClamp(ScaleToEnclosingRect);
  402. }
  403. TEST(RectTest, ScaleToRoundedRect) {
  404. EXPECT_EQ(Rect(), ScaleToRoundedRect(Rect(), 5.f));
  405. EXPECT_EQ(Rect(5, 5, 5, 5), ScaleToRoundedRect(Rect(1, 1, 1, 1), 5.f));
  406. EXPECT_EQ(Rect(-5, -5, 0, 0), ScaleToRoundedRect(Rect(-1, -1, 0, 0), 5.f));
  407. EXPECT_EQ(Rect(5, -5, 0, 5), ScaleToRoundedRect(Rect(1, -1, 0, 1), 5.f));
  408. EXPECT_EQ(Rect(-5, 5, 5, 0), ScaleToRoundedRect(Rect(-1, 1, 1, 0), 5.f));
  409. EXPECT_EQ(Rect(2, 3, 4, 6), ScaleToRoundedRect(Rect(1, 2, 3, 4), 1.5f));
  410. EXPECT_EQ(Rect(-2, -3, 0, 0), ScaleToRoundedRect(Rect(-1, -2, 0, 0), 1.5f));
  411. EXPECT_EQ(Rect(1, 1, 2, 3), ScaleToRoundedRect(Rect(2, 4, 9, 8), 0.3f));
  412. TestScaleRectOverflowClamp(ScaleToRoundedRect);
  413. }
  414. #if BUILDFLAG(IS_WIN)
  415. TEST(RectTest, ConstructAndAssign) {
  416. const RECT rect_1 = { 0, 0, 10, 10 };
  417. const RECT rect_2 = { 0, 0, -10, -10 };
  418. Rect test1(rect_1);
  419. Rect test2(rect_2);
  420. }
  421. #endif
  422. TEST(RectTest, BoundingRect) {
  423. struct {
  424. Point a;
  425. Point b;
  426. Rect expected;
  427. } int_tests[] = {
  428. // If point B dominates A, then A should be the origin.
  429. { Point(4, 6), Point(4, 6), Rect(4, 6, 0, 0) },
  430. { Point(4, 6), Point(8, 6), Rect(4, 6, 4, 0) },
  431. { Point(4, 6), Point(4, 9), Rect(4, 6, 0, 3) },
  432. { Point(4, 6), Point(8, 9), Rect(4, 6, 4, 3) },
  433. // If point A dominates B, then B should be the origin.
  434. { Point(4, 6), Point(4, 6), Rect(4, 6, 0, 0) },
  435. { Point(8, 6), Point(4, 6), Rect(4, 6, 4, 0) },
  436. { Point(4, 9), Point(4, 6), Rect(4, 6, 0, 3) },
  437. { Point(8, 9), Point(4, 6), Rect(4, 6, 4, 3) },
  438. // If neither point dominates, then the origin is a combination of the two.
  439. { Point(4, 6), Point(6, 4), Rect(4, 4, 2, 2) },
  440. { Point(-4, -6), Point(-6, -4), Rect(-6, -6, 2, 2) },
  441. { Point(-4, 6), Point(6, -4), Rect(-4, -4, 10, 10) },
  442. };
  443. for (size_t i = 0; i < std::size(int_tests); ++i) {
  444. Rect actual = BoundingRect(int_tests[i].a, int_tests[i].b);
  445. EXPECT_EQ(int_tests[i].expected, actual);
  446. }
  447. }
  448. TEST(RectTest, Offset) {
  449. Rect i(1, 2, 3, 4);
  450. EXPECT_EQ(Rect(2, 1, 3, 4), (i + Vector2d(1, -1)));
  451. EXPECT_EQ(Rect(2, 1, 3, 4), (Vector2d(1, -1) + i));
  452. i += Vector2d(1, -1);
  453. EXPECT_EQ(Rect(2, 1, 3, 4), i);
  454. EXPECT_EQ(Rect(1, 2, 3, 4), (i - Vector2d(1, -1)));
  455. i -= Vector2d(1, -1);
  456. EXPECT_EQ(Rect(1, 2, 3, 4), i);
  457. i.Offset(2, -2);
  458. EXPECT_EQ(Rect(3, 0, 3, 4), i);
  459. EXPECT_EQ(Rect(kMaxInt - 2, kMaxInt - 2, 2, 2),
  460. (Rect(0, 0, kMaxInt - 2, kMaxInt - 2) +
  461. Vector2d(kMaxInt - 2, kMaxInt - 2)));
  462. EXPECT_EQ(Rect(kMaxInt - 2, kMaxInt - 2, 2, 2),
  463. (Rect(0, 0, kMaxInt - 2, kMaxInt - 2) -
  464. Vector2d(2 - kMaxInt, 2 - kMaxInt)));
  465. }
  466. TEST(RectTest, Corners) {
  467. Rect i(1, 2, 3, 4);
  468. EXPECT_EQ(Point(1, 2), i.origin());
  469. EXPECT_EQ(Point(4, 2), i.top_right());
  470. EXPECT_EQ(Point(1, 6), i.bottom_left());
  471. EXPECT_EQ(Point(4, 6), i.bottom_right());
  472. }
  473. TEST(RectTest, Centers) {
  474. Rect i(10, 20, 30, 40);
  475. EXPECT_EQ(Point(10, 40), i.left_center());
  476. EXPECT_EQ(Point(25, 20), i.top_center());
  477. EXPECT_EQ(Point(40, 40), i.right_center());
  478. EXPECT_EQ(Point(25, 60), i.bottom_center());
  479. }
  480. TEST(RectTest, Transpose) {
  481. Rect i(10, 20, 30, 40);
  482. i.Transpose();
  483. EXPECT_EQ(Rect(20, 10, 40, 30), i);
  484. }
  485. TEST(RectTest, ManhattanDistanceToPoint) {
  486. Rect i(1, 2, 3, 4);
  487. EXPECT_EQ(0, i.ManhattanDistanceToPoint(Point(1, 2)));
  488. EXPECT_EQ(0, i.ManhattanDistanceToPoint(Point(4, 6)));
  489. EXPECT_EQ(0, i.ManhattanDistanceToPoint(Point(2, 4)));
  490. EXPECT_EQ(3, i.ManhattanDistanceToPoint(Point(0, 0)));
  491. EXPECT_EQ(2, i.ManhattanDistanceToPoint(Point(2, 0)));
  492. EXPECT_EQ(3, i.ManhattanDistanceToPoint(Point(5, 0)));
  493. EXPECT_EQ(1, i.ManhattanDistanceToPoint(Point(5, 4)));
  494. EXPECT_EQ(3, i.ManhattanDistanceToPoint(Point(5, 8)));
  495. EXPECT_EQ(2, i.ManhattanDistanceToPoint(Point(3, 8)));
  496. EXPECT_EQ(2, i.ManhattanDistanceToPoint(Point(0, 7)));
  497. EXPECT_EQ(1, i.ManhattanDistanceToPoint(Point(0, 3)));
  498. }
  499. TEST(RectTest, ManhattanInternalDistance) {
  500. Rect i(0, 0, 400, 400);
  501. EXPECT_EQ(0, i.ManhattanInternalDistance(Rect(-1, 0, 2, 1)));
  502. EXPECT_EQ(1, i.ManhattanInternalDistance(Rect(400, 0, 1, 400)));
  503. EXPECT_EQ(2, i.ManhattanInternalDistance(Rect(-100, -100, 100, 100)));
  504. EXPECT_EQ(2, i.ManhattanInternalDistance(Rect(-101, 100, 100, 100)));
  505. EXPECT_EQ(4, i.ManhattanInternalDistance(Rect(-101, -101, 100, 100)));
  506. EXPECT_EQ(435, i.ManhattanInternalDistance(Rect(630, 603, 100, 100)));
  507. }
  508. TEST(RectTest, IntegerOverflow) {
  509. int limit = std::numeric_limits<int>::max();
  510. int min_limit = std::numeric_limits<int>::min();
  511. int expected_thickness = 10;
  512. int large_number = limit - expected_thickness;
  513. Rect height_overflow(0, large_number, 100, 100);
  514. EXPECT_EQ(large_number, height_overflow.y());
  515. EXPECT_EQ(expected_thickness, height_overflow.height());
  516. Rect width_overflow(large_number, 0, 100, 100);
  517. EXPECT_EQ(large_number, width_overflow.x());
  518. EXPECT_EQ(expected_thickness, width_overflow.width());
  519. Rect size_height_overflow(Point(0, large_number), Size(100, 100));
  520. EXPECT_EQ(large_number, size_height_overflow.y());
  521. EXPECT_EQ(expected_thickness, size_height_overflow.height());
  522. Rect size_width_overflow(Point(large_number, 0), Size(100, 100));
  523. EXPECT_EQ(large_number, size_width_overflow.x());
  524. EXPECT_EQ(expected_thickness, size_width_overflow.width());
  525. Rect set_height_overflow(0, large_number, 100, 5);
  526. EXPECT_EQ(5, set_height_overflow.height());
  527. set_height_overflow.set_height(100);
  528. EXPECT_EQ(expected_thickness, set_height_overflow.height());
  529. Rect set_y_overflow(100, 100, 100, 100);
  530. EXPECT_EQ(100, set_y_overflow.height());
  531. set_y_overflow.set_y(large_number);
  532. EXPECT_EQ(expected_thickness, set_y_overflow.height());
  533. Rect set_width_overflow(large_number, 0, 5, 100);
  534. EXPECT_EQ(5, set_width_overflow.width());
  535. set_width_overflow.set_width(100);
  536. EXPECT_EQ(expected_thickness, set_width_overflow.width());
  537. Rect set_x_overflow(100, 100, 100, 100);
  538. EXPECT_EQ(100, set_x_overflow.width());
  539. set_x_overflow.set_x(large_number);
  540. EXPECT_EQ(expected_thickness, set_x_overflow.width());
  541. Point large_offset(large_number, large_number);
  542. Size size(100, 100);
  543. Size expected_size(10, 10);
  544. Rect set_origin_overflow(100, 100, 100, 100);
  545. EXPECT_EQ(size, set_origin_overflow.size());
  546. set_origin_overflow.set_origin(large_offset);
  547. EXPECT_EQ(large_offset, set_origin_overflow.origin());
  548. EXPECT_EQ(expected_size, set_origin_overflow.size());
  549. Rect set_size_overflow(large_number, large_number, 5, 5);
  550. EXPECT_EQ(Size(5, 5), set_size_overflow.size());
  551. set_size_overflow.set_size(size);
  552. EXPECT_EQ(large_offset, set_size_overflow.origin());
  553. EXPECT_EQ(expected_size, set_size_overflow.size());
  554. Rect set_rect_overflow;
  555. set_rect_overflow.SetRect(large_number, large_number, 100, 100);
  556. EXPECT_EQ(large_offset, set_rect_overflow.origin());
  557. EXPECT_EQ(expected_size, set_rect_overflow.size());
  558. // Insetting an empty rect, but the total inset (left + right) could overflow.
  559. Rect inset_overflow;
  560. inset_overflow.Inset(Insets::TLBR(large_number, large_number, 100, 100));
  561. EXPECT_EQ(large_offset, inset_overflow.origin());
  562. EXPECT_EQ(Size(), inset_overflow.size());
  563. // Insetting where the total inset (width - left - right) could overflow.
  564. // Also, this insetting by the min limit in all directions cannot
  565. // represent width() without overflow, so that will also clamp.
  566. Rect inset_overflow2;
  567. inset_overflow2.Inset(min_limit);
  568. EXPECT_EQ(inset_overflow2, Rect(min_limit, min_limit, limit, limit));
  569. // Insetting where the width shouldn't change, but if the insets operations
  570. // clamped in the wrong order, e.g. ((width - left) - right) vs (width - (left
  571. // + right)) then this will not work properly. This is the proper order,
  572. // as if left + right overflows, the width cannot be decreased by more than
  573. // max int anyway. Additionally, if left + right underflows, it cannot be
  574. // increased by more then max int.
  575. Rect inset_overflow3(0, 0, limit, limit);
  576. inset_overflow3.Inset(Insets::TLBR(-100, -100, 100, 100));
  577. EXPECT_EQ(inset_overflow3, Rect(-100, -100, limit, limit));
  578. Rect inset_overflow4(-1000, -1000, limit, limit);
  579. inset_overflow4.Inset(Insets::TLBR(100, 100, -100, -100));
  580. EXPECT_EQ(inset_overflow4, Rect(-900, -900, limit, limit));
  581. Rect offset_overflow(0, 0, 100, 100);
  582. offset_overflow.Offset(large_number, large_number);
  583. EXPECT_EQ(large_offset, offset_overflow.origin());
  584. EXPECT_EQ(expected_size, offset_overflow.size());
  585. Rect operator_overflow(0, 0, 100, 100);
  586. operator_overflow += Vector2d(large_number, large_number);
  587. EXPECT_EQ(large_offset, operator_overflow.origin());
  588. EXPECT_EQ(expected_size, operator_overflow.size());
  589. Rect origin_maxint(limit, limit, limit, limit);
  590. EXPECT_EQ(origin_maxint, Rect(Point(limit, limit), Size()));
  591. // Expect a rect at the origin and a rect whose right/bottom is maxint
  592. // create a rect that extends from 0..maxint in both extents.
  593. {
  594. Rect origin_small(0, 0, 100, 100);
  595. Rect big_clamped(50, 50, limit, limit);
  596. EXPECT_EQ(big_clamped.right(), limit);
  597. Rect unioned = UnionRects(origin_small, big_clamped);
  598. Rect rect_limit(0, 0, limit, limit);
  599. EXPECT_EQ(unioned, rect_limit);
  600. }
  601. // Expect a rect that would overflow width (but not right) to be clamped
  602. // and to have maxint extents after unioning.
  603. {
  604. Rect small(-500, -400, 100, 100);
  605. Rect big(-400, -500, limit, limit);
  606. // Technically, this should be limit + 100 width, but will clamp to maxint.
  607. EXPECT_EQ(UnionRects(small, big), Rect(-500, -500, limit, limit));
  608. }
  609. // Expect a rect that would overflow right *and* width to be clamped.
  610. {
  611. Rect clamped(500, 500, limit, limit);
  612. Rect positive_origin(100, 100, 500, 500);
  613. // Ideally, this should be (100, 100, limit + 400, limit + 400).
  614. // However, width overflows and would be clamped to limit, but right
  615. // overflows too and so will be clamped to limit - 100.
  616. Rect expected_rect(100, 100, limit - 100, limit - 100);
  617. EXPECT_EQ(UnionRects(clamped, positive_origin), expected_rect);
  618. }
  619. // Unioning a left=minint rect with a right=maxint rect.
  620. // We can't represent both ends of the spectrum in the same rect.
  621. // Make sure we keep the most useful area.
  622. {
  623. int part_limit = min_limit / 3;
  624. Rect left_minint(min_limit, min_limit, 1, 1);
  625. Rect right_maxint(limit - 1, limit - 1, limit, limit);
  626. Rect expected_rect(part_limit, part_limit, 2 * part_limit, 2 * part_limit);
  627. Rect result = UnionRects(left_minint, right_maxint);
  628. // The result should be maximally big.
  629. EXPECT_EQ(limit, result.height());
  630. EXPECT_EQ(limit, result.width());
  631. // The result should include the area near the origin.
  632. EXPECT_GT(-part_limit, result.x());
  633. EXPECT_LT(part_limit, result.right());
  634. EXPECT_GT(-part_limit, result.y());
  635. EXPECT_LT(part_limit, result.bottom());
  636. // More succinctly, but harder to read in the results.
  637. EXPECT_TRUE(UnionRects(left_minint, right_maxint).Contains(expected_rect));
  638. }
  639. }
  640. TEST(RectTest, Inset) {
  641. Rect r(10, 20, 30, 40);
  642. r.Inset(0);
  643. EXPECT_EQ(Rect(10, 20, 30, 40), r);
  644. r.Inset(1);
  645. EXPECT_EQ(Rect(11, 21, 28, 38), r);
  646. r.Inset(-1);
  647. EXPECT_EQ(Rect(10, 20, 30, 40), r);
  648. r.Inset(Insets::VH(2, 1));
  649. EXPECT_EQ(Rect(11, 22, 28, 36), r);
  650. r.Inset(Insets::VH(-2, -1));
  651. EXPECT_EQ(Rect(10, 20, 30, 40), r);
  652. // The parameters are left, top, right, bottom.
  653. r.Inset(Insets::TLBR(2, 1, 4, 3));
  654. EXPECT_EQ(Rect(11, 22, 26, 34), r);
  655. r.Inset(Insets::TLBR(-2, -1, -4, -3));
  656. EXPECT_EQ(Rect(10, 20, 30, 40), r);
  657. r.Inset(Insets::TLBR(1, 2, 3, 4));
  658. EXPECT_EQ(Rect(12, 21, 24, 36), r);
  659. r.Inset(Insets::TLBR(-1, -2, -3, -4));
  660. EXPECT_EQ(Rect(10, 20, 30, 40), r);
  661. }
  662. TEST(RectTest, Outset) {
  663. Rect r(10, 20, 30, 40);
  664. r.Outset(0);
  665. EXPECT_EQ(Rect(10, 20, 30, 40), r);
  666. r.Outset(1);
  667. EXPECT_EQ(Rect(9, 19, 32, 42), r);
  668. r.Outset(-1);
  669. EXPECT_EQ(Rect(10, 20, 30, 40), r);
  670. r.Outset(Outsets::VH(2, 1));
  671. EXPECT_EQ(Rect(9, 18, 32, 44), r);
  672. r.Outset(Outsets::VH(-2, -1));
  673. EXPECT_EQ(Rect(10, 20, 30, 40), r);
  674. r.Outset(Outsets::TLBR(2, 1, 4, 3));
  675. EXPECT_EQ(Rect(9, 18, 34, 46), r);
  676. r.Outset(Outsets::TLBR(-2, -1, -4, -3));
  677. EXPECT_EQ(Rect(10, 20, 30, 40), r);
  678. }
  679. TEST(RectTest, InsetOutsetClamped) {
  680. Rect r(10, 20, 30, 40);
  681. r.Inset(18);
  682. EXPECT_EQ(Rect(28, 38, 0, 4), r);
  683. r.Inset(-18);
  684. EXPECT_EQ(Rect(10, 20, 36, 40), r);
  685. r.Inset(Insets::VH(30, 15));
  686. EXPECT_EQ(Rect(25, 50, 6, 0), r);
  687. r.Inset(Insets::VH(-30, -15));
  688. EXPECT_EQ(Rect(10, 20, 36, 60), r);
  689. r.Inset(Insets::TLBR(30, 20, 50, 40));
  690. EXPECT_EQ(Rect(30, 50, 0, 0), r);
  691. r.Inset(Insets::TLBR(-30, -20, -50, -40));
  692. EXPECT_EQ(Rect(10, 20, 60, 80), r);
  693. r.Outset(kMaxInt);
  694. EXPECT_EQ(Rect(10 - kMaxInt, 20 - kMaxInt, kMaxInt, kMaxInt), r);
  695. r.Outset(Outsets().set_top_bottom(kMaxInt, kMaxInt));
  696. EXPECT_EQ(Rect(10 - kMaxInt, kMinInt, kMaxInt, kMaxInt), r);
  697. r.Outset(Outsets().set_right(kMaxInt).set_top(kMaxInt));
  698. EXPECT_EQ(Rect(10 - kMaxInt, kMinInt, kMaxInt, kMaxInt), r);
  699. r.Outset(Outsets().set_left_right(kMaxInt, kMaxInt));
  700. EXPECT_EQ(Rect(kMinInt, kMinInt, kMaxInt, kMaxInt), r);
  701. }
  702. TEST(RectTest, SetByBounds) {
  703. Rect r;
  704. r.SetByBounds(1, 2, 30, 40);
  705. EXPECT_EQ(Rect(1, 2, 29, 38), r);
  706. r.SetByBounds(30, 40, 1, 2);
  707. EXPECT_EQ(Rect(30, 40, 0, 0), r);
  708. r.SetByBounds(0, 0, kMaxInt, kMaxInt);
  709. EXPECT_EQ(Rect(0, 0, kMaxInt, kMaxInt), r);
  710. r.SetByBounds(-1, -1, kMaxInt, kMaxInt);
  711. EXPECT_EQ(Rect(-1, -1, kMaxInt, kMaxInt), r);
  712. r.SetByBounds(1, 1, kMaxInt, kMaxInt);
  713. EXPECT_EQ(Rect(1, 1, kMaxInt - 1, kMaxInt - 1), r);
  714. r.SetByBounds(kMinInt, kMinInt, 0, 0);
  715. EXPECT_EQ(Rect(kMinInt + 1, kMinInt + 1, kMaxInt, kMaxInt), r);
  716. r.SetByBounds(kMinInt, kMinInt, 1, 1);
  717. EXPECT_EQ(Rect(kMinInt + 2, kMinInt + 2, kMaxInt, kMaxInt), r);
  718. r.SetByBounds(kMinInt, kMinInt, -1, -1);
  719. EXPECT_EQ(Rect(kMinInt, kMinInt, kMaxInt, kMaxInt), r);
  720. r.SetByBounds(kMinInt, kMinInt, kMaxInt, kMaxInt);
  721. EXPECT_EQ(Rect(kMinInt / 2 - 1, kMinInt / 2 - 1, kMaxInt, kMaxInt), r);
  722. }
  723. TEST(RectTest, MaximumCoveredRect) {
  724. // X aligned and intersect: unite.
  725. EXPECT_EQ(Rect(10, 20, 30, 60),
  726. MaximumCoveredRect(Rect(10, 20, 30, 40), Rect(10, 30, 30, 50)));
  727. // X aligned and adjacent: unite.
  728. EXPECT_EQ(Rect(10, 20, 30, 90),
  729. MaximumCoveredRect(Rect(10, 20, 30, 40), Rect(10, 60, 30, 50)));
  730. // X aligned and separate: choose the bigger one.
  731. EXPECT_EQ(Rect(10, 61, 30, 50),
  732. MaximumCoveredRect(Rect(10, 20, 30, 40), Rect(10, 61, 30, 50)));
  733. // Y aligned and intersect: unite.
  734. EXPECT_EQ(Rect(10, 20, 60, 40),
  735. MaximumCoveredRect(Rect(10, 20, 30, 40), Rect(30, 20, 40, 40)));
  736. // Y aligned and adjacent: unite.
  737. EXPECT_EQ(Rect(10, 20, 70, 40),
  738. MaximumCoveredRect(Rect(10, 20, 30, 40), Rect(40, 20, 40, 40)));
  739. // Y aligned and separate: choose the bigger one.
  740. EXPECT_EQ(Rect(41, 20, 40, 40),
  741. MaximumCoveredRect(Rect(10, 20, 30, 40), Rect(41, 20, 40, 40)));
  742. // Get the biggest expanded intersection.
  743. EXPECT_EQ(Rect(0, 0, 9, 19),
  744. MaximumCoveredRect(Rect(0, 0, 10, 10), Rect(0, 9, 9, 10)));
  745. EXPECT_EQ(Rect(0, 0, 19, 9),
  746. MaximumCoveredRect(Rect(0, 0, 10, 10), Rect(9, 0, 10, 9)));
  747. // Otherwise choose the bigger one.
  748. EXPECT_EQ(Rect(20, 30, 40, 50),
  749. MaximumCoveredRect(Rect(10, 20, 30, 40), Rect(20, 30, 40, 50)));
  750. EXPECT_EQ(Rect(10, 20, 40, 50),
  751. MaximumCoveredRect(Rect(10, 20, 40, 50), Rect(20, 30, 30, 40)));
  752. EXPECT_EQ(Rect(10, 20, 40, 50),
  753. MaximumCoveredRect(Rect(10, 20, 40, 50), Rect(20, 30, 40, 50)));
  754. }
  755. } // namespace gfx