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

Side by Side Diff: services/ui/display/platform_screen_ozone_unittests.cc

Issue 2549503002: Rename PlatformScreen to ScreenManager. (Closed)
Patch Set: Rebase. Created 4 years 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 unified diff | Download patch
OLDNEW
(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/command_line.h"
9 #include "base/logging.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "services/ui/display/platform_screen.h"
13 #include "services/ui/display/platform_screen_ozone.h"
14 #include "services/ui/display/viewport_metrics.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/display/display_switches.h"
18 #include "ui/display/fake_display_snapshot.h"
19 #include "ui/display/types/display_constants.h"
20 #include "ui/display/types/display_mode.h"
21 #include "ui/display/types/display_snapshot.h"
22 #include "ui/ozone/public/ozone_platform.h"
23
24 namespace display {
25
26 using ui::DisplayMode;
27 using ui::DisplaySnapshot;
28 using testing::IsEmpty;
29 using testing::SizeIs;
30
31 namespace {
32
33 // Holds info about the display state we want to test.
34 struct DisplayState {
35 int64_t id;
36 ViewportMetrics metrics;
37 };
38
39 // Matchers that operate on DisplayState.
40 MATCHER_P(DisplayId, display_id, "") {
41 *result_listener << "has id " << arg.id;
42 return arg.id == display_id;
43 }
44
45 MATCHER_P(DisplaySize, size_string, "") {
46 *result_listener << "has size " << arg.metrics.bounds.size().ToString();
47 return arg.metrics.bounds.size().ToString() == size_string;
48 }
49
50 MATCHER_P(DisplayOrigin, origin_string, "") {
51 *result_listener << "has origin " << arg.metrics.bounds.origin().ToString();
52 return arg.metrics.bounds.origin().ToString() == origin_string;
53 }
54
55 // Make a DisplaySnapshot with specified id and size.
56 std::unique_ptr<DisplaySnapshot> MakeSnapshot(int64_t id,
57 const gfx::Size& size) {
58 return FakeDisplaySnapshot::Builder()
59 .SetId(id)
60 .SetNativeMode(size)
61 .SetCurrentMode(size)
62 .Build();
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 const std::vector<DisplayState>& added() const { return added_; }
72 const std::vector<DisplayState>& modified() const { return modified_; }
73
74 // Returns a string containing the function calls that PlatformScreenDelegate
75 // has received in the order they occured. Each function call will be in the
76 // form "<action>(<id>)" and multiple function calls will be separated by ";".
77 // For example, if display 2 was added then display 1 was modified, changes()
78 // would return "Added(2);Modified(1)".
79 const std::string& changes() const { return changes_; }
80
81 void Reset() {
82 added_.clear();
83 modified_.clear();
84 changes_.clear();
85 }
86
87 private:
88 void AddChange(const std::string& name, const std::string& value) {
89 if (!changes_.empty())
90 changes_ += ";";
91 changes_ += name + "(" + value + ")";
92 }
93
94 void OnDisplayAdded(int64_t id, const ViewportMetrics& metrics) override {
95 added_.push_back({id, metrics});
96 AddChange("Added", base::Int64ToString(id));
97 }
98
99 void OnDisplayRemoved(int64_t id) override {
100 AddChange("Removed", base::Int64ToString(id));
101 }
102
103 void OnDisplayModified(int64_t id, const ViewportMetrics& metrics) override {
104 modified_.push_back({id, metrics});
105 AddChange("Modified", base::Int64ToString(id));
106 }
107
108 void OnPrimaryDisplayChanged(int64_t primary_display_id) override {
109 AddChange("Primary", base::Int64ToString(primary_display_id));
110 }
111
112 std::vector<DisplayState> added_;
113 std::vector<DisplayState> modified_;
114 std::string changes_;
115
116 DISALLOW_COPY_AND_ASSIGN(TestPlatformScreenDelegate);
117 };
118
119 } // namespace
120
121 // Test fixture with helpers to act like ui::DisplayConfigurator and send
122 // OnDisplayModeChanged() to PlatformScreenOzone.
123 class PlatformScreenOzoneTest : public testing::Test {
124 public:
125 PlatformScreenOzoneTest() {}
126 ~PlatformScreenOzoneTest() override {}
127
128 PlatformScreenOzone* platform_screen() { return platform_screen_.get(); }
129 TestPlatformScreenDelegate* delegate() { return &delegate_; }
130
131 // Adds a display snapshot with specified ID and default size.
132 void AddDisplay(int64_t id) { return AddDisplay(id, gfx::Size(1024, 768)); }
133
134 // Adds a display snapshot with specified ID and size to list of snapshots.
135 void AddDisplay(int64_t id, const gfx::Size& size) {
136 snapshots_.push_back(MakeSnapshot(id, size));
137 TriggerOnDisplayModeChanged();
138 }
139
140 // Removes display snapshot with specified ID.
141 void RemoveDisplay(int64_t id) {
142 snapshots_.erase(
143 std::remove_if(snapshots_.begin(), snapshots_.end(),
144 [id](std::unique_ptr<DisplaySnapshot>& snapshot) {
145 return snapshot->display_id() == id;
146 }));
147 TriggerOnDisplayModeChanged();
148 }
149
150 // Modify the size of the display snapshot with specified ID.
151 void ModifyDisplay(int64_t id, const gfx::Size& size) {
152 DisplaySnapshot* snapshot = GetSnapshotById(id);
153
154 const DisplayMode* new_mode = nullptr;
155 for (auto& mode : snapshot->modes()) {
156 if (mode->size() == size) {
157 new_mode = mode.get();
158 break;
159 }
160 }
161
162 if (!new_mode) {
163 snapshot->add_mode(new DisplayMode(size, false, 30.0f));
164 new_mode = snapshot->modes().back().get();
165 }
166
167 snapshot->set_current_mode(new_mode);
168 TriggerOnDisplayModeChanged();
169 }
170
171 // Calls OnDisplayModeChanged with our list of display snapshots.
172 void TriggerOnDisplayModeChanged() {
173 std::vector<DisplaySnapshot*> snapshots_ptrs;
174 for (auto& snapshot : snapshots_) {
175 snapshots_ptrs.push_back(snapshot.get());
176 }
177 platform_screen_->OnDisplayModeChanged(snapshots_ptrs);
178 }
179
180 private:
181 DisplaySnapshot* GetSnapshotById(int64_t id) {
182 for (auto& snapshot : snapshots_) {
183 if (snapshot->display_id() == id)
184 return snapshot.get();
185 }
186 return nullptr;
187 }
188
189 // testing::Test:
190 void SetUp() override {
191 base::CommandLine::ForCurrentProcess()->AppendSwitchNative(
192 switches::kScreenConfig, "none");
193
194 testing::Test::SetUp();
195 ui::OzonePlatform::InitializeForUI();
196 platform_screen_ = base::MakeUnique<PlatformScreenOzone>();
197 platform_screen_->Init(&delegate_);
198
199 // Have all tests start with a 1024x768 display by default.
200 AddDisplay(1, gfx::Size(1024, 768));
201 TriggerOnDisplayModeChanged();
202
203 // Double check the expected display exists and clear counters.
204 ASSERT_THAT(delegate()->added(), SizeIs(1));
205 ASSERT_THAT(delegate_.added()[0], DisplayId(1));
206 ASSERT_THAT(delegate_.added()[0], DisplayOrigin("0,0"));
207 ASSERT_THAT(delegate_.added()[0], DisplaySize("1024x768"));
208 ASSERT_EQ("Added(1);Primary(1)", delegate()->changes());
209 delegate_.Reset();
210 }
211
212 void TearDown() override {
213 snapshots_.clear();
214 delegate_.Reset();
215 platform_screen_.reset();
216 }
217
218 TestPlatformScreenDelegate delegate_;
219 std::unique_ptr<PlatformScreenOzone> platform_screen_;
220 std::vector<std::unique_ptr<DisplaySnapshot>> snapshots_;
221 };
222
223 TEST_F(PlatformScreenOzoneTest, AddDisplay) {
224 AddDisplay(2);
225
226 // Check that display 2 was added.
227 EXPECT_EQ("Added(2)", delegate()->changes());
228 }
229
230 TEST_F(PlatformScreenOzoneTest, RemoveDisplay) {
231 AddDisplay(2);
232 delegate()->Reset();
233
234 RemoveDisplay(2);
235
236 // Check that display 2 was removed.
237 EXPECT_EQ("Removed(2)", delegate()->changes());
238 }
239
240 TEST_F(PlatformScreenOzoneTest, RemoveFirstDisplay) {
241 AddDisplay(2);
242 delegate()->Reset();
243
244 RemoveDisplay(1);
245
246 // Check that display 1 was removed and display 2 was modified due to the
247 // origin changing.
248 EXPECT_EQ("Primary(2);Removed(1);Modified(2)", delegate()->changes());
249 ASSERT_THAT(delegate()->modified(), SizeIs(1));
250 EXPECT_THAT(delegate()->modified()[0], DisplayId(2));
251 EXPECT_THAT(delegate()->modified()[0], DisplayOrigin("0,0"));
252 }
253
254 TEST_F(PlatformScreenOzoneTest, RemoveMultipleDisplay) {
255 AddDisplay(2);
256 AddDisplay(3);
257 delegate()->Reset();
258
259 RemoveDisplay(2);
260 RemoveDisplay(3);
261
262 // Check that display 2 was removed and display 3 is modifed (origin change),
263 // then display 3 was removed.
264 EXPECT_EQ("Removed(2);Modified(3);Removed(3)", delegate()->changes());
265 }
266
267 TEST_F(PlatformScreenOzoneTest, ModifyDisplaySize) {
268 const gfx::Size size1(1920, 1200);
269 const gfx::Size size2(1680, 1050);
270
271 AddDisplay(2, size1);
272
273 // Check that display 2 was added with expected size.
274 ASSERT_THAT(delegate()->added(), SizeIs(1));
275 EXPECT_THAT(delegate()->added()[0], DisplayId(2));
276 EXPECT_THAT(delegate()->added()[0], DisplaySize(size1.ToString()));
277 EXPECT_EQ("Added(2)", delegate()->changes());
278 delegate()->Reset();
279
280 ModifyDisplay(2, size2);
281
282 // Check that display 2 was modified to have the new expected size.
283 ASSERT_THAT(delegate()->modified(), SizeIs(1));
284 EXPECT_THAT(delegate()->modified()[0], DisplayId(2));
285 EXPECT_THAT(delegate()->modified()[0], DisplaySize(size2.ToString()));
286 EXPECT_EQ("Modified(2)", delegate()->changes());
287 }
288
289 TEST_F(PlatformScreenOzoneTest, ModifyFirstDisplaySize) {
290 const gfx::Size size(1920, 1200);
291
292 AddDisplay(2, size);
293
294 // Check that display 2 has the expected initial origin.
295 EXPECT_EQ("Added(2)", delegate()->changes());
296 ASSERT_THAT(delegate()->added(), SizeIs(1));
297 EXPECT_THAT(delegate()->added()[0], DisplayOrigin("1024,0"));
298 delegate()->Reset();
299
300 ModifyDisplay(1, size);
301
302 // Check that display 1 was modified with a new size and display 2 origin was
303 // modified after.
304 EXPECT_EQ("Modified(1);Modified(2)", delegate()->changes());
305 ASSERT_THAT(delegate()->modified(), SizeIs(2));
306 EXPECT_THAT(delegate()->modified()[0], DisplayId(1));
307 EXPECT_THAT(delegate()->modified()[0], DisplaySize(size.ToString()));
308 EXPECT_THAT(delegate()->modified()[1], DisplayId(2));
309 EXPECT_THAT(delegate()->modified()[1], DisplayOrigin("1920,0"));
310 }
311
312 TEST_F(PlatformScreenOzoneTest, RemovePrimaryDisplay) {
313 AddDisplay(2);
314 delegate()->Reset();
315
316 RemoveDisplay(1);
317
318 // Check the primary display changed because the old primary was removed.
319 EXPECT_EQ("Primary(2);Removed(1);Modified(2)", delegate()->changes());
320 }
321
322 TEST_F(PlatformScreenOzoneTest, RemoveLastDisplay) {
323 RemoveDisplay(1);
324
325 // Check that display 1 is removed and no updates for the primary display are
326 // received.
327 EXPECT_EQ("Removed(1)", delegate()->changes());
328 }
329
330 TEST_F(PlatformScreenOzoneTest, SwapPrimaryDisplay) {
331 AddDisplay(2);
332 delegate()->Reset();
333
334 platform_screen()->SwapPrimaryDisplay();
335 EXPECT_EQ("Primary(2)", delegate()->changes());
336 }
337
338 TEST_F(PlatformScreenOzoneTest, SwapPrimaryThreeDisplays) {
339 AddDisplay(2);
340 AddDisplay(3);
341 EXPECT_EQ("Added(2);Added(3)", delegate()->changes());
342 delegate()->Reset();
343
344 platform_screen()->SwapPrimaryDisplay();
345 platform_screen()->SwapPrimaryDisplay();
346 platform_screen()->SwapPrimaryDisplay();
347 EXPECT_EQ("Primary(2);Primary(3);Primary(1)", delegate()->changes());
348 }
349
350 } // namespace display
OLDNEW
« no previous file with comments | « services/ui/display/platform_screen_ozone.cc ('k') | services/ui/display/platform_screen_stub.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698