Add GetNaturalSize to Actor and deriving classes.
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-BitmapImage.cpp
1 /*
2  * Copyright (c) 2014 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 <iostream>
19 #include <algorithm>
20
21 #include <stdlib.h>
22 #include <dali/public-api/dali-core.h>
23 #include <dali-test-suite-utils.h>
24
25 using std::max;
26 using namespace Dali;
27
28 void utc_dali_bitmap_image_startup(void)
29 {
30   test_return_value = TET_UNDEF;
31 }
32
33 void utc_dali_bitmap_image_cleanup(void)
34 {
35   test_return_value = TET_PASS;
36 }
37
38 int UtcDaliBitmapImageNew01(void)
39 {
40   TestApplication application;
41
42   tet_infoline("UtcDaliBitmapImageNew01 - BitmapImage::New(unsigned int, unsigned int, Pixel::Format)");
43
44   // invoke default handle constructor
45   BitmapImage image;
46
47   // initialise handle
48   image = BitmapImage::New(16, 16);
49   application.SendNotification();
50   application.Render(16);
51   application.Render(16);
52   application.SendNotification();
53
54   ImageAttributes attributes = image.GetAttributes();
55
56   DALI_TEST_CHECK( attributes.GetWidth() == 16);
57   END_TEST;
58 }
59
60 int UtcDaliBitmapImageNew02(void)
61 {
62   TestApplication application;
63
64   tet_infoline("UtcDaliBitmapImageNew02 - BitmapImage::New(PixelBuffer*, unsigned int, unsigned int, Pixel::Format, unsigned int)");
65
66   PixelBuffer* buffer = new PixelBuffer[16 * 16];
67   BitmapImage image = BitmapImage::New(buffer, 16, 16, Pixel::A8);
68   application.SendNotification();
69   application.Render(16);
70   application.Render(16);
71   application.SendNotification();
72
73   Dali::ImageAttributes attributes = image.GetAttributes();
74
75   DALI_TEST_CHECK( attributes.GetWidth() == 16);
76
77   delete [] buffer;
78   END_TEST;
79 }
80
81 int UtcDaliBitmapImageNewWithPolicy01(void)
82 {
83   TestApplication application;
84
85   tet_infoline("UtcDaliBitmapImageNewWithPolicy01 - BitmapImage::New(unsigned int, unsigned int, Pixel::Format, LoadPolicy, ReleasePolicy)");
86
87   // Force texture id's
88   std::vector<GLuint> ids;
89   ids.push_back( 23 );
90   application.GetGlAbstraction().SetNextTextureIds( ids );
91
92   // invoke default handle constructor
93   BitmapImage image;
94
95   // initialise handle
96   image = BitmapImage::New(16, 16, Pixel::A8, Image::OnDemand, Image::Unused);
97   application.SendNotification();
98   application.Render(16);
99   application.Render(16);
100   application.SendNotification();
101
102   ImageAttributes attributes = image.GetAttributes();
103   DALI_TEST_CHECK( attributes.GetWidth() == 16);
104   /// @todo: how to test OnDemand? - resource id would be 0 if buffer only allocated on first call to ::GetBuffer()
105
106   ImageActor actor = ImageActor::New(image);
107   Stage::GetCurrent().Add(actor);
108
109   application.SendNotification();
110   application.Render(16);
111   // testing ReleasePolicy::Unused
112   // fake loading image
113   application.Render(16);
114   application.SendNotification();
115
116   DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
117
118   // discard texture when actor comes off stage
119   Stage::GetCurrent().Remove(actor);
120   application.Render(16);
121   application.SendNotification();
122   application.Render(16);
123   application.SendNotification();
124   DALI_TEST_CHECK ( application.GetGlAbstraction().CheckTextureDeleted(23) );
125   END_TEST;
126 }
127
128 int UtcDaliBitmapImageNewWithPolicy02(void)
129 {
130   TestApplication application;
131
132   tet_infoline("UtcDaliBitmapImageNewWithPolicy02 - BitmapImage::New(PixelBuffer*, unsigned int, unsigned int, Pixel::Format, unsigned int, ReleasePolicy)");
133
134   // Force texture id's
135   std::vector<GLuint> ids;
136   ids.push_back( 23 );
137   application.GetGlAbstraction().SetNextTextureIds( ids );
138
139   PixelBuffer* buffer = new PixelBuffer[16 * 16];
140   BitmapImage image = BitmapImage::New(buffer, 16, 16, Pixel::A8, 16, Image::Unused);
141   application.SendNotification();
142   application.Render(16);
143   application.Render(16);
144   application.SendNotification();
145
146   ImageAttributes attributes = image.GetAttributes();
147   DALI_TEST_CHECK( attributes.GetWidth() == 16);
148   /// @todo: how to test OnDemand? - resource id would be 0 if buffer only allocated on first call to ::GetBuffer()
149
150   ImageActor actor = ImageActor::New(image);
151   Stage::GetCurrent().Add(actor);
152
153   application.SendNotification();
154   application.Render(16);
155   // testing ReleasePolicy::Unused
156   // fake loading image
157   application.Render(16);
158   application.SendNotification();
159
160   DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
161
162   // discard texture when actor comes off stage
163   Stage::GetCurrent().Remove(actor);
164   application.Render(16);
165   application.SendNotification();
166   application.Render(16);
167   application.SendNotification();
168   DALI_TEST_CHECK ( application.GetGlAbstraction().CheckTextureDeleted(23) );
169   END_TEST;
170 }
171
172 int UtcDaliBitmapImageDownCast(void)
173 {
174   TestApplication application;
175   tet_infoline("Testing Dali::BitmapImage::DownCast()");
176
177   BitmapImage bitmap = BitmapImage::New(1, 1, Dali::Pixel::BGRA8888);
178   ImageActor imageActor = ImageActor::New(bitmap);
179   application.SendNotification();
180   application.Render(16);
181   application.Render(16);
182   application.SendNotification();
183
184   Image image = imageActor.GetImage();
185   BitmapImage bitmapImage = BitmapImage::DownCast( image );
186
187   DALI_TEST_CHECK(bitmapImage);
188   END_TEST;
189 }
190
191 int UtcDaliBitmapImageDownCast2(void)
192 {
193   TestApplication application;
194   tet_infoline("Testing Dali::BitmapImage::DownCast()");
195
196   Image image = Image::New("IncorrectImageName");
197   ImageActor imageActor = ImageActor::New(image);
198   application.SendNotification();
199   application.Render(16);
200   application.Render(16);
201   application.SendNotification();
202
203   Image image1 = imageActor.GetImage();
204
205   BitmapImage bitmapImage = BitmapImage::DownCast( image1 );
206   DALI_TEST_CHECK(!bitmapImage);
207
208   Actor unInitialzedActor;
209   bitmapImage = BitmapImage::DownCast( unInitialzedActor );
210   DALI_TEST_CHECK(!bitmapImage);
211   END_TEST;
212 }
213
214 int UtcDaliBitmapImageWHITE(void)
215 {
216   TestApplication application;
217
218   tet_infoline("UtcDaliBitmapImageWHITE - BitmapImage::WHITE()");
219
220   BitmapImage image = BitmapImage::WHITE();               // creates a 1x1 RGBA white pixel
221   application.SendNotification();
222   application.Render(16);
223   application.Render(16);
224   application.SendNotification();
225
226   Dali::ImageAttributes attributes = image.GetAttributes();
227   PixelBuffer* buffer = image.GetBuffer();
228
229   DALI_TEST_CHECK( attributes.GetWidth() == 1 &&               // 1 pixel wide
230                    buffer != NULL &&                      // valid buffer
231                    *buffer == 0xff);                       // r component is 255
232   END_TEST;
233 }
234
235 int UtcDaliBitmapImageGetBuffer(void)
236 {
237   TestApplication application;
238
239   tet_infoline("UtcDaliBitmapImageGetBuffer");
240
241   BitmapImage image = BitmapImage::WHITE();               // creates a 1x1 RGBA white pixel
242
243   PixelBuffer* buffer = image.GetBuffer();
244   application.SendNotification();
245   application.Render();
246   application.Render();
247   application.SendNotification();
248
249   ImageAttributes attributes = image.GetAttributes();
250   DALI_TEST_CHECK( attributes.GetWidth() == 1 &&               // 1 pixel wide
251                    buffer != NULL &&                      // valid buffer
252                    *((unsigned int*)buffer) == 0xffffffff); // all component are 255
253   END_TEST;
254 }
255
256 int UtcDaliBitmapImageGetBufferSize(void)
257 {
258   TestApplication application;
259
260   tet_infoline("UtcDaliBitmapImageGetBufferSize");
261
262   BitmapImage image = BitmapImage::WHITE();               // creates a 1x1 RGBA white pixel
263   application.SendNotification();
264   application.Render();
265   application.Render();
266   application.SendNotification();
267
268   Dali::ImageAttributes attributes = image.GetAttributes();
269   PixelBuffer* buffer = image.GetBuffer();
270   unsigned int bufferSize = image.GetBufferSize();
271   unsigned int pixelSize = Pixel::GetBytesPerPixel(attributes.GetPixelFormat());
272
273   DALI_TEST_CHECK( attributes.GetWidth() == 1 &&               // 1 pixel wide
274                    buffer != NULL &&                      // valid buffer
275                    bufferSize == pixelSize);              // r component is 255
276   END_TEST;
277 }
278
279 int UtcDaliBitmapImageGetBufferStride(void)
280 {
281   TestApplication application;
282
283   tet_infoline("UtcDaliBitmapImageGetBufferStride");
284
285   BitmapImage image = BitmapImage::WHITE();               // creates a 1x1 RGBA white pixel
286   application.SendNotification();
287   application.Render();
288   application.Render();
289   application.SendNotification();
290
291   Dali::ImageAttributes attributes = image.GetAttributes();
292   unsigned int pixelSize = Pixel::GetBytesPerPixel(attributes.GetPixelFormat());
293   unsigned int bufferStride = image.GetBufferStride();
294   DALI_TEST_CHECK( bufferStride == pixelSize );
295   DALI_TEST_CHECK( !image.IsDataExternal() );
296
297   PixelBuffer* buffer = new PixelBuffer[20 * 16];
298   image = BitmapImage::New(buffer, 16, 16, Pixel::A8, 20);
299   application.SendNotification();
300   application.Render(16);
301   application.Render(16);
302   application.SendNotification();
303
304   bufferStride = image.GetBufferStride();
305
306   DALI_TEST_CHECK( bufferStride == 20);
307   DALI_TEST_CHECK( image.IsDataExternal() );
308
309   delete [] buffer;
310   END_TEST;
311 }
312
313
314 int UtcDaliBitmapImageIsDataExternal(void)
315 {
316   TestApplication application;
317
318   tet_infoline("UtcDaliBitmapImageIsDataExternal - BitmapImage::IsDataExternal()");
319
320   PixelBuffer* buffer = new PixelBuffer[16 * 16];
321   BitmapImage image = BitmapImage::New(buffer, 16, 16, Pixel::A8);
322   application.SendNotification();
323   application.Render();
324   application.Render();
325   application.SendNotification();
326
327   DALI_TEST_CHECK( image.IsDataExternal() );
328   END_TEST;
329 }
330
331 namespace
332 {
333
334 static bool SignalReceived;
335 static void ImageUploaded(Image image)
336 {
337   tet_infoline("Received image uploaded signal");
338   SignalReceived = true;
339 }
340 }
341
342 int UtcDaliBitmapImageUpdate01(void)
343 {
344   TestApplication application;
345
346   tet_infoline("UtcDaliBitmapImageUpdate01 - single empty rect");
347
348   PixelBuffer* buffer = new PixelBuffer[16 * 16];
349
350   BitmapImage image = BitmapImage::New(buffer, 16, 16, Pixel::A8);
351   ImageActor actor = ImageActor::New(image);
352   Stage::GetCurrent().Add(actor);
353   actor.SetVisible(true);
354
355   SignalReceived = false;
356   image.UploadedSignal().Connect( ImageUploaded );
357
358   std::vector<GLuint> ids;
359   ids.push_back(200);
360   ids.push_back(201);
361   ids.push_back(202);
362   application.GetGlAbstraction().SetNextTextureIds(ids);
363
364   // Allow actor to be staged and rendered
365   application.SendNotification();
366   application.Render(0);
367   application.Render(16);
368   application.SendNotification();
369   application.Render(16);
370   application.SendNotification();
371
372   DALI_TEST_CHECK( image.IsDataExternal() );
373   application.GetGlAbstraction().EnableTextureCallTrace(true);
374
375   image.Update();//(RectArea()); // notify Core that the image has been updated
376   application.SendNotification();
377   application.Render(16);
378   application.Render(16);
379   application.SendNotification();
380   application.Render(16);
381   application.SendNotification();
382
383   const TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
384   DALI_TEST_EQUALS( callStack.TestMethodAndParams(0, "TexSubImage2D", "0, 0, 16, 16"), true, TEST_LOCATION);
385
386   DALI_TEST_CHECK( SignalReceived == true );
387   SignalReceived = false;
388   END_TEST;
389 }
390
391 int UtcDaliBitmapImageUpdate02(void)
392 {
393   TestApplication application;
394
395   tet_infoline("UtcDaliBitmapImageUpdate02 - Multiple rects");
396
397   PixelBuffer* buffer = new PixelBuffer[16 * 16];
398   BitmapImage image = BitmapImage::New(buffer, 16, 16, Pixel::A8);
399   ImageActor actor = ImageActor::New(image);
400   Stage::GetCurrent().Add(actor);
401   actor.SetVisible(true);
402
403   SignalReceived = false;
404   image.UploadedSignal().Connect( ImageUploaded );
405
406   std::vector<GLuint> ids;
407   ids.push_back(200);
408   ids.push_back(201);
409   ids.push_back(202);
410   application.GetGlAbstraction().SetNextTextureIds(ids);
411
412   application.SendNotification();
413   application.Render(0);
414   application.Render(16);
415   application.SendNotification();
416   application.Render(16);
417   application.SendNotification();
418
419   DALI_TEST_CHECK( image.IsDataExternal() );
420   application.GetGlAbstraction().EnableTextureCallTrace(true);
421
422   image.Update(RectArea(9,9,5,5));              // notify Core that the image has been updated
423
424   application.SendNotification();
425   application.Render(16);
426   application.Render(16);
427   application.SendNotification();
428   application.Render(16);
429   application.SendNotification();
430
431   const TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
432   DALI_TEST_EQUALS( callStack.TestMethodAndParams(0, "TexSubImage2D", "9, 9, 5, 1"), true, TEST_LOCATION);
433   DALI_TEST_EQUALS( callStack.TestMethodAndParams(1, "TexSubImage2D", "9, 10, 5, 1"), true, TEST_LOCATION);
434   DALI_TEST_EQUALS( callStack.TestMethodAndParams(2, "TexSubImage2D", "9, 11, 5, 1"), true, TEST_LOCATION);
435   DALI_TEST_EQUALS( callStack.TestMethodAndParams(3, "TexSubImage2D", "9, 12, 5, 1"), true, TEST_LOCATION);
436   DALI_TEST_EQUALS( callStack.TestMethodAndParams(4, "TexSubImage2D", "9, 13, 5, 1"), true, TEST_LOCATION);
437
438   DALI_TEST_CHECK( SignalReceived == true );
439   SignalReceived = false;
440   END_TEST;
441 }
442
443 int UtcDaliBitmapImageUploadedSignal01(void)
444 {
445   TestApplication application;
446
447   tet_infoline("UtcDaliBitmapImageUploadedSignal - Test that Uploaded signal is sent when image is staged");
448
449   PixelBuffer* buffer = new PixelBuffer[16 * 16];
450   BitmapImage image = BitmapImage::New(buffer, 16, 16, Pixel::A8);
451   SignalReceived = false;
452   image.UploadedSignal().Connect( ImageUploaded );
453
454   application.SendNotification();
455   application.Render(16);
456   application.Render(16);
457   application.SendNotification();
458
459   Dali::ImageActor imageActor = ImageActor::New(image);
460   Stage::GetCurrent().Add(imageActor);
461   application.SendNotification();
462   application.Render(16);
463   application.SendNotification();
464   application.Render(16);
465   application.SendNotification();
466   application.Render(16);
467   application.SendNotification();
468
469   DALI_TEST_CHECK( SignalReceived == true );
470   END_TEST;
471 }
472
473 int UtcDaliBitmapImageUploadedSignal02(void)
474 {
475   TestApplication application;
476
477   tet_infoline("UtcDaliBitmapImageUploadedSignal - Test that Uploaded signal is sent after Update");
478
479   PixelBuffer* buffer = new PixelBuffer[16 * 16];
480   BitmapImage image = BitmapImage::New(buffer, 16, 16, Pixel::A8);
481   SignalReceived = false;
482   //ScopedConnection connection =
483   image.UploadedSignal().Connect( ImageUploaded );
484
485   application.SendNotification();
486   application.Render(16);
487   application.Render(16);
488   application.SendNotification();
489
490   Dali::ImageActor imageActor = ImageActor::New(image);
491   Stage::GetCurrent().Add(imageActor);
492   application.SendNotification();
493   application.Render(16);
494   application.SendNotification();
495   application.Render(16);
496   application.SendNotification();
497   application.Render(16);
498   application.SendNotification();
499   DALI_TEST_CHECK( SignalReceived == true );
500   SignalReceived = false;
501
502   image.Update(RectArea());              // notify Core that the whole image has been updated
503   application.SendNotification();
504   application.Render(16);
505   application.SendNotification();
506   application.Render(16);
507   application.SendNotification();
508   application.Render(16);
509   application.SendNotification();
510   DALI_TEST_CHECK( SignalReceived == true );
511   END_TEST;
512 }