[Tizen] Add screen and client rotation itself function
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-CSharp-TypeRegistry.cpp
1 /*
2  * Copyright (c) 2017 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 #include <stdlib.h>
20 #include <limits>
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23 #include <dali/devel-api/object/csharp-type-info.h>
24 #include <dali/devel-api/object/csharp-type-registry.h>
25 #include <dali/internal/event/common/type-registry-impl.h>
26 #include <dali/public-api/object/property.h>
27 using namespace Dali;
28
29
30 namespace
31 {
32
33
34 static bool CreateCustomNamedInitCalled = false;
35
36
37 BaseHandle* CreateCustomNamedInit(const char* const typeName )
38 {
39   CreateCustomNamedInitCalled = true;
40
41   BaseHandle* x = new BaseHandle();
42
43   return x;
44 }
45
46 // Property Registration
47 bool setPropertyCalled = false;
48 bool getPropertyCalled = false;
49 int intPropertyValue = 0;
50
51 void SetProperty( BaseObject* object, const char* const propertyName , Property::Value* value )
52 {
53
54   value->Get( intPropertyValue );
55
56   setPropertyCalled = true;
57 }
58
59 Property::Value* GetProperty( BaseObject* object, const char* const propertyName )
60 {
61   getPropertyCalled = true;
62   Property::Value* x = new Property::Value( 10 );
63   return x;
64 }
65
66 }
67
68 int UtcDaliRegisterCSharpTypeP(void)
69 {
70
71   TestApplication application;
72
73   CSharpTypeRegistry::RegisterType( "CSharpControl", typeid( Dali::Actor), &CreateCustomNamedInit  );
74
75   Dali::TypeInfo info = Dali::TypeRegistry::Get().GetTypeInfo( "CSharpControl" );
76
77   info.CreateInstance();
78
79   DALI_TEST_EQUALS( CreateCustomNamedInitCalled, true, TEST_LOCATION );
80
81   END_TEST;
82 }
83
84 int UtcDaliRegisterCSharpTypeNoInitP(void)
85 {
86
87   TestApplication application;
88
89   CreateCustomNamedInitCalled = false;
90
91   CSharpTypeRegistry::RegisterType( "CSharpControl", typeid( Dali::Actor), &CreateCustomNamedInit );
92
93   GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
94
95   DALI_TEST_EQUALS( CreateCustomNamedInitCalled, false, TEST_LOCATION );
96
97   END_TEST;
98 }
99
100 int UtcDaliRegisterCSharpTypeN(void)
101 {
102   TestApplication application;
103
104   CSharpTypeRegistry::RegisterType( "CSharpControl", typeid( Dali::Actor), &CreateCustomNamedInit );
105
106   // should cause an assert because we're registering same type twice
107   try
108   {
109     CSharpTypeRegistry::RegisterType( "CSharpControl", typeid( Dali::Actor), &CreateCustomNamedInit );
110     tet_result( TET_FAIL );
111   }
112   catch ( DaliException& e )
113   {
114     DALI_TEST_ASSERT( e, "Duplicate type name in Type Registration", TEST_LOCATION );
115   }
116
117   END_TEST;
118 }
119
120 int UtcDaliRegisterCSharpTypeCreateP(void)
121 {
122
123   TestApplication application;
124   CreateCustomNamedInitCalled = false;
125
126   CSharpTypeRegistry::RegisterType( "CSharpControl", typeid( Dali::Actor), &CreateCustomNamedInit );
127
128
129   TypeInfo info = Dali::TypeRegistry::Get().GetTypeInfo( "CSharpControl");
130
131   BaseHandle handle = info.CreateInstance();
132
133   DALI_TEST_EQUALS( CreateCustomNamedInitCalled, true, TEST_LOCATION );
134
135   END_TEST;
136 }
137
138
139
140 int UtcDaliRegisterCSharpPropertyP(void)
141 {
142   TestApplication application;
143
144   CSharpTypeRegistry::RegisterType( "DateControl", typeid( Dali::Actor), &CreateCustomNamedInit );
145
146
147   bool registered = CSharpTypeRegistry::RegisterProperty( "DateControl",
148                     "year",
149                     10000 + 1,
150                     Property::INTEGER,
151                     SetProperty,
152                     GetProperty );
153
154   DALI_TEST_EQUALS( registered, true, TEST_LOCATION );
155
156
157   END_TEST;
158 }
159
160
161 int UtcDaliRegisterCSharpPropertyN(void)
162 {
163   TestApplication application;
164
165   // register the same property twice
166   CSharpTypeRegistry::RegisterType( "DateControl", typeid( Dali::Actor), &CreateCustomNamedInit );
167
168
169   bool registered = CSharpTypeRegistry::RegisterProperty( "DateControl",
170                     "year",
171                     10000 + 1,
172                     Property::INTEGER,
173                     SetProperty,
174                     GetProperty );
175
176   DALI_TEST_EQUALS( registered, true, TEST_LOCATION );
177
178
179   // should fail second time with an assert as the property is already registered
180   try
181   {
182     registered = CSharpTypeRegistry::RegisterProperty( "DateControl",
183                       "year",
184                       10000 + 1,
185                       Property::INTEGER,
186                       SetProperty,
187                       GetProperty );
188
189     tet_result( TET_FAIL );
190   }
191   catch ( DaliException& e )
192   {
193     DALI_TEST_ASSERT( e, "Property index already added to Type", TEST_LOCATION );
194   }
195
196   END_TEST;
197 }
198
199 int UtcDaliRegisterCSharpPropertySetP(void)
200 {
201   TestApplication application;
202
203    // register the same property twice
204    CSharpTypeRegistry::RegisterType( "DateControl", typeid( Dali::Actor), &CreateCustomNamedInit );;
205
206    Property::Index index(100001);
207
208    CSharpTypeRegistry::RegisterProperty( "DateControl",
209                      "year",
210                      index,
211                      Property::INTEGER,
212                      SetProperty,
213                      GetProperty );
214
215    TypeRegistry typeRegistry = TypeRegistry::Get();
216
217    TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "DateControl" );
218
219
220    // Check the property is writable in the type registry
221    Internal::TypeInfo& typeInfoImpl = GetImplementation( typeInfo );
222
223    Property::Value value(25);
224
225    typeInfoImpl.SetProperty( NULL, index, value );
226
227    DALI_TEST_EQUALS( 25, intPropertyValue , TEST_LOCATION );
228
229    Property::Value value2(50);
230
231    typeInfoImpl.SetProperty( NULL, "year", value2 );
232
233    DALI_TEST_EQUALS( 50, intPropertyValue , TEST_LOCATION );
234
235    DALI_TEST_EQUALS( setPropertyCalled, true, TEST_LOCATION );
236
237    END_TEST;
238 }
239
240
241 int UtcDaliRegisterCSharpPropertyGetP(void)
242 {
243   TestApplication application;
244
245    // register the same property twice
246    CSharpTypeRegistry::RegisterType( "DateControl", typeid( Dali::Actor), &CreateCustomNamedInit );
247
248    Property::Index index(100001);
249
250    CSharpTypeRegistry::RegisterProperty( "DateControl",
251                      "year",
252                      index,
253                      Property::INTEGER,
254                      SetProperty,
255                      GetProperty );
256
257    TypeRegistry typeRegistry = TypeRegistry::Get();
258
259    TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "DateControl" );
260
261
262    // Check the property is writable in the type registry
263    Internal::TypeInfo& typeInfoImpl = GetImplementation( typeInfo );
264
265    Property::Value value(1);
266
267    value = typeInfoImpl.GetProperty( NULL, index );
268
269    int propValue;
270    value.Get( propValue );
271
272    DALI_TEST_EQUALS( getPropertyCalled, true, TEST_LOCATION );
273    DALI_TEST_EQUALS( propValue, 10, TEST_LOCATION );
274
275    value = typeInfoImpl.GetProperty( NULL, "year");
276    value.Get( propValue );
277
278    DALI_TEST_EQUALS( propValue, 10, TEST_LOCATION );
279
280    END_TEST;
281 }
282
283 int UtcDaliRegisterCSharpPropertyNotRegisteredN(void)
284 {
285   TestApplication application;
286
287   // control not registered, should fail
288   bool registered = CSharpTypeRegistry::RegisterProperty( "DateControl",
289                     "year",
290                     10000 + 1,
291                     Property::INTEGER,
292                     SetProperty,
293                     GetProperty );
294
295   DALI_TEST_EQUALS( registered, false, TEST_LOCATION );
296
297   END_TEST;
298 }
299