2 #include "mockclient.h"
3 #include "mockcompositor.h"
4 #include <libds-tizen/global_resource.h>
5 #include <tizen-extension-client-protocol.h>
7 #define TIZEN_SURFACE_VERSION 1
9 class MockGlobalResourceCompositor : public MockCompositor
12 MockGlobalResourceCompositor()
13 : MockCompositor(&MockGlobalResourceCompositor::TestSetup, this)
15 ds_inf("%s : this(%p)", __func__, this);
17 // initialize the flags to check
18 bSurfaceDestroyed = false;
21 bGetResourceInfo = false;
22 bDestroyResourceInfo = false;
26 ~MockGlobalResourceCompositor()
28 ds_inf("%s : this(%p)", __func__, this);
31 static void TestSetup(void *data)
33 MockGlobalResourceCompositor *mockComp =
34 static_cast<MockGlobalResourceCompositor *>(data);
35 Compositor *comp = mockComp->compositor;
37 ds_inf("%s: mockComp(%p)", __func__, mockComp);
39 // new surface listener
40 mockComp->mNewSurfaceListener.notify =
41 MockGlobalResourceCompositor::NewSurfaceCallback;
42 mockComp->mNewSurfaceListener.parent = mockComp;
43 ds_compositor_add_new_surface_listener(comp->compositor,
44 &mockComp->mNewSurfaceListener);
46 mockComp->mGlobalResource =
47 ds_tizen_global_resource_create(comp->display);
50 mockComp->mDestroyListener.notify =
51 MockGlobalResourceCompositor::DestroyCallback;
52 mockComp->mDestroyListener.parent = mockComp;
53 ds_tizen_global_resource_add_destroy_listener(mockComp->mGlobalResource,
54 &mockComp->mDestroyListener);
56 // get_resource_info listener
57 mockComp->mGetResourceInfoListener.notify =
58 MockGlobalResourceCompositor::GetResourceInfoCallback;
59 mockComp->mGetResourceInfoListener.parent = mockComp;
60 ds_tizen_global_resource_add_get_resource_info_listener(
61 mockComp->mGlobalResource,
62 &mockComp->mGetResourceInfoListener);
65 static void NewSurfaceCallback(struct wl_listener *listener, void *data)
67 MockGlobalResourceCompositor *mockComp =
68 reinterpret_cast<NewSurfaceListener *>(listener)->parent;
69 struct ds_surface *surface = static_cast<struct ds_surface *>(data);
71 ds_inf("%s: mockComp(%p), surface(%p)", __func__, mockComp, surface);
73 mockComp->mSurface = surface;
75 // del surface listener
76 mockComp->mDelSurfaceListener.notify =
77 MockGlobalResourceCompositor::DelSurfaceCallback;
78 mockComp->mDelSurfaceListener.parent = mockComp;
79 ds_surface_add_destroy_listener(surface,
80 &mockComp->mDelSurfaceListener);
83 static void DelSurfaceCallback(struct wl_listener *listener, void *data)
85 MockGlobalResourceCompositor *mockComp =
86 reinterpret_cast<DelSurfaceListener *>(listener)->parent;
87 struct ds_surface *surface = static_cast<struct ds_surface *>(data);
89 ds_inf("%s: mockComp(%p), surface(%p)", __func__, mockComp, surface);
91 if (ds_surface_get_wl_resource(mockComp->mSurface) ==
92 ds_surface_get_wl_resource(surface)) {
93 ds_inf("%s: surface is deleted.", __func__);
94 mockComp->bSurfaceDestroyed = true;
98 static void DestroyCallback(struct wl_listener *listener, void *data)
100 ds_inf("%s", __func__);
102 MockGlobalResourceCompositor *mockComp =
103 reinterpret_cast<DestroyListener *>(listener)->parent;
105 mockComp->bDestroyed = true;
108 static void GetResourceInfoCallback(struct wl_listener *listener,
111 ds_inf("%s", __func__);
113 MockGlobalResourceCompositor *mockComp =
114 reinterpret_cast<GetResourceInfoListener *>(listener)->parent;
115 struct ds_tizen_global_resource_info *info =
116 static_cast<struct ds_tizen_global_resource_info *>(data);
118 ds_inf("%s: mockComp(%p), info(%p)", __func__, mockComp, info);
120 mockComp->bGetResourceInfo = true;
122 mockComp->mInfo = info;
124 ds_tizen_global_resource_info_get_surface(mockComp->mInfo);
126 mockComp->mResourceId = ds_tizen_global_resource_get_universal_id(mockComp->mInfo);
128 // info destroy listener
129 mockComp->mGobalResourceInfoDestroyListener.notify =
130 MockGlobalResourceCompositor::GobalResourceInfoDestroyCallback;
131 mockComp->mGobalResourceInfoDestroyListener.parent = mockComp;
132 ds_tizen_global_resource_info_add_destroy_listener(mockComp->mInfo,
133 &mockComp->mGobalResourceInfoDestroyListener);
136 static void GobalResourceInfoDestroyCallback(struct wl_listener *listener,
139 ds_inf("%s", __func__);
141 MockGlobalResourceCompositor *mockComp =
142 reinterpret_cast<GobalResourceInfoDestroyListener *>(listener)->parent;
143 struct ds_tizen_global_resource_info *info =
144 static_cast<struct ds_tizen_global_resource_info *>(data);
146 ds_inf("%s: mockComp(%p), info(%p)", __func__, mockComp, info);
148 if (mockComp->mInfo == info) {
149 ds_inf("%s: info is deleted.", __func__);
150 mockComp->bDestroyResourceInfo = true;
155 bool bSurfaceDestroyed;
157 bool bGetResourceInfo;
158 bool bDestroyResourceInfo;
159 uint32_t mResourceId;
162 struct ds_tizen_global_resource_info *mInfo;
163 struct ds_surface *mSurface;
165 struct NewSurfaceListener : ::wl_listener {
166 MockGlobalResourceCompositor *parent;
168 NewSurfaceListener mNewSurfaceListener;
170 struct DelSurfaceListener : ::wl_listener {
171 MockGlobalResourceCompositor *parent;
173 DelSurfaceListener mDelSurfaceListener;
175 struct ds_tizen_global_resource *mGlobalResource;
177 struct DestroyListener : ::wl_listener {
178 MockGlobalResourceCompositor *parent;
180 DestroyListener mDestroyListener;
182 struct GetResourceInfoListener : ::wl_listener {
183 MockGlobalResourceCompositor *parent;
185 GetResourceInfoListener mGetResourceInfoListener;
187 struct GobalResourceInfoDestroyListener : ::wl_listener {
188 MockGlobalResourceCompositor *parent;
190 GobalResourceInfoDestroyListener mGobalResourceInfoDestroyListener;
193 class MockGlobalResourceClient : public MockClient
196 MockGlobalResourceClient()
198 compositor_res(nullptr),
199 tizen_surface_res(nullptr)
201 MockGlobalResourceClient(const struct wl_registry_listener *listener)
202 : MockClient(listener, this)
204 ds_inf("%s", __func__);
208 ~MockGlobalResourceClient()
210 ds_inf("%s", __func__);
213 void SetWlCompositor(struct wl_compositor *global_res)
215 ds_inf("%s", __func__);
217 compositor_res = global_res;
220 struct wl_compositor *GetWlCompositor()
222 ds_inf("%s", __func__);
224 return compositor_res;
227 void SetTizenSurfaceShm(struct tizen_surface *global_res)
229 ds_inf("%s", __func__);
231 tizen_surface_res = global_res;
234 struct tizen_surface *GetTizenSurfaceShm()
236 ds_inf("%s", __func__);
238 return tizen_surface_res;
242 uint32_t mResourceId;
245 struct wl_compositor *compositor_res;
246 struct tizen_surface *tizen_surface_res;
250 client_registry_cb_global(void *data, struct wl_registry *registry,
251 uint32_t name, const char *interface, uint32_t version)
253 ds_inf("%s", __func__);
255 MockGlobalResourceClient *client = static_cast<MockGlobalResourceClient *>(data);
256 struct wl_compositor *compositor_res;
257 struct tizen_surface *tizen_surface_res;
259 if (!strcmp(interface, "wl_compositor")) {
260 compositor_res = (struct wl_compositor *)wl_registry_bind(registry,
261 name, &wl_compositor_interface, 1);
262 if (compositor_res == nullptr) {
263 ds_err("wl_registry_bind() failed. wl_compositor resource.");
266 client->SetWlCompositor(compositor_res);
267 } else if (!strcmp(interface, "tizen_surface")) {
268 tizen_surface_res = (struct tizen_surface *)wl_registry_bind(registry,
269 name, &tizen_surface_interface, TIZEN_SURFACE_VERSION);
270 if (tizen_surface_res == nullptr) {
271 ds_err("wl_registry_bind() failed. tizen_surface resource.");
274 client->SetTizenSurfaceShm(tizen_surface_res);
279 client_registry_cb_global_remove(void *data, struct wl_registry *registry,
282 ds_inf("%s", __func__);
284 MockGlobalResourceClient *client = static_cast<MockGlobalResourceClient *>(data);
285 struct wl_compositor *compositor_res = client->GetWlCompositor();
286 struct tizen_surface *tizen_surface_res = client->GetTizenSurfaceShm();
288 tizen_surface_destroy(tizen_surface_res);
289 wl_compositor_destroy(compositor_res);
292 static const struct wl_registry_listener registry_listener = {
293 .global = client_registry_cb_global,
294 .global_remove = client_registry_cb_global_remove
298 client_tizen_resource_cb_resoruce_id(void *data,
299 struct tizen_resource *resource_res, uint32_t id)
301 ds_inf("%s", __func__);
303 MockGlobalResourceClient *client =
304 static_cast<MockGlobalResourceClient *>(data);
306 client->mResourceId = id;
310 tizen_resource_listener resource_cb_listener = {
311 .resource_id = client_tizen_resource_cb_resoruce_id,
314 class GlobalResourceTest : public ::testing::Test
317 void SetUp(void) override;
318 void TearDown(void) override;
320 MockGlobalResourceCompositor *comp;
321 MockGlobalResourceClient *client;
322 struct wl_compositor *compositor_res;
323 struct tizen_surface *tizen_surface_res;
324 struct wl_surface *surface_res;
325 struct tizen_resource *resource_res;
329 GlobalResourceTest::SetUp(void)
331 //ds_log_init(DS_DBG, NULL);
333 ds_inf("%s", __func__);
335 comp = new MockGlobalResourceCompositor();
336 client = new MockGlobalResourceClient(®istry_listener);
337 compositor_res = client->GetWlCompositor();
338 tizen_surface_res = client->GetTizenSurfaceShm();
339 surface_res = wl_compositor_create_surface(compositor_res);
342 tizen_surface_get_tizen_resource(tizen_surface_res, surface_res);
344 tizen_resource_add_listener(resource_res, &resource_cb_listener, client);
350 GlobalResourceTest::TearDown(void)
352 ds_inf("%s", __func__);
354 tizen_resource_destroy(resource_res);
356 EXPECT_TRUE(comp->bDestroyResourceInfo);
358 wl_surface_destroy(surface_res);
360 EXPECT_TRUE(comp->bSurfaceDestroyed);
366 TEST_F(GlobalResourceTest, Create_P)
369 EXPECT_TRUE(comp->bGetResourceInfo);
370 EXPECT_TRUE(client->mResourceId == 0);