Revert "[Tizen] Appendix log for ttrace + Print keycode and timestamp"
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-PixelData.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <dali-test-suite-utils.h>
19 #include <dali/public-api/common/dali-vector.h>
20 #include <dali/public-api/images/pixel-data.h>
21 #include <dali/public-api/images/pixel.h>
22
23 #include <dali/integration-api/pixel-data-integ.h>
24
25 #include <cstdlib>
26
27 using namespace Dali;
28
29 int UtcDaliPixelData01(void)
30 {
31   TestApplication application;
32
33   unsigned int width      = 10u;
34   unsigned int height     = 10u;
35   unsigned int bufferSize = width * height * Pixel::GetBytesPerPixel(Pixel::RGB888);
36
37   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
38   PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGB888, PixelData::FREE);
39
40   DALI_TEST_CHECK(pixelData);
41   DALI_TEST_CHECK(pixelData.GetWidth() == width);
42   DALI_TEST_CHECK(pixelData.GetHeight() == height);
43   DALI_TEST_CHECK(pixelData.GetPixelFormat() == Pixel::RGB888);
44
45   END_TEST;
46 }
47
48 int UtcDaliPixelData02(void)
49 {
50   TestApplication application;
51
52   unsigned int   width      = 10u;
53   unsigned int   height     = 10u;
54   unsigned int   bufferSize = width * height * Pixel::GetBytesPerPixel(Pixel::L8);
55   unsigned char* buffer     = new unsigned char[bufferSize];
56   buffer[0]                 = 'a';
57
58   PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::L8, PixelData::DELETE_ARRAY);
59
60   DALI_TEST_CHECK(pixelData);
61   DALI_TEST_CHECK(pixelData.GetWidth() == width);
62   DALI_TEST_CHECK(pixelData.GetHeight() == height);
63   DALI_TEST_CHECK(pixelData.GetStride() == 0);
64   DALI_TEST_CHECK(pixelData.GetPixelFormat() == Pixel::L8);
65
66   END_TEST;
67 }
68
69 int UtcDaliPixelData03(void)
70 {
71   TestApplication application;
72
73   uint32_t width      = 10u;
74   uint32_t height     = 10u;
75   uint32_t stride     = 12u;
76   uint32_t bufferSize = stride * height * Pixel::GetBytesPerPixel(Pixel::RGB888);
77
78   uint8_t*  buffer    = reinterpret_cast<uint8_t*>(malloc(bufferSize));
79   PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, stride, Pixel::RGB888, PixelData::FREE);
80
81   DALI_TEST_CHECK(pixelData);
82   DALI_TEST_CHECK(pixelData.GetWidth() == width);
83   DALI_TEST_CHECK(pixelData.GetHeight() == height);
84   DALI_TEST_CHECK(pixelData.GetStride() == stride);
85   DALI_TEST_CHECK(pixelData.GetPixelFormat() == Pixel::RGB888);
86
87   END_TEST;
88 }
89
90 int UtcDaliPixelData04(void)
91 {
92   TestApplication application;
93
94   uint32_t width      = 10u;
95   uint32_t height     = 10u;
96   uint32_t stride     = 12u;
97   uint32_t bufferSize = stride * height * Pixel::GetBytesPerPixel(Pixel::L8);
98   uint8_t* buffer     = new uint8_t[bufferSize];
99   buffer[0]           = 'a';
100
101   PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, stride, Pixel::L8, PixelData::DELETE_ARRAY);
102
103   DALI_TEST_CHECK(pixelData);
104   DALI_TEST_CHECK(pixelData.GetWidth() == width);
105   DALI_TEST_CHECK(pixelData.GetHeight() == height);
106   DALI_TEST_CHECK(pixelData.GetStride() == stride);
107   DALI_TEST_CHECK(pixelData.GetPixelFormat() == Pixel::L8);
108
109   END_TEST;
110 }
111
112 int UtcDaliPixelDataCopyConstructor(void)
113 {
114   TestApplication application;
115
116   unsigned int   width      = 10u;
117   unsigned int   height     = 10u;
118   unsigned int   bufferSize = width * height * Pixel::GetBytesPerPixel(Pixel::L8);
119   unsigned char* buffer     = new unsigned char[bufferSize];
120   PixelData      pixelData  = PixelData::New(buffer, bufferSize, width, height, Pixel::L8, PixelData::DELETE_ARRAY);
121
122   PixelData pixelDataCopy(pixelData);
123
124   DALI_TEST_EQUALS((bool)pixelDataCopy, true, TEST_LOCATION);
125   END_TEST;
126 }
127
128 int UtcDaliPixelDataAssignmentOperator(void)
129 {
130   TestApplication application;
131
132   unsigned int   width      = 10u;
133   unsigned int   height     = 10u;
134   unsigned int   bufferSize = width * height * Pixel::GetBytesPerPixel(Pixel::L8);
135   unsigned char* buffer     = new unsigned char[bufferSize];
136   PixelData      pixelData  = PixelData::New(buffer, bufferSize, width, height, Pixel::L8, PixelData::DELETE_ARRAY);
137
138   PixelData pixelData2;
139   DALI_TEST_EQUALS((bool)pixelData2, false, TEST_LOCATION);
140
141   pixelData2 = pixelData;
142   DALI_TEST_EQUALS((bool)pixelData2, true, TEST_LOCATION);
143
144   END_TEST;
145 }
146
147 int UtcDaliPixelDataMoveConstructor(void)
148 {
149   TestApplication application;
150
151   unsigned int   width      = 10u;
152   unsigned int   height     = 10u;
153   unsigned int   bufferSize = width * height * Pixel::GetBytesPerPixel(Pixel::L8);
154   unsigned char* buffer     = new unsigned char[bufferSize];
155
156   PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::L8, PixelData::DELETE_ARRAY);
157   DALI_TEST_CHECK(pixelData);
158   DALI_TEST_EQUALS(width, pixelData.GetWidth(), TEST_LOCATION);
159   DALI_TEST_EQUALS(height, pixelData.GetHeight(), TEST_LOCATION);
160
161   PixelData moved = std::move(pixelData);
162   DALI_TEST_CHECK(moved);
163   DALI_TEST_EQUALS(width, moved.GetWidth(), TEST_LOCATION);
164   DALI_TEST_EQUALS(height, moved.GetHeight(), TEST_LOCATION);
165   DALI_TEST_CHECK(!pixelData);
166
167   END_TEST;
168 }
169
170 int UtcDaliPixelDataMoveAssignment(void)
171 {
172   TestApplication application;
173
174   unsigned int   width      = 10u;
175   unsigned int   height     = 10u;
176   unsigned int   bufferSize = width * height * Pixel::GetBytesPerPixel(Pixel::L8);
177   unsigned char* buffer     = new unsigned char[bufferSize];
178
179   PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::L8, PixelData::DELETE_ARRAY);
180   DALI_TEST_CHECK(pixelData);
181   DALI_TEST_EQUALS(width, pixelData.GetWidth(), TEST_LOCATION);
182   DALI_TEST_EQUALS(height, pixelData.GetHeight(), TEST_LOCATION);
183
184   PixelData moved;
185   moved = std::move(pixelData);
186   DALI_TEST_CHECK(moved);
187   DALI_TEST_EQUALS(width, moved.GetWidth(), TEST_LOCATION);
188   DALI_TEST_EQUALS(height, moved.GetHeight(), TEST_LOCATION);
189   DALI_TEST_CHECK(!pixelData);
190
191   END_TEST;
192 }
193
194 int UtcDaliPixelDataGetPixelFormatNegative(void)
195 {
196   TestApplication application;
197   Dali::PixelData instance;
198   try
199   {
200     instance.GetPixelFormat();
201     DALI_TEST_CHECK(false); // Should not get here
202   }
203   catch(...)
204   {
205     DALI_TEST_CHECK(true); // We expect an assert
206   }
207   END_TEST;
208 }
209
210 int UtcDaliPixelDataGetWidthNegative(void)
211 {
212   TestApplication application;
213   Dali::PixelData instance;
214   try
215   {
216     instance.GetWidth();
217     DALI_TEST_CHECK(false); // Should not get here
218   }
219   catch(...)
220   {
221     DALI_TEST_CHECK(true); // We expect an assert
222   }
223   END_TEST;
224 }
225
226 int UtcDaliPixelDataGetHeightNegative(void)
227 {
228   TestApplication application;
229   Dali::PixelData instance;
230   try
231   {
232     instance.GetHeight();
233     DALI_TEST_CHECK(false); // Should not get here
234   }
235   catch(...)
236   {
237     DALI_TEST_CHECK(true); // We expect an assert
238   }
239   END_TEST;
240 }
241
242 int UtcDaliPixelDataReleasePixelDataBuffer(void)
243 {
244   TestApplication application;
245
246   uint32_t width      = 10u;
247   uint32_t height     = 10u;
248   uint32_t stride     = 12u;
249   uint32_t bufferSize = stride * height * Pixel::GetBytesPerPixel(Pixel::L8);
250   uint8_t* buffer     = new uint8_t[bufferSize];
251   buffer[0]           = 'a';
252
253   PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, stride, Pixel::L8, PixelData::DELETE_ARRAY);
254
255   DALI_TEST_CHECK(pixelData);
256   DALI_TEST_CHECK(pixelData.GetWidth() == width);
257   DALI_TEST_CHECK(pixelData.GetHeight() == height);
258   DALI_TEST_CHECK(pixelData.GetStride() == stride);
259   DALI_TEST_CHECK(pixelData.GetPixelFormat() == Pixel::L8);
260
261   Dali::Integration::PixelDataBuffer pixelDataBuffer = Dali::Integration::ReleasePixelDataBuffer(pixelData);
262
263   DALI_TEST_CHECK(!pixelData);
264
265   DALI_TEST_EQUALS(pixelDataBuffer.bufferSize, bufferSize, TEST_LOCATION);
266   DALI_TEST_EQUALS(pixelDataBuffer.buffer[0], static_cast<uint8_t>('a'), TEST_LOCATION);
267
268   // Release memory by our self.
269   delete[] pixelDataBuffer.buffer;
270
271   END_TEST;
272 }
273
274 int UtcDaliPixelDataGetPixelDataBuffer(void)
275 {
276   TestApplication application;
277
278   uint32_t width      = 10u;
279   uint32_t height     = 10u;
280   uint32_t stride     = 12u;
281   uint32_t bufferSize = stride * height * Pixel::GetBytesPerPixel(Pixel::L8);
282   uint8_t* buffer     = new uint8_t[bufferSize];
283   buffer[0]           = 'a';
284
285   PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, stride, Pixel::L8, PixelData::DELETE_ARRAY);
286
287   DALI_TEST_CHECK(pixelData);
288   DALI_TEST_CHECK(pixelData.GetWidth() == width);
289   DALI_TEST_CHECK(pixelData.GetHeight() == height);
290   DALI_TEST_CHECK(pixelData.GetStride() == stride);
291   DALI_TEST_CHECK(pixelData.GetPixelFormat() == Pixel::L8);
292
293   Dali::Integration::PixelDataBuffer pixelDataBuffer = Dali::Integration::GetPixelDataBuffer(pixelData);
294
295   DALI_TEST_CHECK(pixelData);
296
297   DALI_TEST_EQUALS(pixelDataBuffer.bufferSize, bufferSize, TEST_LOCATION);
298   DALI_TEST_EQUALS(pixelDataBuffer.buffer[0], static_cast<uint8_t>('a'), TEST_LOCATION);
299
300   END_TEST;
301 }