Conversion to Apache 2.0 license
[platform/core/uifw/dali-toolkit.git] / automated-tests / TET / dali-test-suite / shader-effects / utc-Dali-DistanceFieldEffect.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/public-api/shader-effects/distance-field-effect.h>
25 #include <dali-toolkit/dali-toolkit.h>
26
27 #include <dali-toolkit-test-suite-utils.h>
28
29 using namespace Dali;
30
31 static void Startup();
32 static void Cleanup();
33
34 extern "C" {
35   void (*tet_startup)() = Startup;
36   void (*tet_cleanup)() = Cleanup;
37 }
38
39 static void UtcDaliDistanceFieldEffectUninitialized();
40 static void UtcDaliDistanceFieldEffectPropertyNames();
41 static void UtcDaliDistanceFieldEffectDefaultValues();
42 static void UtcDaliDistanceFieldEffectCustomValues();
43
44 enum {
45   POSITIVE_TC_IDX = 0x01,
46   NEGATIVE_TC_IDX,
47 };
48
49 // Add test functionality for all APIs in the class (Positive and Negative)
50 extern "C" {
51   struct tet_testlist tet_testlist[] = {
52     { UtcDaliDistanceFieldEffectUninitialized, NEGATIVE_TC_IDX },
53     { UtcDaliDistanceFieldEffectPropertyNames, POSITIVE_TC_IDX },
54     { UtcDaliDistanceFieldEffectDefaultValues, POSITIVE_TC_IDX },
55     { UtcDaliDistanceFieldEffectCustomValues, POSITIVE_TC_IDX },
56     { NULL, 0 }
57   };
58 }
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 CreateDistanceField()
72 {
73   BitmapImage image = BitmapImage::New(256, 256, Pixel::RGBA8888);
74   BitmapImage distanceFieldImage = BitmapImage::New(256, 256, Pixel::L8);
75
76   PixelBuffer* pixbuf = image.GetBuffer();
77
78   for(size_t i=0; i<16; i++)
79   {
80     pixbuf[i*4+0] = 0xFF;
81     pixbuf[i*4+1] = 0xFF;
82     pixbuf[i*4+2] = 0xFF;
83     pixbuf[i*4+3] = 0xFF;
84   }
85
86   // GenerateDistanceFieldMap(distanceFieldImage.GetBuffer(), Size(256, 256), pixbuf, Size(256, 256), 8, 4);
87
88   return distanceFieldImage;
89 }
90
91 static void UtcDaliDistanceFieldEffectUninitialized()
92 {
93   ToolkitTestApplication application;
94
95   Toolkit::DistanceFieldEffect effect;
96
97   try
98   {
99     // New() must be called to create a DistanceField effect or it wont be valid.
100     effect.SetShadow( true );
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 UtcDaliDistanceFieldEffectPropertyNames()
112 {
113   ToolkitTestApplication application;
114
115   Toolkit::DistanceFieldEffect effect = Toolkit::DistanceFieldEffect::New();
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.GetColorPropertyName(), "uColor", TEST_LOCATION );
120   DALI_TEST_EQUALS( effect.GetSmoothingPropertyName(), "uSmoothing", TEST_LOCATION );
121
122   // control flags
123   DALI_TEST_EQUALS( effect.GetOutlineEnablePropertyName(), "uDoOutline", TEST_LOCATION );
124   DALI_TEST_EQUALS( effect.GetGlowEnablePropertyName(), "uDoGlow", TEST_LOCATION );
125   DALI_TEST_EQUALS( effect.GetShadowEnablePropertyName(), "uDoShadow", TEST_LOCATION );
126
127   DALI_TEST_EQUALS( effect.GetGlowBoundaryPropertyName(), "uGlowBoundary", TEST_LOCATION );
128   DALI_TEST_EQUALS( effect.GetGlowColorPropertyName(), "uGlowColor", TEST_LOCATION );
129
130   DALI_TEST_EQUALS( effect.GetOutlineColorPropertyName(), "uOutlineColor", TEST_LOCATION );
131   DALI_TEST_EQUALS( effect.GetOutlineSizePropertyName(), "uOutlineParams", TEST_LOCATION );
132
133   DALI_TEST_EQUALS( effect.GetShadowColorPropertyName(), "uShadowColor", TEST_LOCATION );
134   DALI_TEST_EQUALS( effect.GetShadowOffsetPropertyName(), "uShadowOffset", TEST_LOCATION );
135
136 }
137
138 static void UtcDaliDistanceFieldEffectDefaultValues()
139 {
140   ToolkitTestApplication application;
141
142   Toolkit::DistanceFieldEffect effect = Toolkit::DistanceFieldEffect::New();
143   DALI_TEST_CHECK( effect );
144
145   BitmapImage image = CreateDistanceField();
146
147   ImageActor actor = ImageActor::New( image );
148   actor.SetSize( 100.0f, 100.0f );
149   actor.SetShaderEffect( effect );
150   Stage::GetCurrent().Add( actor );
151
152   application.SendNotification();
153   application.Render();
154
155   DALI_TEST_CHECK(
156       application.GetGlAbstraction().CheckUniformValue(
157           effect.GetOutlineEnablePropertyName().c_str(),
158           0.0f ));
159
160   DALI_TEST_CHECK(
161       application.GetGlAbstraction().CheckUniformValue(
162           effect.GetGlowEnablePropertyName().c_str(),
163           0.0f ));
164
165   DALI_TEST_CHECK(
166       application.GetGlAbstraction().CheckUniformValue(
167           effect.GetShadowEnablePropertyName().c_str(),
168           0.0f ));
169 }
170
171 static void UtcDaliDistanceFieldEffectCustomValues()
172 {
173   ToolkitTestApplication application;
174
175   Toolkit::DistanceFieldEffect effect = Toolkit::DistanceFieldEffect::New();
176   DALI_TEST_CHECK( effect );
177
178   BitmapImage image = CreateDistanceField();
179
180   ImageActor actor = ImageActor::New( image );
181   actor.SetSize( 100.0f, 100.0f );
182
183   effect.SetShadowColor(Color::YELLOW);
184   effect.SetGlowColor(Color::BLUE);
185
186   actor.SetShaderEffect( effect );
187   Stage::GetCurrent().Add( actor );
188
189   application.SendNotification();
190   application.Render();
191
192   // Gets converted to opengl viewport coordinates
193   DALI_TEST_CHECK(
194       application.GetGlAbstraction().CheckUniformValue(
195           effect.GetShadowColorPropertyName().c_str(),
196           Color::YELLOW ) );
197
198   DALI_TEST_CHECK(
199       application.GetGlAbstraction().CheckUniformValue(
200           effect.GetGlowColorPropertyName().c_str(),
201           Color::BLUE ) );
202 }
203