selection_bound_unittest.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2014 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/selection_bound.h"
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. #include "ui/gfx/geometry/rect.h"
  7. #include "ui/gfx/geometry/rect_f.h"
  8. namespace gfx {
  9. TEST(SelectionBoundTest, RectBetweenSelectionBounds) {
  10. SelectionBound b1, b2;
  11. // Simple case of aligned vertical bounds of equal height
  12. b1.SetEdge(gfx::PointF(0.f, 20.f), gfx::PointF(0.f, 25.f));
  13. b2.SetEdge(gfx::PointF(110.f, 20.f), gfx::PointF(110.f, 25.f));
  14. gfx::Rect expected_rect(
  15. b1.edge_start_rounded().x(), b1.edge_start_rounded().y(),
  16. b2.edge_start_rounded().x() - b1.edge_start_rounded().x(),
  17. b2.edge_end_rounded().y() - b2.edge_start_rounded().y());
  18. EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2));
  19. EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1));
  20. // Both bounds are invisible.
  21. b1.SetVisibleEdge(gfx::PointF(10.f, 20.f), gfx::PointF(10.f, 25.f));
  22. b2.SetVisibleEdge(gfx::PointF(100.f, 20.f), gfx::PointF(100.f, 25.f));
  23. gfx::RectF expected_visible_rect(
  24. b1.visible_edge_start().x(), b1.visible_edge_start().y(),
  25. b2.visible_edge_start().x() - b1.visible_edge_start().x(),
  26. b2.visible_edge_end().y() - b2.visible_edge_start().y());
  27. EXPECT_EQ(expected_visible_rect, RectFBetweenVisibleSelectionBounds(b1, b2));
  28. EXPECT_EQ(expected_visible_rect, RectFBetweenVisibleSelectionBounds(b2, b1));
  29. // One of the bounds is invisible.
  30. b1.SetVisibleEdge(gfx::PointF(0.f, 20.f), gfx::PointF(0.f, 25.f));
  31. b2.SetVisibleEdge(gfx::PointF(100.f, 20.f), gfx::PointF(100.f, 25.f));
  32. expected_visible_rect =
  33. gfx::RectF(b1.visible_edge_start().x(), b1.visible_edge_start().y(),
  34. b2.visible_edge_start().x() - b1.visible_edge_start().x(),
  35. b2.visible_edge_end().y() - b2.visible_edge_start().y());
  36. EXPECT_EQ(expected_visible_rect, RectFBetweenVisibleSelectionBounds(b1, b2));
  37. EXPECT_EQ(expected_visible_rect, RectFBetweenVisibleSelectionBounds(b2, b1));
  38. // Parallel vertical bounds of different heights
  39. b1.SetEdge(gfx::PointF(10.f, 20.f), gfx::PointF(10.f, 25.f));
  40. b2.SetEdge(gfx::PointF(110.f, 0.f), gfx::PointF(110.f, 35.f));
  41. expected_rect =
  42. gfx::Rect(b1.edge_start_rounded().x(), b2.edge_start_rounded().y(),
  43. b2.edge_start_rounded().x() - b1.edge_start_rounded().x(),
  44. b2.edge_end_rounded().y() - b2.edge_start_rounded().y());
  45. EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2));
  46. EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1));
  47. b1.SetEdge(gfx::PointF(10.f, 20.f), gfx::PointF(10.f, 30.f));
  48. b2.SetEdge(gfx::PointF(110.f, 25.f), gfx::PointF(110.f, 45.f));
  49. expected_rect =
  50. gfx::Rect(b1.edge_start_rounded().x(), b1.edge_start_rounded().y(),
  51. b2.edge_start_rounded().x() - b1.edge_start_rounded().x(),
  52. b2.edge_end_rounded().y() - b1.edge_start_rounded().y());
  53. EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2));
  54. EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1));
  55. b1.SetEdge(gfx::PointF(10.f, 20.f), gfx::PointF(10.f, 30.f));
  56. b2.SetEdge(gfx::PointF(110.f, 40.f), gfx::PointF(110.f, 60.f));
  57. expected_rect =
  58. gfx::Rect(b1.edge_start_rounded().x(), b1.edge_start_rounded().y(),
  59. b2.edge_start_rounded().x() - b1.edge_start_rounded().x(),
  60. b2.edge_end_rounded().y() - b1.edge_start_rounded().y());
  61. EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2));
  62. EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1));
  63. // Overlapping vertical bounds
  64. b1.SetEdge(gfx::PointF(10.f, 20.f), gfx::PointF(10.f, 30.f));
  65. b2.SetEdge(gfx::PointF(10.f, 25.f), gfx::PointF(10.f, 40.f));
  66. expected_rect =
  67. gfx::Rect(b1.edge_start_rounded().x(), b1.edge_start_rounded().y(), 0,
  68. b2.edge_end_rounded().y() - b1.edge_start_rounded().y());
  69. EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2));
  70. EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1));
  71. // Non-vertical bounds: "\ \"
  72. b1.SetEdge(gfx::PointF(10.f, 20.f), gfx::PointF(20.f, 30.f));
  73. b2.SetEdge(gfx::PointF(110.f, 40.f), gfx::PointF(120.f, 60.f));
  74. expected_rect =
  75. gfx::Rect(b1.edge_start_rounded().x(), b1.edge_start_rounded().y(),
  76. b2.edge_end_rounded().x() - b1.edge_start_rounded().x(),
  77. b2.edge_end_rounded().y() - b1.edge_start_rounded().y());
  78. EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2));
  79. EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1));
  80. // Non-vertical bounds: "/ \"
  81. b1.SetEdge(gfx::PointF(20.f, 30.f), gfx::PointF(0.f, 40.f));
  82. b2.SetEdge(gfx::PointF(110.f, 30.f), gfx::PointF(120.f, 40.f));
  83. expected_rect =
  84. gfx::Rect(b1.edge_end_rounded().x(), b1.edge_start_rounded().y(),
  85. b2.edge_end_rounded().x() - b1.edge_end_rounded().x(),
  86. b2.edge_end_rounded().y() - b2.edge_start_rounded().y());
  87. EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2));
  88. EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1));
  89. }
  90. } // namespace gfx