Revert "License conversion from Flora to Apache 2.0"
[platform/core/uifw/dali-toolkit.git] / automated-tests / TET / dali-test-suite / shader-effects / utc-Dali-WaterEffect.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 #include <iostream>
18
19 #include <stdlib.h>
20 #include <tet_api.h>
21
22 #include <dali/public-api/dali-core.h>
23 #include <dali-toolkit/dali-toolkit.h>
24
25 #include <dali-toolkit-test-suite-utils.h>
26
27 using namespace Dali;
28
29 static void Startup();
30 static void Cleanup();
31
32 extern "C" {
33   void (*tet_startup)() = Startup;
34   void (*tet_cleanup)() = Cleanup;
35 }
36
37 enum {
38   POSITIVE_TC_IDX = 0x01,
39   NEGATIVE_TC_IDX,
40 };
41
42 #define MAX_NUMBER_OF_TESTS 10000
43 extern "C" {
44   struct tet_testlist tet_testlist[MAX_NUMBER_OF_TESTS];
45 }
46
47 // Add test functionality for all APIs in the class (Positive and Negative)
48 TEST_FUNCTION( UtcDaliWaterEffectUninitialized, NEGATIVE_TC_IDX );
49 TEST_FUNCTION( UtcDaliWaterEffectPropertyNames, POSITIVE_TC_IDX );
50 TEST_FUNCTION( UtcDaliWaterEffectOutOfBounds, NEGATIVE_TC_IDX );
51 TEST_FUNCTION( UtcDaliWaterEffectDefaultValues, POSITIVE_TC_IDX );
52 TEST_FUNCTION( UtcDaliWaterEffectCustomValues, POSITIVE_TC_IDX );
53 TEST_FUNCTION( UtcDaliWaterEffectGetAmplitudePositive, POSITIVE_TC_IDX );
54 TEST_FUNCTION( UtcDaliWaterEffectGetAmplitudeNegative, NEGATIVE_TC_IDX );
55 TEST_FUNCTION( UtcDaliWaterEffectGetCenterPositive, POSITIVE_TC_IDX );
56 TEST_FUNCTION( UtcDaliWaterEffectGetCenterNegative, NEGATIVE_TC_IDX );
57 TEST_FUNCTION( UtcDaliWaterEffectGetPropagationPositive, POSITIVE_TC_IDX );
58 TEST_FUNCTION( UtcDaliWaterEffectGetPropagationNegative, NEGATIVE_TC_IDX );
59
60 // Called only once before first test is run.
61 static void Startup()
62 {
63 }
64
65 // Called only once after last test is run
66 static void Cleanup()
67 {
68 }
69
70 // Create bitmap image
71 BitmapImage CreateBitmapImage()
72 {
73   BitmapImage image = BitmapImage::New(4,4,Pixel::RGBA8888);
74
75   PixelBuffer* pixbuf = image.GetBuffer();
76
77   // Using a 4x4 image gives a better blend with the GL implementation
78   // than a 3x3 image
79   for(size_t i=0; i<16; i++)
80   {
81     pixbuf[i*4+0] = 0xFF;
82     pixbuf[i*4+1] = 0xFF;
83     pixbuf[i*4+2] = 0xFF;
84     pixbuf[i*4+3] = 0xFF;
85   }
86
87   return image;
88 }
89
90 static void UtcDaliWaterEffectUninitialized()
91 {
92   ToolkitTestApplication application;
93
94   Toolkit::WaterEffect effect;
95
96   try
97   {
98     // New() must be called to create a RippleEffect or it wont be valid.
99     effect.SetAmplitude( 0, 0.5f );
100     DALI_TEST_CHECK( false );
101   }
102   catch (Dali::DaliException& e)
103   {
104     // Tests that a negative test of an assertion succeeds
105     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
106     DALI_TEST_CHECK(!effect);
107   }
108 }
109
110 static void UtcDaliWaterEffectPropertyNames()
111 {
112   ToolkitTestApplication application;
113
114   Toolkit::WaterEffect effect = Toolkit::WaterEffect::New(4);
115   DALI_TEST_CHECK( effect );
116
117   // Check the names, this names are used in the shaders code,
118   // if they change the shader code has to be updated
119   DALI_TEST_EQUALS( effect.GetAmplitudePropertyName( 0 ), "uDrops[0].amplitude", TEST_LOCATION );
120   DALI_TEST_EQUALS( effect.GetCenterPropertyName( 0 ), "uDrops[0].center", TEST_LOCATION );
121   DALI_TEST_EQUALS( effect.GetPropagationPropertyName( 0 ), "uDrops[0].radius", TEST_LOCATION );
122 }
123
124 static void UtcDaliWaterEffectOutOfBounds()
125 {
126   ToolkitTestApplication application;
127
128   Toolkit::WaterEffect effect = Toolkit::WaterEffect::New(4);
129   DALI_TEST_CHECK( effect );
130
131   try
132   {
133     // the highest index acceptable is (GetNumberOfWaves() - 1)
134     effect.SetAmplitude( effect.GetNumberOfWaves(), 0 );
135     DALI_TEST_CHECK( false );
136   }
137   catch (Dali::DaliException& e)
138   {
139     // Tests that a negative test of an assertion succeeds
140     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
141     DALI_TEST_CHECK( true );
142   }
143 }
144
145 static void UtcDaliWaterEffectDefaultValues()
146 {
147   ToolkitTestApplication application;
148
149   Toolkit::WaterEffect effect = Toolkit::WaterEffect::New(4);
150   DALI_TEST_CHECK( effect );
151
152   // Check that the effect has the number of waves it was requested
153   DALI_TEST_CHECK( effect.GetNumberOfWaves() == 4 );
154
155   BitmapImage image = CreateBitmapImage();
156
157   ImageActor actor = ImageActor::New( image );
158   actor.SetSize( 100.0f, 100.0f );
159   actor.SetShaderEffect( effect );
160   Stage::GetCurrent().Add( actor );
161
162   application.SendNotification();
163   application.Render();
164
165   Vector2 topLeft( Stage::GetCurrent().GetSize() * 0.5f );
166   topLeft.y = -topLeft.y;
167
168   for ( unsigned int i = 0; i < effect.GetNumberOfWaves(); ++i )
169   {
170     DALI_TEST_CHECK(
171         application.GetGlAbstraction().CheckUniformValue(
172             effect.GetAmplitudePropertyName(i).c_str(),
173             0.0f ) );
174     DALI_TEST_CHECK(
175         application.GetGlAbstraction().CheckUniformValue(
176             effect.GetCenterPropertyName(i).c_str(),
177             topLeft ) );
178     DALI_TEST_CHECK(
179         application.GetGlAbstraction().CheckUniformValue(
180             effect.GetPropagationPropertyName(i).c_str(),
181             0.0f ) );
182   }
183 }
184
185 static void UtcDaliWaterEffectCustomValues()
186 {
187   ToolkitTestApplication application;
188
189   Toolkit::WaterEffect effect = Toolkit::WaterEffect::New(4);
190   DALI_TEST_CHECK( effect );
191
192   BitmapImage image = CreateBitmapImage();
193
194   ImageActor actor = ImageActor::New( image );
195   actor.SetSize( 100.0f, 100.0f );
196   actor.SetShaderEffect( effect );
197   Stage::GetCurrent().Add( actor );
198
199   effect.SetAmplitude( 0, 0.5f );
200   effect.SetCenter( 0, Vector2 ( 10.0f, 10.0f ) );
201   effect.SetPropagation( 0, 2.0f );
202
203   application.SendNotification();
204   application.Render();
205
206   DALI_TEST_CHECK(
207       application.GetGlAbstraction().CheckUniformValue(
208           effect.GetAmplitudePropertyName(0).c_str(),
209           0.5f ) );
210
211   Vector2 centerPoint( Stage::GetCurrent().GetSize() * 0.5f );
212   centerPoint.y = -centerPoint.y;
213
214   DALI_TEST_CHECK(
215       application.GetGlAbstraction().CheckUniformValue(
216           effect.GetCenterPropertyName(0).c_str(),
217           Vector2( centerPoint.x - 10.0f, centerPoint.y + 10.0f ) ) );
218   DALI_TEST_CHECK(
219       application.GetGlAbstraction().CheckUniformValue(
220           effect.GetPropagationPropertyName(0).c_str(),
221           2.0f ) );
222 }
223
224 static void UtcDaliWaterEffectGetAmplitudePositive()
225 {
226   ToolkitTestApplication application;
227
228   Toolkit::WaterEffect effect = Toolkit::WaterEffect::New(4);
229   DALI_TEST_CHECK( effect );
230
231   BitmapImage image = CreateBitmapImage();
232
233   ImageActor actor = ImageActor::New( image );
234   actor.SetSize( 100.0f, 100.0f );
235   actor.SetShaderEffect( effect );
236   Stage::GetCurrent().Add( actor );
237
238   float amplitude(0.5f);
239   DALI_TEST_CHECK(effect.GetAmplitude(0) != amplitude);
240   effect.SetAmplitude( 0, amplitude );
241
242   application.SendNotification();
243   application.Render();
244
245   DALI_TEST_EQUALS(amplitude, effect.GetAmplitude(0), TEST_LOCATION);
246 }
247
248 static void UtcDaliWaterEffectGetAmplitudeNegative()
249 {
250   ToolkitTestApplication application;
251
252   Toolkit::WaterEffect effect = Toolkit::WaterEffect::New(4);
253   DALI_TEST_CHECK( effect );
254
255   BitmapImage image = CreateBitmapImage();
256
257   ImageActor actor = ImageActor::New( image );
258   actor.SetSize( 100.0f, 100.0f );
259   actor.SetShaderEffect( effect );
260   Stage::GetCurrent().Add( actor );
261
262   try
263   {
264     effect.GetAmplitude(9999);
265     tet_result(TET_FAIL);
266   }
267   catch(DaliException& exception)
268   {
269     if (exception.mCondition == "index < mNumberOfWaves")
270     {
271       tet_result(TET_PASS);
272     }
273   }
274 }
275
276 static void UtcDaliWaterEffectGetCenterPositive()
277 {
278   ToolkitTestApplication application;
279
280   Toolkit::WaterEffect effect = Toolkit::WaterEffect::New(4);
281   DALI_TEST_CHECK( effect );
282
283   BitmapImage image = CreateBitmapImage();
284
285   ImageActor actor = ImageActor::New( image );
286   actor.SetSize( 100.0f, 100.0f );
287   actor.SetShaderEffect( effect );
288   Stage::GetCurrent().Add( actor );
289
290   Vector2 center(10.0f, 20.0f);
291   DALI_TEST_CHECK(effect.GetCenter(0) != center);
292   effect.SetCenter( 0, center );
293
294   application.SendNotification();
295   application.Render();
296
297   DALI_TEST_EQUALS(center, effect.GetCenter(0), TEST_LOCATION);
298 }
299
300 static void UtcDaliWaterEffectGetCenterNegative()
301 {
302   ToolkitTestApplication application;
303
304   Toolkit::WaterEffect effect = Toolkit::WaterEffect::New(4);
305   DALI_TEST_CHECK( effect );
306
307   BitmapImage image = CreateBitmapImage();
308
309   ImageActor actor = ImageActor::New( image );
310   actor.SetSize( 100.0f, 100.0f );
311   actor.SetShaderEffect( effect );
312   Stage::GetCurrent().Add( actor );
313
314   try
315   {
316     effect.GetCenter(9999);
317     tet_result(TET_FAIL);
318   }
319   catch(DaliException& exception)
320   {
321     if (exception.mCondition == "index < mNumberOfWaves")
322     {
323       tet_result(TET_PASS);
324     }
325   }
326 }
327
328 static void UtcDaliWaterEffectGetPropagationPositive()
329 {
330   ToolkitTestApplication application;
331
332   Toolkit::WaterEffect effect = Toolkit::WaterEffect::New(4);
333   DALI_TEST_CHECK( effect );
334
335   BitmapImage image = CreateBitmapImage();
336
337   ImageActor actor = ImageActor::New( image );
338   actor.SetSize( 100.0f, 100.0f );
339   actor.SetShaderEffect( effect );
340   Stage::GetCurrent().Add( actor );
341
342   float propagation(0.5f);
343   DALI_TEST_CHECK(effect.GetPropagation(0) != propagation);
344   effect.SetPropagation( 0, propagation );
345
346   application.SendNotification();
347   application.Render();
348
349   DALI_TEST_EQUALS(propagation, effect.GetPropagation(0), TEST_LOCATION);
350 }
351
352 static void UtcDaliWaterEffectGetPropagationNegative()
353 {
354   ToolkitTestApplication application;
355
356   Toolkit::WaterEffect effect = Toolkit::WaterEffect::New(4);
357   DALI_TEST_CHECK( effect );
358
359   BitmapImage image = CreateBitmapImage();
360
361   ImageActor actor = ImageActor::New( image );
362   actor.SetSize( 100.0f, 100.0f );
363   actor.SetShaderEffect( effect );
364   Stage::GetCurrent().Add( actor );
365
366   try
367   {
368     effect.GetPropagation(9999);
369     tet_result(TET_FAIL);
370   }
371   catch(DaliException& exception)
372   {
373     if (exception.mCondition == "index < mNumberOfWaves")
374     {
375       tet_result(TET_PASS);
376     }
377   }
378 }