| OLD | NEW |
| (Empty) |
| 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 | |
| 5 #include "ui/gfx/display_change_notifier.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include "base/macros.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 #include "ui/gfx/display.h" | |
| 12 #include "ui/gfx/display_observer.h" | |
| 13 | |
| 14 namespace gfx { | |
| 15 | |
| 16 class MockDisplayObserver : public DisplayObserver { | |
| 17 public: | |
| 18 MockDisplayObserver() | |
| 19 : display_added_(0), | |
| 20 display_removed_(0), | |
| 21 display_changed_(0), | |
| 22 latest_metrics_change_(DisplayObserver::DISPLAY_METRIC_NONE) | |
| 23 {} | |
| 24 | |
| 25 ~MockDisplayObserver() override {} | |
| 26 | |
| 27 void OnDisplayAdded(const Display& display) override { display_added_++; } | |
| 28 | |
| 29 void OnDisplayRemoved(const Display& display) override { display_removed_++; } | |
| 30 | |
| 31 void OnDisplayMetricsChanged(const Display& display, | |
| 32 uint32_t metrics) override { | |
| 33 display_changed_++; | |
| 34 latest_metrics_change_ = metrics; | |
| 35 } | |
| 36 | |
| 37 int display_added() const { | |
| 38 return display_added_; | |
| 39 } | |
| 40 | |
| 41 int display_removed() const { | |
| 42 return display_removed_; | |
| 43 } | |
| 44 | |
| 45 int display_changed() const { | |
| 46 return display_changed_; | |
| 47 } | |
| 48 | |
| 49 uint32_t latest_metrics_change() const { | |
| 50 return latest_metrics_change_; | |
| 51 } | |
| 52 | |
| 53 protected: | |
| 54 int display_added_; | |
| 55 int display_removed_; | |
| 56 int display_changed_; | |
| 57 uint32_t latest_metrics_change_; | |
| 58 | |
| 59 DISALLOW_COPY_AND_ASSIGN(MockDisplayObserver); | |
| 60 }; | |
| 61 | |
| 62 TEST(DisplayChangeNotifierTest, AddObserver_Smoke) { | |
| 63 DisplayChangeNotifier change_notifier; | |
| 64 MockDisplayObserver observer; | |
| 65 | |
| 66 change_notifier.NotifyDisplaysChanged( | |
| 67 std::vector<Display>(), std::vector<Display>(1, Display())); | |
| 68 EXPECT_EQ(0, observer.display_added()); | |
| 69 | |
| 70 change_notifier.AddObserver(&observer); | |
| 71 change_notifier.NotifyDisplaysChanged( | |
| 72 std::vector<Display>(), std::vector<Display>(1, Display())); | |
| 73 EXPECT_EQ(1, observer.display_added()); | |
| 74 } | |
| 75 | |
| 76 TEST(DisplayChangeNotifier, RemoveObserver_Smoke) { | |
| 77 DisplayChangeNotifier change_notifier; | |
| 78 MockDisplayObserver observer; | |
| 79 | |
| 80 change_notifier.NotifyDisplaysChanged( | |
| 81 std::vector<Display>(), std::vector<Display>(1, Display())); | |
| 82 EXPECT_EQ(0, observer.display_added()); | |
| 83 | |
| 84 change_notifier.AddObserver(&observer); | |
| 85 change_notifier.RemoveObserver(&observer); | |
| 86 | |
| 87 change_notifier.NotifyDisplaysChanged( | |
| 88 std::vector<Display>(), std::vector<Display>(1, Display())); | |
| 89 EXPECT_EQ(0, observer.display_added()); | |
| 90 } | |
| 91 | |
| 92 TEST(DisplayChangeNotifierTest, RemoveObserver_Unknown) { | |
| 93 DisplayChangeNotifier change_notifier; | |
| 94 MockDisplayObserver observer; | |
| 95 | |
| 96 change_notifier.RemoveObserver(&observer); | |
| 97 // Should not crash. | |
| 98 } | |
| 99 | |
| 100 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Removed) { | |
| 101 DisplayChangeNotifier change_notifier; | |
| 102 | |
| 103 // If the previous display array is empty, no removal. | |
| 104 { | |
| 105 MockDisplayObserver observer; | |
| 106 change_notifier.AddObserver(&observer); | |
| 107 | |
| 108 std::vector<Display> old_displays, new_displays; | |
| 109 new_displays.push_back(Display()); | |
| 110 | |
| 111 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 112 EXPECT_EQ(0, observer.display_removed()); | |
| 113 | |
| 114 change_notifier.RemoveObserver(&observer); | |
| 115 } | |
| 116 | |
| 117 // If the previous and new display array are empty, no removal. | |
| 118 { | |
| 119 MockDisplayObserver observer; | |
| 120 change_notifier.AddObserver(&observer); | |
| 121 | |
| 122 std::vector<Display> old_displays, new_displays; | |
| 123 | |
| 124 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 125 EXPECT_EQ(0, observer.display_removed()); | |
| 126 | |
| 127 change_notifier.RemoveObserver(&observer); | |
| 128 } | |
| 129 | |
| 130 // If the new display array is empty, there are as many removal as old | |
| 131 // displays. | |
| 132 { | |
| 133 MockDisplayObserver observer; | |
| 134 change_notifier.AddObserver(&observer); | |
| 135 | |
| 136 std::vector<Display> old_displays, new_displays; | |
| 137 old_displays.push_back(Display()); | |
| 138 old_displays.push_back(Display()); | |
| 139 old_displays.push_back(Display()); | |
| 140 | |
| 141 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 142 EXPECT_EQ(3, observer.display_removed()); | |
| 143 | |
| 144 change_notifier.RemoveObserver(&observer); | |
| 145 } | |
| 146 | |
| 147 // If displays don't use ids, as long as the new display array has one | |
| 148 // element, there are no removals. | |
| 149 { | |
| 150 MockDisplayObserver observer; | |
| 151 change_notifier.AddObserver(&observer); | |
| 152 | |
| 153 std::vector<Display> old_displays, new_displays; | |
| 154 old_displays.push_back(Display()); | |
| 155 old_displays.push_back(Display()); | |
| 156 old_displays.push_back(Display()); | |
| 157 new_displays.push_back(Display()); | |
| 158 | |
| 159 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 160 EXPECT_EQ(0, observer.display_removed()); | |
| 161 | |
| 162 change_notifier.RemoveObserver(&observer); | |
| 163 } | |
| 164 | |
| 165 // If displays use ids (and they are unique), ids not present in the new | |
| 166 // display array will be marked as removed. | |
| 167 { | |
| 168 MockDisplayObserver observer; | |
| 169 change_notifier.AddObserver(&observer); | |
| 170 | |
| 171 std::vector<Display> old_displays, new_displays; | |
| 172 old_displays.push_back(Display(1)); | |
| 173 old_displays.push_back(Display(2)); | |
| 174 old_displays.push_back(Display(3)); | |
| 175 new_displays.push_back(Display(2)); | |
| 176 | |
| 177 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 178 EXPECT_EQ(2, observer.display_removed()); | |
| 179 | |
| 180 change_notifier.RemoveObserver(&observer); | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Added) { | |
| 185 DisplayChangeNotifier change_notifier; | |
| 186 | |
| 187 // If the new display array is empty, no addition. | |
| 188 { | |
| 189 MockDisplayObserver observer; | |
| 190 change_notifier.AddObserver(&observer); | |
| 191 | |
| 192 std::vector<Display> old_displays, new_displays; | |
| 193 old_displays.push_back(Display()); | |
| 194 | |
| 195 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 196 EXPECT_EQ(0, observer.display_added()); | |
| 197 | |
| 198 change_notifier.RemoveObserver(&observer); | |
| 199 } | |
| 200 | |
| 201 // If the old and new display arrays are empty, no addition. | |
| 202 { | |
| 203 MockDisplayObserver observer; | |
| 204 change_notifier.AddObserver(&observer); | |
| 205 | |
| 206 std::vector<Display> old_displays, new_displays; | |
| 207 | |
| 208 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 209 EXPECT_EQ(0, observer.display_added()); | |
| 210 | |
| 211 change_notifier.RemoveObserver(&observer); | |
| 212 } | |
| 213 | |
| 214 // If the old display array is empty, there are as many addition as new | |
| 215 // displays. | |
| 216 { | |
| 217 MockDisplayObserver observer; | |
| 218 change_notifier.AddObserver(&observer); | |
| 219 | |
| 220 std::vector<Display> old_displays, new_displays; | |
| 221 new_displays.push_back(Display()); | |
| 222 new_displays.push_back(Display()); | |
| 223 new_displays.push_back(Display()); | |
| 224 | |
| 225 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 226 EXPECT_EQ(3, observer.display_added()); | |
| 227 | |
| 228 change_notifier.RemoveObserver(&observer); | |
| 229 } | |
| 230 | |
| 231 // If displays don't use ids, as long as the old display array has one | |
| 232 // element, there are no additions. | |
| 233 { | |
| 234 MockDisplayObserver observer; | |
| 235 change_notifier.AddObserver(&observer); | |
| 236 | |
| 237 std::vector<Display> old_displays, new_displays; | |
| 238 old_displays.push_back(Display()); | |
| 239 new_displays.push_back(Display()); | |
| 240 new_displays.push_back(Display()); | |
| 241 new_displays.push_back(Display()); | |
| 242 | |
| 243 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 244 EXPECT_EQ(0, observer.display_added()); | |
| 245 | |
| 246 change_notifier.RemoveObserver(&observer); | |
| 247 } | |
| 248 | |
| 249 // If displays use ids (and they are unique), ids not present in the old | |
| 250 // display array will be marked as added. | |
| 251 { | |
| 252 MockDisplayObserver observer; | |
| 253 change_notifier.AddObserver(&observer); | |
| 254 | |
| 255 std::vector<Display> old_displays, new_displays; | |
| 256 old_displays.push_back(Display(1)); | |
| 257 new_displays.push_back(Display(1)); | |
| 258 new_displays.push_back(Display(2)); | |
| 259 new_displays.push_back(Display(3)); | |
| 260 | |
| 261 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 262 EXPECT_EQ(2, observer.display_added()); | |
| 263 | |
| 264 change_notifier.RemoveObserver(&observer); | |
| 265 } | |
| 266 } | |
| 267 | |
| 268 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Smoke) { | |
| 269 DisplayChangeNotifier change_notifier; | |
| 270 | |
| 271 // If the old display array is empty, no change. | |
| 272 { | |
| 273 MockDisplayObserver observer; | |
| 274 change_notifier.AddObserver(&observer); | |
| 275 | |
| 276 std::vector<Display> old_displays, new_displays; | |
| 277 new_displays.push_back(Display()); | |
| 278 | |
| 279 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 280 EXPECT_EQ(0, observer.display_changed()); | |
| 281 | |
| 282 change_notifier.RemoveObserver(&observer); | |
| 283 } | |
| 284 | |
| 285 // If the new display array is empty, no change. | |
| 286 { | |
| 287 MockDisplayObserver observer; | |
| 288 change_notifier.AddObserver(&observer); | |
| 289 | |
| 290 std::vector<Display> old_displays, new_displays; | |
| 291 old_displays.push_back(Display()); | |
| 292 | |
| 293 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 294 EXPECT_EQ(0, observer.display_changed()); | |
| 295 | |
| 296 change_notifier.RemoveObserver(&observer); | |
| 297 } | |
| 298 | |
| 299 // If the old and new display arrays are empty, no change. | |
| 300 { | |
| 301 MockDisplayObserver observer; | |
| 302 change_notifier.AddObserver(&observer); | |
| 303 | |
| 304 std::vector<Display> old_displays, new_displays; | |
| 305 | |
| 306 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 307 EXPECT_EQ(0, observer.display_changed()); | |
| 308 | |
| 309 change_notifier.RemoveObserver(&observer); | |
| 310 } | |
| 311 | |
| 312 // If there is an intersection between old and new displays but there are no | |
| 313 // metrics changes, there is no display change. | |
| 314 { | |
| 315 MockDisplayObserver observer; | |
| 316 change_notifier.AddObserver(&observer); | |
| 317 | |
| 318 std::vector<Display> old_displays, new_displays; | |
| 319 old_displays.push_back(Display(1)); | |
| 320 new_displays.push_back(Display(1)); | |
| 321 new_displays.push_back(Display(2)); | |
| 322 new_displays.push_back(Display(3)); | |
| 323 | |
| 324 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 325 EXPECT_EQ(0, observer.display_changed()); | |
| 326 | |
| 327 change_notifier.RemoveObserver(&observer); | |
| 328 } | |
| 329 } | |
| 330 | |
| 331 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Bounds) { | |
| 332 DisplayChangeNotifier change_notifier; | |
| 333 | |
| 334 { | |
| 335 MockDisplayObserver observer; | |
| 336 change_notifier.AddObserver(&observer); | |
| 337 | |
| 338 std::vector<Display> old_displays, new_displays; | |
| 339 old_displays.push_back(Display(1, Rect(0, 0, 200, 200))); | |
| 340 new_displays.push_back(Display(1, Rect(0, 0, 200, 200))); | |
| 341 | |
| 342 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 343 EXPECT_EQ(0, observer.display_changed()); | |
| 344 | |
| 345 change_notifier.RemoveObserver(&observer); | |
| 346 } | |
| 347 | |
| 348 { | |
| 349 MockDisplayObserver observer; | |
| 350 change_notifier.AddObserver(&observer); | |
| 351 | |
| 352 std::vector<Display> old_displays, new_displays; | |
| 353 old_displays.push_back(Display(1, Rect(0, 0, 200, 200))); | |
| 354 new_displays.push_back(Display(1, Rect(10, 10, 300, 300))); | |
| 355 | |
| 356 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 357 EXPECT_EQ(1, observer.display_changed()); | |
| 358 uint32_t metrics_change = DisplayObserver::DISPLAY_METRIC_BOUNDS | | |
| 359 DisplayObserver::DISPLAY_METRIC_WORK_AREA; | |
| 360 EXPECT_EQ(metrics_change, observer.latest_metrics_change()); | |
| 361 | |
| 362 change_notifier.RemoveObserver(&observer); | |
| 363 } | |
| 364 | |
| 365 { | |
| 366 MockDisplayObserver observer; | |
| 367 change_notifier.AddObserver(&observer); | |
| 368 | |
| 369 std::vector<Display> old_displays, new_displays; | |
| 370 old_displays.push_back(Display(1, Rect(0, 0, 200, 200))); | |
| 371 new_displays.push_back(Display(1, Rect(0, 0, 200, 200))); | |
| 372 new_displays[0].set_bounds(Rect(10, 10, 300, 300)); | |
| 373 | |
| 374 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 375 EXPECT_EQ(1, observer.display_changed()); | |
| 376 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_BOUNDS, | |
| 377 observer.latest_metrics_change()); | |
| 378 | |
| 379 change_notifier.RemoveObserver(&observer); | |
| 380 } | |
| 381 } | |
| 382 | |
| 383 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Rotation) { | |
| 384 DisplayChangeNotifier change_notifier; | |
| 385 MockDisplayObserver observer; | |
| 386 change_notifier.AddObserver(&observer); | |
| 387 | |
| 388 std::vector<Display> old_displays, new_displays; | |
| 389 old_displays.push_back(Display(1)); | |
| 390 old_displays[0].SetRotationAsDegree(0); | |
| 391 new_displays.push_back(Display(1)); | |
| 392 new_displays[0].SetRotationAsDegree(180); | |
| 393 | |
| 394 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 395 EXPECT_EQ(1, observer.display_changed()); | |
| 396 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_ROTATION, | |
| 397 observer.latest_metrics_change()); | |
| 398 } | |
| 399 | |
| 400 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_WorkArea) { | |
| 401 DisplayChangeNotifier change_notifier; | |
| 402 MockDisplayObserver observer; | |
| 403 change_notifier.AddObserver(&observer); | |
| 404 | |
| 405 std::vector<Display> old_displays, new_displays; | |
| 406 old_displays.push_back(Display(1)); | |
| 407 old_displays[0].set_work_area(Rect(0, 0, 200, 200)); | |
| 408 new_displays.push_back(Display(1)); | |
| 409 new_displays[0].set_work_area(Rect(20, 20, 300, 300)); | |
| 410 | |
| 411 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 412 EXPECT_EQ(1, observer.display_changed()); | |
| 413 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_WORK_AREA, | |
| 414 observer.latest_metrics_change()); | |
| 415 } | |
| 416 | |
| 417 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_DSF) { | |
| 418 DisplayChangeNotifier change_notifier; | |
| 419 MockDisplayObserver observer; | |
| 420 change_notifier.AddObserver(&observer); | |
| 421 | |
| 422 std::vector<Display> old_displays, new_displays; | |
| 423 old_displays.push_back(Display(1)); | |
| 424 old_displays[0].set_device_scale_factor(1.f); | |
| 425 new_displays.push_back(Display(1)); | |
| 426 new_displays[0].set_device_scale_factor(2.f); | |
| 427 | |
| 428 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 429 EXPECT_EQ(1, observer.display_changed()); | |
| 430 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR, | |
| 431 observer.latest_metrics_change()); | |
| 432 } | |
| 433 | |
| 434 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Multi_Displays) { | |
| 435 DisplayChangeNotifier change_notifier; | |
| 436 MockDisplayObserver observer; | |
| 437 change_notifier.AddObserver(&observer); | |
| 438 | |
| 439 std::vector<Display> old_displays, new_displays; | |
| 440 old_displays.push_back(Display(1)); | |
| 441 old_displays.push_back(Display(2)); | |
| 442 old_displays.push_back(Display(3)); | |
| 443 new_displays.push_back(Display(1)); | |
| 444 new_displays.push_back(Display(2)); | |
| 445 new_displays.push_back(Display(3)); | |
| 446 | |
| 447 old_displays[0].set_device_scale_factor(1.f); | |
| 448 new_displays[0].set_device_scale_factor(2.f); | |
| 449 | |
| 450 old_displays[1].set_bounds(Rect(0, 0, 200, 200)); | |
| 451 new_displays[1].set_bounds(Rect(0, 0, 400, 400)); | |
| 452 | |
| 453 old_displays[2].SetRotationAsDegree(0); | |
| 454 new_displays[2].SetRotationAsDegree(90); | |
| 455 | |
| 456 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 457 EXPECT_EQ(3, observer.display_changed()); | |
| 458 } | |
| 459 | |
| 460 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Multi_Metrics) { | |
| 461 DisplayChangeNotifier change_notifier; | |
| 462 MockDisplayObserver observer; | |
| 463 change_notifier.AddObserver(&observer); | |
| 464 | |
| 465 std::vector<Display> old_displays, new_displays; | |
| 466 old_displays.push_back(Display(1, Rect(0, 0, 200, 200))); | |
| 467 old_displays[0].set_device_scale_factor(1.f); | |
| 468 old_displays[0].SetRotationAsDegree(0); | |
| 469 | |
| 470 new_displays.push_back(Display(1, Rect(100, 100, 200, 200))); | |
| 471 new_displays[0].set_device_scale_factor(2.f); | |
| 472 new_displays[0].SetRotationAsDegree(90); | |
| 473 | |
| 474 change_notifier.NotifyDisplaysChanged(old_displays, new_displays); | |
| 475 EXPECT_EQ(1, observer.display_changed()); | |
| 476 uint32_t metrics = DisplayObserver::DISPLAY_METRIC_BOUNDS | | |
| 477 DisplayObserver::DISPLAY_METRIC_ROTATION | | |
| 478 DisplayObserver::DISPLAY_METRIC_WORK_AREA | | |
| 479 DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR; | |
| 480 EXPECT_EQ(metrics, observer.latest_metrics_change()); | |
| 481 } | |
| 482 | |
| 483 } // namespace gfx | |
| OLD | NEW |