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