display_list_unittest.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2016 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/display/display_list.h"
  5. #include <string>
  6. #include <vector>
  7. #include "base/strings/string_number_conversions.h"
  8. #include "base/test/gtest_util.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "ui/display/display.h"
  11. #include "ui/display/display_observer.h"
  12. namespace display {
  13. namespace {
  14. class DisplayObserverImpl : public DisplayObserver {
  15. public:
  16. DisplayObserverImpl() {}
  17. DisplayObserverImpl(const DisplayObserverImpl&) = delete;
  18. DisplayObserverImpl& operator=(const DisplayObserverImpl&) = delete;
  19. ~DisplayObserverImpl() override {}
  20. std::string GetAndClearChanges() {
  21. std::string changes;
  22. std::swap(changes, changes_);
  23. return changes;
  24. }
  25. private:
  26. static void AddPartChange(uint32_t changed,
  27. uint32_t part,
  28. const std::string& description,
  29. std::string* changed_string) {
  30. if ((changed & part) != part)
  31. return;
  32. *changed_string += " ";
  33. *changed_string += description;
  34. }
  35. void AddChange(const std::string& change) {
  36. if (!changes_.empty())
  37. changes_ += "\n";
  38. changes_ += change;
  39. }
  40. void OnDisplayAdded(const Display& new_display) override {
  41. AddChange("Added id=" + base::NumberToString(new_display.id()));
  42. }
  43. void OnDisplayRemoved(const Display& old_display) override {
  44. AddChange("Removed id=" + base::NumberToString(old_display.id()));
  45. }
  46. void OnDisplayMetricsChanged(const Display& display,
  47. uint32_t changed_metrics) override {
  48. std::string parts;
  49. AddPartChange(changed_metrics, DISPLAY_METRIC_BOUNDS, "bounds", &parts);
  50. AddPartChange(changed_metrics, DISPLAY_METRIC_WORK_AREA, "work_area",
  51. &parts);
  52. AddPartChange(changed_metrics, DISPLAY_METRIC_DEVICE_SCALE_FACTOR,
  53. "scale_factor", &parts);
  54. AddPartChange(changed_metrics, DISPLAY_METRIC_ROTATION, "rotation", &parts);
  55. AddPartChange(changed_metrics, DISPLAY_METRIC_PRIMARY, "primary", &parts);
  56. AddPartChange(changed_metrics, DISPLAY_METRIC_LABEL, "label", &parts);
  57. AddChange("Changed id=" + base::NumberToString(display.id()) + parts);
  58. }
  59. std::string changes_;
  60. };
  61. TEST(DisplayListTest, AddUpdateRemove) {
  62. DisplayList display_list;
  63. DisplayObserverImpl observer;
  64. display_list.AddObserver(&observer);
  65. display_list.AddDisplay(Display(2, gfx::Rect(0, 0, 801, 802)),
  66. DisplayList::Type::PRIMARY);
  67. EXPECT_EQ("Added id=2", observer.GetAndClearChanges());
  68. // Update the bounds.
  69. {
  70. Display updated_display = *(display_list.displays().begin());
  71. updated_display.set_bounds(gfx::Rect(0, 0, 803, 802));
  72. display_list.UpdateDisplay(updated_display, DisplayList::Type::PRIMARY);
  73. EXPECT_EQ("Changed id=2 bounds", observer.GetAndClearChanges());
  74. }
  75. // Update the label.
  76. {
  77. Display updated_display = *(display_list.displays().begin());
  78. updated_display.set_label("new_label");
  79. display_list.UpdateDisplay(updated_display, DisplayList::Type::PRIMARY);
  80. EXPECT_EQ("Changed id=2 label", observer.GetAndClearChanges());
  81. EXPECT_EQ("new_label", display_list.FindDisplayById(2)->label());
  82. }
  83. // Add another.
  84. display_list.AddDisplay(Display(3, gfx::Rect(0, 0, 809, 802)),
  85. DisplayList::Type::NOT_PRIMARY);
  86. EXPECT_EQ("Added id=3", observer.GetAndClearChanges());
  87. ASSERT_EQ(2u, display_list.displays().size());
  88. EXPECT_EQ(2, display_list.displays()[0].id());
  89. EXPECT_EQ(3, display_list.displays()[1].id());
  90. EXPECT_EQ(2, display_list.GetPrimaryDisplayIterator()->id());
  91. // Make the second the primary.
  92. display_list.UpdateDisplay(display_list.displays()[1],
  93. DisplayList::Type::PRIMARY);
  94. EXPECT_EQ("Changed id=3 primary", observer.GetAndClearChanges());
  95. EXPECT_EQ(3, display_list.GetPrimaryDisplayIterator()->id());
  96. // Delete the first.
  97. display_list.RemoveDisplay(2);
  98. ASSERT_EQ(1u, display_list.displays().size());
  99. EXPECT_EQ("Removed id=2", observer.GetAndClearChanges());
  100. EXPECT_EQ(3, display_list.GetPrimaryDisplayIterator()->id());
  101. }
  102. TEST(DisplayListTest, EmptyIsValid) {
  103. DisplayList display_list;
  104. EXPECT_TRUE(display_list.IsValid());
  105. }
  106. TEST(DisplayListTest, FirstDisplayAddedMustBePrimary) {
  107. DisplayList display_list;
  108. EXPECT_DCHECK_DEATH(
  109. display_list.AddDisplay(Display(1), DisplayList::Type::NOT_PRIMARY));
  110. }
  111. TEST(DisplayListTest, DisplaysIdsMustBeUnique) {
  112. DisplayList display_list;
  113. display_list.AddDisplay(Display(1), DisplayList::Type::PRIMARY);
  114. EXPECT_DCHECK_DEATH(
  115. display_list.AddDisplay(Display(1), DisplayList::Type::PRIMARY));
  116. }
  117. TEST(DisplayListTest, GetPrimaryDisplayEmpty) {
  118. DisplayList display_list;
  119. EXPECT_EQ(display_list.displays().end(),
  120. display_list.GetPrimaryDisplayIterator());
  121. }
  122. TEST(DisplayListTest, GetPrimaryDisplayOk) {
  123. DisplayList display_list;
  124. display_list.AddDisplay(Display(1), DisplayList::Type::PRIMARY);
  125. EXPECT_NE(display_list.displays().end(),
  126. display_list.GetPrimaryDisplayIterator());
  127. EXPECT_EQ(1, display_list.GetPrimaryDisplayIterator()->id());
  128. }
  129. } // namespace
  130. } // namespace display