2 * Copyright (c) 2015 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>
26 void utc_dali_resource_image_startup(void)
28 test_return_value = TET_UNDEF;
31 void utc_dali_resource_image_cleanup(void)
33 test_return_value = TET_PASS;
36 static const char* gTestImageFilename = "icon_wrt.png";
41 void LoadBitmapResource(TestPlatformAbstraction& platform)
43 Integration::ResourceRequest* request = platform.GetRequest();
44 Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD );
45 Integration::ResourcePointer resource(bitmap);
46 bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 80, 80, 80, 80);
50 platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
58 int UtcDaliResourceImageNew01(void)
60 TestApplication application;
62 tet_infoline("UtcDaliResourceImageNew01 - ResourceImage::New(const std::string&)");
64 // invoke default handle constructor
67 DALI_TEST_CHECK( !image );
70 image = ResourceImage::New(gTestImageFilename);
72 DALI_TEST_CHECK( image );
77 int UtcDaliResourceImageNew02(void)
79 TestApplication application;
81 tet_infoline("UtcDaliREsourceImageNew02 - ResourceImage::New(const std::string&, const ImageAttributes&)");
83 // invoke default handle constructor
86 DALI_TEST_CHECK( !image );
89 Dali::ImageAttributes imageAttributes;
90 imageAttributes.SetSize(128, 256);
91 imageAttributes.SetScalingMode(Dali::ImageAttributes::FitHeight);
92 image = ResourceImage::New(gTestImageFilename, imageAttributes);
94 DALI_TEST_CHECK( image );
99 int UtcDaliResourceImageNewWithPolicies01(void)
101 TestApplication application;
102 TestPlatformAbstraction& platform = application.GetPlatform();
104 // testing delayed loading
105 tet_infoline("UtcDaliResourceImageNewWithPolicies01 - Load image with LoadPolicy::OnDemand, ReleasePolicy::Never");
106 DALI_TEST_CHECK( !platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
107 ResourceImage image = ResourceImage::New(gTestImageFilename, ResourceImage::ON_DEMAND, Image::NEVER);
109 DALI_TEST_CHECK( image );
111 application.SendNotification();
112 application.Render(16);
114 // request file loading only when actor added to stage
115 DALI_TEST_CHECK( !platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
117 ImageActor actor = ImageActor::New(image);
119 Stage::GetCurrent().Add(actor);
121 application.SendNotification();
122 application.Render(16);
124 DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
126 // testing ReleasePolicy::Never
127 // fake loading image
128 std::vector<GLuint> ids;
130 application.GetGlAbstraction().SetNextTextureIds( ids );
131 LoadBitmapResource( platform );
133 application.Render(16);
134 application.SendNotification();
136 DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
138 // never discard texture
139 Stage::GetCurrent().Remove(actor);
140 application.Render(16);
141 application.SendNotification();
142 application.Render(16);
143 application.SendNotification();
144 DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
149 int UtcDaliResourceImageNewWithPolicies02(void)
151 TestApplication application;
152 TestPlatformAbstraction& platform = application.GetPlatform();
153 const Vector2 closestImageSize( 80, 45);
154 platform.SetClosestImageSize(closestImageSize);
156 // testing resource deletion when taken off stage
157 tet_infoline("UtcDaliResourceImageNewWithPolicies02 - Load image with LoadPolicy::OnDemand, ReleasePolicy::Unused");
159 ResourceImage image = ResourceImage::New(gTestImageFilename, ResourceImage::ON_DEMAND, Image::UNUSED);
161 DALI_TEST_CHECK( image );
163 application.SendNotification();
164 application.Render(16);
166 // request file loading only when actor added to stage
167 DALI_TEST_CHECK( !platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
169 ImageActor actor = ImageActor::New(image);
171 Stage::GetCurrent().Add(actor);
173 application.SendNotification();
174 application.Render(16);
176 DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
178 // testing ReleasePolicy::Unused
179 // fake loading image
180 std::vector<GLuint> ids;
182 application.GetGlAbstraction().SetNextTextureIds( ids );
183 LoadBitmapResource( platform );
185 application.Render(16);
186 application.SendNotification();
188 DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
190 // discard texture when actor comes off stage
191 Stage::GetCurrent().Remove(actor);
192 application.Render(16);
193 application.SendNotification();
194 application.Render(16);
195 application.SendNotification();
196 DALI_TEST_CHECK ( application.GetGlAbstraction().CheckTextureDeleted(23) );
201 int UtcDaliResourceImageNewWithPolicies03(void)
203 TestApplication application;
204 TestPlatformAbstraction& platform = application.GetPlatform();
205 const Vector2 closestImageSize( 80, 45);
206 platform.SetClosestImageSize(closestImageSize);
208 // load immediately -> resource deletion when taken off stage -> put actor back on stage -> load resource again
209 tet_infoline("UtcDaliResourceImageNewWithPolicies03 - Load image with LoadPolicy::Immediate, ReleasePolicy::Unused");
211 ResourceImage image = ResourceImage::New(gTestImageFilename, ResourceImage::IMMEDIATE, Image::UNUSED);
213 DALI_TEST_CHECK( image );
215 application.SendNotification();
216 application.Render(16);
218 // request file loading immediately
219 DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
221 ImageActor actor = ImageActor::New(image);
223 Stage::GetCurrent().Add(actor);
225 application.SendNotification();
226 application.Render(16);
228 // testing ReleasePolicy::Unused
229 // fake loading image
230 std::vector<GLuint> ids;
232 application.GetGlAbstraction().SetNextTextureIds( ids );
233 LoadBitmapResource( platform );
235 application.Render(16);
236 application.SendNotification();
238 DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
240 // discard texture when actor comes off stage
241 Stage::GetCurrent().Remove(actor);
242 application.Render(16);
243 application.SendNotification();
244 application.Render(16);
245 application.SendNotification();
246 DALI_TEST_CHECK ( application.GetGlAbstraction().CheckTextureDeleted(23) );
248 // check load request when actor added back to stage
249 application.GetPlatform().ResetTrace();
251 Stage::GetCurrent().Add(actor);
253 application.SendNotification();
254 application.Render(16);
255 application.SendNotification();
256 application.Render(16);
258 DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
263 int UtcDaliResourceImageNewWithPolicies04(void)
265 TestApplication application;
266 TestPlatformAbstraction& platform = application.GetPlatform();
268 // load immediately, don't release texture when off stage
269 tet_infoline("UtcDaliResourceImageNewWithPolicies03 - Load image with LoadPolicy::Immediate, ReleasePolicy::Never");
271 ResourceImage image = ResourceImage::New(gTestImageFilename, ResourceImage::IMMEDIATE, Image::NEVER);
273 DALI_TEST_CHECK( image );
275 application.SendNotification();
276 application.Render(16);
278 // request file loading immediately
279 DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
281 ImageActor actor = ImageActor::New(image);
283 Stage::GetCurrent().Add(actor);
285 application.SendNotification();
286 application.Render(16);
288 // testing ReleasePolicy::Never
289 // fake loading image
290 std::vector<GLuint> ids;
292 application.GetGlAbstraction().SetNextTextureIds( ids );
293 LoadBitmapResource(platform);
295 application.Render(16);
296 application.SendNotification();
298 DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
300 // texture is not discarded
301 Stage::GetCurrent().Remove(actor);
302 application.Render(16);
303 application.SendNotification();
304 application.Render(16);
305 application.SendNotification();
306 DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
308 // no load request when actor added back to stage
309 application.GetPlatform().ResetTrace();
311 Stage::GetCurrent().Add(actor);
313 application.SendNotification();
314 application.Render(16);
315 application.SendNotification();
316 application.Render(16);
318 DALI_TEST_CHECK( !platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
323 int UtcDaliResourceImageDownCast(void)
325 TestApplication application;
326 tet_infoline("Testing Dali::ResourceImage::DownCast()");
328 ResourceImage image = ResourceImage::New(gTestImageFilename);
330 BaseHandle object(image);
332 ResourceImage image2 = ResourceImage::DownCast(object);
333 DALI_TEST_CHECK(image2);
335 ResourceImage image3 = DownCast< ResourceImage >(object);
336 DALI_TEST_CHECK(image3);
338 BaseHandle unInitializedObject;
339 ResourceImage image4 = ResourceImage::DownCast(unInitializedObject);
340 DALI_TEST_CHECK(!image4);
342 ResourceImage image5 = DownCast< ResourceImage >(unInitializedObject);
343 DALI_TEST_CHECK(!image5);
345 Image image6 = ResourceImage::New(gTestImageFilename);
346 ResourceImage image7 = ResourceImage::DownCast(image6);
347 DALI_TEST_CHECK(image7);
352 int UtcDaliResourceImageGetImageSize(void)
354 TestApplication application;
355 TestPlatformAbstraction& platform = application.GetPlatform();
357 tet_infoline("UtcDaliResourceImageGetImageSize - ResourceImage::GetImageSize()");
359 Vector2 testSize(8.0f, 16.0f);
360 platform.SetClosestImageSize(testSize);
362 Vector2 size = ResourceImage::GetImageSize(gTestImageFilename);
364 DALI_TEST_CHECK( application.GetPlatform().GetTrace().FindMethod("GetClosestImageSize"));
365 DALI_TEST_EQUALS( size, testSize, TEST_LOCATION);
370 int UtcDaliResourceImageGetUrl(void)
372 TestApplication application;
374 tet_infoline("UtcDaliResourceImageGetFilename - ResourceImage::GetUrl()");
376 // invoke default handle constructor
379 DALI_TEST_CHECK( !image );
382 image = ResourceImage::New(gTestImageFilename);
384 DALI_TEST_EQUALS( image.GetUrl(), gTestImageFilename, TEST_LOCATION);
389 int UtcDaliResourceImageGetLoadingState01(void)
391 TestApplication application;
392 tet_infoline("UtcDaliResourceImageGetLoadingState01");
394 ResourceImage image = ResourceImage::New(gTestImageFilename);
395 DALI_TEST_CHECK(image.GetLoadingState() == ResourceLoading);
396 application.SendNotification();
397 application.Render(16);
399 // simulate load success
400 TestPlatformAbstraction& platform = application.GetPlatform();
401 LoadBitmapResource( platform );
402 application.Render(16);
403 application.SendNotification();
405 // Test state == ResourceLoadingSucceeded
406 DALI_TEST_CHECK(image.GetLoadingState() == ResourceLoadingSucceeded);
411 int UtcDaliResourceImageGetLoadingState02(void)
413 TestApplication application;
415 tet_infoline("UtcDaliResourceImageGetLoadingState02");
417 // invoke default handle constructor
420 DALI_TEST_CHECK( !image );
423 image = ResourceImage::New(gTestImageFilename);
425 // Test state == ResourceLoading
426 DALI_TEST_CHECK(image.GetLoadingState() == ResourceLoading);
427 application.SendNotification();
428 application.Render(16);
430 // simulate load failure
431 Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
434 application.GetPlatform().SetResourceLoadFailed(request->GetId(), Integration::FailureUnknown);
436 application.Render(16);
437 application.SendNotification();
439 // Test state == ResourceLoadingFailed
440 DALI_TEST_CHECK(image.GetLoadingState() == ResourceLoadingFailed);
445 int UtcDaliResourceImageGetLoadPolicy(void)
447 TestApplication application;
449 tet_infoline("UtcDaliImageGetLoadPolicy");
451 ResourceImage image = ResourceImage::New(gTestImageFilename, ResourceImage::ON_DEMAND, Image::NEVER);
453 DALI_TEST_CHECK( image );
455 DALI_TEST_CHECK( ResourceImage::ON_DEMAND == image.GetLoadPolicy());
459 static bool SignalLoadFlag = false;
461 static void SignalLoadHandler(ResourceImage image)
463 tet_infoline("Received image load finished signal");
465 SignalLoadFlag = true;
469 int UtcDaliResourceImageSignalLoadingFinished(void)
471 TestApplication application;
473 tet_infoline("UtcDaliResourceImageSignalLoadingFinished");
475 SignalLoadFlag = false;
477 ResourceImage image = ResourceImage::New(gTestImageFilename);
479 image.LoadingFinishedSignal().Connect( SignalLoadHandler );
480 application.SendNotification();
481 application.Render(16);
483 Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
486 application.GetPlatform().SetResourceLoaded(request->GetId(), request->GetType()->id, Integration::ResourcePointer(Integration::Bitmap::New(Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD)));
489 application.Render(16);
490 application.SendNotification();
492 DALI_TEST_CHECK( SignalLoadFlag == true );