]> git.sesse.net Git - mlt/blob - src/tests/test_properties/test_properties.cpp
Add mlt_animation and mlt_property_interpolate().
[mlt] / src / tests / test_properties / test_properties.cpp
1 /*
2  * Copyright (C) 2013 Dan Dennedy <dan@dennedy.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with consumer library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include <QString>
20 #include <QtTest>
21
22 #include <mlt++/Mlt.h>
23 using namespace Mlt;
24
25 extern "C" {
26 #define __DARWIN__
27 #include <framework/mlt_property.h>
28 #include <framework/mlt_animation.h>
29 }
30
31 class TestProperties: public QObject
32 {
33     Q_OBJECT
34
35 public:
36     TestProperties() {}
37
38 private Q_SLOTS:
39     void InstantiationIsAReference()
40     {
41         Properties p;
42         QCOMPARE(p.ref_count(), 1);
43     }
44
45     void CopyAddsReference()
46     {
47         Properties p;
48         Properties q = p;
49         QCOMPARE(p.ref_count(), 2);
50     }
51
52     void DestructionRemovesReference()
53     {
54         Properties p;
55         Properties* q = new Properties(p);
56         QCOMPARE(p.ref_count(), 2);
57         delete q;
58         QCOMPARE(p.ref_count(), 1);
59     }
60
61     void SetAndGetString()
62     {
63         Properties p;
64         p.set("key", "value");
65         QVERIFY(p.get("key"));
66         QVERIFY(QString(p.get("key")) != QString(""));
67         QCOMPARE(p.get("key"), "value");
68     }
69
70     void SetAndGetInt()
71     {
72         Properties p;
73         int i = 1;
74         p.set("key", i);
75         QCOMPARE(p.get_int("key"), i);
76     }
77
78     void SetAndGetDouble()
79     {
80         Properties p;
81         double d = 1.0;
82         p.set("key", d);
83         QCOMPARE(p.get_double("key"), d);
84     }
85
86     void SetAndGetInt64()
87     {
88         Properties p;
89         int64_t i = 1LL << 32;
90         p.set("key", i);
91         QCOMPARE(p.get_int64("key"), i);
92     }
93
94     void SetAndGetData()
95     {
96         Properties p;
97         const char *value = "value";
98         char* const s = strdup(value);
99         p.set("key", s, strlen(s), free);
100         int size = 0;
101         QCOMPARE((char*) p.get_data("key", size), value);
102         QCOMPARE(size, int(strlen(value)));
103     }
104
105     void IntFromString()
106     {
107         Properties p;
108         const char *s = "-1";
109         int i = -1;
110         p.set("key", i);
111         QCOMPARE(p.get("key"), s);
112         p.set("key", s);
113         QCOMPARE(p.get_int("key"), i);
114     }
115
116     void Int64FromString()
117     {
118         Properties p;
119         const char *s = "-1";
120         int64_t i = -1;
121         p.set("key", i);
122         QCOMPARE(p.get("key"), s);
123         p.set("key", s);
124         QCOMPARE(p.get_int64("key"), i);
125     }
126
127     void DoubleFromString()
128     {
129         Properties p;
130         const char *s = "-1.234567";
131         double d = -1.234567;
132         p.set("key", d);
133         QCOMPARE(p.get("key"), s);
134         p.set("key", s);
135         QCOMPARE(p.get_double("key"), d);
136     }
137
138     void SetNullRemovesProperty()
139     {
140         Properties p;
141         const char *s = NULL;
142         p.set("key", "value");
143         p.set("key", s);
144         QCOMPARE(p.get("key"), s);
145     }
146
147     void SetAndGetHexColor()
148     {
149         Properties p;
150         const char *hexColorString = "0xaabbccdd";
151         int hexColorInt = 0xaabbccdd;
152         p.set("key", hexColorString);
153         QCOMPARE(p.get_int("key"), hexColorInt);
154     }
155
156     void SetAndGetCssColor()
157     {
158         Properties p;
159         const char *cssColorString = "#aabbcc";
160         int cssColorInt = 0xaabbccff;
161         p.set("key", cssColorString);
162         QCOMPARE(p.get_int("key"), cssColorInt);
163
164         const char *cssColorAlphaString = "#00aabbcc";
165         int cssColorAlphaInt = 0xaabbcc00;
166         p.set("key", cssColorAlphaString);
167         QCOMPARE(p.get_int("key"), cssColorAlphaInt);
168     }
169
170     void SetAndGetTimeCode()
171     {
172         Profile profile;
173         Properties p;
174         p.set("_profile", profile.get_profile(), 0);
175         const char *timeString = "11:22:33:04";
176         p.set("key", timeString);
177         QCOMPARE(p.get_int("key"), 1023829);
178         p.set("key", 1023829);
179         QCOMPARE(p.get_time("key", mlt_time_smpte), timeString);
180     }
181
182     void SetAndGetTimeClock()
183     {
184         Profile profile;
185         Properties p;
186         p.set("_profile", profile.get_profile(), 0);
187         const char *timeString = "11:22:33.400";
188         p.set("key", timeString);
189         QCOMPARE(p.get_int("key"), 1023835);
190         p.set("key", 1023835);
191         QCOMPARE(p.get_time("key", mlt_time_clock), timeString);
192     }
193
194     void SetSimpleMathExpression()
195     {
196         Properties p;
197         p.set("key", "@16.0/9.0 *2 +3 -1");
198         QCOMPARE(p.get_int("key"), 5);
199         QCOMPARE(p.get_double("key"), 16.0/9.0 *2 +3 -1);
200     }
201
202     void PassOneProperty()
203     {
204         Properties p[2];
205         const char *s = "value";
206         p[0].set("key", s);
207         QCOMPARE(p[1].get("key"), (void*) 0);
208         p[1].pass_property(p[0], "key");
209         QCOMPARE(p[1].get("key"), s);
210     }
211
212     void PassMultipleByPrefix()
213     {
214         Properties p[2];
215         const char *s = "value";
216         p[0].set("key.one", s);
217         p[0].set("key.two", s);
218         QCOMPARE(p[1].get("key.one"), (void*) 0);
219         QCOMPARE(p[1].get("key.two"), (void*) 0);
220         p[1].pass_values(p[0], "key.");
221         QCOMPARE(p[1].get("one"), s);
222         QCOMPARE(p[1].get("two"), s);
223     }
224
225     void PassMultipleByList()
226     {
227         Properties p[2];
228         const char *s = "value";
229         p[0].set("key.one", s);
230         p[0].set("key.two", s);
231         QCOMPARE(p[1].get("key.one"), (void*) 0);
232         QCOMPARE(p[1].get("key.two"), (void*) 0);
233         p[1].pass_list(p[0], "key.one key.two");
234         QCOMPARE(p[1].get("key.one"), s);
235         QCOMPARE(p[1].get("key.two"), s);
236     }
237
238     void MirrorProperties()
239     {
240         Properties p[2];
241         p[0].mirror(p[1]);
242         p[0].set("key", "value");
243         QCOMPARE(p[1].get("key"), "value");
244     }
245
246     void InheritProperties()
247     {
248         Properties p[2];
249         p[0].set("key", "value");
250         QVERIFY(p[1].get("key") == 0);
251         p[1].inherit(p[0]);
252         QCOMPARE(p[1].get("key"), "value");
253     }
254
255     void ParseString()
256     {
257         Properties p;
258         QCOMPARE(p.get("key"), (void*) 0);
259         p.parse("key=value");
260         QCOMPARE(p.get("key"), "value");
261         p.parse("key=\"new value\"");
262         QCOMPARE(p.get("key"), "new value");
263     }
264
265     void RenameProperty()
266     {
267         Properties p;
268         p.set("key", "value");
269         QVERIFY(p.get("new key") == 0);
270         p.rename("key", "new key");
271         QCOMPARE(p.get("new key"), "value");
272     }
273
274     void SequenceDetected()
275     {
276         Properties p;
277         p.set("1", 1);
278         p.set("2", 2);
279         p.set("3", 3);
280         QVERIFY(p.is_sequence());
281         p.set("four", 4);
282         QVERIFY(!p.is_sequence());
283     }
284
285     void SerializesToYamlTiny()
286     {
287         Properties p[2];
288         p[0].set("key1", "value1");
289         p[0].set("key2", "value2");
290         p[1].set("1", "value3");
291         p[1].set("2", "value4");
292         p[0].set("seq", p[1].get_properties(), 0);
293         QCOMPARE(p[0].serialise_yaml(),
294                 "---\n"
295                 "key1: value1\n"
296                 "key2: value2\n"
297                 "seq:\n"
298                 "  - value3\n"
299                 "  - value4\n"
300                 "...\n");
301     }
302
303     void RadixRespondsToLocale()
304     {
305         Properties p;
306         p.set_lcnumeric("en_US");
307         p.set("key", "0.125");
308         QCOMPARE(p.get_double("key"), double(1) / double(8));
309         p.set_lcnumeric("de_DE");
310         p.set("key", "0,125");
311         QCOMPARE(p.get_double("key"), double(1) / double(8));
312     }
313
314     void DoubleAnimation()
315     {
316         locale_t locale;
317 #if defined(__linux__) || defined(__DARWIN__)
318         locale = newlocale( LC_NUMERIC_MASK, "POSIX", NULL );
319 #endif
320         double fps = 25.0;
321         mlt_animation a = mlt_animation_new();
322         struct mlt_animation_item_s item;
323
324         mlt_animation_parse(a, "50=1; 60=60; 100=0", 100, fps, locale);
325         mlt_animation_remove(a, 60);
326         char *a_serialized = mlt_animation_serialize(a);
327         QCOMPARE(a_serialized, "50=1;100=0");
328         if (a_serialized) free(a_serialized);
329         item.property = mlt_property_init();
330
331         mlt_animation_get_item(a, &item, 10);
332         QCOMPARE(mlt_property_get_double(item.property, fps, locale), 1.0);
333         QCOMPARE(item.is_key, 0);
334
335         mlt_animation_get_item(a, &item, 50);
336         QCOMPARE(mlt_property_get_double(item.property, fps, locale), 1.0);
337         QCOMPARE(item.is_key, 1);
338
339         mlt_animation_get_item(a, &item, 75);
340         QCOMPARE(mlt_property_get_double(item.property, fps, locale), 0.5);
341         QCOMPARE(item.is_key, 0);
342
343         mlt_animation_get_item(a, &item, 100);
344         QCOMPARE(mlt_property_get_double(item.property, fps, locale), 0.0);
345         QCOMPARE(item.is_key, 1);
346
347         mlt_animation_get_item(a, &item, 110);
348         QCOMPARE(mlt_property_get_double(item.property, fps, locale), 0.0);
349         QCOMPARE(item.is_key, 0);
350
351         mlt_property_close(item.property);
352         mlt_animation_close(a);
353     }
354
355     void AnimationWithTimeValueKeyframes()
356     {
357         locale_t locale;
358 #if defined(__linux__) || defined(__DARWIN__)
359         locale = newlocale( LC_NUMERIC_MASK, "POSIX", NULL );
360 #endif
361         double fps = 25.0;
362         mlt_animation a = mlt_animation_new();
363         struct mlt_animation_item_s item;
364
365         mlt_animation_parse(a, ":2.0=1; :4.0=0", 100, fps, locale);
366         char *a_serialized = mlt_animation_serialize(a);
367         // Time serializes to frame units :-\.
368         QCOMPARE(a_serialized, "50=1;100=0");
369         if (a_serialized) free(a_serialized);
370         item.property = mlt_property_init();
371
372         mlt_animation_get_item(a, &item, 10);
373         QCOMPARE(mlt_property_get_double(item.property, fps, locale), 1.0);
374         QCOMPARE(item.is_key, 0);
375
376         mlt_animation_get_item(a, &item, 50);
377         QCOMPARE(mlt_property_get_double(item.property, fps, locale), 1.0);
378         QCOMPARE(item.is_key, 1);
379
380         mlt_animation_get_item(a, &item, 75);
381         QCOMPARE(mlt_property_get_double(item.property, fps, locale), 0.5);
382         QCOMPARE(item.is_key, 0);
383
384         mlt_animation_get_item(a, &item, 100);
385         QCOMPARE(mlt_property_get_double(item.property, fps, locale), 0.0);
386         QCOMPARE(item.is_key, 1);
387
388         mlt_animation_get_item(a, &item, 110);
389         QCOMPARE(mlt_property_get_double(item.property, fps, locale), 0.0);
390         QCOMPARE(item.is_key, 0);
391
392         mlt_property_close(item.property);
393         mlt_animation_close(a);
394     }
395 };
396
397 QTEST_APPLESS_MAIN(TestProperties)
398
399 #include "test_properties.moc"