change wl_signal_emit_mutable into wl_signal_emit
[platform/core/uifw/libds-tizen.git] / tests / tc_video.cpp
1 #include "tc_main.h"
2 #include "mockclient.h"
3 #include "mockcompositor.h"
4
5 #include <vector>
6 #include <cstring>
7 #include <drm_fourcc.h>
8 #include <libds/log.h>
9 #include <libds-tizen/video.h>
10 #include <tizen-extension-client-protocol.h>
11
12 TEST(ScalerSimpleTest, CreateScaler)
13 {
14     struct wl_display *display = wl_display_create();
15
16     struct ds_tizen_video *video = ds_tizen_video_create(display);
17     ASSERT_NE(video, nullptr);
18
19     wl_display_destroy(display);
20 }
21
22 class VideoCompositor : public MockCompositor, public ::testing::Test
23 {
24 public:
25     VideoCompositor() :
26         MockCompositor(&VideoCompositor::SetUpComp, this)
27     {
28         /* We are only interested in test results. Let's silence libds log. */
29         ds_log_init(DS_SILENT, nullptr);
30
31         this->video = nullptr;
32     }
33
34     static void SetUpComp(void *data)
35     {
36         VideoCompositor *comp = static_cast<VideoCompositor*>(data);
37
38         comp->video = ds_tizen_video_create(comp->GetWlDisplay());
39         ASSERT_NE(comp->video, nullptr);
40     }
41
42     struct ds_tizen_video *video;
43 };
44
45 static void handle_global(void *data, struct wl_registry *registry,
46     uint32_t name, const char *interface, uint32_t version);
47 static void handle_global_remove(void *data, struct wl_registry *registry,
48         uint32_t name);
49
50 static const struct wl_registry_listener registry_listener = {
51     .global = handle_global,
52     .global_remove = handle_global_remove,
53 };
54
55 class MockVideoClient : public MockClient
56 {
57 public:
58     MockVideoClient() : MockClient(&registry_listener, this)
59     {
60         EXPECT_NE(this->compositor, nullptr);
61         EXPECT_NE(this->video, nullptr);
62
63         surface = wl_compositor_create_surface(this->compositor);
64         EXPECT_NE(this->surface, nullptr);
65     }
66
67     ~MockVideoClient() 
68     {
69         wl_surface_destroy(this->surface);
70         tizen_video_destroy(this->video);
71         wl_compositor_destroy(this->compositor);
72     }
73
74     struct wl_compositor *compositor;
75     struct tizen_video *video;
76     struct wl_surface *surface;
77 };
78
79 static void
80 handle_global(void *data, struct wl_registry *registry,
81     uint32_t name, const char *interface, uint32_t version)
82 {
83     MockVideoClient *client = static_cast<MockVideoClient*>(data);
84
85     if (strcmp(interface, "wl_compositor") == 0) {
86         client->compositor = static_cast<struct wl_compositor*>(
87                 wl_registry_bind(registry, name, &wl_compositor_interface, 4));
88     } else if (strcmp(interface, "tizen_video") == 0) {
89         client->video = static_cast<struct tizen_video*>(
90                 wl_registry_bind(registry, name, &tizen_video_interface, 1));
91     }
92 }
93
94 static void
95 handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
96 {
97 }
98
99 static void
100 video_handle_format(void *data, struct tizen_video *video, uint32_t format)
101 {
102     std::vector<uint32_t> *ret_formats =
103         static_cast<std::vector<uint32_t>*>(data);
104
105     ret_formats->push_back(format);
106 }
107
108 static const struct
109 tizen_video_listener video_listener = {
110     .format = video_handle_format,
111 };
112
113 TEST_F(VideoCompositor, AddFormats)
114 {
115     std::vector<uint32_t> supported_format {
116         DRM_FORMAT_XRGB8888, DRM_FORMAT_ARGB8888,
117     };
118
119     for (uint32_t format : supported_format)
120         ds_tizen_video_add_format(this->video, format);
121
122     std::vector<uint32_t> ret_formats;
123     MockVideoClient *client = new MockVideoClient();
124     tizen_video_add_listener(client->video, &video_listener,
125             static_cast<void*>(&ret_formats));
126     client->RoundTrip();
127
128     ASSERT_EQ(supported_format.size(), ret_formats.size());
129
130     bool found;
131     for (uint32_t format : supported_format) {
132         found = false;
133         for (uint32_t ret_format : ret_formats) {
134             if (format == ret_format) {
135                 found = true;
136                 break;
137             }
138         }
139         ASSERT_EQ(found, true);
140     }
141
142     delete client;
143 }
144
145 TEST_F(VideoCompositor, CreateObject)
146 {
147     MockVideoClient *client = new MockVideoClient();
148
149     struct tizen_video_object *object = tizen_video_get_object(client->video,
150             client->surface);
151     client->ExpectNoError();
152
153     tizen_video_object_destroy(object);
154     delete client;
155 }
156
157 struct size_hint {
158     struct { int32_t width, height; } min;
159     struct { int32_t width, height; } max;
160     int32_t prefer_align;
161 };
162
163 static void
164 video_object_handle_size(void *data, struct tizen_video_object *object,
165         int32_t min_w, int32_t min_h, int32_t max_w, int32_t max_h,
166         int32_t prefer_align)
167 {
168     size_hint *ret_size = static_cast<size_hint*>(data);
169     ret_size->min.width = min_w;
170     ret_size->min.height = min_h;
171     ret_size->max.width = max_w;
172     ret_size->max.height = max_h;
173     ret_size->prefer_align = prefer_align;
174 }
175
176 static const struct
177 tizen_video_object_listener object_size_hint_listener = {
178     .attribute = NULL,
179     .size = video_object_handle_size,
180 };
181
182 TEST_F(VideoCompositor, SetSizeHint)
183 {
184     size_hint hint = { 100, 50, 1000, 500, 777 };
185
186     ds_tizen_video_set_size_hint(this->video,
187             hint.min.width, hint.min.height,
188             hint.max.width, hint.max.height,
189             hint.prefer_align);
190
191     MockVideoClient *client = new MockVideoClient();
192
193     struct tizen_video_object *object =
194         tizen_video_get_object(client->video, client->surface);
195
196     size_hint ret_hint;
197     tizen_video_object_add_listener(object, &object_size_hint_listener,
198             static_cast<void*>(&ret_hint));
199     client->RoundTrip();
200
201     ASSERT_EQ(hint.min.width, ret_hint.min.width);
202     ASSERT_EQ(hint.min.height, ret_hint.min.height);
203     ASSERT_EQ(hint.max.width, ret_hint.max.width);
204     ASSERT_EQ(hint.max.height, ret_hint.max.height);
205     ASSERT_EQ(hint.prefer_align, ret_hint.prefer_align);
206
207     tizen_video_object_destroy(object);
208     delete client;
209 }
210
211 struct property {
212     std::string name;
213     uint32_t value;
214
215     property(std::string _name, uint32_t _value) : name(_name), value(_value) {}
216 };
217
218 static void
219 video_object_handle_attribute(void *data, struct tizen_video_object *object,
220         const char *name, uint32_t value)
221 {
222     std::vector<property*> *ret_properties =
223         static_cast<std::vector<property*>*>(data);
224
225     ret_properties->push_back(new property(std::string(name), value));
226 }
227
228 static void
229 video_object_dummy_handle_size(void *data, struct tizen_video_object *object,
230         int32_t min_w, int32_t min_h, int32_t max_w, int32_t max_h,
231         int32_t prefer_align)
232 {
233     // This left blank intentionally.
234 }
235
236 static const struct
237 tizen_video_object_listener object_attribute_listener = {
238     .attribute = video_object_handle_attribute,
239     .size = video_object_dummy_handle_size,
240 };
241
242 TEST_F(VideoCompositor, AddProperties)
243 {
244     std::vector<property> properties {
245         { "property0", 123 },
246         { "property1", 987 },
247         { "property2", 345 },
248         { "property3", 777 },
249     };
250
251     for (struct property prop : properties)
252         ds_tizen_video_add_property(this->video, prop.name.c_str(), prop.value);
253
254     MockVideoClient *client = new MockVideoClient();
255
256     struct tizen_video_object *object =
257         tizen_video_get_object(client->video, client->surface);
258
259     std::vector<property*> ret_properties;
260     tizen_video_object_add_listener(object, &object_attribute_listener,
261             static_cast<void*>(&ret_properties));
262     client->RoundTrip();
263
264     ASSERT_EQ(properties.size(), ret_properties.size());
265
266     bool found;
267     for (property prop : properties) {
268         found = false;
269         for (property *ret_prop : ret_properties) {
270             if (prop.name == ret_prop->name &&
271                     prop.value == ret_prop->value) {
272                 found = true;
273                 break;
274             }
275         }
276         ASSERT_EQ(found, true);
277     }
278
279     for (property *prop : ret_properties)
280         delete prop;
281     ret_properties.clear();
282     tizen_video_object_destroy(object);
283     delete client;
284 }