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