05de93d8e69d04f879ee6805c096cdf743ff3c48
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-DebugRendering.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 #include <unistd.h>
18
19 #include <toolkit-event-thread-callback.h>
20 #include <dali-toolkit-test-suite-utils.h>
21 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
22 #include <dali-toolkit/internal/visuals/wireframe/wireframe-visual.h>
23 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
24 #include <dali-toolkit/internal/visuals/text/text-visual.h>
25
26 #include <dali-toolkit/dali-toolkit.h>
27
28 #include <toolkit-environment-variable.h> // for setting environment variable: DALI_DEBUG_RENDERING
29 #include "dummy-control.h"
30
31 using namespace Dali;
32 using namespace Dali::Toolkit;
33
34 namespace
35 {
36 const char* TEST_IMAGE_FILE_NAME =  "image_01.jpg";
37 const char* TEST_NPATCH_FILE_NAME =  "image_01.9.jpg";
38 const char* TEST_SVG_FILE_NAME = TEST_RESOURCE_DIR "/svg1.svg";
39 const char* TEST_GIF_FILE_NAME = TEST_RESOURCE_DIR "/anim.gif";
40
41 const std::string DEFAULT_FONT_DIR( "/resources/fonts" );
42
43 void TestDebugVisual( Visual::Base& visual, Visual::Type actualType, Vector2 expectedNaturalSize )
44 {
45   {
46     auto& impl = GetImplementation( visual );
47     DALI_TEST_CHECK( &typeid( Toolkit::Internal::WireframeVisual ) == &typeid( impl ) );
48   }
49
50   Vector2 naturalSize;
51   visual.GetNaturalSize( naturalSize );
52   DALI_TEST_EQUALS( naturalSize, expectedNaturalSize, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
53
54   Property::Map propertyMap;
55   visual.CreatePropertyMap( propertyMap );
56   Property::Value* typeValue = propertyMap.Find( Toolkit::Visual::Property::TYPE,  Property::INTEGER );
57   if ( typeValue )
58   {
59     DALI_TEST_CHECK( typeValue->Get<int>() == actualType );
60   }
61
62   DummyControl actor = DummyControl::New();
63   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
64   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
65   Stage::GetCurrent().Add( actor );
66
67   DALI_TEST_EQUALS( actor.GetRendererCount(), 1, TEST_LOCATION );
68   if( actor.GetRendererCount() > 0 )
69   {
70     Geometry geometry = actor.GetRendererAt( 0 ).GetGeometry();
71     DALI_TEST_CHECK( geometry.GetType() == Geometry::LINES );
72   }
73 }
74 }
75
76 void dali_debug_rendering_startup(void)
77 {
78   test_return_value = TET_UNDEF;
79 }
80
81 void dali_debug_rendering_cleanup(void)
82 {
83   test_return_value = TET_PASS;
84 }
85
86 int UtcDaliDebugRenderingGetVisual1(void)
87 {
88   EnvironmentVariable::SetTestingEnvironmentVariable(true);
89   ToolkitTestApplication application;
90   tet_infoline( "UtcDaliDebugRenderingGetVisual1:  Request visual with a Property::Map" );
91
92   VisualFactory factory = VisualFactory::Get();
93   DALI_TEST_CHECK( factory );
94
95   // Test that color visual is replaced with debug visual
96   Property::Map propertyMap1;
97   propertyMap1.Insert(Visual::Property::TYPE,  Visual::COLOR);
98   propertyMap1.Insert(ColorVisual::Property::MIX_COLOR,  Color::BLUE);
99   Visual::Base colorVisual = factory.CreateVisual(propertyMap1);
100   DALI_TEST_CHECK( colorVisual );
101   TestDebugVisual( colorVisual, Visual::COLOR, Vector2::ZERO );
102
103   // Test that border visual is replaced with debug visual
104   Property::Map propertyMap2;
105   propertyMap2.Insert(Visual::Property::TYPE,  Visual::BORDER);
106   propertyMap2.Insert(BorderVisual::Property::COLOR,  Color::BLUE);
107   propertyMap2.Insert(BorderVisual::Property::SIZE,  2.f);
108   Visual::Base borderVisual = factory.CreateVisual(propertyMap2);
109   DALI_TEST_CHECK( borderVisual );
110   TestDebugVisual( borderVisual, Visual::BORDER, Vector2::ZERO );
111
112   // Test that gradient visual is replaced with debug visual
113   Property::Map propertyMap3;
114   propertyMap3.Insert(Visual::Property::TYPE,  Visual::GRADIENT);
115   Vector2 start(-1.f, -1.f);
116   Vector2 end(1.f, 1.f);
117   propertyMap3.Insert(GradientVisual::Property::START_POSITION, start);
118   propertyMap3.Insert(GradientVisual::Property::END_POSITION, end);
119   propertyMap3.Insert(GradientVisual::Property::SPREAD_METHOD, GradientVisual::SpreadMethod::REPEAT);
120   Property::Array stopOffsets;
121   stopOffsets.PushBack( 0.2f );
122   stopOffsets.PushBack( 0.8f );
123   propertyMap3.Insert(GradientVisual::Property::STOP_OFFSET, stopOffsets);
124   Property::Array stopColors;
125   stopColors.PushBack( Color::RED );
126   stopColors.PushBack( Color::GREEN );
127   propertyMap3.Insert(GradientVisual::Property::STOP_COLOR, stopColors);
128   Visual::Base gradientVisual = factory.CreateVisual(propertyMap3);
129   DALI_TEST_CHECK( gradientVisual );
130   TestDebugVisual( gradientVisual, Visual::GRADIENT, Vector2::ZERO );
131
132   // Test that image visual is replaced with debug visual
133   Property::Map propertyMap4;
134   propertyMap4.Insert( Toolkit::Visual::Property::TYPE,  Visual::IMAGE );
135   propertyMap4.Insert( ImageVisual::Property::URL,  TEST_IMAGE_FILE_NAME );
136   propertyMap4.Insert( ImageVisual::Property::DESIRED_WIDTH,  50.f );
137   propertyMap4.Insert( ImageVisual::Property::DESIRED_HEIGHT,  100.f );
138   Visual::Base imageVisual = factory.CreateVisual( propertyMap4 );
139   DALI_TEST_CHECK( imageVisual );
140   TestDebugVisual( imageVisual, Visual::IMAGE, Vector2( 50.f, 100.f ) );
141
142   // Test that SVG visual is replaced with debug visual
143   // TEST_SVG_FILE:
144   //  <svg width="100" height="100">
145   //  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
146   //  </svg>
147   Property::Map propertyMap5;
148   propertyMap5.Insert( Toolkit::Visual::Property::TYPE, Visual::SVG );
149   propertyMap5.Insert( ImageVisual::Property::URL,  TEST_SVG_FILE_NAME );
150   Visual::Base svgVisual = factory.CreateVisual( propertyMap5 );
151
152   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
153   DALI_TEST_CHECK( svgVisual );
154   TestDebugVisual( svgVisual, Visual::SVG, Vector2(100.f, 100.f) );
155
156   // Test that AnimatedImageVisual is replaced with debug visual
157   // TEST_GIF_FILE: anim.gif
158   // resolution: 50*50, frame count: 4, frame delay: 0.2 second for each frame
159   Property::Map propertyMap6;
160   propertyMap6.Insert( Toolkit::Visual::Property::TYPE, Visual::ANIMATED_IMAGE );
161   propertyMap6.Insert( ImageVisual::Property::URL,  TEST_GIF_FILE_NAME );
162   Visual::Base animatedImageVisual = factory.CreateVisual( propertyMap6 );
163   DALI_TEST_CHECK( animatedImageVisual );
164   TestDebugVisual( animatedImageVisual, Visual::ANIMATED_IMAGE, Vector2(50.f, 50.f) );
165
166   // Test that text visual is replaced with debug visual
167
168   // Load some fonts to get the same metrics on different platforms.
169   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
170   fontClient.SetDpi( 96u, 96u );
171
172   char* pathNamePtr = get_current_dir_name();
173   const std::string pathName( pathNamePtr );
174   free( pathNamePtr );
175
176   fontClient.GetFontId( pathName + DEFAULT_FONT_DIR + "/tizen/TizenSansRegular.ttf" );
177
178   Property::Map propertyMap7;
179   propertyMap7.Insert( Toolkit::Visual::Property::TYPE, Visual::TEXT );
180   propertyMap7.Insert( TextVisual::Property::ENABLE_MARKUP, true );
181   propertyMap7.Insert( TextVisual::Property::TEXT, "<font family='TizenSans' size='12'>Hello world</font>" );
182   propertyMap7.Insert( TextVisual::Property::MULTI_LINE, true );
183
184   Visual::Base textVisual = factory.CreateVisual( propertyMap7 );
185   DALI_TEST_CHECK( textVisual );
186   {
187     auto&& impl = GetImplementation( textVisual );
188     DALI_TEST_CHECK( &typeid( Toolkit::Internal::WireframeVisual ) == &typeid( impl ) );
189   }
190
191   Vector2 naturalSize;
192   textVisual.GetNaturalSize( naturalSize );
193   DALI_TEST_EQUALS( naturalSize, Vector2( 78.f, 20.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
194
195   const float height = textVisual.GetHeightForWidth( 40.f );
196   DALI_TEST_EQUALS( height, 38.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
197
198   // Test that NPatchVisual is replaced with debug visual
199   // TEST_NPATCH_FILE_NAME: image_01.9.jpg
200   Property::Map propertyMap8;
201   propertyMap8.Insert( Toolkit::Visual::Property::TYPE, Visual::N_PATCH );
202   propertyMap8.Insert( ImageVisual::Property::URL,  TEST_NPATCH_FILE_NAME );
203   Visual::Base nPatchVisual = factory.CreateVisual( propertyMap8 );
204   DALI_TEST_CHECK( nPatchVisual );
205   TestDebugVisual( nPatchVisual, Visual::N_PATCH, Vector2::ZERO );
206
207   EnvironmentVariable::SetTestingEnvironmentVariable(false);
208   END_TEST;
209 }
210
211 int UtcDaliDebugRenderingGetVisual2(void)
212 {
213   EnvironmentVariable::SetTestingEnvironmentVariable(true);
214   ToolkitTestApplication application;
215   tet_infoline( "UtcDaliDebugRenderingGetVisual2: Request visual with various parameters" );
216
217   VisualFactory factory = VisualFactory::Get();
218   DALI_TEST_CHECK( factory );
219
220   // Test that color visual is replaced with debug visual
221   Dali::Property::Map map;
222   map[ Toolkit::Visual::Property::TYPE ] = Visual::COLOR;
223   map[ ColorVisual::Property::MIX_COLOR ] = Color::CYAN;
224
225   Visual::Base colorVisual = factory.CreateVisual( map);
226   DALI_TEST_CHECK( colorVisual );
227   TestDebugVisual( colorVisual, Visual::COLOR, Vector2::ZERO );
228
229   // Test that border visual is replaced with debug visual
230   map.Clear();
231   map[ Toolkit::Visual::Property::TYPE ] = Visual::BORDER;
232   map[ BorderVisual::Property::COLOR  ] = Color::GREEN;
233   map[ BorderVisual::Property::SIZE   ] = 2.f;
234   Visual::Base borderVisual = factory.CreateVisual( map );
235   DALI_TEST_CHECK( borderVisual );
236   TestDebugVisual( borderVisual, Visual::BORDER, Vector2::ZERO );
237
238   // Test that image visual is replaced with debug visual
239   Image image = ResourceImage::New(TEST_IMAGE_FILE_NAME);
240   Visual::Base imageVisual = factory.CreateVisual( image );
241   DALI_TEST_CHECK( imageVisual );
242   TestDebugVisual( imageVisual, Visual::IMAGE, Vector2::ZERO);
243
244   // Test that n patch visual is replaced with debug visual
245   Visual::Base nPatchVisual = factory.CreateVisual( TEST_NPATCH_FILE_NAME, ImageDimensions() );
246   DALI_TEST_CHECK( nPatchVisual );
247   TestDebugVisual( nPatchVisual, Visual::N_PATCH, Vector2::ZERO );
248
249   EnvironmentVariable::SetTestingEnvironmentVariable(false);
250   END_TEST;
251 }
252
253 int UtcDaliDebugRenderingGetVisualObject01(void)
254 {
255   EnvironmentVariable::SetTestingEnvironmentVariable( true );
256   ToolkitTestApplication application;
257
258   VisualFactory factory = VisualFactory::Get();
259   DALI_TEST_CHECK( factory );
260
261   tet_infoline( "Create a TextVisual when debugging is enabled, thus creating a proxy Wireframe Visual" );
262
263   Dali::Property::Map map;
264   map[ Toolkit::Visual::Property::TYPE ] = Visual::TEXT;
265   map[ TextVisual::Property::TEXT ] = "Hello";
266
267   Visual::Base textVisual = factory.CreateVisual( map);
268   DALI_TEST_CHECK( textVisual );
269
270   tet_infoline( "Check that GetVisualObject returns the actual TextVisual" );
271   Toolkit::Internal::Visual::Base& visualImpl = GetImplementation( textVisual ).GetVisualObject();
272   DALI_TEST_CHECK( dynamic_cast< Toolkit::Internal::TextVisual* >( &visualImpl ) );
273
274   tet_infoline( "Compare the returned TextVisual with the visual implementation, should differ" );
275   DALI_TEST_CHECK( textVisual.GetObjectPtr() != &visualImpl );
276
277   END_TEST;
278 }
279
280 int UtcDaliDebugRenderingGetVisualObject02(void)
281 {
282   ToolkitTestApplication application;
283
284   VisualFactory factory = VisualFactory::Get();
285   DALI_TEST_CHECK( factory );
286
287   tet_infoline( "Create a TextVisual without debugging enabled, thus no proxy Wireframe Visual" );
288
289   Dali::Property::Map map;
290   map[ Toolkit::Visual::Property::TYPE ] = Visual::TEXT;
291   map[ TextVisual::Property::TEXT ] = "Hello";
292
293   Visual::Base textVisual = factory.CreateVisual( map);
294   DALI_TEST_CHECK( textVisual );
295
296   tet_infoline( "Check that GetVisualObject returns the actual TextVisual" );
297   Toolkit::Internal::Visual::Base& visualImpl = GetImplementation( textVisual ).GetVisualObject();
298   DALI_TEST_CHECK( dynamic_cast< Toolkit::Internal::TextVisual* >( &visualImpl ) );
299
300   tet_infoline( "Compare the returned TextVisual with the visual implementation, should be the same" );
301   DALI_TEST_CHECK( textVisual.GetObjectPtr() == &visualImpl );
302
303   END_TEST;
304 }
305
306 int UtcDaliDebugRenderingGetVisualObject03(void)
307 {
308   ToolkitTestApplication application;
309
310   VisualFactory factory = VisualFactory::Get();
311   DALI_TEST_CHECK( factory );
312
313   tet_infoline( "Create a WireframeVisual without debugging enabled, thus no proxy Wireframe Visual either" );
314
315   Dali::Property::Map map;
316   map[ Toolkit::Visual::Property::TYPE ] = Visual::WIREFRAME;
317
318   Visual::Base textVisual = factory.CreateVisual( map);
319   DALI_TEST_CHECK( textVisual );
320
321   tet_infoline( "Check that GetVisualObject returns the WireframeVisual" );
322   Toolkit::Internal::Visual::Base& visualImpl = GetImplementation( textVisual ).GetVisualObject();
323   DALI_TEST_CHECK( dynamic_cast< Toolkit::Internal::WireframeVisual* >( &visualImpl ) );
324
325   tet_infoline( "Compare the returned Visual with the visual implementation, should be the same" );
326   DALI_TEST_CHECK( textVisual.GetObjectPtr() == &visualImpl );
327
328   END_TEST;
329 }
330
331 int UtcDaliDebugRenderingRenderText(void)
332 {
333   EnvironmentVariable::SetTestingEnvironmentVariable( true );
334   ToolkitTestApplication application;
335   tet_infoline( "Ensure we can render text when in debug mode" );
336
337   try
338   {
339     Toolkit::TextLabel label = TextLabel::New( "Hello" );
340     Stage::GetCurrent().Add( label );
341     DALI_TEST_CHECK( true );
342   } catch( ... )
343   {
344     DALI_TEST_CHECK( false );
345   }
346
347   END_TEST;
348 }