Chromium Code Reviews| 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/gtest/include/gtest/gtest.h" | |
| 13 #include "ui/display/chromeos/display_configurator.h" | |
| 14 #include "ui/display/chromeos/display_snapshot_virtual.h" | |
| 15 #include "ui/display/types/display_constants.h" | |
| 16 #include "ui/display/types/display_mode.h" | |
| 17 #include "ui/display/types/display_snapshot.h" | |
| 18 #include "ui/ozone/public/ozone_platform.h" | |
| 19 | |
| 20 namespace display { | |
| 21 | |
| 22 using ui::DisplayConfigurator; | |
| 23 using ui::DisplayMode; | |
| 24 using ui::DisplaySnapshot; | |
| 25 using ui::DisplaySnapshotVirtual; | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 const int64_t kDefaultDisplayId = 36028797018963969; | |
|
rjkroege
2016/08/30 18:41:59
reasoning for number?
kylechar
2016/08/31 15:58:01
Added comment.
| |
| 30 | |
| 31 // Make a DisplaySnapshot with specified id and size. | |
| 32 std::unique_ptr<DisplaySnapshot> MakeSnapshot(int64_t id, | |
| 33 const gfx::Size& size) { | |
| 34 auto snapshot = base::MakeUnique<DisplaySnapshotVirtual>(id, size); | |
| 35 snapshot->set_current_mode(snapshot->modes()[0].get()); | |
| 36 return snapshot; | |
| 37 } | |
| 38 | |
| 39 // Functor to find DisplaySnapshot with specified id. | |
| 40 class DisplayIdPredicate { | |
|
rjkroege
2016/08/30 18:41:59
can't you just have a "FindForID()" method instead
kylechar
2016/08/31 15:58:01
I added that where it's clearer. I've also just us
| |
| 41 public: | |
| 42 explicit DisplayIdPredicate(int64_t id) : id_(id) {} | |
| 43 bool operator()(const std::unique_ptr<DisplaySnapshot>& snapshot) const { | |
| 44 return snapshot->display_id() == id_; | |
| 45 } | |
| 46 | |
| 47 private: | |
| 48 int64_t id_; | |
| 49 }; | |
| 50 | |
| 51 // Test delegate to track what functions calls the delegate receives. | |
| 52 class TestPlatformScreenDelegate : public PlatformScreenDelegate { | |
|
rjkroege
2016/08/30 18:41:59
seems like mocks would be handy? Is mus allowed to
kylechar
2016/08/31 15:58:01
I'm not sure I understand the mocks bit since Plat
| |
| 53 public: | |
| 54 struct DisplayState { | |
| 55 int64_t id; | |
| 56 gfx::Rect bounds; | |
| 57 }; | |
| 58 | |
| 59 TestPlatformScreenDelegate() {} | |
| 60 ~TestPlatformScreenDelegate() override {} | |
| 61 | |
| 62 std::vector<DisplayState> added() { return added_; } | |
| 63 std::vector<int64_t> removed() { return removed_; } | |
| 64 std::vector<DisplayState> modified() { return modified_; } | |
| 65 | |
| 66 void Reset() { | |
| 67 added_.clear(); | |
| 68 removed_.clear(); | |
| 69 modified_.clear(); | |
| 70 } | |
| 71 | |
| 72 private: | |
| 73 void OnDisplayAdded(PlatformScreen* platform_screen, | |
| 74 int64_t id, | |
| 75 const gfx::Rect& bounds) override { | |
| 76 added_.push_back({id, bounds}); | |
| 77 } | |
| 78 | |
| 79 void OnDisplayRemoved(int64_t id) override { removed_.push_back(id); } | |
| 80 | |
| 81 void OnDisplayModified(int64_t id, const gfx::Rect& bounds) override { | |
| 82 modified_.push_back({id, bounds}); | |
| 83 } | |
| 84 | |
| 85 std::vector<DisplayState> added_; | |
| 86 std::vector<int64_t> removed_; | |
| 87 std::vector<DisplayState> modified_; | |
| 88 | |
| 89 DISALLOW_COPY_AND_ASSIGN(TestPlatformScreenDelegate); | |
| 90 }; | |
| 91 | |
| 92 // Test case that updates PlatformOzoneImplOzone with helpers to trigger | |
| 93 // OnDisplay | |
| 94 class PlatformScreenOzoneTest : public testing::Test { | |
| 95 public: | |
| 96 PlatformScreenOzoneTest() {} | |
| 97 ~PlatformScreenOzoneTest() override {} | |
| 98 | |
| 99 PlatformScreen* platform_screen() { return platform_screen_.get(); } | |
| 100 TestPlatformScreenDelegate* delegate() { return &delegate_; } | |
| 101 | |
| 102 // Adds a display snapshot with specified ID and default size. | |
| 103 void AddDisplay(int64_t id) { return AddDisplay(id, gfx::Size(1024, 768)); } | |
| 104 | |
| 105 // Adds a display snapshot with specified ID and size to list of snapshots. | |
| 106 void AddDisplay(int64_t id, const gfx::Size& size) { | |
| 107 ASSERT_EQ(std::find_if(displays_.begin(), displays_.end(), | |
| 108 DisplayIdPredicate(id)), | |
| 109 displays_.end()); | |
| 110 displays_.push_back(MakeSnapshot(id, size)); | |
| 111 } | |
| 112 | |
| 113 // Removes display snapshot with specified ID. | |
| 114 void RemoveDisplay(int64_t id) { | |
| 115 size_t old_size = displays_.size(); | |
| 116 displays_.erase(std::remove_if(displays_.begin(), displays_.end(), | |
| 117 DisplayIdPredicate(id))); | |
| 118 ASSERT_EQ(old_size, displays_.size() + 1); | |
| 119 } | |
| 120 | |
| 121 // Modify the size of the display snapshot with specified ID. | |
| 122 void ModifyDisplay(int64_t id, const gfx::Size& size) { | |
| 123 auto iter = std::find_if(displays_.begin(), displays_.end(), | |
| 124 DisplayIdPredicate(id)); | |
| 125 ASSERT_NE(iter, displays_.end()); | |
| 126 DisplaySnapshot* snapshot = (*iter).get(); | |
| 127 | |
| 128 const DisplayMode* new_mode = nullptr; | |
| 129 for (auto& mode : snapshot->modes()) { | |
| 130 if (mode->size() == size) { | |
| 131 new_mode = mode.get(); | |
| 132 break; | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 if (!new_mode) { | |
| 137 snapshot->add_mode(new DisplayMode(size, false, 30.0f)); | |
| 138 new_mode = snapshot->modes().back().get(); | |
| 139 } | |
| 140 | |
| 141 snapshot->set_current_mode(new_mode); | |
| 142 } | |
| 143 | |
| 144 // Calls OnDisplayModeChanged with our list of display snapshots. | |
| 145 void TriggerOnDisplayModeChanged() { | |
| 146 std::vector<DisplaySnapshot*> snapshots; | |
| 147 for (auto& snapshot : displays_) { | |
| 148 snapshots.push_back(snapshot.get()); | |
| 149 } | |
| 150 static_cast<DisplayConfigurator::Observer*>(platform_screen_.get()) | |
| 151 ->OnDisplayModeChanged(snapshots); | |
| 152 } | |
| 153 | |
| 154 private: | |
| 155 // testing::Test: | |
| 156 void SetUp() override { | |
| 157 testing::Test::SetUp(); | |
| 158 ui::OzonePlatform::InitializeForUI(); | |
| 159 platform_screen_ = base::MakeUnique<PlatformScreenImplOzone>(); | |
| 160 platform_screen_->Init(&delegate_); | |
| 161 | |
| 162 // Double check the expected display exists and clear counters. | |
| 163 ASSERT_EQ(1u, delegate_.added().size()); | |
| 164 ASSERT_EQ(kDefaultDisplayId, delegate_.added()[0].id); | |
| 165 delegate_.Reset(); | |
| 166 | |
| 167 // Make the initial list of snapshots match what exists. | |
| 168 AddDisplay(kDefaultDisplayId); | |
| 169 } | |
| 170 | |
| 171 void TearDown() override { | |
| 172 displays_.clear(); | |
| 173 delegate_.Reset(); | |
| 174 platform_screen_.reset(); | |
| 175 } | |
| 176 | |
| 177 TestPlatformScreenDelegate delegate_; | |
| 178 std::unique_ptr<PlatformScreenImplOzone> platform_screen_; | |
| 179 std::vector<std::unique_ptr<DisplaySnapshot>> displays_; | |
| 180 }; | |
| 181 | |
| 182 } // namespace | |
| 183 | |
| 184 TEST_F(PlatformScreenOzoneTest, AddDisplay) { | |
| 185 AddDisplay(2); | |
| 186 TriggerOnDisplayModeChanged(); | |
| 187 | |
| 188 // Check that display 2 was added. | |
| 189 ASSERT_EQ(1u, delegate()->added().size()); | |
| 190 EXPECT_EQ(2, delegate()->added()[0].id); | |
| 191 EXPECT_EQ(0u, delegate()->removed().size()); | |
| 192 EXPECT_EQ(0u, delegate()->modified().size()); | |
| 193 } | |
| 194 | |
| 195 TEST_F(PlatformScreenOzoneTest, RemoveDisplay) { | |
| 196 AddDisplay(2); | |
| 197 TriggerOnDisplayModeChanged(); | |
| 198 delegate()->Reset(); | |
| 199 | |
| 200 RemoveDisplay(2); | |
| 201 TriggerOnDisplayModeChanged(); | |
| 202 | |
| 203 // Check that display 2 was removed. | |
| 204 ASSERT_EQ(1u, delegate()->removed().size()); | |
| 205 EXPECT_EQ(2, delegate()->removed()[0]); | |
| 206 EXPECT_EQ(0u, delegate()->added().size()); | |
| 207 EXPECT_EQ(0u, delegate()->added().size()); | |
| 208 } | |
| 209 | |
| 210 TEST_F(PlatformScreenOzoneTest, RemovePrimaryDisplay) { | |
| 211 AddDisplay(2); | |
| 212 TriggerOnDisplayModeChanged(); | |
| 213 delegate()->Reset(); | |
| 214 | |
| 215 RemoveDisplay(kDefaultDisplayId); | |
| 216 TriggerOnDisplayModeChanged(); | |
| 217 | |
| 218 // Check that the default display was removed and display 2 was modified due | |
| 219 // to the origin changing. | |
| 220 EXPECT_EQ(0u, delegate()->added().size()); | |
| 221 ASSERT_EQ(1u, delegate()->removed().size()); | |
| 222 EXPECT_EQ(kDefaultDisplayId, delegate()->removed()[0]); | |
| 223 ASSERT_EQ(1u, delegate()->modified().size()); | |
| 224 EXPECT_EQ(2, delegate()->modified()[0].id); | |
| 225 EXPECT_EQ(0, delegate()->modified()[0].bounds.x()); | |
| 226 | |
| 227 // Also check the primary display changed because the old primary was removed. | |
| 228 EXPECT_EQ(2, platform_screen()->GetPrimaryDisplayId()); | |
| 229 } | |
| 230 | |
| 231 TEST_F(PlatformScreenOzoneTest, RemoveMultipleDisplay) { | |
| 232 AddDisplay(2); | |
| 233 AddDisplay(3); | |
| 234 TriggerOnDisplayModeChanged(); | |
| 235 delegate()->Reset(); | |
| 236 | |
| 237 RemoveDisplay(2); | |
| 238 TriggerOnDisplayModeChanged(); | |
| 239 | |
| 240 // Check that display 2 was removed. | |
| 241 EXPECT_EQ(1u, delegate()->removed().size()); | |
| 242 EXPECT_EQ(2, delegate()->removed()[0]); | |
| 243 | |
| 244 delegate()->Reset(); | |
| 245 RemoveDisplay(3); | |
| 246 TriggerOnDisplayModeChanged(); | |
| 247 | |
| 248 // Check that display 3 was removed. | |
| 249 EXPECT_EQ(1u, delegate()->removed().size()); | |
| 250 EXPECT_EQ(3, delegate()->removed()[0]); | |
| 251 } | |
| 252 | |
| 253 TEST_F(PlatformScreenOzoneTest, ModifyDisplaySize) { | |
| 254 const gfx::Size size1(1920, 1200); | |
| 255 const gfx::Size size2(1680, 1050); | |
| 256 | |
| 257 AddDisplay(2, size1); | |
| 258 TriggerOnDisplayModeChanged(); | |
| 259 | |
| 260 // Check that display 2 was added with expected size. | |
| 261 EXPECT_EQ(1u, delegate()->added().size()); | |
| 262 EXPECT_EQ(2, delegate()->added()[0].id); | |
| 263 EXPECT_EQ(size1, delegate()->added()[0].bounds.size()); | |
| 264 delegate()->Reset(); | |
| 265 | |
| 266 ModifyDisplay(2, size2); | |
| 267 TriggerOnDisplayModeChanged(); | |
| 268 | |
| 269 // Check that display 2 was modified to have the new expected size. | |
| 270 ASSERT_EQ(1u, delegate()->modified().size()); | |
| 271 EXPECT_EQ(2, delegate()->modified()[0].id); | |
| 272 EXPECT_EQ(size2, delegate()->modified()[0].bounds.size()); | |
| 273 } | |
| 274 | |
| 275 TEST_F(PlatformScreenOzoneTest, ModifyDisplayTriggerOriginChange) { | |
| 276 const gfx::Size size2(1920, 1200); | |
| 277 | |
| 278 AddDisplay(2, size2); | |
| 279 TriggerOnDisplayModeChanged(); | |
| 280 | |
| 281 // Check that display two has the expected initial origin. | |
| 282 ASSERT_EQ(1u, delegate()->added().size()); | |
| 283 EXPECT_EQ(1024, delegate()->added()[0].bounds.x()); | |
| 284 delegate()->Reset(); | |
| 285 | |
| 286 ModifyDisplay(kDefaultDisplayId, size2); | |
| 287 TriggerOnDisplayModeChanged(); | |
| 288 | |
| 289 // Check that the default display was modified with a new size and display 2 | |
| 290 // was modified with a new origin. | |
| 291 ASSERT_EQ(2u, delegate()->modified().size()); | |
| 292 EXPECT_EQ(kDefaultDisplayId, delegate()->modified()[0].id); | |
| 293 EXPECT_EQ(size2, delegate()->modified()[0].bounds.size()); | |
| 294 EXPECT_EQ(2, delegate()->modified()[1].id); | |
| 295 EXPECT_EQ(1920, delegate()->modified()[1].bounds.x()); | |
| 296 } | |
| 297 | |
| 298 } // namespace display | |
| OLD | NEW |