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::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 );
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 ResourceTicketPtr ticket2 = imageFactory.Reload( req.Get() );
346 application.SendNotification();
347 application.Render();
348 application.SendNotification();
349 application.Render();
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 ) );
355 // emulate load success
356 EmulateImageLoaded( application, 80, 80 );
358 ResourceTicketPtr ticket3 = imageFactory.Reload( req.Get() );
360 application.SendNotification();
361 application.Render();
362 application.SendNotification();
363 application.Render();
365 DALI_TEST_EQUALS( ticket, ticket3, TEST_LOCATION );
366 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
367 application.GetPlatform().ResetTrace();
369 ticket3 = imageFactory.Reload( req.Get() );
371 application.SendNotification();
372 application.Render();
373 application.SendNotification();
374 application.Render();
376 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
380 // Test for reloading changed image
381 int UtcDaliImageFactoryReload03(void)
383 TestApplication application;
384 tet_infoline( "UtcDaliImageFactoryReload03 - Reload changed image" );
386 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
388 Vector2 testSize( 80.0f, 80.0f );
389 application.GetPlatform().SetClosestImageSize( testSize );
391 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
392 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
394 application.SendNotification();
395 application.Render();
397 // emulate load success
398 EmulateImageLoaded( application, 80, 80 );
400 Vector2 newSize( 192.0f, 192.0f );
401 application.GetPlatform().SetClosestImageSize( newSize );
403 // Image file changed size, new resource request should be issued
404 ResourceTicketPtr ticket2 = imageFactory.Reload( req.Get() );
405 DALI_TEST_CHECK( ticket != ticket2 );
407 ResourceTicketPtr ticket3 = imageFactory.Reload( req.Get() );
408 DALI_TEST_EQUALS( ticket2, ticket3, TEST_LOCATION );
412 // Testing file system access when reloading image
413 int UtcDaliImageFactoryReload04(void)
415 TestApplication application;
416 tet_infoline( "UtcDaliImageFactoryReload04 - Reload unchanged image" );
418 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
420 Vector2 testSize(80.0f, 80.0f);
421 application.GetPlatform().SetClosestImageSize(testSize);
423 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
424 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
426 application.SendNotification();
427 application.Render();
429 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
430 application.GetPlatform().ResetTrace();
432 ResourceTicketPtr ticket2 = imageFactory.Reload( req.Get() );
434 application.SendNotification();
435 application.Render();
436 application.SendNotification();
437 application.Render();
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 ) );
443 // emulate load success
444 EmulateImageLoaded( application, 80, 80 );
446 ResourceTicketPtr ticket3 = imageFactory.Reload( req.Get() );
448 application.SendNotification();
449 application.Render();
450 application.SendNotification();
451 application.Render();
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();
459 ticket3 = imageFactory.Reload( req.Get() );
460 application.SendNotification();
461 application.Render();
462 application.SendNotification();
463 application.Render();
465 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
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)
473 TestApplication application;
475 tet_infoline( "UtcDaliImageFactoryReload05 - Reload OnDemand image" );
477 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
479 Vector2 testSize(80.0f, 80.0f);
480 application.GetPlatform().SetClosestImageSize(testSize);
483 ImageAttributes attr = ImageAttributes::New();
484 attr.SetSize( 80, 80 );
486 // this happens first when loading Image OnDemand
487 req = imageFactory.RegisterRequest( gTestImageFilename, &attr );
489 application.SendNotification();
490 application.Render();
492 ResourceTicketPtr ticket = imageFactory.Reload( req.Get() );
494 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
495 DALI_TEST_CHECK( !ticket );
497 // this happens when Image is put on stage
498 ticket = imageFactory.Load( req.Get() );
500 application.SendNotification();
501 application.Render();
503 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
504 DALI_TEST_CHECK( ticket );
505 application.GetPlatform().ResetTrace();
507 ticket = imageFactory.Reload( req.Get() );
509 application.SendNotification();
510 application.Render();
511 application.SendNotification();
512 application.Render();
514 // still loading, no new request
515 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
517 // emulate load success
518 EmulateImageLoaded( application, 80, 80 );
520 ticket = imageFactory.Reload( req.Get() );
522 application.SendNotification();
523 application.Render();
525 application.SendNotification();
526 application.Render();
529 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
533 // Initally two different requests map to same resource.
534 // After overwriting the file, they load different image resources.
535 int UtcDaliImageFactoryReload06(void)
537 TestApplication application;
538 tet_infoline( "UtcDaliImageFactoryReload06 - Two requests first mapping to same resource, then different resources." );
540 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
542 Vector2 testSize(80.0f, 80.0f);
543 application.GetPlatform().SetClosestImageSize(testSize);
545 // request with default attributes ( size is 0,0 )
546 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
547 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
549 application.SendNotification();
550 application.Render();
551 application.SendNotification();
552 application.Render();
554 // emulate load success
555 EmulateImageLoaded( application, 80, 80 );
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() );
566 DALI_TEST_CHECK( req != req2 ); // different requests
567 DALI_TEST_EQUALS( ticket->GetId(), ticket2->GetId(), TEST_LOCATION ); // same resource
569 Vector2 newSize(512.0f, 512.0f);
570 application.GetPlatform().SetClosestImageSize(newSize);
572 // reload fixed size (192,192) request
573 ticket2 = imageFactory.Reload( req2.Get() );
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 );
579 // reload default size request
580 ticket = imageFactory.Reload( req.Get() );
582 DALI_TEST_CHECK( ticket->GetId() != ticket2->GetId() ); // different resources