3de6688e6b95b34378af29fcf497e3395799964a
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-ImageVisual.cpp
1 /*
2  * Copyright (c) 2017 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 #include <iostream>
18 #include <stdlib.h>
19 #include <dali-toolkit-test-suite-utils.h>
20 #include <toolkit-timer.h>
21 #include <toolkit-event-thread-callback.h>
22 #include <dali/devel-api/object/handle-devel.h>
23 #include <dali-toolkit/devel-api/visual-factory/transition-data.h>
24 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
25 #include <dali-toolkit/devel-api/controls/control-devel.h>
26 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
27 #include <dali-toolkit/dali-toolkit.h>
28 #include "dummy-control.h"
29
30 using namespace Dali;
31 using namespace Dali::Toolkit;
32
33 void dali_image_visual_startup(void)
34 {
35   test_return_value = TET_UNDEF;
36 }
37
38 void dali_image_visual_cleanup(void)
39 {
40   test_return_value = TET_PASS;
41 }
42
43 namespace
44 {
45 const char* TEST_IMAGE_FILE_NAME =  TEST_RESOURCE_DIR  "/gallery-small-1.jpg";
46 const char* TEST_LARGE_IMAGE_FILE_NAME =  TEST_RESOURCE_DIR "/tbcol.png";
47 const char* TEST_SMALL_IMAGE_FILE_NAME = TEST_RESOURCE_DIR "/icon-edit.png";
48 const char* TEST_REMOTE_IMAGE_FILE_NAME = "https://www.tizen.org/sites/all/themes/tizen_theme/logo.png";
49 const char* TEST_INVALID_FILE_NAME =  TEST_RESOURCE_DIR  "/invalid.jpg";
50 const char* TEST_REMOTE_INVALID_FILE_NAME = "https://www.tizen.org/invalid.png";
51 const char* TEST_MASK_IMAGE_FILE_NAME =  TEST_RESOURCE_DIR "/mask.png";
52 const char* TEST_ROTATED_IMAGE =  TEST_RESOURCE_DIR  "/keyboard-Landscape.jpg";
53
54
55 bool gResourceReadySignalFired = false;
56
57 void ResourceReadySignal( Control control )
58 {
59   gResourceReadySignalFired = true;
60 }
61
62
63 Actor CreateActorWithImageVisual(const Property::Map& map)
64 {
65   VisualFactory factory = VisualFactory::Get();
66   DummyControl actor = DummyControl::New();
67   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
68   Visual::Base visual = factory.CreateVisual( map );
69   DALI_TEST_CHECK( visual );
70   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
71   actor.SetSize( 200.f, 200.f );
72   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
73   return actor;
74 }
75
76
77 Visual::Base CreateVisualWithPolicy( const char* url, Property::Index key, const Property::Value& value )
78 {
79   VisualFactory factory = VisualFactory::Get();
80
81   Property::Map propertyMap;
82   propertyMap.Insert( Visual::Property::TYPE,  Visual::IMAGE );
83   propertyMap.Insert( ImageVisual::Property::URL,  url );
84   propertyMap.Insert( ImageVisual::Property::DESIRED_WIDTH,   20 );
85   propertyMap.Insert( ImageVisual::Property::DESIRED_HEIGHT,   30 );
86   propertyMap.Insert( key , value );
87
88   return factory.CreateVisual( propertyMap );
89 }
90
91 } // namespace
92
93 void TestVisualRender( ToolkitTestApplication& application,
94                        DummyControl& actor,
95                        Visual::Base& visual,
96                        std::size_t expectedSamplers = 0,
97                        ImageDimensions imageDimensions = ImageDimensions(),
98                        Integration::ResourcePointer resourcePtr = Integration::ResourcePointer())
99 {
100   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
101   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
102
103   if( resourcePtr )
104   {
105     // set the image size, for test case, this needs to be set before loading started
106     application.GetPlatform().SetClosestImageSize(  Vector2(imageDimensions.GetWidth(), imageDimensions.GetHeight()) );
107   }
108
109   actor.SetSize( 200.f, 200.f );
110   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
111
112   Stage::GetCurrent().Add( actor );
113
114   application.SendNotification(); // Send messages to update
115   application.Render();           // process update and render
116   application.SendNotification(); // process any signals to event
117
118   if( resourcePtr )
119   {
120     DALI_TEST_EQUALS( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceSynchronouslyFunc ), true, TEST_LOCATION);
121   }
122
123   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
124 }
125
126 static void TestMixColor( Visual::Base visual, Property::Index mixColorIndex, const Vector4& testColor )
127 {
128   Property::Map map;
129   visual.CreatePropertyMap(map);
130   Property::Value* value = map.Find( mixColorIndex );
131   DALI_TEST_CHECK( value );
132   Vector3 mixColor1;
133   DALI_TEST_CHECK( value->Get( mixColor1 ) );
134   DALI_TEST_EQUALS( mixColor1, Vector3(testColor), 0.001, TEST_LOCATION );
135
136   value = map.Find( Visual::Property::MIX_COLOR );
137   DALI_TEST_CHECK( value );
138   Vector4 mixColor2;
139   DALI_TEST_CHECK( value->Get( mixColor2 ) );
140   DALI_TEST_EQUALS( mixColor2, testColor, 0.001, TEST_LOCATION );
141
142   value = map.Find( Visual::Property::OPACITY );
143   DALI_TEST_CHECK( value );
144   float opacity;
145   DALI_TEST_CHECK( value->Get( opacity ) );
146   DALI_TEST_EQUALS( opacity, testColor.a, 0.001, TEST_LOCATION );
147 }
148
149
150
151 int UtcDaliImageVisualPropertyMap(void)
152 {
153   ToolkitTestApplication application;
154   tet_infoline( "Request image visual with a Property::Map" );
155
156   VisualFactory factory = VisualFactory::Get();
157   DALI_TEST_CHECK( factory );
158
159   Property::Map propertyMap;
160   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
161   propertyMap.Insert( ImageVisual::Property::URL,  TEST_LARGE_IMAGE_FILE_NAME );
162
163   Visual::Base visual = factory.CreateVisual( propertyMap );
164   DALI_TEST_CHECK( visual );
165
166   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
167   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
168
169   TestGlAbstraction& gl = application.GetGlAbstraction();
170   TraceCallStack& textureTrace = gl.GetTextureTrace();
171   textureTrace.Enable(true);
172
173   DummyControl actor = DummyControl::New();
174   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
175   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
176
177   actor.SetSize( 200.f, 200.f );
178   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
179
180   Stage::GetCurrent().Add( actor );
181   application.SendNotification();
182   application.Render();
183
184   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
185
186   application.SendNotification();
187   application.Render();
188
189   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
190
191   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
192
193   Stage::GetCurrent().Remove( actor );
194   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
195
196   END_TEST;
197 }
198
199
200 int UtcDaliImageVisualRemoteImageLoad(void)
201 {
202   ToolkitTestApplication application;
203   tet_infoline( "Request remote image visual with a Property::Map" );
204
205   VisualFactory factory = VisualFactory::Get();
206   DALI_TEST_CHECK( factory );
207
208   Property::Map propertyMap;
209   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
210   propertyMap.Insert( ImageVisual::Property::URL,  TEST_REMOTE_IMAGE_FILE_NAME );
211
212   Visual::Base visual = factory.CreateVisual( propertyMap );
213   DALI_TEST_CHECK( visual );
214
215   TestGlAbstraction& gl = application.GetGlAbstraction();
216   TraceCallStack& textureTrace = gl.GetTextureTrace();
217   textureTrace.Enable(true);
218
219   DummyControl actor = DummyControl::New();
220   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
221   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
222
223   actor.SetSize( 200.f, 200.f );
224   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
225
226   Stage::GetCurrent().Add( actor );
227   application.SendNotification();
228
229   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
230
231   application.SendNotification();
232   application.Render();
233
234   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
235   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
236
237   Stage::GetCurrent().Remove( actor );
238   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
239
240   END_TEST;
241 }
242
243 int UtcDaliImageVisualTextureReuse1(void)
244 {
245   ToolkitTestApplication application;
246   tet_infoline( "Request remote image visual with a Property::Map; request a second visual with the same property map - should reuse texture" );
247
248   Property::Map propertyMap;
249   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
250   propertyMap.Insert( ImageVisual::Property::URL, TEST_LARGE_IMAGE_FILE_NAME );
251   propertyMap.Insert( DevelImageVisual::Property::RELEASE_POLICY,  DevelImageVisual::ReleasePolicy::DETACHED );
252
253   TestGlAbstraction& gl = application.GetGlAbstraction();
254   TraceCallStack& textureTrace = gl.GetTextureTrace();
255   textureTrace.Enable(true);
256   TraceCallStack& drawTrace = gl.GetDrawTrace();
257   drawTrace.Enable(true);
258
259   Actor actor = CreateActorWithImageVisual( propertyMap );
260   Stage::GetCurrent().Add( actor );
261   application.SendNotification();
262
263   // Wait for image to load
264   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
265
266   application.SendNotification();
267   application.Render();
268
269   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
270   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
271   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
272   DALI_TEST_EQUALS( drawTrace.FindMethod("DrawArrays"), true, TEST_LOCATION );
273   textureTrace.Reset();
274   drawTrace.Reset();
275
276   Actor actor2 = CreateActorWithImageVisual( propertyMap );
277   Stage::GetCurrent().Add(actor2);
278
279   application.SendNotification(); // Send messages to update
280   application.Render();           // process update and render
281   application.SendNotification(); // process any signals to event
282
283   DALI_TEST_EQUALS( actor2.GetRendererCount(), 1u, TEST_LOCATION );
284
285   tet_infoline("Test that 2 draw calls occur with no new texture gens/binds, i.e. both\n"
286                "draw calls use the same texture as the previous draw call\n" );
287
288   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
289   DALI_TEST_EQUALS( drawTrace.CountMethod("DrawArrays"), 2, TEST_LOCATION );
290   DALI_TEST_EQUALS( textureTrace.CountMethod("BindTexture"), 0, TEST_LOCATION );
291
292   tet_infoline("Test that removing 1 actor doesn't delete the texture\n");
293
294   Stage::GetCurrent().Remove( actor );
295   application.SendNotification();
296   application.Render();
297
298   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
299   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION );
300
301   tet_infoline("Test that removing last actor does delete the texture\n");
302
303   Stage::GetCurrent().Remove( actor2 ); // Detaches remaining ImageVisual
304   application.SendNotification();
305   application.Render();
306
307   DALI_TEST_CHECK( actor2.GetRendererCount() == 0u );
308   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION );
309
310   END_TEST;
311 }
312
313
314 int UtcDaliImageVisualTextureReuse2(void)
315 {
316   ToolkitTestApplication application;
317   tet_infoline( "Request remote image visual with a Property::Map; request a second visual with the same url but different property map - should create new texture" );
318
319   Property::Map propertyMap;
320   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
321   propertyMap.Insert( ImageVisual::Property::URL, TEST_REMOTE_IMAGE_FILE_NAME );
322
323   TestGlAbstraction& gl = application.GetGlAbstraction();
324   TraceCallStack& textureTrace = gl.GetTextureTrace();
325   textureTrace.Enable(true);
326   TraceCallStack& drawTrace = gl.GetDrawTrace();
327   drawTrace.Enable(true);
328
329   Actor actor = CreateActorWithImageVisual( propertyMap );
330   Stage::GetCurrent().Add( actor );
331   application.SendNotification();
332
333   // Wait for image to load
334   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
335
336   application.SendNotification();
337   application.Render();
338
339   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
340   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
341   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
342   DALI_TEST_EQUALS( drawTrace.FindMethod("DrawArrays"), true, TEST_LOCATION );
343   textureTrace.Reset();
344   drawTrace.Reset();
345
346   propertyMap.Insert( ImageVisual::Property::SAMPLING_MODE, Dali::SamplingMode::NEAREST );
347   propertyMap.Insert( ImageVisual::Property::DESIRED_WIDTH, 100 );
348   propertyMap.Insert( ImageVisual::Property::DESIRED_HEIGHT, 100 );
349   Actor actor2 = CreateActorWithImageVisual( propertyMap );
350   Stage::GetCurrent().Add(actor2);
351
352   application.SendNotification();
353
354   // Wait for image to load
355   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
356
357   application.SendNotification();
358   application.Render();
359
360   DALI_TEST_EQUALS( actor2.GetRendererCount(), 1u, TEST_LOCATION );
361
362   tet_infoline("Test that 2 draw calls occur with 1 new texture gen/bind, i.e. both "
363                "renderers are using different textures\n" );
364
365   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
366   DALI_TEST_EQUALS( drawTrace.CountMethod("DrawArrays"), 2, TEST_LOCATION );
367   TraceCallStack::NamedParams tex1;
368   tex1["texture"] = "1";
369   TraceCallStack::NamedParams tex2;
370   tex1["texture"] = "2";
371   DALI_TEST_EQUALS( textureTrace.FindMethodAndParams("BindTexture", tex1), true, TEST_LOCATION );
372   DALI_TEST_EQUALS( textureTrace.FindMethodAndParams("BindTexture", tex2), true, TEST_LOCATION );
373
374   tet_infoline("Test that removing 1 actor deletes it's texture\n");
375
376   Stage::GetCurrent().Remove( actor );
377   application.SendNotification();
378   application.Render();
379
380   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
381   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION );
382
383   tet_infoline("Test that removing last actor deletes it's texture\n");
384
385   Stage::GetCurrent().Remove( actor2 );
386   application.SendNotification();
387   application.Render();
388
389   DALI_TEST_CHECK( actor2.GetRendererCount() == 0u );
390   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 2, TEST_LOCATION );
391
392   END_TEST;
393 }
394
395
396 int UtcDaliImageVisualImageHandle(void)
397 {
398   ToolkitTestApplication application;
399   tet_infoline( "Request image visual with an image handle" );
400
401   VisualFactory factory = VisualFactory::Get();
402   DALI_TEST_CHECK( factory );
403
404   Image image = ResourceImage::New(TEST_IMAGE_FILE_NAME);
405   Visual::Base visual = factory.CreateVisual( image );
406
407   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
408   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
409
410   const int width=512;
411   const int height=513;
412
413   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_DISCARD );
414   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, width, height,width, height );
415
416   TestGlAbstraction& gl = application.GetGlAbstraction();
417   TraceCallStack& textureTrace = gl.GetTextureTrace();
418   textureTrace.Enable(true);
419
420   DummyControl actor = DummyControl::New();
421   TestVisualRender( application, actor, visual, 1u,
422                     ImageDimensions(width, height),
423                     Integration::ResourcePointer(bitmap) );
424
425   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
426   END_TEST;
427 }
428
429 int UtcDaliImageVisualCustomWrapModePixelArea(void)
430 {
431   ToolkitTestApplication application;
432   tet_infoline( "Request image visual with a Property::Map, test custom wrap mode and pixel area with atlasing" );
433
434   VisualFactory factory = VisualFactory::Get();
435   DALI_TEST_CHECK( factory );
436
437   // Test wrap mode with atlasing. Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
438   const int width=34;
439   const int height=34;
440   const Vector4 pixelArea(-0.5f, -0.5f, 2.f, 2.f);
441
442   Property::Map propertyMap;
443   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
444   propertyMap.Insert( ImageVisual::Property::URL, TEST_SMALL_IMAGE_FILE_NAME );
445   propertyMap.Insert( ImageVisual::Property::DESIRED_WIDTH, width );
446   propertyMap.Insert( ImageVisual::Property::DESIRED_HEIGHT, height );
447   propertyMap.Insert( ImageVisual::Property::SYNCHRONOUS_LOADING, true );
448   propertyMap.Insert( ImageVisual::Property::PIXEL_AREA, pixelArea );
449   propertyMap.Insert( ImageVisual::Property::WRAP_MODE_U, WrapMode::MIRRORED_REPEAT );
450   propertyMap.Insert( ImageVisual::Property::WRAP_MODE_V, WrapMode::REPEAT );
451   propertyMap.Insert( ImageVisual::Property::ATLASING, true );
452
453   Visual::Base visual = factory.CreateVisual( propertyMap );
454   DALI_TEST_CHECK( visual );
455
456   TestGlAbstraction& gl = application.GetGlAbstraction();
457   TraceCallStack& textureTrace = gl.GetTextureTrace();
458   textureTrace.Enable(true);
459   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
460   texParameterTrace.Enable( true );
461
462   DummyControl actor = DummyControl::New();
463   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
464   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
465   actor.SetSize(2000, 2000);
466   actor.SetParentOrigin(ParentOrigin::CENTER);
467   Stage::GetCurrent().Add( actor );
468
469   // loading started
470   application.SendNotification();
471   application.Render();
472
473   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
474
475   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
476
477   // WITH atlasing, the wrapping is handled manually in shader, so the following gl function should not be called
478   std::stringstream out;
479   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_S << ", " << GL_MIRRORED_REPEAT;
480   DALI_TEST_CHECK( !texParameterTrace.FindMethodAndParams("TexParameteri", out.str()) );
481   out.str("");
482   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_T << ", " << GL_REPEAT;
483   DALI_TEST_CHECK( !texParameterTrace.FindMethodAndParams("TexParameteri", out.str()) );
484
485   // test the uniforms which used to handle the wrap mode
486   Renderer renderer = actor.GetRendererAt( 0u );
487   DALI_TEST_CHECK( renderer );
488
489   Property::Value pixelAreaValue = renderer.GetProperty( renderer.GetPropertyIndex( "pixelArea" ) );
490   DALI_TEST_EQUALS( pixelAreaValue.Get<Vector4>(), pixelArea, TEST_LOCATION );
491   Vector4 pixelAreaUniform;
492   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "pixelArea", pixelAreaUniform ) );
493   DALI_TEST_EQUALS( pixelArea, pixelAreaUniform, Math::MACHINE_EPSILON_100, TEST_LOCATION );
494
495   Property::Value wrapModeValue = renderer.GetProperty( renderer.GetPropertyIndex( "wrapMode" ) );
496   Vector2 wrapMode( WrapMode::MIRRORED_REPEAT-1, WrapMode::REPEAT-1 );
497   DALI_TEST_EQUALS( wrapModeValue.Get<Vector2>(), wrapMode, TEST_LOCATION );
498   Vector2 wrapModeUniform;
499   DALI_TEST_CHECK( gl.GetUniformValue<Vector2>( "wrapMode", wrapModeUniform ) );
500   DALI_TEST_EQUALS( wrapMode, wrapModeUniform, Math::MACHINE_EPSILON_100, TEST_LOCATION );
501
502   actor.Unparent( );
503   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
504
505   END_TEST;
506 }
507
508 int UtcDaliImageVisualCustomWrapModeNoAtlas(void)
509 {
510   ToolkitTestApplication application;
511   tet_infoline( "Request image visual with a Property::Map, test custom wrap mode and pixel area without atlasing" );
512
513   VisualFactory factory = VisualFactory::Get();
514   DALI_TEST_CHECK( factory );
515
516   // Test wrap mode without atlasing. Image with a size bigger than 512*512 will NOT be uploaded as a part of the atlas.
517   const int width=600;
518   const int height=600;
519   const Vector4 pixelArea(-0.5f, -0.5f, 2.f, 2.f);
520
521   Property::Map propertyMap;
522   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
523   propertyMap.Insert( ImageVisual::Property::URL, TEST_LARGE_IMAGE_FILE_NAME );
524   propertyMap.Insert( ImageVisual::Property::DESIRED_WIDTH, width );
525   propertyMap.Insert( ImageVisual::Property::DESIRED_HEIGHT, height );
526   propertyMap.Insert( ImageVisual::Property::SYNCHRONOUS_LOADING, true );
527   propertyMap.Insert( ImageVisual::Property::PIXEL_AREA, pixelArea );
528   propertyMap.Insert( ImageVisual::Property::WRAP_MODE_U, WrapMode::MIRRORED_REPEAT );
529   propertyMap.Insert( ImageVisual::Property::WRAP_MODE_V, WrapMode::REPEAT );
530
531   Visual::Base visual = factory.CreateVisual( propertyMap );
532   DALI_TEST_CHECK( visual );
533
534   TestGlAbstraction& gl = application.GetGlAbstraction();
535   TraceCallStack& textureTrace = gl.GetTextureTrace();
536   textureTrace.Enable(true);
537   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
538   texParameterTrace.Enable( true );
539
540   DummyControl actor = DummyControl::New();
541   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
542   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
543   actor.SetSize(2000, 2000);
544   actor.SetParentOrigin(ParentOrigin::CENTER);
545   Stage::GetCurrent().Add( actor );
546
547   // loading started
548   application.SendNotification();
549   application.Render();
550
551   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
552
553   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
554
555   // WITHOUT atlasing, the wrapping is handled by setting gl texture parameters
556   std::stringstream out;
557   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_S << ", " << GL_MIRRORED_REPEAT;
558   DALI_TEST_CHECK( texParameterTrace.FindMethodAndParams("TexParameteri", out.str()) );
559   out.str("");
560   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_T << ", " << GL_REPEAT;
561   DALI_TEST_CHECK( texParameterTrace.FindMethodAndParams("TexParameteri", out.str()) );
562
563   // test the uniforms which used to handle the wrap mode
564   Renderer renderer = actor.GetRendererAt( 0u );
565   DALI_TEST_CHECK( renderer );
566
567   Property::Value pixelAreaValue = renderer.GetProperty( renderer.GetPropertyIndex( "pixelArea" ) );
568   DALI_TEST_EQUALS( pixelAreaValue.Get<Vector4>(), pixelArea, TEST_LOCATION );
569   Vector4 pixelAreaUniform;
570   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "pixelArea", pixelAreaUniform ) );
571   DALI_TEST_EQUALS( pixelArea, pixelAreaUniform, Math::MACHINE_EPSILON_100, TEST_LOCATION );
572
573   Property::Index wrapModeIndex = renderer.GetPropertyIndex( "wrapMode" );
574   DALI_TEST_CHECK(wrapModeIndex == Property::INVALID_INDEX);
575
576   actor.Unparent();
577   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
578
579   END_TEST;
580 }
581
582 int UtcDaliImageVisualAnimateMixColor(void)
583 {
584   ToolkitTestApplication application;
585   tet_infoline( "Animate mix color" );
586
587   application.GetPlatform().SetClosestImageSize( Vector2(100, 100) );
588
589   VisualFactory factory = VisualFactory::Get();
590   Property::Map propertyMap;
591   propertyMap.Insert(Visual::Property::TYPE,  Visual::IMAGE);
592   propertyMap.Insert(ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME );
593   propertyMap.Insert("mixColor", Color::BLUE);
594   propertyMap.Insert(ImageVisual::Property::SYNCHRONOUS_LOADING, true);
595   Visual::Base visual = factory.CreateVisual( propertyMap );
596
597   DummyControl actor = DummyControl::New(true);
598   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
599   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
600
601   actor.SetSize(2000, 2000);
602   actor.SetParentOrigin(ParentOrigin::CENTER);
603   actor.SetColor(Color::BLACK);
604   Stage::GetCurrent().Add(actor);
605
606   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION);
607
608   Renderer renderer = actor.GetRendererAt(0);
609   Property::Index index = DevelHandle::GetPropertyIndex( renderer, Visual::Property::MIX_COLOR );
610   Property::Value blendModeValue = renderer.GetProperty( Renderer::Property::BLEND_MODE );
611   DALI_TEST_EQUALS( blendModeValue.Get<int>(), (int)BlendMode::AUTO, TEST_LOCATION );
612
613   tet_infoline("Test that the renderer has the mixColor property");
614   DALI_TEST_CHECK( index != Property::INVALID_INDEX );
615
616   const Vector4 TARGET_MIX_COLOR( 1.0f, 0.0f, 0.0f, 0.5f );
617
618   Property::Map map;
619   map["target"] = "testVisual";
620   map["property"] = "mixColor";
621   map["initialValue"] = Color::MAGENTA;
622   map["targetValue"] = TARGET_MIX_COLOR;
623   map["animator"] = Property::Map()
624     .Add("alphaFunction", "LINEAR")
625     .Add("timePeriod", Property::Map()
626          .Add("delay", 0.0f)
627          .Add("duration", 4.0f));
628
629   Dali::Toolkit::TransitionData transition = TransitionData::New( map );
630
631   Animation animation = dummyImpl.CreateTransition( transition );
632
633   blendModeValue = renderer.GetProperty( Renderer::Property::BLEND_MODE );
634   DALI_TEST_EQUALS( blendModeValue.Get<int>(), (int)BlendMode::ON, TEST_LOCATION );
635
636   animation.AnimateTo( Property(actor, Actor::Property::COLOR), Color::WHITE );
637   animation.Play();
638
639   application.SendNotification();
640   application.Render(0); // Ensure animation starts
641   application.Render(2000u); // Halfway point
642   Vector4 testColor(1.0f, 0.0f, 0.5f, 0.75f );
643
644   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<Vector4>("uColor", Vector4(0.5f, 0.5f, 0.5f, 1.0f )), true, TEST_LOCATION );
645   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<Vector3>("mixColor", Vector3(testColor)), true, TEST_LOCATION );
646   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<float>("opacity", testColor.a), true, TEST_LOCATION );
647
648   application.Render(2000u); // Halfway point between blue and white
649
650   DALI_TEST_EQUALS( actor.GetCurrentColor(), Color::WHITE, TEST_LOCATION );
651   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<Vector4>("uColor", Color::WHITE ), true, TEST_LOCATION );
652   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<Vector3>("mixColor", Vector3(TARGET_MIX_COLOR)), true, TEST_LOCATION );
653   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<float>("opacity", TARGET_MIX_COLOR.a), true, TEST_LOCATION );
654
655   TestMixColor( visual, Visual::Property::MIX_COLOR, TARGET_MIX_COLOR );
656
657   blendModeValue = renderer.GetProperty( Renderer::Property::BLEND_MODE );
658   DALI_TEST_EQUALS( blendModeValue.Get<int>(), (int)BlendMode::ON, TEST_LOCATION );
659
660   END_TEST;
661 }
662
663 int UtcDaliImageVisualAnimateOpacity(void)
664 {
665   ToolkitTestApplication application;
666   tet_infoline( "Animate image visual opacity" );
667
668   application.GetPlatform().SetClosestImageSize( Vector2(100, 100) );
669
670   VisualFactory factory = VisualFactory::Get();
671   Property::Map propertyMap;
672   propertyMap.Insert(Visual::Property::TYPE,  Visual::IMAGE);
673   propertyMap.Insert(ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME );
674   propertyMap.Insert("opacity", 0.5f);
675   propertyMap.Insert(ImageVisual::Property::SYNCHRONOUS_LOADING, true);
676   Visual::Base visual = factory.CreateVisual( propertyMap );
677
678   DummyControl actor = DummyControl::New(true);
679   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
680   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
681
682   actor.SetSize(2000, 2000);
683   actor.SetParentOrigin(ParentOrigin::CENTER);
684   actor.SetColor(Color::BLACK);
685   Stage::GetCurrent().Add(actor);
686
687   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION);
688
689   Renderer renderer = actor.GetRendererAt(0);
690   tet_infoline("Test that the renderer has the opacity property");
691   Property::Index index = DevelHandle::GetPropertyIndex( renderer, Visual::Property::OPACITY );
692   DALI_TEST_CHECK( index != Property::INVALID_INDEX );
693
694
695   Property::Value blendModeValue = renderer.GetProperty( Renderer::Property::BLEND_MODE );
696   DALI_TEST_EQUALS( blendModeValue.Get<int>(), (int)BlendMode::ON, TEST_LOCATION );
697
698   {
699     tet_infoline( "Test that the opacity can be increased to full via animation, and that the blend mode is set appropriately at the start and end of the animation." );
700
701     Property::Map map;
702     map["target"] = "testVisual";
703     map["property"] = "opacity";
704     map["targetValue"] = 1.0f;
705     map["animator"] = Property::Map()
706       .Add("alphaFunction", "LINEAR")
707       .Add("timePeriod", Property::Map()
708            .Add("delay", 0.0f)
709            .Add("duration", 4.0f));
710
711     Dali::Toolkit::TransitionData transition = TransitionData::New( map );
712     Animation animation = dummyImpl.CreateTransition( transition );
713     animation.Play();
714
715     application.SendNotification();
716     application.Render(0);     // Ensure animation starts
717     application.Render(2000u); // Halfway point through animation
718     application.SendNotification(); // Handle any signals
719
720     DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<float>("opacity", 0.75f), true, TEST_LOCATION );
721
722     application.Render(2001u); // end
723     application.SendNotification(); // ensure animation finished signal is sent
724
725     DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<float>("opacity", 1.0f), true, TEST_LOCATION );
726
727     blendModeValue = renderer.GetProperty( Renderer::Property::BLEND_MODE );
728     DALI_TEST_EQUALS( blendModeValue.Get<int>(), (int)BlendMode::AUTO, TEST_LOCATION );
729   }
730
731
732   {
733     tet_infoline( "Test that the opacity can be reduced via animation, and that the blend mode is set appropriately at the start and end of the animation." );
734
735     Property::Map map;
736     map["target"] = "testVisual";
737     map["property"] = Visual::Property::OPACITY;
738     map["targetValue"] = 0.1f;
739     map["animator"] = Property::Map()
740       .Add("alphaFunction", "LINEAR")
741       .Add("timePeriod", Property::Map()
742            .Add("delay", 0.0f)
743            .Add("duration", 4.0f));
744
745     Dali::Toolkit::TransitionData transition = TransitionData::New( map );
746     Animation animation = dummyImpl.CreateTransition( transition );
747     animation.Play();
748
749     blendModeValue = renderer.GetProperty( Renderer::Property::BLEND_MODE );
750     DALI_TEST_EQUALS( blendModeValue.Get<int>(), (int)BlendMode::ON, TEST_LOCATION );
751
752     application.SendNotification();
753     application.Render(0);     // Ensure animation starts
754     application.Render(2000u); // Halfway point
755     application.SendNotification();
756
757     DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<float>("opacity", 0.55f), true, TEST_LOCATION );
758
759     application.Render(2016u); // end
760     application.SendNotification();
761
762     DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<float>("opacity", 0.1f), true, TEST_LOCATION );
763
764     blendModeValue = renderer.GetProperty( Renderer::Property::BLEND_MODE );
765     DALI_TEST_EQUALS( blendModeValue.Get<int>(), (int)BlendMode::ON, TEST_LOCATION );
766   }
767
768
769   END_TEST;
770 }
771
772 int UtcDaliImageVisualAnimatePixelArea(void)
773 {
774   ToolkitTestApplication application;
775   tet_infoline( "ImageVisual animate pixel area" );
776
777   application.GetPlatform().SetClosestImageSize( Vector2(100, 100) );
778
779   VisualFactory factory = VisualFactory::Get();
780   Property::Map propertyMap;
781   propertyMap.Insert(Visual::Property::TYPE,  Visual::IMAGE);
782   propertyMap.Insert(ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME );
783   propertyMap.Insert("mixColor", Color::BLUE);
784   propertyMap.Insert(ImageVisual::Property::SYNCHRONOUS_LOADING, true);
785   Visual::Base visual = factory.CreateVisual( propertyMap );
786
787   DummyControl actor = DummyControl::New(true);
788   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
789   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
790
791   actor.SetSize(2000, 2000);
792   actor.SetParentOrigin(ParentOrigin::CENTER);
793   actor.SetColor(Color::BLACK);
794   Stage::GetCurrent().Add(actor);
795
796   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION);
797
798   Renderer renderer = actor.GetRendererAt(0);
799   Property::Index index = DevelHandle::GetPropertyIndex( renderer, Visual::Property::MIX_COLOR );
800
801   tet_infoline("Test that the renderer has the mixColor property");
802   DALI_TEST_CHECK( index != Property::INVALID_INDEX );
803
804   // TransitionData only takes string keys
805   Property::Map map;
806   map["target"] = "testVisual";
807   map["property"] = "pixelArea";
808   map["initialValue"] = Vector4( 0,0,0,1 );
809   map["targetValue"] = Vector4( 0,0,1,1 ); // Animate width from zero to full
810   map["animator"] = Property::Map()
811     .Add("alphaFunction", "LINEAR")
812     .Add("timePeriod", Property::Map()
813          .Add("delay", 0.0f)
814          .Add("duration", 4.0f));
815
816   Dali::Toolkit::TransitionData transition = TransitionData::New( map );
817
818   Animation animation = dummyImpl.CreateTransition( transition );
819   animation.AnimateTo( Property(actor, Actor::Property::COLOR), Color::WHITE );
820   animation.Play();
821
822   application.SendNotification();
823   application.Render(0);     // Ensure animation starts
824   application.Render(2000u); // Halfway point
825
826   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<Vector4>("pixelArea", Vector4(0.0f, 0.0f, 0.5f, 1.0f )), true, TEST_LOCATION );
827
828   application.Render(2000u); // End of animation
829
830   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<Vector4>("pixelArea", Vector4( 0.0f, 0.0f, 1.0f, 1.0f )), true, TEST_LOCATION );
831
832   END_TEST;
833 }
834
835 int UtcDaliImageVisualTextureCancelRemoteLoad(void)
836 {
837   ToolkitTestApplication application;
838   tet_infoline( "Request remote image visual, then destroy visual to cancel load" );
839
840   Property::Map propertyMap;
841   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
842   propertyMap.Insert( ImageVisual::Property::URL, TEST_REMOTE_IMAGE_FILE_NAME );
843
844   TestGlAbstraction& gl = application.GetGlAbstraction();
845   TraceCallStack& textureTrace = gl.GetTextureTrace();
846   textureTrace.Enable(true);
847   TraceCallStack& drawTrace = gl.GetDrawTrace();
848   drawTrace.Enable(true);
849
850   Actor actor = CreateActorWithImageVisual( propertyMap );
851   Stage::GetCurrent().Add( actor );
852   application.SendNotification();
853
854   Stage::GetCurrent().Remove( actor );
855   application.SendNotification();
856
857   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
858   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
859   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), false, TEST_LOCATION );
860   DALI_TEST_EQUALS( drawTrace.FindMethod("DrawArrays"), false, TEST_LOCATION );
861
862   END_TEST;
863 }
864
865 int UtcDaliImageVisualTextureCancelAsyncLoad(void)
866 {
867   ToolkitTestApplication application;
868   tet_infoline( "Load image asynchronosly, cancel loading, then load again" );
869
870   VisualFactory factory = VisualFactory::Get();
871   DALI_TEST_CHECK( factory );
872
873   Property::Map propertyMap;
874   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
875   propertyMap.Insert( ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME );
876
877   Visual::Base visual = factory.CreateVisual( propertyMap );
878   DALI_TEST_CHECK( visual );
879
880   TestGlAbstraction& gl = application.GetGlAbstraction();
881   TraceCallStack& textureTrace = gl.GetTextureTrace();
882   textureTrace.Enable( true );
883   TraceCallStack& drawTrace = gl.GetDrawTrace();
884   drawTrace.Enable( true );
885
886   DummyControl actor = DummyControl::New();
887   DummyControlImpl& dummyImpl = static_cast< DummyControlImpl& >( actor.GetImplementation() );
888   dummyImpl.RegisterVisual( Control::Property::BACKGROUND, visual );
889
890   Stage::GetCurrent().Add( actor );
891
892   // Cancel loading
893   Stage::GetCurrent().Remove( actor );
894
895   Stage::GetCurrent().Add( actor );
896
897   // Create another visual with the same image
898   visual = factory.CreateVisual( propertyMap );
899   DALI_TEST_CHECK( visual );
900
901   dummyImpl.RegisterVisual( Control::Property::BACKGROUND, visual );
902
903   application.SendNotification();
904   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
905
906   application.SendNotification();
907   application.Render();
908
909   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
910   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
911   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
912   DALI_TEST_EQUALS( drawTrace.FindMethod("DrawArrays"), true, TEST_LOCATION );
913
914   END_TEST;
915 }
916
917 int UtcDaliImageVisualSetInvalidAsyncImage(void)
918 {
919   ToolkitTestApplication application;
920   tet_infoline( "Request image visual with invalid images - should draw broken.png" );
921
922   VisualFactory factory = VisualFactory::Get();
923   DALI_TEST_CHECK( factory );
924
925   Property::Map propertyMap;
926   propertyMap.Insert( Toolkit::Visual::Property::TYPE, Visual::IMAGE );
927   propertyMap.Insert( ImageVisual::Property::URL, TEST_INVALID_FILE_NAME );
928
929   Visual::Base visual = factory.CreateVisual( propertyMap );
930   DALI_TEST_CHECK( visual );
931
932   TestGlAbstraction& gl = application.GetGlAbstraction();
933   TraceCallStack& textureTrace = gl.GetTextureTrace();
934   textureTrace.Enable(true);
935
936   DummyControl actor = DummyControl::New();
937   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
938   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
939
940   actor.SetSize( 200.f, 200.f );
941   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
942
943   Stage::GetCurrent().Add( actor );
944
945   application.SendNotification();
946   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
947
948   application.SendNotification();
949   application.Render();
950
951   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
952   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
953
954   Stage::GetCurrent().Remove( actor );
955   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
956
957   END_TEST;
958 }
959
960 int UtcDaliImageVisualSetInvalidSyncImage(void)
961 {
962   ToolkitTestApplication application;
963   tet_infoline( "Request image visual with invalid images - should draw broken.png" );
964
965   VisualFactory factory = VisualFactory::Get();
966   DALI_TEST_CHECK( factory );
967
968   Property::Map propertyMap;
969   propertyMap.Insert( Toolkit::Visual::Property::TYPE, Visual::IMAGE );
970   propertyMap.Insert( ImageVisual::Property::URL, TEST_INVALID_FILE_NAME );
971   propertyMap.Insert( ImageVisual::Property::SYNCHRONOUS_LOADING, true );
972
973   Visual::Base visual = factory.CreateVisual( propertyMap );
974   DALI_TEST_CHECK( visual );
975
976   TestGlAbstraction& gl = application.GetGlAbstraction();
977   TraceCallStack& textureTrace = gl.GetTextureTrace();
978   textureTrace.Enable(true);
979
980   DummyControl actor = DummyControl::New();
981   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
982   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
983
984   actor.SetSize( 200.f, 200.f );
985   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
986
987   Stage::GetCurrent().Add( actor );
988
989   application.SendNotification();
990   application.Render();
991
992   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
993   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
994
995   Stage::GetCurrent().Remove( actor );
996   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
997
998   END_TEST;
999 }
1000
1001 int UtcDaliImageVisualSetInvalidRemoteImage(void)
1002 {
1003   ToolkitTestApplication application;
1004   tet_infoline( "Request image visual with invalid images - should draw broken.png" );
1005
1006   VisualFactory factory = VisualFactory::Get();
1007   DALI_TEST_CHECK( factory );
1008
1009   // Local invalid file, asynchronous loading
1010   Property::Map propertyMap;
1011   propertyMap.Insert( Toolkit::Visual::Property::TYPE, Visual::IMAGE );
1012   propertyMap.Insert( ImageVisual::Property::URL, TEST_REMOTE_INVALID_FILE_NAME );
1013
1014   Visual::Base visual = factory.CreateVisual( propertyMap );
1015   DALI_TEST_CHECK( visual );
1016
1017   TestGlAbstraction& gl = application.GetGlAbstraction();
1018   TraceCallStack& textureTrace = gl.GetTextureTrace();
1019   textureTrace.Enable(true);
1020
1021   DummyControl actor = DummyControl::New();
1022   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
1023   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
1024
1025   actor.SetSize( 200.f, 200.f );
1026   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
1027
1028   Stage::GetCurrent().Add( actor );
1029
1030   application.SendNotification();
1031   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1032
1033   application.SendNotification();
1034   application.Render();
1035
1036   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1037   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
1038
1039   Stage::GetCurrent().Remove( actor );
1040   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1041
1042   END_TEST;
1043 }
1044
1045 int UtcDaliImageVisualAlphaMask(void)
1046 {
1047   ToolkitTestApplication application;
1048   tet_infoline( "Request image visual with a Property::Map containing an Alpha mask" );
1049
1050   VisualFactory factory = VisualFactory::Get();
1051   DALI_TEST_CHECK( factory );
1052
1053   Property::Map propertyMap;
1054   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
1055   propertyMap.Insert( ImageVisual::Property::URL,  TEST_LARGE_IMAGE_FILE_NAME );
1056   propertyMap.Insert( ImageVisual::Property::ALPHA_MASK_URL, TEST_MASK_IMAGE_FILE_NAME );
1057
1058   Visual::Base visual = factory.CreateVisual( propertyMap );
1059   DALI_TEST_CHECK( visual );
1060
1061   Property::Map testMap;
1062   visual.CreatePropertyMap(testMap);
1063   DALI_TEST_EQUALS(*testMap.Find(ImageVisual::Property::ALPHA_MASK_URL),Property::Value(TEST_MASK_IMAGE_FILE_NAME), TEST_LOCATION );
1064
1065   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
1066   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
1067
1068   TestGlAbstraction& gl = application.GetGlAbstraction();
1069   TraceCallStack& textureTrace = gl.GetTextureTrace();
1070   textureTrace.Enable(true);
1071
1072   DummyControl actor = DummyControl::New();
1073   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
1074   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
1075
1076   actor.SetSize( 200.f, 200.f );
1077   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
1078   DALI_TEST_EQUALS( actor.IsResourceReady(), false, TEST_LOCATION );
1079
1080   Stage::GetCurrent().Add( actor );
1081   application.SendNotification();
1082   application.Render();
1083
1084   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 2 ), true, TEST_LOCATION );
1085
1086   application.SendNotification();
1087   application.Render();
1088
1089   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1090   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
1091   DALI_TEST_EQUALS( actor.IsResourceReady(), true, TEST_LOCATION );
1092
1093   dummyImpl.UnregisterVisual(  Control::CONTROL_PROPERTY_END_INDEX + 1 );
1094   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
1095
1096   END_TEST;
1097 }
1098
1099 int UtcDaliImageVisualRemoteAlphaMask(void)
1100 {
1101   ToolkitTestApplication application;
1102   tet_infoline( "Request image visual with a Property::Map containing an Alpha mask" );
1103
1104   VisualFactory factory = VisualFactory::Get();
1105   DALI_TEST_CHECK( factory );
1106
1107   const std::string MASK_IMAGE = TEST_REMOTE_IMAGE_FILE_NAME;
1108
1109   Property::Map propertyMap;
1110   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
1111   propertyMap.Insert( ImageVisual::Property::URL,  TEST_IMAGE_FILE_NAME );
1112   propertyMap.Insert( "alphaMaskUrl", MASK_IMAGE );
1113
1114   Visual::Base visual = factory.CreateVisual( propertyMap );
1115   DALI_TEST_CHECK( visual );
1116
1117   Property::Map testMap;
1118   visual.CreatePropertyMap(testMap);
1119   DALI_TEST_EQUALS(*testMap.Find(ImageVisual::Property::ALPHA_MASK_URL),Property::Value(MASK_IMAGE), TEST_LOCATION );
1120
1121   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
1122   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
1123
1124   TestGlAbstraction& gl = application.GetGlAbstraction();
1125   TraceCallStack& textureTrace = gl.GetTextureTrace();
1126   textureTrace.Enable(true);
1127
1128   DummyControl actor = DummyControl::New();
1129   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
1130   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
1131   DALI_TEST_EQUALS( actor.IsResourceReady(), false, TEST_LOCATION );
1132
1133   actor.SetSize( 200.f, 200.f );
1134   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
1135
1136   Stage::GetCurrent().Add( actor );
1137   application.SendNotification();
1138   application.Render();
1139
1140   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 2 ), true, TEST_LOCATION );
1141
1142   application.SendNotification();
1143   application.Render();
1144
1145   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1146   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
1147   DALI_TEST_EQUALS( actor.IsResourceReady(), true, TEST_LOCATION );
1148
1149   END_TEST;
1150 }
1151
1152
1153 int UtcDaliImageVisualAlphaMaskCrop(void)
1154 {
1155   ToolkitTestApplication application;
1156   tet_infoline( "Request image visual with an Alpha mask and scale/cropping" );
1157
1158   VisualFactory factory = VisualFactory::Get();
1159   DALI_TEST_CHECK( factory );
1160
1161   Property::Map propertyMap;
1162   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
1163   propertyMap.Insert( ImageVisual::Property::URL,  TEST_LARGE_IMAGE_FILE_NAME );
1164   propertyMap.Insert( ImageVisual::Property::ALPHA_MASK_URL, TEST_MASK_IMAGE_FILE_NAME );
1165   propertyMap.Insert( ImageVisual::Property::MASK_CONTENT_SCALE, 1.6f );
1166   propertyMap.Insert( ImageVisual::Property::CROP_TO_MASK, true );
1167
1168   Visual::Base visual = factory.CreateVisual( propertyMap );
1169   DALI_TEST_CHECK( visual );
1170
1171   Property::Map testMap;
1172   visual.CreatePropertyMap(testMap);
1173   DALI_TEST_EQUALS( *testMap.Find(ImageVisual::Property::ALPHA_MASK_URL),Property::Value(TEST_MASK_IMAGE_FILE_NAME), TEST_LOCATION );
1174   DALI_TEST_EQUALS( *testMap.Find(ImageVisual::Property::MASK_CONTENT_SCALE), Property::Value(1.6f), TEST_LOCATION );
1175   DALI_TEST_EQUALS( *testMap.Find(ImageVisual::Property::CROP_TO_MASK),Property::Value(true), TEST_LOCATION );
1176
1177   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
1178   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
1179
1180   TestGlAbstraction& gl = application.GetGlAbstraction();
1181   TraceCallStack& textureTrace = gl.GetTextureTrace();
1182   textureTrace.Enable(true);
1183
1184   DummyControl actor = DummyControl::New();
1185   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
1186   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
1187
1188   actor.SetSize( 200.f, 200.f );
1189   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
1190   DALI_TEST_EQUALS( actor.IsResourceReady(), false, TEST_LOCATION );
1191
1192   Stage::GetCurrent().Add( actor );
1193   application.SendNotification();
1194   application.Render();
1195
1196   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 2 ), true, TEST_LOCATION );
1197
1198   application.SendNotification();
1199   application.Render();
1200
1201   Vector2 size;
1202   visual.GetNaturalSize(size);
1203
1204   DALI_TEST_EQUALS( size, Vector2( 100.0f, 100.0f ), 0.001f, TEST_LOCATION );
1205   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1206   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
1207   DALI_TEST_EQUALS( actor.IsResourceReady(), true, TEST_LOCATION );
1208
1209   END_TEST;
1210 }
1211
1212 int UtcDaliImageVisualReleasePolicy01(void)
1213 {
1214   ToolkitTestApplication application;
1215   tet_infoline( "UtcDaliImageVisualReleasePolicy01 Detached Policy, disabling visual with this policy deletes texture" );
1216
1217   Visual::Base imageVisual = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, DevelImageVisual::Property::RELEASE_POLICY, DevelImageVisual::ReleasePolicy::DETACHED );
1218   DALI_TEST_CHECK( imageVisual );
1219
1220   // Set up debug trace
1221   TestGlAbstraction& gl = application.GetGlAbstraction();
1222   TraceCallStack& textureTrace = gl.GetTextureTrace();
1223   textureTrace.Enable(true);
1224
1225   tet_infoline( "Register visual with control and ensure it has the only handle" );
1226   DummyControl actor = DummyControl::New(true);
1227   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1228   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual );
1229   imageVisual.Reset();
1230
1231   actor.SetSize(200.f, 200.f);
1232
1233   application.SendNotification();
1234   application.Render(0);
1235   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1236   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
1237
1238   Stage::GetCurrent().Add( actor );
1239
1240   // Wait for image to load
1241   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1242
1243   application.SendNotification();
1244   application.Render(0);
1245   // Test renderer and texture created
1246   tet_infoline( "Confirm texture created" );
1247   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1248   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
1249
1250   tet_infoline( "Disable visual causing the texture to be deleted" );
1251   dummyImpl.EnableVisual( DummyControl::Property::TEST_VISUAL, false );
1252
1253   application.SendNotification();
1254   application.Render(0);
1255   // Test renderer and textures removed.
1256   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1257   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION );
1258
1259   END_TEST;
1260 }
1261
1262 int UtcDaliImageVisualReleasePolicy02(void)
1263 {
1264   ToolkitTestApplication application;
1265   tet_infoline( "UtcDaliImageVisualReleasePolicy02 Destroyed Policy, Texture should be deleted when visual destroyed" );
1266
1267   Visual::Base imageVisual = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, DevelImageVisual::Property::RELEASE_POLICY, DevelImageVisual::ReleasePolicy::DESTROYED );
1268   DALI_TEST_CHECK( imageVisual );
1269
1270   // Setup debug trace
1271   TestGlAbstraction& gl = application.GetGlAbstraction();
1272   TraceCallStack& textureTrace = gl.GetTextureTrace();
1273   textureTrace.Enable(true);
1274
1275   tet_infoline( "Register visual with control and ensure it has the only handle" );
1276   DummyControl actor = DummyControl::New(true);
1277   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1278   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual );
1279   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
1280
1281   actor.SetSize(200.f, 200.f);
1282
1283   application.SendNotification();
1284   application.Render(0);
1285   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1286   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
1287
1288   Stage::GetCurrent().Add( actor );
1289
1290   // Wait for image to load
1291   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1292
1293   application.SendNotification();
1294   application.Render(0);
1295   // Test renderer and texture created
1296   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1297   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
1298
1299
1300   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
1301   tet_infoline( "Destroy visual by UnRegistering visual with control, check renderer is destroyed" );
1302   dummyImpl.UnregisterVisual( DummyControl::Property::TEST_VISUAL );
1303   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1304   application.SendNotification();
1305   application.Render();
1306
1307   // Test texture removed after visual destroyed.
1308   tet_infoline( "Ensure texture is deleted after visual destroyed" );
1309   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION );
1310
1311   END_TEST;
1312 }
1313
1314 int UtcDaliImageVisualReleasePolicy03(void)
1315 {
1316   ToolkitTestApplication application;
1317   tet_infoline( "UtcDaliImageVisualReleasePolicy03 Never Policy, texture should not be deleted after visual destroyed" );
1318
1319   Visual::Base imageVisual = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, DevelImageVisual::Property::RELEASE_POLICY, DevelImageVisual::ReleasePolicy::NEVER );
1320   DALI_TEST_CHECK( imageVisual );
1321
1322   TestGlAbstraction& gl = application.GetGlAbstraction();
1323   TraceCallStack& textureTrace = gl.GetTextureTrace();
1324   textureTrace.Enable(true);
1325
1326   tet_infoline( "Register visual with control and ensure it has the only handle" );
1327   DummyControl actor = DummyControl::New(true);
1328   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1329   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual );
1330   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
1331
1332   actor.SetSize(200.f, 200.f);
1333
1334   application.SendNotification();
1335   application.Render(0);
1336   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1337   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
1338
1339   Stage::GetCurrent().Add( actor );
1340
1341   // Wait for image to load
1342   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1343
1344   application.SendNotification();
1345   application.Render(0);
1346   // Test renderer and texture created
1347   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1348   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
1349
1350   tet_infoline( "Destroy visual by UnRegistering visual with control, check renderer is destroyed" );
1351   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
1352   dummyImpl.UnregisterVisual( DummyControl::Property::TEST_VISUAL );
1353   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1354   application.SendNotification();
1355   application.Render();
1356
1357   tet_infoline( "Ensure texture is not deleted as policy is set to NEVER" );
1358   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION );
1359
1360   END_TEST;
1361 }
1362
1363 int UtcDaliImageVisualReleasePolicy04(void)
1364 {
1365   ToolkitTestApplication application;
1366   tet_infoline( "UtcDaliImageVisualReleasePolicy04 Two visuals with different policies sharing a texture" );
1367
1368   tet_infoline( "Create first visual with Never release policy" );
1369   Visual::Base imageVisualNever = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, DevelImageVisual::Property::RELEASE_POLICY, DevelImageVisual::ReleasePolicy::NEVER );
1370
1371   tet_infoline( "Create second visual with Destroyed release policy");
1372     Visual::Base imageVisualDestroyed = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, DevelImageVisual::Property::RELEASE_POLICY, DevelImageVisual::ReleasePolicy::DESTROYED );
1373
1374   // Set up trace debug
1375   TestGlAbstraction& gl = application.GetGlAbstraction();
1376   TraceCallStack& textureTrace = gl.GetTextureTrace();
1377   textureTrace.Enable(true);
1378
1379   tet_infoline( "Register visuals with control and ensure it has the only handles" );
1380   DummyControl actor = DummyControl::New(true);
1381   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1382   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisualNever );
1383   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL2, imageVisualDestroyed );
1384   imageVisualNever.Reset(); // reduce ref count so only the control keeps the visual alive.
1385   imageVisualDestroyed.Reset(); // reduce ref count so only the control keeps the visual alive.
1386
1387   actor.SetSize(200.f, 200.f);
1388
1389   // Test initially zero renderers
1390   application.SendNotification();
1391   application.Render(0);
1392   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1393   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
1394
1395   Stage::GetCurrent().Add( actor );
1396
1397   // Wait for image to load
1398   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1399
1400   application.SendNotification();
1401   application.Render(0);
1402   tet_infoline( "Ensure a texture is created, shared amongst both visuals.  Each visual has its own renderer" );
1403   DALI_TEST_EQUALS( actor.GetRendererCount(), 2u, TEST_LOCATION );
1404   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
1405
1406   // Test renderer removed when visual destroyed
1407   DALI_TEST_CHECK( actor.GetRendererCount() == 2u );
1408   dummyImpl.UnregisterVisual( DummyControl::Property::TEST_VISUAL2 );  // TEST_VISUAL2 no longer requires the texture as release policy DESTROYED
1409   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
1410   application.SendNotification();
1411   application.Render();
1412
1413   // Test texture was not deleted as TEST_VISUAL release policy is NEVER so it is still required.
1414   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION );
1415
1416   dummyImpl.UnregisterVisual( DummyControl::Property::TEST_VISUAL );
1417   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1418   application.SendNotification();
1419   application.Render();
1420
1421   tet_infoline( "Ensure a texture is not deleted as second visual used the NEVER release policy" );
1422   // Test texture was not deleted as TEST_VISUAL release policy is NEVER so it is still required.
1423   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION );
1424
1425   END_TEST;
1426 }
1427
1428 int UtcDaliImageVisualReleasePolicy05(void)
1429 {
1430   ToolkitTestApplication application;
1431   tet_infoline( "UtcDaliImageVisualReleasePolicy05 Testing settung by string currents correct enum" );
1432
1433   VisualFactory factory = VisualFactory::Get();
1434
1435   Property::Map propertyMapNeverReleasePolicy;
1436   propertyMapNeverReleasePolicy.Insert( Visual::Property::TYPE,  Visual::IMAGE );
1437   propertyMapNeverReleasePolicy.Insert( ImageVisual::Property::URL,  TEST_IMAGE_FILE_NAME );
1438   propertyMapNeverReleasePolicy.Insert( ImageVisual::Property::DESIRED_WIDTH,   20 );
1439   propertyMapNeverReleasePolicy.Insert( ImageVisual::Property::DESIRED_HEIGHT,   30 );
1440   propertyMapNeverReleasePolicy.Insert( "releasePolicy" , "never" );
1441
1442   Visual::Base imageVisualNever = factory.CreateVisual( propertyMapNeverReleasePolicy );
1443
1444   Property::Map resultMap;
1445   imageVisualNever.CreatePropertyMap( resultMap );
1446   DALI_TEST_CHECK( ! resultMap.Empty() );
1447
1448   DALI_TEST_EQUALS( ( resultMap.Find( DevelImageVisual::Property::RELEASE_POLICY ) )->Get<int>(), (int)DevelImageVisual::ReleasePolicy::NEVER, TEST_LOCATION );
1449
1450   END_TEST;
1451 }
1452
1453 int UtcDaliImageVisualReleasePolicy06(void)
1454 {
1455   ToolkitTestApplication application;
1456   tet_infoline( "UtcDaliImageVisualReleasePolicy06 Never Policy, texture should not be affected by Disabling and Enabling visual" );
1457
1458   Visual::Base imageVisual= CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, DevelImageVisual::Property::RELEASE_POLICY, DevelImageVisual::ReleasePolicy::NEVER );
1459   DALI_TEST_CHECK( imageVisual );
1460
1461   TestGlAbstraction& gl = application.GetGlAbstraction();
1462   TraceCallStack& textureTrace = gl.GetTextureTrace();
1463   textureTrace.Enable(true);
1464
1465   tet_infoline( "Register visual with control and ensure it has the only handle" );
1466   DummyControl actor = DummyControl::New(true);
1467   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1468   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual );
1469   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
1470
1471   actor.SetSize(200.f, 200.f);
1472
1473   application.SendNotification();
1474   application.Render(0);
1475   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1476   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
1477
1478   Stage::GetCurrent().Add( actor );
1479
1480   // Wait for image to load
1481   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1482
1483   application.SendNotification();
1484   application.Render(0);
1485   // Test renderer and texture created
1486   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1487   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
1488   textureTrace.Reset();
1489
1490   tet_infoline( "Disable Visual and check texture not affected" );
1491   dummyImpl.EnableVisual( DummyControl::Property::TEST_VISUAL, false );
1492   application.SendNotification();
1493   application.Render(0);
1494   tet_infoline( "Check renderer is destroyed when visual off stage" );
1495   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1496   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION );
1497   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
1498   textureTrace.Reset();
1499
1500   tet_infoline( "Re-enable Visual and check texture not affected" );
1501   dummyImpl.EnableVisual( DummyControl::Property::TEST_VISUAL, true );
1502   application.SendNotification();
1503   application.Render(0);
1504   tet_infoline( "Check texture not affected and renderer is destroyed when visual off stage" );
1505   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
1506   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION );
1507   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
1508
1509   END_TEST;
1510 }
1511
1512 int UtcDaliImageVisualReleasePolicy07(void)
1513 {
1514   ToolkitTestApplication application;
1515   tet_infoline( "UtcDaliImageVisualReleasePolicy07 Two visuals with different policies sharing a texture DETACHED and DESTROYED" );
1516
1517   tet_infoline( "Create first visual with DESTROYED release policy" );
1518   Visual::Base imageVisualDestroyed = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, DevelImageVisual::Property::RELEASE_POLICY, DevelImageVisual::ReleasePolicy::DESTROYED );
1519
1520
1521   tet_infoline( "Create second visual with DETACHED release policy");
1522   Visual::Base imageVisualDetached = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, DevelImageVisual::Property::RELEASE_POLICY, DevelImageVisual::ReleasePolicy::DETACHED );
1523
1524   // Set up trace debug
1525   TestGlAbstraction& gl = application.GetGlAbstraction();
1526   TraceCallStack& textureTrace = gl.GetTextureTrace();
1527   textureTrace.Enable(true);
1528
1529   tet_infoline( "Register visuals with control and ensure it has the only handles" );
1530   DummyControl actor = DummyControl::New(true);
1531   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1532   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisualDestroyed );
1533   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL2, imageVisualDetached );
1534   imageVisualDestroyed.Reset(); // reduce ref count so only the control keeps the visual alive.
1535   imageVisualDetached.Reset(); // reduce ref count so only the control keeps the visual alive.
1536
1537   actor.SetSize(200.f, 200.f);
1538
1539   // Test initially zero renderers
1540   application.SendNotification();
1541   application.Render(0);
1542   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1543   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
1544
1545   Stage::GetCurrent().Add( actor );
1546
1547   // Wait for image to load
1548   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1549
1550   application.SendNotification();
1551   application.Render(0);
1552   tet_infoline( "Ensure a texture is created, shared amongst both visuals.  Each visual has its own renderer" );
1553   DALI_TEST_EQUALS( actor.GetRendererCount(), 2u, TEST_LOCATION );
1554   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
1555
1556   // Test renderer removed when visual destroyed
1557   DALI_TEST_CHECK( actor.GetRendererCount() == 2u );
1558   dummyImpl.EnableVisual( DummyControl::Property::TEST_VISUAL2, false );  // TEST_VISUAL2 no longer requires the texture as release policy DETACHED
1559   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
1560   application.SendNotification();
1561   application.Render();
1562
1563   // Test texture was not deleted as TEST_VISUAL release policy is DESTROYED and is still required.
1564   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION );
1565
1566   dummyImpl.EnableVisual( DummyControl::Property::TEST_VISUAL, false );
1567   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1568   application.SendNotification();
1569   application.Render();
1570
1571   tet_infoline( "Ensure a texture is not deleted as second visual used the DESTROYED release policy" );
1572   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION );
1573
1574   END_TEST;
1575 }
1576
1577 int UtcDaliImageVisualLoadPolicy01(void)
1578 {
1579   ToolkitTestApplication application;
1580   tet_infoline( "UtcDaliImageVisualLoadPolicy01 Load a visual image before attaching to stage" );
1581
1582   // Set up trace debug
1583   TestGlAbstraction& gl = application.GetGlAbstraction();
1584   TraceCallStack& textureTrace = gl.GetTextureTrace();
1585   textureTrace.Enable(true);
1586
1587   tet_infoline( "Create visual with IMMEDIATE load policy" );
1588   Visual::Base imageVisual = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, DevelImageVisual::Property::LOAD_POLICY, DevelImageVisual::LoadPolicy::IMMEDIATE );
1589
1590   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1591
1592   // Ensure texture has been uploaded
1593   application.SendNotification();
1594   application.Render();
1595
1596   tet_infoline( "Ensure texture loading starts after visual created" );
1597   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
1598   textureTrace.Reset();
1599
1600   tet_infoline( "Register visuals with control and ensure it has the only handles" );
1601   DummyControl actor = DummyControl::New(true);
1602   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1603   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual );
1604   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
1605
1606   actor.SetSize(200.f, 200.f);
1607   Stage::GetCurrent().Add( actor );
1608   tet_infoline( "Ensure nothing triggers another load as texure already loaded" );
1609   const unsigned int TIME_OUT_3_SECONDS = 3;
1610   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1, TIME_OUT_3_SECONDS ), false, TEST_LOCATION );
1611
1612   application.SendNotification();
1613   application.Render();
1614
1615   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1616   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
1617
1618   // Ensure texture is deleted when no longer needed (ref count was correct )
1619   dummyImpl.UnregisterVisual( DummyControl::Property::TEST_VISUAL );
1620
1621   application.SendNotification();
1622   application.Render();
1623
1624   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
1625   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION );
1626
1627   END_TEST;
1628 }
1629
1630 int UtcDaliImageVisualLoadPolicy02(void)
1631 {
1632   ToolkitTestApplication application;
1633   tet_infoline( "UtcDaliImageVisualLoadPolicy01 Load a visual image only after attached to stage" );
1634
1635   // Set up trace debug
1636   TestGlAbstraction& gl = application.GetGlAbstraction();
1637   TraceCallStack& textureTrace = gl.GetTextureTrace();
1638   textureTrace.Enable(true);
1639
1640   tet_infoline( "Create visual with IMMEDIATE load policy" );
1641   Visual::Base imageVisual = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, DevelImageVisual::Property::LOAD_POLICY, DevelImageVisual::LoadPolicy::ATTACHED );
1642
1643   const unsigned int TIME_OUT_3_SECONDS = 3;
1644   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1, TIME_OUT_3_SECONDS ), false, TEST_LOCATION );
1645
1646   // Act on meeage queue even although nothing expected to load
1647   application.SendNotification();
1648   application.Render();
1649
1650   tet_infoline( "Ensure texture is not generated yet" );
1651   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
1652   textureTrace.Reset();
1653
1654   tet_infoline( "Register visuals with control and ensure it has the only handles" );
1655   DummyControl actor = DummyControl::New(true);
1656   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1657   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual );
1658   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
1659
1660   actor.SetSize(200.f, 200.f);
1661   Stage::GetCurrent().Add( actor );
1662   tet_infoline( "Allow image time to load" );
1663   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1664
1665   application.SendNotification();
1666   application.Render();
1667
1668   tet_infoline( "Ensure texture generated and renderer created" );
1669   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1670   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
1671
1672   // Ensure texture is delete when no longer needed
1673   dummyImpl.UnregisterVisual( DummyControl::Property::TEST_VISUAL );
1674
1675   application.SendNotification();
1676   application.Render();
1677
1678   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
1679   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION );
1680
1681   END_TEST;
1682 }
1683
1684 int UtcDaliImageVisualLoadPolicy03(void)
1685 {
1686   ToolkitTestApplication application;
1687   tet_infoline( "UtcDaliImageVisualLoadPolicy03 Load a visual image before attaching to stage and receive ResourceReady signal" );
1688
1689   // Set up trace debug
1690   TestGlAbstraction& gl = application.GetGlAbstraction();
1691   TraceCallStack& textureTrace = gl.GetTextureTrace();
1692   textureTrace.Enable(true);
1693
1694   tet_infoline( "Create visual with IMMEDIATE load policy" );
1695   Visual::Base imageVisual = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, DevelImageVisual::Property::LOAD_POLICY, DevelImageVisual::LoadPolicy::IMMEDIATE );
1696
1697   // Wait for image to load, ResourceReady signal will not be emitted until Visual is registered with a control and on stage.
1698   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1699
1700   // Ensure texture has been uploaded
1701   application.SendNotification();
1702   application.Render();
1703
1704   tet_infoline( "Ensure texture loading starts after visual created" );
1705   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
1706   textureTrace.Reset();
1707
1708   tet_infoline( "Register visuals with control and ensure it has the only handles" );
1709   DummyControl actor = DummyControl::New(true);
1710   actor.ResourceReadySignal().Connect( &ResourceReadySignal);
1711   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1712
1713   tet_infoline( "Registering visual attaches it to stage and trigger the loading signal if Image loaded" );
1714   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual );
1715   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
1716   actor.SetSize(200.f, 200.f);
1717   // Adding the Control hence Visual to stage will cause the Visual to trigger ResourceReadySignal if the image is already loaded.
1718   Stage::GetCurrent().Add( actor ); // If LoadPolicy was not IMMEDIATE then as this point (after attached to stage) the test would need to wait for Loading
1719
1720   DALI_TEST_EQUALS( gResourceReadySignalFired, true, TEST_LOCATION );
1721
1722   END_TEST;
1723 }
1724
1725 int UtcDaliImageVisualOrientationCorrection(void)
1726 {
1727   ToolkitTestApplication application;
1728   tet_infoline( "UtcDaliImageVisualOrientationCorrection Enabling OrientationCorrection should rotate an image with exif (90deg) orientation data with requested" );
1729
1730   VisualFactory factory = VisualFactory::Get();
1731   tet_infoline( "Create visual with Orientation correction set OFF" );
1732   Property::Map propertyMap;
1733   propertyMap.Insert( Visual::Property::TYPE,  Visual::IMAGE );
1734   propertyMap.Insert( ImageVisual::Property::URL, TEST_ROTATED_IMAGE );
1735   propertyMap.Insert( DevelImageVisual::Property::ORIENTATION_CORRECTION, false );
1736   Visual::Base imageVisual = factory.CreateVisual( propertyMap );
1737
1738   tet_infoline( "Create control for visual, need to loaded it" );
1739   DummyControl actor = DummyControl::New(true);
1740   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1741   Stage::GetCurrent().Add( actor );
1742
1743   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual );
1744   // Wait for image to load
1745   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1746
1747   Vector2 originalImageSize;
1748   tet_infoline( "Get size of original visual to compare later with rotated image" );
1749   imageVisual.GetNaturalSize( originalImageSize );
1750   DALI_TEST_GREATER( originalImageSize.width, originalImageSize.height, TEST_LOCATION ); // Width and Height must be different for this test.
1751   imageVisual.Reset();  // remove handle so can unregister it and remove from cache
1752   dummyImpl.UnregisterVisual( DummyControl::Property::TEST_VISUAL );
1753   application.SendNotification();
1754   application.Render();
1755
1756   tet_infoline( "Create visual with Orientation correction set ON " );
1757   propertyMap.Clear();
1758   propertyMap.Insert( Visual::Property::TYPE,  Visual::IMAGE );
1759   propertyMap.Insert( ImageVisual::Property::URL, TEST_ROTATED_IMAGE );
1760   propertyMap.Insert( DevelImageVisual::Property::ORIENTATION_CORRECTION, true );
1761   imageVisual = factory.CreateVisual( propertyMap );
1762
1763   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual );
1764   // Wait for image to load
1765   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1766
1767   Vector2 rotatedImageSize;
1768   imageVisual.GetNaturalSize( rotatedImageSize );
1769   tet_infoline( "Confirm that visual has rotated" );
1770   DALI_TEST_EQUALS( originalImageSize.width, rotatedImageSize.height , TEST_LOCATION );
1771   DALI_TEST_EQUALS( originalImageSize.height, rotatedImageSize.width , TEST_LOCATION );
1772
1773   Property::Map resultMap;
1774   imageVisual.CreatePropertyMap( resultMap );
1775
1776   // check the Property::ORIENTATION_CORRECTION value from the returned map
1777   Property::Value* typeValue = resultMap.Find( DevelImageVisual::Property::ORIENTATION_CORRECTION,  Property::BOOLEAN );
1778   DALI_TEST_EQUALS( typeValue->Get<bool>(), true, TEST_LOCATION );
1779
1780   END_TEST;
1781 }