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