Merge "Change not to use the mask if it is set to an empty path" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-AnimatedImageVisual.cpp
1
2 /*
3  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <iostream>
19 #include <stdlib.h>
20 #include <dali-toolkit-test-suite-utils.h>
21 #include <toolkit-timer.h>
22 #include <toolkit-event-thread-callback.h>
23 #include <dali-toolkit/dali-toolkit.h>
24 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
25 #include <dali-toolkit/devel-api/controls/control-devel.h>
26 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
27 #include <dali-toolkit/devel-api/visuals/animated-image-visual-actions-devel.h>
28 #include "dummy-control.h"
29
30 using namespace Dali;
31 using namespace Dali::Toolkit;
32
33 void dali_animated_image_visual_startup(void)
34 {
35   test_return_value = TET_UNDEF;
36 }
37
38 void dali_animated_image_visual_cleanup(void)
39 {
40   test_return_value = TET_PASS;
41 }
42
43 namespace
44 {
45 const char* TEST_IMAGE_FILE_NAME =  TEST_RESOURCE_DIR  "/application-icon-%02d.png";
46 const char* TEST_GIF_FILE_NAME = TEST_RESOURCE_DIR "/anim.gif";
47 }
48
49
50 void CopyUrlsIntoArray( Property::Array& urls, int startIndex=0 )
51 {
52   for( int i=20+startIndex;i<=30;++i)
53   {
54     char* url;
55     if(asprintf(&url, TEST_IMAGE_FILE_NAME, i) > 0)
56     {
57       Property::Value value(url);
58       urls.Add(value);
59       free(url);
60     }
61   }
62 }
63
64 int UtcDaliAnimatedImageVisualGetPropertyMap01(void)
65 {
66   ToolkitTestApplication application;
67   tet_infoline( "UtcDaliAnimatedImageVisualGetPropertyMap" );
68
69   // request AnimatedImageVisual with a property map
70   VisualFactory factory = VisualFactory::Get();
71   Visual::Base animatedImageVisual = factory.CreateVisual(
72     Property::Map()
73     .Add( Toolkit::Visual::Property::TYPE, Visual::ANIMATED_IMAGE )
74     .Add( ImageVisual::Property::URL, TEST_GIF_FILE_NAME )
75     .Add( ImageVisual::Property::PIXEL_AREA, Vector4() )
76     .Add( ImageVisual::Property::WRAP_MODE_U, WrapMode::REPEAT )
77     .Add( ImageVisual::Property::WRAP_MODE_V, WrapMode::DEFAULT ));
78
79   Property::Map resultMap;
80   animatedImageVisual.CreatePropertyMap( resultMap );
81   // check the property values from the returned map from a visual
82   Property::Value* value = resultMap.Find( Toolkit::Visual::Property::TYPE,  Property::INTEGER );
83   DALI_TEST_CHECK( value );
84   DALI_TEST_CHECK( value->Get<int>() == Visual::ANIMATED_IMAGE );
85
86   value = resultMap.Find( ImageVisual::Property::URL,  Property::STRING );
87   DALI_TEST_CHECK( value );
88   DALI_TEST_CHECK( value->Get<std::string>() == TEST_GIF_FILE_NAME );
89
90   // request AnimatedImageVisual with an URL
91   Visual::Base animatedImageVisual2 = factory.CreateVisual( TEST_GIF_FILE_NAME, ImageDimensions() );
92   resultMap.Clear();
93   animatedImageVisual2.CreatePropertyMap( resultMap );
94   // check the property values from the returned map from a visual
95   value = resultMap.Find( Toolkit::Visual::Property::TYPE,  Property::INTEGER );
96   DALI_TEST_CHECK( value );
97   DALI_TEST_CHECK( value->Get<int>() == Visual::ANIMATED_IMAGE );
98
99   value = resultMap.Find( ImageVisual::Property::URL,  Property::STRING );
100   DALI_TEST_CHECK( value );
101   DALI_TEST_CHECK( value->Get<std::string>() == TEST_GIF_FILE_NAME );
102
103   END_TEST;
104 }
105
106
107 int UtcDaliAnimatedImageVisualGetPropertyMap02(void)
108 {
109   ToolkitTestApplication application;
110   tet_infoline( "UtcDaliAnimatedImageVisualGetPropertyMap for multi image" );
111
112   // request AnimatedImageVisual with a property map
113   VisualFactory factory = VisualFactory::Get();
114   Property::Array urls;
115   CopyUrlsIntoArray( urls );
116
117   Visual::Base animatedImageVisual = factory.CreateVisual(
118     Property::Map()
119     .Add( Toolkit::Visual::Property::TYPE, Visual::ANIMATED_IMAGE )
120     .Add( "url", urls )
121     .Add( "batchSize", 4 )
122     .Add( "cacheSize", 8 )
123     .Add( "loopCount", 10 )
124     .Add( "frameDelay", 200 )
125     .Add( "pixelArea", Vector4() )
126     .Add( "wrapModeU", WrapMode::REPEAT )
127     .Add( "wrapModeV", WrapMode::DEFAULT ));
128
129   Property::Map resultMap;
130   animatedImageVisual.CreatePropertyMap( resultMap );
131   // check the property values from the returned map from a visual
132   Property::Value* value = resultMap.Find( Toolkit::Visual::Property::TYPE,  Property::INTEGER );
133   DALI_TEST_CHECK( value );
134   DALI_TEST_CHECK( value->Get<int>() == Visual::ANIMATED_IMAGE );
135
136   value = resultMap.Find( ImageVisual::Property::URL, "url" );
137   DALI_TEST_CHECK( value );
138   Property::Array* resultUrls = value->GetArray();
139   DALI_TEST_CHECK( resultUrls );
140   DALI_TEST_EQUALS( resultUrls->Count(), urls.Count(), TEST_LOCATION );
141
142   value = resultMap.Find( ImageVisual::Property::BATCH_SIZE, "batchSize" );
143   DALI_TEST_CHECK( value );
144   DALI_TEST_EQUALS( value->Get<int>(), 4, TEST_LOCATION );
145
146   value = resultMap.Find( ImageVisual::Property::CACHE_SIZE, "cacheSize" );
147   DALI_TEST_CHECK( value );
148   DALI_TEST_EQUALS( value->Get<int>(), 8, TEST_LOCATION );
149
150   value = resultMap.Find( Toolkit::DevelImageVisual::Property::LOOP_COUNT, "loopCount" );
151   DALI_TEST_CHECK( value );
152   DALI_TEST_EQUALS( value->Get<int>(), 10, TEST_LOCATION );
153
154   value = resultMap.Find( ImageVisual::Property::FRAME_DELAY, "frameDelay" );
155   DALI_TEST_CHECK( value );
156   DALI_TEST_EQUALS( value->Get<int>(), 200, TEST_LOCATION );
157
158   END_TEST;
159 }
160
161
162 int UtcDaliAnimatedImageVisualJumpToAction(void)
163 {
164   ToolkitTestApplication application;
165   TestGlAbstraction& gl = application.GetGlAbstraction();
166
167   Property::Array urls;
168   CopyUrlsIntoArray( urls );
169
170   {
171     Property::Map propertyMap;
172     propertyMap.Insert(Visual::Property::TYPE, Visual::IMAGE );
173     propertyMap.Insert( ImageVisual::Property::URL, Property::Value(urls) );
174     propertyMap.Insert( ImageVisual::Property::BATCH_SIZE, 4);
175     propertyMap.Insert( ImageVisual::Property::CACHE_SIZE, 12);
176     propertyMap.Insert( ImageVisual::Property::FRAME_DELAY, 20);
177
178     VisualFactory factory = VisualFactory::Get();
179     Visual::Base visual = factory.CreateVisual( propertyMap );
180
181     DummyControl dummyControl = DummyControl::New(true);
182     Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(dummyControl.GetImplementation());
183     dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
184
185     dummyControl.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
186     Stage::GetCurrent().Add( dummyControl );
187     application.SendNotification();
188     application.Render(20);
189
190     tet_infoline( "Ready the visual after the visual is on stage" );
191     DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 4 ), true, TEST_LOCATION );
192
193     tet_infoline( "Test that a timer has been started" );
194     DALI_TEST_EQUALS( Test::GetTimerCount(), 1, TEST_LOCATION );
195
196     TraceCallStack& textureTrace = gl.GetTextureTrace();
197     textureTrace.Enable(true);
198
199     application.SendNotification();
200     application.Render(20);
201
202     DALI_TEST_EQUALS( gl.GetLastGenTextureId(), 4, TEST_LOCATION );
203
204     DevelControl::DoAction( dummyControl, DummyControl::Property::TEST_VISUAL, Dali::Toolkit::DevelAnimatedImageVisual::Action::STOP, Property::Map() );
205
206     DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 4, TEST_LOCATION );
207
208     DevelControl::DoAction( dummyControl, DummyControl::Property::TEST_VISUAL, Dali::Toolkit::DevelAnimatedImageVisual::Action::JUMP_TO, 20 );
209
210     DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 4, TEST_LOCATION );
211
212     DevelControl::DoAction( dummyControl, DummyControl::Property::TEST_VISUAL, Dali::Toolkit::DevelAnimatedImageVisual::Action::JUMP_TO, 6 );
213
214     DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 6 ), true, TEST_LOCATION );
215     DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 4, TEST_LOCATION );
216
217     dummyControl.Unparent();
218   }
219   tet_infoline("Test that removing the visual from stage deletes all textures");
220   application.SendNotification();
221   application.Render(16);
222   DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 0, TEST_LOCATION );
223
224   END_TEST;
225 }
226
227
228 int UtcDaliAnimatedImageVisualStopBehavior(void)
229 {
230   ToolkitTestApplication application;
231   TestGlAbstraction& gl = application.GetGlAbstraction();
232
233   Property::Array urls;
234   CopyUrlsIntoArray( urls );
235
236   {
237     Property::Map propertyMap;
238     propertyMap.Insert( Visual::Property::TYPE, Visual::IMAGE );
239     propertyMap.Insert( ImageVisual::Property::URL, Property::Value(urls) );
240     propertyMap.Insert( DevelImageVisual::Property::STOP_BEHAVIOR, DevelImageVisual::StopBehavior::FIRST_FRAME);
241     propertyMap.Insert( ImageVisual::Property::BATCH_SIZE, 4);
242     propertyMap.Insert( ImageVisual::Property::CACHE_SIZE, 8);
243     propertyMap.Insert( ImageVisual::Property::FRAME_DELAY, 20);
244
245     VisualFactory factory = VisualFactory::Get();
246     Visual::Base visual = factory.CreateVisual( propertyMap );
247
248     // Expect that a batch of 4 textures has been requested. These will be serially loaded
249     // below.
250
251     DummyControl dummyControl = DummyControl::New(true);
252     Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(dummyControl.GetImplementation());
253     dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
254
255     dummyControl.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
256     Stage::GetCurrent().Add( dummyControl );
257     application.SendNotification();
258     application.Render(20);
259
260     tet_infoline( "Ready the visual after the visual is on stage" );
261     DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 4 ), true, TEST_LOCATION );
262
263     tet_infoline( "Test that a timer has been started" );
264     DALI_TEST_EQUALS( Test::GetTimerCount(), 1, TEST_LOCATION );
265
266     TraceCallStack& textureTrace = gl.GetTextureTrace();
267     textureTrace.Enable(true);
268
269     application.SendNotification();
270     application.Render(20);
271
272     DALI_TEST_EQUALS( gl.GetLastGenTextureId(), 4, TEST_LOCATION );
273
274     DevelControl::DoAction( dummyControl, DummyControl::Property::TEST_VISUAL, Dali::Toolkit::DevelAnimatedImageVisual::Action::STOP, Property::Map() );
275
276     DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 4, TEST_LOCATION );
277
278     DevelControl::DoAction( dummyControl, DummyControl::Property::TEST_VISUAL, Dali::Toolkit::DevelAnimatedImageVisual::Action::JUMP_TO, 1 );
279
280     // Expect the second batch has been requested
281     DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 4 ), true, TEST_LOCATION );
282
283     DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 4, TEST_LOCATION );
284
285     dummyControl.Unparent();
286   }
287   tet_infoline("Test that removing the visual from stage deletes all textures");
288   application.SendNotification();
289   application.Render(16);
290   DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 0, TEST_LOCATION );
291
292   END_TEST;
293 }
294
295
296 int UtcDaliAnimatedImageVisualAnimatedImage01(void)
297 {
298   ToolkitTestApplication application;
299   TestGlAbstraction& gl = application.GetGlAbstraction();
300
301   {
302     Property::Map propertyMap;
303     propertyMap.Insert(Visual::Property::TYPE, Visual::ANIMATED_IMAGE );
304     propertyMap.Insert( ImageVisual::Property::URL, TEST_GIF_FILE_NAME );
305     propertyMap.Insert( ImageVisual::Property::BATCH_SIZE, 2);
306     propertyMap.Insert( ImageVisual::Property::CACHE_SIZE, 4);
307     propertyMap.Insert( ImageVisual::Property::FRAME_DELAY, 20);
308
309     VisualFactory factory = VisualFactory::Get();
310     Visual::Base visual = factory.CreateVisual( propertyMap );
311
312     // Expect that a batch of 4 textures has been requested. These will be serially loaded
313     // below.
314
315     DummyControl dummyControl = DummyControl::New(true);
316     Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(dummyControl.GetImplementation());
317     dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
318
319     dummyControl.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
320     Stage::GetCurrent().Add( dummyControl );
321     application.SendNotification();
322     application.Render(20);
323
324     DALI_TEST_EQUALS( gl.GetLastGenTextureId(), 2, TEST_LOCATION );
325
326     tet_infoline( "Test that a timer has been started" );
327
328     TraceCallStack& textureTrace = gl.GetTextureTrace();
329     textureTrace.Enable(true);
330
331     Test::EmitGlobalTimerSignal();
332
333     application.SendNotification();
334     application.Render(20);
335
336     DALI_TEST_EQUALS( gl.GetLastGenTextureId(), 4, TEST_LOCATION );
337
338     dummyControl.Unparent();
339   }
340   tet_infoline("Test that removing the visual from stage deletes all textures");
341   application.SendNotification();
342   application.Render(20);
343   DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 0, TEST_LOCATION );
344
345   END_TEST;
346 }
347
348
349 int UtcDaliAnimatedImageVisualMultiImage01(void)
350 {
351   ToolkitTestApplication application;
352   TestGlAbstraction& gl = application.GetGlAbstraction();
353
354   Property::Array urls;
355   CopyUrlsIntoArray( urls );
356
357   {
358     Property::Map propertyMap;
359     propertyMap.Insert(Visual::Property::TYPE, Visual::IMAGE );
360     propertyMap.Insert( ImageVisual::Property::URL, Property::Value(urls) );
361     propertyMap.Insert( ImageVisual::Property::BATCH_SIZE, 4);
362     propertyMap.Insert( ImageVisual::Property::CACHE_SIZE, 8);
363     propertyMap.Insert( ImageVisual::Property::FRAME_DELAY, 100);
364
365     VisualFactory factory = VisualFactory::Get();
366     Visual::Base visual = factory.CreateVisual( propertyMap );
367
368     // Expect that a batch of 4 textures has been requested. These will be serially loaded
369     // below.
370
371     DummyControl dummyControl = DummyControl::New(true);
372     Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(dummyControl.GetImplementation());
373     dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
374
375     dummyControl.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
376     Stage::GetCurrent().Add( dummyControl );
377     application.SendNotification();
378     application.Render(16);
379
380     tet_infoline( "Ready the visual after the visual is on stage" );
381     DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 4 ), true, TEST_LOCATION );
382
383     tet_infoline( "Test that a timer has been started" );
384     DALI_TEST_EQUALS( Test::GetTimerCount(), 1, TEST_LOCATION );
385
386     TraceCallStack& textureTrace = gl.GetTextureTrace();
387     textureTrace.Enable(true);
388
389     application.SendNotification();
390     application.Render(16);
391
392     DALI_TEST_EQUALS( gl.GetLastGenTextureId(), 4, TEST_LOCATION );
393     DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
394
395     tet_infoline( "Test that after 1 tick, and file loads completed, that we have 7 textures" );
396     Test::EmitGlobalTimerSignal();
397
398     // Expect the second batch has been requested
399     DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 4 ), true, TEST_LOCATION );
400
401     application.SendNotification();
402     application.Render(16);
403     DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 7, TEST_LOCATION );
404
405
406     tet_infoline( "Test that after 2 ticks that we have 6 textures" );
407
408     Test::EmitGlobalTimerSignal();
409     application.SendNotification();
410     application.Render(16);
411     DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 6, TEST_LOCATION );
412
413     tet_infoline("And that at least 2 textures were requested");
414     DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 2 ), true, TEST_LOCATION );
415     application.SendNotification();
416     application.Render(16);
417     DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 8, TEST_LOCATION );
418
419
420     tet_infoline( "Test that after 3rd tick that we have 7 textures and 1 request" );
421     Test::EmitGlobalTimerSignal();
422     application.SendNotification();
423     application.Render(16);
424     DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 7, TEST_LOCATION );
425
426     DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
427     application.SendNotification();
428     application.Render(16);
429     DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 8, TEST_LOCATION );
430
431     dummyControl.Unparent();
432   }
433   tet_infoline("Test that removing the visual from stage deletes all textures");
434   application.SendNotification();
435   application.Render(16);
436   DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 0, TEST_LOCATION );
437
438   END_TEST;
439 }
440
441 int UtcDaliAnimatedImageVisualMultiImage02(void)
442 {
443   ToolkitTestApplication application;
444   TestGlAbstraction& gl = application.GetGlAbstraction();
445
446   tet_infoline( "Test that the animated visual still works with zero sized cache" );
447
448   {
449     Property::Array urls;
450     CopyUrlsIntoArray( urls );
451
452     Property::Map propertyMap;
453     propertyMap.Insert(Visual::Property::TYPE, Visual::IMAGE );
454     propertyMap.Insert( ImageVisual::Property::URL, Property::Value(urls) );
455     propertyMap.Insert( ImageVisual::Property::BATCH_SIZE, 0);
456     propertyMap.Insert( ImageVisual::Property::CACHE_SIZE, 0);
457     propertyMap.Insert( ImageVisual::Property::FRAME_DELAY, 100);
458
459     VisualFactory factory = VisualFactory::Get();
460     Visual::Base visual = factory.CreateVisual( propertyMap ); // TexMgr::Request load tId:0
461
462     // Expect that each image is loaded each tick
463
464     DummyControl dummyControl = DummyControl::New(true);
465     Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(dummyControl.GetImplementation());
466     dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
467
468     dummyControl.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
469     Stage::GetCurrent().Add( dummyControl );
470     application.SendNotification();
471     application.Render(16);
472
473     tet_infoline( "Ready the visual after the visual is on stage" );
474     DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
475     application.SendNotification();
476     application.Render(16);//glGenTextures 1
477     DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 1, TEST_LOCATION );
478
479     tet_infoline( "Test that each tick, a new image is requested" );
480     Test::EmitGlobalTimerSignal(); // TexMgr::Remove tId:0
481     application.SendNotification();
482     application.Render(16);
483     DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1, 10 ), true, TEST_LOCATION );
484     application.SendNotification();
485     application.Render(16);//glGenTextures 2
486     DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 1, TEST_LOCATION );
487
488     tet_infoline( "Test that each tick, a new image is requested" );
489     Test::EmitGlobalTimerSignal(); // Internal::~TextureSet()
490     application.SendNotification();
491     application.Render(16);//glDeleteTextures 2
492     DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1, 10 ), true, TEST_LOCATION );
493     application.SendNotification();
494     application.Render(16);//glGenTextures 3
495     DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 1, TEST_LOCATION );
496
497     tet_infoline( "Test that each tick, a new image is requested" );
498     Test::EmitGlobalTimerSignal();
499     application.SendNotification();
500     application.Render(16);//glDeleteTextures 3
501     DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1, 10 ), true, TEST_LOCATION );
502     application.SendNotification();
503     application.Render(16);//Gen4
504     DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 1, TEST_LOCATION );
505     dummyControl.Unparent();
506   }
507   tet_infoline("Test that removing the visual from stage deletes all textures");
508   application.SendNotification();
509   application.Render(16);
510   DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 0, TEST_LOCATION );
511
512   END_TEST;
513 }
514
515 int UtcDaliAnimatedImageVisualMultiImage03(void)
516 {
517   ToolkitTestApplication application;
518   TestGlAbstraction& gl = application.GetGlAbstraction();
519
520   {
521     Property::Array urls1, urls2;
522     CopyUrlsIntoArray( urls1 );
523     CopyUrlsIntoArray( urls2 );
524
525     Property::Map animatedImageMap1;
526     animatedImageMap1.Insert(Visual::Property::TYPE, Visual::IMAGE );
527     animatedImageMap1.Insert( ImageVisual::Property::URL, Property::Value(urls1) );
528     animatedImageMap1.Insert( ImageVisual::Property::BATCH_SIZE, 3);
529     animatedImageMap1.Insert( ImageVisual::Property::CACHE_SIZE, 3);
530     animatedImageMap1.Insert( ImageVisual::Property::FRAME_DELAY, 100);
531
532     Property::Map animatedImageMap2;
533     animatedImageMap2.Insert(Visual::Property::TYPE, Visual::IMAGE );
534     animatedImageMap2.Insert( ImageVisual::Property::URL, Property::Value(urls2) );
535     animatedImageMap2.Insert( ImageVisual::Property::BATCH_SIZE, 2);
536     animatedImageMap2.Insert( ImageVisual::Property::CACHE_SIZE, 2);
537     animatedImageMap2.Insert( ImageVisual::Property::FRAME_DELAY, 100);
538
539     VisualFactory factory = VisualFactory::Get();
540     Visual::Base animatedImageVisual1 = factory.CreateVisual( animatedImageMap1 );
541
542     tet_infoline( "Create two image views with the same URLs, offset by 1 frame.");
543
544     DummyControl dummyControl1 = DummyControl::New(true);
545     Impl::DummyControl& dummyImpl1 = static_cast<Impl::DummyControl&>(dummyControl1.GetImplementation());
546     dummyImpl1.RegisterVisual( DummyControl::Property::TEST_VISUAL, animatedImageVisual1 );
547     dummyControl1.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
548     Stage::GetCurrent().Add( dummyControl1 );
549
550     application.SendNotification();
551     application.Render(16);
552
553     tet_infoline( "Ready the requested image after the first visual is on stage" );
554     DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
555     application.SendNotification();
556     application.Render(16);
557     DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 3, TEST_LOCATION );
558
559     Visual::Base animatedImageVisual2 = factory.CreateVisual( animatedImageMap2 );
560     DummyControl dummyControl2 = DummyControl::New(true);
561     Impl::DummyControl& dummyImpl2 = static_cast<Impl::DummyControl&>(dummyControl2.GetImplementation());
562     dummyImpl2.RegisterVisual( DummyControl::Property::TEST_VISUAL, animatedImageVisual2 );
563     dummyControl2.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
564     Stage::GetCurrent().Add( dummyControl2 );
565     application.SendNotification();
566     application.Render(16);
567
568     tet_infoline( "The texture cache should be holding the requested images; check that the renderer has a texture" );
569     TextureSet ts = dummyControl2.GetRendererAt(0).GetTextures();
570     Texture t1 = ts.GetTexture( 0 );
571     DALI_TEST_EQUALS( ts.GetTextureCount(), 1, TEST_LOCATION );
572
573     tet_infoline( "Test that on the first tick, 1 new image is requested" );
574     Test::EmitGlobalTimerSignal(); // Both visuals should tick
575
576     DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
577     DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 3, TEST_LOCATION );
578
579     ts = dummyControl2.GetRendererAt(0).GetTextures();
580     Texture t2 = ts.GetTexture( 0 );
581     DALI_TEST_CHECK( t1 != t2 );
582
583     dummyControl1.Unparent();
584     dummyControl2.Unparent();
585   }
586   tet_infoline("Test that removing the visual from stage deletes all textures");
587   application.SendNotification();
588   application.Render(16);
589   DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 0, TEST_LOCATION );
590
591   END_TEST;
592 }
593
594
595 int UtcDaliAnimatedImageVisualMultiImage04(void)
596 {
597   ToolkitTestApplication application;
598   TestGlAbstraction& gl = application.GetGlAbstraction();
599   TraceCallStack& textureTrace = gl.GetTextureTrace();
600   textureTrace.Enable(true);
601
602   tet_infoline( "Test that if the cache size is the same as the number of urls, that once the cache is full, no new images are loaded" );
603
604   Property::Array urls;
605   CopyUrlsIntoArray( urls );
606
607   {
608     Property::Map propertyMap;
609     propertyMap.Insert(Visual::Property::TYPE, Visual::IMAGE );
610     propertyMap.Insert( ImageVisual::Property::URL, Property::Value(urls) );
611     propertyMap.Insert( ImageVisual::Property::BATCH_SIZE, 6);
612     propertyMap.Insert( ImageVisual::Property::CACHE_SIZE, 11);
613     propertyMap.Insert( ImageVisual::Property::FRAME_DELAY, 100);
614
615     VisualFactory factory = VisualFactory::Get();
616     Visual::Base visual = factory.CreateVisual( propertyMap );
617
618     tet_infoline( "Expect that a batch of 7 textures has been requested." );
619
620     DummyControl dummyControl = DummyControl::New(true);
621     Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(dummyControl.GetImplementation());
622     dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
623
624     dummyControl.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
625     Stage::GetCurrent().Add( dummyControl );
626     application.SendNotification();
627     application.Render(16);
628
629     tet_infoline( "Wait for the first batch to complete" );
630     DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 6 ), true, TEST_LOCATION );
631
632     tet_infoline( "Test that a timer has been started" );
633     DALI_TEST_EQUALS( Test::GetTimerCount(), 1, TEST_LOCATION );
634
635     application.SendNotification();
636     application.Render(16);
637
638     DALI_TEST_EQUALS( gl.GetLastGenTextureId(), 6, TEST_LOCATION );
639     tet_infoline( "Test that after 1 tick, and 5 file loads completed, that we have 11 textures" );
640     Test::EmitGlobalTimerSignal();
641     application.SendNotification();
642     application.Render(16);
643
644     // Expect the second batch has been requested
645     DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 5 ), true, TEST_LOCATION );
646
647     application.SendNotification();
648     application.Render(16);
649     DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 11, TEST_LOCATION );
650
651     tet_infoline( "Test that after 2 ticks that we have 11 textures and no requests" );
652
653     Test::EmitGlobalTimerSignal();
654     application.SendNotification();
655     application.Render(16);
656     DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1, 5 ), false, TEST_LOCATION );
657     application.SendNotification();
658     application.Render(16);
659     DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 11, TEST_LOCATION );
660
661     tet_infoline( "Test that after 3rd tick that we have 11 textures and no requests" );
662     Test::EmitGlobalTimerSignal();
663     application.SendNotification();
664     application.Render(16);
665
666     DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1, 5 ), false, TEST_LOCATION );
667     application.SendNotification();
668     application.Render(16);
669     DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 11, TEST_LOCATION );
670
671     dummyControl.Unparent();
672   }
673
674   tet_infoline("Test that removing the visual from stage deletes all textures");
675   application.SendNotification();
676   application.Render(16);
677   DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 0, TEST_LOCATION );
678
679   END_TEST;
680 }
681
682
683 int UtcDaliAnimatedImageVisualMultiImage05(void)
684 {
685   ToolkitTestApplication application;
686   TestGlAbstraction& gl = application.GetGlAbstraction();
687
688   tet_infoline( "Test that if the cache size is the same as the number of urls, that removing a partially loaded visual removes all textures" );
689
690   Property::Array urls;
691   CopyUrlsIntoArray( urls );
692
693   {
694     Property::Map propertyMap;
695     propertyMap.Insert(Visual::Property::TYPE, Visual::IMAGE );
696     propertyMap.Insert( ImageVisual::Property::URL, Property::Value(urls) );
697     propertyMap.Insert( ImageVisual::Property::BATCH_SIZE, 4);
698     propertyMap.Insert( ImageVisual::Property::CACHE_SIZE, 11);
699     propertyMap.Insert( ImageVisual::Property::FRAME_DELAY, 100);
700
701     VisualFactory factory = VisualFactory::Get();
702     Visual::Base visual = factory.CreateVisual( propertyMap );
703
704     tet_infoline( "Expect that a batch of 4 textures has been requested." );
705
706     DummyControl dummyControl = DummyControl::New(true);
707     Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(dummyControl.GetImplementation());
708     dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
709
710     dummyControl.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
711     Stage::GetCurrent().Add( dummyControl );
712     application.SendNotification();
713     application.Render(16);
714
715     tet_infoline( "Wait for the first batch to complete" );
716     DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 4 ), true, TEST_LOCATION );
717
718     tet_infoline( "Test that a timer has been started" );
719     DALI_TEST_EQUALS( Test::GetTimerCount(), 1, TEST_LOCATION );
720
721     application.SendNotification();
722     application.Render(16);
723
724     tet_infoline( "Test that a timer has been started" );
725     Test::EmitGlobalTimerSignal();
726     application.SendNotification();
727     application.Render(16);
728
729     dummyControl.Unparent();
730   }
731
732   application.SendNotification();
733   application.Render(16);
734   DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 0, TEST_LOCATION );
735
736   tet_infoline( "Test that pending batch of image loads are cancelled instead of uploaded");
737   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 4 ), true, TEST_LOCATION );
738   application.SendNotification();
739   application.Render(16);
740   DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 0, TEST_LOCATION );
741
742   END_TEST;
743 }
744
745 void TestLoopCount( ToolkitTestApplication &application, DummyControl &dummyControl, uint16_t frameCount, uint16_t loopCount, const char * location )
746 {
747   TestGlAbstraction& gl = application.GetGlAbstraction();
748   TraceCallStack& textureTrace = gl.GetTextureTrace();
749
750   textureTrace.Enable(true);
751   Stage::GetCurrent().Add( dummyControl );
752   application.SendNotification();
753   application.Render(16);
754
755   tet_infoline( "Test that a timer has been created" );
756   DALI_TEST_EQUALS( Test::GetTimerCount(), 1, TEST_INNER_LOCATION( location ) );
757
758   for ( uint16_t i = 0; i < loopCount; i++ )
759   {
760     for ( uint16_t j = 0; j < frameCount; j++ )
761     {
762       if( i == 0 && j == 0 )
763       {
764         continue; // Because first frame is already showed and we call 2nd frame at the first time of timer animation.
765       }
766       tet_printf( "Test that after %u ticks, and we have %u frame \n", j + 1u, j + 1u );
767       Test::EmitGlobalTimerSignal();
768       application.SendNotification();
769       application.Render(16);
770       DALI_TEST_EQUALS( gl.GetNumGeneratedTextures(), 1, TEST_INNER_LOCATION( location ) );
771       DALI_TEST_EQUALS( Test::AreTimersRunning(), true, TEST_INNER_LOCATION( location ) );
772     }
773     tet_printf( "\nTest Loop %u \n", i );
774   }
775
776   tet_printf( "Test that after %u loops, and we have no frame. Timer should stop \n", loopCount );
777   Test::EmitGlobalTimerSignal();
778   application.SendNotification();
779   application.Render(16);
780   DALI_TEST_EQUALS( Test::AreTimersRunning(), false, TEST_INNER_LOCATION( location ) );
781
782   dummyControl.Unparent();
783 }
784
785 int UtcDaliAnimatedImageVisualLoopCount(void)
786 {
787   ToolkitTestApplication application;
788
789   tet_infoline( "UtcDaliAnimatedImageVisualLoopCount" );
790
791   {
792     // request AnimatedImageVisual with a property map
793     // Test with no (0) loop count
794     VisualFactory factory = VisualFactory::Get();
795     Visual::Base animatedImageVisual = factory.CreateVisual(
796       Property::Map()
797       .Add( Toolkit::Visual::Property::TYPE, Visual::ANIMATED_IMAGE )
798       .Add( ImageVisual::Property::URL, TEST_GIF_FILE_NAME )
799       .Add( ImageVisual::Property::PIXEL_AREA, Vector4() )
800       .Add( ImageVisual::Property::WRAP_MODE_U, WrapMode::REPEAT )
801       .Add( ImageVisual::Property::WRAP_MODE_V, WrapMode::DEFAULT )
802       .Add( DevelImageVisual::Property::LOOP_COUNT, 0 ));
803
804     DummyControl dummyControl = DummyControl::New(true);
805     Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(dummyControl.GetImplementation());
806     dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, animatedImageVisual );
807     dummyControl.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
808
809     TestLoopCount( application, dummyControl, 4, 0, TEST_LOCATION );
810
811     // Test with no (1) loop count. Request AnimatedImageVisual with a property map
812     animatedImageVisual = factory.CreateVisual(
813       Property::Map()
814       .Add( Toolkit::Visual::Property::TYPE, Visual::ANIMATED_IMAGE )
815       .Add( ImageVisual::Property::URL, TEST_GIF_FILE_NAME )
816       .Add( ImageVisual::Property::PIXEL_AREA, Vector4() )
817       .Add( ImageVisual::Property::WRAP_MODE_U, WrapMode::REPEAT )
818       .Add( ImageVisual::Property::WRAP_MODE_V, WrapMode::DEFAULT )
819       .Add( DevelImageVisual::Property::LOOP_COUNT, 1 ));
820
821     dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, animatedImageVisual );
822
823     TestLoopCount( application, dummyControl, 4, 1, TEST_LOCATION );
824
825     // Test with no (100) loop count. Request AnimatedImageVisual with a property map
826     animatedImageVisual = factory.CreateVisual(
827       Property::Map()
828       .Add( Toolkit::Visual::Property::TYPE, Visual::ANIMATED_IMAGE )
829       .Add( ImageVisual::Property::URL, TEST_GIF_FILE_NAME )
830       .Add( ImageVisual::Property::PIXEL_AREA, Vector4() )
831       .Add( ImageVisual::Property::WRAP_MODE_U, WrapMode::REPEAT )
832       .Add( ImageVisual::Property::WRAP_MODE_V, WrapMode::DEFAULT )
833       .Add( DevelImageVisual::Property::LOOP_COUNT, 100 ));
834
835     dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, animatedImageVisual );
836
837     TestLoopCount( application, dummyControl, 4, 100, TEST_LOCATION );
838   }
839
840   END_TEST;
841 }
842
843 int UtcDaliAnimatedImageVisualPlayback(void)
844 {
845   ToolkitTestApplication application;
846   TestGlAbstraction& gl = application.GetGlAbstraction();
847   TraceCallStack& textureTrace = gl.GetTextureTrace();
848
849   tet_infoline( "UtcDaliAnimatedImageVisualPlayback" );
850
851   {
852     // request AnimatedImageVisual with a property map
853     // Test with forever (-1) loop count
854     VisualFactory factory = VisualFactory::Get();
855     Visual::Base animatedImageVisual = factory.CreateVisual(
856       Property::Map()
857       .Add( Toolkit::Visual::Property::TYPE, Visual::ANIMATED_IMAGE )
858       .Add( ImageVisual::Property::URL, TEST_GIF_FILE_NAME )
859       .Add( ImageVisual::Property::PIXEL_AREA, Vector4() )
860       .Add( ImageVisual::Property::WRAP_MODE_U, WrapMode::REPEAT )
861       .Add( ImageVisual::Property::WRAP_MODE_V, WrapMode::DEFAULT )
862       .Add( DevelImageVisual::Property::LOOP_COUNT, -1 ));
863
864     DummyControl dummyControl = DummyControl::New(true);
865     Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(dummyControl.GetImplementation());
866     dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, animatedImageVisual );
867     dummyControl.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
868
869     textureTrace.Enable(true);
870     Stage::GetCurrent().Add( dummyControl );
871     application.SendNotification();
872     application.Render(16);
873
874     tet_infoline( "Test that a timer has been created" );
875     DALI_TEST_EQUALS( Test::GetTimerCount(), 1, TEST_LOCATION );
876
877     Test::EmitGlobalTimerSignal();
878     application.SendNotification();
879     application.Render(16);
880     DALI_TEST_EQUALS( Test::AreTimersRunning(), true, TEST_LOCATION );
881
882     Property::Map attributes;
883     tet_infoline( "Test Pause action. Timer should stop after Pause action" );
884     DevelControl::DoAction( dummyControl, DummyControl::Property::TEST_VISUAL, Dali::Toolkit::DevelAnimatedImageVisual::Action::PAUSE, attributes );
885     Test::EmitGlobalTimerSignal();
886     application.SendNotification();
887     application.Render(16);
888     DALI_TEST_EQUALS( Test::AreTimersRunning(), false, TEST_LOCATION );
889
890     tet_infoline( "Test Play action. Timer should Restart after Play action" );
891     DevelControl::DoAction( dummyControl, DummyControl::Property::TEST_VISUAL, Dali::Toolkit::DevelAnimatedImageVisual::Action::PLAY, attributes );
892     Test::EmitGlobalTimerSignal();
893     application.SendNotification();
894     application.Render(16);
895     DALI_TEST_EQUALS( Test::AreTimersRunning(), true, TEST_LOCATION );
896
897     tet_infoline( "Test Stop action. Timer should stop after Stop action" );
898     DevelControl::DoAction( dummyControl, DummyControl::Property::TEST_VISUAL, Dali::Toolkit::DevelAnimatedImageVisual::Action::STOP, attributes );
899     Test::EmitGlobalTimerSignal();
900     application.SendNotification();
901     application.Render(16);
902     DALI_TEST_EQUALS( Test::AreTimersRunning(), false, TEST_LOCATION );
903
904     tet_infoline( "Test Play action. Timer should Restart after Play action" );
905     DevelControl::DoAction( dummyControl, DummyControl::Property::TEST_VISUAL, Dali::Toolkit::DevelAnimatedImageVisual::Action::PLAY, attributes );
906     Test::EmitGlobalTimerSignal();
907     application.SendNotification();
908     application.Render(16);
909     DALI_TEST_EQUALS( Test::AreTimersRunning(), true, TEST_LOCATION );
910
911     dummyControl.Unparent();
912   }
913
914   END_TEST;
915 }