Formatting automated-tests
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-VertexBuffer.cpp
1 /*
2  * Copyright (c) 2020 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
18 #include <dali-test-suite-utils.h>
19 #include <dali/public-api/dali-core.h>
20
21 using namespace Dali;
22
23 #include <mesh-builder.h>
24
25 void vertexBuffer_test_startup(void)
26 {
27   test_return_value = TET_UNDEF;
28 }
29
30 void vertexBuffer_test_cleanup(void)
31 {
32   test_return_value = TET_PASS;
33 }
34
35 int UtcDaliVertexBufferNew01(void)
36 {
37   TestApplication application;
38
39   Property::Map texturedQuadVertexFormat;
40   texturedQuadVertexFormat["aPosition"]    = Property::VECTOR2;
41   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
42
43   VertexBuffer vertexBuffer = VertexBuffer::New(texturedQuadVertexFormat);
44
45   DALI_TEST_EQUALS((bool)vertexBuffer, true, TEST_LOCATION);
46   END_TEST;
47 }
48
49 int UtcDaliVertexBufferNew02(void)
50 {
51   TestApplication application;
52   VertexBuffer    vertexBuffer;
53   DALI_TEST_EQUALS((bool)vertexBuffer, false, TEST_LOCATION);
54   END_TEST;
55 }
56
57 int UtcDaliVertexBufferDownCast01(void)
58 {
59   TestApplication application;
60
61   Property::Map texturedQuadVertexFormat;
62   texturedQuadVertexFormat["aPosition"]    = Property::VECTOR2;
63   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
64
65   VertexBuffer vertexBuffer = VertexBuffer::New(texturedQuadVertexFormat);
66
67   BaseHandle   handle(vertexBuffer);
68   VertexBuffer vertexBuffer2 = VertexBuffer::DownCast(handle);
69   DALI_TEST_EQUALS((bool)vertexBuffer2, true, TEST_LOCATION);
70   END_TEST;
71 }
72
73 int UtcDaliVertexBufferDownCast02(void)
74 {
75   TestApplication application;
76
77   Handle       handle       = Handle::New(); // Create a custom object
78   VertexBuffer vertexBuffer = VertexBuffer::DownCast(handle);
79   DALI_TEST_EQUALS((bool)vertexBuffer, false, TEST_LOCATION);
80   END_TEST;
81 }
82
83 int UtcDaliVertexBufferCopyConstructor(void)
84 {
85   TestApplication application;
86
87   VertexBuffer vertexBuffer = CreateVertexBuffer();
88
89   VertexBuffer vertexBufferCopy(vertexBuffer);
90
91   DALI_TEST_EQUALS((bool)vertexBufferCopy, true, TEST_LOCATION);
92   DALI_TEST_EQUALS(vertexBufferCopy.GetSize(), 0u, TEST_LOCATION);
93
94   END_TEST;
95 }
96
97 int UtcDaliVertexBufferAssignmentOperator(void)
98 {
99   TestApplication application;
100
101   VertexBuffer vertexBuffer = CreateVertexBuffer();
102
103   VertexBuffer vertexBuffer2;
104   DALI_TEST_EQUALS((bool)vertexBuffer2, false, TEST_LOCATION);
105
106   vertexBuffer2 = vertexBuffer;
107   DALI_TEST_EQUALS((bool)vertexBuffer2, true, TEST_LOCATION);
108   DALI_TEST_EQUALS(vertexBuffer2.GetSize(), 0u, TEST_LOCATION);
109
110   END_TEST;
111 }
112
113 int UtcDaliVertexBufferMoveConstructor(void)
114 {
115   TestApplication application;
116
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);
121
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);
127
128   END_TEST;
129 }
130
131 int UtcDaliVertexBufferMoveAssignment(void)
132 {
133   TestApplication application;
134
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);
139
140   VertexBuffer move;
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);
146
147   END_TEST;
148 }
149
150 int UtcDaliVertexBufferSetData01(void)
151 {
152   TestApplication application;
153
154   Property::Map texturedQuadVertexFormat;
155   texturedQuadVertexFormat["aPosition"]    = Property::VECTOR2;
156   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
157
158   {
159     VertexBuffer vertexBuffer = VertexBuffer::New(texturedQuadVertexFormat);
160     DALI_TEST_EQUALS((bool)vertexBuffer, true, TEST_LOCATION);
161
162     const float halfQuadSize = .5f;
163     struct TexturedQuadVertex
164     {
165       Vector2 position;
166       Vector2 textureCoordinates;
167     };
168     TexturedQuadVertex texturedQuadVertexData[4] = {
169       {Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f)},
170       {Vector2(halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f)},
171       {Vector2(-halfQuadSize, halfQuadSize), Vector2(0.f, 1.f)},
172       {Vector2(halfQuadSize, halfQuadSize), Vector2(1.f, 1.f)}};
173
174     vertexBuffer.SetData(texturedQuadVertexData, 4);
175
176     Geometry geometry = Geometry::New();
177     geometry.AddVertexBuffer(vertexBuffer);
178
179     Shader   shader   = CreateShader();
180     Renderer renderer = Renderer::New(geometry, shader);
181     Actor    actor    = Actor::New();
182     actor.SetProperty(Actor::Property::SIZE, Vector3::ONE * 100.f);
183     actor.AddRenderer(renderer);
184     application.GetScene().Add(actor);
185
186     application.SendNotification();
187     application.Render(0);
188     application.Render();
189     application.SendNotification();
190
191     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
192       application.GetGlAbstraction().GetBufferDataCalls();
193
194     DALI_TEST_EQUALS(bufferDataCalls.size(), 1u, TEST_LOCATION);
195
196     DALI_TEST_EQUALS(bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION);
197   }
198   // end of scope to let the buffer and geometry die; do another notification and render to get the deletion processed
199   application.SendNotification();
200   application.Render(0);
201
202   END_TEST;
203 }
204
205 int UtcDaliVertexBufferSetData02(void)
206 {
207   TestApplication application;
208
209   Property::Map texturedQuadVertexFormat;
210   texturedQuadVertexFormat["aPosition"]    = Property::VECTOR2;
211   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
212
213   VertexBuffer vertexBuffer = VertexBuffer::New(texturedQuadVertexFormat);
214   DALI_TEST_EQUALS((bool)vertexBuffer, true, TEST_LOCATION);
215
216   const float halfQuadSize = .5f;
217   struct TexturedQuadVertex
218   {
219     Vector2 position;
220     Vector2 textureCoordinates;
221   };
222   TexturedQuadVertex texturedQuadVertexData[4] = {
223     {Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f)},
224     {Vector2(halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f)},
225     {Vector2(-halfQuadSize, halfQuadSize), Vector2(0.f, 1.f)},
226     {Vector2(halfQuadSize, halfQuadSize), Vector2(1.f, 1.f)}};
227
228   vertexBuffer.SetData(texturedQuadVertexData, 4);
229
230   Geometry geometry = Geometry::New();
231   geometry.AddVertexBuffer(vertexBuffer);
232
233   Shader   shader   = CreateShader();
234   Renderer renderer = Renderer::New(geometry, shader);
235   Actor    actor    = Actor::New();
236   actor.SetProperty(Actor::Property::SIZE, Vector3::ONE * 100.f);
237   actor.AddRenderer(renderer);
238   application.GetScene().Add(actor);
239
240   application.SendNotification();
241   application.Render(0);
242   application.Render();
243   application.SendNotification();
244
245   {
246     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
247       application.GetGlAbstraction().GetBufferDataCalls();
248
249     DALI_TEST_EQUALS(bufferDataCalls.size(), 1u, TEST_LOCATION);
250
251     DALI_TEST_EQUALS(bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION);
252   }
253
254   // Re-upload the data on the vertexBuffer
255   vertexBuffer.SetData(texturedQuadVertexData, 4);
256
257   application.SendNotification();
258   application.Render(0);
259   application.Render();
260   application.SendNotification();
261
262   {
263     const TestGlAbstraction::BufferSubDataCalls& bufferSubDataCalls =
264       application.GetGlAbstraction().GetBufferSubDataCalls();
265
266     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
267       application.GetGlAbstraction().GetBufferDataCalls();
268
269     DALI_TEST_EQUALS(bufferSubDataCalls.size(), 1u, TEST_LOCATION);
270     DALI_TEST_EQUALS(bufferDataCalls.size(), 1u, TEST_LOCATION);
271
272     if(bufferSubDataCalls.size())
273     {
274       DALI_TEST_EQUALS(bufferSubDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION);
275     }
276   }
277
278   END_TEST;
279 }
280
281 int UtcDaliVertexBufferInvalidTypeN(void)
282 {
283   TestApplication application;
284
285   Property::Map texturedQuadVertexFormat;
286   texturedQuadVertexFormat["aPosition"]    = Property::MAP;
287   texturedQuadVertexFormat["aVertexCoord"] = Property::STRING;
288
289   try
290   {
291     VertexBuffer vertexBuffer = VertexBuffer::New(texturedQuadVertexFormat);
292     tet_result(TET_FAIL);
293   }
294   catch(Dali::DaliException& e)
295   {
296     DALI_TEST_ASSERT(e, "Property::Type not supported in VertexBuffer", TEST_LOCATION);
297   }
298   END_TEST;
299 }
300
301 int UtcDaliVertexBufferSetDataNegative(void)
302 {
303   TestApplication    application;
304   Dali::VertexBuffer instance;
305   try
306   {
307     void*         arg1(nullptr);
308     unsigned long arg2(0u);
309     instance.SetData(arg1, arg2);
310     DALI_TEST_CHECK(false); // Should not get here
311   }
312   catch(...)
313   {
314     DALI_TEST_CHECK(true); // We expect an assert
315   }
316   END_TEST;
317 }
318
319 int UtcDaliVertexBufferGetSizeNegative(void)
320 {
321   TestApplication    application;
322   Dali::VertexBuffer instance;
323   try
324   {
325     instance.GetSize();
326     DALI_TEST_CHECK(false); // Should not get here
327   }
328   catch(...)
329   {
330     DALI_TEST_CHECK(true); // We expect an assert
331   }
332   END_TEST;
333 }