Add devel api to support C++11 for loop: for ( auto i : container )
[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 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 UtcDaliPropertyBufferSetData01(void)
114 {
115   TestApplication application;
116
117   Property::Map texturedQuadVertexFormat;
118   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
119   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
120
121   {
122     PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat );
123     DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
124
125     const float halfQuadSize = .5f;
126     struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
127     TexturedQuadVertex texturedQuadVertexData[4] = {
128       { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
129       { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
130       { Vector2(-halfQuadSize,  halfQuadSize), Vector2(0.f, 1.f) },
131       { Vector2( halfQuadSize,  halfQuadSize), Vector2(1.f, 1.f) } };
132
133     propertyBuffer.SetData( texturedQuadVertexData, 4 );
134
135     Geometry geometry = Geometry::New();
136     geometry.AddVertexBuffer( propertyBuffer );
137
138     Shader shader = CreateShader();
139     Renderer renderer = Renderer::New(geometry, shader);
140     Actor actor = Actor::New();
141     actor.SetSize(Vector3::ONE * 100.f);
142     actor.AddRenderer(renderer);
143     Stage::GetCurrent().Add(actor);
144
145     application.SendNotification();
146     application.Render(0);
147     application.Render();
148     application.SendNotification();
149
150     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
151         application.GetGlAbstraction().GetBufferDataCalls();
152
153     DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
154
155     DALI_TEST_EQUALS( bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
156
157   }
158   // end of scope to let the buffer and geometry die; do another notification and render to get the deletion processed
159   application.SendNotification();
160   application.Render(0);
161
162   END_TEST;
163 }
164
165 int UtcDaliPropertyBufferSetData02(void)
166 {
167   TestApplication application;
168
169   Property::Map texturedQuadVertexFormat;
170   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
171   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
172
173   PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat );
174   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
175
176   const float halfQuadSize = .5f;
177   struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
178   TexturedQuadVertex texturedQuadVertexData[4] = {
179     { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
180     { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
181     { Vector2(-halfQuadSize,  halfQuadSize), Vector2(0.f, 1.f) },
182     { Vector2( halfQuadSize,  halfQuadSize), Vector2(1.f, 1.f) } };
183
184   propertyBuffer.SetData( texturedQuadVertexData, 4 );
185
186   Geometry geometry = Geometry::New();
187   geometry.AddVertexBuffer( propertyBuffer );
188
189   Shader shader = CreateShader();
190   Renderer renderer = Renderer::New(geometry, shader);
191   Actor actor = Actor::New();
192   actor.SetSize(Vector3::ONE * 100.f);
193   actor.AddRenderer(renderer);
194   Stage::GetCurrent().Add(actor);
195
196   application.SendNotification();
197   application.Render(0);
198   application.Render();
199   application.SendNotification();
200
201   {
202     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
203       application.GetGlAbstraction().GetBufferDataCalls();
204
205     DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
206
207     DALI_TEST_EQUALS( bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
208   }
209
210   // Re-upload the data on the propertyBuffer
211   propertyBuffer.SetData( texturedQuadVertexData, 4 );
212
213   application.SendNotification();
214   application.Render(0);
215   application.Render();
216   application.SendNotification();
217
218   {
219     const TestGlAbstraction::BufferSubDataCalls& bufferSubDataCalls =
220       application.GetGlAbstraction().GetBufferSubDataCalls();
221
222     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
223           application.GetGlAbstraction().GetBufferDataCalls();
224
225     DALI_TEST_EQUALS( bufferSubDataCalls.size(), 1u, TEST_LOCATION );
226     DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
227
228     if ( bufferSubDataCalls.size() )
229     {
230       DALI_TEST_EQUALS( bufferSubDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
231     }
232   }
233
234   END_TEST;
235 }
236
237 int UtcDaliPropertyBufferInvalidTypeN(void)
238 {
239   TestApplication application;
240
241   Property::Map texturedQuadVertexFormat;
242   texturedQuadVertexFormat["aPosition"] = Property::MAP;
243   texturedQuadVertexFormat["aVertexCoord"] = Property::STRING;
244
245   try
246   {
247     PropertyBuffer propertyBuffer = PropertyBuffer::New( texturedQuadVertexFormat );
248     tet_result(TET_FAIL);
249   }
250   catch ( Dali::DaliException& e )
251   {
252     DALI_TEST_ASSERT( e, "Property::Type not supported in PropertyBuffer", TEST_LOCATION );
253   }
254   END_TEST;
255 }
256