2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
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>
31 using Internal::ResourceTicketPtr;
32 using Internal::ImageFactory;
33 using Internal::ImageFactoryCache::RequestPtr;
39 static const char* gTestImageFilename = "icon_wrt.png";
41 static void EmulateImageLoaded( TestApplication& application, unsigned int width, unsigned int height )
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 );
50 application.GetPlatform().SetResourceLoaded( request->GetId(), request->GetType()->id, resource );
53 application.SendNotification();
56 application.SendNotification();
60 } //anonymous namespace
63 // High-level test for image factory request cache
64 int UtcDaliImageFactoryUseCachedRequest01(void)
66 TestApplication application;
68 tet_infoline( "UtcDaliImageFactoryCachedRequest01 - Request same image more than once" );
70 Image image = Image::New( gTestImageFilename );
72 application.SendNotification();
74 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
75 application.GetPlatform().ResetTrace();
77 Image image2 = Image::New( gTestImageFilename );
79 application.SendNotification();
82 // check resource is not loaded twice
83 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
84 application.GetPlatform().ResetTrace();
86 Image image3 = Image::New( gTestImageFilename );
88 application.SendNotification();
90 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
94 // High-level test for image factory request cache
95 int UtcDaliImageFactoryUseCachedRequest02(void)
97 TestApplication application;
99 // testing resource deletion when taken off stage
100 tet_infoline( "UtcDaliImageFactoryCachedRequest02 - Discard previously requested resource" );
102 Image image = Image::New( gTestImageFilename, Image::Immediate, Image::Unused );
103 ImageActor actor = ImageActor::New( image );
105 application.SendNotification();
106 application.Render();
108 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
109 application.GetPlatform().ResetTrace();
111 // Add actor to stage
112 Stage::GetCurrent().Add( actor );
114 application.Render();
115 application.SendNotification();
116 application.Render();
117 application.SendNotification();
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();
126 // Should find stale request in cache, so load image from filesystem
127 Image image2 = Image::New( gTestImageFilename );
129 application.SendNotification();
130 application.Render();
132 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
133 application.GetPlatform().ResetTrace();
135 // Resource is reloaded
136 Image image3 = Image::New( gTestImageFilename );
138 application.SendNotification();
139 application.Render();
140 application.SendNotification();
141 application.Render();
143 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
147 // Low-level test for image factory request cache
148 int UtcDaliImageFactoryUseCachedRequest03(void)
150 TestApplication application;
151 tet_infoline( "UtcDaliImageFactoryCachedRequest03 - Request same image more than once - Request Ids" );
153 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
155 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
156 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
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 );
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 );
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 );
176 // Low-level test for image factory request cache
177 int UtcDaliImageFactoryUseCachedRequest04(void)
179 TestApplication application;
180 tet_infoline( "UtcDaliImageFactoryCachedRequest04 - Request same image with different Image objects - Request Ids" );
182 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
184 ImageAttributes attr = ImageAttributes::New( 80, 160, Pixel::BGR8888 );
185 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, &attr );
187 ImageAttributes attr2 = ImageAttributes::New( 80, 160, Pixel::BGR8888 );
188 RequestPtr req2 = imageFactory.RegisterRequest( gTestImageFilename, &attr2 );
189 DALI_TEST_EQUALS( req, req2, TEST_LOCATION );
193 // Different requests, compatible resource
194 int UtcDaliImageFactoryCompatibleResource01(void)
196 TestApplication application;
197 tet_infoline( "UtcDaliImageFactoryCompatibleResource01 - Two requests mapping to same resource" );
199 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
201 Vector2 testSize(80.0f, 80.0f);
202 application.GetPlatform().SetClosestImageSize(testSize);
204 // request with default attributes ( size is 0,0 )
205 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
206 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
208 application.SendNotification();
209 application.Render();
210 application.SendNotification();
211 application.Render();
213 // emulate load success
214 EmulateImageLoaded( application, 80, 80 );
216 ImageAttributes attr = ImageAttributes::New();
217 attr.SetSize( 80, 80 );
218 RequestPtr req2 = imageFactory.RegisterRequest( gTestImageFilename, &attr );
219 ResourceTicketPtr ticket2 = imageFactory.Load( req2.Get() );
221 DALI_TEST_CHECK( req != req2 ); // different requests
222 DALI_TEST_EQUALS( ticket->GetId(), ticket2->GetId(), TEST_LOCATION ); // same resource
226 // Different requests, compatible resource
227 int UtcDaliImageFactoryCompatibleResource02(void)
229 TestApplication application;
230 tet_infoline( "UtcDaliImageFactoryCompatibleResource02 - Two requests mapping to same resource." );
232 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
234 Vector2 testSize(80.0f, 80.0f);
235 application.GetPlatform().SetClosestImageSize(testSize);
237 // request with default attributes ( size is 0,0 )
238 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
239 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
241 application.SendNotification();
242 application.Render();
243 application.SendNotification();
244 application.Render();
246 // emulate load success
247 EmulateImageLoaded( application, 80, 80 );
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() );
259 DALI_TEST_CHECK( req != req2 ); // different requests
260 DALI_TEST_EQUALS( ticket->GetId(), ticket2->GetId(), TEST_LOCATION ); // same resource
264 // Different requests, compatible resource
265 int UtcDaliImageFactoryCompatibleResource03(void)
267 TestApplication application;
268 tet_infoline( "UtcDaliImageFactoryCompatibleResource03 - Two requests mapping to same resource" );
270 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
272 Vector2 testSize(80.0f, 80.0f);
273 application.GetPlatform().SetClosestImageSize(testSize);
275 // this time use defined attributes, nut just NULL
276 ImageAttributes attr = ImageAttributes::New();
277 attr.SetSize( 120, 120 );
279 // request with default attributes ( size is 0,0 )
280 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, &attr );
281 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
283 application.SendNotification();
284 application.Render();
285 application.SendNotification();
286 application.Render();
288 // emulate load success
289 EmulateImageLoaded( application, 80, 80 );
291 ImageAttributes attr2 = ImageAttributes::New();
292 attr2.SetSize( 80, 80 );
293 RequestPtr req2 = imageFactory.RegisterRequest( gTestImageFilename, &attr2 );
294 ResourceTicketPtr ticket2 = imageFactory.Load( req2.Get() );
296 DALI_TEST_CHECK( req != req2 ); // different requests
297 DALI_TEST_EQUALS( ticket->GetId(), ticket2->GetId(), TEST_LOCATION ); // same resource
301 // Test for reloading image
302 int UtcDaliImageFactoryReload01(void)
304 TestApplication application;
305 tet_infoline( "UtcDaliImageFactoryReload01 - Reload unchanged image" );
307 Vector2 testSize(80.0f, 80.0f);
308 application.GetPlatform().SetClosestImageSize(testSize);
310 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
312 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
313 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
315 ResourceTicketPtr ticket2 = imageFactory.Reload( req.Get() );
316 DALI_TEST_EQUALS( ticket, ticket2, TEST_LOCATION );
318 ResourceTicketPtr ticket3 = imageFactory.Reload( req.Get() );
319 DALI_TEST_EQUALS( ticket, ticket3, TEST_LOCATION );
323 // Testing file system access when reloading image
324 int UtcDaliImageFactoryReload02(void)
326 TestApplication application;
327 tet_infoline( "UtcDaliImageFactoryReload02 - Reload unchanged image" );
329 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
331 Vector2 testSize(80.0f, 80.0f);
332 application.GetPlatform().SetClosestImageSize(testSize);
334 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
335 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
337 application.SendNotification();
338 application.Render();
339 application.SendNotification();
340 application.Render();
342 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
343 application.GetPlatform().ResetTrace();
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 ) );
381 // Test for reloading changed image
382 int UtcDaliImageFactoryReload03(void)
384 TestApplication application;
385 tet_infoline( "UtcDaliImageFactoryReload03 - Reload changed image" );
387 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
389 Vector2 testSize( 80.0f, 80.0f );
390 application.GetPlatform().SetClosestImageSize( testSize );
392 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
393 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
395 application.SendNotification();
396 application.Render();
398 // emulate load success
399 EmulateImageLoaded( application, 80, 80 );
401 Vector2 newSize( 192.0f, 192.0f );
402 application.GetPlatform().SetClosestImageSize( newSize );
404 // Image file changed size, new resource request should be issued
405 ResourceTicketPtr ticket2 = imageFactory.Reload( req.Get() );
406 DALI_TEST_CHECK( ticket != ticket2 );
408 ResourceTicketPtr ticket3 = imageFactory.Reload( req.Get() );
409 DALI_TEST_EQUALS( ticket2, ticket3, TEST_LOCATION );
413 // Testing file system access when reloading image
414 int UtcDaliImageFactoryReload04(void)
416 TestApplication application;
417 tet_infoline( "UtcDaliImageFactoryReload04 - Reload unchanged image" );
419 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
421 Vector2 testSize(80.0f, 80.0f);
422 application.GetPlatform().SetClosestImageSize(testSize);
424 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
425 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
427 application.SendNotification();
428 application.Render();
430 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
431 application.GetPlatform().ResetTrace();
433 ResourceTicketPtr ticket2 = imageFactory.Reload( req.Get() );
435 application.SendNotification();
436 application.Render();
437 application.SendNotification();
438 application.Render();
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 ) );
444 // emulate load success
445 EmulateImageLoaded( application, 80, 80 );
447 ResourceTicketPtr ticket3 = imageFactory.Reload( req.Get() );
449 application.SendNotification();
450 application.Render();
451 application.SendNotification();
452 application.Render();
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();
460 ticket3 = imageFactory.Reload( req.Get() );
461 application.SendNotification();
462 application.Render();
463 application.SendNotification();
464 application.Render();
466 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
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)
474 TestApplication application;
476 tet_infoline( "UtcDaliImageFactoryReload05 - Reload OnDemand image" );
478 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
480 Vector2 testSize(80.0f, 80.0f);
481 application.GetPlatform().SetClosestImageSize(testSize);
484 ImageAttributes attr = ImageAttributes::New();
485 attr.SetSize( 80, 80 );
487 // this happens first when loading Image OnDemand
488 req = imageFactory.RegisterRequest( gTestImageFilename, &attr );
490 application.SendNotification();
491 application.Render();
493 ResourceTicketPtr ticket = imageFactory.Reload( req.Get() );
495 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
496 DALI_TEST_CHECK( !ticket );
498 // this happens when Image is put on stage
499 ticket = imageFactory.Load( req.Get() );
501 application.SendNotification();
502 application.Render();
504 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
505 DALI_TEST_CHECK( ticket );
506 application.GetPlatform().ResetTrace();
508 ticket = imageFactory.Reload( req.Get() );
510 application.SendNotification();
511 application.Render();
512 application.SendNotification();
513 application.Render();
515 // still loading, no new request
516 DALI_TEST_CHECK( !application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
518 // emulate load success
519 EmulateImageLoaded( application, 80, 80 );
521 ticket = imageFactory.Reload( req.Get() );
523 application.SendNotification();
524 application.Render();
526 application.SendNotification();
527 application.Render();
530 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::LoadResourceFunc ) );
534 // Initally two different requests map to same resource.
535 // After overwriting the file, they load different image resources.
536 int UtcDaliImageFactoryReload06(void)
538 TestApplication application;
539 tet_infoline( "UtcDaliImageFactoryReload06 - Two requests first mapping to same resource, then different resources." );
541 ImageFactory& imageFactory = Internal::ThreadLocalStorage::Get().GetImageFactory();
543 Vector2 testSize(80.0f, 80.0f);
544 application.GetPlatform().SetClosestImageSize(testSize);
546 // request with default attributes ( size is 0,0 )
547 RequestPtr req = imageFactory.RegisterRequest( gTestImageFilename, NULL );
548 ResourceTicketPtr ticket = imageFactory.Load( req.Get() );
550 application.SendNotification();
551 application.Render();
552 application.SendNotification();
553 application.Render();
555 // emulate load success
556 EmulateImageLoaded( application, 80, 80 );
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() );
567 DALI_TEST_CHECK( req != req2 ); // different requests
568 DALI_TEST_EQUALS( ticket->GetId(), ticket2->GetId(), TEST_LOCATION ); // same resource
570 Vector2 newSize(512.0f, 512.0f);
571 application.GetPlatform().SetClosestImageSize(newSize);
573 // reload fixed size (192,192) request
574 ticket2 = imageFactory.Reload( req2.Get() );
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 );
580 // reload default size request
581 ticket = imageFactory.Reload( req.Get() );
583 DALI_TEST_CHECK( ticket->GetId() != ticket2->GetId() ); // different resources