insets_unittest.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // Copyright (c) 2009 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/insets.h"
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. #include "ui/gfx/geometry/outsets.h"
  7. #include "ui/gfx/geometry/rect.h"
  8. #include "ui/gfx/geometry/size.h"
  9. #include "ui/gfx/geometry/vector2d.h"
  10. namespace gfx {
  11. TEST(InsetsTest, Default) {
  12. Insets insets;
  13. EXPECT_EQ(0, insets.top());
  14. EXPECT_EQ(0, insets.left());
  15. EXPECT_EQ(0, insets.bottom());
  16. EXPECT_EQ(0, insets.right());
  17. }
  18. TEST(InsetsTest, TLBR) {
  19. Insets insets = Insets::TLBR(1, 2, 3, 4);
  20. EXPECT_EQ(1, insets.top());
  21. EXPECT_EQ(2, insets.left());
  22. EXPECT_EQ(3, insets.bottom());
  23. EXPECT_EQ(4, insets.right());
  24. }
  25. TEST(InsetsTest, VH) {
  26. Insets insets = Insets::VH(1, 2);
  27. EXPECT_EQ(1, insets.top());
  28. EXPECT_EQ(2, insets.left());
  29. EXPECT_EQ(1, insets.bottom());
  30. EXPECT_EQ(2, insets.right());
  31. }
  32. TEST(InsetsTest, SetLeftRight) {
  33. Insets insets(1);
  34. insets.set_left_right(3, 4);
  35. EXPECT_EQ(1, insets.top());
  36. EXPECT_EQ(3, insets.left());
  37. EXPECT_EQ(1, insets.bottom());
  38. EXPECT_EQ(4, insets.right());
  39. EXPECT_EQ(insets, Insets(1).set_left_right(3, 4));
  40. }
  41. TEST(InsetsTest, SetTopBottom) {
  42. Insets insets(1);
  43. insets.set_top_bottom(3, 4);
  44. EXPECT_EQ(3, insets.top());
  45. EXPECT_EQ(1, insets.left());
  46. EXPECT_EQ(4, insets.bottom());
  47. EXPECT_EQ(1, insets.right());
  48. EXPECT_EQ(insets, Insets(1).set_top_bottom(3, 4));
  49. }
  50. TEST(InsetsTest, SetTop) {
  51. Insets insets(1);
  52. insets.set_top(2);
  53. EXPECT_EQ(2, insets.top());
  54. EXPECT_EQ(1, insets.left());
  55. EXPECT_EQ(1, insets.bottom());
  56. EXPECT_EQ(1, insets.right());
  57. EXPECT_EQ(insets, Insets(1).set_top(2));
  58. }
  59. TEST(InsetsTest, SetBottom) {
  60. Insets insets(1);
  61. insets.set_bottom(2);
  62. EXPECT_EQ(1, insets.top());
  63. EXPECT_EQ(1, insets.left());
  64. EXPECT_EQ(2, insets.bottom());
  65. EXPECT_EQ(1, insets.right());
  66. EXPECT_EQ(insets, Insets(1).set_bottom(2));
  67. }
  68. TEST(InsetsTest, SetLeft) {
  69. Insets insets(1);
  70. insets.set_left(2);
  71. EXPECT_EQ(1, insets.top());
  72. EXPECT_EQ(2, insets.left());
  73. EXPECT_EQ(1, insets.bottom());
  74. EXPECT_EQ(1, insets.right());
  75. EXPECT_EQ(insets, Insets(1).set_left(2));
  76. }
  77. TEST(InsetsTest, SetRight) {
  78. Insets insets(1);
  79. insets.set_right(2);
  80. EXPECT_EQ(1, insets.top());
  81. EXPECT_EQ(1, insets.left());
  82. EXPECT_EQ(1, insets.bottom());
  83. EXPECT_EQ(2, insets.right());
  84. EXPECT_EQ(insets, Insets(1).set_right(2));
  85. }
  86. TEST(InsetsTest, WidthHeightAndIsEmpty) {
  87. Insets insets;
  88. EXPECT_EQ(0, insets.width());
  89. EXPECT_EQ(0, insets.height());
  90. EXPECT_TRUE(insets.IsEmpty());
  91. insets.set_left_right(3, 4);
  92. EXPECT_EQ(7, insets.width());
  93. EXPECT_EQ(0, insets.height());
  94. EXPECT_FALSE(insets.IsEmpty());
  95. insets.set_left_right(0, 0);
  96. insets.set_top_bottom(1, 2);
  97. EXPECT_EQ(0, insets.width());
  98. EXPECT_EQ(3, insets.height());
  99. EXPECT_FALSE(insets.IsEmpty());
  100. insets.set_left_right(4, 5);
  101. EXPECT_EQ(9, insets.width());
  102. EXPECT_EQ(3, insets.height());
  103. EXPECT_FALSE(insets.IsEmpty());
  104. }
  105. TEST(InsetsTest, Operators) {
  106. Insets insets = Insets().set_left_right(2, 4).set_top_bottom(1, 3);
  107. insets += Insets().set_left_right(6, 8).set_top_bottom(5, 7);
  108. EXPECT_EQ(6, insets.top());
  109. EXPECT_EQ(8, insets.left());
  110. EXPECT_EQ(10, insets.bottom());
  111. EXPECT_EQ(12, insets.right());
  112. insets -= Insets().set_left_right(0, 2).set_top_bottom(-1, 1);
  113. EXPECT_EQ(7, insets.top());
  114. EXPECT_EQ(8, insets.left());
  115. EXPECT_EQ(9, insets.bottom());
  116. EXPECT_EQ(10, insets.right());
  117. insets = Insets(10) + Insets().set_left_right(5, -20).set_top_bottom(10, 0);
  118. EXPECT_EQ(20, insets.top());
  119. EXPECT_EQ(15, insets.left());
  120. EXPECT_EQ(10, insets.bottom());
  121. EXPECT_EQ(-10, insets.right());
  122. insets = Insets(10) - Insets().set_left_right(5, -20).set_top_bottom(10, 0);
  123. EXPECT_EQ(0, insets.top());
  124. EXPECT_EQ(5, insets.left());
  125. EXPECT_EQ(10, insets.bottom());
  126. EXPECT_EQ(30, insets.right());
  127. }
  128. TEST(InsetsTest, Equality) {
  129. Insets insets1 = Insets().set_left_right(2, 4).set_top_bottom(1, 3);
  130. Insets insets2;
  131. // Test operator== and operator!=.
  132. EXPECT_FALSE(insets1 == insets2);
  133. EXPECT_TRUE(insets1 != insets2);
  134. insets2.set_left_right(2, 4).set_top_bottom(1, 3);
  135. EXPECT_TRUE(insets1 == insets2);
  136. EXPECT_FALSE(insets1 != insets2);
  137. }
  138. TEST(InsetsTest, ToString) {
  139. Insets insets = Insets().set_left_right(2, 4).set_top_bottom(1, 3);
  140. EXPECT_EQ("x:2,4 y:1,3", insets.ToString());
  141. }
  142. TEST(InsetsTest, Offset) {
  143. const Insets insets = Insets().set_left_right(2, 4).set_top_bottom(1, 3);
  144. const Rect rect(5, 6, 7, 8);
  145. const Vector2d vector(9, 10);
  146. // Whether you inset then offset the rect, offset then inset the rect, or
  147. // offset the insets then apply to the rect, the outcome should be the same.
  148. Rect inset_first = rect;
  149. inset_first.Inset(insets);
  150. inset_first.Offset(vector);
  151. Rect offset_first = rect;
  152. offset_first.Offset(vector);
  153. offset_first.Inset(insets);
  154. Insets insets_with_offset = insets;
  155. insets_with_offset.Offset(vector);
  156. EXPECT_EQ(gfx::Insets().set_left_right(11, -5).set_top_bottom(11, -7),
  157. insets_with_offset);
  158. EXPECT_EQ(insets_with_offset, insets + vector);
  159. Rect inset_by_offset = rect;
  160. inset_by_offset.Inset(insets_with_offset);
  161. EXPECT_EQ(inset_first, offset_first);
  162. EXPECT_EQ(inset_by_offset, inset_first);
  163. }
  164. TEST(InsetsTest, Scale) {
  165. Insets in = Insets().set_left_right(5, 1).set_top_bottom(7, 3);
  166. Insets test = ScaleToFlooredInsets(in, 2.5f, 3.5f);
  167. EXPECT_EQ(Insets().set_left_right(12, 2).set_top_bottom(24, 10), test);
  168. test = ScaleToFlooredInsets(in, 2.5f);
  169. EXPECT_EQ(Insets().set_left_right(12, 2).set_top_bottom(17, 7), test);
  170. test = ScaleToCeiledInsets(in, 2.5f, 3.5f);
  171. EXPECT_EQ(Insets().set_left_right(13, 3).set_top_bottom(25, 11), test);
  172. test = ScaleToCeiledInsets(in, 2.5f);
  173. EXPECT_EQ(Insets().set_left_right(13, 3).set_top_bottom(18, 8), test);
  174. test = ScaleToRoundedInsets(in, 2.49f, 3.49f);
  175. EXPECT_EQ(Insets().set_left_right(12, 2).set_top_bottom(24, 10), test);
  176. test = ScaleToRoundedInsets(in, 2.49f);
  177. EXPECT_EQ(Insets().set_left_right(12, 2).set_top_bottom(17, 7), test);
  178. test = ScaleToRoundedInsets(in, 2.5f, 3.5f);
  179. EXPECT_EQ(Insets().set_left_right(13, 3).set_top_bottom(25, 11), test);
  180. test = ScaleToRoundedInsets(in, 2.5f);
  181. EXPECT_EQ(Insets().set_left_right(13, 3).set_top_bottom(18, 8), test);
  182. }
  183. TEST(InsetsTest, ScaleNegative) {
  184. Insets in = Insets().set_left_right(-5, -1).set_top_bottom(-7, -3);
  185. Insets test = ScaleToFlooredInsets(in, 2.5f, 3.5f);
  186. EXPECT_EQ(Insets().set_left_right(-13, -3).set_top_bottom(-25, -11), test);
  187. test = ScaleToFlooredInsets(in, 2.5f);
  188. EXPECT_EQ(Insets().set_left_right(-13, -3).set_top_bottom(-18, -8), test);
  189. test = ScaleToCeiledInsets(in, 2.5f, 3.5f);
  190. EXPECT_EQ(Insets().set_left_right(-12, -2).set_top_bottom(-24, -10), test);
  191. test = ScaleToCeiledInsets(in, 2.5f);
  192. EXPECT_EQ(Insets().set_left_right(-12, -2).set_top_bottom(-17, -7), test);
  193. test = ScaleToRoundedInsets(in, 2.49f, 3.49f);
  194. EXPECT_EQ(Insets().set_left_right(-12, -2).set_top_bottom(-24, -10), test);
  195. test = ScaleToRoundedInsets(in, 2.49f);
  196. EXPECT_EQ(Insets().set_left_right(-12, -2).set_top_bottom(-17, -7), test);
  197. test = ScaleToRoundedInsets(in, 2.5f, 3.5f);
  198. EXPECT_EQ(Insets().set_left_right(-13, -3).set_top_bottom(-25, -11), test);
  199. test = ScaleToRoundedInsets(in, 2.5f);
  200. EXPECT_EQ(Insets().set_left_right(-13, -3).set_top_bottom(-18, -8), test);
  201. }
  202. TEST(InsetsTest, IntegerOverflow) {
  203. constexpr int int_min = std::numeric_limits<int>::min();
  204. constexpr int int_max = std::numeric_limits<int>::max();
  205. Insets width_height_test(int_max);
  206. EXPECT_EQ(int_max, width_height_test.width());
  207. EXPECT_EQ(int_max, width_height_test.height());
  208. Insets plus_test(int_max);
  209. plus_test += Insets(int_max);
  210. EXPECT_EQ(Insets(int_max), plus_test);
  211. Insets negation_test = -Insets(int_min);
  212. EXPECT_EQ(Insets(int_max), negation_test);
  213. Insets scale_test(int_max);
  214. scale_test = ScaleToRoundedInsets(scale_test, 2.f);
  215. EXPECT_EQ(Insets(int_max), scale_test);
  216. }
  217. TEST(InsetsTest, IntegerUnderflow) {
  218. constexpr int int_min = std::numeric_limits<int>::min();
  219. constexpr int int_max = std::numeric_limits<int>::max();
  220. Insets width_height_test = Insets(int_min);
  221. EXPECT_EQ(int_min, width_height_test.width());
  222. EXPECT_EQ(int_min, width_height_test.height());
  223. Insets minus_test(int_min);
  224. minus_test -= Insets(int_max);
  225. EXPECT_EQ(Insets(int_min), minus_test);
  226. Insets scale_test = Insets(int_min);
  227. scale_test = ScaleToRoundedInsets(scale_test, 2.f);
  228. EXPECT_EQ(Insets(int_min), scale_test);
  229. }
  230. TEST(InsetsTest, IntegerOverflowSetVariants) {
  231. constexpr int int_max = std::numeric_limits<int>::max();
  232. Insets set_test(20);
  233. set_test.set_top(int_max);
  234. EXPECT_EQ(int_max, set_test.top());
  235. EXPECT_EQ(0, set_test.bottom());
  236. set_test.set_left(int_max);
  237. EXPECT_EQ(int_max, set_test.left());
  238. EXPECT_EQ(0, set_test.right());
  239. set_test = Insets(30);
  240. set_test.set_bottom(int_max);
  241. EXPECT_EQ(int_max - 30, set_test.bottom());
  242. EXPECT_EQ(30, set_test.top());
  243. set_test.set_right(int_max);
  244. EXPECT_EQ(int_max - 30, set_test.right());
  245. EXPECT_EQ(30, set_test.left());
  246. }
  247. TEST(InsetsTest, IntegerUnderflowSetVariants) {
  248. constexpr int int_min = std::numeric_limits<int>::min();
  249. Insets set_test(-20);
  250. set_test.set_top(int_min);
  251. EXPECT_EQ(int_min, set_test.top());
  252. EXPECT_EQ(0, set_test.bottom());
  253. set_test.set_left(int_min);
  254. EXPECT_EQ(int_min, set_test.left());
  255. EXPECT_EQ(0, set_test.right());
  256. set_test = Insets(-30);
  257. set_test.set_bottom(int_min);
  258. EXPECT_EQ(int_min + 30, set_test.bottom());
  259. EXPECT_EQ(-30, set_test.top());
  260. set_test.set_right(int_min);
  261. EXPECT_EQ(int_min + 30, set_test.right());
  262. EXPECT_EQ(-30, set_test.left());
  263. }
  264. TEST(InsetsTest, IntegerOverflowSet) {
  265. constexpr int int_max = std::numeric_limits<int>::max();
  266. Insets set_all_test =
  267. Insets().set_left_right(int_max, 20).set_top_bottom(10, int_max);
  268. EXPECT_EQ(
  269. Insets().set_left_right(int_max, 0).set_top_bottom(10, int_max - 10),
  270. set_all_test);
  271. }
  272. TEST(InsetsTest, IntegerOverflowOffset) {
  273. constexpr int int_max = std::numeric_limits<int>::max();
  274. const gfx::Vector2d max_vector(int_max, int_max);
  275. Insets insets = Insets().set_left_right(2, 4).set_top_bottom(1, 3);
  276. insets.Offset(max_vector);
  277. EXPECT_EQ(gfx::Insets()
  278. .set_left_right(int_max, 4 - int_max)
  279. .set_top_bottom(int_max, 3 - int_max),
  280. insets);
  281. }
  282. TEST(InsetsTest, IntegerUnderflowOffset) {
  283. constexpr int int_min = std::numeric_limits<int>::min();
  284. const Vector2d min_vector(int_min, int_min);
  285. Insets insets(-10);
  286. insets.Offset(min_vector);
  287. EXPECT_EQ(gfx::Insets()
  288. .set_left_right(int_min, -10 - int_min)
  289. .set_top_bottom(int_min, -10 - int_min),
  290. insets);
  291. }
  292. TEST(InsetsTest, Size) {
  293. Insets insets = Insets().set_left_right(2, 4).set_top_bottom(1, 3);
  294. EXPECT_EQ(Size(6, 4), insets.size());
  295. }
  296. TEST(InsetsTest, SetToMax) {
  297. Insets insets;
  298. insets.SetToMax(Insets().set_left_right(2, 4).set_top_bottom(-1, -3));
  299. EXPECT_EQ(Insets().set_left_right(2, 4), insets);
  300. insets.SetToMax(Insets());
  301. EXPECT_EQ(Insets().set_left_right(2, 4), insets);
  302. insets.SetToMax(Insets().set_top_bottom(1, 3));
  303. EXPECT_EQ(Insets().set_left_right(2, 4).set_top_bottom(1, 3), insets);
  304. insets.SetToMax(Insets().set_left_right(30, 50).set_top_bottom(20, 40));
  305. EXPECT_EQ(Insets().set_left_right(30, 50).set_top_bottom(20, 40), insets);
  306. Insets insets1 = Insets().set_left_right(-2, -4).set_top_bottom(-2, -4);
  307. insets1.SetToMax(Insets());
  308. EXPECT_EQ(Insets(), insets1);
  309. }
  310. TEST(InsetsTest, ConversionFromToOutsets) {
  311. Insets insets = Insets().set_left_right(2, 4).set_top_bottom(-1, -3);
  312. EXPECT_EQ(Outsets().set_left_right(-2, -4).set_top_bottom(1, 3),
  313. insets.ToOutsets());
  314. EXPECT_EQ(insets, insets.ToOutsets().ToInsets());
  315. }
  316. } // namespace gfx