Updated test files to match dali-core Pipeline VtxFmt
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-ImageVisual.cpp
1 /*
2  * Copyright (c) 2021 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 <vector>
20 #include <dali-toolkit-test-suite-utils.h>
21 #include <toolkit-timer.h>
22 #include <toolkit-event-thread-callback.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_BROKEN_IMAGE_FILE_NAME =  TEST_RESOURCE_DIR  "/a-random-nonimage.jpg";
47 const char* TEST_LARGE_IMAGE_FILE_NAME =  TEST_RESOURCE_DIR "/tbcol.png";
48 const char* TEST_SMALL_IMAGE_FILE_NAME = TEST_RESOURCE_DIR "/icon-edit.png";
49 const char* TEST_REMOTE_IMAGE_FILE_NAME = "https://www.tizen.org/sites/all/themes/tizen_theme/logo.png";
50 const char* TEST_INVALID_FILE_NAME =  TEST_RESOURCE_DIR  "/invalid.jpg";
51 const char* TEST_REMOTE_INVALID_FILE_NAME = "https://www.tizen.org/invalid.png";
52 const char* TEST_MASK_IMAGE_FILE_NAME =  TEST_RESOURCE_DIR "/mask.png";
53 const char* TEST_ROTATED_IMAGE =  TEST_RESOURCE_DIR  "/keyboard-Landscape.jpg";
54
55
56 bool gResourceReadySignalFired = false;
57 std::vector<int> gReadyIds = {};
58 void ResourceReadySignal( Control control )
59 {
60   gResourceReadySignalFired = true;
61   gReadyIds.push_back(control.GetProperty< int >( Actor::Property::ID ));
62 }
63 void ClearReadyIds()
64 {
65   gReadyIds.clear();
66 }
67
68 Actor CreateActorWithImageVisual(const Property::Map& map)
69 {
70   VisualFactory factory = VisualFactory::Get();
71   DummyControl actor = DummyControl::New();
72   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
73   Visual::Base visual = factory.CreateVisual( map );
74   DALI_TEST_CHECK( visual );
75   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
76   actor.SetProperty( Actor::Property::SIZE, Vector2( 200.f, 200.f ) );
77   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
78   return actor;
79 }
80
81
82 Visual::Base CreateVisualWithPolicy( const char* url, Property::Index key, const Property::Value& value )
83 {
84   VisualFactory factory = VisualFactory::Get();
85
86   Property::Map propertyMap;
87   propertyMap.Insert( Visual::Property::TYPE,  Visual::IMAGE );
88   propertyMap.Insert( ImageVisual::Property::URL,  url );
89   propertyMap.Insert( ImageVisual::Property::DESIRED_WIDTH,   20 );
90   propertyMap.Insert( ImageVisual::Property::DESIRED_HEIGHT,   30 );
91   propertyMap.Insert( key , value );
92
93   return factory.CreateVisual( propertyMap );
94 }
95
96 } // namespace
97
98 void TestVisualRender( ToolkitTestApplication& application,
99                        DummyControl& actor,
100                        Visual::Base& visual,
101                        std::size_t expectedSamplers = 0,
102                        ImageDimensions imageDimensions = ImageDimensions(),
103                        Integration::ResourcePointer resourcePtr = Integration::ResourcePointer())
104 {
105   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
106   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
107
108   if( resourcePtr )
109   {
110     // set the image size, for test case, this needs to be set before loading started
111     application.GetPlatform().SetClosestImageSize(  Vector2(imageDimensions.GetWidth(), imageDimensions.GetHeight()) );
112   }
113
114   actor.SetProperty( Actor::Property::SIZE, Vector2( 200.f, 200.f ) );
115   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
116
117   application.GetScene().Add( actor );
118
119   application.SendNotification(); // Send messages to update
120   application.Render();           // process update and render
121   application.SendNotification(); // process any signals to event
122
123   if( resourcePtr )
124   {
125     DALI_TEST_EQUALS( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceSynchronouslyFunc ), true, TEST_LOCATION);
126   }
127
128   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
129 }
130
131 static void TestMixColor( Visual::Base visual, Property::Index mixColorIndex, const Vector4& testColor )
132 {
133   Property::Map map;
134   visual.CreatePropertyMap(map);
135   Property::Value* value = map.Find( mixColorIndex );
136   DALI_TEST_CHECK( value );
137   Vector3 mixColor1;
138   DALI_TEST_CHECK( value->Get( mixColor1 ) );
139   DALI_TEST_EQUALS( mixColor1, Vector3(testColor), 0.001, TEST_LOCATION );
140
141   value = map.Find( Visual::Property::MIX_COLOR );
142   DALI_TEST_CHECK( value );
143   Vector4 mixColor2;
144   DALI_TEST_CHECK( value->Get( mixColor2 ) );
145   DALI_TEST_EQUALS( mixColor2, testColor, 0.001, TEST_LOCATION );
146
147   value = map.Find( Visual::Property::OPACITY );
148   DALI_TEST_CHECK( value );
149   float opacity;
150   DALI_TEST_CHECK( value->Get( opacity ) );
151   DALI_TEST_EQUALS( opacity, testColor.a, 0.001, TEST_LOCATION );
152 }
153
154
155
156 int UtcDaliImageVisualPropertyMap(void)
157 {
158   ToolkitTestApplication application;
159   tet_infoline( "Request image visual with a Property::Map" );
160
161   VisualFactory factory = VisualFactory::Get();
162   DALI_TEST_CHECK( factory );
163   factory.SetPreMultiplyOnLoad( true );
164
165   Property::Map propertyMap;
166   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
167   propertyMap.Insert( ImageVisual::Property::URL,  TEST_LARGE_IMAGE_FILE_NAME );
168
169   Visual::Base visual = factory.CreateVisual( propertyMap );
170   DALI_TEST_CHECK( visual );
171
172   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
173   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
174
175   TestGlAbstraction& gl = application.GetGlAbstraction();
176   TraceCallStack& textureTrace = gl.GetTextureTrace();
177   textureTrace.Enable(true);
178
179   DummyControl actor = DummyControl::New();
180   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
181   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
182
183   actor.SetProperty( Actor::Property::SIZE, Vector2( 200.f, 200.f ) );
184   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
185
186   application.GetScene().Add( actor );
187   application.SendNotification();
188   application.Render();
189
190   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
191
192   application.SendNotification();
193   application.Render();
194
195   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
196   auto renderer = actor.GetRendererAt(0);
197   auto preMultipliedIndex = renderer.GetPropertyIndex( "preMultipliedAlpha" );
198   DALI_TEST_CHECK( preMultipliedIndex != Property::INVALID_INDEX );
199   auto preMultipliedAlpha = renderer.GetProperty<float>( preMultipliedIndex );
200   auto preMultipliedAlpha2 = renderer.GetProperty<bool>( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA );
201   DALI_TEST_EQUALS( preMultipliedAlpha, 1.0f, TEST_LOCATION );
202   DALI_TEST_EQUALS( preMultipliedAlpha2, true, TEST_LOCATION );
203   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
204
205   application.GetScene().Remove( actor );
206   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
207
208   END_TEST;
209 }
210
211
212 int UtcDaliImageVisualNoPremultipliedAlpha01(void)
213 {
214   ToolkitTestApplication application;
215   tet_infoline( "Request image visual without pre-multiplied alpha" );
216
217   VisualFactory factory = VisualFactory::Get();
218   DALI_TEST_CHECK( factory );
219   factory.SetPreMultiplyOnLoad( false );
220
221   Property::Map propertyMap;
222   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
223   propertyMap.Insert( ImageVisual::Property::URL,  TEST_LARGE_IMAGE_FILE_NAME );
224
225   Visual::Base visual = factory.CreateVisual( propertyMap );
226   DALI_TEST_CHECK( visual );
227
228   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
229   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
230
231   TestGlAbstraction& gl = application.GetGlAbstraction();
232   TraceCallStack& textureTrace = gl.GetTextureTrace();
233   textureTrace.Enable(true);
234
235   DummyControl actor = DummyControl::New();
236   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
237   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
238
239   actor.SetProperty( Actor::Property::SIZE, Vector2( 200.f, 200.f ) );
240   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
241
242   application.GetScene().Add( actor );
243   application.SendNotification();
244   application.Render();
245
246   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
247
248   application.SendNotification();
249   application.Render();
250
251   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
252   auto renderer = actor.GetRendererAt(0);
253   auto preMultipliedIndex = renderer.GetPropertyIndex( "preMultipliedAlpha" );
254   DALI_TEST_CHECK( preMultipliedIndex != Property::INVALID_INDEX );
255   auto preMultipliedAlpha = renderer.GetProperty<bool>( preMultipliedIndex );
256   auto preMultipliedAlpha2 = renderer.GetProperty<bool>( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA );
257
258   DALI_TEST_EQUALS( preMultipliedAlpha, false, TEST_LOCATION );
259   DALI_TEST_EQUALS( preMultipliedAlpha2, false, TEST_LOCATION );
260
261   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
262
263   application.GetScene().Remove( actor );
264   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
265
266   END_TEST;
267 }
268
269
270 int UtcDaliImageVisualNoPremultipliedAlpha02(void)
271 {
272   ToolkitTestApplication application;
273   tet_infoline( "Request image visual with no alpha channel" );
274
275   VisualFactory factory = VisualFactory::Get();
276   DALI_TEST_CHECK( factory );
277
278   Property::Map propertyMap;
279   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
280   propertyMap.Insert( ImageVisual::Property::URL,  TEST_IMAGE_FILE_NAME );
281
282   Visual::Base visual = factory.CreateVisual( propertyMap );
283   DALI_TEST_CHECK( visual );
284
285   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
286   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
287
288   TestGlAbstraction& gl = application.GetGlAbstraction();
289   TraceCallStack& textureTrace = gl.GetTextureTrace();
290   textureTrace.Enable(true);
291
292   DummyControl actor = DummyControl::New();
293   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
294   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
295
296   actor.SetProperty( Actor::Property::SIZE, Vector2( 200.f, 200.f ) );
297   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
298
299   application.GetScene().Add( actor );
300   application.SendNotification();
301   application.Render();
302
303   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
304
305   application.SendNotification();
306   application.Render();
307
308   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
309   auto renderer = actor.GetRendererAt(0);
310   auto preMultipliedIndex = renderer.GetPropertyIndex( "preMultipliedAlpha" );
311   DALI_TEST_CHECK( preMultipliedIndex != Property::INVALID_INDEX );
312   auto preMultipliedAlpha = renderer.GetProperty<bool>( preMultipliedIndex );
313   auto preMultipliedAlpha2 = renderer.GetProperty<bool>( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA );
314
315   DALI_TEST_EQUALS( preMultipliedAlpha, false, TEST_LOCATION );
316   DALI_TEST_EQUALS( preMultipliedAlpha2, false, TEST_LOCATION );
317
318   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
319
320   int srcFactorRgb    = renderer.GetProperty<int>( Renderer::Property::BLEND_FACTOR_SRC_RGB );
321   int destFactorRgb   = renderer.GetProperty<int>( Renderer::Property::BLEND_FACTOR_DEST_RGB );
322   int srcFactorAlpha  = renderer.GetProperty<int>( Renderer::Property::BLEND_FACTOR_SRC_ALPHA );
323   int destFactorAlpha = renderer.GetProperty<int>( Renderer::Property::BLEND_FACTOR_DEST_ALPHA );
324   DALI_TEST_CHECK( srcFactorRgb == BlendFactor::SRC_ALPHA );
325   DALI_TEST_CHECK( destFactorRgb == BlendFactor::ONE_MINUS_SRC_ALPHA );
326   DALI_TEST_CHECK( srcFactorAlpha == BlendFactor::ONE );
327   DALI_TEST_CHECK( destFactorAlpha == BlendFactor::ONE_MINUS_SRC_ALPHA );
328
329   textureTrace.Reset();
330
331   // Make a new visual with the same image
332   Visual::Base newVisual = factory.CreateVisual( propertyMap );
333   DALI_TEST_CHECK( newVisual );
334
335   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
336   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
337
338   DummyControl newActor = DummyControl::New();
339   DummyControlImpl& newDummyImpl = static_cast< DummyControlImpl& >( newActor.GetImplementation() );
340   newDummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, newVisual );
341
342   newActor.SetProperty( Actor::Property::SIZE, Vector2( 200.f, 200.f ) );
343   DALI_TEST_EQUALS( newActor.GetRendererCount(), 0u, TEST_LOCATION );
344
345   application.GetScene().Add( newActor );
346
347   application.SendNotification();
348   application.Render();
349
350   DALI_TEST_EQUALS( newActor.GetRendererCount(), 1u, TEST_LOCATION );
351   auto newRenderer = newActor.GetRendererAt( 0 );
352   preMultipliedIndex = newRenderer.GetPropertyIndex( "preMultipliedAlpha" );
353   DALI_TEST_CHECK( preMultipliedIndex != Property::INVALID_INDEX );
354   preMultipliedAlpha = newRenderer.GetProperty< bool >( preMultipliedIndex );
355   preMultipliedAlpha2 = newRenderer.GetProperty< bool >( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA );
356
357   DALI_TEST_EQUALS( preMultipliedAlpha, false, TEST_LOCATION );
358   DALI_TEST_EQUALS( preMultipliedAlpha2, false, TEST_LOCATION );
359
360   srcFactorRgb    = newRenderer.GetProperty<int>( Renderer::Property::BLEND_FACTOR_SRC_RGB );
361   destFactorRgb   = newRenderer.GetProperty<int>( Renderer::Property::BLEND_FACTOR_DEST_RGB );
362   srcFactorAlpha  = newRenderer.GetProperty<int>( Renderer::Property::BLEND_FACTOR_SRC_ALPHA );
363   destFactorAlpha = newRenderer.GetProperty<int>( Renderer::Property::BLEND_FACTOR_DEST_ALPHA );
364   DALI_TEST_CHECK( srcFactorRgb == BlendFactor::SRC_ALPHA );
365   DALI_TEST_CHECK( destFactorRgb == BlendFactor::ONE_MINUS_SRC_ALPHA );
366   DALI_TEST_CHECK( srcFactorAlpha == BlendFactor::ONE );
367   DALI_TEST_CHECK( destFactorAlpha == BlendFactor::ONE_MINUS_SRC_ALPHA );
368
369   application.GetScene().Remove( actor );
370   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
371
372   END_TEST;
373 }
374
375
376 int UtcDaliImageVisualRemoteImageLoad(void)
377 {
378   ToolkitTestApplication application;
379   tet_infoline( "Request remote image visual with a Property::Map" );
380
381   VisualFactory factory = VisualFactory::Get();
382   DALI_TEST_CHECK( factory );
383
384   Property::Map propertyMap;
385   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
386   propertyMap.Insert( ImageVisual::Property::URL,  TEST_REMOTE_IMAGE_FILE_NAME );
387
388   Visual::Base visual = factory.CreateVisual( propertyMap );
389   DALI_TEST_CHECK( visual );
390
391   TestGlAbstraction& gl = application.GetGlAbstraction();
392   TraceCallStack& textureTrace = gl.GetTextureTrace();
393   textureTrace.Enable(true);
394
395   DummyControl actor = DummyControl::New();
396   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
397   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
398
399   actor.SetProperty( Actor::Property::SIZE, Vector2( 200.f, 200.f ) );
400   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
401
402   application.GetScene().Add( actor );
403   application.SendNotification();
404
405   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
406
407   application.SendNotification();
408   application.Render();
409
410   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
411   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
412
413   application.GetScene().Remove( actor );
414   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
415
416   END_TEST;
417 }
418
419 int UtcDaliImageVisualTextureReuse1(void)
420 {
421   ToolkitTestApplication application;
422   tet_infoline( "Request remote image visual with a Property::Map; request a second visual with the same property map - should reuse texture" );
423
424   Property::Map propertyMap;
425   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
426   propertyMap.Insert( ImageVisual::Property::URL, TEST_LARGE_IMAGE_FILE_NAME );
427   propertyMap.Insert( ImageVisual::Property::RELEASE_POLICY,  ImageVisual::ReleasePolicy::DETACHED );
428
429   TestGlAbstraction& gl = application.GetGlAbstraction();
430   TraceCallStack& textureTrace = gl.GetTextureTrace();
431   textureTrace.Enable(true);
432   TraceCallStack& drawTrace = gl.GetDrawTrace();
433   drawTrace.Enable(true);
434
435   Actor actor = CreateActorWithImageVisual( propertyMap );
436   application.GetScene().Add( actor );
437   application.SendNotification();
438
439   // Wait for image to load
440   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
441
442   application.SendNotification();
443   application.Render();
444
445   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
446   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
447   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
448   DALI_TEST_EQUALS( drawTrace.FindMethod("DrawArrays"), true, TEST_LOCATION );
449   textureTrace.Reset();
450   drawTrace.Reset();
451
452   Actor actor2 = CreateActorWithImageVisual( propertyMap );
453   application.GetScene().Add(actor2);
454
455   application.SendNotification(); // Send messages to update
456   application.Render();           // process update and render
457   application.SendNotification(); // process any signals to event
458
459   DALI_TEST_EQUALS( actor2.GetRendererCount(), 1u, TEST_LOCATION );
460
461   tet_infoline("Test that 2 draw calls occur with no new texture gens/binds, i.e. both\n"
462                "draw calls use the same texture as the previous draw call\n" );
463
464   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
465   DALI_TEST_EQUALS( drawTrace.CountMethod("DrawArrays"), 2, TEST_LOCATION );
466   // TODO: Temporarily commented out the line below when caching is disabled. Will need to add it back.
467 //  DALI_TEST_EQUALS( textureTrace.CountMethod("BindTexture"), 0, TEST_LOCATION );
468
469   tet_infoline("Test that removing 1 actor doesn't delete the texture\n");
470
471   application.GetScene().Remove( actor );
472   application.SendNotification();
473   application.Render();
474
475   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
476   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION );
477
478   tet_infoline("Test that removing last actor does delete the texture\n");
479
480   application.GetScene().Remove( actor2 ); // Detaches remaining ImageVisual
481   application.SendNotification();
482   application.Render();
483
484   DALI_TEST_CHECK( actor2.GetRendererCount() == 0u );
485   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION );
486
487   END_TEST;
488 }
489
490
491 int UtcDaliImageVisualTextureReuse2(void)
492 {
493   ToolkitTestApplication application;
494   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" );
495
496   Property::Map propertyMap;
497   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
498   propertyMap.Insert( ImageVisual::Property::URL, TEST_REMOTE_IMAGE_FILE_NAME );
499
500   TestGlAbstraction& gl = application.GetGlAbstraction();
501   TraceCallStack& textureTrace = gl.GetTextureTrace();
502   textureTrace.Enable(true);
503   TraceCallStack& drawTrace = gl.GetDrawTrace();
504   drawTrace.Enable(true);
505
506   Actor actor = CreateActorWithImageVisual( propertyMap );
507   application.GetScene().Add( actor );
508   application.SendNotification();
509
510   // Wait for image to load
511   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
512
513   application.SendNotification();
514   application.Render();
515
516   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
517   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
518   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
519   DALI_TEST_EQUALS( drawTrace.FindMethod("DrawArrays"), true, TEST_LOCATION );
520   textureTrace.Reset();
521   drawTrace.Reset();
522
523   propertyMap.Insert( ImageVisual::Property::SAMPLING_MODE, Dali::SamplingMode::NEAREST );
524   propertyMap.Insert( ImageVisual::Property::DESIRED_WIDTH, 100 );
525   propertyMap.Insert( ImageVisual::Property::DESIRED_HEIGHT, 100 );
526   Actor actor2 = CreateActorWithImageVisual( propertyMap );
527   application.GetScene().Add(actor2);
528
529   application.SendNotification();
530
531   // Wait for image to load
532   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
533
534   application.SendNotification();
535   application.Render();
536
537   DALI_TEST_EQUALS( actor2.GetRendererCount(), 1u, TEST_LOCATION );
538
539   tet_infoline("Test that 2 draw calls occur with 1 new texture gen/bind, i.e. both "
540                "renderers are using different textures\n" );
541
542   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
543   DALI_TEST_EQUALS( drawTrace.CountMethod("DrawArrays"), 2, TEST_LOCATION );
544   TraceCallStack::NamedParams tex1;
545   tex1["texture"] << 1;
546   TraceCallStack::NamedParams tex2;
547   tex2["texture"] << 2;
548   DALI_TEST_EQUALS( textureTrace.FindMethodAndParams("BindTexture", tex1), true, TEST_LOCATION );
549   DALI_TEST_EQUALS( textureTrace.FindMethodAndParams("BindTexture", tex2), true, TEST_LOCATION );
550
551   tet_infoline("Test that removing 1 actor deletes it's texture\n");
552
553   application.GetScene().Remove( actor );
554   application.SendNotification();
555   application.Render();
556
557   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
558   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION );
559
560   tet_infoline("Test that removing last actor deletes it's texture\n");
561
562   application.GetScene().Remove( actor2 );
563   application.SendNotification();
564   application.Render();
565
566   DALI_TEST_CHECK( actor2.GetRendererCount() == 0u );
567   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 2, TEST_LOCATION );
568
569   END_TEST;
570 }
571
572
573 int UtcDaliImageVisualCustomWrapModePixelArea(void)
574 {
575   ToolkitTestApplication application;
576   tet_infoline( "Request image visual with a Property::Map, test custom wrap mode and pixel area with atlasing" );
577
578   VisualFactory factory = VisualFactory::Get();
579   DALI_TEST_CHECK( factory );
580
581   // Test wrap mode with atlasing. Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
582   const int width=34;
583   const int height=34;
584   const Vector4 pixelArea(-0.5f, -0.5f, 2.f, 2.f);
585
586   Property::Map propertyMap;
587   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
588   propertyMap.Insert( ImageVisual::Property::URL, TEST_SMALL_IMAGE_FILE_NAME );
589   propertyMap.Insert( ImageVisual::Property::DESIRED_WIDTH, width );
590   propertyMap.Insert( ImageVisual::Property::DESIRED_HEIGHT, height );
591   propertyMap.Insert( ImageVisual::Property::SYNCHRONOUS_LOADING, true );
592   propertyMap.Insert( ImageVisual::Property::PIXEL_AREA, pixelArea );
593   propertyMap.Insert( ImageVisual::Property::WRAP_MODE_U, WrapMode::MIRRORED_REPEAT );
594   propertyMap.Insert( ImageVisual::Property::WRAP_MODE_V, WrapMode::REPEAT );
595   propertyMap.Insert( ImageVisual::Property::ATLASING, true );
596
597   Visual::Base visual = factory.CreateVisual( propertyMap );
598   DALI_TEST_CHECK( visual );
599
600   TestGlAbstraction& gl = application.GetGlAbstraction();
601   TraceCallStack& textureTrace = gl.GetTextureTrace();
602   textureTrace.Enable(true);
603   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
604   texParameterTrace.Enable( true );
605
606   DummyControl actor = DummyControl::New();
607   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
608   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
609   actor.SetProperty( Actor::Property::SIZE, Vector2(2000, 2000) );
610   actor.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER);
611   application.GetScene().Add( actor );
612
613   // loading started
614   application.SendNotification();
615   application.Render();
616
617   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
618
619   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
620
621   // WITH atlasing, the wrapping is handled manually in shader, so the following gl function should not be called
622   std::stringstream out;
623   out << std::hex << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_S << ", " << GL_MIRRORED_REPEAT;
624   DALI_TEST_CHECK( !texParameterTrace.FindMethodAndParams("TexParameteri", out.str()) );
625   out.str("");
626   out << std::hex << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_T << ", " << GL_REPEAT;
627   DALI_TEST_CHECK( !texParameterTrace.FindMethodAndParams("TexParameteri", out.str()) );
628
629   // test the uniforms which used to handle the wrap mode
630   Renderer renderer = actor.GetRendererAt( 0u );
631   DALI_TEST_CHECK( renderer );
632
633   Property::Value pixelAreaValue = renderer.GetProperty( renderer.GetPropertyIndex( "pixelArea" ) );
634   DALI_TEST_EQUALS( pixelAreaValue.Get<Vector4>(), pixelArea, TEST_LOCATION );
635   Vector4 pixelAreaUniform;
636   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "pixelArea", pixelAreaUniform ) );
637   DALI_TEST_EQUALS( pixelArea, pixelAreaUniform, Math::MACHINE_EPSILON_100, TEST_LOCATION );
638
639   Property::Value wrapModeValue = renderer.GetProperty( renderer.GetPropertyIndex( "wrapMode" ) );
640   Vector2 wrapMode( WrapMode::MIRRORED_REPEAT-1, WrapMode::REPEAT-1 );
641   DALI_TEST_EQUALS( wrapModeValue.Get<Vector2>(), wrapMode, TEST_LOCATION );
642   Vector2 wrapModeUniform;
643   DALI_TEST_CHECK( gl.GetUniformValue<Vector2>( "wrapMode", wrapModeUniform ) );
644   DALI_TEST_EQUALS( wrapMode, wrapModeUniform, Math::MACHINE_EPSILON_100, TEST_LOCATION );
645
646   actor.Unparent( );
647   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
648
649   END_TEST;
650 }
651
652 int UtcDaliImageVisualCustomWrapModeNoAtlas(void)
653 {
654   ToolkitTestApplication application;
655   tet_infoline( "Request image visual with a Property::Map, test custom wrap mode and pixel area without atlasing" );
656
657   VisualFactory factory = VisualFactory::Get();
658   DALI_TEST_CHECK( factory );
659
660   // Test wrap mode without atlasing. Image with a size bigger than 512*512 will NOT be uploaded as a part of the atlas.
661   const int width=600;
662   const int height=600;
663   const Vector4 pixelArea(-0.5f, -0.5f, 2.f, 2.f);
664
665   Property::Map propertyMap;
666   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
667   propertyMap.Insert( ImageVisual::Property::URL, TEST_LARGE_IMAGE_FILE_NAME );
668   propertyMap.Insert( ImageVisual::Property::DESIRED_WIDTH, width );
669   propertyMap.Insert( ImageVisual::Property::DESIRED_HEIGHT, height );
670   propertyMap.Insert( ImageVisual::Property::SYNCHRONOUS_LOADING, true );
671   propertyMap.Insert( ImageVisual::Property::PIXEL_AREA, pixelArea );
672   propertyMap.Insert( ImageVisual::Property::WRAP_MODE_U, WrapMode::MIRRORED_REPEAT );
673   propertyMap.Insert( ImageVisual::Property::WRAP_MODE_V, WrapMode::REPEAT );
674
675   Visual::Base visual = factory.CreateVisual( propertyMap );
676   DALI_TEST_CHECK( visual );
677
678   TestGlAbstraction& gl = application.GetGlAbstraction();
679   TraceCallStack& textureTrace = gl.GetTextureTrace();
680   textureTrace.Enable(true);
681   textureTrace.EnableLogging(true);
682   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
683   texParameterTrace.Enable( true );
684   texParameterTrace.EnableLogging( true );
685
686   DummyControl actor = DummyControl::New();
687   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
688   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
689   actor.SetProperty( Actor::Property::SIZE, Vector2(2000, 2000) );
690   actor.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER);
691   application.GetScene().Add( actor );
692
693   // loading started
694   application.SendNotification();
695   application.Render();
696   application.SendNotification();
697
698   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
699
700   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
701
702   // WITHOUT atlasing, the wrapping is handled by setting gl texture parameters
703   std::stringstream out;
704   out << std::hex << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_S << ", " << GL_MIRRORED_REPEAT;
705   DALI_TEST_CHECK( texParameterTrace.FindMethodAndParams("TexParameteri", out.str()) );
706   out.str("");
707   out << std::hex << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_T << ", " << GL_REPEAT;
708   DALI_TEST_CHECK( texParameterTrace.FindMethodAndParams("TexParameteri", out.str()) );
709
710   // test the uniforms which used to handle the wrap mode
711   Renderer renderer = actor.GetRendererAt( 0u );
712   DALI_TEST_CHECK( renderer );
713
714   Property::Value pixelAreaValue = renderer.GetProperty( renderer.GetPropertyIndex( "pixelArea" ) );
715   DALI_TEST_EQUALS( pixelAreaValue.Get<Vector4>(), pixelArea, TEST_LOCATION );
716   Vector4 pixelAreaUniform;
717   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "pixelArea", pixelAreaUniform ) );
718   DALI_TEST_EQUALS( pixelArea, pixelAreaUniform, Math::MACHINE_EPSILON_100, TEST_LOCATION );
719
720   Property::Index wrapModeIndex = renderer.GetPropertyIndex( "wrapMode" );
721   DALI_TEST_CHECK(wrapModeIndex == Property::INVALID_INDEX);
722
723   actor.Unparent();
724   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
725
726   END_TEST;
727 }
728
729 int UtcDaliImageVisualAnimateMixColor(void)
730 {
731   ToolkitTestApplication application;
732   tet_infoline( "Animate mix color" );
733
734   application.GetPlatform().SetClosestImageSize( Vector2(100, 100) );
735
736   VisualFactory factory = VisualFactory::Get();
737   Property::Map propertyMap;
738   propertyMap.Insert(Visual::Property::TYPE,  Visual::IMAGE);
739   propertyMap.Insert(ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME );
740   propertyMap.Insert("mixColor", Color::BLUE);
741   propertyMap.Insert(ImageVisual::Property::SYNCHRONOUS_LOADING, true);
742   Visual::Base visual = factory.CreateVisual( propertyMap );
743
744   DummyControl actor = DummyControl::New(true);
745   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
746   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
747
748   actor.SetProperty( Actor::Property::SIZE, Vector2(2000, 2000) );
749   actor.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER);
750   actor.SetProperty( Actor::Property::COLOR,Color::BLACK);
751   application.GetScene().Add(actor);
752
753   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION);
754
755   Renderer renderer = actor.GetRendererAt(0);
756   Property::Index index = renderer.GetPropertyIndex( Visual::Property::MIX_COLOR );
757   Property::Value blendModeValue = renderer.GetProperty( Renderer::Property::BLEND_MODE );
758   DALI_TEST_EQUALS( blendModeValue.Get<int>(), (int)BlendMode::AUTO, TEST_LOCATION );
759
760   tet_infoline("Test that the renderer has the mixColor property");
761   DALI_TEST_CHECK( index != Property::INVALID_INDEX );
762
763   const Vector4 TARGET_MIX_COLOR( 1.0f, 0.0f, 0.0f, 0.5f );
764
765   Property::Map map;
766   map["target"] = "testVisual";
767   map["property"] = "mixColor";
768   map["initialValue"] = Color::MAGENTA;
769   map["targetValue"] = TARGET_MIX_COLOR;
770   map["animator"] = Property::Map()
771     .Add("alphaFunction", "LINEAR")
772     .Add("timePeriod", Property::Map()
773          .Add("delay", 0.0f)
774          .Add("duration", 4.0f));
775
776   Dali::Toolkit::TransitionData transition = TransitionData::New( map );
777
778   Animation animation = dummyImpl.CreateTransition( transition );
779
780   animation.AnimateTo( Property(actor, Actor::Property::COLOR), Color::WHITE );
781   animation.Play();
782
783   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
784   glAbstraction.EnableEnableDisableCallTrace( true );
785   TraceCallStack& glEnableStack = glAbstraction.GetEnableDisableTrace();
786   std::ostringstream blendStr;
787   blendStr << GL_BLEND;
788
789   application.SendNotification();
790   application.Render(0); // Ensure animation starts
791   application.Render(2000u); // Halfway point
792   Vector3 testColor( 1.0f, 0.0f, 0.5f );
793
794   // uColor.a should be actor's alpha * mixColor.a.
795   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<Vector4>( "uColor", Vector4( 0.5f, 0.5f, 0.5f, 0.75f ) ), true, TEST_LOCATION );
796   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<Vector3>( "mixColor", testColor ), true, TEST_LOCATION );
797
798   DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
799
800   glEnableStack.Reset();
801
802   application.Render(2000u); // Halfway point between blue and white
803
804   DALI_TEST_EQUALS( actor.GetCurrentProperty< Vector4 >( Actor::Property::COLOR ), Color::WHITE, TEST_LOCATION );
805   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<Vector4>( "uColor", Vector4( 1.0f, 1.0f, 1.0f, 0.5f ) ), true, TEST_LOCATION );
806   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<Vector3>( "mixColor", Vector3( TARGET_MIX_COLOR ) ), true, TEST_LOCATION );
807
808   // GL_BLEND should not be changed: Keep enabled
809   // TODO: Temporarily commented out the line below when caching is disabled. Will need to add it back.
810 //  DALI_TEST_CHECK( !glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
811   DALI_TEST_CHECK( !glEnableStack.FindMethodAndParams( "Disable", blendStr.str().c_str() ) );
812
813   TestMixColor( visual, Visual::Property::MIX_COLOR, TARGET_MIX_COLOR );
814
815   END_TEST;
816 }
817
818 int UtcDaliImageVisualAnimateOpacity(void)
819 {
820   ToolkitTestApplication application;
821   tet_infoline( "Animate image visual opacity" );
822
823   application.GetPlatform().SetClosestImageSize( Vector2(100, 100) );
824
825   VisualFactory factory = VisualFactory::Get();
826   Property::Map propertyMap;
827   propertyMap.Insert(Visual::Property::TYPE,  Visual::IMAGE);
828   propertyMap.Insert(ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME );
829   propertyMap.Insert("opacity", 0.5f);
830   propertyMap.Insert(ImageVisual::Property::SYNCHRONOUS_LOADING, true);
831   Visual::Base visual = factory.CreateVisual( propertyMap );
832
833   DummyControl actor = DummyControl::New(true);
834   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
835   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
836
837   actor.SetProperty( Actor::Property::SIZE, Vector2(2000, 2000) );
838   actor.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER);
839   actor.SetProperty( Actor::Property::COLOR,Color::BLACK);
840   application.GetScene().Add(actor);
841
842   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION);
843
844   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
845   glAbstraction.EnableEnableDisableCallTrace( true );
846   TraceCallStack& glEnableStack = glAbstraction.GetEnableDisableTrace();
847   std::ostringstream blendStr;
848   blendStr << GL_BLEND;
849
850   application.SendNotification();
851   application.Render();
852
853   DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
854
855   {
856     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." );
857
858     Property::Map map;
859     map["target"] = "testVisual";
860     map["property"] = "opacity";
861     map["targetValue"] = 1.0f;
862     map["animator"] = Property::Map()
863       .Add("alphaFunction", "LINEAR")
864       .Add("timePeriod", Property::Map()
865            .Add("delay", 0.0f)
866            .Add("duration", 4.0f));
867
868     Dali::Toolkit::TransitionData transition = TransitionData::New( map );
869     Animation animation = dummyImpl.CreateTransition( transition );
870     animation.Play();
871
872     glEnableStack.Reset();
873
874     application.SendNotification();
875     application.Render(0);     // Ensure animation starts
876     application.Render(2000u); // Halfway point through animation
877     application.SendNotification(); // Handle any signals
878
879     Vector4 color;
880     DALI_TEST_CHECK( application.GetGlAbstraction().GetUniformValue< Vector4 >( "uColor", color ) );
881     DALI_TEST_EQUALS( color.a, 0.75f, TEST_LOCATION );
882
883     application.Render(2001u); // end
884     application.SendNotification(); // ensure animation finished signal is sent
885
886     DALI_TEST_CHECK( application.GetGlAbstraction().GetUniformValue< Vector4 >( "uColor", color ) );
887     DALI_TEST_EQUALS( color.a, 1.0f, TEST_LOCATION );
888
889     // TODO: Temporarily commented out the line below when caching is disabled. Will need to add it back.
890 //    DALI_TEST_CHECK( !glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
891     DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Disable", blendStr.str().c_str() ) );
892   }
893
894
895   {
896     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." );
897
898     Property::Map map;
899     map["target"] = "testVisual";
900     map["property"] = Visual::Property::OPACITY;
901     map["targetValue"] = 0.1f;
902     map["animator"] = Property::Map()
903       .Add("alphaFunction", "LINEAR")
904       .Add("timePeriod", Property::Map()
905            .Add("delay", 0.0f)
906            .Add("duration", 4.0f));
907
908     Dali::Toolkit::TransitionData transition = TransitionData::New( map );
909     Animation animation = dummyImpl.CreateTransition( transition );
910     animation.Play();
911
912     glEnableStack.Reset();
913
914     application.SendNotification();
915     application.Render(0);     // Ensure animation starts
916     application.Render(2000u); // Halfway point
917     application.SendNotification();
918
919     Vector4 color;
920     DALI_TEST_CHECK( application.GetGlAbstraction().GetUniformValue< Vector4 >( "uColor", color ) );
921     DALI_TEST_EQUALS( color.a, 0.55f, TEST_LOCATION );
922
923     DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
924
925     glEnableStack.Reset();
926
927     application.Render(2016u); // end
928     application.SendNotification();
929
930     DALI_TEST_CHECK( application.GetGlAbstraction().GetUniformValue< Vector4 >( "uColor", color ) );
931     DALI_TEST_EQUALS( color.a, 0.1f, TEST_LOCATION );
932
933     // GL_BLEND should not be changed: Keep enabled
934     // TODO: Temporarily commented out the line below when caching is disabled. Will need to add it back.
935 //    DALI_TEST_CHECK( !glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
936     DALI_TEST_CHECK( !glEnableStack.FindMethodAndParams( "Disable", blendStr.str().c_str() ) );
937   }
938
939   END_TEST;
940 }
941
942
943
944 int UtcDaliImageVisualAnimateOpacity02(void)
945 {
946   ToolkitTestApplication application;
947   tet_infoline( "Animate image visual opacity" );
948
949   application.GetPlatform().SetClosestImageSize( Vector2(100, 100) );
950
951   VisualFactory factory = VisualFactory::Get();
952   Property::Map propertyMap;
953   propertyMap.Insert(Visual::Property::TYPE,  Visual::IMAGE);
954   propertyMap.Insert(ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME );
955   propertyMap.Insert("opacity", 0.5f);
956   propertyMap.Insert(ImageVisual::Property::SYNCHRONOUS_LOADING, true);
957   Visual::Base visual = factory.CreateVisual( propertyMap );
958
959   DummyControl actor = DummyControl::New(true);
960   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
961   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
962
963   actor.SetProperty( Actor::Property::SIZE, Vector2(2000, 2000) );
964   actor.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER);
965   actor.SetProperty( Actor::Property::COLOR,Color::BLACK);
966
967   tet_infoline( "Test that the opacity doesn't animate when actor not staged" );
968
969   Property::Array array;
970
971   Property::Map map;
972   map["target"] = "testVisual";
973   map["property"] = "opacity";
974   map["initialValue"] = 0.0f;
975   map["targetValue"] = 1.0f;
976   map["animator"] = Property::Map()
977     .Add("alphaFunction", "LINEAR")
978     .Add("timePeriod", Property::Map()
979          .Add("delay", 0.0f)
980          .Add("duration", 4.0f));
981
982   Property::Map map2;
983   map2["target"] = "testVisual";
984   map2["property"] = "size";
985   map2["targetValue"] = Vector2(1.0f, 1.0f);
986
987   array.Add( map ).Add(map2);
988
989   Dali::Toolkit::TransitionData transition = TransitionData::New( array );
990   Animation animation = dummyImpl.CreateTransition( transition );
991
992   application.GetScene().Add(actor);
993   application.SendNotification();
994   application.Render(0);     // Ensure animation starts
995
996   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION);
997
998   Renderer renderer = actor.GetRendererAt(0);
999   Property::Value blendModeValue = renderer.GetProperty( Renderer::Property::BLEND_MODE );
1000   DALI_TEST_EQUALS( blendModeValue.Get<int>(), (int)BlendMode::AUTO, TEST_LOCATION );
1001
1002   animation = dummyImpl.CreateTransition( transition );
1003   animation.Play();
1004
1005   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
1006   glAbstraction.EnableEnableDisableCallTrace( true );
1007   TraceCallStack& glEnableStack = glAbstraction.GetEnableDisableTrace();
1008   std::ostringstream blendStr;
1009   blendStr << GL_BLEND;
1010
1011   application.SendNotification();
1012   application.Render(0);     // Ensure animation starts
1013   application.Render(2000u); // Halfway point through animation
1014   application.SendNotification(); // Handle any signals
1015
1016   DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
1017
1018   Vector4 color;
1019   DALI_TEST_CHECK( application.GetGlAbstraction().GetUniformValue< Vector4 >( "uColor", color ) );
1020   DALI_TEST_EQUALS( color.a, 0.5f, TEST_LOCATION );
1021
1022   glEnableStack.Reset();
1023
1024   application.Render(2001u); // end
1025   application.SendNotification(); // ensure animation finished signal is sent
1026
1027   DALI_TEST_CHECK( application.GetGlAbstraction().GetUniformValue< Vector4 >( "uColor", color ) );
1028   DALI_TEST_EQUALS( color.a, 1.0f, TEST_LOCATION );
1029
1030   DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Disable", blendStr.str().c_str() ) );
1031
1032   END_TEST;
1033 }
1034
1035
1036
1037 int UtcDaliImageVisualAnimatePixelArea(void)
1038 {
1039   ToolkitTestApplication application;
1040   tet_infoline( "ImageVisual animate pixel area" );
1041
1042   application.GetPlatform().SetClosestImageSize( Vector2(100, 100) );
1043
1044   VisualFactory factory = VisualFactory::Get();
1045   Property::Map propertyMap;
1046   propertyMap.Insert(Visual::Property::TYPE,  Visual::IMAGE);
1047   propertyMap.Insert(ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME );
1048   propertyMap.Insert("mixColor", Color::BLUE);
1049   propertyMap.Insert(ImageVisual::Property::SYNCHRONOUS_LOADING, true);
1050   Visual::Base visual = factory.CreateVisual( propertyMap );
1051
1052   DummyControl actor = DummyControl::New(true);
1053   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1054   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
1055
1056   actor.SetProperty( Actor::Property::SIZE, Vector2(2000, 2000) );
1057   actor.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER);
1058   actor.SetProperty( Actor::Property::COLOR,Color::BLACK);
1059   application.GetScene().Add(actor);
1060
1061   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION);
1062
1063   Renderer renderer = actor.GetRendererAt(0);
1064   Property::Index index = renderer.GetPropertyIndex( Visual::Property::MIX_COLOR );
1065
1066   tet_infoline("Test that the renderer has the mixColor property");
1067   DALI_TEST_CHECK( index != Property::INVALID_INDEX );
1068
1069   // TransitionData only takes string keys
1070   Property::Map map;
1071   map["target"] = "testVisual";
1072   map["property"] = "pixelArea";
1073   map["initialValue"] = Vector4( 0,0,0,1 );
1074   map["targetValue"] = Vector4( 0,0,1,1 ); // Animate width from zero to full
1075   map["animator"] = Property::Map()
1076     .Add("alphaFunction", "LINEAR")
1077     .Add("timePeriod", Property::Map()
1078          .Add("delay", 0.0f)
1079          .Add("duration", 4.0f));
1080
1081   Dali::Toolkit::TransitionData transition = TransitionData::New( map );
1082
1083   Animation animation = dummyImpl.CreateTransition( transition );
1084   animation.AnimateTo( Property(actor, Actor::Property::COLOR), Color::WHITE );
1085   animation.Play();
1086
1087   application.SendNotification();
1088   application.Render(0);     // Ensure animation starts
1089   application.Render(2000u); // Halfway point
1090
1091   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<Vector4>("pixelArea", Vector4(0.0f, 0.0f, 0.5f, 1.0f )), true, TEST_LOCATION );
1092
1093   application.Render(2000u); // End of animation
1094
1095   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<Vector4>("pixelArea", Vector4( 0.0f, 0.0f, 1.0f, 1.0f )), true, TEST_LOCATION );
1096
1097   END_TEST;
1098 }
1099
1100 int UtcDaliImageVisualTextureCancelRemoteLoad(void)
1101 {
1102   ToolkitTestApplication application;
1103   tet_infoline( "Request remote image visual, then destroy visual to cancel load" );
1104
1105   Property::Map propertyMap;
1106   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
1107   propertyMap.Insert( ImageVisual::Property::URL, TEST_REMOTE_IMAGE_FILE_NAME );
1108
1109   TestGlAbstraction& gl = application.GetGlAbstraction();
1110   TraceCallStack& textureTrace = gl.GetTextureTrace();
1111   textureTrace.Enable(true);
1112   TraceCallStack& drawTrace = gl.GetDrawTrace();
1113   drawTrace.Enable(true);
1114
1115   Actor actor = CreateActorWithImageVisual( propertyMap );
1116   application.GetScene().Add( actor );
1117   application.SendNotification();
1118
1119   application.GetScene().Remove( actor );
1120   application.SendNotification();
1121
1122   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
1123   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
1124   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), false, TEST_LOCATION );
1125   DALI_TEST_EQUALS( drawTrace.FindMethod("DrawArrays"), false, TEST_LOCATION );
1126
1127   END_TEST;
1128 }
1129
1130 int UtcDaliImageVisualTextureCancelAsyncLoad(void)
1131 {
1132   ToolkitTestApplication application;
1133   tet_infoline( "Load image asynchronously, cancel loading, then load again" );
1134
1135   VisualFactory factory = VisualFactory::Get();
1136   DALI_TEST_CHECK( factory );
1137
1138   Property::Map propertyMap;
1139   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
1140   propertyMap.Insert( ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME );
1141
1142   Visual::Base visual = factory.CreateVisual( propertyMap );
1143   DALI_TEST_CHECK( visual );
1144
1145   TestGlAbstraction& gl = application.GetGlAbstraction();
1146   TraceCallStack& textureTrace = gl.GetTextureTrace();
1147   textureTrace.Enable( true );
1148   TraceCallStack& drawTrace = gl.GetDrawTrace();
1149   drawTrace.Enable( true );
1150
1151   DummyControl actor = DummyControl::New();
1152   DummyControlImpl& dummyImpl = static_cast< DummyControlImpl& >( actor.GetImplementation() );
1153   dummyImpl.RegisterVisual( Control::Property::BACKGROUND, visual );
1154
1155   application.GetScene().Add( actor );
1156
1157   // Cancel loading
1158   application.GetScene().Remove( actor );
1159
1160   application.GetScene().Add( actor );
1161
1162   // Create another visual with the same image
1163   visual = factory.CreateVisual( propertyMap );
1164   DALI_TEST_CHECK( visual );
1165
1166   dummyImpl.RegisterVisual( Control::Property::BACKGROUND, visual );
1167
1168   application.SendNotification();
1169   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1170
1171   application.SendNotification();
1172   application.Render();
1173
1174   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1175   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
1176   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
1177   DALI_TEST_EQUALS( drawTrace.FindMethod("DrawArrays"), true, TEST_LOCATION );
1178
1179   END_TEST;
1180 }
1181
1182 int UtcDaliImageVisualSetInvalidAsyncImage(void)
1183 {
1184   ToolkitTestApplication application;
1185   tet_infoline( "Request image visual with invalid images - should draw broken.png" );
1186
1187   VisualFactory factory = VisualFactory::Get();
1188   DALI_TEST_CHECK( factory );
1189
1190   Property::Map propertyMap;
1191   propertyMap.Insert( Toolkit::Visual::Property::TYPE, Visual::IMAGE );
1192   propertyMap.Insert( ImageVisual::Property::URL, TEST_INVALID_FILE_NAME );
1193
1194   Visual::Base visual = factory.CreateVisual( propertyMap );
1195   DALI_TEST_CHECK( visual );
1196
1197   TestGlAbstraction& gl = application.GetGlAbstraction();
1198   TraceCallStack& textureTrace = gl.GetTextureTrace();
1199   textureTrace.Enable(true);
1200
1201   DummyControl actor = DummyControl::New();
1202   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
1203   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
1204
1205   actor.SetProperty( Actor::Property::SIZE, Vector2( 200.f, 200.f ) );
1206   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
1207
1208   application.GetScene().Add( actor );
1209
1210   application.SendNotification();
1211   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1212
1213   application.SendNotification();
1214   application.Render();
1215
1216   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1217   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
1218
1219   application.GetScene().Remove( actor );
1220   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1221
1222   END_TEST;
1223 }
1224
1225 int UtcDaliImageVisualSetInvalidSyncImage(void)
1226 {
1227   ToolkitTestApplication application;
1228   tet_infoline( "Request image visual with invalid images - should draw broken.png" );
1229
1230   VisualFactory factory = VisualFactory::Get();
1231   DALI_TEST_CHECK( factory );
1232
1233   Property::Map propertyMap;
1234   propertyMap.Insert( Toolkit::Visual::Property::TYPE, Visual::IMAGE );
1235   propertyMap.Insert( ImageVisual::Property::URL, TEST_INVALID_FILE_NAME );
1236   propertyMap.Insert( ImageVisual::Property::SYNCHRONOUS_LOADING, true );
1237
1238   Visual::Base visual = factory.CreateVisual( propertyMap );
1239   DALI_TEST_CHECK( visual );
1240
1241   TestGlAbstraction& gl = application.GetGlAbstraction();
1242   TraceCallStack& textureTrace = gl.GetTextureTrace();
1243   textureTrace.Enable(true);
1244
1245   DummyControl actor = DummyControl::New();
1246   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
1247   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
1248
1249   actor.SetProperty( Actor::Property::SIZE, Vector2( 200.f, 200.f ) );
1250   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
1251
1252   application.GetScene().Add( actor );
1253
1254   application.SendNotification();
1255   application.Render();
1256
1257   // Check resource status
1258   Visual::ResourceStatus status = actor.GetVisualResourceStatus(Control::CONTROL_PROPERTY_END_INDEX + 1);
1259   DALI_TEST_EQUALS(status, Visual::ResourceStatus::FAILED, TEST_LOCATION);
1260
1261   // The broken image should be shown.
1262   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1263   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
1264
1265   application.GetScene().Remove( actor );
1266   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1267
1268   END_TEST;
1269 }
1270
1271 int UtcDaliImageVisualSetInvalidRemoteImage(void)
1272 {
1273   ToolkitTestApplication application;
1274   tet_infoline( "Request image visual with invalid images - should draw broken.png" );
1275
1276   VisualFactory factory = VisualFactory::Get();
1277   DALI_TEST_CHECK( factory );
1278
1279   // Local invalid file, asynchronous loading
1280   Property::Map propertyMap;
1281   propertyMap.Insert( Toolkit::Visual::Property::TYPE, Visual::IMAGE );
1282   propertyMap.Insert( ImageVisual::Property::URL, TEST_REMOTE_INVALID_FILE_NAME );
1283
1284   Visual::Base visual = factory.CreateVisual( propertyMap );
1285   DALI_TEST_CHECK( visual );
1286
1287   TestGlAbstraction& gl = application.GetGlAbstraction();
1288   TraceCallStack& textureTrace = gl.GetTextureTrace();
1289   textureTrace.Enable(true);
1290
1291   DummyControl actor = DummyControl::New();
1292   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
1293   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
1294
1295   actor.SetProperty( Actor::Property::SIZE, Vector2( 200.f, 200.f ) );
1296   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
1297
1298   application.GetScene().Add( actor );
1299
1300   application.SendNotification();
1301   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1302
1303   application.SendNotification();
1304   application.Render();
1305
1306   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1307   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
1308
1309   application.GetScene().Remove( actor );
1310   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1311
1312   END_TEST;
1313 }
1314
1315 int UtcDaliImageVisualAlphaMask(void)
1316 {
1317   ToolkitTestApplication application;
1318   tet_infoline( "Request image visual with a Property::Map containing an Alpha mask" );
1319
1320   VisualFactory factory = VisualFactory::Get();
1321   DALI_TEST_CHECK( factory );
1322
1323   Property::Map propertyMap;
1324   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
1325   propertyMap.Insert( ImageVisual::Property::URL,  TEST_LARGE_IMAGE_FILE_NAME );
1326   propertyMap.Insert( ImageVisual::Property::ALPHA_MASK_URL, TEST_MASK_IMAGE_FILE_NAME );
1327
1328   Visual::Base visual = factory.CreateVisual( propertyMap );
1329   DALI_TEST_CHECK( visual );
1330
1331   Property::Map testMap;
1332   visual.CreatePropertyMap(testMap);
1333   DALI_TEST_EQUALS(*testMap.Find(ImageVisual::Property::ALPHA_MASK_URL),Property::Value(TEST_MASK_IMAGE_FILE_NAME), TEST_LOCATION );
1334
1335   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
1336   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
1337
1338   TestGlAbstraction& gl = application.GetGlAbstraction();
1339   TraceCallStack& textureTrace = gl.GetTextureTrace();
1340   textureTrace.Enable(true);
1341
1342   DummyControl actor = DummyControl::New();
1343   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
1344   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
1345
1346   actor.SetProperty( Actor::Property::SIZE, Vector2( 200.f, 200.f ) );
1347   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
1348   DALI_TEST_EQUALS( actor.IsResourceReady(), false, TEST_LOCATION );
1349
1350   application.GetScene().Add( actor );
1351   application.SendNotification();
1352   application.Render();
1353
1354   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
1355
1356   application.SendNotification();
1357   application.Render();
1358
1359   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1360   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
1361   DALI_TEST_EQUALS( actor.IsResourceReady(), true, TEST_LOCATION );
1362
1363   dummyImpl.UnregisterVisual(  Control::CONTROL_PROPERTY_END_INDEX + 1 );
1364   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
1365
1366   END_TEST;
1367 }
1368
1369 int UtcDaliImageVisualSynchronousLoadAlphaMask(void)
1370 {
1371   ToolkitTestApplication application;
1372   tet_infoline( "Request image visual with a Property::Map containing an Alpha mask with synchronous loading" );
1373
1374   VisualFactory factory = VisualFactory::Get();
1375   DALI_TEST_CHECK( factory );
1376
1377   Property::Map propertyMap;
1378   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
1379   propertyMap.Insert( ImageVisual::Property::URL,  TEST_LARGE_IMAGE_FILE_NAME );
1380   propertyMap.Insert( ImageVisual::Property::ALPHA_MASK_URL, TEST_MASK_IMAGE_FILE_NAME );
1381   propertyMap.Insert( ImageVisual::Property::SYNCHRONOUS_LOADING, true );
1382
1383   Visual::Base visual = factory.CreateVisual( propertyMap );
1384   DALI_TEST_CHECK( visual );
1385
1386   Property::Map testMap;
1387   visual.CreatePropertyMap(testMap);
1388   DALI_TEST_EQUALS(*testMap.Find(ImageVisual::Property::ALPHA_MASK_URL),Property::Value(TEST_MASK_IMAGE_FILE_NAME), TEST_LOCATION );
1389
1390   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
1391   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
1392
1393   TestGlAbstraction& gl = application.GetGlAbstraction();
1394   TraceCallStack& textureTrace = gl.GetTextureTrace();
1395   textureTrace.Enable(true);
1396
1397   DummyControl actor = DummyControl::New();
1398   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
1399   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
1400
1401   actor.SetProperty( Actor::Property::SIZE, Vector2( 200.f, 200.f ) );
1402   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
1403   DALI_TEST_EQUALS( actor.IsResourceReady(), false, TEST_LOCATION );
1404
1405   application.GetScene().Add( actor );
1406
1407   // Do not wait for any EventThreadTrigger in synchronous alpha mask.
1408
1409   application.SendNotification();
1410   application.Render();
1411
1412   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1413   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
1414   DALI_TEST_EQUALS( actor.IsResourceReady(), true, TEST_LOCATION );
1415
1416   dummyImpl.UnregisterVisual(  Control::CONTROL_PROPERTY_END_INDEX + 1 );
1417   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
1418
1419   END_TEST;
1420 }
1421
1422 int UtcDaliImageVisualRemoteAlphaMask(void)
1423 {
1424   ToolkitTestApplication application;
1425   tet_infoline( "Request image visual with a Property::Map containing an Alpha mask" );
1426
1427   VisualFactory factory = VisualFactory::Get();
1428   DALI_TEST_CHECK( factory );
1429
1430   const std::string MASK_IMAGE = TEST_REMOTE_IMAGE_FILE_NAME;
1431
1432   Property::Map propertyMap;
1433   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
1434   propertyMap.Insert( ImageVisual::Property::URL,  TEST_IMAGE_FILE_NAME );
1435   propertyMap.Insert( ImageVisual::Property::ALPHA_MASK_URL, MASK_IMAGE );
1436
1437   Visual::Base visual = factory.CreateVisual( propertyMap );
1438   DALI_TEST_CHECK( visual );
1439
1440   Property::Map testMap;
1441   visual.CreatePropertyMap(testMap);
1442
1443   DALI_TEST_EQUALS(*testMap.Find(ImageVisual::Property::ALPHA_MASK_URL),Property::Value(MASK_IMAGE), TEST_LOCATION );
1444
1445   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
1446   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
1447
1448   TestGlAbstraction& gl = application.GetGlAbstraction();
1449   TraceCallStack& textureTrace = gl.GetTextureTrace();
1450   textureTrace.Enable(true);
1451
1452   DummyControl actor = DummyControl::New();
1453   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
1454   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
1455
1456   DALI_TEST_EQUALS( actor.IsResourceReady(), false, TEST_LOCATION );
1457
1458   actor.SetProperty( Actor::Property::SIZE, Vector2( 200.f, 200.f ) );
1459   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
1460
1461   application.GetScene().Add( actor );
1462   application.SendNotification();
1463   application.Render();
1464
1465   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
1466
1467   application.SendNotification();
1468   application.Render();
1469
1470   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1471   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
1472   DALI_TEST_EQUALS( actor.IsResourceReady(), true, TEST_LOCATION );
1473
1474   END_TEST;
1475 }
1476
1477 int UtcDaliImageVisualAlphaMaskCrop(void)
1478 {
1479   ToolkitTestApplication application;
1480   tet_infoline( "Request image visual with an Alpha mask and scale/cropping" );
1481
1482   VisualFactory factory = VisualFactory::Get();
1483   DALI_TEST_CHECK( factory );
1484
1485   Property::Map propertyMap;
1486   propertyMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
1487   propertyMap.Insert( ImageVisual::Property::URL,  TEST_LARGE_IMAGE_FILE_NAME );
1488   propertyMap.Insert( ImageVisual::Property::ALPHA_MASK_URL, TEST_MASK_IMAGE_FILE_NAME );
1489   propertyMap.Insert( ImageVisual::Property::MASK_CONTENT_SCALE, 1.6f );
1490   propertyMap.Insert( ImageVisual::Property::CROP_TO_MASK, true );
1491
1492   Visual::Base visual = factory.CreateVisual( propertyMap );
1493   DALI_TEST_CHECK( visual );
1494
1495   Property::Map testMap;
1496   visual.CreatePropertyMap(testMap);
1497   DALI_TEST_EQUALS( *testMap.Find(ImageVisual::Property::ALPHA_MASK_URL),Property::Value(TEST_MASK_IMAGE_FILE_NAME), TEST_LOCATION );
1498   DALI_TEST_EQUALS( *testMap.Find(ImageVisual::Property::MASK_CONTENT_SCALE), Property::Value(1.6f), TEST_LOCATION );
1499   DALI_TEST_EQUALS( *testMap.Find(ImageVisual::Property::CROP_TO_MASK),Property::Value(true), TEST_LOCATION );
1500
1501   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
1502   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
1503
1504   TestGlAbstraction& gl = application.GetGlAbstraction();
1505   TraceCallStack& textureTrace = gl.GetTextureTrace();
1506   textureTrace.Enable(true);
1507
1508   DummyControl actor = DummyControl::New();
1509   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
1510   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
1511
1512   actor.SetProperty( Actor::Property::SIZE, Vector2( 200.f, 200.f ) );
1513   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
1514   DALI_TEST_EQUALS( actor.IsResourceReady(), false, TEST_LOCATION );
1515
1516   application.GetScene().Add( actor );
1517   application.SendNotification();
1518   application.Render();
1519
1520   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
1521
1522   application.SendNotification();
1523   application.Render();
1524
1525   Vector2 size;
1526   visual.GetNaturalSize(size);
1527
1528   DALI_TEST_EQUALS( size, Vector2( 100.0f, 100.0f ), 0.001f, TEST_LOCATION );
1529   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1530   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
1531   DALI_TEST_EQUALS( actor.IsResourceReady(), true, TEST_LOCATION );
1532
1533   END_TEST;
1534 }
1535
1536 int UtcDaliImageVisualReleasePolicy01(void)
1537 {
1538   ToolkitTestApplication application;
1539   tet_infoline( "UtcDaliImageVisualReleasePolicy01 Detached Policy, disabling visual with this policy deletes texture" );
1540
1541   Visual::Base imageVisual = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, ImageVisual::Property::RELEASE_POLICY, ImageVisual::ReleasePolicy::DETACHED );
1542   DALI_TEST_CHECK( imageVisual );
1543
1544   // Set up debug trace
1545   TestGlAbstraction& gl = application.GetGlAbstraction();
1546   TraceCallStack& textureTrace = gl.GetTextureTrace();
1547   textureTrace.Enable(true);
1548
1549   tet_infoline( "Register visual with control and ensure it has the only handle" );
1550   DummyControl actor = DummyControl::New(true);
1551   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1552   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual );
1553   imageVisual.Reset();
1554
1555   actor.SetProperty( Actor::Property::SIZE, Vector2(200.f, 200.f) );
1556
1557   application.SendNotification();
1558   application.Render(0);
1559   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1560   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
1561
1562   application.GetScene().Add( actor );
1563
1564   // Wait for image to load
1565   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1566
1567   application.SendNotification();
1568   application.Render(0);
1569   // Test renderer and texture created
1570   tet_infoline( "Confirm texture created" );
1571   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1572   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
1573
1574   tet_infoline( "Disable visual causing the texture to be deleted" );
1575   dummyImpl.EnableVisual( DummyControl::Property::TEST_VISUAL, false );
1576
1577   application.SendNotification();
1578   application.Render(0);
1579   // Test renderer and textures removed.
1580   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1581   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION );
1582
1583   END_TEST;
1584 }
1585
1586 int UtcDaliImageVisualReleasePolicy02(void)
1587 {
1588   ToolkitTestApplication application;
1589   tet_infoline( "UtcDaliImageVisualReleasePolicy02 Destroyed Policy, Texture should be deleted when visual destroyed" );
1590
1591   Visual::Base imageVisual = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, ImageVisual::Property::RELEASE_POLICY, ImageVisual::ReleasePolicy::DESTROYED );
1592   DALI_TEST_CHECK( imageVisual );
1593
1594   // Setup debug trace
1595   TestGlAbstraction& gl = application.GetGlAbstraction();
1596   TraceCallStack& textureTrace = gl.GetTextureTrace();
1597   textureTrace.Enable(true);
1598
1599   tet_infoline( "Register visual with control and ensure it has the only handle" );
1600   DummyControl actor = DummyControl::New(true);
1601   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1602   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual );
1603   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
1604
1605   actor.SetProperty( Actor::Property::SIZE, Vector2(200.f, 200.f) );
1606
1607   application.SendNotification();
1608   application.Render(0);
1609   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1610   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
1611
1612   application.GetScene().Add( actor );
1613
1614   // Wait for image to load
1615   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1616
1617   application.SendNotification();
1618   application.Render(0);
1619   // Test renderer and texture created
1620   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1621   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
1622
1623
1624   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
1625   tet_infoline( "Destroy visual by UnRegistering visual with control, check renderer is destroyed" );
1626   dummyImpl.UnregisterVisual( DummyControl::Property::TEST_VISUAL );
1627   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1628   application.SendNotification();
1629   application.Render();
1630
1631   // Test texture removed after visual destroyed.
1632   tet_infoline( "Ensure texture is deleted after visual destroyed" );
1633   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION );
1634
1635   END_TEST;
1636 }
1637
1638 int UtcDaliImageVisualReleasePolicy03(void)
1639 {
1640   ToolkitTestApplication application;
1641   tet_infoline( "UtcDaliImageVisualReleasePolicy03 Never Policy, texture should not be deleted after visual destroyed" );
1642
1643   Visual::Base imageVisual = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, ImageVisual::Property::RELEASE_POLICY, ImageVisual::ReleasePolicy::NEVER );
1644   DALI_TEST_CHECK( imageVisual );
1645
1646   TestGlAbstraction& gl = application.GetGlAbstraction();
1647   TraceCallStack& textureTrace = gl.GetTextureTrace();
1648   textureTrace.Enable(true);
1649
1650   tet_infoline( "Register visual with control and ensure it has the only handle" );
1651   DummyControl actor = DummyControl::New(true);
1652   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1653   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual );
1654   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
1655
1656   actor.SetProperty( Actor::Property::SIZE, Vector2(200.f, 200.f) );
1657
1658   application.SendNotification();
1659   application.Render(0);
1660   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1661   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
1662
1663   application.GetScene().Add( actor );
1664
1665   // Wait for image to load
1666   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1667
1668   application.SendNotification();
1669   application.Render(0);
1670   // Test renderer and texture created
1671   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1672   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
1673
1674   tet_infoline( "Destroy visual by UnRegistering visual with control, check renderer is destroyed" );
1675   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
1676   dummyImpl.UnregisterVisual( DummyControl::Property::TEST_VISUAL );
1677   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1678   application.SendNotification();
1679   application.Render();
1680
1681   tet_infoline( "Ensure texture is not deleted as policy is set to NEVER" );
1682   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION );
1683
1684   END_TEST;
1685 }
1686
1687 int UtcDaliImageVisualReleasePolicy04(void)
1688 {
1689   ToolkitTestApplication application;
1690   tet_infoline( "UtcDaliImageVisualReleasePolicy04 Two visuals with different policies sharing a texture" );
1691
1692   tet_infoline( "Create first visual with Never release policy" );
1693   Visual::Base imageVisualNever = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, ImageVisual::Property::RELEASE_POLICY, ImageVisual::ReleasePolicy::NEVER );
1694
1695   tet_infoline( "Create second visual with Destroyed release policy");
1696     Visual::Base imageVisualDestroyed = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, ImageVisual::Property::RELEASE_POLICY, ImageVisual::ReleasePolicy::DESTROYED );
1697
1698   // Set up trace debug
1699   TestGlAbstraction& gl = application.GetGlAbstraction();
1700   TraceCallStack& textureTrace = gl.GetTextureTrace();
1701   textureTrace.Enable(true);
1702
1703   tet_infoline( "Register visuals with control and ensure it has the only handles" );
1704   DummyControl actor = DummyControl::New(true);
1705   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1706   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisualNever );
1707   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL2, imageVisualDestroyed );
1708   imageVisualNever.Reset(); // reduce ref count so only the control keeps the visual alive.
1709   imageVisualDestroyed.Reset(); // reduce ref count so only the control keeps the visual alive.
1710
1711   actor.SetProperty( Actor::Property::SIZE, Vector2(200.f, 200.f) );
1712
1713   // Test initially zero renderers
1714   application.SendNotification();
1715   application.Render(0);
1716   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1717   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
1718
1719   application.GetScene().Add( actor );
1720
1721   // Wait for image to load
1722   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1723
1724   application.SendNotification();
1725   application.Render(0);
1726   tet_infoline( "Ensure a texture is created, shared amongst both visuals.  Each visual has its own renderer" );
1727   DALI_TEST_EQUALS( actor.GetRendererCount(), 2u, TEST_LOCATION );
1728   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
1729
1730   // Test renderer removed when visual destroyed
1731   DALI_TEST_CHECK( actor.GetRendererCount() == 2u );
1732   dummyImpl.UnregisterVisual( DummyControl::Property::TEST_VISUAL2 );  // TEST_VISUAL2 no longer requires the texture as release policy DESTROYED
1733   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
1734   application.SendNotification();
1735   application.Render();
1736
1737   // Test texture was not deleted as TEST_VISUAL release policy is NEVER so it is still required.
1738   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION );
1739
1740   dummyImpl.UnregisterVisual( DummyControl::Property::TEST_VISUAL );
1741   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1742   application.SendNotification();
1743   application.Render();
1744
1745   tet_infoline( "Ensure a texture is not deleted as second visual used the NEVER release policy" );
1746   // Test texture was not deleted as TEST_VISUAL release policy is NEVER so it is still required.
1747   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION );
1748
1749   END_TEST;
1750 }
1751
1752 int UtcDaliImageVisualReleasePolicy05(void)
1753 {
1754   ToolkitTestApplication application;
1755   tet_infoline( "UtcDaliImageVisualReleasePolicy05 Testing settung by string currents correct enum" );
1756
1757   VisualFactory factory = VisualFactory::Get();
1758
1759   Property::Map propertyMapNeverReleasePolicy;
1760   propertyMapNeverReleasePolicy.Insert( Visual::Property::TYPE,  Visual::IMAGE );
1761   propertyMapNeverReleasePolicy.Insert( ImageVisual::Property::URL,  TEST_IMAGE_FILE_NAME );
1762   propertyMapNeverReleasePolicy.Insert( ImageVisual::Property::DESIRED_WIDTH,   20 );
1763   propertyMapNeverReleasePolicy.Insert( ImageVisual::Property::DESIRED_HEIGHT,   30 );
1764   propertyMapNeverReleasePolicy.Insert( "releasePolicy" , "never" );
1765
1766   Visual::Base imageVisualNever = factory.CreateVisual( propertyMapNeverReleasePolicy );
1767
1768   Property::Map resultMap;
1769   imageVisualNever.CreatePropertyMap( resultMap );
1770   DALI_TEST_CHECK( ! resultMap.Empty() );
1771
1772   DALI_TEST_EQUALS( ( resultMap.Find( ImageVisual::Property::RELEASE_POLICY ) )->Get<int>(), (int)ImageVisual::ReleasePolicy::NEVER, TEST_LOCATION );
1773
1774   END_TEST;
1775 }
1776
1777 int UtcDaliImageVisualReleasePolicy06(void)
1778 {
1779   ToolkitTestApplication application;
1780   tet_infoline( "UtcDaliImageVisualReleasePolicy06 Never Policy, texture should not be affected by Disabling and Enabling visual" );
1781
1782   Visual::Base imageVisual= CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, ImageVisual::Property::RELEASE_POLICY, ImageVisual::ReleasePolicy::NEVER );
1783   DALI_TEST_CHECK( imageVisual );
1784
1785   TestGlAbstraction& gl = application.GetGlAbstraction();
1786   TraceCallStack& textureTrace = gl.GetTextureTrace();
1787   textureTrace.Enable(true);
1788
1789   tet_infoline( "Register visual with control and ensure it has the only handle" );
1790   DummyControl actor = DummyControl::New(true);
1791   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1792   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual );
1793   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
1794
1795   actor.SetProperty( Actor::Property::SIZE, Vector2(200.f, 200.f) );
1796
1797   application.SendNotification();
1798   application.Render(0);
1799   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1800   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
1801
1802   application.GetScene().Add( actor );
1803
1804   // Wait for image to load
1805   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1806
1807   application.SendNotification();
1808   application.Render(0);
1809   // Test renderer and texture created
1810   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1811   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
1812   textureTrace.Reset();
1813
1814   tet_infoline( "Disable Visual and check texture not affected" );
1815   dummyImpl.EnableVisual( DummyControl::Property::TEST_VISUAL, false );
1816   application.SendNotification();
1817   application.Render(0);
1818   tet_infoline( "Check renderer is destroyed when visual off stage" );
1819   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1820   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION );
1821   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
1822   textureTrace.Reset();
1823
1824   tet_infoline( "Re-enable Visual and check texture not affected" );
1825   dummyImpl.EnableVisual( DummyControl::Property::TEST_VISUAL, true );
1826   application.SendNotification();
1827   application.Render(0);
1828   tet_infoline( "Check texture not affected and renderer is destroyed when visual off stage" );
1829   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
1830   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION );
1831   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
1832
1833   END_TEST;
1834 }
1835
1836 int UtcDaliImageVisualReleasePolicy07(void)
1837 {
1838   ToolkitTestApplication application;
1839   tet_infoline( "UtcDaliImageVisualReleasePolicy07 Two visuals with different policies sharing a texture DETACHED and DESTROYED" );
1840
1841   tet_infoline( "Create first visual with DESTROYED release policy" );
1842   Visual::Base imageVisualDestroyed = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, ImageVisual::Property::RELEASE_POLICY, ImageVisual::ReleasePolicy::DESTROYED );
1843
1844
1845   tet_infoline( "Create second visual with DETACHED release policy");
1846   Visual::Base imageVisualDetached = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, ImageVisual::Property::RELEASE_POLICY, ImageVisual::ReleasePolicy::DETACHED );
1847
1848   // Set up trace debug
1849   TestGlAbstraction& gl = application.GetGlAbstraction();
1850   TraceCallStack& textureTrace = gl.GetTextureTrace();
1851   textureTrace.Enable(true);
1852
1853   tet_infoline( "Register visuals with control and ensure it has the only handles" );
1854   DummyControl actor = DummyControl::New(true);
1855   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1856   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisualDestroyed );
1857   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL2, imageVisualDetached );
1858   imageVisualDestroyed.Reset(); // reduce ref count so only the control keeps the visual alive.
1859   imageVisualDetached.Reset(); // reduce ref count so only the control keeps the visual alive.
1860
1861   actor.SetProperty( Actor::Property::SIZE, Vector2(200.f, 200.f) );
1862
1863   // Test initially zero renderers
1864   application.SendNotification();
1865   application.Render(0);
1866   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1867   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
1868
1869   application.GetScene().Add( actor );
1870
1871   // Wait for image to load
1872   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1873
1874   application.SendNotification();
1875   application.Render(0);
1876   tet_infoline( "Ensure a texture is created, shared amongst both visuals.  Each visual has its own renderer" );
1877   DALI_TEST_EQUALS( actor.GetRendererCount(), 2u, TEST_LOCATION );
1878   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
1879
1880   // Test renderer removed when visual destroyed
1881   DALI_TEST_CHECK( actor.GetRendererCount() == 2u );
1882   dummyImpl.EnableVisual( DummyControl::Property::TEST_VISUAL2, false );  // TEST_VISUAL2 no longer requires the texture as release policy DETACHED
1883   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
1884   application.SendNotification();
1885   application.Render();
1886
1887   // Test texture was not deleted as TEST_VISUAL release policy is DESTROYED and is still required.
1888   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION );
1889
1890   dummyImpl.EnableVisual( DummyControl::Property::TEST_VISUAL, false );
1891   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1892   application.SendNotification();
1893   application.Render();
1894
1895   tet_infoline( "Ensure a texture is not deleted as second visual used the DESTROYED release policy" );
1896   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION );
1897
1898   END_TEST;
1899 }
1900
1901 int UtcDaliImageVisualReleasePolicy08(void)
1902 {
1903   ToolkitTestApplication application;
1904   tet_infoline( "UtcDaliImageVisualReleasePolicy08 Ensure TextureSet is same after detach/attach on stage when texture used the DESTROYED release policy" );
1905
1906   tet_infoline( "Create first visual with DESTROYED release policy" );
1907   Visual::Base imageVisualDestroyed = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, ImageVisual::Property::RELEASE_POLICY, ImageVisual::ReleasePolicy::DESTROYED );
1908
1909   // Set up trace debug
1910   TestGlAbstraction& gl = application.GetGlAbstraction();
1911   TraceCallStack& textureTrace = gl.GetTextureTrace();
1912   textureTrace.Enable(true);
1913
1914   tet_infoline( "Register visuals with control and ensure it has the only handles" );
1915   DummyControl actor = DummyControl::New(true);
1916   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1917   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisualDestroyed );
1918   imageVisualDestroyed.Reset(); // reduce ref count so only the control keeps the visual alive.
1919
1920   actor.SetProperty( Actor::Property::SIZE, Vector2(200.f, 200.f) );
1921
1922   // Test initially zero renderers
1923   application.SendNotification();
1924   application.Render(0);
1925   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1926   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
1927   textureTrace.Reset();
1928
1929   application.GetScene().Add( actor );
1930
1931   // Wait for image to load
1932   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1933
1934   application.SendNotification();
1935   application.Render(0);
1936   tet_infoline( "Ensure a texture is created" );
1937   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
1938   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
1939   textureTrace.Reset();
1940
1941   // Ensure TextureSet is same after detach/attach on stage when texture used the DESTROYED release policy
1942   // 1. Get TextureSet
1943   TextureSet textureSetBefore = actor.GetRendererAt( 0u ).GetTextures();
1944
1945   // 2.Remove actor from stage. In this case, renderer also is deleted.
1946   tet_infoline( "Remove actor from stage" );
1947   application.GetScene().Remove( actor );
1948   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1949   application.SendNotification();
1950   application.Render();
1951
1952   tet_infoline( "Ensure a texture is not deleted as visual used the DESTROYED release policy" );
1953   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION );
1954   textureTrace.Reset();
1955
1956   // 3.Add actor in stage. In this case, renderer is created.
1957   tet_infoline( "Add actor in stage" );
1958   application.GetScene().Add( actor );
1959   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
1960   application.SendNotification();
1961   application.Render();
1962   tet_infoline( "Ensure a texture is not created again" );
1963   DALI_TEST_EQUALS( textureTrace.CountMethod("GenTextures"), 0, TEST_LOCATION );
1964   textureTrace.Reset();
1965
1966   // 4.Compare Texture with before and after. textureSet need to be same because release policy is the DESTROYED.
1967   tet_infoline( "Ensure a textureSet is not deleted because it is used the DESTROYED release policy" );
1968   TextureSet textureSetAfter = actor.GetRendererAt( 0u ).GetTextures();
1969   DALI_TEST_CHECK( textureSetBefore == textureSetAfter );
1970   textureSetBefore.Reset();
1971   textureSetAfter.Reset();
1972
1973   dummyImpl.UnregisterVisual( DummyControl::Property::TEST_VISUAL );
1974   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
1975   application.SendNotification();
1976   application.Render();
1977   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION );
1978
1979   END_TEST;
1980 }
1981
1982 int UtcDaliImageVisualLoadPolicy01(void)
1983 {
1984   ToolkitTestApplication application;
1985   tet_infoline( "UtcDaliImageVisualLoadPolicy01 Load a visual image before attaching to stage" );
1986
1987   // Set up trace debug
1988   TestGlAbstraction& gl = application.GetGlAbstraction();
1989   TraceCallStack& textureTrace = gl.GetTextureTrace();
1990   textureTrace.Enable(true);
1991
1992   tet_infoline( "Create visual with IMMEDIATE load policy" );
1993   VisualFactory factory = VisualFactory::Get();
1994
1995   Property::Map propertyMap;
1996   propertyMap.Insert( Visual::Property::TYPE,  Visual::IMAGE );
1997   propertyMap.Insert( ImageVisual::Property::URL,  TEST_IMAGE_FILE_NAME );
1998   propertyMap.Insert( ImageVisual::Property::DESIRED_WIDTH,   20 );
1999   propertyMap.Insert( ImageVisual::Property::DESIRED_HEIGHT,   30 );
2000   propertyMap.Insert( "loadPolicy" , ImageVisual::LoadPolicy::IMMEDIATE );
2001
2002   Visual::Base imageVisual = factory.CreateVisual( propertyMap );
2003
2004   Property::Map resultMap;
2005   imageVisual.CreatePropertyMap( resultMap );
2006   DALI_TEST_CHECK( ! resultMap.Empty() );
2007   DALI_TEST_EQUALS( ( resultMap.Find( ImageVisual::Property::LOAD_POLICY ) )->Get<int>(), (int)ImageVisual::LoadPolicy::IMMEDIATE, TEST_LOCATION );
2008
2009   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
2010
2011   // Ensure texture has been uploaded
2012   application.SendNotification();
2013   application.Render();
2014
2015   tet_infoline( "Ensure texture loading starts after visual created" );
2016   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
2017   textureTrace.Reset();
2018
2019   tet_infoline( "Register visuals with control and ensure it has the only handles" );
2020   DummyControl actor = DummyControl::New(true);
2021   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2022   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual );
2023   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
2024
2025   actor.SetProperty( Actor::Property::SIZE, Vector2(200.f, 200.f) );
2026   application.GetScene().Add( actor );
2027   tet_infoline( "Ensure nothing triggers another load as texure already loaded" );
2028   const unsigned int TIME_OUT_3_SECONDS = 3;
2029   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1, TIME_OUT_3_SECONDS ), false, TEST_LOCATION );
2030
2031   application.SendNotification();
2032   application.Render();
2033
2034   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
2035   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
2036
2037   // Ensure texture is deleted when no longer needed (ref count was correct )
2038   dummyImpl.UnregisterVisual( DummyControl::Property::TEST_VISUAL );
2039
2040   application.SendNotification();
2041   application.Render();
2042
2043   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
2044   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION );
2045
2046   END_TEST;
2047 }
2048
2049 int UtcDaliImageVisualLoadPolicy02(void)
2050 {
2051   ToolkitTestApplication application;
2052   tet_infoline( "UtcDaliImageVisualLoadPolicy01 Load a visual image only after attached to stage" );
2053
2054   // Set up trace debug
2055   TestGlAbstraction& gl = application.GetGlAbstraction();
2056   TraceCallStack& textureTrace = gl.GetTextureTrace();
2057   textureTrace.Enable(true);
2058
2059   tet_infoline( "Create visual with IMMEDIATE load policy" );
2060   Visual::Base imageVisual = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, ImageVisual::Property::LOAD_POLICY, ImageVisual::LoadPolicy::ATTACHED );
2061
2062   const unsigned int TIME_OUT_3_SECONDS = 3;
2063   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1, TIME_OUT_3_SECONDS ), false, TEST_LOCATION );
2064
2065   // Act on meeage queue even although nothing expected to load
2066   application.SendNotification();
2067   application.Render();
2068
2069   tet_infoline( "Ensure texture is not generated yet" );
2070   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
2071   textureTrace.Reset();
2072
2073   tet_infoline( "Register visuals with control and ensure it has the only handles" );
2074   DummyControl actor = DummyControl::New(true);
2075   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2076   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual );
2077   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
2078
2079   actor.SetProperty( Actor::Property::SIZE, Vector2(200.f, 200.f) );
2080   application.GetScene().Add( actor );
2081   tet_infoline( "Allow image time to load" );
2082   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
2083
2084   application.SendNotification();
2085   application.Render();
2086
2087   tet_infoline( "Ensure texture generated and renderer created" );
2088   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
2089   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
2090
2091   // Ensure texture is delete when no longer needed
2092   dummyImpl.UnregisterVisual( DummyControl::Property::TEST_VISUAL );
2093
2094   application.SendNotification();
2095   application.Render();
2096
2097   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
2098   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION );
2099
2100   END_TEST;
2101 }
2102
2103 int UtcDaliImageVisualLoadPolicy03(void)
2104 {
2105   ToolkitTestApplication application;
2106   tet_infoline( "UtcDaliImageVisualLoadPolicy03 Load a visual image and receive ResourceReady Signal when loaded" );
2107
2108   const bool VISUAL_NOT_ENABLED( false ); // Instead of just passing 'false' into an API.
2109
2110   // Set up trace debug
2111   TestGlAbstraction& gl = application.GetGlAbstraction();
2112   TraceCallStack& textureTrace = gl.GetTextureTrace();
2113   textureTrace.Enable(true);
2114
2115   tet_infoline( "Create a control and connect to resource ready signal without adding to stage" );
2116   DummyControl actor = DummyControl::New(true);
2117   actor.ResourceReadySignal().Connect( &ResourceReadySignal);
2118   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2119   actor.SetProperty( Actor::Property::SIZE, Vector2(200.f, 200.f) );
2120
2121   tet_infoline( "Create visual with IMMEDIATE load policy" );
2122   Visual::Base imageVisual = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, ImageVisual::Property::LOAD_POLICY, ImageVisual::LoadPolicy::IMMEDIATE );
2123
2124   tet_infoline( "Registering visual allows control to get a signal once loaded even if visual not enabled( not staged )" );
2125   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual, VISUAL_NOT_ENABLED );
2126   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
2127
2128   tet_infoline( "Allow image time to load resource" );
2129   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
2130   application.SendNotification();
2131   application.Render();
2132
2133   // Ensure texture has been uploaded
2134   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
2135   DALI_TEST_EQUALS( gResourceReadySignalFired, true, TEST_LOCATION );
2136
2137   END_TEST;
2138 }
2139
2140 int UtcDaliImageVisualLoadPolicy04(void)
2141 {
2142   ToolkitTestApplication application;
2143   tet_infoline( "UtcDaliImageVisualLoadPolicy04 First part  Load a visual image before attaching to stage");
2144   tet_infoline( "Second part, Reuse the same image in aonther control and check resource ready signal fired" );
2145
2146   const bool VISUAL_NOT_ENABLED( false ); // Instead of just passing false into an API.
2147
2148   // Set up trace debug
2149   TestGlAbstraction& gl = application.GetGlAbstraction();
2150   TraceCallStack& textureTrace = gl.GetTextureTrace();
2151   textureTrace.Enable(true);
2152
2153   tet_infoline( "Create a control and connect to resource ready signal" );
2154   DummyControl actor = DummyControl::New(true);
2155   actor.ResourceReadySignal().Connect( &ResourceReadySignal);
2156   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2157   actor.SetProperty( Actor::Property::SIZE, Vector2(200.f, 200.f) );
2158
2159   tet_infoline( "Create visual with IMMEDIATE load policy" );
2160   Visual::Base imageVisual = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, ImageVisual::Property::LOAD_POLICY, ImageVisual::LoadPolicy::IMMEDIATE );
2161
2162   tet_infoline( "Registering visual allows control to get a signal once loaded even if visual not enabled( staged )" );
2163   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual, VISUAL_NOT_ENABLED );
2164   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
2165
2166   tet_infoline( "Allow image time to load" );
2167   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
2168   application.SendNotification();
2169   application.Render();
2170
2171   tet_infoline( "Testing texture is loaded and resource ready signal fired" );
2172   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
2173   DALI_TEST_EQUALS( gResourceReadySignalFired, true, TEST_LOCATION );
2174
2175   tet_infoline( "Original control correctly signalled, now testing for signal with new Control reusing the image" );
2176
2177   gResourceReadySignalFired = false; // Reset signal check ready for testing next Control
2178   Visual::Base imageVisual2 = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, ImageVisual::Property::LOAD_POLICY, ImageVisual::LoadPolicy::IMMEDIATE );
2179   DummyControl actor2 = DummyControl::New(true);
2180   Impl::DummyControl& dummyImpl2 = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2181   actor2.ResourceReadySignal().Connect( &ResourceReadySignal);
2182
2183   tet_infoline( "Registering visual this should trigger the loading signal as is already image loaded for previous control" );
2184   dummyImpl2.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual2 );
2185   imageVisual2.Reset(); // reduce ref count so only the control keeps the visual alive.
2186   actor2.SetProperty( Actor::Property::SIZE, Vector2(200.f, 200.f) );
2187   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 0 ), true, TEST_LOCATION ); // Not expecting any further loading as texture is being reused.
2188   DALI_TEST_EQUALS( gResourceReadySignalFired, true, TEST_LOCATION );
2189
2190   END_TEST;
2191 }
2192
2193 int UtcDaliImageVisualLoadPolicy05(void)
2194 {
2195   ToolkitTestApplication application;
2196   tet_infoline( "UtcDaliImageVisualLoadPolicy05 LoadPolicy::ATTACHED (default) First part  Load a visual image before attaching to stage");
2197   tet_infoline( "Second part, Reuse the same image in aonther control and check resource ready signal fired" );
2198   // Set up trace debug
2199   TestGlAbstraction& gl = application.GetGlAbstraction();
2200   TraceCallStack& textureTrace = gl.GetTextureTrace();
2201   textureTrace.Enable(true);
2202
2203   tet_infoline( "Create a control and connect to resource ready signal" );
2204   DummyControl actor = DummyControl::New(true);
2205   actor.ResourceReadySignal().Connect( &ResourceReadySignal);
2206   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2207   actor.SetProperty( Actor::Property::SIZE, Vector2(200.f, 200.f) );
2208   application.GetScene().Add( actor );
2209
2210   tet_infoline( "Create visual with ATTACHED load policy" );
2211   Visual::Base imageVisual = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, ImageVisual::Property::LOAD_POLICY, ImageVisual::LoadPolicy::ATTACHED );
2212
2213   tet_infoline( "Registering visual allows control to get a signal once loaded" );
2214   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual );
2215   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
2216
2217   tet_infoline( "Allow image time to load" );
2218   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
2219   application.SendNotification();
2220   application.Render();
2221
2222   tet_infoline( "Testing texture is loaded and resource ready signal fired" );
2223   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
2224   DALI_TEST_EQUALS( gResourceReadySignalFired, true, TEST_LOCATION );
2225
2226   tet_infoline( "Original control correctly signalled, now testing for signal with new Control reusing the image" );
2227
2228   gResourceReadySignalFired = false; // Reset signal check ready for testing next Control
2229   Visual::Base imageVisual2 = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, ImageVisual::Property::LOAD_POLICY, ImageVisual::LoadPolicy::ATTACHED );
2230   DummyControl actor2 = DummyControl::New(true);
2231   Impl::DummyControl& dummyImpl2 = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2232   actor2.ResourceReadySignal().Connect( &ResourceReadySignal);
2233
2234   tet_infoline( "Registering visual this should trigger the loading signal as is already image loaded for previous control" );
2235   dummyImpl2.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual2 );
2236   imageVisual2.Reset(); // reduce ref count so only the control keeps the visual alive.
2237   actor2.SetProperty( Actor::Property::SIZE, Vector2(200.f, 200.f) );
2238   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 0 ), true, TEST_LOCATION ); // Not expecting any further loading as texture is being reused.
2239   DALI_TEST_EQUALS( gResourceReadySignalFired, true, TEST_LOCATION );
2240
2241   END_TEST;
2242 }
2243
2244
2245 int UtcDaliImageVisualOrientationCorrection(void)
2246 {
2247   ToolkitTestApplication application;
2248   tet_infoline( "UtcDaliImageVisualOrientationCorrection Enabling OrientationCorrection should rotate an image with exif (90deg) orientation data with requested" );
2249
2250   VisualFactory factory = VisualFactory::Get();
2251   tet_infoline( "Create visual with Orientation correction set OFF" );
2252   Property::Map propertyMap;
2253   propertyMap.Insert( Visual::Property::TYPE,  Visual::IMAGE );
2254   propertyMap.Insert( ImageVisual::Property::URL, TEST_ROTATED_IMAGE );
2255   propertyMap.Insert( "orientationCorrection", false );
2256   Visual::Base imageVisual = factory.CreateVisual( propertyMap );
2257
2258   tet_infoline( "Create control for visual, need to loaded it" );
2259   DummyControl actor = DummyControl::New(true);
2260   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2261   application.GetScene().Add( actor );
2262
2263   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual );
2264   // Wait for image to load
2265   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
2266
2267   Vector2 originalImageSize;
2268   tet_infoline( "Get size of original visual to compare later with rotated image" );
2269   imageVisual.GetNaturalSize( originalImageSize );
2270   DALI_TEST_GREATER( originalImageSize.width, originalImageSize.height, TEST_LOCATION ); // Width and Height must be different for this test.
2271   imageVisual.Reset();  // remove handle so can unregister it and remove from cache
2272   dummyImpl.UnregisterVisual( DummyControl::Property::TEST_VISUAL );
2273   application.SendNotification();
2274   application.Render();
2275
2276   tet_infoline( "Create visual with Orientation correction set ON " );
2277   propertyMap.Clear();
2278   propertyMap.Insert( Visual::Property::TYPE,  Visual::IMAGE );
2279   propertyMap.Insert( ImageVisual::Property::URL, TEST_ROTATED_IMAGE );
2280   propertyMap.Insert( ImageVisual::Property::ORIENTATION_CORRECTION, true );
2281   imageVisual = factory.CreateVisual( propertyMap );
2282
2283   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual );
2284   // Wait for image to load
2285   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
2286
2287   Vector2 rotatedImageSize;
2288   imageVisual.GetNaturalSize( rotatedImageSize );
2289   tet_infoline( "Confirm that visual has rotated" );
2290   DALI_TEST_EQUALS( originalImageSize.width, rotatedImageSize.height , TEST_LOCATION );
2291   DALI_TEST_EQUALS( originalImageSize.height, rotatedImageSize.width , TEST_LOCATION );
2292
2293   Property::Map resultMap;
2294   imageVisual.CreatePropertyMap( resultMap );
2295
2296   // check the Property::ORIENTATION_CORRECTION value from the returned map
2297   Property::Value* typeValue = resultMap.Find( ImageVisual::Property::ORIENTATION_CORRECTION,  Property::BOOLEAN );
2298   DALI_TEST_EQUALS( typeValue->Get<bool>(), true, TEST_LOCATION );
2299
2300   END_TEST;
2301 }
2302
2303 int UtcDaliImageVisualCustomShader(void)
2304 {
2305   ToolkitTestApplication application;
2306   tet_infoline( "UtcDaliImageVisualCustomShader Test custom shader" );
2307
2308   VisualFactory factory = VisualFactory::Get();
2309   Property::Map properties;
2310   Property::Map shader;
2311   const std::string vertexShader = "Foobar";
2312   const std::string fragmentShader = "Foobar";
2313   shader[Visual::Shader::Property::FRAGMENT_SHADER] = fragmentShader;
2314   shader[Visual::Shader::Property::VERTEX_SHADER] = vertexShader;
2315
2316   properties[Visual::Property::TYPE] = Visual::IMAGE;
2317   properties[Visual::Property::SHADER] = shader;
2318   properties[ImageVisual::Property::URL] = TEST_IMAGE_FILE_NAME;
2319
2320   Visual::Base visual = factory.CreateVisual( properties );
2321
2322   // trigger creation through setting on stage
2323   DummyControl dummy = DummyControl::New( true );
2324   Impl::DummyControl& dummyImpl = static_cast< Impl::DummyControl& >( dummy.GetImplementation() );
2325   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
2326
2327   dummy.SetProperty( Actor::Property::SIZE, Vector2( 200.f, 200.f ) );
2328   dummy.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
2329   application.GetScene().Add( dummy );
2330
2331   application.SendNotification();
2332   application.Render();
2333
2334   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
2335
2336   Renderer renderer = dummy.GetRendererAt( 0 );
2337   Shader shader2 = renderer.GetShader();
2338   Property::Value value = shader2.GetProperty( Shader::Property::PROGRAM );
2339   Property::Map* map = value.GetMap();
2340   DALI_TEST_CHECK( map );
2341
2342   Property::Value* fragment = map->Find( "fragment" ); // fragment key name from shader-impl.cpp
2343   DALI_TEST_EQUALS( fragmentShader, fragment->Get< std::string >(), TEST_LOCATION );
2344
2345   Property::Value* vertex = map->Find( "vertex" ); // vertex key name from shader-impl.cpp
2346   DALI_TEST_EQUALS( vertexShader, vertex->Get< std::string >(), TEST_LOCATION );
2347
2348   shader.Clear();
2349
2350   shader[Visual::Shader::Property::HINTS] = Shader::Hint::OUTPUT_IS_TRANSPARENT;
2351   properties[Visual::Property::SHADER] = shader;
2352
2353   Visual::Base visual1 = factory.CreateVisual( properties );
2354
2355   // trigger creation through setting on stage
2356   DummyControl dummy1 = DummyControl::New( true );
2357   Impl::DummyControl& dummyImpl1 = static_cast< Impl::DummyControl& >( dummy1.GetImplementation() );
2358   dummyImpl1.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual1 );
2359   dummy1.SetProperty( Actor::Property::SIZE, Vector2( 200, 200 ) );
2360   dummy1.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
2361   application.GetScene().Add( dummy1 );
2362
2363   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
2364   glAbstraction.EnableEnableDisableCallTrace( true );
2365
2366   application.SendNotification();
2367   application.Render();
2368
2369   TraceCallStack& glEnableStack = glAbstraction.GetEnableDisableTrace();
2370   std::ostringstream blendStr;
2371   blendStr << GL_BLEND;
2372   DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
2373
2374   END_TEST;
2375 }
2376
2377
2378 void ResourceReadyLoadNext( Control control )
2379 {
2380   static int callNumber = 0;
2381
2382   gResourceReadySignalFired = true;
2383   gReadyIds.push_back(control.GetProperty< int >( Actor::Property::ID ));
2384
2385   if( callNumber == 0 )
2386   {
2387     DALI_TEST_EQUALS( control.GetVisualResourceStatus(DummyControl::Property::TEST_VISUAL), Toolkit::Visual::ResourceStatus::FAILED, TEST_LOCATION );
2388
2389     tet_infoline( "Create visual with loaded image from within the signal handler" );
2390     VisualFactory factory = VisualFactory::Get();
2391     Visual::Base imageVisual = factory.CreateVisual( TEST_IMAGE_FILE_NAME, ImageDimensions{20,30} );
2392
2393     Impl::DummyControl& controlImpl = static_cast<Impl::DummyControl&>(control.GetImplementation());
2394     controlImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual ); // This should trigger another signal.
2395     callNumber = 1;
2396   }
2397   else
2398   {
2399     tet_infoline( "3rd signal called" );
2400     DALI_TEST_CHECK(true);
2401   }
2402 }
2403
2404 int UtcDaliImageVisualLoadReady01(void)
2405 {
2406   ToolkitTestApplication application;
2407   tet_infoline( "UtcDaliImageVisualLoadReady01");
2408   tet_infoline( "First part:  Load an image visual for one resource, then another image visual for a second resource.");
2409   tet_infoline( "Second part, In the ready signal for the second image visual, add a 3rd visual with the first URL" );
2410   tet_infoline( "Should get a ready signal for all three visuals");
2411
2412   ClearReadyIds();
2413
2414   tet_infoline( "Create a control and connect to resource ready signal" );
2415   DummyControl actor = DummyControl::New(true);
2416   int actor1Id = actor.GetProperty< int >( Actor::Property::ID );
2417   actor.ResourceReadySignal().Connect( &ResourceReadySignal);
2418   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2419   actor.SetProperty( Actor::Property::SIZE, Vector2(200.f, 200.f) );
2420   application.GetScene().Add(actor);
2421
2422   tet_infoline( "Create visual with IMMEDIATE load policy" );
2423   Visual::Base imageVisual1 = CreateVisualWithPolicy( TEST_IMAGE_FILE_NAME, ImageVisual::Property::LOAD_POLICY, ImageVisual::LoadPolicy::IMMEDIATE );
2424
2425   tet_infoline( "Registering visual allows control to get a signal once loaded even if visual not enabled( staged )" );
2426   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual1 );
2427
2428
2429   tet_infoline( "Allow image time to load" );
2430   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
2431   application.SendNotification();
2432   application.Render();
2433
2434   tet_infoline( "Testing texture is loaded and resource ready signal fired" );
2435   DALI_TEST_EQUALS( gResourceReadySignalFired, true, TEST_LOCATION );
2436   DALI_TEST_EQUALS( gReadyIds[0], actor1Id, TEST_LOCATION );
2437
2438
2439   tet_infoline( "Original control correctly signalled, now testing failing image" );
2440
2441   gResourceReadySignalFired = false; // Reset signal check ready for testing next Control
2442   ClearReadyIds();
2443
2444   Visual::Base imageVisual2 = CreateVisualWithPolicy( TEST_BROKEN_IMAGE_FILE_NAME, ImageVisual::Property::LOAD_POLICY, ImageVisual::LoadPolicy::IMMEDIATE );
2445
2446   DummyControl actor2 = DummyControl::New(true);
2447   int actor2Id = actor2.GetProperty< int >( Actor::Property::ID );
2448   Impl::DummyControl& dummyImpl2 = static_cast<Impl::DummyControl&>(actor2.GetImplementation());
2449   actor2.ResourceReadySignal().Connect( &ResourceReadyLoadNext);
2450
2451   tet_infoline( "Registering visual this should trigger the ready signal when the image fails to load" );
2452   dummyImpl2.RegisterVisual( DummyControl::Property::TEST_VISUAL, imageVisual2 );
2453
2454   actor2.SetProperty( Actor::Property::SIZE, Vector2(200.f, 200.f) );
2455   application.GetScene().Add(actor2);
2456
2457   tet_infoline( "Wait for loading thread to finish");
2458   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
2459   DALI_TEST_EQUALS( gResourceReadySignalFired, true, TEST_LOCATION );
2460
2461   DALI_TEST_EQUALS( gReadyIds[0], actor2Id, TEST_LOCATION);
2462
2463   tet_infoline( "Check for 3rd signal");
2464   application.SendNotification();
2465   DALI_TEST_EQUALS( gReadyIds.size(), 2, TEST_LOCATION );
2466   DALI_TEST_EQUALS( gReadyIds[1], actor2Id, TEST_LOCATION);
2467
2468   END_TEST;
2469 }