Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Unified Diff: ui/gfx/display_change_notifier_unittest.cc

Issue 341983008: Listen to Display reconfiguration and notify DisplayObservers on Mac. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mac_display
Patch Set: review comments + tests Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/gfx/display_change_notifier_unittest.cc
diff --git a/ui/gfx/display_change_notifier_unittest.cc b/ui/gfx/display_change_notifier_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..be2147d494278ea21d8f3b496302c161d830a237
--- /dev/null
+++ b/ui/gfx/display_change_notifier_unittest.cc
@@ -0,0 +1,514 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/gfx/display_change_notifier.h"
+
+#include "testing/gtest/include/gtest/gtest.h"
+#include "ui/gfx/display.h"
+#include "ui/gfx/display_observer.h"
+
+using gfx::Display;
+using gfx::DisplayObserver;
+
+#if 0
+class DisplayChangeNotifierTest : public testing::Test {
+ protected:
+ virtual void SetUp() OVERRIDE {}
+ virtual void TearDown() OVERRIDE {}
+};
+#endif // 0
oshima 2014/07/11 19:13:25 remove this?
mlamouri (slow - plz ping) 2014/07/13 15:20:40 Done.
+
+class MockDisplayObserver : public DisplayObserver {
+ public:
+ MockDisplayObserver()
+ : display_added_(0)
+ , display_removed_(0)
oshima 2014/07/11 19:13:25 move ","s to previous line. Or you can just run "g
mlamouri (slow - plz ping) 2014/07/13 15:20:40 Done.
+ , display_changed_(0)
+ , latest_metrics_change_(DisplayObserver::DISPLAY_METRIC_NONE)
+ {}
+
+ virtual void OnDisplayAdded(const Display& display) OVERRIDE {
+ display_added_++;
+ }
+
+ virtual void OnDisplayRemoved(const Display& display) OVERRIDE {
+ display_removed_++;
+ }
+
+ virtual void OnDisplayMetricsChanged(const Display& display,
+ uint32_t metrics) OVERRIDE {
+ display_changed_++;
+ latest_metrics_change_ = metrics;
+ }
+
+ int display_added() const {
+ return display_added_;
+ }
+
+ int display_removed() const {
+ return display_removed_;
+ }
+
+ int display_changed() const {
+ return display_changed_;
+ }
+
+ uint32_t latest_metrics_change() const {
+ return latest_metrics_change_;
+ }
+
+ protected:
+ int display_added_;
+ int display_removed_;
+ int display_changed_;
+ uint32_t latest_metrics_change_;
+};
oshima 2014/07/11 19:13:25 DISALLOW_COPY_AND_ASSIGN
mlamouri (slow - plz ping) 2014/07/13 15:20:40 Done.
+
+namespace {
oshima 2014/07/11 19:13:25 Don't put tests in anonymous namespace because tes
mlamouri (slow - plz ping) 2014/07/13 15:20:40 Done.
+
+TEST(DisplayChangeNotifierTest, AddObserver_Smoke) {
+ gfx::DisplayChangeNotifier change_notifier;
+ MockDisplayObserver observer;
+
+ change_notifier.NotifyDisplaysChanged(
+ std::vector<gfx::Display>(), std::vector<Display>(1, gfx::Display()));
+ EXPECT_EQ(0, observer.display_added());
+
+ change_notifier.AddObserver(&observer);
+ change_notifier.NotifyDisplaysChanged(
+ std::vector<gfx::Display>(), std::vector<Display>(1, gfx::Display()));
+ EXPECT_EQ(1, observer.display_added());
+}
+
+TEST(DisplayChangeNotifierTest, AddObserver_Null) {
+ gfx::DisplayChangeNotifier change_notifier;
+
+ change_notifier.AddObserver(NULL);
+ // Should not crash.
+}
+
+TEST(DisplayChangeNotifierTest, AddObserver_Twice) {
+ gfx::DisplayChangeNotifier change_notifier;
+ MockDisplayObserver observer;
+
+ change_notifier.AddObserver(&observer);
+ ASSERT_DEATH(change_notifier.AddObserver(&observer),
+ "Observers can only be added once!");
oshima 2014/07/11 19:13:25 Null/Twice/Unknowns are covered by ObserverList's
mlamouri (slow - plz ping) 2014/07/13 15:20:40 ObserverList's tests should indeed cover that but
+}
+
+TEST(DisplayChangeNotifier, RemoveObserver_Smoke) {
+ gfx::DisplayChangeNotifier change_notifier;
+ MockDisplayObserver observer;
+
+ change_notifier.NotifyDisplaysChanged(
+ std::vector<gfx::Display>(), std::vector<Display>(1, gfx::Display()));
+ EXPECT_EQ(0, observer.display_added());
+
+ change_notifier.AddObserver(&observer);
+ change_notifier.RemoveObserver(&observer);
+
+ change_notifier.NotifyDisplaysChanged(
+ std::vector<gfx::Display>(), std::vector<Display>(1, gfx::Display()));
+ EXPECT_EQ(0, observer.display_added());
+}
+
+TEST(DisplayChangeNotifierTest, RemoveObserver_Null) {
+ gfx::DisplayChangeNotifier change_notifier;
+
+ change_notifier.RemoveObserver(NULL);
+ // Should not crash.
+}
+
+TEST(DisplayChangeNotifierTest, RemoveObserver_Unknown) {
+ gfx::DisplayChangeNotifier change_notifier;
+ MockDisplayObserver observer;
+
+ change_notifier.RemoveObserver(&observer);
+ // Should not crash.
+}
+
+TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Removed) {
+ gfx::DisplayChangeNotifier change_notifier;
+
+ // If the previous display array is empty, no removal.
+ {
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+ new_displays.push_back(gfx::Display());
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(0, observer.display_removed());
+
+ change_notifier.RemoveObserver(&observer);
+ }
+
+ // If the previous and new display array are empty, no removal.
+ {
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(0, observer.display_removed());
+
+ change_notifier.RemoveObserver(&observer);
+ }
+
+ // If the new display array is empty, there are as many removal as old
+ // displays.
+ {
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+ old_displays.push_back(Display());
+ old_displays.push_back(Display());
+ old_displays.push_back(Display());
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(3, observer.display_removed());
+
+ change_notifier.RemoveObserver(&observer);
+ }
+
+ // If displays don't use ids, as long as the new display array has one
+ // element, there are no removals.
+ {
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+ old_displays.push_back(Display());
+ old_displays.push_back(Display());
+ old_displays.push_back(Display());
+ new_displays.push_back(Display());
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(0, observer.display_removed());
+
+ change_notifier.RemoveObserver(&observer);
+ }
+
+ // If displays use ids (and they are unique), ids not present in the new
+ // display array will be marked as removed.
+ {
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+ old_displays.push_back(Display(1));
+ old_displays.push_back(Display(2));
+ old_displays.push_back(Display(3));
+ new_displays.push_back(Display(2));
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(2, observer.display_removed());
+
+ change_notifier.RemoveObserver(&observer);
+ }
+}
+
+TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Added) {
+ gfx::DisplayChangeNotifier change_notifier;
+
+ // If the new display array is empty, no addition.
+ {
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+ old_displays.push_back(gfx::Display());
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(0, observer.display_added());
+
+ change_notifier.RemoveObserver(&observer);
+ }
+
+ // If the old and new display arrays are empty, no addition.
+ {
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(0, observer.display_added());
+
+ change_notifier.RemoveObserver(&observer);
+ }
+
+ // If the old display array is empty, there are as many addition as new
+ // displays.
+ {
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+ new_displays.push_back(Display());
+ new_displays.push_back(Display());
+ new_displays.push_back(Display());
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(3, observer.display_added());
+
+ change_notifier.RemoveObserver(&observer);
+ }
+
+ // If displays don't use ids, as long as the old display array has one
+ // element, there are no additions.
+ {
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+ old_displays.push_back(Display());
+ new_displays.push_back(Display());
+ new_displays.push_back(Display());
+ new_displays.push_back(Display());
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(0, observer.display_added());
+
+ change_notifier.RemoveObserver(&observer);
+ }
+
+ // If displays use ids (and they are unique), ids not present in the old
+ // display array will be marked as added.
+ {
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+ old_displays.push_back(Display(1));
+ new_displays.push_back(Display(1));
+ new_displays.push_back(Display(2));
+ new_displays.push_back(Display(3));
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(2, observer.display_added());
+
+ change_notifier.RemoveObserver(&observer);
+ }
+}
+
+TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Smoke) {
+ gfx::DisplayChangeNotifier change_notifier;
+
+ // If the old display array is empty, no change.
+ {
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+ new_displays.push_back(gfx::Display());
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(0, observer.display_changed());
+
+ change_notifier.RemoveObserver(&observer);
+ }
+
+ // If the new display array is empty, no change.
+ {
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+ old_displays.push_back(gfx::Display());
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(0, observer.display_changed());
+
+ change_notifier.RemoveObserver(&observer);
+ }
+
+ // If the old and new display arrays are empty, no change.
+ {
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(0, observer.display_changed());
+
+ change_notifier.RemoveObserver(&observer);
+ }
+
+ // If there is an intersection between old and new displays but there are no
+ // metrics changes, there is no display change.
+ {
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+ old_displays.push_back(Display(1));
+ new_displays.push_back(Display(1));
+ new_displays.push_back(Display(2));
+ new_displays.push_back(Display(3));
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(0, observer.display_changed());
+
+ change_notifier.RemoveObserver(&observer);
+ }
+}
+
+TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Bounds) {
+ gfx::DisplayChangeNotifier change_notifier;
+
+ {
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+ old_displays.push_back(Display(1, gfx::Rect(0, 0, 200, 200)));
+ new_displays.push_back(Display(1, gfx::Rect(0, 0, 200, 200)));
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(0, observer.display_changed());
+
+ change_notifier.RemoveObserver(&observer);
+ }
+
+ {
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+ old_displays.push_back(Display(1, gfx::Rect(0, 0, 200, 200)));
+ new_displays.push_back(Display(1, gfx::Rect(10, 10, 300, 300)));
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(1, observer.display_changed());
+ uint32_t metrics_change = DisplayObserver::DISPLAY_METRIC_BOUNDS |
+ DisplayObserver::DISPLAY_METRIC_WORK_AREA;
+ EXPECT_EQ(metrics_change, observer.latest_metrics_change());
+
+ change_notifier.RemoveObserver(&observer);
+ }
+
+ {
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+ old_displays.push_back(Display(1, gfx::Rect(0, 0, 200, 200)));
+ new_displays.push_back(Display(1, gfx::Rect(0, 0, 200, 200)));
+ new_displays[0].set_bounds(gfx::Rect(10, 10, 300, 300));
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(1, observer.display_changed());
+ EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_BOUNDS,
+ observer.latest_metrics_change());
+
+ change_notifier.RemoveObserver(&observer);
+ }
+}
+
+TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Rotation) {
+ gfx::DisplayChangeNotifier change_notifier;
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+ old_displays.push_back(Display(1));
+ old_displays[0].SetRotationAsDegree(0);
+ new_displays.push_back(Display(1));
+ new_displays[0].SetRotationAsDegree(180);
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(1, observer.display_changed());
+ EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_ROTATION,
+ observer.latest_metrics_change());
+}
+
+TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_WorkArea) {
+ gfx::DisplayChangeNotifier change_notifier;
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+ old_displays.push_back(Display(1));
+ old_displays[0].set_work_area(gfx::Rect(0, 0, 200, 200));
+ new_displays.push_back(Display(1));
+ new_displays[0].set_work_area(gfx::Rect(20, 20, 300, 300));
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(1, observer.display_changed());
+ EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_WORK_AREA,
+ observer.latest_metrics_change());
+}
+
+TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_DSF) {
+ gfx::DisplayChangeNotifier change_notifier;
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+ old_displays.push_back(Display(1));
+ old_displays[0].set_device_scale_factor(1.f);
+ new_displays.push_back(Display(1));
+ new_displays[0].set_device_scale_factor(2.f);
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(1, observer.display_changed());
+ EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR,
+ observer.latest_metrics_change());
+}
+
+TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Multi_Displays) {
+ gfx::DisplayChangeNotifier change_notifier;
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+ old_displays.push_back(Display(1));
+ old_displays.push_back(Display(2));
+ old_displays.push_back(Display(3));
+ new_displays.push_back(Display(1));
+ new_displays.push_back(Display(2));
+ new_displays.push_back(Display(3));
+
+ old_displays[0].set_device_scale_factor(1.f);
+ new_displays[0].set_device_scale_factor(2.f);
+
+ old_displays[1].set_bounds(gfx::Rect(0, 0, 200, 200));
+ new_displays[1].set_bounds(gfx::Rect(0, 0, 400, 400));
+
+ old_displays[2].SetRotationAsDegree(0);
+ new_displays[2].SetRotationAsDegree(90);
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(3, observer.display_changed());
+}
+
+TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Multi_Metrics) {
+ gfx::DisplayChangeNotifier change_notifier;
+ MockDisplayObserver observer;
+ change_notifier.AddObserver(&observer);
+
+ std::vector<Display> old_displays, new_displays;
+ old_displays.push_back(Display(1, gfx::Rect(0, 0, 200, 200)));
+ old_displays[0].set_device_scale_factor(1.f);
+ old_displays[0].SetRotationAsDegree(0);
+
+ new_displays.push_back(Display(1, gfx::Rect(100, 100, 200, 200)));
+ new_displays[0].set_device_scale_factor(2.f);
+ new_displays[0].SetRotationAsDegree(90);
+
+ change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
+ EXPECT_EQ(1, observer.display_changed());
+ uint32_t metrics = DisplayObserver::DISPLAY_METRIC_BOUNDS |
+ DisplayObserver::DISPLAY_METRIC_ROTATION |
+ DisplayObserver::DISPLAY_METRIC_WORK_AREA |
+ DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR;
+ EXPECT_EQ(metrics, observer.latest_metrics_change());
+}
+
+}

Powered by Google App Engine
This is Rietveld 408576698