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