Merge remote-tracking branch 'origin/tizen' into devel/new_mesh
[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 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( PropertyBuffer::STATIC,
44                                                        texturedQuadVertexFormat, 4 );
45
46   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
47   END_TEST;
48 }
49
50 int UtcDaliPropertyBufferNew02(void)
51 {
52   TestApplication application;
53   PropertyBuffer propertyBuffer;
54   DALI_TEST_EQUALS( (bool)propertyBuffer, false, TEST_LOCATION );
55   END_TEST;
56 }
57
58 int UtcDaliPropertyBufferDownCast01(void)
59 {
60   TestApplication application;
61
62   Property::Map texturedQuadVertexFormat;
63   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
64   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
65
66   PropertyBuffer propertyBuffer = PropertyBuffer::New( PropertyBuffer::STATIC,
67                                                        texturedQuadVertexFormat, 4 );
68
69   BaseHandle handle(propertyBuffer);
70   PropertyBuffer propertyBuffer2 = PropertyBuffer::DownCast(handle);
71   DALI_TEST_EQUALS( (bool)propertyBuffer2, true, TEST_LOCATION );
72   END_TEST;
73 }
74
75 int UtcDaliPropertyBufferDownCast02(void)
76 {
77   TestApplication application;
78
79   Handle handle = Handle::New(); // Create a custom object
80   PropertyBuffer propertyBuffer = PropertyBuffer::DownCast(handle);
81   DALI_TEST_EQUALS( (bool)propertyBuffer, false, TEST_LOCATION );
82   END_TEST;
83 }
84
85 int UtcDaliPropertyBufferSetData01(void)
86 {
87   TestApplication application;
88
89   Property::Map texturedQuadVertexFormat;
90   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
91   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
92
93   PropertyBuffer propertyBuffer = PropertyBuffer::New( PropertyBuffer::STATIC,
94                                                        texturedQuadVertexFormat, 4 );
95   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
96
97   const float halfQuadSize = .5f;
98   struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
99   TexturedQuadVertex texturedQuadVertexData[4] = {
100     { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
101     { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
102     { Vector2(-halfQuadSize,  halfQuadSize), Vector2(0.f, 1.f) },
103     { Vector2( halfQuadSize,  halfQuadSize), Vector2(1.f, 1.f) } };
104
105   propertyBuffer.SetData( texturedQuadVertexData );
106
107   Geometry geometry = Geometry::New();
108   geometry.AddVertexBuffer( propertyBuffer );
109
110   Material material = CreateMaterial(1.f);
111   Renderer renderer = Renderer::New(geometry, material);
112   Actor actor = Actor::New();
113   actor.SetSize(Vector3::ONE * 100.f);
114   actor.AddRenderer(renderer);
115   Stage::GetCurrent().Add(actor);
116
117   application.SendNotification();
118   application.Render(0);
119   application.Render();
120   application.SendNotification();
121
122   const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
123       application.GetGlAbstraction().GetBufferDataCalls();
124
125   DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
126
127   DALI_TEST_EQUALS( bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
128
129   END_TEST;
130 }
131
132 int UtcDaliPropertyBufferSetData02(void)
133 {
134   TestApplication application;
135
136   Property::Map texturedQuadVertexFormat;
137   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
138   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
139
140   PropertyBuffer propertyBuffer = PropertyBuffer::New( PropertyBuffer::STATIC,
141                                                        texturedQuadVertexFormat, 4 );
142   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
143
144   const float halfQuadSize = .5f;
145   struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
146   TexturedQuadVertex texturedQuadVertexData[4] = {
147     { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
148     { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
149     { Vector2(-halfQuadSize,  halfQuadSize), Vector2(0.f, 1.f) },
150     { Vector2( halfQuadSize,  halfQuadSize), Vector2(1.f, 1.f) } };
151
152   propertyBuffer.SetData( texturedQuadVertexData );
153
154   Geometry geometry = Geometry::New();
155   geometry.AddVertexBuffer( propertyBuffer );
156
157   Material material = CreateMaterial(1.f);
158   Renderer renderer = Renderer::New(geometry, material);
159   Actor actor = Actor::New();
160   actor.SetSize(Vector3::ONE * 100.f);
161   actor.AddRenderer(renderer);
162   Stage::GetCurrent().Add(actor);
163
164   application.SendNotification();
165   application.Render(0);
166   application.Render();
167   application.SendNotification();
168
169   {
170     const TestGlAbstraction::BufferDataCalls& bufferDataCalls =
171       application.GetGlAbstraction().GetBufferDataCalls();
172
173     DALI_TEST_EQUALS( bufferDataCalls.size(), 1u, TEST_LOCATION );
174
175     DALI_TEST_EQUALS( bufferDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
176   }
177
178   // Re-upload the data on the propertyBuffer
179   propertyBuffer.SetData( texturedQuadVertexData );
180
181   application.SendNotification();
182   application.Render(0);
183   application.Render();
184   application.SendNotification();
185
186   {
187     const TestGlAbstraction::BufferSubDataCalls& bufferSubDataCalls =
188       application.GetGlAbstraction().GetBufferSubDataCalls();
189
190     DALI_TEST_EQUALS( bufferSubDataCalls.size(), 1u, TEST_LOCATION );
191
192     if ( bufferSubDataCalls.size() )
193     {
194       DALI_TEST_EQUALS( bufferSubDataCalls[0], sizeof(texturedQuadVertexData), TEST_LOCATION );
195     }
196   }
197
198   END_TEST;
199 }
200
201 int UtcDaliPropertyBufferSetSize01(void)
202 {
203   TestApplication application;
204
205   Property::Map texturedQuadVertexFormat;
206   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
207   texturedQuadVertexFormat["aVertexCoord"] = Property::VECTOR2;
208
209   PropertyBuffer propertyBuffer = PropertyBuffer::New( PropertyBuffer::STATIC,
210                                                        texturedQuadVertexFormat,
211                                                        4u );
212   DALI_TEST_EQUALS( (bool)propertyBuffer, true, TEST_LOCATION );
213
214   size_t size = propertyBuffer.GetSize();
215   DALI_TEST_EQUALS( size, 4u, TEST_LOCATION );
216
217   propertyBuffer.SetSize( 10u );
218   size = propertyBuffer.GetSize();
219   DALI_TEST_EQUALS( size, 10u, TEST_LOCATION );
220
221   END_TEST;
222 }
223