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