2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
22 #include <dali/public-api/dali-core.h>
24 #include <dali-test-suite-utils.h>
26 // Internal headers are allowed here
27 #include <dali/internal/event/common/thread-local-storage.h>
28 #include <dali/internal/event/images/image-factory.h>
29 #include <dali/internal/event/resources/resource-ticket.h>
33 using Internal::ResourceTicketPtr;
34 using Internal::ImageFactory;
35 using Internal::ImageFactoryCache::RequestPtr;
37 static void Startup();
38 static void Cleanup();
41 void (*tet_startup)() = Startup;
42 void (*tet_cleanup)() = Cleanup;
45 static const char* gTestImageFilename = "icon_wrt.png";
48 POSITIVE_TC_IDX = 0x01,
52 #define MAX_NUMBER_OF_TESTS 10000
54 struct tet_testlist tet_testlist[MAX_NUMBER_OF_TESTS];
57 // Add test functionality for all APIs in the class (Positive and Negative)
59 TEST_FUNCTION( UtcDaliImageFactoryUseCachedRequest01, POSITIVE_TC_IDX );
60 TEST_FUNCTION( UtcDaliImageFactoryUseCachedRequest02, POSITIVE_TC_IDX );
61 TEST_FUNCTION( UtcDaliImageFactoryUseCachedRequest03, POSITIVE_TC_IDX );
62 TEST_FUNCTION( UtcDaliImageFactoryUseCachedRequest04, POSITIVE_TC_IDX );
63 TEST_FUNCTION( UtcDaliImageFactoryCompatibleResource01, POSITIVE_TC_IDX );
64 TEST_FUNCTION( UtcDaliImageFactoryCompatibleResource02, POSITIVE_TC_IDX );
65 TEST_FUNCTION( UtcDaliImageFactoryCompatibleResource03, POSITIVE_TC_IDX );
66 TEST_FUNCTION( UtcDaliImageFactoryReload01, POSITIVE_TC_IDX );
67 TEST_FUNCTION( UtcDaliImageFactoryReload02, POSITIVE_TC_IDX );
68 TEST_FUNCTION( UtcDaliImageFactoryReload03, POSITIVE_TC_IDX );
69 TEST_FUNCTION( UtcDaliImageFactoryReload04, POSITIVE_TC_IDX );
70 TEST_FUNCTION( UtcDaliImageFactoryReload05, POSITIVE_TC_IDX );
71 TEST_FUNCTION( UtcDaliImageFactoryReload06, POSITIVE_TC_IDX );
73 // Called only once before first test is run.
78 // Called only once after last test is run
83 static void EmulateImageLoaded( TestApplication& application, unsigned int width, unsigned int height )
85 // emulate load success
86 Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
87 Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, true );
88 Integration::ResourcePointer resource( bitmap );
89 bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, width, height, width, height );
92 application.GetPlatform().SetResourceLoaded( request->GetId(), request->GetType()->id, resource );
95 application.SendNotification();
98 application.SendNotification();
102 // High-level test for image factory request cache
103 static void UtcDaliImageFactoryUseCachedRequest01()
105 TestApplication application;
107 tet_infoline( "UtcDaliImageFactoryCachedRequest01 - Request same image more than once" );
109 Image image = Image::New( gTestImageFilename );
111 application.SendNotification();
112 application.Render();
113 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
114 application.GetPlatform().ResetTrace();
116 Image image2 = Image::New( gTestImageFilename );
118 application.SendNotification();
119 application.Render();
121 // check resource is not loaded twice
122 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
123 application.GetPlatform().ResetTrace();
125 Image image3 = Image::New( gTestImageFilename );
127 application.SendNotification();
128 application.Render();
129 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
132 // High-level test for image factory request cache
133 static void UtcDaliImageFactoryUseCachedRequest02()
135 TestApplication application;
137 // testing resource deletion when taken off stage
138 tet_infoline( "UtcDaliImageFactoryCachedRequest02 - Discard previously requested resource" );
140 Image image = Image::New( gTestImageFilename, Image::Immediate, Image::Unused );
141 ImageActor actor = ImageActor::New( image );
143 application.SendNotification();
144 application.Render();
146 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
147 application.GetPlatform().ResetTrace();
149 // Add actor to stage
150 Stage::GetCurrent().Add( actor );
152 application.Render();
153 application.SendNotification();
154 application.Render();
155 application.SendNotification();
157 // Release the resource, request is still cached
158 Stage::GetCurrent().Remove( actor );
159 application.Render();
160 application.SendNotification();
161 application.Render();
162 application.SendNotification();
164 // Should find stale request in cache, so load image from filesystem
165 Image image2 = Image::New( gTestImageFilename );
167 application.SendNotification();
168 application.Render();
170 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
171 application.GetPlatform().ResetTrace();
173 // Resource is reloaded
174 Image image3 = Image::New( gTestImageFilename );
176 application.SendNotification();
177 application.Render();
178 application.SendNotification();
179 application.Render();
181 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
184 // Low-level test for image factory request cache
185 static void UtcDaliImageFactoryUseCachedRequest03()
187 TestApplication application;
188 tet_infoline( "UtcDaliImageFactoryCachedRequest03 - Request same image more than once - Request Ids" );
190 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
192 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
193 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
195 RequestPtr req2 = imageFactory.RegisterRequest( gTestImageFilename, NULL );
196 ResourceTicketPtr ticket2 = imageFactory.Load( req2.Get() );
197 DALI_TEST_EQUALS( req, req2, TEST_LOCATION );
198 DALI_TEST_EQUALS( ticket, ticket2, TEST_LOCATION );
200 req2 = imageFactory.RegisterRequest( gTestImageFilename, NULL );
201 ResourceTicketPtr ticket3 = imageFactory.Load( req2.Get() );
202 DALI_TEST_EQUALS( req, req2, TEST_LOCATION );
203 DALI_TEST_EQUALS( ticket, ticket3, TEST_LOCATION );
205 // request differs in scaled size - not default size
206 ImageAttributes attr = ImageAttributes::New( 80, 160, Pixel::BGR8888 );
207 req2 = imageFactory.RegisterRequest( gTestImageFilename, &attr );
208 ResourceTicketPtr ticket4 = imageFactory.Load( req2.Get() );
209 DALI_TEST_CHECK( req != req2 );
212 // Low-level test for image factory request cache
213 static void UtcDaliImageFactoryUseCachedRequest04()
215 TestApplication application;
216 tet_infoline( "UtcDaliImageFactoryCachedRequest04 - Request same image with different Image objects - Request Ids" );
218 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
220 ImageAttributes attr = ImageAttributes::New( 80, 160, Pixel::BGR8888 );
221 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, &attr );
223 ImageAttributes attr2 = ImageAttributes::New( 80, 160, Pixel::BGR8888 );
224 RequestPtr req2 = imageFactory.RegisterRequest( gTestImageFilename, &attr2 );
225 DALI_TEST_EQUALS( req, req2, TEST_LOCATION );
228 // Different requests, compatible resource
229 static void UtcDaliImageFactoryCompatibleResource01()
231 TestApplication application;
232 tet_infoline( "UtcDaliImageFactoryCompatibleResource01 - Two requests mapping to same resource" );
234 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
236 Vector2 testSize(80.0f, 80.0f);
237 application.GetPlatform().SetImageMetaDataSize(testSize);
239 // request with default attributes ( size is 0,0 )
240 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
241 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
243 application.SendNotification();
244 application.Render();
245 application.SendNotification();
246 application.Render();
248 // emulate load success
249 EmulateImageLoaded( application, 80, 80 );
251 ImageAttributes attr = ImageAttributes::New();
252 attr.SetSize( 80, 80 );
253 RequestPtr req2 = imageFactory.RegisterRequest( gTestImageFilename, &attr );
254 ResourceTicketPtr ticket2 = imageFactory.Load( req2.Get() );
256 DALI_TEST_CHECK( req != req2 ); // different requests
257 DALI_TEST_EQUALS( ticket->GetId(), ticket2->GetId(), TEST_LOCATION ); // same resource
260 // Different requests, compatible resource
261 static void UtcDaliImageFactoryCompatibleResource02()
263 TestApplication application;
264 tet_infoline( "UtcDaliImageFactoryCompatibleResource02 - Two requests mapping to same resource." );
266 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
268 Vector2 testSize(80.0f, 80.0f);
269 application.GetPlatform().SetImageMetaDataSize(testSize);
271 // request with default attributes ( size is 0,0 )
272 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
273 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
275 application.SendNotification();
276 application.Render();
277 application.SendNotification();
278 application.Render();
280 // emulate load success
281 EmulateImageLoaded( application, 80, 80 );
283 // Request bigger size than actual image.
284 // This will load the same resource.
285 // However if image size changes later on to eg. 512*512 (file is overwritten),
286 // reissuing these two requests will load different resources.
287 // See UtcDaliImageFactoryReload06
288 ImageAttributes attr = ImageAttributes::New();
289 attr.SetSize( 92, 92 );
290 RequestPtr req2 = imageFactory.RegisterRequest( gTestImageFilename, &attr );
291 ResourceTicketPtr ticket2 = imageFactory.Load( req2.Get() );
293 DALI_TEST_CHECK( req != req2 ); // different requests
294 DALI_TEST_EQUALS( ticket->GetId(), ticket2->GetId(), TEST_LOCATION ); // same resource
297 // Different requests, compatible resource
298 static void UtcDaliImageFactoryCompatibleResource03()
300 TestApplication application;
301 tet_infoline( "UtcDaliImageFactoryCompatibleResource03 - Two requests mapping to same resource" );
303 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
305 Vector2 testSize(80.0f, 80.0f);
306 application.GetPlatform().SetImageMetaDataSize(testSize);
308 // this time use defined attributes, nut just NULL
309 ImageAttributes attr = ImageAttributes::New();
310 attr.SetSize( 120, 120 );
312 // request with default attributes ( size is 0,0 )
313 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, &attr );
314 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
316 application.SendNotification();
317 application.Render();
318 application.SendNotification();
319 application.Render();
321 // emulate load success
322 EmulateImageLoaded( application, 80, 80 );
324 ImageAttributes attr2 = ImageAttributes::New();
325 attr2.SetSize( 80, 80 );
326 RequestPtr req2 = imageFactory.RegisterRequest( gTestImageFilename, &attr2 );
327 ResourceTicketPtr ticket2 = imageFactory.Load( req2.Get() );
329 DALI_TEST_CHECK( req != req2 ); // different requests
330 DALI_TEST_EQUALS( ticket->GetId(), ticket2->GetId(), TEST_LOCATION ); // same resource
333 // Test for reloading image
334 static void UtcDaliImageFactoryReload01()
336 TestApplication application;
337 tet_infoline( "UtcDaliImageFactoryReload01 - Reload unchanged image" );
339 Vector2 testSize(80.0f, 80.0f);
340 application.GetPlatform().SetImageMetaDataSize(testSize);
342 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
344 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
345 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
347 ResourceTicketPtr ticket2 = imageFactory.Reload( req.Get() );
348 DALI_TEST_EQUALS( ticket, ticket2, TEST_LOCATION );
350 ResourceTicketPtr ticket3 = imageFactory.Reload( req.Get() );
351 DALI_TEST_EQUALS( ticket, ticket3, TEST_LOCATION );
354 // Testing file system access when reloading image
355 static void UtcDaliImageFactoryReload02()
357 TestApplication application;
358 tet_infoline( "UtcDaliImageFactoryReload02 - Reload unchanged image" );
360 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
362 Vector2 testSize(80.0f, 80.0f);
363 application.GetPlatform().SetImageMetaDataSize(testSize);
365 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
366 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
368 application.SendNotification();
369 application.Render();
370 application.SendNotification();
371 application.Render();
373 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
374 application.GetPlatform().ResetTrace();
376 ResourceTicketPtr ticket2 = imageFactory.Reload( req.Get() );
378 application.SendNotification();
379 application.Render();
380 application.SendNotification();
381 application.Render();
383 DALI_TEST_EQUALS( ticket, ticket2, TEST_LOCATION );
384 // resource is still loading, do not issue another request
385 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
387 // emulate load success
388 EmulateImageLoaded( application, 80, 80 );
390 ResourceTicketPtr ticket3 = imageFactory.Reload( req.Get() );
392 application.SendNotification();
393 application.Render();
394 application.SendNotification();
395 application.Render();
397 DALI_TEST_EQUALS( ticket, ticket3, TEST_LOCATION );
398 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
399 application.GetPlatform().ResetTrace();
401 ticket3 = imageFactory.Reload( req.Get() );
403 application.SendNotification();
404 application.Render();
405 application.SendNotification();
406 application.Render();
408 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
411 // Test for reloading changed image
412 static void UtcDaliImageFactoryReload03()
414 TestApplication application;
415 tet_infoline( "UtcDaliImageFactoryReload03 - Reload changed image" );
417 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
419 Vector2 testSize( 80.0f, 80.0f );
420 application.GetPlatform().SetImageMetaDataSize( testSize );
422 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
423 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
425 application.SendNotification();
426 application.Render();
428 // emulate load success
429 EmulateImageLoaded( application, 80, 80 );
431 Vector2 newSize( 192.0f, 192.0f );
432 application.GetPlatform().SetImageMetaDataSize( newSize );
434 // Image file changed size, new resource request should be issued
435 ResourceTicketPtr ticket2 = imageFactory.Reload( req.Get() );
436 DALI_TEST_CHECK( ticket != ticket2 );
438 ResourceTicketPtr ticket3 = imageFactory.Reload( req.Get() );
439 DALI_TEST_EQUALS( ticket2, ticket3, TEST_LOCATION );
442 // Testing file system access when reloading image
443 static void UtcDaliImageFactoryReload04()
445 TestApplication application;
446 tet_infoline( "UtcDaliImageFactoryReload04 - Reload unchanged image" );
448 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
450 Vector2 testSize(80.0f, 80.0f);
451 application.GetPlatform().SetImageMetaDataSize(testSize);
453 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
454 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
456 application.SendNotification();
457 application.Render();
459 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
460 application.GetPlatform().ResetTrace();
462 ResourceTicketPtr ticket2 = imageFactory.Reload( req.Get() );
464 application.SendNotification();
465 application.Render();
466 application.SendNotification();
467 application.Render();
469 DALI_TEST_EQUALS( ticket, ticket2, TEST_LOCATION );
470 // resource is still loading, do not issue another request
471 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
473 // emulate load success
474 EmulateImageLoaded( application, 80, 80 );
476 ResourceTicketPtr ticket3 = imageFactory.Reload( req.Get() );
478 application.SendNotification();
479 application.Render();
480 application.SendNotification();
481 application.Render();
483 // size didn't change, using same ticket
484 DALI_TEST_EQUALS( ticket, ticket3, TEST_LOCATION );
485 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
486 application.GetPlatform().ResetTrace();
489 ticket3 = imageFactory.Reload( req.Get() );
490 application.SendNotification();
491 application.Render();
492 application.SendNotification();
493 application.Render();
495 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
498 // Testing OnDemand + Reload
499 // Reload should have no effect if OnDemand Image is not loaded yet, as stated in the API documentation
500 static void UtcDaliImageFactoryReload05()
502 TestApplication application;
504 tet_infoline( "UtcDaliImageFactoryReload05 - Reload OnDemand image" );
506 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
508 Vector2 testSize(80.0f, 80.0f);
509 application.GetPlatform().SetImageMetaDataSize(testSize);
512 ImageAttributes attr = ImageAttributes::New();
513 attr.SetSize( 80, 80 );
515 // this happens first when loading Image OnDemand
516 req = imageFactory.RegisterRequest( gTestImageFilename, &attr );
518 application.SendNotification();
519 application.Render();
521 ResourceTicketPtr ticket = imageFactory.Reload( req.Get() );
523 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
524 DALI_TEST_CHECK( !ticket );
526 // this happens when Image is put on stage
527 ticket = imageFactory.Load( req.Get() );
529 application.SendNotification();
530 application.Render();
532 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
533 DALI_TEST_CHECK( ticket );
534 application.GetPlatform().ResetTrace();
536 ticket = imageFactory.Reload( req.Get() );
538 application.SendNotification();
539 application.Render();
540 application.SendNotification();
541 application.Render();
543 // still loading, no new request
544 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
546 // emulate load success
547 EmulateImageLoaded( application, 80, 80 );
549 ticket = imageFactory.Reload( req.Get() );
551 application.SendNotification();
552 application.Render();
554 application.SendNotification();
555 application.Render();
558 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
561 // Initally two different requests map to same resource.
562 // After overwriting the file, they load different image resources.
563 static void UtcDaliImageFactoryReload06()
565 TestApplication application;
566 tet_infoline( "UtcDaliImageFactoryReload06 - Two requests first mapping to same resource, then different resources." );
568 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
570 Vector2 testSize(80.0f, 80.0f);
571 application.GetPlatform().SetImageMetaDataSize(testSize);
573 // request with default attributes ( size is 0,0 )
574 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
575 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
577 application.SendNotification();
578 application.Render();
579 application.SendNotification();
580 application.Render();
582 // emulate load success
583 EmulateImageLoaded( application, 80, 80 );
585 // Request bigger size than actual image.
586 // This will load the same resource.
587 // However if image size changes later on to eg. 512*512 (file is overwritten),
588 // reissuing these two requests will load different resources.
589 ImageAttributes attr = ImageAttributes::New();
590 attr.SetSize( 92, 92 );
591 RequestPtr req2 = imageFactory.RegisterRequest( gTestImageFilename, &attr );
592 ResourceTicketPtr ticket2 = imageFactory.Load( req2.Get() );
594 DALI_TEST_CHECK( req != req2 ); // different requests
595 DALI_TEST_EQUALS( ticket->GetId(), ticket2->GetId(), TEST_LOCATION ); // same resource
597 Vector2 newSize(512.0f, 512.0f);
598 application.GetPlatform().SetImageMetaDataSize(newSize);
600 // reload fixed size (192,192) request
601 ticket2 = imageFactory.Reload( req2.Get() );
603 // emulate load success
604 // note: this is the only way to emulate what size is loaded by platform abstraction
605 EmulateImageLoaded( application, 92, 92 );
607 // reload default size request
608 ticket = imageFactory.Reload( req.Get() );
610 DALI_TEST_CHECK( ticket->GetId() != ticket2->GetId() ); // different resources