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.
20 #include <dali/dali.h>
21 #include <dali-test-suite-utils.h>
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>
30 using Internal::ResourceTicketPtr;
31 using Internal::ImageFactory;
32 using Internal::ImageFactoryCache::RequestPtr;
38 static const char* gTestImageFilename = "icon_wrt.png";
40 static void EmulateImageLoaded( TestApplication& application, unsigned int width, unsigned int height )
42 // emulate load success
43 Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
44 Integration::ImageDataPtr bitmap = Integration::NewBitmapImageData( width, height, Pixel::RGBA8888 );
46 Integration::ResourcePointer resource( bitmap );
49 application.GetPlatform().SetResourceLoaded( request->GetId(), request->GetType()->id, resource );
52 application.SendNotification();
55 application.SendNotification();
59 } //anonymous namespace
62 // High-level test for image factory request cache
63 int UtcDaliImageFactoryUseCachedRequest01(void)
65 TestApplication application;
67 tet_infoline( "UtcDaliImageFactoryCachedRequest01 - Request same image more than once" );
69 Image image = Image::New( gTestImageFilename );
71 application.SendNotification();
73 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
74 application.GetPlatform().ResetTrace();
76 Image image2 = Image::New( gTestImageFilename );
78 application.SendNotification();
81 // check resource is not loaded twice
82 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
83 application.GetPlatform().ResetTrace();
85 Image image3 = Image::New( gTestImageFilename );
87 application.SendNotification();
89 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
93 // High-level test for image factory request cache
94 int UtcDaliImageFactoryUseCachedRequest02(void)
96 TestApplication application;
98 // testing resource deletion when taken off stage
99 tet_infoline( "UtcDaliImageFactoryCachedRequest02 - Discard previously requested resource" );
101 Image image = Image::New( gTestImageFilename, Image::Immediate, Image::Unused );
102 ImageActor actor = ImageActor::New( image );
104 application.SendNotification();
105 application.Render();
107 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
108 application.GetPlatform().ResetTrace();
110 // Add actor to stage
111 Stage::GetCurrent().Add( actor );
113 application.Render();
114 application.SendNotification();
115 application.Render();
116 application.SendNotification();
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();
125 // Should find stale request in cache, so load image from filesystem
126 Image image2 = Image::New( gTestImageFilename );
128 application.SendNotification();
129 application.Render();
131 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
132 application.GetPlatform().ResetTrace();
134 // Resource is reloaded
135 Image image3 = Image::New( gTestImageFilename );
137 application.SendNotification();
138 application.Render();
139 application.SendNotification();
140 application.Render();
142 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
146 // Low-level test for image factory request cache
147 int UtcDaliImageFactoryUseCachedRequest03(void)
149 TestApplication application;
150 tet_infoline( "UtcDaliImageFactoryCachedRequest03 - Request same image more than once - Request Ids" );
152 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
154 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
155 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
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 );
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 );
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 );
175 // Low-level test for image factory request cache
176 int UtcDaliImageFactoryUseCachedRequest04(void)
178 TestApplication application;
179 tet_infoline( "UtcDaliImageFactoryCachedRequest04 - Request same image with different Image objects - Request Ids" );
181 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
183 ImageAttributes attr = ImageAttributes::New( 80, 160, Pixel::BGR8888 );
184 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, &attr );
186 ImageAttributes attr2 = ImageAttributes::New( 80, 160, Pixel::BGR8888 );
187 RequestPtr req2 = imageFactory.RegisterRequest( gTestImageFilename, &attr2 );
188 DALI_TEST_EQUALS( req, req2, TEST_LOCATION );
192 // Different requests, compatible resource
193 int UtcDaliImageFactoryCompatibleResource01(void)
195 TestApplication application;
196 tet_infoline( "UtcDaliImageFactoryCompatibleResource01 - Two requests mapping to same resource" );
198 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
200 Vector2 testSize(80.0f, 80.0f);
201 application.GetPlatform().SetClosestImageSize(testSize);
203 // request with default attributes ( size is 0,0 )
204 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
205 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
207 application.SendNotification();
208 application.Render();
209 application.SendNotification();
210 application.Render();
212 // emulate load success
213 EmulateImageLoaded( application, 80, 80 );
215 ImageAttributes attr = ImageAttributes::New();
216 attr.SetSize( 80, 80 );
217 RequestPtr req2 = imageFactory.RegisterRequest( gTestImageFilename, &attr );
218 ResourceTicketPtr ticket2 = imageFactory.Load( req2.Get() );
220 DALI_TEST_CHECK( req != req2 ); // different requests
221 DALI_TEST_EQUALS( ticket->GetId(), ticket2->GetId(), TEST_LOCATION ); // same resource
225 // Different requests, compatible resource
226 int UtcDaliImageFactoryCompatibleResource02(void)
228 TestApplication application;
229 tet_infoline( "UtcDaliImageFactoryCompatibleResource02 - Two requests mapping to same resource." );
231 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
233 Vector2 testSize(80.0f, 80.0f);
234 application.GetPlatform().SetClosestImageSize(testSize);
236 // request with default attributes ( size is 0,0 )
237 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
238 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
240 application.SendNotification();
241 application.Render();
242 application.SendNotification();
243 application.Render();
245 // emulate load success
246 EmulateImageLoaded( application, 80, 80 );
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() );
258 DALI_TEST_CHECK( req != req2 ); // different requests
259 DALI_TEST_EQUALS( ticket->GetId(), ticket2->GetId(), TEST_LOCATION ); // same resource
263 // Different requests, compatible resource
264 int UtcDaliImageFactoryCompatibleResource03(void)
266 TestApplication application;
267 tet_infoline( "UtcDaliImageFactoryCompatibleResource03 - Two requests mapping to same resource" );
269 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
271 Vector2 testSize(80.0f, 80.0f);
272 application.GetPlatform().SetClosestImageSize(testSize);
274 // this time use defined attributes, nut just NULL
275 ImageAttributes attr = ImageAttributes::New();
276 attr.SetSize( 120, 120 );
278 // request with default attributes ( size is 0,0 )
279 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, &attr );
280 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
282 application.SendNotification();
283 application.Render();
284 application.SendNotification();
285 application.Render();
287 // emulate load success
288 EmulateImageLoaded( application, 80, 80 );
290 ImageAttributes attr2 = ImageAttributes::New();
291 attr2.SetSize( 80, 80 );
292 RequestPtr req2 = imageFactory.RegisterRequest( gTestImageFilename, &attr2 );
293 ResourceTicketPtr ticket2 = imageFactory.Load( req2.Get() );
295 DALI_TEST_CHECK( req != req2 ); // different requests
296 DALI_TEST_EQUALS( ticket->GetId(), ticket2->GetId(), TEST_LOCATION ); // same resource
300 // Test for reloading image
301 int UtcDaliImageFactoryReload01(void)
303 TestApplication application;
304 tet_infoline( "UtcDaliImageFactoryReload01 - Reload unchanged image" );
306 Vector2 testSize(80.0f, 80.0f);
307 application.GetPlatform().SetClosestImageSize(testSize);
309 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
311 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
312 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
314 ResourceTicketPtr ticket2 = imageFactory.Reload( req.Get() );
315 DALI_TEST_EQUALS( ticket, ticket2, TEST_LOCATION );
317 ResourceTicketPtr ticket3 = imageFactory.Reload( req.Get() );
318 DALI_TEST_EQUALS( ticket, ticket3, TEST_LOCATION );
322 // Testing file system access when reloading image
323 int UtcDaliImageFactoryReload02(void)
325 TestApplication application;
326 tet_infoline( "UtcDaliImageFactoryReload02 - Reload unchanged image" );
328 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
330 Vector2 testSize(80.0f, 80.0f);
331 application.GetPlatform().SetClosestImageSize(testSize);
333 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
334 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
336 application.SendNotification();
337 application.Render();
338 application.SendNotification();
339 application.Render();
341 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
342 application.GetPlatform().ResetTrace();
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() );
347 /// application.SendNotification();
348 /// application.Render();
349 /// application.SendNotification();
350 /// application.Render();
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 ) );
356 /// // emulate load success
357 /// EmulateImageLoaded( application, 80, 80 );
359 /// ResourceTicketPtr ticket3 = imageFactory.Reload( req.Get() );
361 /// application.SendNotification();
362 /// application.Render();
363 /// application.SendNotification();
364 /// application.Render();
366 /// DALI_TEST_EQUALS( ticket, ticket3, TEST_LOCATION );
367 /// DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
368 /// application.GetPlatform().ResetTrace();
370 /// ticket3 = imageFactory.Reload( req.Get() );
372 /// application.SendNotification();
373 /// application.Render();
374 /// application.SendNotification();
375 /// application.Render();
377 /// DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
382 // Test for reloading changed image
383 int UtcDaliImageFactoryReload03(void)
385 TestApplication application;
386 tet_infoline( "UtcDaliImageFactoryReload03 - Reload changed image" );
388 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
390 Vector2 testSize( 80.0f, 80.0f );
391 application.GetPlatform().SetClosestImageSize( testSize );
393 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
394 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
396 application.SendNotification();
397 application.Render();
399 // emulate load success
400 EmulateImageLoaded( application, 80, 80 );
402 Vector2 newSize( 192.0f, 192.0f );
403 application.GetPlatform().SetClosestImageSize( newSize );
405 // Image file changed size, new resource request should be issued
406 ResourceTicketPtr ticket2 = imageFactory.Reload( req.Get() );
407 DALI_TEST_CHECK( ticket != ticket2 );
409 ResourceTicketPtr ticket3 = imageFactory.Reload( req.Get() );
410 DALI_TEST_EQUALS( ticket2, ticket3, TEST_LOCATION );
414 // Testing file system access when reloading image
415 int UtcDaliImageFactoryReload04(void)
417 TestApplication application;
418 tet_infoline( "UtcDaliImageFactoryReload04 - Reload unchanged image" );
420 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
422 Vector2 testSize(80.0f, 80.0f);
423 application.GetPlatform().SetClosestImageSize(testSize);
425 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
426 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
428 application.SendNotification();
429 application.Render();
431 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
432 application.GetPlatform().ResetTrace();
434 ResourceTicketPtr ticket2 = imageFactory.Reload( req.Get() );
436 application.SendNotification();
437 application.Render();
438 application.SendNotification();
439 application.Render();
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 ) );
445 // emulate load success
446 EmulateImageLoaded( application, 80, 80 );
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() );
451 /// application.SendNotification();
452 /// application.Render();
453 /// application.SendNotification();
454 /// application.Render();
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();
462 /// ticket3 = imageFactory.Reload( req.Get() );
463 /// application.SendNotification();
464 /// application.Render();
465 /// application.SendNotification();
466 /// application.Render();
468 /// DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
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)
476 TestApplication application;
478 tet_infoline( "UtcDaliImageFactoryReload05 - Reload OnDemand image" );
480 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
482 Vector2 testSize(80.0f, 80.0f);
483 application.GetPlatform().SetClosestImageSize(testSize);
486 ImageAttributes attr = ImageAttributes::New();
487 attr.SetSize( 80, 80 );
489 // this happens first when loading Image OnDemand
490 req = imageFactory.RegisterRequest( gTestImageFilename, &attr );
492 application.SendNotification();
493 application.Render();
495 ResourceTicketPtr ticket = imageFactory.Reload( req.Get() );
497 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
498 DALI_TEST_CHECK( !ticket );
500 // this happens when Image is put on stage
501 ticket = imageFactory.Load( req.Get() );
503 application.SendNotification();
504 application.Render();
506 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
507 DALI_TEST_CHECK( ticket );
508 application.GetPlatform().ResetTrace();
510 ticket = imageFactory.Reload( req.Get() );
512 application.SendNotification();
513 application.Render();
514 application.SendNotification();
515 application.Render();
517 // still loading, no new request
518 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
520 // emulate load success
521 EmulateImageLoaded( application, 80, 80 );
523 ticket = imageFactory.Reload( req.Get() );
525 // emulate load success
526 EmulateImageLoaded( application, 80, 80 );
528 application.SendNotification();
529 application.Render();
531 application.SendNotification();
532 application.Render();
535 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
539 // Initally two different requests map to same resource.
540 // After overwriting the file, they load different image resources.
541 int UtcDaliImageFactoryReload06(void)
543 TestApplication application;
544 tet_infoline( "UtcDaliImageFactoryReload06 - Two requests first mapping to same resource, then different resources." );
546 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
548 Vector2 testSize(80.0f, 80.0f);
549 application.GetPlatform().SetClosestImageSize(testSize);
551 // request with default attributes ( size is 0,0 )
552 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
553 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
555 application.SendNotification();
556 application.Render();
557 application.SendNotification();
558 application.Render();
560 // emulate load success
561 EmulateImageLoaded( application, 80, 80 );
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() );
572 DALI_TEST_CHECK( req != req2 ); // different requests
573 DALI_TEST_EQUALS( ticket->GetId(), ticket2->GetId(), TEST_LOCATION ); // same resource
575 Vector2 newSize(512.0f, 512.0f);
576 application.GetPlatform().SetClosestImageSize(newSize);
578 // reload fixed size (192,192) request
579 ticket2 = imageFactory.Reload( req2.Get() );
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 );
585 // reload default size request
586 ticket = imageFactory.Reload( req.Get() );
588 DALI_TEST_CHECK( ticket->GetId() != ticket2->GetId() ); // different resources