2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali/public-api/dali-core.h>
19 #include <dali-test-suite-utils.h>
23 #include <mesh-builder.h>
25 void geometry_test_startup(void)
27 test_return_value = TET_UNDEF;
30 void geometry_test_cleanup(void)
32 test_return_value = TET_PASS;
38 void TestConstraintNoBlue( Vector4& current, const PropertyInputContainer& inputs )
43 struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
45 PropertyBuffer CreateVertexBuffer( const std::string& aPosition, const std::string& aTexCoord )
47 const float halfQuadSize = .5f;
48 TexturedQuadVertex texturedQuadVertexData[4] = {
49 { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
50 { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
51 { Vector2(-halfQuadSize, halfQuadSize), Vector2(0.f, 1.f) },
52 { Vector2( halfQuadSize, halfQuadSize), Vector2(1.f, 1.f) } };
54 Property::Map vertexFormat;
55 vertexFormat[aPosition] = Property::VECTOR2;
56 vertexFormat[aTexCoord] = Property::VECTOR2;
58 PropertyBuffer vertexData = PropertyBuffer::New( vertexFormat, 4 );
59 vertexData.SetData(texturedQuadVertexData);
64 PropertyBuffer CreateIndexBuffer()
66 unsigned short indexData[6] = { 0, 3, 1, 0, 2, 3 };
67 Property::Map indexFormat;
68 indexFormat["indices"] = Property::UNSIGNED_INTEGER; // Should be Unsigned Short
69 PropertyBuffer indices = PropertyBuffer::New( indexFormat, 3 );
70 indices.SetData(indexData);
78 int UtcDaliGeometryNew01(void)
80 TestApplication application;
82 Geometry geometry = Geometry::New();
84 DALI_TEST_EQUALS( (bool)geometry, true, TEST_LOCATION );
88 int UtcDaliGeometryNew02(void)
90 TestApplication application;
92 DALI_TEST_EQUALS( (bool)geometry, false, TEST_LOCATION );
96 int UtcDaliGeometryCopyConstructor(void)
98 TestApplication application;
100 Geometry geometry = Geometry::New();
102 Geometry geometryCopy(geometry);
104 DALI_TEST_EQUALS( (bool)geometryCopy, true, TEST_LOCATION );
108 int UtcDaliGeometryAssignmentOperator(void)
110 TestApplication application;
112 Geometry geometry = Geometry::New();
115 DALI_TEST_EQUALS( (bool)geometry2, false, TEST_LOCATION );
117 geometry2 = geometry;
118 DALI_TEST_EQUALS( (bool)geometry2, true, TEST_LOCATION );
123 int UtcDaliGeometryDownCast01(void)
125 TestApplication application;
127 Geometry geometry = Geometry::New();
129 BaseHandle handle(geometry);
130 Geometry geometry2 = Geometry::DownCast(handle);
131 DALI_TEST_EQUALS( (bool)geometry2, true, TEST_LOCATION );
135 int UtcDaliGeometryDownCast02(void)
137 TestApplication application;
139 Handle handle = Handle::New(); // Create a custom object
140 Geometry geometry = Geometry::DownCast(handle);
141 DALI_TEST_EQUALS( (bool)geometry, false, TEST_LOCATION );
145 int UtcDaliGeometryAddVertexBuffer(void)
147 TestApplication application;
149 tet_infoline("Test AddVertexBuffer");
151 PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
152 Geometry geometry = Geometry::New();
153 geometry.AddVertexBuffer( vertexBuffer1 );
155 Material material = CreateMaterial(1.f);
156 Renderer renderer = Renderer::New(geometry, material);
157 Actor actor = Actor::New();
158 actor.SetSize(Vector3::ONE * 100.f);
159 actor.AddRenderer(renderer);
160 Stage::GetCurrent().Add(actor);
162 application.SendNotification();
163 application.Render(0);
164 application.Render();
165 application.SendNotification();
168 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
169 application.GetGlAbstraction().GetBufferDataCalls();
171 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
173 DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
176 // add the second vertex buffer
177 application.GetGlAbstraction().ResetBufferDataCalls();
179 PropertyBuffer vertexBuffer2 = CreateVertexBuffer( "aPosition2", "aTexCoord2" );
180 geometry.AddVertexBuffer( vertexBuffer2 );
181 application.SendNotification();
182 application.Render(0);
183 application.Render();
184 application.SendNotification();
187 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
188 application.GetGlAbstraction().GetBufferDataCalls();
190 DALI_TEST_EQUALS( bufferDataCalls.size(), 2u, TEST_LOCATION );
192 DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
193 DALI_TEST_EQUALS( bufferDataCalls[1], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
199 int UtcDaliGeometryGetNumberOfVertexBuffers(void)
201 TestApplication application;
203 tet_infoline("Test GetNumberOfVertexBuffers");
204 PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
205 PropertyBuffer vertexBuffer2 = CreateVertexBuffer("aPosition2", "aTexCoord2" );
206 PropertyBuffer vertexBuffer3 = CreateVertexBuffer("aPosition3", "aTexCoord3" );
208 Geometry geometry = Geometry::New();
209 geometry.AddVertexBuffer( vertexBuffer1 );
210 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
212 geometry.AddVertexBuffer( vertexBuffer2 );
213 geometry.AddVertexBuffer( vertexBuffer3 );
214 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 3u, TEST_LOCATION );
216 geometry.RemoveVertexBuffer( 2u );
217 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 2u, TEST_LOCATION );
222 int UtcDaliGeometryRemoveVertexBuffer(void)
224 TestApplication application;
226 tet_infoline("Test RemoveVertexBuffer");
228 PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
229 PropertyBuffer vertexBuffer2 = CreateVertexBuffer("aPosition2", "aTexCoord2" );
231 Geometry geometry = Geometry::New();
232 geometry.AddVertexBuffer( vertexBuffer1 );
234 Material material = CreateMaterial(1.f);
235 Renderer renderer = Renderer::New(geometry, material);
236 Actor actor = Actor::New();
237 actor.SetSize(Vector3::ONE * 100.f);
238 actor.AddRenderer(renderer);
239 Stage::GetCurrent().Add(actor);
241 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
243 geometry.RemoveVertexBuffer( 0 );
244 geometry.AddVertexBuffer( vertexBuffer2 );
245 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
247 geometry.RemoveVertexBuffer( 0 );
248 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 0u, TEST_LOCATION );
250 //Todo: test by checking the BufferDataCalls
251 // make sure the vertex buffer in actually removed from gl
256 int UtcDaliGeometrySetIndexBuffer(void)
258 TestApplication application;
260 tet_infoline("Test SetIndexBuffer");
262 PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
263 PropertyBuffer indexBuffer = CreateIndexBuffer( );
265 Geometry geometry = Geometry::New();
266 geometry.AddVertexBuffer( vertexBuffer );
268 Material material = CreateMaterial(1.f);
269 Renderer renderer = Renderer::New(geometry, material);
270 Actor actor = Actor::New();
271 actor.SetSize(Vector3::ONE * 100.f);
272 actor.AddRenderer(renderer);
273 Stage::GetCurrent().Add(actor);
275 application.SendNotification();
276 application.Render(0);
277 application.Render();
278 application.SendNotification();
281 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
282 application.GetGlAbstraction().GetBufferDataCalls();
284 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
286 DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
290 application.GetGlAbstraction().ResetBufferDataCalls();
292 geometry.SetIndexBuffer( indexBuffer );
293 application.SendNotification();
294 application.Render(0);
295 application.Render();
296 application.SendNotification();
299 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
300 application.GetGlAbstraction().GetBufferDataCalls();
302 DALI_TEST_EQUALS( bufferDataCalls.size(), 2u, TEST_LOCATION );
304 DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
305 // should be unsigned short instead of unsigned int
306 DALI_TEST_EQUALS( bufferDataCalls[1], 6*sizeof( unsigned short ), TEST_LOCATION );
313 int UtcDaliGeometrySetGetGeometryType(void)
315 TestApplication application;
317 tet_infoline("Test SetGeometryType and GetGeometryType");
319 unsigned int numVertex = 4u;
320 PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
322 Geometry geometry = Geometry::New();
323 geometry.AddVertexBuffer( vertexBuffer );
325 Material material = CreateMaterial(1.f);
326 Renderer renderer = Renderer::New(geometry, material);
327 Actor actor = Actor::New();
328 actor.SetSize(Vector3::ONE * 100.f);
329 actor.AddRenderer(renderer);
330 Stage::GetCurrent().Add(actor);
332 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
333 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
335 /****************************************************/
336 // Default (TRIANGLES), no index buffer
338 drawTrace.Enable(true);
339 application.SendNotification();
340 application.Render(0);
341 application.Render();
342 application.SendNotification();
343 drawTrace.Enable( false );
345 // Test the default geometry type is GL_TRIANGLE
346 // no index buffer, call glDrawArrays,
347 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
348 std::stringstream out;
349 out << GL_TRIANGLES << ", " << 0 << ", " << numVertex;
350 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
352 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::TRIANGLES, TEST_LOCATION);
354 /*********************************************************/
355 // LINES, no index buffer
356 geometry.SetGeometryType( Geometry::LINES );
359 drawTrace.Enable(true);
360 application.SendNotification();
361 application.Render(0);
362 application.Render();
363 application.SendNotification();
364 drawTrace.Enable( false );
366 // geometry type is set as GL_LINES
367 // no index buffer, call glDrawArrays,
368 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
370 out << GL_LINES << ", " << 0 << ", " << numVertex;
371 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
373 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::LINES, TEST_LOCATION);
375 /*****************************************************/
377 geometry.SetGeometryType( Geometry::POINTS );
380 drawTrace.Enable(true);
381 application.SendNotification();
382 application.Render(0);
383 application.Render();
384 application.SendNotification();
385 drawTrace.Enable( false );
387 // geometry type is set as GL_POINTS
388 // no index buffer, call glDrawArrays,
389 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
391 out << GL_POINTS << ", " << 0 << ", " << numVertex;
392 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
394 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::POINTS, TEST_LOCATION);
399 int UtcDaliGeometrySetGetRequireDepthTesting(void)
401 TestApplication application;
403 tet_infoline("Test SetRequiresDepthTesting, GetRequiresDepthTesting");
405 Shader shader = Shader::New("VertexSource", "FragmentSource");
406 Material material = Material::New( shader );
408 Geometry geometry = CreateQuadGeometry();
409 Renderer renderer = Renderer::New( geometry, material );
411 Actor actor = Actor::New();
412 actor.AddRenderer(renderer);
413 actor.SetSize(400, 400);
414 Stage::GetCurrent().Add(actor);
416 DALI_TEST_EQUALS( geometry.GetRequiresDepthTesting(), false, TEST_LOCATION );
418 geometry.SetRequiresDepthTesting(true);
420 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
421 glAbstraction.EnableCullFaceCallTrace(true);
422 application.SendNotification();
423 application.Render();
424 TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
425 std::ostringstream out;
426 out << GL_DEPTH_TEST;
427 DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", out.str().c_str() ) );
429 DALI_TEST_EQUALS( geometry.GetRequiresDepthTesting(), true, TEST_LOCATION );
434 int UtcDaliGeometryPropertyRequiresDepthTest(void)
436 TestApplication application;
438 tet_infoline("Test SetRequiresDepthTesting, GetRequiresDepthTesting");
440 Shader shader = Shader::New("VertexSource", "FragmentSource");
441 Material material = Material::New( shader );
443 Geometry geometry = CreateQuadGeometry();
444 Renderer renderer = Renderer::New( geometry, material );
446 Actor actor = Actor::New();
447 actor.AddRenderer(renderer);
448 actor.SetSize(400, 400);
449 Stage::GetCurrent().Add(actor);
451 DALI_TEST_EQUALS( geometry.GetProperty<bool>(Geometry::Property::REQUIRES_DEPTH_TEST), false, TEST_LOCATION );
453 geometry.SetProperty(Geometry::Property::REQUIRES_DEPTH_TEST, true );
455 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
456 glAbstraction.EnableCullFaceCallTrace(true);
457 application.SendNotification();
458 application.Render();
459 TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
460 std::ostringstream out;
461 out << GL_DEPTH_TEST;
462 DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", out.str().c_str() ) );
464 DALI_TEST_EQUALS( geometry.GetProperty<bool>(Geometry::Property::REQUIRES_DEPTH_TEST), true, TEST_LOCATION );
469 int UtcDaliGeometryConstraint(void)
471 TestApplication application;
473 tet_infoline("Test that a custom geometry property can be constrained");
475 Shader shader = Shader::New("VertexSource", "FragmentSource");
476 Material material = Material::New( shader );
477 material.SetProperty(Material::Property::COLOR, Color::WHITE);
479 Geometry geometry = CreateQuadGeometry();
480 Renderer renderer = Renderer::New( geometry, material );
482 Actor actor = Actor::New();
483 actor.AddRenderer(renderer);
484 actor.SetSize(400, 400);
485 Stage::GetCurrent().Add(actor);
487 Vector4 initialColor = Color::WHITE;
488 Property::Index colorIndex = geometry.RegisterProperty( "uFadeColor", initialColor );
490 application.SendNotification();
491 application.Render(0);
492 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
495 Constraint constraint = Constraint::New<Vector4>( geometry, colorIndex, TestConstraintNoBlue );
497 application.SendNotification();
498 application.Render(0);
500 // Expect no blue component in either buffer - yellow
501 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
502 application.Render(0);
503 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
505 geometry.RemoveConstraints();
506 geometry.SetProperty(colorIndex, Color::WHITE );
507 application.SendNotification();
508 application.Render(0);
509 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), Color::WHITE, TEST_LOCATION );
514 int UtcDaliGeometryConstraint02(void)
516 TestApplication application;
518 tet_infoline("Test that a uniform map geometry property can be constrained");
520 Shader shader = Shader::New("VertexSource", "FragmentSource");
521 Material material = Material::New( shader );
522 material.SetProperty(Material::Property::COLOR, Color::WHITE);
524 Geometry geometry = CreateQuadGeometry();
525 Renderer renderer = Renderer::New( geometry, material );
527 Actor actor = Actor::New();
528 actor.AddRenderer(renderer);
529 actor.SetSize(400, 400);
530 Stage::GetCurrent().Add(actor);
531 application.SendNotification();
532 application.Render(0);
534 Vector4 initialColor = Color::WHITE;
535 Property::Index colorIndex = geometry.RegisterProperty( "uFadeColor", initialColor );
537 TestGlAbstraction& gl = application.GetGlAbstraction();
539 application.SendNotification();
540 application.Render(0);
542 Vector4 actualValue(Vector4::ZERO);
543 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
544 DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
547 Constraint constraint = Constraint::New<Vector4>( geometry, colorIndex, TestConstraintNoBlue );
549 application.SendNotification();
550 application.Render(0);
552 // Expect no blue component in either buffer - yellow
553 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
554 DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
556 application.Render(0);
557 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
558 DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
560 geometry.RemoveConstraints();
561 geometry.SetProperty(colorIndex, Color::WHITE );
562 application.SendNotification();
563 application.Render(0);
565 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
566 DALI_TEST_EQUALS( actualValue, Color::WHITE, TEST_LOCATION );
573 int UtcDaliGeometryAnimatedProperty01(void)
575 TestApplication application;
577 tet_infoline("Test that a custom geometry property can be animated");
579 Shader shader = Shader::New("VertexSource", "FragmentSource");
580 Material material = Material::New( shader );
581 material.SetProperty(Material::Property::COLOR, Color::WHITE);
583 Geometry geometry = CreateQuadGeometry();
584 Renderer renderer = Renderer::New( geometry, material );
586 Actor actor = Actor::New();
587 actor.AddRenderer(renderer);
588 actor.SetSize(400, 400);
589 Stage::GetCurrent().Add(actor);
591 Vector4 initialColor = Color::WHITE;
592 Property::Index colorIndex = geometry.RegisterProperty( "uFadeColor", initialColor );
594 application.SendNotification();
595 application.Render(0);
596 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
598 Animation animation = Animation::New(1.0f);
599 KeyFrames keyFrames = KeyFrames::New();
600 keyFrames.Add(0.0f, initialColor);
601 keyFrames.Add(1.0f, Color::TRANSPARENT);
602 animation.AnimateBetween( Property( geometry, colorIndex ), keyFrames );
605 application.SendNotification();
606 application.Render(500);
608 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), Color::WHITE * 0.5f, TEST_LOCATION );
610 application.Render(500);
612 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), Color::TRANSPARENT, TEST_LOCATION );
617 int UtcDaliGeometryAnimatedProperty02(void)
619 TestApplication application;
621 tet_infoline("Test that a uniform map geometry property can be animated");
623 Shader shader = Shader::New("VertexSource", "FragmentSource");
624 Material material = Material::New( shader );
625 material.SetProperty(Material::Property::COLOR, Color::WHITE);
627 Geometry geometry = CreateQuadGeometry();
628 Renderer renderer = Renderer::New( geometry, material );
630 Actor actor = Actor::New();
631 actor.AddRenderer(renderer);
632 actor.SetSize(400, 400);
633 Stage::GetCurrent().Add(actor);
634 application.SendNotification();
635 application.Render(0);
637 Vector4 initialColor = Color::WHITE;
638 Property::Index colorIndex = geometry.RegisterProperty( "uFadeColor", initialColor );
640 TestGlAbstraction& gl = application.GetGlAbstraction();
642 application.SendNotification();
643 application.Render(0);
645 Vector4 actualValue(Vector4::ZERO);
646 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
647 DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
649 Animation animation = Animation::New(1.0f);
650 KeyFrames keyFrames = KeyFrames::New();
651 keyFrames.Add(0.0f, initialColor);
652 keyFrames.Add(1.0f, Color::TRANSPARENT);
653 animation.AnimateBetween( Property( geometry, colorIndex ), keyFrames );
656 application.SendNotification();
657 application.Render(500);
659 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
660 DALI_TEST_EQUALS( actualValue, Color::WHITE * 0.5f, TEST_LOCATION );
662 application.Render(500);
663 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
664 DALI_TEST_EQUALS( actualValue, Color::TRANSPARENT, TEST_LOCATION );