selection_model_unittest.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright (c) 2020 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 <vector>
  5. #include "ui/gfx/selection_model.h"
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. #include "ui/gfx/range/range.h"
  8. namespace gfx {
  9. TEST(SelectionModelTest, Construction) {
  10. {
  11. SelectionModel selection_model;
  12. EXPECT_EQ(selection_model.selection(), Range(0));
  13. EXPECT_EQ(selection_model.caret_pos(), 0u);
  14. EXPECT_EQ(selection_model.secondary_selections(), std::vector<Range>());
  15. }
  16. {
  17. SelectionModel selection_model{5, CURSOR_FORWARD};
  18. EXPECT_EQ(selection_model.selection(), Range(5));
  19. EXPECT_EQ(selection_model.caret_pos(), 5u);
  20. EXPECT_EQ(selection_model.secondary_selections(), std::vector<Range>());
  21. }
  22. {
  23. SelectionModel selection_model{{3, 2}, CURSOR_BACKWARD};
  24. EXPECT_EQ(selection_model.selection(), Range(3, 2));
  25. EXPECT_EQ(selection_model.caret_pos(), 2u);
  26. EXPECT_EQ(selection_model.secondary_selections(), std::vector<Range>());
  27. }
  28. {
  29. SelectionModel selection_model{{{2, 3}, {5, 5}, {1, 0}}, CURSOR_BACKWARD};
  30. EXPECT_EQ(selection_model.selection(), Range(2, 3));
  31. EXPECT_EQ(selection_model.caret_pos(), 3u);
  32. EXPECT_EQ(selection_model.secondary_selections(),
  33. std::vector<Range>({{5, 5}, {1, 0}}));
  34. }
  35. }
  36. TEST(SelectionModelTest, AddSecondarySelection) {
  37. SelectionModel selection_model;
  38. selection_model.AddSecondarySelection({5, 6});
  39. selection_model.AddSecondarySelection({7, 6});
  40. selection_model.AddSecondarySelection({8, 8});
  41. EXPECT_EQ(selection_model.selection(), Range(0));
  42. EXPECT_EQ(selection_model.caret_pos(), 0u);
  43. EXPECT_EQ(selection_model.secondary_selections(),
  44. std::vector<Range>({{5, 6}, {7, 6}, {8, 8}}));
  45. }
  46. TEST(SelectionModelTest, GetAllSelections) {
  47. SelectionModel selection_model{{3, 2}, CURSOR_BACKWARD};
  48. selection_model.AddSecondarySelection({5, 6});
  49. selection_model.AddSecondarySelection({7, 6});
  50. selection_model.AddSecondarySelection({8, 8});
  51. EXPECT_EQ(selection_model.GetAllSelections(),
  52. std::vector<Range>({{3, 2}, {5, 6}, {7, 6}, {8, 8}}));
  53. }
  54. TEST(SelectionModelTest, EqualityOperators) {
  55. SelectionModel selection_model{{3, 2}, CURSOR_BACKWARD};
  56. selection_model.AddSecondarySelection({5, 6});
  57. selection_model.AddSecondarySelection({7, 6});
  58. selection_model.AddSecondarySelection({8, 8});
  59. // Equal
  60. EXPECT_EQ(selection_model,
  61. SelectionModel({{3, 2}, {5, 6}, {7, 6}, {8, 8}}, CURSOR_BACKWARD));
  62. // Unequal selection
  63. EXPECT_NE(selection_model,
  64. SelectionModel({{3, 3}, {5, 6}, {7, 6}, {8, 8}}, CURSOR_BACKWARD));
  65. // Unequal secondary selections
  66. EXPECT_NE(selection_model,
  67. SelectionModel({{3, 2}, {5, 6}, {7, 6}, {9, 8}}, CURSOR_BACKWARD));
  68. // Unequal cursor affinity
  69. EXPECT_NE(selection_model,
  70. SelectionModel({{3, 2}, {5, 6}, {7, 6}, {8, 8}}, CURSOR_FORWARD));
  71. }
  72. TEST(SelectionModelTest, ToString) {
  73. SelectionModel selection_model{{3, 2}, CURSOR_BACKWARD};
  74. selection_model.AddSecondarySelection({5, 6});
  75. selection_model.AddSecondarySelection({7, 6});
  76. selection_model.AddSecondarySelection({8, 8});
  77. EXPECT_EQ(selection_model.ToString(), "{{3,2},BACKWARD,{5,6},{7,6},8}");
  78. }
  79. } // namespace gfx