Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
|
sky
2016/09/01 19:25:08
Should this filename by platform_screen_ozone_impl
kylechar
2016/09/02 15:18:34
I'm planning on renaming the others to PlatformScr
| |
| 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 ASSERT_FALSE(GetSnapshotById(id)); | |
|
sky
2016/09/01 19:25:08
If you're going to have ASSERTS in function like t
kylechar
2016/09/02 15:18:34
Oh, I didn't realize it worked that way with gtest
| |
| 119 snapshots_.push_back(MakeSnapshot(id, size)); | |
| 120 } | |
| 121 | |
| 122 // Removes display snapshot with specified ID. | |
| 123 void RemoveDisplay(int64_t id) { | |
| 124 size_t old_size = snapshots_.size(); | |
| 125 snapshots_.erase( | |
| 126 std::remove_if(snapshots_.begin(), snapshots_.end(), | |
| 127 [id](std::unique_ptr<DisplaySnapshot>& snapshot) { | |
| 128 return snapshot->display_id() == id; | |
| 129 })); | |
| 130 ASSERT_EQ(old_size, snapshots_.size() + 1); | |
| 131 } | |
| 132 | |
| 133 // Modify the size of the display snapshot with specified ID. | |
| 134 void ModifyDisplay(int64_t id, const gfx::Size& size) { | |
| 135 DisplaySnapshot* snapshot = GetSnapshotById(id); | |
| 136 ASSERT_TRUE(snapshot); | |
| 137 | |
| 138 const DisplayMode* new_mode = nullptr; | |
| 139 for (auto& mode : snapshot->modes()) { | |
| 140 if (mode->size() == size) { | |
| 141 new_mode = mode.get(); | |
| 142 break; | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 if (!new_mode) { | |
| 147 snapshot->add_mode(new DisplayMode(size, false, 30.0f)); | |
| 148 new_mode = snapshot->modes().back().get(); | |
| 149 } | |
| 150 | |
| 151 snapshot->set_current_mode(new_mode); | |
| 152 } | |
| 153 | |
| 154 // Calls OnDisplayModeChanged with our list of display snapshots. | |
| 155 void TriggerOnDisplayModeChanged() { | |
| 156 std::vector<DisplaySnapshot*> snapshots_ptrs; | |
| 157 for (auto& snapshot : snapshots_) { | |
| 158 snapshots_ptrs.push_back(snapshot.get()); | |
| 159 } | |
| 160 static_cast<DisplayConfigurator::Observer*>(platform_screen_.get()) | |
| 161 ->OnDisplayModeChanged(snapshots_ptrs); | |
| 162 } | |
| 163 | |
| 164 private: | |
| 165 DisplaySnapshot* GetSnapshotById(int64_t id) { | |
| 166 for (auto& snapshot : snapshots_) { | |
| 167 if (snapshot->display_id() == id) | |
| 168 return snapshot.get(); | |
| 169 } | |
| 170 return nullptr; | |
| 171 } | |
| 172 | |
| 173 // testing::Test: | |
| 174 void SetUp() override { | |
| 175 testing::Test::SetUp(); | |
| 176 ui::OzonePlatform::InitializeForUI(); | |
| 177 platform_screen_ = base::MakeUnique<PlatformScreenImplOzone>(); | |
| 178 platform_screen_->Init(&delegate_); | |
| 179 | |
| 180 // Double check the expected display exists and clear counters. | |
| 181 ASSERT_THAT(delegate()->added(), SizeIs(1)); | |
| 182 ASSERT_THAT(delegate_.added()[0], DisplayId(kDefaultDisplayId)); | |
| 183 ASSERT_THAT(delegate_.added()[0], DisplayOrigin("0,0")); | |
| 184 ASSERT_THAT(delegate_.added()[0], DisplaySize("1024x768")); | |
| 185 delegate_.Reset(); | |
| 186 | |
| 187 // Make the initial list of snapshots match what exists. | |
| 188 AddDisplay(kDefaultDisplayId); | |
| 189 } | |
| 190 | |
| 191 void TearDown() override { | |
| 192 snapshots_.clear(); | |
| 193 delegate_.Reset(); | |
| 194 platform_screen_.reset(); | |
| 195 } | |
| 196 | |
| 197 TestPlatformScreenDelegate delegate_; | |
| 198 std::unique_ptr<PlatformScreenImplOzone> platform_screen_; | |
| 199 std::vector<std::unique_ptr<DisplaySnapshot>> snapshots_; | |
| 200 }; | |
| 201 | |
| 202 } // namespace | |
| 203 | |
| 204 TEST_F(PlatformScreenOzoneTest, AddDisplay) { | |
| 205 AddDisplay(2); | |
| 206 TriggerOnDisplayModeChanged(); | |
| 207 | |
| 208 // Check that display 2 was added. | |
| 209 ASSERT_THAT(delegate()->added(), SizeIs(1)); | |
| 210 EXPECT_THAT(delegate()->added()[0], DisplayId(2)); | |
| 211 EXPECT_THAT(delegate()->removed(), IsEmpty()); | |
| 212 EXPECT_THAT(delegate()->modified(), IsEmpty()); | |
| 213 } | |
| 214 | |
| 215 TEST_F(PlatformScreenOzoneTest, RemoveDisplay) { | |
| 216 AddDisplay(2); | |
| 217 TriggerOnDisplayModeChanged(); | |
| 218 delegate()->Reset(); | |
| 219 | |
| 220 RemoveDisplay(2); | |
| 221 TriggerOnDisplayModeChanged(); | |
| 222 | |
| 223 // Check that display 2 was removed. | |
| 224 ASSERT_THAT(delegate()->removed(), SizeIs(1)); | |
| 225 EXPECT_THAT(delegate()->removed()[0], DisplayId(2)); | |
| 226 EXPECT_THAT(delegate()->added(), IsEmpty()); | |
| 227 EXPECT_THAT(delegate()->modified(), IsEmpty()); | |
| 228 } | |
| 229 | |
| 230 TEST_F(PlatformScreenOzoneTest, RemoveFirstDisplay) { | |
| 231 AddDisplay(2); | |
| 232 TriggerOnDisplayModeChanged(); | |
| 233 delegate()->Reset(); | |
| 234 | |
| 235 RemoveDisplay(kDefaultDisplayId); | |
| 236 TriggerOnDisplayModeChanged(); | |
| 237 | |
| 238 // Check that the default display was removed and display 2 was modified due | |
| 239 // to the origin changing. | |
| 240 EXPECT_THAT(delegate()->added(), IsEmpty()); | |
| 241 ASSERT_THAT(delegate()->removed(), SizeIs(1)); | |
| 242 EXPECT_THAT(delegate()->removed()[0], DisplayId(kDefaultDisplayId)); | |
| 243 | |
| 244 ASSERT_THAT(delegate()->modified(), SizeIs(1)); | |
| 245 EXPECT_THAT(delegate()->modified()[0], DisplayId(2)); | |
| 246 EXPECT_THAT(delegate()->modified()[0], DisplayOrigin("0,0")); | |
| 247 } | |
| 248 | |
| 249 TEST_F(PlatformScreenOzoneTest, RemovePrimaryDisplay) { | |
| 250 EXPECT_EQ(kDefaultDisplayId, platform_screen()->GetPrimaryDisplayId()); | |
| 251 | |
| 252 AddDisplay(2); | |
| 253 RemoveDisplay(kDefaultDisplayId); | |
| 254 TriggerOnDisplayModeChanged(); | |
| 255 | |
| 256 // Check the primary display changed because the old primary was removed. | |
| 257 EXPECT_EQ(2, platform_screen()->GetPrimaryDisplayId()); | |
| 258 } | |
| 259 | |
| 260 TEST_F(PlatformScreenOzoneTest, RemoveMultipleDisplay) { | |
| 261 AddDisplay(2); | |
| 262 AddDisplay(3); | |
| 263 TriggerOnDisplayModeChanged(); | |
| 264 delegate()->Reset(); | |
| 265 | |
| 266 RemoveDisplay(2); | |
| 267 TriggerOnDisplayModeChanged(); | |
| 268 | |
| 269 // Check that display 2 was removed. | |
| 270 ASSERT_THAT(delegate()->removed(), SizeIs(1)); | |
| 271 EXPECT_THAT(delegate()->removed()[0], DisplayId(2)); | |
| 272 | |
| 273 delegate()->Reset(); | |
| 274 RemoveDisplay(3); | |
| 275 TriggerOnDisplayModeChanged(); | |
| 276 | |
| 277 // Check that display 3 was removed. | |
| 278 ASSERT_THAT(delegate()->removed(), SizeIs(1)); | |
| 279 EXPECT_THAT(delegate()->removed()[0], DisplayId(3)); | |
| 280 } | |
| 281 | |
| 282 TEST_F(PlatformScreenOzoneTest, ModifyDisplaySize) { | |
| 283 const gfx::Size size1(1920, 1200); | |
| 284 const gfx::Size size2(1680, 1050); | |
| 285 | |
| 286 AddDisplay(2, size1); | |
| 287 TriggerOnDisplayModeChanged(); | |
| 288 | |
| 289 // Check that display 2 was added with expected size. | |
| 290 ASSERT_THAT(delegate()->added(), SizeIs(1)); | |
| 291 EXPECT_THAT(delegate()->added()[0], DisplayId(2)); | |
| 292 EXPECT_THAT(delegate()->added()[0], DisplaySize(size1.ToString())); | |
| 293 delegate()->Reset(); | |
| 294 | |
| 295 ModifyDisplay(2, size2); | |
| 296 TriggerOnDisplayModeChanged(); | |
| 297 | |
| 298 // Check that display 2 was modified to have the new expected size. | |
| 299 ASSERT_THAT(delegate()->modified(), SizeIs(1)); | |
| 300 EXPECT_THAT(delegate()->modified()[0], DisplayId(2)); | |
| 301 EXPECT_THAT(delegate()->modified()[0], DisplaySize(size2.ToString())); | |
| 302 } | |
| 303 | |
| 304 TEST_F(PlatformScreenOzoneTest, ModifyFirstDisplaySize) { | |
| 305 const gfx::Size size(1920, 1200); | |
| 306 | |
| 307 AddDisplay(2, size); | |
| 308 TriggerOnDisplayModeChanged(); | |
| 309 | |
| 310 // Check that display two has the expected initial origin. | |
| 311 ASSERT_THAT(delegate()->added(), SizeIs(1)); | |
| 312 EXPECT_THAT(delegate()->added()[0], DisplayOrigin("1024,0")); | |
| 313 delegate()->Reset(); | |
| 314 | |
| 315 ModifyDisplay(kDefaultDisplayId, size); | |
| 316 TriggerOnDisplayModeChanged(); | |
| 317 | |
| 318 // Check that the default display was modified with a new size and display 2 | |
| 319 // was modified with a new origin. | |
| 320 ASSERT_THAT(delegate()->modified(), SizeIs(2)); | |
| 321 EXPECT_THAT(delegate()->modified()[0], DisplayId(kDefaultDisplayId)); | |
| 322 EXPECT_THAT(delegate()->modified()[0], DisplaySize(size.ToString())); | |
| 323 EXPECT_THAT(delegate()->modified()[1], DisplayId(2)); | |
| 324 EXPECT_THAT(delegate()->modified()[1], DisplayOrigin("1920,0")); | |
| 325 } | |
| 326 | |
| 327 } // namespace display | |
| OLD | NEW |