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