Merge "Remove unsafe new-delete pair from program and change unoptimal resizes to...
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-ResourceImage.cpp
1 /*
2  * Copyright (c) 2015 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 #include <stdlib.h>
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23
24 using namespace Dali;
25
26 void utc_dali_resource_image_startup(void)
27 {
28   test_return_value = TET_UNDEF;
29 }
30
31 void utc_dali_resource_image_cleanup(void)
32 {
33   test_return_value = TET_PASS;
34 }
35
36 static const char* gTestImageFilename = "icon_wrt.png";
37
38 namespace
39 {
40
41 void LoadBitmapResource(TestPlatformAbstraction& platform)
42 {
43   Integration::ResourceRequest* request = platform.GetRequest();
44   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD );
45   Integration::ResourcePointer resource(bitmap);
46   bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 80, 80, 80, 80);
47
48   if(request)
49   {
50     platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
51   }
52 }
53
54 } // namespace
55
56
57 // 1.1
58 int UtcDaliResourceImageNew01(void)
59 {
60   TestApplication application;
61
62   tet_infoline("UtcDaliResourceImageNew01 - ResourceImage::New(const std::string&)");
63
64   // invoke default handle constructor
65   ResourceImage image;
66
67   DALI_TEST_CHECK( !image );
68
69   // initialise handle
70   image = ResourceImage::New(gTestImageFilename);
71
72   DALI_TEST_CHECK( image );
73   END_TEST;
74 }
75
76 // 1.2
77 int UtcDaliResourceImageNew02(void)
78 {
79   TestApplication application;
80
81   tet_infoline("UtcDaliREsourceImageNew02 - ResourceImage::New(const std::string&, const ImageAttributes&)");
82
83   // invoke default handle constructor
84   ResourceImage image;
85
86   DALI_TEST_CHECK( !image );
87
88   // initialise handle
89   Dali::ImageAttributes imageAttributes;
90   imageAttributes.SetSize(128, 256);
91   imageAttributes.SetScalingMode(Dali::ImageAttributes::FitHeight);
92   image = ResourceImage::New(gTestImageFilename, imageAttributes);
93
94   DALI_TEST_CHECK( image );
95   END_TEST;
96 }
97
98 // 1.3
99 int UtcDaliResourceImageNewWithPolicies01(void)
100 {
101   TestApplication application;
102   TestPlatformAbstraction& platform = application.GetPlatform();
103
104   // testing delayed loading
105   tet_infoline("UtcDaliResourceImageNewWithPolicies01 - Load image with LoadPolicy::OnDemand, ReleasePolicy::Never");
106   DALI_TEST_CHECK( !platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
107   ResourceImage image = ResourceImage::New(gTestImageFilename, ResourceImage::ON_DEMAND, Image::NEVER);
108
109   DALI_TEST_CHECK( image );
110
111   application.SendNotification();
112   application.Render(16);
113
114   // request file loading only when actor added to stage
115   DALI_TEST_CHECK( !platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
116
117   ImageActor actor = ImageActor::New(image);
118
119   Stage::GetCurrent().Add(actor);
120
121   application.SendNotification();
122   application.Render(16);
123
124   DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
125
126   // testing ReleasePolicy::Never
127   // fake loading image
128   std::vector<GLuint> ids;
129   ids.push_back( 23 );
130   application.GetGlAbstraction().SetNextTextureIds( ids );
131   LoadBitmapResource( platform );
132
133   application.Render(16);
134   application.SendNotification();
135
136   DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
137
138   // never discard texture
139   Stage::GetCurrent().Remove(actor);
140   application.Render(16);
141   application.SendNotification();
142   application.Render(16);
143   application.SendNotification();
144   DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
145   END_TEST;
146 }
147
148 // 1.4
149 int UtcDaliResourceImageNewWithPolicies02(void)
150 {
151   TestApplication application;
152   TestPlatformAbstraction& platform = application.GetPlatform();
153   const Vector2 closestImageSize( 80, 45);
154   platform.SetClosestImageSize(closestImageSize);
155
156   // testing resource deletion when taken off stage
157   tet_infoline("UtcDaliResourceImageNewWithPolicies02 - Load image with LoadPolicy::OnDemand, ReleasePolicy::Unused");
158
159   ResourceImage image = ResourceImage::New(gTestImageFilename, ResourceImage::ON_DEMAND, Image::UNUSED);
160
161   DALI_TEST_CHECK( image );
162
163   application.SendNotification();
164   application.Render(16);
165
166   // request file loading only when actor added to stage
167   DALI_TEST_CHECK( !platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
168
169   ImageActor actor = ImageActor::New(image);
170
171   Stage::GetCurrent().Add(actor);
172
173   application.SendNotification();
174   application.Render(16);
175
176   DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
177
178   // testing ReleasePolicy::Unused
179   // fake loading image
180   std::vector<GLuint> ids;
181   ids.push_back( 23 );
182   application.GetGlAbstraction().SetNextTextureIds( ids );
183   LoadBitmapResource( platform );
184
185   application.Render(16);
186   application.SendNotification();
187
188   DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
189
190   // discard texture when actor comes off stage
191   Stage::GetCurrent().Remove(actor);
192   application.Render(16);
193   application.SendNotification();
194   application.Render(16);
195   application.SendNotification();
196   DALI_TEST_CHECK ( application.GetGlAbstraction().CheckTextureDeleted(23) );
197   END_TEST;
198 }
199
200 // 1.5
201 int UtcDaliResourceImageNewWithPolicies03(void)
202 {
203   TestApplication application;
204   TestPlatformAbstraction& platform = application.GetPlatform();
205   const Vector2 closestImageSize( 80, 45);
206   platform.SetClosestImageSize(closestImageSize);
207
208   // load immediately -> resource deletion when taken off stage -> put actor back on stage -> load resource again
209   tet_infoline("UtcDaliResourceImageNewWithPolicies03 - Load image with LoadPolicy::Immediate, ReleasePolicy::Unused");
210
211   ResourceImage image = ResourceImage::New(gTestImageFilename, ResourceImage::IMMEDIATE, Image::UNUSED);
212
213   DALI_TEST_CHECK( image );
214
215   application.SendNotification();
216   application.Render(16);
217
218   // request file loading immediately
219   DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
220
221   ImageActor actor = ImageActor::New(image);
222
223   Stage::GetCurrent().Add(actor);
224
225   application.SendNotification();
226   application.Render(16);
227
228   // testing ReleasePolicy::Unused
229   // fake loading image
230   std::vector<GLuint> ids;
231   ids.push_back( 23 );
232   application.GetGlAbstraction().SetNextTextureIds( ids );
233   LoadBitmapResource( platform );
234
235   application.Render(16);
236   application.SendNotification();
237
238   DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
239
240   // discard texture when actor comes off stage
241   Stage::GetCurrent().Remove(actor);
242   application.Render(16);
243   application.SendNotification();
244   application.Render(16);
245   application.SendNotification();
246   DALI_TEST_CHECK ( application.GetGlAbstraction().CheckTextureDeleted(23) );
247
248   // check load request when actor added back to stage
249   application.GetPlatform().ResetTrace();
250
251   Stage::GetCurrent().Add(actor);
252
253   application.SendNotification();
254   application.Render(16);
255   application.SendNotification();
256   application.Render(16);
257
258   DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
259   END_TEST;
260 }
261
262 // 1.6
263 int UtcDaliResourceImageNewWithPolicies04(void)
264 {
265   TestApplication application;
266   TestPlatformAbstraction& platform = application.GetPlatform();
267
268   // load immediately, don't release texture when off stage
269   tet_infoline("UtcDaliResourceImageNewWithPolicies03 - Load image with LoadPolicy::Immediate, ReleasePolicy::Never");
270
271   ResourceImage image = ResourceImage::New(gTestImageFilename, ResourceImage::IMMEDIATE, Image::NEVER);
272
273   DALI_TEST_CHECK( image );
274
275   application.SendNotification();
276   application.Render(16);
277
278   // request file loading immediately
279   DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
280
281   ImageActor actor = ImageActor::New(image);
282
283   Stage::GetCurrent().Add(actor);
284
285   application.SendNotification();
286   application.Render(16);
287
288   // testing ReleasePolicy::Never
289   // fake loading image
290   std::vector<GLuint> ids;
291   ids.push_back( 23 );
292   application.GetGlAbstraction().SetNextTextureIds( ids );
293   LoadBitmapResource(platform);
294
295   application.Render(16);
296   application.SendNotification();
297
298   DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
299
300   // texture is not discarded
301   Stage::GetCurrent().Remove(actor);
302   application.Render(16);
303   application.SendNotification();
304   application.Render(16);
305   application.SendNotification();
306   DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
307
308   // no load request when actor added back to stage
309   application.GetPlatform().ResetTrace();
310
311   Stage::GetCurrent().Add(actor);
312
313   application.SendNotification();
314   application.Render(16);
315   application.SendNotification();
316   application.Render(16);
317
318   DALI_TEST_CHECK( !platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
319   END_TEST;
320 }
321
322 // 1.7
323 int UtcDaliResourceImageDownCast(void)
324 {
325   TestApplication application;
326   tet_infoline("Testing Dali::ResourceImage::DownCast()");
327
328   ResourceImage image = ResourceImage::New(gTestImageFilename);
329
330   BaseHandle object(image);
331
332   ResourceImage image2 = ResourceImage::DownCast(object);
333   DALI_TEST_CHECK(image2);
334
335   ResourceImage image3 = DownCast< ResourceImage >(object);
336   DALI_TEST_CHECK(image3);
337
338   BaseHandle unInitializedObject;
339   ResourceImage image4 = ResourceImage::DownCast(unInitializedObject);
340   DALI_TEST_CHECK(!image4);
341
342   ResourceImage image5 = DownCast< ResourceImage >(unInitializedObject);
343   DALI_TEST_CHECK(!image5);
344
345   Image image6 = ResourceImage::New(gTestImageFilename);
346   ResourceImage image7 = ResourceImage::DownCast(image6);
347   DALI_TEST_CHECK(image7);
348   END_TEST;
349 }
350
351 // 1.8
352 int UtcDaliResourceImageGetImageSize(void)
353 {
354   TestApplication application;
355   TestPlatformAbstraction& platform = application.GetPlatform();
356
357   tet_infoline("UtcDaliResourceImageGetImageSize - ResourceImage::GetImageSize()");
358
359   Vector2 testSize(8.0f, 16.0f);
360   platform.SetClosestImageSize(testSize);
361
362   Vector2 size = ResourceImage::GetImageSize(gTestImageFilename);
363
364   DALI_TEST_CHECK( application.GetPlatform().GetTrace().FindMethod("GetClosestImageSize"));
365   DALI_TEST_EQUALS( size, testSize, TEST_LOCATION);
366   END_TEST;
367 }
368
369 // 1.9
370 int UtcDaliResourceImageGetUrl(void)
371 {
372   TestApplication application;
373
374   tet_infoline("UtcDaliResourceImageGetFilename - ResourceImage::GetUrl()");
375
376   // invoke default handle constructor
377   ResourceImage image;
378
379   DALI_TEST_CHECK( !image );
380
381   // initialise handle
382   image = ResourceImage::New(gTestImageFilename);
383
384   DALI_TEST_EQUALS( image.GetUrl(), gTestImageFilename, TEST_LOCATION);
385   END_TEST;
386 }
387
388 // 1.10
389 int UtcDaliResourceImageGetLoadingState01(void)
390 {
391   TestApplication application;
392   tet_infoline("UtcDaliResourceImageGetLoadingState01");
393
394   ResourceImage image = ResourceImage::New(gTestImageFilename);
395   DALI_TEST_CHECK(image.GetLoadingState() == ResourceLoading);
396   application.SendNotification();
397   application.Render(16);
398
399   // simulate load success
400   TestPlatformAbstraction& platform = application.GetPlatform();
401   LoadBitmapResource( platform );
402   application.Render(16);
403   application.SendNotification();
404
405   // Test state == ResourceLoadingSucceeded
406   DALI_TEST_CHECK(image.GetLoadingState() == ResourceLoadingSucceeded);
407   END_TEST;
408 }
409
410 // 1.11
411 int UtcDaliResourceImageGetLoadingState02(void)
412 {
413   TestApplication application;
414
415   tet_infoline("UtcDaliResourceImageGetLoadingState02");
416
417   // invoke default handle constructor
418   ResourceImage image;
419
420   DALI_TEST_CHECK( !image );
421
422   // initialise handle
423   image = ResourceImage::New(gTestImageFilename);
424
425   // Test state == ResourceLoading
426   DALI_TEST_CHECK(image.GetLoadingState() == ResourceLoading);
427   application.SendNotification();
428   application.Render(16);
429
430   // simulate load failure
431   Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
432   if(request)
433   {
434     application.GetPlatform().SetResourceLoadFailed(request->GetId(), Integration::FailureUnknown);
435   }
436   application.Render(16);
437   application.SendNotification();
438
439   // Test state == ResourceLoadingFailed
440   DALI_TEST_CHECK(image.GetLoadingState() == ResourceLoadingFailed);
441   END_TEST;
442 }
443
444 // 1.12
445 int UtcDaliResourceImageGetLoadPolicy(void)
446 {
447   TestApplication application;
448
449   tet_infoline("UtcDaliImageGetLoadPolicy");
450
451   ResourceImage image = ResourceImage::New(gTestImageFilename, ResourceImage::ON_DEMAND, Image::NEVER);
452
453   DALI_TEST_CHECK( image );
454
455   DALI_TEST_CHECK( ResourceImage::ON_DEMAND == image.GetLoadPolicy());
456   END_TEST;
457 }
458
459 static bool SignalLoadFlag = false;
460
461 static void SignalLoadHandler(ResourceImage image)
462 {
463   tet_infoline("Received image load finished signal");
464
465   SignalLoadFlag = true;
466 }
467
468 // 1.13
469 int UtcDaliResourceImageSignalLoadingFinished(void)
470 {
471   TestApplication application;
472
473   tet_infoline("UtcDaliResourceImageSignalLoadingFinished");
474
475   SignalLoadFlag = false;
476
477   ResourceImage image = ResourceImage::New(gTestImageFilename);
478
479   image.LoadingFinishedSignal().Connect( SignalLoadHandler );
480   application.SendNotification();
481   application.Render(16);
482
483   Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
484   if(request)
485   {
486     application.GetPlatform().SetResourceLoaded(request->GetId(), request->GetType()->id, Integration::ResourcePointer(Integration::Bitmap::New(Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD)));
487   }
488
489   application.Render(16);
490   application.SendNotification();
491
492   DALI_TEST_CHECK( SignalLoadFlag == true );
493   END_TEST;
494 }