Added test cases and fixed bugs
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-PropertyBuffer.cpp
1 /*
2  * Copyright (c) 2015 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 namespace
26 {
27 void TestConstraintNoBlue( Vector4& current, const PropertyInputContainer& inputs )
28 {
29   current.b = 0.0f;
30 }
31 }
32
33 void propertyBuffer_test_startup(void)
34 {
35   test_return_value = TET_UNDEF;
36 }
37
38 void propertyBuffer_test_cleanup(void)
39 {
40   test_return_value = TET_PASS;
41 }
42
43 int UtcDaliPropertyBufferNew01(void)
44 {
45   TestApplication application;
46
47   Property::Map texturedQuadVertexFormat;
48   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
49   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
50
51   PropertyBuffer propertyBuffer = PropertyBuffer::New( PropertyBuffer::STATIC,
52                                                        texturedQuadVertexFormat, 4 );
53
54   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
55   END_TEST;
56 }
57
58 int UtcDaliPropertyBufferNew02(void)
59 {
60   TestApplication application;
61   PropertyBuffer propertyBuffer;
62   DALI_TEST_EQUALS( (bool)propertyBuffer, false, TEST_LOCATION );
63   END_TEST;
64 }
65
66 int UtcDaliPropertyBufferDownCast01(void)
67 {
68   TestApplication application;
69
70   Property::Map texturedQuadVertexFormat;
71   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
72   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
73
74   PropertyBuffer propertyBuffer = PropertyBuffer::New( PropertyBuffer::STATIC,
75                                                        texturedQuadVertexFormat, 4 );
76
77   BaseHandle handle(propertyBuffer);
78   PropertyBuffer propertyBuffer2 = PropertyBuffer::DownCast(handle);
79   DALI_TEST_EQUALS( (bool)propertyBuffer2, true, TEST_LOCATION );
80   END_TEST;
81 }
82
83 int UtcDaliPropertyBufferDownCast02(void)
84 {
85   TestApplication application;
86
87   Handle handle = Handle::New(); // Create a custom object
88   PropertyBuffer propertyBuffer = PropertyBuffer::DownCast(handle);
89   DALI_TEST_EQUALS( (bool)propertyBuffer, false, TEST_LOCATION );
90   END_TEST;
91 }
92
93
94 int UtcDaliPropertyBufferConstraint01(void)
95 {
96   TestApplication application;
97
98   tet_infoline("Test that a non-uniform propertyBuffer property can be constrained");
99
100   Shader shader = Shader::New("VertexSource", "FragmentSource");
101   Material material = Material::New( shader );
102   material.SetProperty(Material::Property::COLOR, Color::WHITE);
103
104   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
105   Geometry geometry = CreateQuadGeometryFromBuffer(propertyBuffer);
106   Renderer renderer = Renderer::New( geometry, material );
107
108   Actor actor = Actor::New();
109   actor.AddRenderer(renderer);
110   actor.SetSize(400, 400);
111   Stage::GetCurrent().Add(actor);
112
113   Vector4 initialColor = Color::WHITE;
114   Property::Index colorIndex = propertyBuffer.RegisterProperty( "fade-color", initialColor );
115
116   application.SendNotification();
117   application.Render(0);
118   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
119
120   // Apply constraint
121   Constraint constraint = Constraint::New<Vector4>( propertyBuffer, colorIndex, TestConstraintNoBlue );
122   constraint.Apply();
123   application.SendNotification();
124   application.Render(0);
125
126   // Expect no blue component in either buffer - yellow
127   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
128   application.Render(0);
129   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
130
131   propertyBuffer.RemoveConstraints();
132   propertyBuffer.SetProperty(colorIndex, Color::WHITE );
133   application.SendNotification();
134   application.Render(0);
135   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), Color::WHITE, TEST_LOCATION );
136
137   END_TEST;
138 }
139
140 int UtcDaliPropertyBufferConstraint02(void)
141 {
142   TestApplication application;
143
144   tet_infoline("Test that a uniform map propertyBuffer property can be constrained");
145
146   Shader shader = Shader::New("VertexSource", "FragmentSource");
147   Material material = Material::New( shader );
148   material.SetProperty(Material::Property::COLOR, Color::WHITE);
149
150   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
151   Geometry geometry = CreateQuadGeometryFromBuffer(propertyBuffer);
152   Renderer renderer = Renderer::New( geometry, material );
153
154   Actor actor = Actor::New();
155   actor.AddRenderer(renderer);
156   actor.SetSize(400, 400);
157   Stage::GetCurrent().Add(actor);
158   application.SendNotification();
159   application.Render(0);
160
161   Vector4 initialColor = Color::WHITE;
162   Property::Index colorIndex = propertyBuffer.RegisterProperty( "fade-color", initialColor );
163   propertyBuffer.AddUniformMapping( colorIndex, std::string("uFadeColor") );
164
165   TestGlAbstraction& gl = application.GetGlAbstraction();
166
167   application.SendNotification();
168   application.Render(0);
169
170   Vector4 actualValue(Vector4::ZERO);
171   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
172   DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
173
174   // Apply constraint
175   Constraint constraint = Constraint::New<Vector4>( propertyBuffer, colorIndex, TestConstraintNoBlue );
176   constraint.Apply();
177   application.SendNotification();
178   application.Render(0);
179
180    // Expect no blue component in either buffer - yellow
181   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
182   DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
183
184   application.Render(0);
185   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
186   DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
187
188   propertyBuffer.RemoveConstraints();
189   propertyBuffer.SetProperty(colorIndex, Color::WHITE );
190   application.SendNotification();
191   application.Render(0);
192
193   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
194   DALI_TEST_EQUALS( actualValue, Color::WHITE, TEST_LOCATION );
195
196   END_TEST;
197 }
198
199
200
201 int UtcDaliPropertyBufferAnimatedProperty01(void)
202 {
203   TestApplication application;
204
205   tet_infoline("Test that a non-uniform propertyBuffer property can be animated");
206
207   Shader shader = Shader::New("VertexSource", "FragmentSource");
208   Material material = Material::New( shader );
209   material.SetProperty(Material::Property::COLOR, Color::WHITE);
210
211   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
212   Geometry geometry = CreateQuadGeometryFromBuffer(propertyBuffer);
213   Renderer renderer = Renderer::New( geometry, material );
214
215   Actor actor = Actor::New();
216   actor.AddRenderer(renderer);
217   actor.SetSize(400, 400);
218   Stage::GetCurrent().Add(actor);
219
220   Vector4 initialColor = Color::WHITE;
221   Property::Index colorIndex = propertyBuffer.RegisterProperty( "fade-color", initialColor );
222
223   application.SendNotification();
224   application.Render(0);
225   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
226
227   Animation  animation = Animation::New(1.0f);
228   KeyFrames keyFrames = KeyFrames::New();
229   keyFrames.Add(0.0f, initialColor);
230   keyFrames.Add(1.0f, Color::TRANSPARENT);
231   animation.AnimateBetween( Property( propertyBuffer, colorIndex ), keyFrames );
232   animation.Play();
233
234   application.SendNotification();
235   application.Render(500);
236
237   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), Color::WHITE * 0.5f, TEST_LOCATION );
238
239   application.Render(500);
240
241   DALI_TEST_EQUALS( propertyBuffer.GetProperty<Vector4>(colorIndex), Color::TRANSPARENT, TEST_LOCATION );
242
243   END_TEST;
244 }
245
246 int UtcDaliPropertyBufferAnimatedProperty02(void)
247 {
248   TestApplication application;
249
250   tet_infoline("Test that a uniform map propertyBuffer property can be animated");
251
252   Shader shader = Shader::New("VertexSource", "FragmentSource");
253   Material material = Material::New( shader );
254   material.SetProperty(Material::Property::COLOR, Color::WHITE);
255
256   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
257   Geometry geometry = CreateQuadGeometryFromBuffer(propertyBuffer);
258   Renderer renderer = Renderer::New( geometry, material );
259
260   Actor actor = Actor::New();
261   actor.AddRenderer(renderer);
262   actor.SetSize(400, 400);
263   Stage::GetCurrent().Add(actor);
264   application.SendNotification();
265   application.Render(0);
266
267   Vector4 initialColor = Color::WHITE;
268   Property::Index colorIndex = propertyBuffer.RegisterProperty( "fade-color", initialColor );
269   propertyBuffer.AddUniformMapping( colorIndex, std::string("uFadeColor") );
270
271   TestGlAbstraction& gl = application.GetGlAbstraction();
272
273   application.SendNotification();
274   application.Render(0);
275
276   Vector4 actualValue(Vector4::ZERO);
277   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
278   DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
279
280   Animation  animation = Animation::New(1.0f);
281   KeyFrames keyFrames = KeyFrames::New();
282   keyFrames.Add(0.0f, initialColor);
283   keyFrames.Add(1.0f, Color::TRANSPARENT);
284   animation.AnimateBetween( Property( propertyBuffer, colorIndex ), keyFrames );
285   animation.Play();
286
287   application.SendNotification();
288   application.Render(500);
289
290   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
291   DALI_TEST_EQUALS( actualValue, Color::WHITE * 0.5f, TEST_LOCATION );
292
293   application.Render(500);
294   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
295   DALI_TEST_EQUALS( actualValue, Color::TRANSPARENT, TEST_LOCATION );
296
297   END_TEST;
298 }
299
300 int UtcDaliPropertyBufferSetData01(void)
301 {
302   TestApplication application;
303
304   Property::Map texturedQuadVertexFormat;
305   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
306   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
307
308   PropertyBuffer propertyBuffer = PropertyBuffer::New( PropertyBuffer::STATIC,
309                                                        texturedQuadVertexFormat, 4 );
310   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
311
312   const float halfQuadSize = .5f;
313   struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
314   TexturedQuadVertex texturedQuadVertexData[4] = {
315     { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
316     { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
317     { Vector2(-halfQuadSize,  halfQuadSize), Vector2(0.f, 1.f) },
318     { Vector2( halfQuadSize,  halfQuadSize), Vector2(1.f, 1.f) } };
319
320   propertyBuffer.SetData( texturedQuadVertexData );
321
322   Geometry geometry = Geometry::New();
323   geometry.AddVertexBuffer( propertyBuffer );
324
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);
331
332   application.SendNotification();
333   application.Render(0);
334   application.Render();
335   application.SendNotification();
336
337   const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
338       application.GetGlAbstraction().GetBufferDataCalls();
339
340   DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
341
342   DALI_TEST_EQUALS( bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
343
344   END_TEST;
345 }
346
347 int UtcDaliPropertyBufferSetData02(void)
348 {
349   TestApplication application;
350
351   Property::Map texturedQuadVertexFormat;
352   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
353   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
354
355   PropertyBuffer propertyBuffer = PropertyBuffer::New( PropertyBuffer::STATIC,
356                                                        texturedQuadVertexFormat, 4 );
357   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
358
359   const float halfQuadSize = .5f;
360   struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
361   TexturedQuadVertex texturedQuadVertexData[4] = {
362     { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
363     { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
364     { Vector2(-halfQuadSize,  halfQuadSize), Vector2(0.f, 1.f) },
365     { Vector2( halfQuadSize,  halfQuadSize), Vector2(1.f, 1.f) } };
366
367   propertyBuffer.SetData( texturedQuadVertexData );
368
369   Geometry geometry = Geometry::New();
370   geometry.AddVertexBuffer( propertyBuffer );
371
372   Material material = CreateMaterial(1.f);
373   Renderer renderer = Renderer::New(geometry, material);
374   Actor actor = Actor::New();
375   actor.SetSize(Vector3::ONE * 100.f);
376   actor.AddRenderer(renderer);
377   Stage::GetCurrent().Add(actor);
378
379   application.SendNotification();
380   application.Render(0);
381   application.Render();
382   application.SendNotification();
383
384   {
385     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
386       application.GetGlAbstraction().GetBufferDataCalls();
387
388     DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
389
390     DALI_TEST_EQUALS( bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
391   }
392
393   // Re-upload the data on the propertyBuffer
394   propertyBuffer.SetData( texturedQuadVertexData );
395
396   application.SendNotification();
397   application.Render(0);
398   application.Render();
399   application.SendNotification();
400
401   {
402     const TestGlAbstraction::BufferSubDataCalls& bufferSubDataCalls =
403       application.GetGlAbstraction().GetBufferSubDataCalls();
404
405     DALI_TEST_EQUALS( bufferSubDataCalls.size(), 1u, TEST_LOCATION );
406
407     if ( bufferSubDataCalls.size() )
408     {
409       DALI_TEST_EQUALS( bufferSubDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
410     }
411   }
412
413   END_TEST;
414 }
415
416 int UtcDaliPropertyBufferSetSize01(void)
417 {
418   TestApplication application;
419
420   Property::Map texturedQuadVertexFormat;
421   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
422   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
423
424   PropertyBuffer propertyBuffer = PropertyBuffer::New( PropertyBuffer::STATIC,
425                                                        texturedQuadVertexFormat,
426                                                        4u );
427   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
428
429   size_t size = propertyBuffer.GetSize();
430   DALI_TEST_EQUALS( size, 4u, TEST_LOCATION );
431
432   propertyBuffer.SetSize( 10u );
433   size = propertyBuffer.GetSize();
434   DALI_TEST_EQUALS( size, 10u, TEST_LOCATION );
435
436   END_TEST;
437 }
438