Switch Sync- and AsynchImageLoaders to use new (object less) image loading adaptor API
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-ImageView.cpp
1 /*
2  * Copyright (c) 2016 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
18 // Need to override adaptor classes for toolkit test harness, so include
19 // test harness headers before dali headers.
20 #include <dali-toolkit-test-suite-utils.h>
21 #include <toolkit-bitmap-loader.h>
22 #include <toolkit-event-thread-callback.h>
23
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <dali/devel-api/scripting/scripting.h>
26 #include <dali/public-api/rendering/renderer.h>
27
28 #include <test-native-image.h>
29 #include <sstream>
30
31 using namespace Dali;
32 using namespace Toolkit;
33
34 void utc_dali_toolkit_image_view_startup(void)
35 {
36   test_return_value = TET_UNDEF;
37 }
38
39 void utc_dali_toolkit_image_view_cleanup(void)
40 {
41   test_return_value = TET_PASS;
42 }
43
44 namespace
45 {
46
47 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
48   attribute mediump vec2 aPosition;\n
49   varying mediump vec2 vTexCoord;\n
50   uniform mediump mat4 uMvpMatrix;\n
51   uniform mediump vec3 uSize;\n
52   \n
53   void main()\n
54   {\n
55     mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
56     vertexPosition.xyz *= uSize;\n
57     vertexPosition = uMvpMatrix * vertexPosition;\n
58     \n
59     vTexCoord = aPosition + vec2(0.5);\n
60     gl_Position = vertexPosition;\n
61   }\n
62 );
63
64 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
65   varying mediump vec2 vTexCoord;\n
66   uniform sampler2D sTexture;\n
67   uniform lowp vec4 uColor;\n
68   \n
69   void main()\n
70   {\n
71     gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
72   }\n
73 );
74
75 const char* TEST_IMAGE_FILE_NAME =  "gallery_image_01.jpg";
76 const char* TEST_IMAGE_FILE_NAME2 =  "gallery_image_02.jpg";
77
78 // resolution: 34*34, pixel format: RGBA8888
79 static const char* gImage_34_RGBA = TEST_RESOURCE_DIR "/icon-edit.png";
80 // resolution: 600*600, pixel format: RGB888
81 static const char* gImage_600_RGB = TEST_RESOURCE_DIR "/test-image-600.jpg";
82
83 void TestImage( ImageView imageView, BufferImage image )
84 {
85   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
86
87   Property::Map map;
88   DALI_TEST_CHECK( value.Get( map ) );
89
90   DALI_TEST_CHECK( map.Find( "width" ) );
91   DALI_TEST_CHECK( map.Find( "height" ) );
92   DALI_TEST_CHECK( map.Find( "type" ) );
93
94   int width = 0;
95   DALI_TEST_CHECK( map[ "width" ].Get( width ) );
96   DALI_TEST_EQUALS( (unsigned int)width, image.GetWidth(), TEST_LOCATION );
97
98   int height = 0;
99   DALI_TEST_CHECK( map[ "height" ].Get( height ) );
100   DALI_TEST_EQUALS( (unsigned int)height, image.GetHeight(), TEST_LOCATION );
101
102   std::string type;
103   DALI_TEST_CHECK( map[ "type" ].Get( type ) );
104   DALI_TEST_EQUALS( type, "BufferImage", TEST_LOCATION );
105 }
106
107 void TestImage( ImageView imageView, ResourceImage image )
108 {
109   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
110
111   Property::Map map;
112   DALI_TEST_CHECK( value.Get( map ) );
113
114   if( map.Find( "width" ) )
115   {
116     int width = 0;
117     DALI_TEST_CHECK( map[ "width" ].Get( width ) );
118     DALI_TEST_EQUALS( (unsigned int)width, image.GetWidth(), TEST_LOCATION );
119   }
120
121   if( map.Find( "height" ) )
122   {
123     int height = 0;
124     DALI_TEST_CHECK( map[ "height" ].Get( height ) );
125     DALI_TEST_EQUALS( (unsigned int)height, image.GetHeight(), TEST_LOCATION );
126   }
127
128   DALI_TEST_CHECK( map.Find( "type" ) );
129
130   std::string type;
131   DALI_TEST_CHECK( map[ "type" ].Get( type ) );
132   DALI_TEST_EQUALS( type, "ResourceImage", TEST_LOCATION );
133
134   std::string filename;
135   DALI_TEST_CHECK( map[ "filename" ].Get( filename ) );
136   DALI_TEST_EQUALS( filename, image.GetUrl(), TEST_LOCATION );
137 }
138
139 void TestUrl( ImageView imageView, const std::string url )
140 {
141   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
142
143   std::string urlActual;
144   DALI_TEST_CHECK( value.Get( urlActual ) );
145   DALI_TEST_EQUALS( urlActual, url, TEST_LOCATION );
146 }
147
148 } // namespace
149
150 int UtcDaliImageViewNewP(void)
151 {
152   TestApplication application;
153
154   ImageView imageView = ImageView::New();
155
156   DALI_TEST_CHECK( imageView );
157
158   END_TEST;
159 }
160
161 int UtcDaliImageViewNewImageP(void)
162 {
163   TestApplication application;
164
165   BufferImage image = CreateBufferImage( 100, 200, Vector4( 1.f, 1.f, 1.f, 1.f ) );
166   ImageView imageView = ImageView::New( image );
167
168   DALI_TEST_CHECK( imageView );
169   TestImage( imageView, image );
170
171   END_TEST;
172 }
173
174 int UtcDaliImageViewNewUrlP(void)
175 {
176   TestApplication application;
177
178   ImageView imageView = ImageView::New( TEST_IMAGE_FILE_NAME );
179   DALI_TEST_CHECK( imageView );
180
181   TestUrl( imageView, TEST_IMAGE_FILE_NAME );
182
183   END_TEST;
184 }
185
186 int UtcDaliImageViewConstructorP(void)
187 {
188   TestApplication application;
189
190   ImageView imageView;
191
192   DALI_TEST_CHECK( !imageView );
193
194   END_TEST;
195 }
196
197 int UtcDaliImageViewCopyConstructorP(void)
198 {
199   TestApplication application;
200
201   // Initialize an object, ref count == 1
202   ImageView imageView = ImageView::New();
203
204   ImageView copy( imageView );
205   DALI_TEST_CHECK( copy );
206
207   END_TEST;
208 }
209
210 int UtcDaliImageViewAssignmentOperatorP(void)
211 {
212   TestApplication application;
213
214   ImageView imageView = ImageView::New();
215
216   ImageView copy( imageView );
217   DALI_TEST_CHECK( copy );
218   DALI_TEST_EQUALS( imageView, copy, TEST_LOCATION );
219
220   END_TEST;
221 }
222
223 int UtcDaliImageViewDownCastP(void)
224 {
225   TestApplication application;
226
227   ImageView imageView = ImageView::New();
228
229   BaseHandle object(imageView);
230
231   ImageView imageView2 = ImageView::DownCast( object );
232   DALI_TEST_CHECK(imageView2);
233
234   ImageView imageView3 = DownCast< ImageView >( object );
235   DALI_TEST_CHECK(imageView3);
236
237   END_TEST;
238 }
239
240 int UtcDaliImageViewDownCastN(void)
241 {
242   TestApplication application;
243
244   BaseHandle unInitializedObject;
245
246   ImageView imageView1 = ImageView::DownCast( unInitializedObject );
247   DALI_TEST_CHECK( !imageView1 );
248
249   ImageView imageView2 = DownCast< ImageView >( unInitializedObject );
250   DALI_TEST_CHECK( !imageView2 );
251
252   END_TEST;
253 }
254
255 int UtcDaliImageViewTypeRegistry(void)
256 {
257   ToolkitTestApplication application;
258
259   TypeRegistry typeRegistry = TypeRegistry::Get();
260   DALI_TEST_CHECK( typeRegistry );
261
262   TypeInfo typeInfo = typeRegistry.GetTypeInfo( "ImageView" );
263   DALI_TEST_CHECK( typeInfo );
264
265   BaseHandle handle = typeInfo.CreateInstance();
266   DALI_TEST_CHECK( handle );
267
268   ImageView imageView = ImageView::DownCast( handle );
269   DALI_TEST_CHECK( imageView );
270
271   END_TEST;
272 }
273
274 int UtcDaliImageViewSetGetProperty01(void)
275 {
276   ToolkitTestApplication application;
277
278   ImageView imageView = ImageView::New();
279
280   Property::Index idx = imageView.GetPropertyIndex( "image" );
281   DALI_TEST_EQUALS( idx, (Property::Index)ImageView::Property::IMAGE, TEST_LOCATION );
282
283   imageView.SetProperty( idx, TEST_IMAGE_FILE_NAME );
284   TestUrl( imageView, TEST_IMAGE_FILE_NAME );
285
286   END_TEST;
287 }
288
289 int UtcDaliImageViewSetGetProperty02(void)
290 {
291   ToolkitTestApplication application;
292
293   Image image = CreateBufferImage( 10, 10, Color::WHITE );
294   ImageView imageView = ImageView::New(image);
295   Vector4 fullImageRect( 0.f, 0.f, 1.f, 1.f );
296
297   Stage::GetCurrent().Add( imageView );
298
299   application.SendNotification();
300   application.Render();
301   TestGlAbstraction& gl = application.GetGlAbstraction();
302
303   Vector4 pixelAreaUniform;
304   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "pixelArea", pixelAreaUniform ) );
305   DALI_TEST_EQUALS( pixelAreaUniform, fullImageRect, TEST_LOCATION );
306
307   Property::Value value = imageView.GetProperty( ImageView::Property::PIXEL_AREA );
308   Vector4 pixelAreaValue;
309   DALI_TEST_CHECK( value.Get(pixelAreaValue) );
310   DALI_TEST_EQUALS( pixelAreaValue, fullImageRect, TEST_LOCATION );
311
312   Vector4 pixelAreaSet( 0.2f, 0.2f, 0.3f, 0.3f );
313   imageView.SetProperty( ImageView::Property::PIXEL_AREA, pixelAreaSet);
314
315   application.SendNotification();
316   application.Render();
317
318   value = imageView.GetProperty( ImageView::Property::PIXEL_AREA );
319   value.Get(pixelAreaValue);
320   DALI_TEST_EQUALS( pixelAreaValue, pixelAreaSet, TEST_LOCATION );
321
322   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "pixelArea", pixelAreaUniform ) );
323   DALI_TEST_EQUALS( pixelAreaUniform, pixelAreaSet, TEST_LOCATION );
324
325   END_TEST;
326 }
327
328 int UtcDaliImageViewSetGetProperty03(void)
329 {
330   ToolkitTestApplication application;
331
332   Image image = CreateBufferImage( 10, 10, Color::WHITE );
333   ImageView imageView = ImageView::New(image);
334   Stage::GetCurrent().Add( imageView );
335   application.SendNotification();
336   application.Render();
337
338   // conventional alpha blending
339   Renderer renderer = imageView.GetRendererAt( 0 );
340   Property::Value value = renderer.GetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA );
341   bool enable;
342   DALI_TEST_CHECK( value.Get( enable ) );
343   DALI_TEST_CHECK( !enable );
344
345   // pre-multiplied alpha blending
346   imageView.SetProperty( Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA, true );
347   application.SendNotification();
348   application.Render();
349
350   int srcFactorRgb    = renderer.GetProperty<int>( Renderer::Property::BLEND_FACTOR_SRC_RGB );
351   int destFactorRgb   = renderer.GetProperty<int>( Renderer::Property::BLEND_FACTOR_DEST_RGB );
352   int srcFactorAlpha  = renderer.GetProperty<int>( Renderer::Property::BLEND_FACTOR_SRC_ALPHA );
353   int destFactorAlpha = renderer.GetProperty<int>( Renderer::Property::BLEND_FACTOR_DEST_ALPHA );
354   DALI_TEST_CHECK( srcFactorRgb == BlendFactor::ONE );
355   DALI_TEST_CHECK( destFactorRgb == BlendFactor::ONE_MINUS_SRC_ALPHA );
356   DALI_TEST_CHECK( srcFactorAlpha == BlendFactor::ONE );
357   DALI_TEST_CHECK( destFactorAlpha == BlendFactor::ONE );
358
359   value = renderer.GetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA );
360   DALI_TEST_CHECK( value.Get( enable ) );
361   DALI_TEST_CHECK( enable );
362
363   END_TEST;
364 }
365
366 int UtcDaliImageViewAsyncLoadingWithoutAltasing(void)
367 {
368   ToolkitTestApplication application;
369
370   // Async loading, no atlasing for big size image
371   ImageView imageView = ImageView::New( gImage_600_RGB );
372
373   // By default, Aysnc loading is used
374   Stage::GetCurrent().Add( imageView );
375   application.SendNotification();
376   application.Render(16);
377   application.Render(16);
378   application.SendNotification();
379
380   // BitmapLoader is not used
381   BitmapLoader loader = BitmapLoader::GetLatestCreated();
382   DALI_TEST_CHECK( !loader );
383
384   END_TEST;
385 }
386
387 int UtcDaliImageViewAsyncLoadingWithAtlasing(void)
388 {
389   ToolkitTestApplication application;
390
391   //Async loading, automatic atlasing for small size image
392   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
393   callStack.Reset();
394   callStack.Enable(true);
395
396   ImageView imageView = ImageView::New( gImage_34_RGBA, ImageDimensions( 34, 34 ) );
397
398   // By default, Aysnc loading is used
399   // loading is not started if the actor is offStage
400
401   Stage::GetCurrent().Add( imageView );
402   application.SendNotification();
403   application.Render(16);
404   application.Render(16);
405   application.SendNotification();
406
407   // loading started, this waits for the loader thread for max 30 seconds
408   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
409
410   application.SendNotification();
411   application.Render(16);
412
413   callStack.Enable(false);
414
415   TraceCallStack::NamedParams params;
416   params["width"] = ToString(34);
417   params["height"] = ToString(34);
418   DALI_TEST_EQUALS( callStack.FindMethodAndParams( "TexSubImage2D", params ), true, TEST_LOCATION );
419
420   END_TEST;
421 }
422
423 int UtcDaliImageViewSyncLoading(void)
424 {
425   ToolkitTestApplication application;
426
427   Property::Map syncLoadingMap;
428   syncLoadingMap[ "synchronousLoading" ] = true;
429
430   // Sync loading, no atlasing for big size image
431   {
432     ImageView imageView = ImageView::New();
433
434     // Sync loading is used
435     syncLoadingMap[ ImageVisual::Property::URL ] = gImage_600_RGB;
436     imageView.SetProperty( ImageView::Property::IMAGE, syncLoadingMap );
437
438     // BitmapLoader is used, and the loading is started immediately even the actor is not on stage.
439     BitmapLoader loader = BitmapLoader::GetLatestCreated();
440     DALI_TEST_CHECK( loader );
441   }
442
443   // Sync loading, automatic atlasing for small size image
444   {
445     BitmapLoader::ResetLatestCreated();
446     TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
447     callStack.Reset();
448     callStack.Enable(true);
449
450     ImageView imageView = ImageView::New( );
451     // Sync loading is used
452     syncLoadingMap[ ImageVisual::Property::URL ] = gImage_34_RGBA;
453     syncLoadingMap[ ImageVisual::Property::DESIRED_HEIGHT ] = 34;
454     syncLoadingMap[ ImageVisual::Property::DESIRED_WIDTH ] = 34;
455     imageView.SetProperty( ImageView::Property::IMAGE, syncLoadingMap );
456
457     // loading is started even if the actor is offStage
458     BitmapLoader loader = BitmapLoader::GetLatestCreated();
459     DALI_TEST_CHECK( loader );
460
461     loader.WaitForLoading();
462
463     DALI_TEST_CHECK( loader.IsLoaded() );
464
465     Stage::GetCurrent().Add( imageView );
466     application.SendNotification();
467     application.Render(16);
468
469     TraceCallStack::NamedParams params;
470     params["width"] = ToString(34);
471     params["height"] = ToString(34);
472     DALI_TEST_EQUALS( callStack.FindMethodAndParams( "TexSubImage2D", params ),
473                       true, TEST_LOCATION );
474   }
475   END_TEST;
476 }
477
478 int UtcDaliImageViewSizeWithBackground(void)
479 {
480   ToolkitTestApplication application;
481
482   int width = 100;
483   int height = 200;
484   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
485   ImageView imageView = ImageView::New();
486   imageView.SetBackgroundImage( image );
487
488   Stage::GetCurrent().Add( imageView );
489   application.SendNotification();
490   application.Render();
491
492   DALI_TEST_EQUALS( imageView.GetCurrentSize().width, (float)width, TEST_LOCATION );
493   DALI_TEST_EQUALS( imageView.GetCurrentSize().height, (float)height, TEST_LOCATION );
494
495   END_TEST;
496 }
497
498 int UtcDaliImageViewSizeWithBackgroundAndImage(void)
499 {
500   ToolkitTestApplication application;
501
502   int widthBackground = 100;
503   int heightBackground = 200;
504   int width = 300;
505   int height = 400;
506   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
507   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
508
509   ImageView imageView = ImageView::New();
510   imageView.SetBackgroundImage( imageBackground );
511   imageView.SetImage( image );
512
513   Stage::GetCurrent().Add( imageView );
514   application.SendNotification();
515   application.Render();
516
517   DALI_TEST_EQUALS( imageView.GetCurrentSize().width, (float)width, TEST_LOCATION );
518   DALI_TEST_EQUALS( imageView.GetCurrentSize().height, (float)height, TEST_LOCATION );
519
520   END_TEST;
521 }
522
523 int UtcDaliImageViewHeightForWidthBackground(void)
524 {
525   ToolkitTestApplication application;
526
527   int widthBackground = 100;
528   int heightBackground = 200;
529   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
530
531   ImageView imageView = ImageView::New();
532   imageView.SetBackgroundImage( imageBackground );
533
534   Stage::GetCurrent().Add( imageView );
535   application.SendNotification();
536   application.Render();
537
538   Control control = Control::DownCast( imageView );
539   DALI_TEST_CHECK( control );
540   DALI_TEST_EQUALS( imageView.GetHeightForWidth( 123.f ), control.GetHeightForWidth( 123.f ), TEST_LOCATION );
541   DALI_TEST_EQUALS( imageView.GetWidthForHeight( 321.f ), control.GetWidthForHeight( 321.f ), TEST_LOCATION );
542
543   END_TEST;
544 }
545
546 int UtcDaliImageViewHeightForWidthBackgroundAndImage(void)
547 {
548   ToolkitTestApplication application;
549
550   int widthBackground = 100;
551   int heightBackground = 200;
552   int width = 300;
553   int height = 400;
554   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
555   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
556
557   ImageView imageView = ImageView::New();
558   imageView.SetBackgroundImage( imageBackground );
559   imageView.SetImage( image );
560
561   Stage::GetCurrent().Add( imageView );
562   application.SendNotification();
563   application.Render();
564
565   DALI_TEST_EQUALS( imageView.GetHeightForWidth( width ), (float)height, TEST_LOCATION );
566   DALI_TEST_EQUALS( imageView.GetWidthForHeight( height ), (float)width, TEST_LOCATION );
567
568   END_TEST;
569 }
570
571 int UtcDaliImageViewSetBufferImage(void)
572 {
573   ToolkitTestApplication application;
574
575   int width1 = 300;
576   int height1 = 400;
577   BufferImage image1 = CreateBufferImage( width1, height1, Vector4( 1.f, 1.f, 1.f, 1.f ) );
578   ImageView imageView = ImageView::New();
579   imageView.SetImage( image1 );
580
581   TestImage( imageView, image1 );
582
583   int width2 = 600;
584   int height2 = 500;
585   BufferImage image2 = CreateBufferImage( width2, height2, Vector4( 1.f, 1.f, 1.f, 1.f ) );
586   imageView.SetImage( image2 );
587
588   TestImage( imageView, image2 );
589
590   END_TEST;
591 }
592
593 int UtcDaliImageViewSetImageUrl(void)
594 {
595   ToolkitTestApplication application;
596
597   ImageView imageView = ImageView::New();
598   imageView.SetImage( TEST_IMAGE_FILE_NAME );
599   TestUrl( imageView, TEST_IMAGE_FILE_NAME );
600
601
602   imageView.SetImage( TEST_IMAGE_FILE_NAME2 );
603   TestUrl( imageView, TEST_IMAGE_FILE_NAME2 );
604
605   END_TEST;
606 }
607
608 int UtcDaliImageViewSetImageOnstageP(void)
609 {
610   ToolkitTestApplication application;
611
612   ImageView imageView = ImageView::New();
613
614   Stage::GetCurrent().Add( imageView );
615   application.SendNotification();
616   application.Render();
617
618   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
619   imageView.SetImage( image1 );
620   TestImage( imageView, image1 );
621
622   int width = 300;
623   int height = 400;
624   BufferImage image2 = CreateBufferImage( width, height, Vector4( 1.f, 1.f, 1.f, 1.f ) );
625   imageView.SetImage( image2 );
626   TestImage( imageView, image2 );
627
628   END_TEST;
629 }
630
631 int UtcDaliImageViewSetImageOnstageN(void)
632 {
633   ToolkitTestApplication application;
634
635   ImageView imageView = ImageView::New();
636
637   Stage::GetCurrent().Add( imageView );
638   application.SendNotification();
639   application.Render();
640
641   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
642   imageView.SetImage( image1 );
643   TestImage( imageView, image1 );
644
645   Image image2;
646   imageView.SetImage( image2 );
647
648   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
649
650   //the value should be empty
651   std::string url;
652   DALI_TEST_CHECK( !value.Get( url ) );
653
654   Property::Map map;
655   DALI_TEST_CHECK( !value.Get( map ) );
656
657   END_TEST;
658 }
659
660 int UtcDaliImageViewSetImageOffstageP(void)
661 {
662   ToolkitTestApplication application;
663
664   ImageView imageView = ImageView::New();
665
666   Stage::GetCurrent().Add( imageView );
667   application.SendNotification();
668   application.Render();
669   Stage::GetCurrent().Remove( imageView );
670
671   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
672   imageView.SetImage( image1 );
673   TestImage( imageView, image1 );
674
675   int width = 300;
676   int height = 400;
677   BufferImage image2 = CreateBufferImage( width, height, Vector4( 1.f, 1.f, 1.f, 1.f ) );
678   imageView.SetImage( image2 );
679   TestImage( imageView, image2 );
680
681   END_TEST;
682 }
683
684 int UtcDaliImageViewSetImageOffstageN(void)
685 {
686   ToolkitTestApplication application;
687
688   ImageView imageView = ImageView::New();
689
690   Stage::GetCurrent().Add( imageView );
691   application.SendNotification();
692   application.Render();
693   Stage::GetCurrent().Remove( imageView );
694
695   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
696   imageView.SetImage( image1 );
697   TestImage( imageView, image1 );
698
699   Image image2;
700   imageView.SetImage( image2 );
701
702   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
703
704   //the value should be empty
705   std::string url;
706   DALI_TEST_CHECK( !value.Get( url ) );
707
708   Property::Map map;
709   DALI_TEST_CHECK( !value.Get( map ) );
710
711   END_TEST;
712 }
713
714 int UtcDaliImageViewSetImageN(void)
715 {
716   ToolkitTestApplication application;
717
718   Image image1;
719   ImageView imageView = ImageView::New();
720   imageView.SetImage( image1 );
721
722   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
723
724   //the value should be empty
725   std::string url;
726   DALI_TEST_CHECK( !value.Get( url ) );
727
728   Property::Map map;
729   DALI_TEST_CHECK( !value.Get( map ) );
730
731   std::string resource_url;
732   Property::Value val = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
733   DALI_TEST_CHECK( !val.Get( resource_url ) );
734
735   END_TEST;
736 }
737
738 int UtcDaliImageViewSetImageTypeChangesP(void)
739 {
740   ToolkitTestApplication application;
741
742   ImageView imageView = ImageView::New();
743
744
745   std::string url;
746   Property::Map map;
747
748   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
749   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
750   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
751
752   // Set a URL
753   imageView.SetImage( "TEST_URL" );
754   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
755
756   DALI_TEST_CHECK( value.Get( url ) );   // Value should NOT be empty
757   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
758
759   // Set an empty Image
760   imageView.SetImage( Image() );
761   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
762
763   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
764   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
765
766   // Set an Image
767   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
768   imageView.SetImage( image1 );
769   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
770
771   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
772   DALI_TEST_CHECK( value.Get( map ) );   // Value should NOT be empty
773
774   // Set an empty URL
775   imageView.SetImage( "" );
776   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
777
778   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
779   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
780
781   END_TEST;
782 }
783
784 int UtcDaliImageViewResourceUrlP(void)
785 {
786   ToolkitTestApplication application;
787
788   ImageView imageView = ImageView::New();
789   DALI_TEST_CHECK( imageView.GetProperty( ImageView::Property::RESOURCE_URL ).Get< std::string >().empty() );
790
791   imageView.SetProperty( ImageView::Property::RESOURCE_URL, "TestString" );
792   DALI_TEST_EQUALS( imageView.GetProperty( ImageView::Property::RESOURCE_URL ).Get< std::string >(), "TestString", TEST_LOCATION );
793
794   END_TEST;
795 }
796
797 // Scenarios 1: ImageView from regular image
798 int UtcDaliImageViewSetImageBufferImage(void)
799 {
800   ToolkitTestApplication application;
801
802   ImageView imageView = ImageView::New();
803   Stage::GetCurrent().Add( imageView );
804
805   TestGlAbstraction& gl = application.GetGlAbstraction();
806   gl.EnableTextureCallTrace( true );
807
808   std::vector< GLuint > ids;
809   ids.push_back( 23 );
810   application.GetGlAbstraction().SetNextTextureIds( ids );
811
812   int width = 300;
813   int height = 400;
814   BufferImage image = CreateBufferImage( width, height, Color::WHITE );
815
816   imageView.SetImage( image );
817
818   application.SendNotification();
819   application.Render();
820
821   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
822
823   std::stringstream params;
824   params << GL_TEXTURE_2D << ", " << 23;
825   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
826
827   END_TEST;
828 }
829
830 // Scenarios 2: ImageView from Native image
831 int UtcDaliImageViewSetImageNativeImage(void)
832 {
833   ToolkitTestApplication application;
834
835   ImageView imageView = ImageView::New();
836   Stage::GetCurrent().Add( imageView );
837
838   TestGlAbstraction& gl = application.GetGlAbstraction();
839   gl.EnableTextureCallTrace( true );
840
841   std::vector< GLuint > ids;
842   ids.push_back( 23 );
843   application.GetGlAbstraction().SetNextTextureIds( ids );
844
845   int width = 200;
846   int height = 500;
847   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
848   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
849
850   imageView.SetImage( nativeImage );
851   application.SendNotification();
852   application.Render();
853
854   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
855
856   std::stringstream params;
857   params << GL_TEXTURE_2D << ", " << 23;
858   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
859
860   END_TEST;
861 }
862
863 // Scenarios 3: ImageView initially from regular image but then SetImage called with Native image
864 int UtcDaliImageViewSetImageBufferImageToNativeImage(void)
865 {
866   ToolkitTestApplication application;
867
868   int width = 300;
869   int height = 400;
870   BufferImage image = CreateBufferImage( width, height, Color::WHITE );
871
872   ImageView imageView = ImageView::New( image );
873   Stage::GetCurrent().Add( imageView );
874
875   TestGlAbstraction& gl = application.GetGlAbstraction();
876   gl.EnableTextureCallTrace( true );
877
878   std::vector< GLuint > ids;
879   ids.push_back( 23 );
880   application.GetGlAbstraction().SetNextTextureIds( ids );
881
882   application.SendNotification();
883   application.Render();
884
885   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
886
887   std::stringstream params;
888   params << GL_TEXTURE_2D << ", " << 23;
889   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
890
891   width = 200;
892   height = 500;
893   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
894   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
895   imageView.SetImage( nativeImage );
896
897   ids.clear();
898   ids.push_back( 24 );
899   application.GetGlAbstraction().SetNextTextureIds( ids );
900
901   application.SendNotification();
902   application.Render();
903
904   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
905
906   std::stringstream nextTextureParams;
907   nextTextureParams << GL_TEXTURE_2D << ", " << 24;
908   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", nextTextureParams.str()) );
909
910   END_TEST;
911 }
912
913 // Scenarios 4: ImageView initially from Native image but then SetImage called with regular image
914 int UtcDaliImageViewSetImageNativeImageToBufferImage(void)
915 {
916   ToolkitTestApplication application;
917
918   int width = 300;
919   int height = 400;
920   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
921   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
922
923   ImageView imageView = ImageView::New( nativeImage );
924   Stage::GetCurrent().Add( imageView );
925
926   TestGlAbstraction& gl = application.GetGlAbstraction();
927   gl.EnableTextureCallTrace( true );
928
929   std::vector< GLuint > ids;
930   ids.push_back( 23 );
931   application.GetGlAbstraction().SetNextTextureIds( ids );
932
933   application.SendNotification();
934   application.Render();
935
936   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
937
938   std::stringstream params;
939   params << GL_TEXTURE_2D << ", " << 23;
940   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
941
942   width = 200;
943   height = 500;
944   BufferImage image = CreateBufferImage( width, height, Color::WHITE );
945   imageView.SetImage( image );
946
947   ids.clear();
948   ids.push_back( 24 );
949   application.GetGlAbstraction().SetNextTextureIds( ids );
950
951   application.SendNotification();
952   application.Render();
953
954   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
955
956   std::stringstream nextTextureParams;
957   nextTextureParams << GL_TEXTURE_2D << ", " << 24;
958   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", nextTextureParams.str()) );
959
960   END_TEST;
961 }
962
963 // Scenarios 5: ImageView from Native image with custom shader
964 int UtcDaliImageViewSetImageNativeImageWithCustomShader(void)
965 {
966   ToolkitTestApplication application;
967
968   int width = 300;
969   int height = 400;
970
971   Property::Map customShader;
972   customShader.Insert( "vertexShader", VERTEX_SHADER );
973   customShader.Insert( "fragmentShader", FRAGMENT_SHADER );
974
975   Property::Array shaderHints;
976   shaderHints.PushBack( "requiresSelfDepthTest" );
977   shaderHints.PushBack( "outputIsTransparent" );
978   shaderHints.PushBack( "outputIsOpaque" );
979   shaderHints.PushBack( "modifiesGeometry" );
980
981   customShader.Insert( "hints", shaderHints );
982
983   Property::Map map;
984   map.Insert( "shader", customShader );
985
986   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
987   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
988
989   ImageView imageView = ImageView::New( nativeImage );
990   imageView.SetProperty( ImageView::Property::IMAGE, map );
991   Stage::GetCurrent().Add( imageView );
992
993   TestGlAbstraction& gl = application.GetGlAbstraction();
994   gl.EnableTextureCallTrace( true );
995
996   std::vector< GLuint > ids;
997   ids.push_back( 23 );
998   application.GetGlAbstraction().SetNextTextureIds( ids );
999
1000   application.SendNotification();
1001   application.Render();
1002
1003   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1004
1005   std::stringstream params;
1006   params << GL_TEXTURE_2D << ", " << 23;
1007   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
1008
1009   END_TEST;
1010 }
1011
1012 // Scenarios 6: ImageView initially from regular image with custom shader but then SetImage called with Native
1013 int UtcDaliImageViewSetImageBufferImageWithCustomShaderToNativeImage(void)
1014 {
1015   ToolkitTestApplication application;
1016
1017   int width = 300;
1018   int height = 400;
1019
1020   Property::Map customShader;
1021   customShader.Insert( "vertexShader", VERTEX_SHADER );
1022   customShader.Insert( "fragmentShader", FRAGMENT_SHADER );
1023
1024   Property::Array shaderHints;
1025   shaderHints.PushBack( "requiresSelfDepthTest" );
1026   shaderHints.PushBack( "outputIsTransparent" );
1027   shaderHints.PushBack( "outputIsOpaque" );
1028   shaderHints.PushBack( "modifiesGeometry" );
1029
1030   customShader.Insert( "hints", shaderHints );
1031
1032   Property::Map map;
1033   map.Insert( "shader", customShader );
1034
1035   BufferImage image = CreateBufferImage( width, height, Color::WHITE );
1036
1037   ImageView imageView = ImageView::New( image );
1038   imageView.SetProperty( ImageView::Property::IMAGE, map );
1039   Stage::GetCurrent().Add( imageView );
1040
1041   TestGlAbstraction& gl = application.GetGlAbstraction();
1042   gl.EnableTextureCallTrace( true );
1043
1044   std::vector< GLuint > ids;
1045   ids.push_back( 23 );
1046   application.GetGlAbstraction().SetNextTextureIds( ids );
1047
1048   application.SendNotification();
1049   application.Render();
1050
1051   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1052
1053   std::stringstream params;
1054   params << GL_TEXTURE_2D << ", " << 23;
1055   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
1056
1057   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
1058   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
1059   imageView.SetImage( nativeImage );
1060
1061   ids.clear();
1062   ids.push_back( 24 );
1063   application.GetGlAbstraction().SetNextTextureIds( ids );
1064
1065   application.SendNotification();
1066   application.Render();
1067
1068   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1069
1070   std::stringstream nativeImageParams;
1071   nativeImageParams << GL_TEXTURE_2D << ", " << 24;
1072   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", nativeImageParams.str()) );
1073
1074
1075   END_TEST;
1076 }
1077
1078 int UtcDaliImageViewGetImageP1(void)
1079 {
1080   ToolkitTestApplication application;
1081
1082   ImageView imageView = ImageView::New();
1083   DALI_TEST_CHECK( ! imageView.GetImage() );
1084
1085   Image image = CreateBufferImage();
1086   imageView.SetImage( image );
1087   DALI_TEST_CHECK( imageView.GetImage() == image );
1088
1089   END_TEST;
1090 }
1091
1092 int UtcDaliImageViewGetImageP2(void)
1093 {
1094   ToolkitTestApplication application;
1095
1096   BufferImage image = CreateBufferImage();
1097   ImageView imageView = ImageView::New( image );
1098   DALI_TEST_CHECK( imageView.GetImage() == image );
1099
1100   END_TEST;
1101 }
1102
1103 int UtcDaliImageViewGetImageN(void)
1104 {
1105   ToolkitTestApplication application;
1106
1107   ImageView imageView = ImageView::New( TEST_IMAGE_FILE_NAME );
1108   DALI_TEST_CHECK( ! imageView.GetImage() );
1109
1110   Image image = CreateBufferImage();
1111   imageView.SetImage( image );
1112   DALI_TEST_CHECK( imageView.GetImage() == image );
1113
1114   imageView.SetImage( TEST_IMAGE_FILE_NAME );
1115   DALI_TEST_CHECK( ! imageView.GetImage() );
1116
1117   END_TEST;
1118 }