ab2ec1e0d43fc546da198e1cb237f0aadb70d0bd
[platform/core/uifw/dali-toolkit.git] / automated-tests / dali-test-suite / shader-effects / utc-Dali-SwirlEffect.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 static void UtcDaliSwirlUninitializedEffect();
38 static void UtcDaliSwirlPropertyNamesEffect();
39 static void UtcDaliSwirlDefaultValuesEffect();
40 static void UtcDaliSwirlCustomValuesEffect();
41
42 enum {
43   POSITIVE_TC_IDX = 0x01,
44   NEGATIVE_TC_IDX,
45 };
46
47 // Add test functionality for all APIs in the class (Positive and Negative)
48 extern "C" {
49   struct tet_testlist tet_testlist[] = {
50     { UtcDaliSwirlUninitializedEffect, NEGATIVE_TC_IDX },
51     { UtcDaliSwirlPropertyNamesEffect, POSITIVE_TC_IDX },
52     { UtcDaliSwirlDefaultValuesEffect, POSITIVE_TC_IDX },
53     { UtcDaliSwirlCustomValuesEffect, POSITIVE_TC_IDX },
54     { NULL, 0 }
55   };
56 }
57
58 // Called only once before first test is run.
59 static void Startup()
60 {
61 }
62
63 // Called only once after last test is run
64 static void Cleanup()
65 {
66 }
67
68 // Create bitmap image
69 BitmapImage CreateBitmapImage()
70 {
71   BitmapImage image = BitmapImage::New(4,4,Pixel::RGBA8888);
72
73   PixelBuffer* pixbuf = image.GetBuffer();
74
75   // Using a 4x4 image gives a better blend with the GL implementation
76   // than a 3x3 image
77   for(size_t i=0; i<16; i++)
78   {
79     pixbuf[i*4+0] = 0xFF;
80     pixbuf[i*4+1] = 0xFF;
81     pixbuf[i*4+2] = 0xFF;
82     pixbuf[i*4+3] = 0xFF;
83   }
84
85   return image;
86 }
87
88 static void UtcDaliSwirlUninitializedEffect()
89 {
90   ToolkitTestApplication application;
91
92   Toolkit::SwirlEffect effect;
93
94   try
95   {
96     // New() must be called to create a SwirlEffect or it wont be valid.
97     effect.SetRadius( 0.5f );
98     DALI_TEST_CHECK( false );
99   }
100   catch (Dali::DaliException& e)
101   {
102     // Tests that a negative test of an assertion succeeds
103     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
104     DALI_TEST_CHECK(!effect);
105   }
106 }
107
108 static void UtcDaliSwirlPropertyNamesEffect()
109 {
110   ToolkitTestApplication application;
111
112   Toolkit::SwirlEffect effect = Toolkit::SwirlEffect::New(false);
113
114   // Check the names, this names are used in the shaders code,
115   // if they change the shader code has to be updated
116   DALI_TEST_EQUALS( effect.GetAnglePropertyName(), "uAngle", TEST_LOCATION );
117   DALI_TEST_EQUALS( effect.GetCenterPropertyName(), "uCenter", TEST_LOCATION );
118   DALI_TEST_EQUALS( effect.GetRadiusPropertyName(), "uRadius", TEST_LOCATION );
119 }
120
121 static void UtcDaliSwirlDefaultValuesEffect()
122 {
123   ToolkitTestApplication application;
124
125   Toolkit::SwirlEffect effect = Toolkit::SwirlEffect::New(true);
126   DALI_TEST_CHECK( effect );
127
128   BitmapImage image = CreateBitmapImage();
129
130   ImageActor actor = ImageActor::New( image );
131   actor.SetSize( 100.0f, 100.0f );
132   actor.SetShaderEffect( effect );
133   Stage::GetCurrent().Add( actor );
134
135   application.SendNotification();
136   application.Render();
137
138   // Gets converted to opengl viewport coordinates
139   DALI_TEST_CHECK(
140       application.GetGlAbstraction().CheckUniformValue(
141           effect.GetAnglePropertyName().c_str(),
142           0.0f ) );
143
144   DALI_TEST_CHECK(
145       application.GetGlAbstraction().CheckUniformValue(
146           effect.GetCenterPropertyName().c_str(),
147           Vector2(0.5f, 0.5f) ) );
148
149   DALI_TEST_CHECK(
150       application.GetGlAbstraction().CheckUniformValue(
151           effect.GetRadiusPropertyName().c_str(),
152           1.0f ) );
153 }
154
155 static void UtcDaliSwirlCustomValuesEffect()
156 {
157   ToolkitTestApplication application;
158
159   Toolkit::SwirlEffect effect = Toolkit::SwirlEffect::New(false);
160   DALI_TEST_CHECK( effect );
161
162   BitmapImage image = CreateBitmapImage();
163
164   ImageActor actor = ImageActor::New( image );
165   actor.SetSize( 100.0f, 100.0f );
166
167   effect.SetAngle( 1.0f );
168   effect.SetCenter( Vector2(0.3f, 0.7f) );
169   effect.SetRadius( 2.0f );
170
171   actor.SetShaderEffect( effect );
172   Stage::GetCurrent().Add( actor );
173
174   application.SendNotification();
175   application.Render();
176
177   // Gets converted to opengl viewport coordinates
178   DALI_TEST_CHECK(
179       application.GetGlAbstraction().CheckUniformValue(
180           effect.GetAnglePropertyName().c_str(),
181           1.0f ) );
182
183   DALI_TEST_CHECK(
184       application.GetGlAbstraction().CheckUniformValue(
185           effect.GetCenterPropertyName().c_str(),
186           Vector2(0.3f, 0.7f) ) );
187
188   DALI_TEST_CHECK(
189       application.GetGlAbstraction().CheckUniformValue(
190           effect.GetRadiusPropertyName().c_str(),
191           2.0f ) );
192 }