Revert "License conversion from Flora to Apache 2.0"
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-internal / utc-Dali-Internal-ImageFactory.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 #include <iostream>
18
19 #include <stdlib.h>
20 #include <dali/dali.h>
21 #include <dali-test-suite-utils.h>
22
23 // Internal headers are allowed here
24 #include <dali/internal/event/common/thread-local-storage.h>
25 #include <dali/internal/event/images/image-factory.h>
26 #include <dali/internal/event/resources/resource-ticket.h>
27
28 using namespace Dali;
29
30 using Internal::ResourceTicketPtr;
31 using Internal::ImageFactory;
32 using Internal::ImageFactoryCache::RequestPtr;
33
34
35 namespace
36 {
37
38 static const char* gTestImageFilename = "icon_wrt.png";
39
40 static void EmulateImageLoaded( TestApplication& application, unsigned int width, unsigned int height )
41 {
42   // emulate load success
43   Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
44   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS,  true  );
45   Integration::ResourcePointer resource( bitmap );
46   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, width, height, width, height );
47   if( request )
48   {
49     application.GetPlatform().SetResourceLoaded( request->GetId(), request->GetType()->id, resource );
50   }
51
52   application.SendNotification();
53   application.Render();
54
55   application.SendNotification();
56   application.Render();
57 }
58
59 } //anonymous namespace
60
61
62 // High-level test for image factory request cache
63 int UtcDaliImageFactoryUseCachedRequest01(void)
64 {
65   TestApplication application;
66
67   tet_infoline( "UtcDaliImageFactoryCachedRequest01 - Request same image more than once" );
68
69   Image image = Image::New( gTestImageFilename );
70
71   application.SendNotification();
72   application.Render();
73   DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
74   application.GetPlatform().ResetTrace();
75
76   Image image2 = Image::New( gTestImageFilename );
77
78   application.SendNotification();
79   application.Render();
80
81   // check resource is not loaded twice
82   DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
83   application.GetPlatform().ResetTrace();
84
85   Image image3 = Image::New( gTestImageFilename );
86
87   application.SendNotification();
88   application.Render();
89   DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
90   END_TEST;
91 }
92
93 // High-level test for image factory request cache
94 int UtcDaliImageFactoryUseCachedRequest02(void)
95 {
96   TestApplication application;
97
98   // testing resource deletion when taken off stage
99   tet_infoline( "UtcDaliImageFactoryCachedRequest02 - Discard previously requested resource" );
100
101   Image image = Image::New( gTestImageFilename, Image::Immediate, Image::Unused );
102   ImageActor actor = ImageActor::New( image );
103
104   application.SendNotification();
105   application.Render();
106
107   DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
108   application.GetPlatform().ResetTrace();
109
110   // Add actor to stage
111   Stage::GetCurrent().Add( actor );
112
113   application.Render();
114   application.SendNotification();
115   application.Render();
116   application.SendNotification();
117
118   // Release the resource, request is still cached
119   Stage::GetCurrent().Remove( actor );
120   application.Render();
121   application.SendNotification();
122   application.Render();
123   application.SendNotification();
124
125   // Should find stale request in cache, so load image from filesystem
126   Image image2 = Image::New( gTestImageFilename );
127
128   application.SendNotification();
129   application.Render();
130
131   DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
132   application.GetPlatform().ResetTrace();
133
134   // Resource is reloaded
135   Image image3 = Image::New( gTestImageFilename );
136
137   application.SendNotification();
138   application.Render();
139   application.SendNotification();
140   application.Render();
141
142   DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
143   END_TEST;
144 }
145
146 // Low-level test for image factory request cache
147 int UtcDaliImageFactoryUseCachedRequest03(void)
148 {
149   TestApplication application;
150   tet_infoline( "UtcDaliImageFactoryCachedRequest03 - Request same image more than once - Request Ids" );
151
152   ImageFactory& imageFactory  = Internal::ThreadLocalStorage::Get().GetImageFactory();
153
154   RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
155   ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
156
157   RequestPtr req2 = imageFactory.RegisterRequest( gTestImageFilename, NULL );
158   ResourceTicketPtr ticket2 = imageFactory.Load( req2.Get() );
159   DALI_TEST_EQUALS( req, req2, TEST_LOCATION );
160   DALI_TEST_EQUALS( ticket, ticket2, TEST_LOCATION );
161
162   req2 = imageFactory.RegisterRequest( gTestImageFilename, NULL );
163   ResourceTicketPtr ticket3 = imageFactory.Load( req2.Get() );
164   DALI_TEST_EQUALS( req, req2, TEST_LOCATION );
165   DALI_TEST_EQUALS( ticket, ticket3, TEST_LOCATION );
166
167   // request differs in scaled size - not default size
168   ImageAttributes attr = ImageAttributes::New( 80, 160, Pixel::BGR8888 );
169   req2 = imageFactory.RegisterRequest( gTestImageFilename, &attr );
170   ResourceTicketPtr ticket4 = imageFactory.Load( req2.Get() );
171   DALI_TEST_CHECK( req != req2 );
172   END_TEST;
173 }
174
175 // Low-level test for image factory request cache
176 int UtcDaliImageFactoryUseCachedRequest04(void)
177 {
178   TestApplication application;
179   tet_infoline( "UtcDaliImageFactoryCachedRequest04 - Request same image with different Image objects - Request Ids" );
180
181   ImageFactory& imageFactory  = Internal::ThreadLocalStorage::Get().GetImageFactory();
182
183   ImageAttributes attr = ImageAttributes::New( 80, 160, Pixel::BGR8888 );
184   RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, &attr );
185
186   ImageAttributes attr2 = ImageAttributes::New( 80, 160, Pixel::BGR8888 );
187   RequestPtr req2 = imageFactory.RegisterRequest( gTestImageFilename, &attr2 );
188   DALI_TEST_EQUALS( req, req2, TEST_LOCATION );
189   END_TEST;
190 }
191
192 // Different requests, compatible resource
193 int UtcDaliImageFactoryCompatibleResource01(void)
194 {
195   TestApplication application;
196   tet_infoline( "UtcDaliImageFactoryCompatibleResource01 - Two requests mapping to same resource" );
197
198   ImageFactory& imageFactory  = Internal::ThreadLocalStorage::Get().GetImageFactory();
199
200   Vector2 testSize(80.0f, 80.0f);
201   application.GetPlatform().SetClosestImageSize(testSize);
202
203   // request with default attributes ( size is 0,0 )
204   RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
205   ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
206
207   application.SendNotification();
208   application.Render();
209   application.SendNotification();
210   application.Render();
211
212   // emulate load success
213   EmulateImageLoaded( application, 80, 80 );
214
215   ImageAttributes attr = ImageAttributes::New();
216   attr.SetSize( 80, 80 );
217   RequestPtr req2 = imageFactory.RegisterRequest( gTestImageFilename, &attr );
218   ResourceTicketPtr ticket2 = imageFactory.Load( req2.Get() );
219
220   DALI_TEST_CHECK( req != req2 ); // different requests
221   DALI_TEST_EQUALS( ticket->GetId(), ticket2->GetId(), TEST_LOCATION ); // same resource
222   END_TEST;
223 }
224
225 // Different requests, compatible resource
226 int UtcDaliImageFactoryCompatibleResource02(void)
227 {
228   TestApplication application;
229   tet_infoline( "UtcDaliImageFactoryCompatibleResource02 - Two requests mapping to same resource." );
230
231   ImageFactory& imageFactory  = Internal::ThreadLocalStorage::Get().GetImageFactory();
232
233   Vector2 testSize(80.0f, 80.0f);
234   application.GetPlatform().SetClosestImageSize(testSize);
235
236   // request with default attributes ( size is 0,0 )
237   RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
238   ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
239
240   application.SendNotification();
241   application.Render();
242   application.SendNotification();
243   application.Render();
244
245   // emulate load success
246   EmulateImageLoaded( application, 80, 80 );
247
248   // Request bigger size than actual image.
249   // This will load the same resource.
250   // However if image size changes later on to eg. 512*512 (file is overwritten),
251   // reissuing these two requests will load different resources.
252   // See UtcDaliImageFactoryReload06
253   ImageAttributes attr = ImageAttributes::New();
254   attr.SetSize( 92, 92 );
255   RequestPtr req2 = imageFactory.RegisterRequest( gTestImageFilename, &attr );
256   ResourceTicketPtr ticket2 = imageFactory.Load( req2.Get() );
257
258   DALI_TEST_CHECK( req != req2 ); // different requests
259   DALI_TEST_EQUALS( ticket->GetId(), ticket2->GetId(), TEST_LOCATION ); // same resource
260   END_TEST;
261 }
262
263 // Different requests, compatible resource
264 int UtcDaliImageFactoryCompatibleResource03(void)
265 {
266   TestApplication application;
267   tet_infoline( "UtcDaliImageFactoryCompatibleResource03 - Two requests mapping to same resource" );
268
269   ImageFactory& imageFactory  = Internal::ThreadLocalStorage::Get().GetImageFactory();
270
271   Vector2 testSize(80.0f, 80.0f);
272   application.GetPlatform().SetClosestImageSize(testSize);
273
274   // this time use defined attributes, nut just NULL
275   ImageAttributes attr = ImageAttributes::New();
276   attr.SetSize( 120, 120 );
277
278   // request with default attributes ( size is 0,0 )
279   RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, &attr );
280   ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
281
282   application.SendNotification();
283   application.Render();
284   application.SendNotification();
285   application.Render();
286
287   // emulate load success
288   EmulateImageLoaded( application, 80, 80 );
289
290   ImageAttributes attr2 = ImageAttributes::New();
291   attr2.SetSize( 80, 80 );
292   RequestPtr req2 = imageFactory.RegisterRequest( gTestImageFilename, &attr2 );
293   ResourceTicketPtr ticket2 = imageFactory.Load( req2.Get() );
294
295   DALI_TEST_CHECK( req != req2 ); // different requests
296   DALI_TEST_EQUALS( ticket->GetId(), ticket2->GetId(), TEST_LOCATION ); // same resource
297   END_TEST;
298 }
299
300 // Test for reloading image
301 int UtcDaliImageFactoryReload01(void)
302 {
303   TestApplication application;
304   tet_infoline( "UtcDaliImageFactoryReload01 - Reload unchanged image" );
305
306   Vector2 testSize(80.0f, 80.0f);
307   application.GetPlatform().SetClosestImageSize(testSize);
308
309   ImageFactory& imageFactory  = Internal::ThreadLocalStorage::Get().GetImageFactory();
310
311   RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
312   ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
313
314   ResourceTicketPtr ticket2 = imageFactory.Reload( req.Get() );
315   DALI_TEST_EQUALS( ticket, ticket2, TEST_LOCATION );
316
317   ResourceTicketPtr ticket3 = imageFactory.Reload( req.Get() );
318   DALI_TEST_EQUALS( ticket, ticket3, TEST_LOCATION );
319   END_TEST;
320 }
321
322 // Testing file system access when reloading image
323 int UtcDaliImageFactoryReload02(void)
324 {
325   TestApplication application;
326   tet_infoline( "UtcDaliImageFactoryReload02 - Reload unchanged image" );
327
328   ImageFactory& imageFactory  = Internal::ThreadLocalStorage::Get().GetImageFactory();
329
330   Vector2 testSize(80.0f, 80.0f);
331   application.GetPlatform().SetClosestImageSize(testSize);
332
333   RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
334   ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
335
336   application.SendNotification();
337   application.Render();
338   application.SendNotification();
339   application.Render();
340
341   DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
342   application.GetPlatform().ResetTrace();
343
344   ResourceTicketPtr ticket2 = imageFactory.Reload( req.Get() );
345
346   application.SendNotification();
347   application.Render();
348   application.SendNotification();
349   application.Render();
350
351   DALI_TEST_EQUALS( ticket, ticket2, TEST_LOCATION );
352   // resource is still loading, do not issue another request
353   DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
354
355   // emulate load success
356   EmulateImageLoaded( application, 80, 80 );
357
358   ResourceTicketPtr ticket3 = imageFactory.Reload( req.Get() );
359
360   application.SendNotification();
361   application.Render();
362   application.SendNotification();
363   application.Render();
364
365   DALI_TEST_EQUALS( ticket, ticket3, TEST_LOCATION );
366   DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
367   application.GetPlatform().ResetTrace();
368
369   ticket3 = imageFactory.Reload( req.Get() );
370
371   application.SendNotification();
372   application.Render();
373   application.SendNotification();
374   application.Render();
375
376   DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
377   END_TEST;
378 }
379
380 // Test for reloading changed image
381 int UtcDaliImageFactoryReload03(void)
382 {
383   TestApplication application;
384   tet_infoline( "UtcDaliImageFactoryReload03 - Reload changed image" );
385
386   ImageFactory& imageFactory  = Internal::ThreadLocalStorage::Get().GetImageFactory();
387
388   Vector2 testSize( 80.0f, 80.0f );
389   application.GetPlatform().SetClosestImageSize( testSize );
390
391   RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
392   ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
393
394   application.SendNotification();
395   application.Render();
396
397   // emulate load success
398   EmulateImageLoaded( application, 80, 80 );
399
400   Vector2 newSize( 192.0f, 192.0f );
401   application.GetPlatform().SetClosestImageSize( newSize );
402
403   // Image file changed size, new resource request should be issued
404   ResourceTicketPtr ticket2 = imageFactory.Reload( req.Get() );
405   DALI_TEST_CHECK( ticket != ticket2 );
406
407   ResourceTicketPtr ticket3 = imageFactory.Reload( req.Get() );
408   DALI_TEST_EQUALS( ticket2, ticket3, TEST_LOCATION );
409   END_TEST;
410 }
411
412 // Testing file system access when reloading image
413 int UtcDaliImageFactoryReload04(void)
414 {
415   TestApplication application;
416   tet_infoline( "UtcDaliImageFactoryReload04 - Reload unchanged image" );
417
418   ImageFactory& imageFactory  = Internal::ThreadLocalStorage::Get().GetImageFactory();
419
420   Vector2 testSize(80.0f, 80.0f);
421   application.GetPlatform().SetClosestImageSize(testSize);
422
423   RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
424   ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
425
426   application.SendNotification();
427   application.Render();
428
429   DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
430   application.GetPlatform().ResetTrace();
431
432   ResourceTicketPtr ticket2 = imageFactory.Reload( req.Get() );
433
434   application.SendNotification();
435   application.Render();
436   application.SendNotification();
437   application.Render();
438
439   DALI_TEST_EQUALS( ticket, ticket2, TEST_LOCATION );
440   // resource is still loading, do not issue another request
441   DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
442
443   // emulate load success
444   EmulateImageLoaded( application, 80, 80 );
445
446   ResourceTicketPtr ticket3 = imageFactory.Reload( req.Get() );
447
448   application.SendNotification();
449   application.Render();
450   application.SendNotification();
451   application.Render();
452
453   // size didn't change, using same ticket
454   DALI_TEST_EQUALS( ticket, ticket3, TEST_LOCATION );
455   DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
456   application.GetPlatform().ResetTrace();
457
458   // still loading
459   ticket3 = imageFactory.Reload( req.Get() );
460   application.SendNotification();
461   application.Render();
462   application.SendNotification();
463   application.Render();
464
465   DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
466   END_TEST;
467 }
468
469 // Testing OnDemand + Reload
470 // Reload should have no effect if OnDemand Image is not loaded yet, as stated in the API documentation
471 int UtcDaliImageFactoryReload05(void)
472 {
473   TestApplication application;
474
475   tet_infoline( "UtcDaliImageFactoryReload05 - Reload OnDemand image" );
476
477   ImageFactory& imageFactory  = Internal::ThreadLocalStorage::Get().GetImageFactory();
478
479   Vector2 testSize(80.0f, 80.0f);
480   application.GetPlatform().SetClosestImageSize(testSize);
481
482   RequestPtr req;
483   ImageAttributes attr = ImageAttributes::New();
484   attr.SetSize( 80, 80 );
485
486   // this happens first when loading Image OnDemand
487   req = imageFactory.RegisterRequest( gTestImageFilename, &attr );
488
489   application.SendNotification();
490   application.Render();
491
492   ResourceTicketPtr ticket = imageFactory.Reload( req.Get() );
493
494   DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
495   DALI_TEST_CHECK( !ticket );
496
497   // this happens when Image is put on stage
498   ticket = imageFactory.Load( req.Get() );
499
500   application.SendNotification();
501   application.Render();
502
503   DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
504   DALI_TEST_CHECK( ticket );
505   application.GetPlatform().ResetTrace();
506
507   ticket = imageFactory.Reload( req.Get() );
508
509   application.SendNotification();
510   application.Render();
511   application.SendNotification();
512   application.Render();
513
514   // still loading, no new request
515   DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
516
517   // emulate load success
518   EmulateImageLoaded( application, 80, 80 );
519
520   ticket = imageFactory.Reload( req.Get() );
521
522   application.SendNotification();
523   application.Render();
524
525   application.SendNotification();
526   application.Render();
527
528
529   DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
530   END_TEST;
531 }
532
533 // Initally two different requests map to same resource.
534 // After overwriting the file, they load different image resources.
535 int UtcDaliImageFactoryReload06(void)
536 {
537   TestApplication application;
538   tet_infoline( "UtcDaliImageFactoryReload06 - Two requests first mapping to same resource, then different resources." );
539
540   ImageFactory& imageFactory  = Internal::ThreadLocalStorage::Get().GetImageFactory();
541
542   Vector2 testSize(80.0f, 80.0f);
543   application.GetPlatform().SetClosestImageSize(testSize);
544
545   // request with default attributes ( size is 0,0 )
546   RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
547   ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
548
549   application.SendNotification();
550   application.Render();
551   application.SendNotification();
552   application.Render();
553
554   // emulate load success
555   EmulateImageLoaded( application, 80, 80 );
556
557   // Request bigger size than actual image.
558   // This will load the same resource.
559   // However if image size changes later on to eg. 512*512 (file is overwritten),
560   // reissuing these two requests will load different resources.
561   ImageAttributes attr = ImageAttributes::New();
562   attr.SetSize( 92, 92 );
563   RequestPtr req2 = imageFactory.RegisterRequest( gTestImageFilename, &attr );
564   ResourceTicketPtr ticket2 = imageFactory.Load( req2.Get() );
565
566   DALI_TEST_CHECK( req != req2 ); // different requests
567   DALI_TEST_EQUALS( ticket->GetId(), ticket2->GetId(), TEST_LOCATION ); // same resource
568
569   Vector2 newSize(512.0f, 512.0f);
570   application.GetPlatform().SetClosestImageSize(newSize);
571
572   // reload fixed size (192,192) request
573   ticket2 = imageFactory.Reload( req2.Get() );
574
575   // emulate load success
576   // note: this is the only way to emulate what size is loaded by platform abstraction
577   EmulateImageLoaded( application, 92, 92 );
578
579   // reload default size request
580   ticket = imageFactory.Reload( req.Get() );
581
582   DALI_TEST_CHECK( ticket->GetId() != ticket2->GetId() ); // different resources
583   END_TEST;
584 }