| OLD | NEW |
| (Empty) | |
| 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 |
| 5 #include <memory> |
| 6 #include <vector> |
| 7 |
| 8 #include "base/logging.h" |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "services/ui/display/platform_screen.h" |
| 11 #include "services/ui/display/platform_screen_impl_ozone.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "ui/display/chromeos/display_configurator.h" |
| 15 #include "ui/display/chromeos/display_snapshot_virtual.h" |
| 16 #include "ui/display/types/display_constants.h" |
| 17 #include "ui/display/types/display_mode.h" |
| 18 #include "ui/display/types/display_snapshot.h" |
| 19 #include "ui/ozone/public/ozone_platform.h" |
| 20 |
| 21 namespace display { |
| 22 |
| 23 using ui::DisplayConfigurator; |
| 24 using ui::DisplayMode; |
| 25 using ui::DisplaySnapshot; |
| 26 using ui::DisplaySnapshotVirtual; |
| 27 using testing::IsEmpty; |
| 28 using testing::SizeIs; |
| 29 |
| 30 namespace { |
| 31 |
| 32 // The ID of default "display" that gets added when running off device. |
| 33 const int64_t kDefaultDisplayId = 36028797018963969; |
| 34 |
| 35 // Holds info about the display state we want to test. |
| 36 struct DisplayState { |
| 37 int64_t id; |
| 38 gfx::Rect bounds; |
| 39 }; |
| 40 |
| 41 // Matchers that operate on DisplayState. |
| 42 MATCHER_P(DisplayId, display_id, "") { |
| 43 *result_listener << "has id " << arg.id; |
| 44 return arg.id == display_id; |
| 45 } |
| 46 |
| 47 MATCHER_P(DisplaySize, size_string, "") { |
| 48 *result_listener << "has size " << arg.bounds.size().ToString(); |
| 49 return arg.bounds.size().ToString() == size_string; |
| 50 } |
| 51 |
| 52 MATCHER_P(DisplayOrigin, origin_string, "") { |
| 53 *result_listener << "has origin " << arg.bounds.origin().ToString(); |
| 54 return arg.bounds.origin().ToString() == origin_string; |
| 55 } |
| 56 |
| 57 // Make a DisplaySnapshot with specified id and size. |
| 58 std::unique_ptr<DisplaySnapshot> MakeSnapshot(int64_t id, |
| 59 const gfx::Size& size) { |
| 60 auto snapshot = base::MakeUnique<DisplaySnapshotVirtual>(id, size); |
| 61 snapshot->set_current_mode(snapshot->modes()[0].get()); |
| 62 return snapshot; |
| 63 } |
| 64 |
| 65 // Test delegate to track what functions calls the delegate receives. |
| 66 class TestPlatformScreenDelegate : public PlatformScreenDelegate { |
| 67 public: |
| 68 TestPlatformScreenDelegate() {} |
| 69 ~TestPlatformScreenDelegate() override {} |
| 70 |
| 71 std::vector<DisplayState> added() { return added_; } |
| 72 std::vector<DisplayState> removed() { return removed_; } |
| 73 std::vector<DisplayState> modified() { return modified_; } |
| 74 |
| 75 void Reset() { |
| 76 added_.clear(); |
| 77 removed_.clear(); |
| 78 modified_.clear(); |
| 79 } |
| 80 |
| 81 private: |
| 82 void OnDisplayAdded(PlatformScreen* platform_screen, |
| 83 int64_t id, |
| 84 const gfx::Rect& bounds) override { |
| 85 added_.push_back({id, bounds}); |
| 86 } |
| 87 |
| 88 void OnDisplayRemoved(int64_t id) override { |
| 89 removed_.push_back({id, gfx::Rect()}); |
| 90 } |
| 91 |
| 92 void OnDisplayModified(int64_t id, const gfx::Rect& bounds) override { |
| 93 modified_.push_back({id, bounds}); |
| 94 } |
| 95 |
| 96 std::vector<DisplayState> added_; |
| 97 std::vector<DisplayState> removed_; |
| 98 std::vector<DisplayState> modified_; |
| 99 |
| 100 DISALLOW_COPY_AND_ASSIGN(TestPlatformScreenDelegate); |
| 101 }; |
| 102 |
| 103 // Test fixture with helpers to act like ui::DisplayConfigurator and send |
| 104 // OnDisplayModeChanged() to PlatformScreenImplOzone. |
| 105 class PlatformScreenOzoneTest : public testing::Test { |
| 106 public: |
| 107 PlatformScreenOzoneTest() {} |
| 108 ~PlatformScreenOzoneTest() override {} |
| 109 |
| 110 PlatformScreen* platform_screen() { return platform_screen_.get(); } |
| 111 TestPlatformScreenDelegate* delegate() { return &delegate_; } |
| 112 |
| 113 // Adds a display snapshot with specified ID and default size. |
| 114 void AddDisplay(int64_t id) { return AddDisplay(id, gfx::Size(1024, 768)); } |
| 115 |
| 116 // Adds a display snapshot with specified ID and size to list of snapshots. |
| 117 void AddDisplay(int64_t id, const gfx::Size& size) { |
| 118 snapshots_.push_back(MakeSnapshot(id, size)); |
| 119 } |
| 120 |
| 121 // Removes display snapshot with specified ID. |
| 122 void RemoveDisplay(int64_t id) { |
| 123 snapshots_.erase( |
| 124 std::remove_if(snapshots_.begin(), snapshots_.end(), |
| 125 [id](std::unique_ptr<DisplaySnapshot>& snapshot) { |
| 126 return snapshot->display_id() == id; |
| 127 })); |
| 128 } |
| 129 |
| 130 // Modify the size of the display snapshot with specified ID. |
| 131 void ModifyDisplay(int64_t id, const gfx::Size& size) { |
| 132 DisplaySnapshot* snapshot = GetSnapshotById(id); |
| 133 |
| 134 const DisplayMode* new_mode = nullptr; |
| 135 for (auto& mode : snapshot->modes()) { |
| 136 if (mode->size() == size) { |
| 137 new_mode = mode.get(); |
| 138 break; |
| 139 } |
| 140 } |
| 141 |
| 142 if (!new_mode) { |
| 143 snapshot->add_mode(new DisplayMode(size, false, 30.0f)); |
| 144 new_mode = snapshot->modes().back().get(); |
| 145 } |
| 146 |
| 147 snapshot->set_current_mode(new_mode); |
| 148 } |
| 149 |
| 150 // Calls OnDisplayModeChanged with our list of display snapshots. |
| 151 void TriggerOnDisplayModeChanged() { |
| 152 std::vector<DisplaySnapshot*> snapshots_ptrs; |
| 153 for (auto& snapshot : snapshots_) { |
| 154 snapshots_ptrs.push_back(snapshot.get()); |
| 155 } |
| 156 static_cast<DisplayConfigurator::Observer*>(platform_screen_.get()) |
| 157 ->OnDisplayModeChanged(snapshots_ptrs); |
| 158 } |
| 159 |
| 160 private: |
| 161 DisplaySnapshot* GetSnapshotById(int64_t id) { |
| 162 for (auto& snapshot : snapshots_) { |
| 163 if (snapshot->display_id() == id) |
| 164 return snapshot.get(); |
| 165 } |
| 166 return nullptr; |
| 167 } |
| 168 |
| 169 // testing::Test: |
| 170 void SetUp() override { |
| 171 testing::Test::SetUp(); |
| 172 ui::OzonePlatform::InitializeForUI(); |
| 173 platform_screen_ = base::MakeUnique<PlatformScreenImplOzone>(); |
| 174 platform_screen_->Init(&delegate_); |
| 175 |
| 176 // Double check the expected display exists and clear counters. |
| 177 ASSERT_THAT(delegate()->added(), SizeIs(1)); |
| 178 ASSERT_THAT(delegate_.added()[0], DisplayId(kDefaultDisplayId)); |
| 179 ASSERT_THAT(delegate_.added()[0], DisplayOrigin("0,0")); |
| 180 ASSERT_THAT(delegate_.added()[0], DisplaySize("1024x768")); |
| 181 delegate_.Reset(); |
| 182 |
| 183 // Make the initial list of snapshots match what exists. |
| 184 AddDisplay(kDefaultDisplayId); |
| 185 } |
| 186 |
| 187 void TearDown() override { |
| 188 snapshots_.clear(); |
| 189 delegate_.Reset(); |
| 190 platform_screen_.reset(); |
| 191 } |
| 192 |
| 193 TestPlatformScreenDelegate delegate_; |
| 194 std::unique_ptr<PlatformScreenImplOzone> platform_screen_; |
| 195 std::vector<std::unique_ptr<DisplaySnapshot>> snapshots_; |
| 196 }; |
| 197 |
| 198 } // namespace |
| 199 |
| 200 TEST_F(PlatformScreenOzoneTest, AddDisplay) { |
| 201 AddDisplay(2); |
| 202 TriggerOnDisplayModeChanged(); |
| 203 |
| 204 // Check that display 2 was added. |
| 205 ASSERT_THAT(delegate()->added(), SizeIs(1)); |
| 206 EXPECT_THAT(delegate()->added()[0], DisplayId(2)); |
| 207 EXPECT_THAT(delegate()->removed(), IsEmpty()); |
| 208 EXPECT_THAT(delegate()->modified(), IsEmpty()); |
| 209 } |
| 210 |
| 211 TEST_F(PlatformScreenOzoneTest, RemoveDisplay) { |
| 212 AddDisplay(2); |
| 213 TriggerOnDisplayModeChanged(); |
| 214 delegate()->Reset(); |
| 215 |
| 216 RemoveDisplay(2); |
| 217 TriggerOnDisplayModeChanged(); |
| 218 |
| 219 // Check that display 2 was removed. |
| 220 ASSERT_THAT(delegate()->removed(), SizeIs(1)); |
| 221 EXPECT_THAT(delegate()->removed()[0], DisplayId(2)); |
| 222 EXPECT_THAT(delegate()->added(), IsEmpty()); |
| 223 EXPECT_THAT(delegate()->modified(), IsEmpty()); |
| 224 } |
| 225 |
| 226 TEST_F(PlatformScreenOzoneTest, RemoveFirstDisplay) { |
| 227 AddDisplay(2); |
| 228 TriggerOnDisplayModeChanged(); |
| 229 delegate()->Reset(); |
| 230 |
| 231 RemoveDisplay(kDefaultDisplayId); |
| 232 TriggerOnDisplayModeChanged(); |
| 233 |
| 234 // Check that the default display was removed and display 2 was modified due |
| 235 // to the origin changing. |
| 236 EXPECT_THAT(delegate()->added(), IsEmpty()); |
| 237 ASSERT_THAT(delegate()->removed(), SizeIs(1)); |
| 238 EXPECT_THAT(delegate()->removed()[0], DisplayId(kDefaultDisplayId)); |
| 239 |
| 240 ASSERT_THAT(delegate()->modified(), SizeIs(1)); |
| 241 EXPECT_THAT(delegate()->modified()[0], DisplayId(2)); |
| 242 EXPECT_THAT(delegate()->modified()[0], DisplayOrigin("0,0")); |
| 243 } |
| 244 |
| 245 TEST_F(PlatformScreenOzoneTest, RemovePrimaryDisplay) { |
| 246 EXPECT_EQ(kDefaultDisplayId, platform_screen()->GetPrimaryDisplayId()); |
| 247 |
| 248 AddDisplay(2); |
| 249 RemoveDisplay(kDefaultDisplayId); |
| 250 TriggerOnDisplayModeChanged(); |
| 251 |
| 252 // Check the primary display changed because the old primary was removed. |
| 253 EXPECT_EQ(2, platform_screen()->GetPrimaryDisplayId()); |
| 254 } |
| 255 |
| 256 TEST_F(PlatformScreenOzoneTest, RemoveMultipleDisplay) { |
| 257 AddDisplay(2); |
| 258 AddDisplay(3); |
| 259 TriggerOnDisplayModeChanged(); |
| 260 delegate()->Reset(); |
| 261 |
| 262 RemoveDisplay(2); |
| 263 TriggerOnDisplayModeChanged(); |
| 264 |
| 265 // Check that display 2 was removed. |
| 266 ASSERT_THAT(delegate()->removed(), SizeIs(1)); |
| 267 EXPECT_THAT(delegate()->removed()[0], DisplayId(2)); |
| 268 |
| 269 delegate()->Reset(); |
| 270 RemoveDisplay(3); |
| 271 TriggerOnDisplayModeChanged(); |
| 272 |
| 273 // Check that display 3 was removed. |
| 274 ASSERT_THAT(delegate()->removed(), SizeIs(1)); |
| 275 EXPECT_THAT(delegate()->removed()[0], DisplayId(3)); |
| 276 } |
| 277 |
| 278 TEST_F(PlatformScreenOzoneTest, ModifyDisplaySize) { |
| 279 const gfx::Size size1(1920, 1200); |
| 280 const gfx::Size size2(1680, 1050); |
| 281 |
| 282 AddDisplay(2, size1); |
| 283 TriggerOnDisplayModeChanged(); |
| 284 |
| 285 // Check that display 2 was added with expected size. |
| 286 ASSERT_THAT(delegate()->added(), SizeIs(1)); |
| 287 EXPECT_THAT(delegate()->added()[0], DisplayId(2)); |
| 288 EXPECT_THAT(delegate()->added()[0], DisplaySize(size1.ToString())); |
| 289 delegate()->Reset(); |
| 290 |
| 291 ModifyDisplay(2, size2); |
| 292 TriggerOnDisplayModeChanged(); |
| 293 |
| 294 // Check that display 2 was modified to have the new expected size. |
| 295 ASSERT_THAT(delegate()->modified(), SizeIs(1)); |
| 296 EXPECT_THAT(delegate()->modified()[0], DisplayId(2)); |
| 297 EXPECT_THAT(delegate()->modified()[0], DisplaySize(size2.ToString())); |
| 298 } |
| 299 |
| 300 TEST_F(PlatformScreenOzoneTest, ModifyFirstDisplaySize) { |
| 301 const gfx::Size size(1920, 1200); |
| 302 |
| 303 AddDisplay(2, size); |
| 304 TriggerOnDisplayModeChanged(); |
| 305 |
| 306 // Check that display two has the expected initial origin. |
| 307 ASSERT_THAT(delegate()->added(), SizeIs(1)); |
| 308 EXPECT_THAT(delegate()->added()[0], DisplayOrigin("1024,0")); |
| 309 delegate()->Reset(); |
| 310 |
| 311 ModifyDisplay(kDefaultDisplayId, size); |
| 312 TriggerOnDisplayModeChanged(); |
| 313 |
| 314 // Check that the default display was modified with a new size and display 2 |
| 315 // was modified with a new origin. |
| 316 ASSERT_THAT(delegate()->modified(), SizeIs(2)); |
| 317 EXPECT_THAT(delegate()->modified()[0], DisplayId(kDefaultDisplayId)); |
| 318 EXPECT_THAT(delegate()->modified()[0], DisplaySize(size.ToString())); |
| 319 EXPECT_THAT(delegate()->modified()[1], DisplayId(2)); |
| 320 EXPECT_THAT(delegate()->modified()[1], DisplayOrigin("1920,0")); |
| 321 } |
| 322 |
| 323 } // namespace display |
| OLD | NEW |