[Tizen] Add Integration API to Create public event type
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-PropertyBuffer.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/public-api/dali-core.h>
19 #include <dali-test-suite-utils.h>
20
21 using namespace Dali;
22
23 #include <mesh-builder.h>
24
25 void propertyBuffer_test_startup(void)
26 {
27   test_return_value = TET_UNDEF;
28 }
29
30 void propertyBuffer_test_cleanup(void)
31 {
32   test_return_value = TET_PASS;
33 }
34
35 int UtcDaliPropertyBufferNew01(void)
36 {
37   TestApplication application;
38
39   Property::Map texturedQuadVertexFormat;
40   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
41   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
42
43   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat );
44
45   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
46   END_TEST;
47 }
48
49 int UtcDaliPropertyBufferNew02(void)
50 {
51   TestApplication application;
52   PropertyBuffer propertyBuffer;
53   DALI_TEST_EQUALS( (bool)propertyBuffer, false, TEST_LOCATION );
54   END_TEST;
55 }
56
57 int UtcDaliPropertyBufferDownCast01(void)
58 {
59   TestApplication application;
60
61   Property::Map texturedQuadVertexFormat;
62   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
63   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
64
65   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat );
66
67   BaseHandle handle(propertyBuffer);
68   PropertyBuffer propertyBuffer2 = PropertyBuffer::DownCast(handle);
69   DALI_TEST_EQUALS( (bool)propertyBuffer2, true, TEST_LOCATION );
70   END_TEST;
71 }
72
73 int UtcDaliPropertyBufferDownCast02(void)
74 {
75   TestApplication application;
76
77   Handle handle = Handle::New(); // Create a custom object
78   PropertyBuffer propertyBuffer = PropertyBuffer::DownCast(handle);
79   DALI_TEST_EQUALS( (bool)propertyBuffer, false, TEST_LOCATION );
80   END_TEST;
81 }
82
83 int UtcDaliPropertyBufferCopyConstructor(void)
84 {
85   TestApplication application;
86
87   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
88
89   PropertyBuffer propertyBufferCopy(propertyBuffer);
90
91   DALI_TEST_EQUALS( (bool)propertyBufferCopy, true, TEST_LOCATION );
92   DALI_TEST_EQUALS( propertyBufferCopy.GetSize(), 0u, TEST_LOCATION );
93
94   END_TEST;
95 }
96
97 int UtcDaliPropertyBufferAssignmentOperator(void)
98 {
99   TestApplication application;
100
101   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
102
103   PropertyBuffer propertyBuffer2;
104   DALI_TEST_EQUALS( (bool)propertyBuffer2, false, TEST_LOCATION );
105
106   propertyBuffer2 = propertyBuffer;
107   DALI_TEST_EQUALS( (bool)propertyBuffer2, true, TEST_LOCATION );
108   DALI_TEST_EQUALS( propertyBuffer2.GetSize(), 0u, TEST_LOCATION );
109
110   END_TEST;
111 }
112
113 int UtcDaliPropertyBufferMoveConstructor(void)
114 {
115   TestApplication application;
116
117   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
118   DALI_TEST_CHECK( propertyBuffer );
119   DALI_TEST_EQUALS( 1, propertyBuffer.GetBaseObject().ReferenceCount(), TEST_LOCATION );
120   DALI_TEST_EQUALS( 0u, propertyBuffer.GetSize(), TEST_LOCATION );
121
122   PropertyBuffer move = std::move( propertyBuffer );
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( !propertyBuffer );
127
128   END_TEST;
129 }
130
131 int UtcDaliPropertyBufferMoveAssignment(void)
132 {
133   TestApplication application;
134
135   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
136   DALI_TEST_CHECK( propertyBuffer );
137   DALI_TEST_EQUALS( 1, propertyBuffer.GetBaseObject().ReferenceCount(), TEST_LOCATION );
138   DALI_TEST_EQUALS( 0u, propertyBuffer.GetSize(), TEST_LOCATION );
139
140   PropertyBuffer move;
141   move = std::move( propertyBuffer );
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( !propertyBuffer );
146
147   END_TEST;
148 }
149
150 int UtcDaliPropertyBufferSetData01(void)
151 {
152   TestApplication application;
153
154   Property::Map texturedQuadVertexFormat;
155   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
156   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
157
158   {
159     PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat );
160     DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
161
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) } };
169
170     propertyBuffer.SetData( texturedQuadVertexData, 4 );
171
172     Geometry geometry = Geometry::New();
173     geometry.AddVertexBuffer( propertyBuffer );
174
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);
181
182     application.SendNotification();
183     application.Render(0);
184     application.Render();
185     application.SendNotification();
186
187     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
188         application.GetGlAbstraction().GetBufferDataCalls();
189
190     DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
191
192     DALI_TEST_EQUALS( bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
193
194   }
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);
198
199   END_TEST;
200 }
201
202 int UtcDaliPropertyBufferSetData02(void)
203 {
204   TestApplication application;
205
206   Property::Map texturedQuadVertexFormat;
207   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
208   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
209
210   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat );
211   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
212
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) } };
220
221   propertyBuffer.SetData( texturedQuadVertexData, 4 );
222
223   Geometry geometry = Geometry::New();
224   geometry.AddVertexBuffer( propertyBuffer );
225
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);
232
233   application.SendNotification();
234   application.Render(0);
235   application.Render();
236   application.SendNotification();
237
238   {
239     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
240       application.GetGlAbstraction().GetBufferDataCalls();
241
242     DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
243
244     DALI_TEST_EQUALS( bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
245   }
246
247   // Re-upload the data on the propertyBuffer
248   propertyBuffer.SetData( texturedQuadVertexData, 4 );
249
250   application.SendNotification();
251   application.Render(0);
252   application.Render();
253   application.SendNotification();
254
255   {
256     const TestGlAbstraction::BufferSubDataCalls& bufferSubDataCalls =
257       application.GetGlAbstraction().GetBufferSubDataCalls();
258
259     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
260           application.GetGlAbstraction().GetBufferDataCalls();
261
262     DALI_TEST_EQUALS( bufferSubDataCalls.size(), 1u, TEST_LOCATION );
263     DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
264
265     if ( bufferSubDataCalls.size() )
266     {
267       DALI_TEST_EQUALS( bufferSubDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
268     }
269   }
270
271   END_TEST;
272 }
273
274 int UtcDaliPropertyBufferInvalidTypeN(void)
275 {
276   TestApplication application;
277
278   Property::Map texturedQuadVertexFormat;
279   texturedQuadVertexFormat["aPosition"] = Property::MAP;
280   texturedQuadVertexFormat["aVertexCoord"] = Property::STRING;
281
282   try
283   {
284     PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat );
285     tet_result(TET_FAIL);
286   }
287   catch ( Dali::DaliException& e )
288   {
289     DALI_TEST_ASSERT( e, "Property::Type not supported in PropertyBuffer", TEST_LOCATION );
290   }
291   END_TEST;
292 }
293
294
295 int UtcDaliPropertyBufferSetDataNegative(void)
296 {
297   TestApplication application;
298   Dali::PropertyBuffer instance;
299   try
300   {
301     void* arg1(nullptr);
302     unsigned long arg2(0u);
303     instance.SetData(arg1,arg2);
304     DALI_TEST_CHECK(false); // Should not get here
305   }
306   catch(...)
307   {
308     DALI_TEST_CHECK(true); // We expect an assert
309   }
310   END_TEST;
311 }
312
313 int UtcDaliPropertyBufferGetSizeNegative(void)
314 {
315   TestApplication application;
316   Dali::PropertyBuffer instance;
317   try
318   {
319     instance.GetSize();
320     DALI_TEST_CHECK(false); // Should not get here
321   }
322   catch(...)
323   {
324     DALI_TEST_CHECK(true); // We expect an assert
325   }
326   END_TEST;
327 }