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