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