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 #ifndef UI_OZONE_PLATFORM_WAYLAND_FAKE_SERVER_H_ |
| 6 #define UI_OZONE_PLATFORM_WAYLAND_FAKE_SERVER_H_ |
| 7 |
| 8 #include <wayland-server-core.h> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/message_loop/message_pump_libevent.h" |
| 12 #include "base/synchronization/waitable_event.h" |
| 13 #include "base/threading/thread.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 |
| 16 struct wl_client; |
| 17 struct wl_display; |
| 18 struct wl_event_loop; |
| 19 struct wl_global; |
| 20 struct wl_resource; |
| 21 |
| 22 namespace wl { |
| 23 |
| 24 class ServerObject { |
| 25 public: |
| 26 ServerObject(wl_resource* resource); |
| 27 virtual ~ServerObject(); |
| 28 |
| 29 wl_resource* resource() { return resource_; } |
| 30 |
| 31 static void OnResourceDestroyed(wl_resource* resource); |
| 32 |
| 33 private: |
| 34 wl_resource* resource_; |
| 35 |
| 36 DISALLOW_COPY_AND_ASSIGN(ServerObject); |
| 37 }; |
| 38 |
| 39 class MockXdgSurface : public ServerObject { |
| 40 public: |
| 41 MockXdgSurface(wl_resource* resource); |
| 42 ~MockXdgSurface() override; |
| 43 |
| 44 MOCK_METHOD1(SetParent, void(wl_resource* parent)); |
| 45 MOCK_METHOD1(SetTitle, void(const char* title)); |
| 46 MOCK_METHOD1(SetAppId, void(const char* app_id)); |
| 47 MOCK_METHOD1(AckConfigure, void(uint32_t serial)); |
| 48 MOCK_METHOD0(SetMaximized, void()); |
| 49 MOCK_METHOD0(UnsetMaximized, void()); |
| 50 MOCK_METHOD0(SetMinimized, void()); |
| 51 |
| 52 private: |
| 53 DISALLOW_COPY_AND_ASSIGN(MockXdgSurface); |
| 54 }; |
| 55 |
| 56 class MockSurface : public ServerObject { |
| 57 public: |
| 58 MockSurface(wl_resource* resource); |
| 59 ~MockSurface() override; |
| 60 |
| 61 static MockSurface* FromResource(wl_resource* resource); |
| 62 |
| 63 MOCK_METHOD3(Attach, void(wl_resource* buffer, int32_t x, int32_t y)); |
| 64 MOCK_METHOD4(Damage, |
| 65 void(int32_t x, int32_t y, int32_t width, int32_t height)); |
| 66 MOCK_METHOD0(Commit, void()); |
| 67 |
| 68 scoped_ptr<MockXdgSurface> xdg_surface; |
| 69 |
| 70 private: |
| 71 DISALLOW_COPY_AND_ASSIGN(MockSurface); |
| 72 }; |
| 73 |
| 74 struct GlobalDeleter { |
| 75 void operator()(wl_global* global); |
| 76 }; |
| 77 |
| 78 class Global { |
| 79 public: |
| 80 Global(const wl_interface* interface, |
| 81 const void* implementation, |
| 82 uint32_t version); |
| 83 virtual ~Global(); |
| 84 |
| 85 bool Initialize(wl_display* display); |
| 86 |
| 87 // The first bound resource to this global, which is usually all that is |
| 88 // useful when testing a simple client. |
| 89 wl_resource* resource() { return resource_; } |
| 90 |
| 91 static void Bind(wl_client* client, |
| 92 void* data, |
| 93 uint32_t version, |
| 94 uint32_t id); |
| 95 static void OnResourceDestroyed(wl_resource* resource); |
| 96 |
| 97 private: |
| 98 scoped_ptr<wl_global, GlobalDeleter> global_; |
| 99 |
| 100 const wl_interface* interface_; |
| 101 const void* implementation_; |
| 102 const uint32_t version_; |
| 103 wl_resource* resource_ = nullptr; |
| 104 |
| 105 DISALLOW_COPY_AND_ASSIGN(Global); |
| 106 }; |
| 107 |
| 108 class MockCompositor : public Global { |
| 109 public: |
| 110 MockCompositor(); |
| 111 ~MockCompositor() override; |
| 112 |
| 113 void AddSurface(scoped_ptr<MockSurface> surface); |
| 114 |
| 115 private: |
| 116 std::vector<scoped_ptr<MockSurface>> surfaces_; |
| 117 |
| 118 DISALLOW_COPY_AND_ASSIGN(MockCompositor); |
| 119 }; |
| 120 |
| 121 class MockXdgShell : public Global { |
| 122 public: |
| 123 MockXdgShell(); |
| 124 ~MockXdgShell() override; |
| 125 |
| 126 MOCK_METHOD1(UseUnstableVersion, void(int32_t version)); |
| 127 MOCK_METHOD1(Pong, void(uint32_t serial)); |
| 128 |
| 129 private: |
| 130 DISALLOW_COPY_AND_ASSIGN(MockXdgShell); |
| 131 }; |
| 132 |
| 133 struct DisplayDeleter { |
| 134 void operator()(wl_display* display); |
| 135 }; |
| 136 |
| 137 class FakeServer : public base::Thread, base::MessagePumpLibevent::Watcher { |
| 138 public: |
| 139 FakeServer(); |
| 140 ~FakeServer() override; |
| 141 |
| 142 // Start the fake Wayland server. If this succeeds, the WAYLAND_SOCKET |
| 143 // environment variable will be set to the string representation of a file |
| 144 // descriptor that a client can connect to. The caller is responsible for |
| 145 // ensuring that this file descriptor gets closed (for example, by calling |
| 146 // wl_display_connect). |
| 147 bool Start(); |
| 148 |
| 149 void Flush(); |
| 150 |
| 151 void Pause(); |
| 152 void Resume(); |
| 153 |
| 154 template <typename T> |
| 155 T* GetObject(uint32_t id) { |
| 156 wl_resource* resource = wl_client_get_object(client_, id); |
| 157 return resource ? T::FromResource(resource) : nullptr; |
| 158 } |
| 159 |
| 160 MockXdgShell* xdg_shell() { return &xdg_shell_; } |
| 161 |
| 162 private: |
| 163 void DoPause(); |
| 164 |
| 165 scoped_ptr<base::MessagePump> CreateMessagePump(); |
| 166 |
| 167 // base::MessagePumpLibevent::Watcher |
| 168 void OnFileCanReadWithoutBlocking(int fd) override; |
| 169 void OnFileCanWriteWithoutBlocking(int fd) override; |
| 170 |
| 171 scoped_ptr<wl_display, DisplayDeleter> display_; |
| 172 wl_client* client_ = nullptr; |
| 173 wl_event_loop* event_loop_ = nullptr; |
| 174 |
| 175 base::WaitableEvent pause_event_; |
| 176 base::WaitableEvent resume_event_; |
| 177 bool paused_ = false; |
| 178 |
| 179 MockCompositor compositor_; |
| 180 MockXdgShell xdg_shell_; |
| 181 |
| 182 base::MessagePumpLibevent::FileDescriptorWatcher controller_; |
| 183 |
| 184 DISALLOW_COPY_AND_ASSIGN(FakeServer); |
| 185 }; |
| 186 |
| 187 } // namespace wl |
| 188 |
| 189 #endif // UI_OZONE_PLATFORM_WAYLAND_FAKE_SERVER_H_ |
OLD | NEW |