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