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& url, ImageDimensions size, FittingMode scalingMode, SamplingMode samplingMode, bool orientationCorrection = true )");
83 // invoke default handle constructor
86 DALI_TEST_CHECK( !image );
89 image = ResourceImage::New(gTestImageFilename, ImageDimensions( 128, 256 ), FittingMode::FIT_HEIGHT );
91 DALI_TEST_CHECK( image );
96 int UtcDaliResourceImageNewWithPolicies01(void)
98 TestApplication application;
99 TestPlatformAbstraction& platform = application.GetPlatform();
101 // testing delayed loading
102 tet_infoline("UtcDaliResourceImageNewWithPolicies01 - Load image with LoadPolicy::OnDemand, ReleasePolicy::Never");
103 DALI_TEST_CHECK( !platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
104 ResourceImage image = ResourceImage::New(gTestImageFilename, ResourceImage::ON_DEMAND, Image::NEVER);
106 DALI_TEST_CHECK( image );
108 application.SendNotification();
109 application.Render(16);
111 // request file loading only when actor added to stage
112 DALI_TEST_CHECK( !platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
114 ImageActor actor = ImageActor::New(image);
116 Stage::GetCurrent().Add(actor);
118 application.SendNotification();
119 application.Render(16);
121 DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
123 // testing ReleasePolicy::Never
124 // fake loading image
125 std::vector<GLuint> ids;
127 application.GetGlAbstraction().SetNextTextureIds( ids );
128 LoadBitmapResource( platform );
130 application.Render(16);
131 application.SendNotification();
133 DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
135 // never discard texture
136 Stage::GetCurrent().Remove(actor);
137 application.Render(16);
138 application.SendNotification();
139 application.Render(16);
140 application.SendNotification();
141 DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
146 int UtcDaliResourceImageNewWithPolicies02(void)
148 TestApplication application;
149 TestPlatformAbstraction& platform = application.GetPlatform();
150 const Vector2 closestImageSize( 80, 45);
151 platform.SetClosestImageSize(closestImageSize);
153 // testing resource deletion when taken off stage
154 tet_infoline("UtcDaliResourceImageNewWithPolicies02 - Load image with LoadPolicy::OnDemand, ReleasePolicy::Unused");
156 ResourceImage image = ResourceImage::New(gTestImageFilename, ResourceImage::ON_DEMAND, Image::UNUSED);
158 DALI_TEST_CHECK( image );
160 application.SendNotification();
161 application.Render(16);
163 // request file loading only when actor added to stage
164 DALI_TEST_CHECK( !platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
166 ImageActor actor = ImageActor::New(image);
168 Stage::GetCurrent().Add(actor);
170 application.SendNotification();
171 application.Render(16);
173 DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
175 // testing ReleasePolicy::Unused
176 // fake loading image
177 std::vector<GLuint> ids;
179 application.GetGlAbstraction().SetNextTextureIds( ids );
180 LoadBitmapResource( platform );
182 application.Render(16);
183 application.SendNotification();
185 DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
187 // discard texture when actor comes off stage
188 Stage::GetCurrent().Remove(actor);
189 application.Render(16);
190 application.SendNotification();
191 application.Render(16);
192 application.SendNotification();
193 DALI_TEST_CHECK ( application.GetGlAbstraction().CheckTextureDeleted(23) );
198 int UtcDaliResourceImageNewWithPolicies03(void)
200 TestApplication application;
201 TestPlatformAbstraction& platform = application.GetPlatform();
202 const Vector2 closestImageSize( 80, 45);
203 platform.SetClosestImageSize(closestImageSize);
205 // load immediately -> resource deletion when taken off stage -> put actor back on stage -> load resource again
206 tet_infoline("UtcDaliResourceImageNewWithPolicies03 - Load image with LoadPolicy::Immediate, ReleasePolicy::Unused");
208 ResourceImage image = ResourceImage::New(gTestImageFilename, ResourceImage::IMMEDIATE, Image::UNUSED);
210 DALI_TEST_CHECK( image );
212 application.SendNotification();
213 application.Render(16);
215 // request file loading immediately
216 DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
218 ImageActor actor = ImageActor::New(image);
220 Stage::GetCurrent().Add(actor);
222 application.SendNotification();
223 application.Render(16);
225 // testing ReleasePolicy::Unused
226 // fake loading image
227 std::vector<GLuint> ids;
229 application.GetGlAbstraction().SetNextTextureIds( ids );
230 LoadBitmapResource( platform );
232 application.Render(16);
233 application.SendNotification();
235 DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
237 // discard texture when actor comes off stage
238 Stage::GetCurrent().Remove(actor);
239 application.Render(16);
240 application.SendNotification();
241 application.Render(16);
242 application.SendNotification();
243 DALI_TEST_CHECK ( application.GetGlAbstraction().CheckTextureDeleted(23) );
245 // check load request when actor added back to stage
246 application.GetPlatform().ResetTrace();
248 Stage::GetCurrent().Add(actor);
250 application.SendNotification();
251 application.Render(16);
252 application.SendNotification();
253 application.Render(16);
255 DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
260 int UtcDaliResourceImageNewWithPolicies04(void)
262 TestApplication application;
263 TestPlatformAbstraction& platform = application.GetPlatform();
265 // load immediately, don't release texture when off stage
266 tet_infoline("UtcDaliResourceImageNewWithPolicies03 - Load image with LoadPolicy::Immediate, ReleasePolicy::Never");
268 ResourceImage image = ResourceImage::New(gTestImageFilename, ResourceImage::IMMEDIATE, Image::NEVER);
270 DALI_TEST_CHECK( image );
272 application.SendNotification();
273 application.Render(16);
275 // request file loading immediately
276 DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
278 ImageActor actor = ImageActor::New(image);
280 Stage::GetCurrent().Add(actor);
282 application.SendNotification();
283 application.Render(16);
285 // testing ReleasePolicy::Never
286 // fake loading image
287 std::vector<GLuint> ids;
289 application.GetGlAbstraction().SetNextTextureIds( ids );
290 LoadBitmapResource(platform);
292 application.Render(16);
293 application.SendNotification();
295 DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
297 // texture is not discarded
298 Stage::GetCurrent().Remove(actor);
299 application.Render(16);
300 application.SendNotification();
301 application.Render(16);
302 application.SendNotification();
303 DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
305 // no load request when actor added back to stage
306 application.GetPlatform().ResetTrace();
308 Stage::GetCurrent().Add(actor);
310 application.SendNotification();
311 application.Render(16);
312 application.SendNotification();
313 application.Render(16);
315 DALI_TEST_CHECK( !platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
320 int UtcDaliResourceImageDownCast(void)
322 TestApplication application;
323 tet_infoline("Testing Dali::ResourceImage::DownCast()");
325 ResourceImage image = ResourceImage::New(gTestImageFilename);
327 BaseHandle object(image);
329 ResourceImage image2 = ResourceImage::DownCast(object);
330 DALI_TEST_CHECK(image2);
332 ResourceImage image3 = DownCast< ResourceImage >(object);
333 DALI_TEST_CHECK(image3);
335 BaseHandle unInitializedObject;
336 ResourceImage image4 = ResourceImage::DownCast(unInitializedObject);
337 DALI_TEST_CHECK(!image4);
339 ResourceImage image5 = DownCast< ResourceImage >(unInitializedObject);
340 DALI_TEST_CHECK(!image5);
342 Image image6 = ResourceImage::New(gTestImageFilename);
343 ResourceImage image7 = ResourceImage::DownCast(image6);
344 DALI_TEST_CHECK(image7);
349 int UtcDaliResourceImageGetImageSize(void)
351 TestApplication application;
352 TestPlatformAbstraction& platform = application.GetPlatform();
354 tet_infoline("UtcDaliResourceImageGetImageSize - ResourceImage::GetImageSize()");
356 Vector2 testSize(8.0f, 16.0f);
357 platform.SetClosestImageSize(testSize);
359 const ImageDimensions size = ResourceImage::GetImageSize(gTestImageFilename);
361 DALI_TEST_CHECK( application.GetPlatform().GetTrace().FindMethod("GetClosestImageSize"));
362 DALI_TEST_EQUALS( Vector2( size.GetX(), size.GetY() ), testSize, TEST_LOCATION);
367 int UtcDaliResourceImageGetUrl(void)
369 TestApplication application;
371 tet_infoline("UtcDaliResourceImageGetFilename - ResourceImage::GetUrl()");
373 // invoke default handle constructor
376 DALI_TEST_CHECK( !image );
379 image = ResourceImage::New(gTestImageFilename);
381 DALI_TEST_EQUALS( image.GetUrl(), gTestImageFilename, TEST_LOCATION);
386 int UtcDaliResourceImageGetLoadingState01(void)
388 TestApplication application;
389 tet_infoline("UtcDaliResourceImageGetLoadingState01");
391 ResourceImage image = ResourceImage::New(gTestImageFilename);
392 DALI_TEST_CHECK(image.GetLoadingState() == ResourceLoading);
393 application.SendNotification();
394 application.Render(16);
396 // simulate load success
397 TestPlatformAbstraction& platform = application.GetPlatform();
398 LoadBitmapResource( platform );
399 application.Render(16);
400 application.SendNotification();
402 // Test state == ResourceLoadingSucceeded
403 DALI_TEST_CHECK(image.GetLoadingState() == ResourceLoadingSucceeded);
408 int UtcDaliResourceImageGetLoadingState02(void)
410 TestApplication application;
412 tet_infoline("UtcDaliResourceImageGetLoadingState02");
414 // invoke default handle constructor
417 DALI_TEST_CHECK( !image );
420 image = ResourceImage::New(gTestImageFilename);
422 // Test state == ResourceLoading
423 DALI_TEST_CHECK(image.GetLoadingState() == ResourceLoading);
424 application.SendNotification();
425 application.Render(16);
427 // simulate load failure
428 Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
431 application.GetPlatform().SetResourceLoadFailed(request->GetId(), Integration::FailureUnknown);
433 application.Render(16);
434 application.SendNotification();
436 // Test state == ResourceLoadingFailed
437 DALI_TEST_CHECK(image.GetLoadingState() == ResourceLoadingFailed);
442 int UtcDaliResourceImageGetLoadPolicy(void)
444 TestApplication application;
446 tet_infoline("UtcDaliImageGetLoadPolicy");
448 ResourceImage image = ResourceImage::New(gTestImageFilename, ResourceImage::ON_DEMAND, Image::NEVER);
450 DALI_TEST_CHECK( image );
452 DALI_TEST_CHECK( ResourceImage::ON_DEMAND == image.GetLoadPolicy());
456 static bool SignalLoadFlag = false;
458 static void SignalLoadHandler(ResourceImage image)
460 tet_infoline("Received image load finished signal");
462 SignalLoadFlag = true;
466 int UtcDaliResourceImageSignalLoadingFinished(void)
468 TestApplication application;
470 tet_infoline("UtcDaliResourceImageSignalLoadingFinished");
472 SignalLoadFlag = false;
474 ResourceImage image = ResourceImage::New(gTestImageFilename);
476 image.LoadingFinishedSignal().Connect( SignalLoadHandler );
477 application.SendNotification();
478 application.Render(16);
480 Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
483 application.GetPlatform().SetResourceLoaded(request->GetId(), request->GetType()->id, Integration::ResourcePointer(Integration::Bitmap::New(Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD)));
486 application.Render(16);
487 application.SendNotification();
489 DALI_TEST_CHECK( SignalLoadFlag == true );