Bitmap core patch 2 of 4 - Replace all uses of the Bitmap class with new simpler...
[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::ImageDataPtr bitmap = Integration::NewBitmapImageData( width, height, Pixel::RGBA8888 );
45
46   Integration::ResourcePointer resource( bitmap );
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 ///@todo Modify the TestPlatformAbstraction to not keep a reference to the previous ImageData loaded around after it has lost its buffer to a bitmap and expand it to allow this detailed reload testing.
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
379   END_TEST;
380 }
381
382 // Test for reloading changed image
383 int UtcDaliImageFactoryReload03(void)
384 {
385   TestApplication application;
386   tet_infoline( "UtcDaliImageFactoryReload03 - Reload changed image" );
387
388   ImageFactory& imageFactory  = Internal::ThreadLocalStorage::Get().GetImageFactory();
389
390   Vector2 testSize( 80.0f, 80.0f );
391   application.GetPlatform().SetClosestImageSize( testSize );
392
393   RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
394   ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
395
396   application.SendNotification();
397   application.Render();
398
399   // emulate load success
400   EmulateImageLoaded( application, 80, 80 );
401
402   Vector2 newSize( 192.0f, 192.0f );
403   application.GetPlatform().SetClosestImageSize( newSize );
404
405   // Image file changed size, new resource request should be issued
406   ResourceTicketPtr ticket2 = imageFactory.Reload( req.Get() );
407   DALI_TEST_CHECK( ticket != ticket2 );
408
409   ResourceTicketPtr ticket3 = imageFactory.Reload( req.Get() );
410   DALI_TEST_EQUALS( ticket2, ticket3, TEST_LOCATION );
411   END_TEST;
412 }
413
414 // Testing file system access when reloading image
415 int UtcDaliImageFactoryReload04(void)
416 {
417   TestApplication application;
418   tet_infoline( "UtcDaliImageFactoryReload04 - Reload unchanged image" );
419
420   ImageFactory& imageFactory  = Internal::ThreadLocalStorage::Get().GetImageFactory();
421
422   Vector2 testSize(80.0f, 80.0f);
423   application.GetPlatform().SetClosestImageSize(testSize);
424
425   RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
426   ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
427
428   application.SendNotification();
429   application.Render();
430
431   DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
432   application.GetPlatform().ResetTrace();
433
434   ResourceTicketPtr ticket2 = imageFactory.Reload( req.Get() );
435
436   application.SendNotification();
437   application.Render();
438   application.SendNotification();
439   application.Render();
440
441   DALI_TEST_EQUALS( ticket, ticket2, TEST_LOCATION );
442   // resource is still loading, do not issue another request
443   DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
444
445   // emulate load success
446   EmulateImageLoaded( application, 80, 80 );
447
448 ///@todo Modify the TestPlatformAbstraction to not keep a reference to the previous ImageData loaded around after it has lost its buffer to a bitmap and expand it to allow this detailed reload testing.
449 ///  ResourceTicketPtr ticket3 = imageFactory.Reload( req.Get() );
450 ///
451 ///  application.SendNotification();
452 ///  application.Render();
453 ///  application.SendNotification();
454 ///  application.Render();
455 ///
456 ///  // size didn't change, using same ticket
457 ///  DALI_TEST_EQUALS( ticket, ticket3, TEST_LOCATION );
458 ///  DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
459 ///  application.GetPlatform().ResetTrace();
460 ///
461 ///  // still loading
462 ///  ticket3 = imageFactory.Reload( req.Get() );
463 ///  application.SendNotification();
464 ///  application.Render();
465 ///  application.SendNotification();
466 ///  application.Render();
467 ///
468 ///  DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
469   END_TEST;
470 }
471
472 // Testing OnDemand + Reload
473 // Reload should have no effect if OnDemand Image is not loaded yet, as stated in the API documentation
474 int UtcDaliImageFactoryReload05(void)
475 {
476   TestApplication application;
477
478   tet_infoline( "UtcDaliImageFactoryReload05 - Reload OnDemand image" );
479
480   ImageFactory& imageFactory  = Internal::ThreadLocalStorage::Get().GetImageFactory();
481
482   Vector2 testSize(80.0f, 80.0f);
483   application.GetPlatform().SetClosestImageSize(testSize);
484
485   RequestPtr req;
486   ImageAttributes attr = ImageAttributes::New();
487   attr.SetSize( 80, 80 );
488
489   // this happens first when loading Image OnDemand
490   req = imageFactory.RegisterRequest( gTestImageFilename, &attr );
491
492   application.SendNotification();
493   application.Render();
494
495   ResourceTicketPtr ticket = imageFactory.Reload( req.Get() );
496
497   DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
498   DALI_TEST_CHECK( !ticket );
499
500   // this happens when Image is put on stage
501   ticket = imageFactory.Load( req.Get() );
502
503   application.SendNotification();
504   application.Render();
505
506   DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
507   DALI_TEST_CHECK( ticket );
508   application.GetPlatform().ResetTrace();
509
510   ticket = imageFactory.Reload( req.Get() );
511
512   application.SendNotification();
513   application.Render();
514   application.SendNotification();
515   application.Render();
516
517   // still loading, no new request
518   DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
519   
520   // emulate load success
521   EmulateImageLoaded( application, 80, 80 );
522
523   ticket = imageFactory.Reload( req.Get() );
524
525   // emulate load success
526   EmulateImageLoaded( application, 80, 80 );
527
528   application.SendNotification();
529   application.Render();
530
531   application.SendNotification();
532   application.Render();
533
534
535   DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
536   END_TEST;
537 }
538
539 // Initally two different requests map to same resource.
540 // After overwriting the file, they load different image resources.
541 int UtcDaliImageFactoryReload06(void)
542 {
543   TestApplication application;
544   tet_infoline( "UtcDaliImageFactoryReload06 - Two requests first mapping to same resource, then different resources." );
545
546   ImageFactory& imageFactory  = Internal::ThreadLocalStorage::Get().GetImageFactory();
547
548   Vector2 testSize(80.0f, 80.0f);
549   application.GetPlatform().SetClosestImageSize(testSize);
550
551   // request with default attributes ( size is 0,0 )
552   RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
553   ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
554
555   application.SendNotification();
556   application.Render();
557   application.SendNotification();
558   application.Render();
559
560   // emulate load success
561   EmulateImageLoaded( application, 80, 80 );
562
563   // Request bigger size than actual image.
564   // This will load the same resource.
565   // However if image size changes later on to eg. 512*512 (file is overwritten),
566   // reissuing these two requests will load different resources.
567   ImageAttributes attr = ImageAttributes::New();
568   attr.SetSize( 92, 92 );
569   RequestPtr req2 = imageFactory.RegisterRequest( gTestImageFilename, &attr );
570   ResourceTicketPtr ticket2 = imageFactory.Load( req2.Get() );
571
572   DALI_TEST_CHECK( req != req2 ); // different requests
573   DALI_TEST_EQUALS( ticket->GetId(), ticket2->GetId(), TEST_LOCATION ); // same resource
574
575   Vector2 newSize(512.0f, 512.0f);
576   application.GetPlatform().SetClosestImageSize(newSize);
577
578   // reload fixed size (192,192) request
579   ticket2 = imageFactory.Reload( req2.Get() );
580
581   // emulate load success
582   // note: this is the only way to emulate what size is loaded by platform abstraction
583   EmulateImageLoaded( application, 92, 92 );
584
585   // reload default size request
586   ticket = imageFactory.Reload( req.Get() );
587
588   DALI_TEST_CHECK( ticket->GetId() != ticket2->GetId() ); // different resources
589   END_TEST;
590 }