5955748f51f40f87063402698eab0b8002c2b9b3
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-ImageView.cpp
1 /*
2  * Copyright (c) 2014 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/devel-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 == BlendingFactor::ONE );
355   DALI_TEST_CHECK( destFactorRgb == BlendingFactor::ONE_MINUS_SRC_ALPHA );
356   DALI_TEST_CHECK( srcFactorAlpha == BlendingFactor::ONE );
357   DALI_TEST_CHECK( destFactorAlpha == BlendingFactor::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 UtcDaliImageViewAsyncLoadingWithAltasing(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   BitmapLoader loader = BitmapLoader::GetLatestCreated();
401   DALI_TEST_CHECK( !loader );
402
403   Stage::GetCurrent().Add( imageView );
404   application.SendNotification();
405   application.Render(16);
406   application.Render(16);
407   application.SendNotification();
408
409   // loading started
410   loader = BitmapLoader::GetLatestCreated();
411   DALI_TEST_CHECK( loader );
412
413   // worker thread is created
414   EventThreadCallback* eventTrigger = EventThreadCallback::Get();
415   DALI_TEST_CHECK( eventTrigger );
416
417   loader.WaitForLoading();// waiting until the image to be loaded
418   DALI_TEST_CHECK( loader.IsLoaded() );
419
420   CallbackBase* callback = eventTrigger->GetCallback();
421   CallbackBase::Execute( *callback );
422
423   application.SendNotification();
424   application.Render(16);
425
426   callStack.Enable(false);
427
428   DALI_TEST_CHECK(  callStack.FindMethodAndParams("TexSubImage2D", "0, 0, 34, 34" ) );
429
430
431   END_TEST;
432 }
433
434 int UtcDaliImageViewSyncLoading(void)
435 {
436   ToolkitTestApplication application;
437
438   Property::Map syncLoadingMap;
439   syncLoadingMap[ "synchronousLoading" ] = true;
440
441   // Sync loading, no atlasing for big size image
442   {
443     ImageView imageView = ImageView::New( gImage_600_RGB );
444
445     // Sync loading is used
446     imageView.SetProperty( ImageView::Property::IMAGE, syncLoadingMap );
447
448     // BitmapLoader is used, and the loading is started immediately even the actor is not on stage.
449     BitmapLoader loader = BitmapLoader::GetLatestCreated();
450     DALI_TEST_CHECK( loader );
451   }
452
453   // Sync loading, automatic atlasing for small size image
454   {
455     BitmapLoader::ResetLatestCreated();
456     TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
457     callStack.Reset();
458     callStack.Enable(true);
459
460     ImageView imageView = ImageView::New( );
461     // Sync loading is used
462     syncLoadingMap[ "url" ] = gImage_34_RGBA;
463     syncLoadingMap[ "desiredHeight" ] = 34;
464     syncLoadingMap[ "desiredWidth" ] = 34;
465     imageView.SetProperty( ImageView::Property::IMAGE, syncLoadingMap );
466
467     // loading is started even if the actor is offStage
468     BitmapLoader loader = BitmapLoader::GetLatestCreated();
469     DALI_TEST_CHECK( loader );
470
471     loader.WaitForLoading();
472
473     DALI_TEST_CHECK( loader.IsLoaded() );
474
475     Stage::GetCurrent().Add( imageView );
476     application.SendNotification();
477     application.Render(16);
478     DALI_TEST_CHECK(  callStack.FindMethodAndParams("TexSubImage2D", "0, 0, 34, 34" ) );
479   }
480
481   END_TEST;
482 }
483
484 int UtcDaliImageViewSizeWithBackground(void)
485 {
486   ToolkitTestApplication application;
487
488   int width = 100;
489   int height = 200;
490   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
491   ImageView imageView = ImageView::New();
492   imageView.SetBackgroundImage( image );
493
494   Stage::GetCurrent().Add( imageView );
495   application.SendNotification();
496   application.Render();
497
498   DALI_TEST_EQUALS( imageView.GetCurrentSize().width, (float)width, TEST_LOCATION );
499   DALI_TEST_EQUALS( imageView.GetCurrentSize().height, (float)height, TEST_LOCATION );
500
501   END_TEST;
502 }
503
504 int UtcDaliImageViewSizeWithBackgroundAndImage(void)
505 {
506   ToolkitTestApplication application;
507
508   int widthBackground = 100;
509   int heightBackground = 200;
510   int width = 300;
511   int height = 400;
512   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
513   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
514
515   ImageView imageView = ImageView::New();
516   imageView.SetBackgroundImage( imageBackground );
517   imageView.SetImage( image );
518
519   Stage::GetCurrent().Add( imageView );
520   application.SendNotification();
521   application.Render();
522
523   DALI_TEST_EQUALS( imageView.GetCurrentSize().width, (float)width, TEST_LOCATION );
524   DALI_TEST_EQUALS( imageView.GetCurrentSize().height, (float)height, TEST_LOCATION );
525
526   END_TEST;
527 }
528
529 int UtcDaliImageViewHeightForWidthBackground(void)
530 {
531   ToolkitTestApplication application;
532
533   int widthBackground = 100;
534   int heightBackground = 200;
535   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
536
537   ImageView imageView = ImageView::New();
538   imageView.SetBackgroundImage( imageBackground );
539
540   Stage::GetCurrent().Add( imageView );
541   application.SendNotification();
542   application.Render();
543
544   Control control = Control::DownCast( imageView );
545   DALI_TEST_CHECK( control );
546   DALI_TEST_EQUALS( imageView.GetHeightForWidth( 123.f ), control.GetHeightForWidth( 123.f ), TEST_LOCATION );
547   DALI_TEST_EQUALS( imageView.GetWidthForHeight( 321.f ), control.GetWidthForHeight( 321.f ), TEST_LOCATION );
548
549   END_TEST;
550 }
551
552 int UtcDaliImageViewHeightForWidthBackgroundAndImage(void)
553 {
554   ToolkitTestApplication application;
555
556   int widthBackground = 100;
557   int heightBackground = 200;
558   int width = 300;
559   int height = 400;
560   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
561   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
562
563   ImageView imageView = ImageView::New();
564   imageView.SetBackgroundImage( imageBackground );
565   imageView.SetImage( image );
566
567   Stage::GetCurrent().Add( imageView );
568   application.SendNotification();
569   application.Render();
570
571   DALI_TEST_EQUALS( imageView.GetHeightForWidth( width ), (float)height, TEST_LOCATION );
572   DALI_TEST_EQUALS( imageView.GetWidthForHeight( height ), (float)width, TEST_LOCATION );
573
574   END_TEST;
575 }
576
577 int UtcDaliImageViewSetBufferImage(void)
578 {
579   ToolkitTestApplication application;
580
581   int width1 = 300;
582   int height1 = 400;
583   BufferImage image1 = CreateBufferImage( width1, height1, Vector4( 1.f, 1.f, 1.f, 1.f ) );
584   ImageView imageView = ImageView::New();
585   imageView.SetImage( image1 );
586
587   TestImage( imageView, image1 );
588
589   int width2 = 600;
590   int height2 = 500;
591   BufferImage image2 = CreateBufferImage( width2, height2, Vector4( 1.f, 1.f, 1.f, 1.f ) );
592   imageView.SetImage( image2 );
593
594   TestImage( imageView, image2 );
595
596   END_TEST;
597 }
598
599 int UtcDaliImageViewSetImageUrl(void)
600 {
601   ToolkitTestApplication application;
602
603   ImageView imageView = ImageView::New();
604   imageView.SetImage( TEST_IMAGE_FILE_NAME );
605   TestUrl( imageView, TEST_IMAGE_FILE_NAME );
606
607
608   imageView.SetImage( TEST_IMAGE_FILE_NAME2 );
609   TestUrl( imageView, TEST_IMAGE_FILE_NAME2 );
610
611   END_TEST;
612 }
613
614 int UtcDaliImageViewSetImageOnstageP(void)
615 {
616   ToolkitTestApplication application;
617
618   ImageView imageView = ImageView::New();
619
620   Stage::GetCurrent().Add( imageView );
621   application.SendNotification();
622   application.Render();
623
624   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
625   imageView.SetImage( image1 );
626   TestImage( imageView, image1 );
627
628   int width = 300;
629   int height = 400;
630   BufferImage image2 = CreateBufferImage( width, height, Vector4( 1.f, 1.f, 1.f, 1.f ) );
631   imageView.SetImage( image2 );
632   TestImage( imageView, image2 );
633
634   END_TEST;
635 }
636
637 int UtcDaliImageViewSetImageOnstageN(void)
638 {
639   ToolkitTestApplication application;
640
641   ImageView imageView = ImageView::New();
642
643   Stage::GetCurrent().Add( imageView );
644   application.SendNotification();
645   application.Render();
646
647   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
648   imageView.SetImage( image1 );
649   TestImage( imageView, image1 );
650
651   Image image2;
652   imageView.SetImage( image2 );
653
654   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
655
656   //the value should be empty
657   std::string url;
658   DALI_TEST_CHECK( !value.Get( url ) );
659
660   Property::Map map;
661   DALI_TEST_CHECK( !value.Get( map ) );
662
663   END_TEST;
664 }
665
666 int UtcDaliImageViewSetImageOffstageP(void)
667 {
668   ToolkitTestApplication application;
669
670   ImageView imageView = ImageView::New();
671
672   Stage::GetCurrent().Add( imageView );
673   application.SendNotification();
674   application.Render();
675   Stage::GetCurrent().Remove( imageView );
676
677   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
678   imageView.SetImage( image1 );
679   TestImage( imageView, image1 );
680
681   int width = 300;
682   int height = 400;
683   BufferImage image2 = CreateBufferImage( width, height, Vector4( 1.f, 1.f, 1.f, 1.f ) );
684   imageView.SetImage( image2 );
685   TestImage( imageView, image2 );
686
687   END_TEST;
688 }
689
690 int UtcDaliImageViewSetImageOffstageN(void)
691 {
692   ToolkitTestApplication application;
693
694   ImageView imageView = ImageView::New();
695
696   Stage::GetCurrent().Add( imageView );
697   application.SendNotification();
698   application.Render();
699   Stage::GetCurrent().Remove( imageView );
700
701   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
702   imageView.SetImage( image1 );
703   TestImage( imageView, image1 );
704
705   Image image2;
706   imageView.SetImage( image2 );
707
708   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
709
710   //the value should be empty
711   std::string url;
712   DALI_TEST_CHECK( !value.Get( url ) );
713
714   Property::Map map;
715   DALI_TEST_CHECK( !value.Get( map ) );
716
717   END_TEST;
718 }
719
720 int UtcDaliImageViewSetImageN(void)
721 {
722   ToolkitTestApplication application;
723
724   Image image1;
725   ImageView imageView = ImageView::New();
726   imageView.SetImage( image1 );
727
728   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
729
730   //the value should be empty
731   std::string url;
732   DALI_TEST_CHECK( !value.Get( url ) );
733
734   Property::Map map;
735   DALI_TEST_CHECK( !value.Get( map ) );
736
737   std::string resource_url;
738   Property::Value val = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
739   DALI_TEST_CHECK( !val.Get( resource_url ) );
740
741   END_TEST;
742 }
743
744 int UtcDaliImageViewSetImageTypeChangesP(void)
745 {
746   ToolkitTestApplication application;
747
748   ImageView imageView = ImageView::New();
749
750
751   std::string url;
752   Property::Map map;
753
754   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
755   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
756   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
757
758   // Set a URL
759   imageView.SetImage( "TEST_URL" );
760   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
761
762   DALI_TEST_CHECK( value.Get( url ) );   // Value should NOT be empty
763   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
764
765   // Set an empty Image
766   imageView.SetImage( Image() );
767   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
768
769   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
770   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
771
772   // Set an Image
773   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
774   imageView.SetImage( image1 );
775   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
776
777   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
778   DALI_TEST_CHECK( value.Get( map ) );   // Value should NOT be empty
779
780   // Set an empty URL
781   imageView.SetImage( "" );
782   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
783
784   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
785   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
786
787   END_TEST;
788 }
789
790 int UtcDaliImageViewResourceUrlP(void)
791 {
792   ToolkitTestApplication application;
793
794   ImageView imageView = ImageView::New();
795   DALI_TEST_CHECK( imageView.GetProperty( ImageView::Property::RESOURCE_URL ).Get< std::string >().empty() );
796
797   imageView.SetProperty( ImageView::Property::RESOURCE_URL, "TestString" );
798   DALI_TEST_EQUALS( imageView.GetProperty( ImageView::Property::RESOURCE_URL ).Get< std::string >(), "TestString", TEST_LOCATION );
799
800   END_TEST;
801 }
802
803 // Scenarios 1: ImageView from regular image
804 int UtcDaliImageViewSetImageBufferImage(void)
805 {
806   ToolkitTestApplication application;
807
808   ImageView imageView = ImageView::New();
809   Stage::GetCurrent().Add( imageView );
810
811   TestGlAbstraction& gl = application.GetGlAbstraction();
812   gl.EnableTextureCallTrace( true );
813
814   std::vector< GLuint > ids;
815   ids.push_back( 23 );
816   application.GetGlAbstraction().SetNextTextureIds( ids );
817
818   int width = 300;
819   int height = 400;
820   BufferImage image = CreateBufferImage( width, height, Color::WHITE );
821
822   imageView.SetImage( image );
823
824   application.SendNotification();
825   application.Render();
826
827   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
828
829   std::stringstream params;
830   params << GL_TEXTURE_2D << ", " << 23;
831   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
832
833   END_TEST;
834 }
835
836 // Scenarios 2: ImageView from Native image
837 int UtcDaliImageViewSetImageNativeImage(void)
838 {
839   ToolkitTestApplication application;
840
841   ImageView imageView = ImageView::New();
842   Stage::GetCurrent().Add( imageView );
843
844   TestGlAbstraction& gl = application.GetGlAbstraction();
845   gl.EnableTextureCallTrace( true );
846
847   std::vector< GLuint > ids;
848   ids.push_back( 23 );
849   application.GetGlAbstraction().SetNextTextureIds( ids );
850
851   int width = 200;
852   int height = 500;
853   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
854   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
855
856   imageView.SetImage( nativeImage );
857   application.SendNotification();
858   application.Render();
859
860   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
861
862   std::stringstream params;
863   params << GL_TEXTURE_2D << ", " << 23;
864   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
865
866   END_TEST;
867 }
868
869 // Scenarios 3: ImageView initially from regular image but then SetImage called with Native image
870 int UtcDaliImageViewSetImageBufferImageToNativeImage(void)
871 {
872   ToolkitTestApplication application;
873
874   int width = 300;
875   int height = 400;
876   BufferImage image = CreateBufferImage( width, height, Color::WHITE );
877
878   ImageView imageView = ImageView::New( image );
879   Stage::GetCurrent().Add( imageView );
880
881   TestGlAbstraction& gl = application.GetGlAbstraction();
882   gl.EnableTextureCallTrace( true );
883
884   std::vector< GLuint > ids;
885   ids.push_back( 23 );
886   application.GetGlAbstraction().SetNextTextureIds( ids );
887
888   application.SendNotification();
889   application.Render();
890
891   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
892
893   std::stringstream params;
894   params << GL_TEXTURE_2D << ", " << 23;
895   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
896
897   width = 200;
898   height = 500;
899   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
900   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
901   imageView.SetImage( nativeImage );
902
903   ids.clear();
904   ids.push_back( 24 );
905   application.GetGlAbstraction().SetNextTextureIds( ids );
906
907   application.SendNotification();
908   application.Render();
909
910   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
911
912   std::stringstream nextTextureParams;
913   nextTextureParams << GL_TEXTURE_2D << ", " << 24;
914   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", nextTextureParams.str()) );
915
916   END_TEST;
917 }
918
919 // Scenarios 4: ImageView initially from Native image but then SetImage called with regular image
920 int UtcDaliImageViewSetImageNativeImageToBufferImage(void)
921 {
922   ToolkitTestApplication application;
923
924   int width = 300;
925   int height = 400;
926   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
927   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
928
929   ImageView imageView = ImageView::New( nativeImage );
930   Stage::GetCurrent().Add( imageView );
931
932   TestGlAbstraction& gl = application.GetGlAbstraction();
933   gl.EnableTextureCallTrace( true );
934
935   std::vector< GLuint > ids;
936   ids.push_back( 23 );
937   application.GetGlAbstraction().SetNextTextureIds( ids );
938
939   application.SendNotification();
940   application.Render();
941
942   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
943
944   std::stringstream params;
945   params << GL_TEXTURE_2D << ", " << 23;
946   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
947
948   width = 200;
949   height = 500;
950   BufferImage image = CreateBufferImage( width, height, Color::WHITE );
951   imageView.SetImage( image );
952
953   ids.clear();
954   ids.push_back( 24 );
955   application.GetGlAbstraction().SetNextTextureIds( ids );
956
957   application.SendNotification();
958   application.Render();
959
960   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
961
962   std::stringstream nextTextureParams;
963   nextTextureParams << GL_TEXTURE_2D << ", " << 24;
964   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", nextTextureParams.str()) );
965
966   END_TEST;
967 }
968
969 // Scenarios 5: ImageView from Native image with custom shader
970 int UtcDaliImageViewSetImageNativeImageWithCustomShader(void)
971 {
972   ToolkitTestApplication application;
973
974   int width = 300;
975   int height = 400;
976
977   Property::Map customShader;
978   customShader.Insert( "vertexShader", VERTEX_SHADER );
979   customShader.Insert( "fragmentShader", FRAGMENT_SHADER );
980
981   Property::Array shaderHints;
982   shaderHints.PushBack( "requiresSelfDepthTest" );
983   shaderHints.PushBack( "outputIsTransparent" );
984   shaderHints.PushBack( "outputIsOpaque" );
985   shaderHints.PushBack( "modifiesGeometry" );
986
987   customShader.Insert( "hints", shaderHints );
988
989   Property::Map map;
990   map.Insert( "rendererType", "image" );
991   map.Insert( "shader", customShader );
992
993   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
994   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
995
996   ImageView imageView = ImageView::New( nativeImage );
997   imageView.SetProperty( ImageView::Property::IMAGE, map );
998   Stage::GetCurrent().Add( imageView );
999
1000   imageView.SetProperty( ImageView::Property::IMAGE, map );
1001
1002   TestGlAbstraction& gl = application.GetGlAbstraction();
1003   gl.EnableTextureCallTrace( true );
1004
1005   std::vector< GLuint > ids;
1006   ids.push_back( 23 );
1007   application.GetGlAbstraction().SetNextTextureIds( ids );
1008
1009   application.SendNotification();
1010   application.Render();
1011
1012   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1013
1014   std::stringstream params;
1015   params << GL_TEXTURE_2D << ", " << 23;
1016   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
1017
1018   END_TEST;
1019 }
1020
1021 // Scenarios 6: ImageView initially from regular image with custom shader but then SetImage called with Native
1022 int UtcDaliImageViewSetImageBufferImageWithCustomShaderToNativeImage(void)
1023 {
1024   ToolkitTestApplication application;
1025
1026   int width = 300;
1027   int height = 400;
1028
1029   Property::Map customShader;
1030   customShader.Insert( "vertexShader", VERTEX_SHADER );
1031   customShader.Insert( "fragmentShader", FRAGMENT_SHADER );
1032
1033   Property::Array shaderHints;
1034   shaderHints.PushBack( "requiresSelfDepthTest" );
1035   shaderHints.PushBack( "outputIsTransparent" );
1036   shaderHints.PushBack( "outputIsOpaque" );
1037   shaderHints.PushBack( "modifiesGeometry" );
1038
1039   customShader.Insert( "hints", shaderHints );
1040
1041   Property::Map map;
1042   map.Insert( "rendererType", "image" );
1043   map.Insert( "shader", customShader );
1044
1045   BufferImage image = CreateBufferImage( width, height, Color::WHITE );
1046
1047   ImageView imageView = ImageView::New( image );
1048   imageView.SetProperty( ImageView::Property::IMAGE, map );
1049   Stage::GetCurrent().Add( imageView );
1050
1051   imageView.SetProperty( ImageView::Property::IMAGE, map );
1052
1053   TestGlAbstraction& gl = application.GetGlAbstraction();
1054   gl.EnableTextureCallTrace( true );
1055
1056   std::vector< GLuint > ids;
1057   ids.push_back( 23 );
1058   application.GetGlAbstraction().SetNextTextureIds( ids );
1059
1060   application.SendNotification();
1061   application.Render();
1062
1063   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1064
1065   std::stringstream params;
1066   params << GL_TEXTURE_2D << ", " << 23;
1067   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
1068
1069   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
1070   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
1071   imageView.SetImage( nativeImage );
1072
1073   ids.clear();
1074   ids.push_back( 24 );
1075   application.GetGlAbstraction().SetNextTextureIds( ids );
1076
1077   application.SendNotification();
1078   application.Render();
1079
1080   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1081
1082   std::stringstream nativeImageParams;
1083   nativeImageParams << GL_TEXTURE_2D << ", " << 24;
1084   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", nativeImageParams.str()) );
1085
1086
1087   END_TEST;
1088 }
1089
1090 int UtcDaliImageViewGetImageP1(void)
1091 {
1092   ToolkitTestApplication application;
1093
1094   ImageView imageView = ImageView::New();
1095   DALI_TEST_CHECK( ! imageView.GetImage() );
1096
1097   Image image = CreateBufferImage();
1098   imageView.SetImage( image );
1099   DALI_TEST_CHECK( imageView.GetImage() == image );
1100
1101   END_TEST;
1102 }
1103
1104 int UtcDaliImageViewGetImageP2(void)
1105 {
1106   ToolkitTestApplication application;
1107
1108   BufferImage image = CreateBufferImage();
1109   ImageView imageView = ImageView::New( image );
1110   DALI_TEST_CHECK( imageView.GetImage() == image );
1111
1112   END_TEST;
1113 }
1114
1115 int UtcDaliImageViewGetImageN(void)
1116 {
1117   ToolkitTestApplication application;
1118
1119   ImageView imageView = ImageView::New( TEST_IMAGE_FILE_NAME );
1120   DALI_TEST_CHECK( ! imageView.GetImage() );
1121
1122   Image image = CreateBufferImage();
1123   imageView.SetImage( image );
1124   DALI_TEST_CHECK( imageView.GetImage() == image );
1125
1126   imageView.SetImage( TEST_IMAGE_FILE_NAME );
1127   DALI_TEST_CHECK( ! imageView.GetImage() );
1128
1129   END_TEST;
1130 }
1131