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 // TODO: Not supported yes
425 // TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
426 // std::ostringstream out;
427 // out << GL_DEPTH_TEST;
428 // DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", out.str().c_str() ) );
430 DALI_TEST_EQUALS( geometry.GetRequiresDepthTesting(), true, TEST_LOCATION );
435 int UtcDaliGeometryPropertyRequiresDepthTest(void)
437 TestApplication application;
439 tet_infoline("Test SetRequiresDepthTesting, GetRequiresDepthTesting");
441 Shader shader = Shader::New("VertexSource", "FragmentSource");
442 Material material = Material::New( shader );
444 Geometry geometry = CreateQuadGeometry();
445 Renderer renderer = Renderer::New( geometry, material );
447 Actor actor = Actor::New();
448 actor.AddRenderer(renderer);
449 actor.SetSize(400, 400);
450 Stage::GetCurrent().Add(actor);
452 DALI_TEST_EQUALS( geometry.GetProperty<bool>(Geometry::Property::REQUIRES_DEPTH_TEST), false, TEST_LOCATION );
454 geometry.SetProperty(Geometry::Property::REQUIRES_DEPTH_TEST, true );
456 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
457 glAbstraction.EnableCullFaceCallTrace(true);
458 application.SendNotification();
459 application.Render();
460 // TODO: Not supported yes
461 // TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
462 // std::ostringstream out;
463 // out << GL_DEPTH_TEST;
464 // DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", out.str().c_str() ) );
466 DALI_TEST_EQUALS( geometry.GetProperty<bool>(Geometry::Property::REQUIRES_DEPTH_TEST), true, TEST_LOCATION );
471 int UtcDaliGeometryConstraint(void)
473 TestApplication application;
475 tet_infoline("Test that a custom geometry property can be constrained");
477 Shader shader = Shader::New("VertexSource", "FragmentSource");
478 Material material = Material::New( shader );
479 material.SetProperty(Material::Property::COLOR, Color::WHITE);
481 Geometry geometry = CreateQuadGeometry();
482 Renderer renderer = Renderer::New( geometry, material );
484 Actor actor = Actor::New();
485 actor.AddRenderer(renderer);
486 actor.SetSize(400, 400);
487 Stage::GetCurrent().Add(actor);
489 Vector4 initialColor = Color::WHITE;
490 Property::Index colorIndex = geometry.RegisterProperty( "uFadeColor", initialColor );
492 application.SendNotification();
493 application.Render(0);
494 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
497 Constraint constraint = Constraint::New<Vector4>( geometry, colorIndex, TestConstraintNoBlue );
499 application.SendNotification();
500 application.Render(0);
502 // Expect no blue component in either buffer - yellow
503 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
504 application.Render(0);
505 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
507 geometry.RemoveConstraints();
508 geometry.SetProperty(colorIndex, Color::WHITE );
509 application.SendNotification();
510 application.Render(0);
511 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), Color::WHITE, TEST_LOCATION );
516 int UtcDaliGeometryConstraint02(void)
518 TestApplication application;
520 tet_infoline("Test that a uniform map geometry property can be constrained");
522 Shader shader = Shader::New("VertexSource", "FragmentSource");
523 Material material = Material::New( shader );
524 material.SetProperty(Material::Property::COLOR, Color::WHITE);
526 Geometry geometry = CreateQuadGeometry();
527 Renderer renderer = Renderer::New( geometry, material );
529 Actor actor = Actor::New();
530 actor.AddRenderer(renderer);
531 actor.SetSize(400, 400);
532 Stage::GetCurrent().Add(actor);
533 application.SendNotification();
534 application.Render(0);
536 Vector4 initialColor = Color::WHITE;
537 Property::Index colorIndex = geometry.RegisterProperty( "uFadeColor", initialColor );
539 TestGlAbstraction& gl = application.GetGlAbstraction();
541 application.SendNotification();
542 application.Render(0);
544 Vector4 actualValue(Vector4::ZERO);
545 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
546 DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
549 Constraint constraint = Constraint::New<Vector4>( geometry, colorIndex, TestConstraintNoBlue );
551 application.SendNotification();
552 application.Render(0);
554 // Expect no blue component in either buffer - yellow
555 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
556 DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
558 application.Render(0);
559 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
560 DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
562 geometry.RemoveConstraints();
563 geometry.SetProperty(colorIndex, Color::WHITE );
564 application.SendNotification();
565 application.Render(0);
567 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
568 DALI_TEST_EQUALS( actualValue, Color::WHITE, TEST_LOCATION );
575 int UtcDaliGeometryAnimatedProperty01(void)
577 TestApplication application;
579 tet_infoline("Test that a custom geometry property can be animated");
581 Shader shader = Shader::New("VertexSource", "FragmentSource");
582 Material material = Material::New( shader );
583 material.SetProperty(Material::Property::COLOR, Color::WHITE);
585 Geometry geometry = CreateQuadGeometry();
586 Renderer renderer = Renderer::New( geometry, material );
588 Actor actor = Actor::New();
589 actor.AddRenderer(renderer);
590 actor.SetSize(400, 400);
591 Stage::GetCurrent().Add(actor);
593 Vector4 initialColor = Color::WHITE;
594 Property::Index colorIndex = geometry.RegisterProperty( "uFadeColor", initialColor );
596 application.SendNotification();
597 application.Render(0);
598 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
600 Animation animation = Animation::New(1.0f);
601 KeyFrames keyFrames = KeyFrames::New();
602 keyFrames.Add(0.0f, initialColor);
603 keyFrames.Add(1.0f, Color::TRANSPARENT);
604 animation.AnimateBetween( Property( geometry, colorIndex ), keyFrames );
607 application.SendNotification();
608 application.Render(500);
610 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), Color::WHITE * 0.5f, TEST_LOCATION );
612 application.Render(500);
614 DALI_TEST_EQUALS( geometry.GetProperty<Vector4>(colorIndex), Color::TRANSPARENT, TEST_LOCATION );
619 int UtcDaliGeometryAnimatedProperty02(void)
621 TestApplication application;
623 tet_infoline("Test that a uniform map geometry property can be animated");
625 Shader shader = Shader::New("VertexSource", "FragmentSource");
626 Material material = Material::New( shader );
627 material.SetProperty(Material::Property::COLOR, Color::WHITE);
629 Geometry geometry = CreateQuadGeometry();
630 Renderer renderer = Renderer::New( geometry, material );
632 Actor actor = Actor::New();
633 actor.AddRenderer(renderer);
634 actor.SetSize(400, 400);
635 Stage::GetCurrent().Add(actor);
636 application.SendNotification();
637 application.Render(0);
639 Vector4 initialColor = Color::WHITE;
640 Property::Index colorIndex = geometry.RegisterProperty( "uFadeColor", initialColor );
642 TestGlAbstraction& gl = application.GetGlAbstraction();
644 application.SendNotification();
645 application.Render(0);
647 Vector4 actualValue(Vector4::ZERO);
648 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
649 DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
651 Animation animation = Animation::New(1.0f);
652 KeyFrames keyFrames = KeyFrames::New();
653 keyFrames.Add(0.0f, initialColor);
654 keyFrames.Add(1.0f, Color::TRANSPARENT);
655 animation.AnimateBetween( Property( geometry, colorIndex ), keyFrames );
658 application.SendNotification();
659 application.Render(500);
661 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
662 DALI_TEST_EQUALS( actualValue, Color::WHITE * 0.5f, TEST_LOCATION );
664 application.Render(500);
665 DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
666 DALI_TEST_EQUALS( actualValue, Color::TRANSPARENT, TEST_LOCATION );