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