Macro qtTrIdx() replaced by tr() and QT_TRANSLATE_NOOP()
[mafwsubrenderer] / qmafw-gst-subtitles-renderer / unittests / ut_GstScreenshot / ut_GstScreenshot.cpp
1 /* 
2  * This file is part of QMAFW 
3  *
4  * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights
5  * reserved.
6  *
7  * Contact: Visa Smolander <visa.smolander@nokia.com>
8  *
9  * This software, including documentation, is protected by copyright controlled
10  * by Nokia Corporation. All rights are reserved. Copying, including
11  * reproducing, storing, adapting or translating, any or all of this material
12  * requires the prior written consent of Nokia Corporation. This material also
13  * contains confidential information which may not be disclosed to others
14  * without the prior written consent of Nokia.
15  *
16  */
17  
18 #include <QTimer>
19
20 #include <string.h>
21 #include <glib.h>
22 #include <gst/gst.h>
23
24 #include "MafwGstScreenshot.h"
25 #include "ut_GstScreenshot.h"
26
27 static gchar *global_failing_gst_element = NULL;
28
29 /* START OF STUB DEFINITIONS */
30 GstElement *gst_element_factory_make(const gchar *factoryname, const gchar *name)
31 {
32
33     GstElementFactory *factory;
34
35     qDebug() << __PRETTY_FUNCTION__ << factoryname;
36
37     if (global_failing_gst_element != NULL &&
38         g_str_equal(factoryname, global_failing_gst_element))
39     {
40         qDebug() << "SIMULATED FAIL OF gst_element_factory_make()";
41         return NULL;
42     }
43     else
44     {
45         factory = gst_element_factory_find(factoryname);
46         return gst_element_factory_create(factory, name);
47     }
48
49 }
50 /* END OF STUB DEFINITIONS */
51
52 void ut_GstScreenshot::testFailures()
53 {
54     gint width = 1024;
55     gint height = 1024;
56     gint size = width * height + width * height / 2;
57     GstBuffer *buf = gst_buffer_new_and_alloc(size);
58     QCOMPARE(GST_OBJECT_REFCOUNT_VALUE(buf), 1);
59
60     memset(GST_BUFFER_DATA(buf), 0, size);
61
62     /* no caps set, returns false */
63     QVERIFY(m_screenshot->savePauseFrame(buf,
64                                    "/dev/null") == FALSE);
65
66     buf = gst_buffer_new_and_alloc(size);
67     memset(GST_BUFFER_DATA(buf), 0, size);
68
69     GstCaps *caps;
70     caps = gst_caps_new_simple("video/x-raw-yuv",
71                                "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('I','4','2','0'),
72                                "framerate", GST_TYPE_FRACTION, 25, 1,
73                                "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
74                                "width", G_TYPE_INT, width,
75                                "height", G_TYPE_INT, height,
76                                NULL);
77     gst_buffer_set_caps(buf, caps);
78
79     /* fakesrc creation fails, returns false */
80     global_failing_gst_element = g_strdup("fakesrc");
81     QVERIFY(m_screenshot->savePauseFrame(buf,
82                                    "/dev/null") == FALSE);
83
84     gst_caps_unref(caps);
85
86     /* TODO: Gst pipeline errors are difficult to test... */
87 }
88
89 void ut_GstScreenshot::testConvert()
90 {
91     gint width = 1024;
92     gint height = 512;
93     gint size = width * height + width * height / 2;
94     GstBuffer *buf = gst_buffer_new_and_alloc(size);
95     memset(GST_BUFFER_DATA(buf), 0, size);
96
97     /* set caps, returns true */
98     GstCaps *caps;
99     caps = gst_caps_new_simple("video/x-raw-yuv",
100                                "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('I','4','2','0'),
101                                "framerate", GST_TYPE_FRACTION, 25, 1,
102                                "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
103                                "width", G_TYPE_INT, width,
104                                "height", G_TYPE_INT, height,
105                                NULL);
106     gst_buffer_set_caps(buf, caps);
107     QVERIFY(m_screenshot->savePauseFrame(buf,
108                                    "/dev/null") == TRUE);
109
110     QTest::qWait(500);
111     QVERIFY(m_gotScreenshotSignal == TRUE);
112
113     gst_caps_unref(caps);
114 }
115
116
117 void ut_GstScreenshot::testCancel()
118 {
119     gint width = 1024;
120     gint height = 512;
121     gint size = width * height + width * height / 2;
122     GstBuffer *buf = gst_buffer_new_and_alloc(size);
123     memset(GST_BUFFER_DATA(buf), 0, size);
124
125     QFETCH(uint, timeout);
126
127     /* set caps, returns true */
128     GstCaps *caps;
129     caps = gst_caps_new_simple("video/x-raw-yuv",
130                                "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('I','4','2','0'),
131                                "framerate", GST_TYPE_FRACTION, 25, 1,
132                                "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
133                                "width", G_TYPE_INT, width,
134                                "height", G_TYPE_INT, height,
135                                NULL);
136     gst_buffer_set_caps(buf, caps);
137
138     QVERIFY(m_screenshot->savePauseFrame(buf,
139                                    "/dev/null") == TRUE);
140     QTest::qWait(timeout);
141     if (m_gotScreenshotSignal == false )
142     {
143         m_screenshot->cancelPauseFrame();
144         QVERIFY(m_gotCancelSignal == TRUE);
145     }
146 }
147
148 void ut_GstScreenshot::testCancel_data()
149 {
150     QTest::addColumn<uint>("timeout");
151     QTest::newRow("0 ms") << (uint)0;
152     QTest::newRow("1 ms") << (uint)1;
153     QTest::newRow("5 ms") << (uint)5;
154     QTest::newRow("10 ms") << (uint)10;
155     QTest::newRow("20 ms") << (uint)20;
156     QTest::newRow("35 ms") << (uint)35;
157     QTest::newRow("50 ms") << (uint)50;
158     QTest::newRow("100 ms") << (uint)100;
159 }
160
161 void ut_GstScreenshot::slotScreenshot(char *location, GError *error)
162 {
163     qDebug() << __PRETTY_FUNCTION__;
164     Q_UNUSED(location);
165     Q_UNUSED(error);
166     m_gotScreenshotSignal = TRUE;
167 }
168
169 void ut_GstScreenshot::slotCancelScreenshot()
170 {
171     qDebug() << __PRETTY_FUNCTION__;
172
173     m_gotCancelSignal = TRUE;
174 }
175
176 void ut_GstScreenshot::init()
177 {
178
179     qDebug() << __PRETTY_FUNCTION__;
180
181     gst_init(0, 0);
182
183     m_gotScreenshotSignal = FALSE;
184     m_gotCancelSignal = FALSE;
185
186     m_screenshot = new MafwGstScreenshot(this);
187     connect(m_screenshot, SIGNAL(screenshotTaken(char*,GError*)), this, SLOT(slotScreenshot(char*,GError*)));
188     connect(m_screenshot, SIGNAL(screenshotCancelled()), this, SLOT(slotCancelScreenshot()));
189
190     global_failing_gst_element = NULL;
191
192 }
193
194 void ut_GstScreenshot::cleanup()
195 {
196
197     qDebug() << __PRETTY_FUNCTION__;
198     g_free(global_failing_gst_element);
199     
200     while(QCoreApplication::hasPendingEvents())
201     {
202         QCoreApplication::sendPostedEvents();
203         QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
204         QCoreApplication::processEvents();
205     }
206     delete m_screenshot;
207 }
208
209 QTEST_MAIN(ut_GstScreenshot)