Merge "Add missing includes." into devel/master
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-PropertyBuffer.cpp
1 /*
2  * Copyright (c) 2016 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( texturedQuadVertexFormat );
52
53   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
54   END_TEST;
55 }
56
57 int UtcDaliPropertyBufferNew02(void)
58 {
59   TestApplication application;
60   PropertyBuffer propertyBuffer;
61   DALI_TEST_EQUALS( (bool)propertyBuffer, false, TEST_LOCATION );
62   END_TEST;
63 }
64
65 int UtcDaliPropertyBufferDownCast01(void)
66 {
67   TestApplication application;
68
69   Property::Map texturedQuadVertexFormat;
70   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
71   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
72
73   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat );
74
75   BaseHandle handle(propertyBuffer);
76   PropertyBuffer propertyBuffer2 = PropertyBuffer::DownCast(handle);
77   DALI_TEST_EQUALS( (bool)propertyBuffer2, true, TEST_LOCATION );
78   END_TEST;
79 }
80
81 int UtcDaliPropertyBufferDownCast02(void)
82 {
83   TestApplication application;
84
85   Handle handle = Handle::New(); // Create a custom object
86   PropertyBuffer propertyBuffer = PropertyBuffer::DownCast(handle);
87   DALI_TEST_EQUALS( (bool)propertyBuffer, false, TEST_LOCATION );
88   END_TEST;
89 }
90
91 int UtcDaliPropertyBufferCopyConstructor(void)
92 {
93   TestApplication application;
94
95   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
96
97   PropertyBuffer propertyBufferCopy(propertyBuffer);
98
99   DALI_TEST_EQUALS( (bool)propertyBufferCopy, true, TEST_LOCATION );
100   DALI_TEST_EQUALS( propertyBufferCopy.GetSize(), 0u, TEST_LOCATION );
101
102   END_TEST;
103 }
104
105 int UtcDaliPropertyBufferAssignmentOperator(void)
106 {
107   TestApplication application;
108
109   PropertyBuffer propertyBuffer = CreatePropertyBuffer();
110
111   PropertyBuffer propertyBuffer2;
112   DALI_TEST_EQUALS( (bool)propertyBuffer2, false, TEST_LOCATION );
113
114   propertyBuffer2 = propertyBuffer;
115   DALI_TEST_EQUALS( (bool)propertyBuffer2, true, TEST_LOCATION );
116   DALI_TEST_EQUALS( propertyBuffer2.GetSize(), 0u, TEST_LOCATION );
117
118   END_TEST;
119 }
120
121 int UtcDaliPropertyBufferSetData01(void)
122 {
123   TestApplication application;
124
125   Property::Map texturedQuadVertexFormat;
126   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
127   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
128
129   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat );
130   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
131
132   const float halfQuadSize = .5f;
133   struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
134   TexturedQuadVertex texturedQuadVertexData[4] = {
135     { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
136     { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
137     { Vector2(-halfQuadSize,  halfQuadSize), Vector2(0.f, 1.f) },
138     { Vector2( halfQuadSize,  halfQuadSize), Vector2(1.f, 1.f) } };
139
140   propertyBuffer.SetData( texturedQuadVertexData, 4 );
141
142   Geometry geometry = Geometry::New();
143   geometry.AddVertexBuffer( propertyBuffer );
144
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);
151
152   application.SendNotification();
153   application.Render(0);
154   application.Render();
155   application.SendNotification();
156
157   const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
158       application.GetGlAbstraction().GetBufferDataCalls();
159
160   DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
161
162   DALI_TEST_EQUALS( bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
163
164   END_TEST;
165 }
166
167 int UtcDaliPropertyBufferSetData02(void)
168 {
169   TestApplication application;
170
171   Property::Map texturedQuadVertexFormat;
172   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
173   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
174
175   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat );
176   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
177
178   const float halfQuadSize = .5f;
179   struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
180   TexturedQuadVertex texturedQuadVertexData[4] = {
181     { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
182     { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
183     { Vector2(-halfQuadSize,  halfQuadSize), Vector2(0.f, 1.f) },
184     { Vector2( halfQuadSize,  halfQuadSize), Vector2(1.f, 1.f) } };
185
186   propertyBuffer.SetData( texturedQuadVertexData, 4 );
187
188   Geometry geometry = Geometry::New();
189   geometry.AddVertexBuffer( propertyBuffer );
190
191   Shader shader = CreateShader();
192   Renderer renderer = Renderer::New(geometry, shader);
193   Actor actor = Actor::New();
194   actor.SetSize(Vector3::ONE * 100.f);
195   actor.AddRenderer(renderer);
196   Stage::GetCurrent().Add(actor);
197
198   application.SendNotification();
199   application.Render(0);
200   application.Render();
201   application.SendNotification();
202
203   {
204     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
205       application.GetGlAbstraction().GetBufferDataCalls();
206
207     DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
208
209     DALI_TEST_EQUALS( bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
210   }
211
212   // Re-upload the data on the propertyBuffer
213   propertyBuffer.SetData( texturedQuadVertexData, 4 );
214
215   application.SendNotification();
216   application.Render(0);
217   application.Render();
218   application.SendNotification();
219
220   {
221     const TestGlAbstraction::BufferSubDataCalls& bufferSubDataCalls =
222       application.GetGlAbstraction().GetBufferSubDataCalls();
223
224     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
225           application.GetGlAbstraction().GetBufferDataCalls();
226
227     DALI_TEST_EQUALS( bufferSubDataCalls.size(), 1u, TEST_LOCATION );
228     DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
229
230     if ( bufferSubDataCalls.size() )
231     {
232       DALI_TEST_EQUALS( bufferSubDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
233     }
234   }
235
236   END_TEST;
237 }
238
239 int UtcDaliPropertyBufferInvalidTypeN(void)
240 {
241   TestApplication application;
242
243   Property::Map texturedQuadVertexFormat;
244   texturedQuadVertexFormat["aPosition"] = Property::MAP;
245   texturedQuadVertexFormat["aVertexCoord"] = Property::STRING;
246
247   try
248   {
249     PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat );
250     tet_result(TET_FAIL);
251   }
252   catch ( Dali::DaliException& e )
253   {
254     DALI_TEST_ASSERT( e, "Property::Type not supported in PropertyBuffer", TEST_LOCATION );
255   }
256   END_TEST;
257 }
258