[3.0] Mark Clipping API as being part of the Tizen 3 version
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-ResourceImage.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <iostream>
19 #include <algorithm>
20 #include <stdlib.h>
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23
24 using namespace Dali;
25
26 void utc_dali_resource_image_startup(void)
27 {
28   test_return_value = TET_UNDEF;
29 }
30
31 void utc_dali_resource_image_cleanup(void)
32 {
33   test_return_value = TET_PASS;
34 }
35
36 static const char* gTestImageFilename = "icon_wrt.png";
37
38 namespace
39 {
40
41 void LoadBitmapResource(TestPlatformAbstraction& platform)
42 {
43   Integration::ResourceRequest* request = platform.GetRequest();
44   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_DISCARD );
45   Integration::ResourcePointer resource(bitmap);
46   bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 80, 80, 80, 80);
47
48   if(request)
49   {
50     platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
51   }
52 }
53
54 } // namespace
55
56
57 // 1.1
58 int UtcDaliResourceImageNew01(void)
59 {
60   TestApplication application;
61
62   tet_infoline("UtcDaliResourceImageNew01 - ResourceImage::New(const std::string&)");
63
64   // invoke default handle constructor
65   ResourceImage image;
66
67   DALI_TEST_CHECK( !image );
68
69   // initialise handle
70   image = ResourceImage::New(gTestImageFilename);
71
72   DALI_TEST_CHECK( image );
73   END_TEST;
74 }
75
76 // 1.2
77 int UtcDaliResourceImageNew02(void)
78 {
79   TestApplication application;
80
81   tet_infoline("UtcDaliREsourceImageNew02 - ResourceImage New( const std::string& url, ImageDimensions size, FittingMode scalingMode, SamplingMode samplingMode, bool orientationCorrection = true )");
82
83   // invoke default handle constructor
84   ResourceImage image;
85
86   DALI_TEST_CHECK( !image );
87
88   // initialise handle
89   image = ResourceImage::New(gTestImageFilename, ImageDimensions( 128, 256 ), FittingMode::FIT_HEIGHT );
90
91   DALI_TEST_CHECK( image );
92   END_TEST;
93 }
94
95 // 1.3
96 int UtcDaliResourceImageNewWithPolicies01(void)
97 {
98   TestApplication application;
99   TestPlatformAbstraction& platform = application.GetPlatform();
100
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);
105
106   DALI_TEST_CHECK( image );
107
108   application.SendNotification();
109   application.Render(16);
110
111   // request file loading only when actor added to stage
112   DALI_TEST_CHECK( !platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
113
114   Actor actor = CreateRenderableActor(image);
115
116   Stage::GetCurrent().Add(actor);
117
118   application.SendNotification();
119   application.Render(16);
120
121   DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
122
123   // testing ReleasePolicy::Never
124   // fake loading image
125   std::vector<GLuint> ids;
126   ids.push_back( 23 );
127   application.GetGlAbstraction().SetNextTextureIds( ids );
128   LoadBitmapResource( platform );
129
130   application.Render(16);
131   application.SendNotification();
132
133   DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
134
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) );
142   END_TEST;
143 }
144
145 // 1.4
146 int UtcDaliResourceImageNewWithPolicies02(void)
147 {
148   TestApplication application;
149   TestPlatformAbstraction& platform = application.GetPlatform();
150   const Vector2 closestImageSize( 80, 45);
151   platform.SetClosestImageSize(closestImageSize);
152
153   // testing resource deletion when taken off stage
154   tet_infoline("UtcDaliResourceImageNewWithPolicies02 - Load image with LoadPolicy::OnDemand, ReleasePolicy::Unused");
155
156   ResourceImage image = ResourceImage::New(gTestImageFilename, ResourceImage::ON_DEMAND, Image::UNUSED);
157
158   DALI_TEST_CHECK( image );
159
160   application.SendNotification();
161   application.Render(16);
162
163   // request file loading only when actor added to stage
164   DALI_TEST_CHECK( !platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
165
166   Actor actor = CreateRenderableActor(image);
167
168   Stage::GetCurrent().Add(actor);
169
170   application.SendNotification();
171   application.Render(16);
172
173   DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
174
175   // testing ReleasePolicy::Unused
176   // fake loading image
177   std::vector<GLuint> ids;
178   ids.push_back( 23 );
179   application.GetGlAbstraction().SetNextTextureIds( ids );
180   LoadBitmapResource( platform );
181
182   application.Render(16);
183   application.SendNotification();
184
185   DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
186
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) );
194   END_TEST;
195 }
196
197 // 1.5
198 int UtcDaliResourceImageNewWithPolicies03(void)
199 {
200   TestApplication application;
201   TestPlatformAbstraction& platform = application.GetPlatform();
202   const Vector2 closestImageSize( 80, 45);
203   platform.SetClosestImageSize(closestImageSize);
204
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");
207
208   ResourceImage image = ResourceImage::New(gTestImageFilename, ResourceImage::IMMEDIATE, Image::UNUSED);
209
210   DALI_TEST_CHECK( image );
211
212   application.SendNotification();
213   application.Render(16);
214
215   // request file loading immediately
216   DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
217
218   Actor actor = CreateRenderableActor(image);
219
220   Stage::GetCurrent().Add(actor);
221
222   application.SendNotification();
223   application.Render(16);
224
225   // testing ReleasePolicy::Unused
226   // fake loading image
227   std::vector<GLuint> ids;
228   ids.push_back( 23 );
229   application.GetGlAbstraction().SetNextTextureIds( ids );
230   LoadBitmapResource( platform );
231
232   application.Render(16);
233   application.SendNotification();
234
235   DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
236
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) );
244
245   // check load request when actor added back to stage
246   application.GetPlatform().ResetTrace();
247
248   Stage::GetCurrent().Add(actor);
249
250   application.SendNotification();
251   application.Render(16);
252   application.SendNotification();
253   application.Render(16);
254
255   DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
256   END_TEST;
257 }
258
259 // 1.6
260 int UtcDaliResourceImageNewWithPolicies04(void)
261 {
262   TestApplication application;
263   TestPlatformAbstraction& platform = application.GetPlatform();
264
265   // load immediately, don't release texture when off stage
266   tet_infoline("UtcDaliResourceImageNewWithPolicies03 - Load image with LoadPolicy::Immediate, ReleasePolicy::Never");
267
268   ResourceImage image = ResourceImage::New(gTestImageFilename, ResourceImage::IMMEDIATE, Image::NEVER);
269
270   DALI_TEST_CHECK( image );
271
272   application.SendNotification();
273   application.Render(16);
274
275   // request file loading immediately
276   DALI_TEST_CHECK( platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
277
278   Actor actor = CreateRenderableActor(image);
279
280   Stage::GetCurrent().Add(actor);
281
282   application.SendNotification();
283   application.Render(16);
284
285   // testing ReleasePolicy::Never
286   // fake loading image
287   std::vector<GLuint> ids;
288   ids.push_back( 23 );
289   application.GetGlAbstraction().SetNextTextureIds( ids );
290   LoadBitmapResource(platform);
291
292   application.Render(16);
293   application.SendNotification();
294
295   DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
296
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) );
304
305   // no load request when actor added back to stage
306   application.GetPlatform().ResetTrace();
307
308   Stage::GetCurrent().Add(actor);
309
310   application.SendNotification();
311   application.Render(16);
312   application.SendNotification();
313   application.Render(16);
314
315   DALI_TEST_CHECK( !platform.WasCalled(TestPlatformAbstraction::LoadResourceFunc) );
316   END_TEST;
317 }
318
319 // 1.7
320 int UtcDaliResourceImageDownCast(void)
321 {
322   TestApplication application;
323   tet_infoline("Testing Dali::ResourceImage::DownCast()");
324
325   ResourceImage image = ResourceImage::New(gTestImageFilename);
326
327   BaseHandle object(image);
328
329   ResourceImage image2 = ResourceImage::DownCast(object);
330   DALI_TEST_CHECK(image2);
331
332   ResourceImage image3 = DownCast< ResourceImage >(object);
333   DALI_TEST_CHECK(image3);
334
335   BaseHandle unInitializedObject;
336   ResourceImage image4 = ResourceImage::DownCast(unInitializedObject);
337   DALI_TEST_CHECK(!image4);
338
339   ResourceImage image5 = DownCast< ResourceImage >(unInitializedObject);
340   DALI_TEST_CHECK(!image5);
341
342   Image image6 = ResourceImage::New(gTestImageFilename);
343   ResourceImage image7 = ResourceImage::DownCast(image6);
344   DALI_TEST_CHECK(image7);
345   END_TEST;
346 }
347
348 // 1.8
349 int UtcDaliResourceImageGetImageSize(void)
350 {
351   TestApplication application;
352   TestPlatformAbstraction& platform = application.GetPlatform();
353
354   tet_infoline("UtcDaliResourceImageGetImageSize - ResourceImage::GetImageSize()");
355
356   Vector2 testSize(8.0f, 16.0f);
357   platform.SetClosestImageSize(testSize);
358
359   const ImageDimensions size = ResourceImage::GetImageSize(gTestImageFilename);
360
361   DALI_TEST_CHECK( application.GetPlatform().GetTrace().FindMethod("GetClosestImageSize"));
362   DALI_TEST_EQUALS( Vector2( size.GetX(), size.GetY() ), testSize, TEST_LOCATION);
363   END_TEST;
364 }
365
366 // 1.9
367 int UtcDaliResourceImageGetUrl(void)
368 {
369   TestApplication application;
370
371   tet_infoline("UtcDaliResourceImageGetFilename - ResourceImage::GetUrl()");
372
373   // invoke default handle constructor
374   ResourceImage image;
375
376   DALI_TEST_CHECK( !image );
377
378   // initialise handle
379   image = ResourceImage::New(gTestImageFilename);
380
381   DALI_TEST_EQUALS( image.GetUrl(), gTestImageFilename, TEST_LOCATION);
382   END_TEST;
383 }
384
385 // 1.10
386 int UtcDaliResourceImageGetLoadingState01(void)
387 {
388   TestApplication application;
389   tet_infoline("UtcDaliResourceImageGetLoadingState01");
390
391   ResourceImage image = ResourceImage::New(gTestImageFilename);
392   DALI_TEST_CHECK(image.GetLoadingState() == ResourceLoading);
393   application.SendNotification();
394   application.Render(16);
395
396   // simulate load success
397   TestPlatformAbstraction& platform = application.GetPlatform();
398   LoadBitmapResource( platform );
399   application.Render(16);
400   application.SendNotification();
401
402   // Test state == ResourceLoadingSucceeded
403   DALI_TEST_CHECK(image.GetLoadingState() == ResourceLoadingSucceeded);
404   END_TEST;
405 }
406
407 // 1.11
408 int UtcDaliResourceImageGetLoadingState02(void)
409 {
410   TestApplication application;
411
412   tet_infoline("UtcDaliResourceImageGetLoadingState02");
413
414   // invoke default handle constructor
415   ResourceImage image;
416
417   DALI_TEST_CHECK( !image );
418
419   // initialise handle
420   image = ResourceImage::New(gTestImageFilename);
421
422   // Test state == ResourceLoading
423   DALI_TEST_CHECK(image.GetLoadingState() == ResourceLoading);
424   application.SendNotification();
425   application.Render(16);
426
427   // simulate load failure
428   Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
429   if(request)
430   {
431     application.GetPlatform().SetResourceLoadFailed(request->GetId(), Integration::FailureUnknown);
432   }
433   application.Render(16);
434   application.SendNotification();
435
436   // Test state == ResourceLoadingFailed
437   DALI_TEST_CHECK(image.GetLoadingState() == ResourceLoadingFailed);
438   END_TEST;
439 }
440
441 // 1.12
442 int UtcDaliResourceImageGetLoadPolicy(void)
443 {
444   TestApplication application;
445
446   tet_infoline("UtcDaliImageGetLoadPolicy");
447
448   ResourceImage image = ResourceImage::New(gTestImageFilename, ResourceImage::ON_DEMAND, Image::NEVER);
449
450   DALI_TEST_CHECK( image );
451
452   DALI_TEST_CHECK( ResourceImage::ON_DEMAND == image.GetLoadPolicy());
453   END_TEST;
454 }
455
456 static bool SignalLoadFlag = false;
457
458 static void SignalLoadHandler(ResourceImage image)
459 {
460   tet_infoline("Received image load finished signal");
461
462   SignalLoadFlag = true;
463 }
464
465 // 1.13
466 int UtcDaliResourceImageSignalLoadingFinished(void)
467 {
468   TestApplication application;
469
470   tet_infoline("UtcDaliResourceImageSignalLoadingFinished");
471
472   SignalLoadFlag = false;
473
474   ResourceImage image = ResourceImage::New(gTestImageFilename);
475
476   image.LoadingFinishedSignal().Connect( SignalLoadHandler );
477   application.SendNotification();
478   application.Render(16);
479
480   Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
481   if(request)
482   {
483     application.GetPlatform().SetResourceLoaded(request->GetId(), request->GetType()->id, Integration::ResourcePointer(Integration::Bitmap::New(Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_DISCARD)));
484   }
485
486   application.Render(16);
487   application.SendNotification();
488
489   DALI_TEST_CHECK( SignalLoadFlag == true );
490   END_TEST;
491 }