2 * Copyright (c) 2016 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 );
59 vertexData.SetData( texturedQuadVertexData, 4 );
68 int UtcDaliGeometryNew01(void)
70 TestApplication application;
72 Geometry geometry = Geometry::New();
74 DALI_TEST_EQUALS( (bool)geometry, true, TEST_LOCATION );
78 int UtcDaliGeometryNew02(void)
80 TestApplication application;
82 DALI_TEST_EQUALS( (bool)geometry, false, TEST_LOCATION );
86 int UtcDaliGeometryCopyConstructor(void)
88 TestApplication application;
90 Geometry geometry = Geometry::New();
92 Geometry geometryCopy(geometry);
94 DALI_TEST_EQUALS( (bool)geometryCopy, true, TEST_LOCATION );
98 int UtcDaliGeometryAssignmentOperator(void)
100 TestApplication application;
102 Geometry geometry = Geometry::New();
105 DALI_TEST_EQUALS( (bool)geometry2, false, TEST_LOCATION );
107 geometry2 = geometry;
108 DALI_TEST_EQUALS( (bool)geometry2, true, TEST_LOCATION );
113 int UtcDaliGeometryDownCast01(void)
115 TestApplication application;
117 Geometry geometry = Geometry::New();
119 BaseHandle handle(geometry);
120 Geometry geometry2 = Geometry::DownCast(handle);
121 DALI_TEST_EQUALS( (bool)geometry2, true, TEST_LOCATION );
125 int UtcDaliGeometryDownCast02(void)
127 TestApplication application;
129 Handle handle = Handle::New(); // Create a custom object
130 Geometry geometry = Geometry::DownCast(handle);
131 DALI_TEST_EQUALS( (bool)geometry, false, TEST_LOCATION );
135 int UtcDaliGeometryAddVertexBuffer(void)
137 TestApplication application;
139 tet_infoline("Test AddVertexBuffer");
141 PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
142 Geometry geometry = Geometry::New();
143 geometry.AddVertexBuffer( vertexBuffer1 );
145 Shader shader = CreateShader();
146 Renderer renderer = Renderer::New(geometry, shader);
147 Actor actor = Actor::New();
148 actor.SetSize(Vector3::ONE * 100.f);
149 actor.AddRenderer(renderer);
150 Stage::GetCurrent().Add(actor);
152 application.SendNotification();
153 application.Render(0);
154 application.Render();
155 application.SendNotification();
158 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
159 application.GetGlAbstraction().GetBufferDataCalls();
161 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
163 DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
166 // add the second vertex buffer
167 application.GetGlAbstraction().ResetBufferDataCalls();
169 PropertyBuffer vertexBuffer2 = CreateVertexBuffer( "aPosition2", "aTexCoord2" );
170 geometry.AddVertexBuffer( vertexBuffer2 );
171 application.SendNotification();
172 application.Render(0);
173 application.Render();
174 application.SendNotification();
177 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
178 application.GetGlAbstraction().GetBufferDataCalls();
180 //Check that only the new buffer gets uploaded
181 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
182 DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
188 int UtcDaliGeometryGetNumberOfVertexBuffers(void)
190 TestApplication application;
192 tet_infoline("Test GetNumberOfVertexBuffers");
193 PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
194 PropertyBuffer vertexBuffer2 = CreateVertexBuffer("aPosition2", "aTexCoord2" );
195 PropertyBuffer vertexBuffer3 = CreateVertexBuffer("aPosition3", "aTexCoord3" );
197 Geometry geometry = Geometry::New();
198 geometry.AddVertexBuffer( vertexBuffer1 );
199 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
201 geometry.AddVertexBuffer( vertexBuffer2 );
202 geometry.AddVertexBuffer( vertexBuffer3 );
203 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 3u, TEST_LOCATION );
205 geometry.RemoveVertexBuffer( 2u );
206 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 2u, TEST_LOCATION );
211 int UtcDaliGeometryRemoveVertexBuffer(void)
213 TestApplication application;
215 tet_infoline("Test RemoveVertexBuffer");
217 PropertyBuffer vertexBuffer1 = CreateVertexBuffer("aPosition1", "aTexCoord1" );
218 PropertyBuffer vertexBuffer2 = CreateVertexBuffer("aPosition2", "aTexCoord2" );
220 Geometry geometry = Geometry::New();
221 geometry.AddVertexBuffer( vertexBuffer1 );
223 Shader shader = CreateShader();
224 Renderer renderer = Renderer::New(geometry, shader);
225 Actor actor = Actor::New();
226 actor.SetSize(Vector3::ONE * 100.f);
227 actor.AddRenderer(renderer);
228 Stage::GetCurrent().Add(actor);
230 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
232 geometry.RemoveVertexBuffer( 0 );
233 geometry.AddVertexBuffer( vertexBuffer2 );
234 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 1u, TEST_LOCATION );
236 geometry.RemoveVertexBuffer( 0 );
237 DALI_TEST_EQUALS( geometry.GetNumberOfVertexBuffers(), 0u, TEST_LOCATION );
239 //Todo: test by checking the BufferDataCalls
240 // make sure the vertex buffer in actually removed from gl
245 int UtcDaliGeometrySetIndexBuffer(void)
247 TestApplication application;
249 tet_infoline("Test SetIndexBuffer");
251 PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
253 Geometry geometry = Geometry::New();
254 geometry.AddVertexBuffer( vertexBuffer );
256 Shader shader = CreateShader();
257 Renderer renderer = Renderer::New(geometry, shader);
258 Actor actor = Actor::New();
259 actor.SetSize(Vector3::ONE * 100.f);
260 actor.AddRenderer(renderer);
261 Stage::GetCurrent().Add(actor);
263 application.SendNotification();
264 application.Render(0);
265 application.Render();
266 application.SendNotification();
269 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
270 application.GetGlAbstraction().GetBufferDataCalls();
272 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
274 DALI_TEST_EQUALS( bufferDataCalls[0], 4*sizeof( TexturedQuadVertex ), TEST_LOCATION );
278 application.GetGlAbstraction().ResetBufferDataCalls();
280 const unsigned short indexData[6] = { 0, 3, 1, 0, 2, 3 };
281 geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
282 application.SendNotification();
283 application.Render(0);
284 application.Render();
285 application.SendNotification();
288 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
289 application.GetGlAbstraction().GetBufferDataCalls();
291 //Only the index buffer should be uploaded
292 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
294 // should be unsigned short instead of unsigned int
295 DALI_TEST_EQUALS( bufferDataCalls[0], 6*sizeof( unsigned short ), TEST_LOCATION );
302 int UtcDaliGeometrySetGetGeometryType01(void)
304 TestApplication application;
306 tet_infoline("Test SetGeometryType and GetGeometryType: without index buffer");
308 unsigned int numVertex = 4u;
309 PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
311 Geometry geometry = Geometry::New();
312 geometry.AddVertexBuffer( vertexBuffer );
314 Shader shader = CreateShader();
315 Renderer renderer = Renderer::New(geometry, shader);
316 Actor actor = Actor::New();
317 actor.SetSize(Vector3::ONE * 100.f);
318 actor.AddRenderer(renderer);
319 Stage::GetCurrent().Add(actor);
321 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
322 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
324 /****************************************************/
325 // Default (TRIANGLES), no index buffer
327 drawTrace.Enable(true);
328 application.SendNotification();
329 application.Render(0);
330 application.Render();
331 application.SendNotification();
332 drawTrace.Enable( false );
334 // Test the default geometry type is GL_TRIANGLE
335 // no index buffer, call glDrawArrays,
336 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
337 std::stringstream out;
338 out << GL_TRIANGLES << ", " << 0 << ", " << numVertex;
339 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
341 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::TRIANGLES, TEST_LOCATION);
343 /*********************************************************/
344 // LINES, no index buffer
345 geometry.SetGeometryType( Geometry::LINES );
348 drawTrace.Enable(true);
349 application.SendNotification();
350 application.Render(0);
351 application.Render();
352 application.SendNotification();
353 drawTrace.Enable( false );
355 // geometry type is set as GL_LINES
356 // no index buffer, call glDrawArrays,
357 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
359 out << GL_LINES << ", " << 0 << ", " << numVertex;
360 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
362 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::LINES, TEST_LOCATION);
364 /*****************************************************/
366 geometry.SetGeometryType( Geometry::POINTS );
369 drawTrace.Enable(true);
370 application.SendNotification();
371 application.Render(0);
372 application.Render();
373 application.SendNotification();
374 drawTrace.Enable( false );
376 // geometry type is set as GL_POINTS
377 // no index buffer, call glDrawArrays,
378 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
380 out << GL_POINTS << ", " << 0 << ", " << numVertex;
381 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
383 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::POINTS, TEST_LOCATION);
385 /*****************************************************/
386 //TRIANGLE_STRIP, no index buffer
387 geometry.SetGeometryType( Geometry::TRIANGLE_STRIP );
390 drawTrace.Enable(true);
391 application.SendNotification();
392 application.Render(0);
393 application.Render();
394 application.SendNotification();
395 drawTrace.Enable( false );
397 // geometry type is set as GL_TRIANGLE_STRIP
398 // no index buffer, call glDrawArrays,
399 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
401 out << GL_TRIANGLE_STRIP << ", " << 0 << ", " << numVertex;
402 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
404 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::TRIANGLE_STRIP, TEST_LOCATION);
406 /*****************************************************/
407 //TRIANGLE_FAN, no index buffer
408 geometry.SetGeometryType( Geometry::TRIANGLE_FAN );
411 drawTrace.Enable(true);
412 application.SendNotification();
413 application.Render(0);
414 application.Render();
415 application.SendNotification();
416 drawTrace.Enable( false );
418 // geometry type is set as GL_TRIANGLE_FAN
419 // no index buffer, call glDrawArrays,
420 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
422 out << GL_TRIANGLE_FAN << ", " << 0 << ", " << numVertex;
423 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
425 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::TRIANGLE_FAN, TEST_LOCATION);
430 int UtcDaliGeometrySetGetGeometryType02(void)
432 TestApplication application;
434 tet_infoline("Test SetGeometryType and GetGeometryType: with index buffer");
436 unsigned int numVertex = 4u;
437 unsigned int numIndex = 6u; // 6 unsigned short
438 PropertyBuffer vertexBuffer = CreateVertexBuffer("aPosition", "aTexCoord" );
441 Geometry geometry = Geometry::New();
442 geometry.AddVertexBuffer( vertexBuffer );
443 const unsigned short indexData[6] = { 0, 3, 1, 0, 2, 3 };
444 geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
446 Shader shader = CreateShader();
447 Renderer renderer = Renderer::New(geometry, shader);
448 Actor actor = Actor::New();
449 actor.SetSize(Vector3::ONE * 100.f);
450 actor.AddRenderer(renderer);
451 Stage::GetCurrent().Add(actor);
453 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
454 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
456 /****************************************************/
457 // Default (TRIANGLES), with index buffer
459 drawTrace.Enable(true);
460 application.SendNotification();
461 application.Render(0);
462 application.Render();
463 application.SendNotification();
464 drawTrace.Enable( false );
466 // Test the default geometry type is GL_TRIANGLE
467 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
468 std::stringstream out;
469 out << GL_TRIANGLES << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
470 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
472 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::TRIANGLES, TEST_LOCATION);
474 /*********************************************************/
475 // LINES, with index buffer
476 geometry.SetGeometryType( Geometry::LINES );
479 drawTrace.Enable(true);
480 application.SendNotification();
481 application.Render(0);
482 application.Render();
483 application.SendNotification();
484 drawTrace.Enable( false );
486 // geometry type is set as GL_LINES
487 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
489 out << GL_LINES << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
490 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
492 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::LINES, TEST_LOCATION);
494 /*****************************************************/
496 geometry.SetGeometryType( Geometry::POINTS );
499 drawTrace.Enable(true);
500 application.SendNotification();
501 application.Render(0);
502 application.Render();
503 application.SendNotification();
504 drawTrace.Enable( false );
506 // geometry type is set as GL_POINTS
507 // As Points does not use the index buffer, call glDrawArrays,
508 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawArrays" ), 2, TEST_LOCATION);
510 out << GL_POINTS << ", " << 0 << ", " << numVertex;
511 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawArrays", out.str()), true, TEST_LOCATION);
513 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::POINTS, TEST_LOCATION);
515 /*****************************************************/
517 geometry.SetGeometryType( Geometry::TRIANGLE_STRIP );
520 drawTrace.Enable(true);
521 application.SendNotification();
522 application.Render(0);
523 application.Render();
524 application.SendNotification();
525 drawTrace.Enable( false );
527 // geometry type is set as GL_TRIANGLE_STRIP
528 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
530 out << GL_TRIANGLE_STRIP << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
531 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
533 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::TRIANGLE_STRIP, TEST_LOCATION);
535 /*****************************************************/
537 geometry.SetGeometryType( Geometry::TRIANGLE_FAN );
540 drawTrace.Enable(true);
541 application.SendNotification();
542 application.Render(0);
543 application.Render();
544 application.SendNotification();
545 drawTrace.Enable( false );
547 // geometry type is set as GL_TRIANGLE_FAN
548 DALI_TEST_EQUALS( drawTrace.CountMethod( "DrawElements" ), 2, TEST_LOCATION);
550 out << GL_TRIANGLE_FAN << ", " << numIndex << ", " << GL_UNSIGNED_SHORT<<", "<<"indices";
551 DALI_TEST_EQUALS( drawTrace.TestMethodAndParams(1, "DrawElements", out.str()), true, TEST_LOCATION);
553 DALI_TEST_EQUALS( geometry.GetGeometryType(), Geometry::TRIANGLE_FAN, TEST_LOCATION);