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

Side by Side Diff: ui/ozone/platform/wayland/fake_server.cc

Issue 1673063002: ozone/platform/wayland: Add some tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use two WaitableEvents to prevent race, re-arrange some definitions, other minor test robustness fi… Created 4 years, 10 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 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 "ui/ozone/platform/wayland/fake_server.h"
6
7 #include <sys/socket.h>
8 #include <wayland-server.h>
9 #include <xdg-shell-unstable-v5-server-protocol.h>
10
11 #include "base/bind.h"
12 #include "base/files/scoped_file.h"
13 #include "base/strings/string_number_conversions.h"
14
15 namespace wl {
16 namespace {
17
18 const uint32_t kCompositorVersion = 4;
19 const uint32_t kSurfaceVersion = 4;
20 const uint32_t kXdgShellVersion = 1;
21 const uint32_t kXdgSurfaceVersion = 1;
22
23 void DestroyResource(wl_client* client, wl_resource* resource) {
24 wl_resource_destroy(resource);
25 }
26
27 // wl_compositor
28
29 void CreateSurface(wl_client* client, wl_resource* resource, uint32_t id) {
30 auto compositor =
31 static_cast<MockCompositor*>(wl_resource_get_user_data(resource));
32 wl_resource* surface_resource =
33 wl_resource_create(client, &wl_surface_interface, kSurfaceVersion, id);
34 if (!surface_resource) {
35 wl_client_post_no_memory(client);
36 return;
37 }
38 compositor->AddSurface(make_scoped_ptr(new MockSurface(surface_resource)));
39 }
40
41 const struct wl_compositor_interface compositor_impl = {
42 &CreateSurface, // create_surface
43 nullptr, // create_region
44 };
45
46 // wl_surface
47
48 void Attach(wl_client* client,
49 wl_resource* resource,
50 wl_resource* buffer_resource,
51 int32_t x,
52 int32_t y) {
53 static_cast<MockSurface*>(wl_resource_get_user_data(resource))
54 ->Attach(buffer_resource, x, y);
55 }
56
57 void Damage(wl_client* client,
58 wl_resource* resource,
59 int32_t x,
60 int32_t y,
61 int32_t width,
62 int32_t height) {
63 static_cast<MockSurface*>(wl_resource_get_user_data(resource))
64 ->Damage(x, y, width, height);
65 }
66
67 void Commit(wl_client* client, wl_resource* resource) {
68 static_cast<MockSurface*>(wl_resource_get_user_data(resource))->Commit();
69 }
70
71 const struct wl_surface_interface surface_impl = {
72 &DestroyResource, // destroy
73 &Attach, // attach
74 &Damage, // damage
75 nullptr, // frame
76 nullptr, // set_opaque_region
77 nullptr, // set_input_region
78 &Commit, // commit
79 nullptr, // set_buffer_transform
80 nullptr, // set_buffer_scale
81 };
82
83 // xdg_shell
84
85 void UseUnstableVersion(wl_client* client,
86 wl_resource* resource,
87 int32_t version) {
88 static_cast<MockXdgShell*>(wl_resource_get_user_data(resource))
89 ->UseUnstableVersion(version);
90 }
91
92 void GetXdgSurface(wl_client* client,
93 wl_resource* resource,
94 uint32_t id,
95 wl_resource* surface_resource) {
96 auto surface =
97 static_cast<MockSurface*>(wl_resource_get_user_data(surface_resource));
98 if (surface->xdg_surface) {
99 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
100 "surface already has a role");
101 return;
102 }
103 wl_resource* xdg_surface_resource = wl_resource_create(
104 client, &xdg_surface_interface, kXdgSurfaceVersion, id);
105 if (!xdg_surface_resource) {
106 wl_client_post_no_memory(client);
107 return;
108 }
109 surface->xdg_surface.reset(new MockXdgSurface(xdg_surface_resource));
110 }
111
112 void Pong(wl_client* client, wl_resource* resource, uint32_t serial) {
113 static_cast<MockXdgShell*>(wl_resource_get_user_data(resource))->Pong(serial);
114 }
115
116 const struct xdg_shell_interface xdg_shell_impl = {
117 &DestroyResource, // destroy
118 &UseUnstableVersion, // use_unstable_version
119 &GetXdgSurface, // get_xdg_surface
120 nullptr, // get_xdg_popup
121 &Pong, // pong
122 };
123
124 // xdg_surface
125
126 void SetTitle(wl_client* client, wl_resource* resource, const char* title) {
127 static_cast<MockXdgSurface*>(wl_resource_get_user_data(resource))
128 ->SetTitle(title);
129 }
130
131 void SetAppId(wl_client* client, wl_resource* resource, const char* app_id) {
132 static_cast<MockXdgSurface*>(wl_resource_get_user_data(resource))
133 ->SetAppId(app_id);
134 }
135
136 void AckConfigure(wl_client* client, wl_resource* resource, uint32_t serial) {
137 static_cast<MockXdgSurface*>(wl_resource_get_user_data(resource))
138 ->AckConfigure(serial);
139 }
140
141 void SetMaximized(wl_client* client, wl_resource* resource) {
142 static_cast<MockXdgSurface*>(wl_resource_get_user_data(resource))
143 ->SetMaximized();
144 }
145
146 void UnsetMaximized(wl_client* client, wl_resource* resource) {
147 static_cast<MockXdgSurface*>(wl_resource_get_user_data(resource))
148 ->UnsetMaximized();
149 }
150
151 void SetMinimized(wl_client* client, wl_resource* resource) {
152 static_cast<MockXdgSurface*>(wl_resource_get_user_data(resource))
153 ->SetMinimized();
154 }
155
156 const struct xdg_surface_interface xdg_surface_impl = {
157 &DestroyResource, // destroy
158 nullptr, // set_parent
159 &SetTitle, // set_title
160 &SetAppId, // set_app_id
161 nullptr, // show_window_menu
162 nullptr, // move
163 nullptr, // resize
164 &AckConfigure, // ack_configure
165 nullptr, // set_window_geometry
166 &SetMaximized, // set_maximized
167 &UnsetMaximized, // set_unmaximized
168 nullptr, // set_fullscreen
169 nullptr, // unset_fullscreen
170 &SetMinimized, // set_minimized
171 };
172
173 } // namespace
174
175 ServerObject::ServerObject(wl_resource* resource) : resource_(resource) {}
176
177 ServerObject::~ServerObject() {
178 if (resource_)
179 wl_resource_destroy(resource_);
180 }
181
182 // static
183 void ServerObject::OnResourceDestroyed(wl_resource* resource) {
184 auto obj = static_cast<ServerObject*>(wl_resource_get_user_data(resource));
185 obj->resource_ = nullptr;
186 }
187
188 MockXdgSurface::MockXdgSurface(wl_resource* resource) : ServerObject(resource) {
189 wl_resource_set_implementation(resource, &xdg_surface_impl, this,
190 &ServerObject::OnResourceDestroyed);
191 }
192
193 MockXdgSurface::~MockXdgSurface() {}
194
195 MockSurface::MockSurface(wl_resource* resource) : ServerObject(resource) {
196 wl_resource_set_implementation(resource, &surface_impl, this,
197 &ServerObject::OnResourceDestroyed);
198 }
199
200 MockSurface::~MockSurface() {
201 if (xdg_surface && xdg_surface->resource())
202 wl_resource_destroy(xdg_surface->resource());
203 }
204
205 MockSurface* MockSurface::FromResource(wl_resource* resource) {
206 if (!wl_resource_instance_of(resource, &wl_surface_interface, &surface_impl))
207 return nullptr;
208 return static_cast<MockSurface*>(wl_resource_get_user_data(resource));
209 }
210
211 void GlobalDeleter::operator()(wl_global* global) {
212 wl_global_destroy(global);
213 }
214
215 Global::Global(const wl_interface* interface,
216 const void* implementation,
217 uint32_t version)
218 : interface_(interface),
219 implementation_(implementation),
220 version_(version) {}
221
222 Global::~Global() {}
223
224 bool Global::Initialize(wl_display* display) {
225 global_.reset(wl_global_create(display, interface_, version_, this, &Bind));
226 return global_;
227 }
228
229 // static
230 void Global::Bind(wl_client* client,
231 void* data,
232 uint32_t version,
233 uint32_t id) {
234 auto global = static_cast<Global*>(data);
235 wl_resource* resource =
236 wl_resource_create(client, global->interface_, global->version_, id);
237 if (!resource) {
238 wl_client_post_no_memory(client);
239 return;
240 }
241 if (!global->resource_)
242 global->resource_ = resource;
243 wl_resource_set_implementation(resource, global->implementation_, global,
244 &Global::OnResourceDestroyed);
245 }
246
247 // static
248 void Global::OnResourceDestroyed(wl_resource* resource) {
249 auto global = static_cast<Global*>(wl_resource_get_user_data(resource));
250 if (global->resource_ == resource)
251 global->resource_ = nullptr;
252 }
253
254 MockCompositor::MockCompositor()
255 : Global(&wl_compositor_interface, &compositor_impl, kCompositorVersion) {}
256
257 MockCompositor::~MockCompositor() {}
258
259 void MockCompositor::AddSurface(scoped_ptr<MockSurface> surface) {
260 surfaces_.push_back(std::move(surface));
261 }
262
263 MockXdgShell::MockXdgShell()
264 : Global(&xdg_shell_interface, &xdg_shell_impl, kXdgShellVersion) {}
265
266 MockXdgShell::~MockXdgShell() {}
267
268 void DisplayDeleter::operator()(wl_display* display) {
269 wl_display_destroy(display);
270 }
271
272 FakeServer::FakeServer()
273 : Thread("fake_wayland_server"),
274 pause_event_(false, false),
275 resume_event_(false, false) {}
276
277 FakeServer::~FakeServer() {
278 Stop();
279 }
280
281 bool FakeServer::Start() {
282 display_.reset(wl_display_create());
283 if (!display_)
284 return false;
285 event_loop_ = wl_display_get_event_loop(display_.get());
286
287 int fd[2];
288 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, fd) < 0)
289 return false;
290 base::ScopedFD server_fd(fd[0]);
291 base::ScopedFD client_fd(fd[1]);
292
293 if (wl_display_init_shm(display_.get()) < 0)
294 return false;
295 if (!compositor_.Initialize(display_.get()))
296 return false;
297 if (!xdg_shell_.Initialize(display_.get()))
298 return false;
299
300 client_ = wl_client_create(display_.get(), server_fd.get());
301 if (!client_)
302 return false;
303 (void)server_fd.release();
304
305 base::Thread::Options options;
306 options.message_pump_factory =
307 base::Bind(&FakeServer::CreateMessagePump, base::Unretained(this));
308 if (!base::Thread::StartWithOptions(options))
309 return false;
310
311 setenv("WAYLAND_SOCKET", base::UintToString(client_fd.release()).c_str(), 1);
312
313 return true;
314 }
315
316 void FakeServer::Flush() {
317 wl_display_flush_clients(display_.get());
318 }
319
320 void FakeServer::Pause() {
321 task_runner()->PostTask(
322 FROM_HERE, base::Bind(&FakeServer::DoPause, base::Unretained(this)));
323 pause_event_.Wait();
324 }
325
326 void FakeServer::Resume() {
327 resume_event_.Signal();
328 }
329
330 void FakeServer::DoPause() {
331 pause_event_.Signal();
332 resume_event_.Wait();
333 }
334
335 scoped_ptr<base::MessagePump> FakeServer::CreateMessagePump() {
336 auto pump = make_scoped_ptr(new base::MessagePumpLibevent);
337 pump->WatchFileDescriptor(wl_event_loop_get_fd(event_loop_), true,
338 base::MessagePumpLibevent::WATCH_READ, &controller_,
339 this);
340 return std::move(pump);
341 }
342
343 void FakeServer::OnFileCanReadWithoutBlocking(int fd) {
344 wl_event_loop_dispatch(event_loop_, 0);
345 wl_display_flush_clients(display_.get());
346 }
347
348 void FakeServer::OnFileCanWriteWithoutBlocking(int fd) {}
349
350 } // namespace wl
OLDNEW
« no previous file with comments | « ui/ozone/platform/wayland/fake_server.h ('k') | ui/ozone/platform/wayland/mock_platform_window_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698