2 * Copyright (c) 2020 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 vertexBuffer_test_startup(void)
27 test_return_value = TET_UNDEF;
30 void vertexBuffer_test_cleanup(void)
32 test_return_value = TET_PASS;
35 int UtcDaliVertexBufferNew01(void)
37 TestApplication application;
39 Property::Map texturedQuadVertexFormat;
40 texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
41 texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
43 VertexBuffer vertexBuffer = VertexBuffer::New( texturedQuadVertexFormat );
45 DALI_TEST_EQUALS( (bool)vertexBuffer, true, TEST_LOCATION );
49 int UtcDaliVertexBufferNew02(void)
51 TestApplication application;
52 VertexBuffer vertexBuffer;
53 DALI_TEST_EQUALS( (bool)vertexBuffer, false, TEST_LOCATION );
57 int UtcDaliVertexBufferDownCast01(void)
59 TestApplication application;
61 Property::Map texturedQuadVertexFormat;
62 texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
63 texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
65 VertexBuffer vertexBuffer = VertexBuffer::New( texturedQuadVertexFormat );
67 BaseHandle handle(vertexBuffer);
68 VertexBuffer vertexBuffer2 = VertexBuffer::DownCast(handle);
69 DALI_TEST_EQUALS( (bool)vertexBuffer2, true, TEST_LOCATION );
73 int UtcDaliVertexBufferDownCast02(void)
75 TestApplication application;
77 Handle handle = Handle::New(); // Create a custom object
78 VertexBuffer vertexBuffer = VertexBuffer::DownCast(handle);
79 DALI_TEST_EQUALS( (bool)vertexBuffer, false, TEST_LOCATION );
83 int UtcDaliVertexBufferCopyConstructor(void)
85 TestApplication application;
87 VertexBuffer vertexBuffer = CreateVertexBuffer();
89 VertexBuffer vertexBufferCopy(vertexBuffer);
91 DALI_TEST_EQUALS( (bool)vertexBufferCopy, true, TEST_LOCATION );
92 DALI_TEST_EQUALS( vertexBufferCopy.GetSize(), 0u, TEST_LOCATION );
97 int UtcDaliVertexBufferAssignmentOperator(void)
99 TestApplication application;
101 VertexBuffer vertexBuffer = CreateVertexBuffer();
103 VertexBuffer vertexBuffer2;
104 DALI_TEST_EQUALS( (bool)vertexBuffer2, false, TEST_LOCATION );
106 vertexBuffer2 = vertexBuffer;
107 DALI_TEST_EQUALS( (bool)vertexBuffer2, true, TEST_LOCATION );
108 DALI_TEST_EQUALS( vertexBuffer2.GetSize(), 0u, TEST_LOCATION );
113 int UtcDaliVertexBufferMoveConstructor(void)
115 TestApplication application;
117 VertexBuffer vertexBuffer = CreateVertexBuffer();
118 DALI_TEST_CHECK( vertexBuffer );
119 DALI_TEST_EQUALS( 1, vertexBuffer.GetBaseObject().ReferenceCount(), TEST_LOCATION );
120 DALI_TEST_EQUALS( 0u, vertexBuffer.GetSize(), TEST_LOCATION );
122 VertexBuffer move = std::move( vertexBuffer );
123 DALI_TEST_CHECK( move );
124 DALI_TEST_EQUALS( 1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION );
125 DALI_TEST_EQUALS( 0u, move.GetSize(), TEST_LOCATION );
126 DALI_TEST_CHECK( !vertexBuffer );
131 int UtcDaliVertexBufferMoveAssignment(void)
133 TestApplication application;
135 VertexBuffer vertexBuffer = CreateVertexBuffer();
136 DALI_TEST_CHECK( vertexBuffer );
137 DALI_TEST_EQUALS( 1, vertexBuffer.GetBaseObject().ReferenceCount(), TEST_LOCATION );
138 DALI_TEST_EQUALS( 0u, vertexBuffer.GetSize(), TEST_LOCATION );
141 move = std::move( vertexBuffer );
142 DALI_TEST_CHECK( move );
143 DALI_TEST_EQUALS( 1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION );
144 DALI_TEST_EQUALS( 0u, move.GetSize(), TEST_LOCATION );
145 DALI_TEST_CHECK( !vertexBuffer );
150 int UtcDaliVertexBufferSetData01(void)
152 TestApplication application;
154 Property::Map texturedQuadVertexFormat;
155 texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
156 texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
159 VertexBuffer vertexBuffer = VertexBuffer::New( texturedQuadVertexFormat );
160 DALI_TEST_EQUALS( (bool)vertexBuffer, true, TEST_LOCATION );
162 const float halfQuadSize = .5f;
163 struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
164 TexturedQuadVertex texturedQuadVertexData[4] = {
165 { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
166 { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
167 { Vector2(-halfQuadSize, halfQuadSize), Vector2(0.f, 1.f) },
168 { Vector2( halfQuadSize, halfQuadSize), Vector2(1.f, 1.f) } };
170 vertexBuffer.SetData( texturedQuadVertexData, 4 );
172 Geometry geometry = Geometry::New();
173 geometry.AddVertexBuffer( vertexBuffer );
175 Shader shader = CreateShader();
176 Renderer renderer = Renderer::New(geometry, shader);
177 Actor actor = Actor::New();
178 actor.SetProperty( Actor::Property::SIZE,Vector3::ONE * 100.f);
179 actor.AddRenderer(renderer);
180 application.GetScene().Add(actor);
182 application.SendNotification();
183 application.Render(0);
184 application.Render();
185 application.SendNotification();
187 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
188 application.GetGlAbstraction().GetBufferDataCalls();
190 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
192 DALI_TEST_EQUALS( bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
195 // end of scope to let the buffer and geometry die; do another notification and render to get the deletion processed
196 application.SendNotification();
197 application.Render(0);
202 int UtcDaliVertexBufferSetData02(void)
204 TestApplication application;
206 Property::Map texturedQuadVertexFormat;
207 texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
208 texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
210 VertexBuffer vertexBuffer = VertexBuffer::New( texturedQuadVertexFormat );
211 DALI_TEST_EQUALS( (bool)vertexBuffer, true, TEST_LOCATION );
213 const float halfQuadSize = .5f;
214 struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
215 TexturedQuadVertex texturedQuadVertexData[4] = {
216 { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
217 { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
218 { Vector2(-halfQuadSize, halfQuadSize), Vector2(0.f, 1.f) },
219 { Vector2( halfQuadSize, halfQuadSize), Vector2(1.f, 1.f) } };
221 vertexBuffer.SetData( texturedQuadVertexData, 4 );
223 Geometry geometry = Geometry::New();
224 geometry.AddVertexBuffer( vertexBuffer );
226 Shader shader = CreateShader();
227 Renderer renderer = Renderer::New(geometry, shader);
228 Actor actor = Actor::New();
229 actor.SetProperty( Actor::Property::SIZE,Vector3::ONE * 100.f);
230 actor.AddRenderer(renderer);
231 application.GetScene().Add(actor);
233 application.SendNotification();
234 application.Render(0);
235 application.Render();
236 application.SendNotification();
239 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
240 application.GetGlAbstraction().GetBufferDataCalls();
242 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
244 DALI_TEST_EQUALS( bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
247 // Re-upload the data on the vertexBuffer
248 vertexBuffer.SetData( texturedQuadVertexData, 4 );
250 application.SendNotification();
251 application.Render(0);
252 application.Render();
253 application.SendNotification();
256 const TestGlAbstraction::BufferSubDataCalls& bufferSubDataCalls =
257 application.GetGlAbstraction().GetBufferSubDataCalls();
259 const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
260 application.GetGlAbstraction().GetBufferDataCalls();
262 DALI_TEST_EQUALS( bufferSubDataCalls.size(), 1u, TEST_LOCATION );
263 DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
265 if ( bufferSubDataCalls.size() )
267 DALI_TEST_EQUALS( bufferSubDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
274 int UtcDaliVertexBufferInvalidTypeN(void)
276 TestApplication application;
278 Property::Map texturedQuadVertexFormat;
279 texturedQuadVertexFormat["aPosition"] = Property::MAP;
280 texturedQuadVertexFormat["aVertexCoord"] = Property::STRING;
284 VertexBuffer vertexBuffer = VertexBuffer::New( texturedQuadVertexFormat );
285 tet_result(TET_FAIL);
287 catch ( Dali::DaliException& e )
289 DALI_TEST_ASSERT( e, "Property::Type not supported in VertexBuffer", TEST_LOCATION );
295 int UtcDaliVertexBufferSetDataNegative(void)
297 TestApplication application;
298 Dali::VertexBuffer instance;
302 unsigned long arg2(0u);
303 instance.SetData(arg1,arg2);
304 DALI_TEST_CHECK(false); // Should not get here
308 DALI_TEST_CHECK(true); // We expect an assert
313 int UtcDaliVertexBufferGetSizeNegative(void)
315 TestApplication application;
316 Dali::VertexBuffer instance;
320 DALI_TEST_CHECK(false); // Should not get here
324 DALI_TEST_CHECK(true); // We expect an assert