Support animatable property registration during type registry
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-TypeRegistry.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 #include <stdlib.h>
20 #include <dali/public-api/dali-core.h>
21 #include <dali-test-suite-utils.h>
22 #include <dali/integration-api/events/long-press-gesture-event.h>
23 #include <dali/integration-api/events/pan-gesture-event.h>
24 #include <dali/integration-api/events/pinch-gesture-event.h>
25 #include <dali/integration-api/events/tap-gesture-event.h>
26 #include <dali/integration-api/events/touch-event-integ.h>
27 #include <dali/integration-api/events/hover-event-integ.h>
28
29 using namespace Dali;
30
31
32 namespace
33 {
34
35 // Stores data that is populated in the callback and will be read by the Test cases
36 struct SignalData
37 {
38   SignalData()
39   : functorCalled( false ),
40     voidFunctorCalled( false ),
41     receivedGesture( Gesture::Clear ),
42     pressedActor()
43   {}
44
45   void Reset()
46   {
47     functorCalled = false;
48     voidFunctorCalled = false;
49
50     receivedGesture.numberOfTouches = 0u;
51     receivedGesture.screenPoint = Vector2(0.0f, 0.0f);
52     receivedGesture.localPoint = Vector2(0.0f, 0.0f);
53
54     pressedActor.Reset();
55   }
56
57   bool functorCalled;
58   bool voidFunctorCalled;
59   LongPressGesture receivedGesture;
60   Actor pressedActor;
61 };
62
63 // Functor that sets the data when called
64 struct GestureReceivedFunctor
65 {
66   GestureReceivedFunctor(SignalData& data) : signalData(data) { }
67
68   void operator()(Actor actor, LongPressGesture longPress)
69   {
70     signalData.functorCalled = true;
71     signalData.receivedGesture = longPress;
72     signalData.pressedActor = actor;
73   }
74
75   void operator()()
76   {
77     signalData.voidFunctorCalled = true;
78   }
79
80   SignalData& signalData;
81 };
82
83 // Generate a LongPressGestureEvent to send to Core
84 Integration::LongPressGestureEvent GenerateLongPress(
85     Gesture::State state,
86     unsigned int numberOfTouches,
87     Vector2 point)
88 {
89   Integration::LongPressGestureEvent longPress( state );
90
91   longPress.numberOfTouches = numberOfTouches;
92   longPress.point = point;
93
94   return longPress;
95 }
96
97 // Generate a PanGestureEvent to send to Core
98 Integration::PanGestureEvent GeneratePan(
99     Gesture::State state,
100     Vector2 previousPosition,
101     Vector2 currentPosition,
102     unsigned long timeDelta,
103     unsigned int numberOfTouches = 1,
104     unsigned int time = 1u)
105 {
106   Integration::PanGestureEvent pan(state);
107
108   pan.previousPosition = previousPosition;
109   pan.currentPosition = currentPosition;
110   pan.timeDelta = timeDelta;
111   pan.numberOfTouches = numberOfTouches;
112   pan.time = time;
113
114   return pan;
115 }
116 // Generate a PinchGestureEvent to send to Core
117 Integration::PinchGestureEvent GeneratePinch(
118     Gesture::State state,
119     float scale,
120     float speed,
121     Vector2 centerpoint)
122 {
123   Integration::PinchGestureEvent pinch(state);
124
125   pinch.scale = scale;
126   pinch.speed = speed;
127   pinch.centerPoint = centerpoint;
128
129   return pinch;
130 }
131 // Generate a TapGestureEvent to send to Core
132 Integration::TapGestureEvent GenerateTap(
133     Gesture::State state,
134     unsigned int numberOfTaps,
135     unsigned int numberOfTouches,
136     Vector2 point)
137 {
138   Integration::TapGestureEvent tap( state );
139
140   tap.numberOfTaps = numberOfTaps;
141   tap.numberOfTouches = numberOfTouches;
142   tap.point = point;
143
144   return tap;
145 }
146
147 //
148 // Create function as Init function called
149 //
150 static bool CreateCustomInitCalled = false;
151 BaseHandle CreateCustomInit(void)
152 {
153   CreateCustomInitCalled = true;
154   return BaseHandle();
155 }
156
157 static bool CreateCustomNamedInitCalled = false;
158 BaseHandle CreateCustomNamedInit(void)
159 {
160   CreateCustomNamedInitCalled = true;
161   return BaseHandle();
162 }
163
164 const std::string scriptedName("PopupStyle");
165 static TypeRegistration scriptedType( scriptedName, typeid(Dali::CustomActor), CreateCustomNamedInit );
166
167 // Property Registration
168 bool setPropertyCalled = false;
169 bool getPropertyCalled = false;
170 void SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
171 {
172   setPropertyCalled = true;
173 }
174 Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex )
175 {
176   getPropertyCalled = true;
177   return Property::Value( true );
178 }
179
180
181
182 /*******************************************************************************
183  *
184  * Custom Actor
185  *
186  ******************************************************************************/
187 namespace Impl
188 {
189 struct MyTestCustomActor : public CustomActorImpl
190 {
191   typedef Signal< void ()> SignalType;
192   typedef Signal< void (float)> SignalTypeFloat;
193
194   MyTestCustomActor() : CustomActorImpl( true ) // requires touch
195   { }
196
197   virtual ~MyTestCustomActor()
198   { }
199
200   void ResetCallStack()
201   {
202   }
203
204   // From CustomActorImpl
205   virtual void OnStageConnection()
206   {
207   }
208   virtual void OnStageDisconnection()
209   {
210   }
211   virtual void OnChildAdd(Actor& child)
212   {
213   }
214   virtual void OnChildRemove(Actor& child)
215   {
216   }
217   virtual void OnSizeSet(const Vector3& targetSize)
218   {
219   }
220   virtual void OnSizeAnimation(Animation& animation, const Vector3& targetSize)
221   {
222   }
223   virtual bool OnTouchEvent(const TouchEvent& event)
224   {
225     return true;
226   }
227   virtual bool OnHoverEvent(const HoverEvent& event)
228   {
229     return true;
230   }
231   virtual bool OnMouseWheelEvent(const MouseWheelEvent& event)
232   {
233     return true;
234   }
235   virtual bool OnKeyEvent(const KeyEvent& event)
236   {
237     return true;
238   }
239   virtual void OnKeyInputFocusGained()
240   {
241   }
242   virtual void OnKeyInputFocusLost()
243   {
244   }
245   virtual Vector3 GetNaturalSize()
246   {
247     return Vector3( 0.0f, 0.0f, 0.0f );
248   }
249
250 public:
251
252   SignalType mSignal;
253 };
254
255 }; // namespace Impl
256
257 class MyTestCustomActor : public CustomActor
258 {
259 public:
260
261   typedef Signal< void ()> SignalType;
262   typedef Signal< void (float)> SignalTypeFloat;
263
264   MyTestCustomActor()
265   {
266   }
267
268   static MyTestCustomActor New()
269   {
270     Impl::MyTestCustomActor* p = new Impl::MyTestCustomActor;
271     return MyTestCustomActor( *p ); // takes ownership
272   }
273
274   virtual ~MyTestCustomActor()
275   {
276   }
277
278   static MyTestCustomActor DownCast( BaseHandle handle )
279   {
280     MyTestCustomActor result;
281
282     CustomActor custom = Dali::CustomActor::DownCast( handle );
283     if ( custom )
284     {
285       CustomActorImpl& customImpl = custom.GetImplementation();
286
287       Impl::MyTestCustomActor* impl = dynamic_cast<Impl::MyTestCustomActor*>(&customImpl);
288
289       if (impl)
290       {
291         result = MyTestCustomActor(customImpl.GetOwner());
292       }
293     }
294
295     return result;
296   }
297
298   SignalType& GetCustomSignal()
299   {
300     Dali::RefObject& obj = GetImplementation();
301     return static_cast<Impl::MyTestCustomActor&>( obj ).mSignal;
302   }
303
304 private:
305
306   MyTestCustomActor(Internal::CustomActor* internal)
307   : CustomActor(internal)
308   {
309   }
310
311   MyTestCustomActor( Impl::MyTestCustomActor& impl )
312   : CustomActor( impl )
313   {
314   }
315 };
316
317
318 class MyTestCustomActor2 : public CustomActor
319 {
320 public:
321
322   MyTestCustomActor2()
323   {
324   }
325
326   static MyTestCustomActor2 New()
327   {
328     return MyTestCustomActor2(); // takes ownership
329   }
330
331   virtual ~MyTestCustomActor2()
332   {
333   }
334
335   static MyTestCustomActor2 DownCast( BaseHandle handle )
336   {
337     MyTestCustomActor2 result;
338
339     CustomActor custom = Dali::CustomActor::DownCast( handle );
340     if ( custom )
341     {
342       CustomActorImpl& customImpl = custom.GetImplementation();
343
344       Impl::MyTestCustomActor* impl = dynamic_cast<Impl::MyTestCustomActor*>(&customImpl);
345
346       if (impl)
347       {
348         result = MyTestCustomActor2(customImpl.GetOwner());
349       }
350     }
351
352     return result;
353   }
354
355 private:
356
357   MyTestCustomActor2(Internal::CustomActor* internal)
358   : CustomActor(internal)
359   {
360   }
361
362   MyTestCustomActor2( Impl::MyTestCustomActor& impl )
363   : CustomActor( impl )
364   {
365   }
366 };
367
368 static TypeRegistration customTypeInit( typeid(MyTestCustomActor2), typeid(Dali::CustomActor), CreateCustomInit, true );
369
370
371 BaseHandle CreateCustom(void)
372 {
373   return MyTestCustomActor::New();
374 }
375
376 static std::string lastSignalConnectionCustom;
377
378 bool DoConnectSignalCustom( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
379 {
380   lastSignalConnectionCustom = signalName;
381
382   bool connected( true );
383
384   Dali::BaseHandle handle(object);
385   MyTestCustomActor customActor = MyTestCustomActor::DownCast(handle);
386
387   if( "sig1" == signalName )
388   {
389     customActor.GetCustomSignal().Connect( tracker, functor );
390   }
391   else
392   {
393     // signalName does not match any signal
394     connected = false;
395   }
396
397   return connected;
398 }
399
400 bool DoConnectSignalCustomFailure( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
401 {
402   lastSignalConnectionCustom = "failed";
403
404   return false; // This is supposed to fail
405 }
406
407 struct CustomTestFunctor
408 {
409   CustomTestFunctor()
410   {
411     ++mTotalInstanceCount;
412     ++mCurrentInstanceCount;
413   }
414
415   CustomTestFunctor( const CustomTestFunctor& copyMe )
416   {
417     ++mTotalInstanceCount;
418     ++mCurrentInstanceCount;
419   }
420
421   ~CustomTestFunctor()
422   {
423     --mCurrentInstanceCount;
424   }
425
426   void operator()()
427   {
428     ++mCallbackCount;
429   }
430
431   static int mTotalInstanceCount;
432   static int mCurrentInstanceCount;
433   static int mCallbackCount;
434 };
435
436 int CustomTestFunctor::mTotalInstanceCount = 0;
437 int CustomTestFunctor::mCurrentInstanceCount = 0;
438 int CustomTestFunctor::mCallbackCount = 0;
439
440 static void ResetFunctorCounts()
441 {
442   CustomTestFunctor::mTotalInstanceCount   = 0;
443   CustomTestFunctor::mCurrentInstanceCount = 0;
444   CustomTestFunctor::mCallbackCount        = 0;
445 }
446
447 static std::string lastActionCustom;
448 bool DoActionCustom(BaseObject* object, const std::string& actionName, const std::vector<Property::Value>& attributes)
449 {
450   lastActionCustom = actionName;
451   return true;
452 }
453
454 // Custom type registration
455 static TypeRegistration customType1( typeid(MyTestCustomActor), typeid(Dali::CustomActor), CreateCustom );
456
457 // Custom signals
458 static SignalConnectorType customSignalConnector1( customType1, "sig1", DoConnectSignalCustom );
459 static SignalConnectorType customSignalConnector2( customType1, "sig2", DoConnectSignalCustomFailure );
460 static const int TEST_SIGNAL_COUNT = 2;
461
462 // Custom actions
463 static TypeAction customAction1( customType1, "act1", DoActionCustom);
464 static const int TEST_ACTION_COUNT = 1;
465
466 class TestConnectionTracker : public ConnectionTracker
467 {
468 public:
469
470   TestConnectionTracker()
471   {
472   }
473 };
474
475 BaseHandle CreateNamedActorType()
476 {
477   Actor actor = Actor::New();
478   actor.SetName( "NamedActor" );
479   return actor;
480 }
481
482 TypeRegistration namedActorType( "MyNamedActor", typeid(Dali::Actor), CreateNamedActorType );
483 PropertyRegistration namedActorPropertyOne( namedActorType, "prop-name", PROPERTY_REGISTRATION_START_INDEX, Property::BOOLEAN, &SetProperty, &GetProperty );
484
485 } // Anonymous namespace
486
487
488 // Positive test case for a method
489 int UtcDaliTypeRegistryCreateDaliObjects(void)
490 {
491   TestApplication application;
492
493   TypeRegistry registry; // like this for ctor test coverage
494   registry = TypeRegistry::Get();
495
496   TypeInfo type;
497
498   type = registry.GetTypeInfo( "ImageActor" );
499   DALI_TEST_CHECK( type );
500   DALI_TEST_CHECK( type.GetCreator() );
501   DALI_TEST_CHECK( ImageActor::DownCast( type.GetCreator()() ) );
502   ImageActor ia = ImageActor::DownCast(type.CreateInstance());
503   DALI_TEST_CHECK( ia );
504   Stage::GetCurrent().Add( ia );
505   application.Render();
506
507   type = registry.GetTypeInfo( "TextActor" );
508   DALI_TEST_CHECK( type );
509   TextActor ta = TextActor::DownCast(type.CreateInstance());
510   DALI_TEST_CHECK( ta );
511   Stage::GetCurrent().Add( ta );
512   application.Render();
513
514   type = registry.GetTypeInfo( "CameraActor" );
515   DALI_TEST_CHECK( type );
516   CameraActor ca = CameraActor::DownCast(type.CreateInstance());
517   DALI_TEST_CHECK( ca );
518   Stage::GetCurrent().Add( ca );
519   application.Render();
520
521   // animations
522   type = registry.GetTypeInfo( "Animation" );
523   DALI_TEST_CHECK( type );
524   Animation an = Animation::DownCast(type.CreateInstance());
525   DALI_TEST_CHECK( an );
526   an.Play();
527   application.Render();
528
529   //
530   type = registry.GetTypeInfo( "ShaderEffect" );
531   DALI_TEST_CHECK( type );
532   ShaderEffect ef = ShaderEffect::DownCast(type.CreateInstance());
533   DALI_TEST_CHECK( ef );
534   application.Render();
535
536   END_TEST;
537 }
538
539 /*******************************************************************************
540  *
541  * Action through the base handle
542  *
543  ******************************************************************************/
544 int UtcDaliTypeRegistryActionViaBaseHandle(void)
545 {
546   TestApplication application;
547
548   TypeInfo type;
549
550   type = TypeRegistry::Get().GetTypeInfo( "Actor" );
551   DALI_TEST_CHECK( type );
552
553   BaseHandle hdl = type.CreateInstance();
554   DALI_TEST_CHECK( hdl );
555
556   Actor a = Actor::DownCast(hdl);
557   DALI_TEST_CHECK( a );
558
559   a.SetVisible(false);
560
561   application.SendNotification();
562   application.Render(0);
563   DALI_TEST_CHECK(!a.IsVisible());
564
565   std::vector<Property::Value> attributes;
566
567   DALI_TEST_CHECK(hdl.DoAction("show", attributes));
568
569   application.SendNotification();
570   application.Render(0);
571   DALI_TEST_CHECK(a.IsVisible());
572
573   DALI_TEST_CHECK(!hdl.DoAction("unknown-action", attributes));
574   END_TEST;
575 }
576
577 int UtcDaliTypeRegistryNames(void)
578 {
579   TestApplication application;
580
581   TypeInfo type;
582
583   TypeRegistry::NameContainer names = TypeRegistry::Get().GetTypeNames();
584
585   for(TypeRegistry::NameContainer::iterator iter = names.begin();
586       iter != names.end(); ++iter)
587   {
588     type = TypeRegistry::Get().GetTypeInfo( *iter );
589     DALI_TEST_CHECK( type );
590   }
591
592   END_TEST;
593 }
594
595 // Check named and typeid are equivalent
596 int UtcDaliTypeRegistryNameEquivalence(void)
597 {
598   TypeInfo named_type = TypeRegistry::Get().GetTypeInfo( "TextActor" );
599   TypeInfo typeinfo_type = TypeRegistry::Get().GetTypeInfo( typeid(Dali::TextActor) );
600
601   DALI_TEST_CHECK( named_type );
602   DALI_TEST_CHECK( typeinfo_type );
603
604   DALI_TEST_CHECK( named_type == typeinfo_type );
605
606   DALI_TEST_CHECK( named_type.GetName() == typeinfo_type.GetName() );
607   DALI_TEST_CHECK( named_type.GetBaseName() == typeinfo_type.GetBaseName() );
608
609   END_TEST;
610 }
611
612
613 int UtcDaliTypeRegistryCustomActor(void)
614 {
615   ResetFunctorCounts();
616
617   TestApplication application;
618
619   TypeInfo type = TypeRegistry::Get().GetTypeInfo( "MyTestCustomActor" );
620   DALI_TEST_CHECK( type );
621
622   TypeInfo baseType = TypeRegistry::Get().GetTypeInfo( "CustomActor" );
623   DALI_TEST_CHECK( baseType );
624
625   BaseHandle handle = type.CreateInstance();
626   DALI_TEST_CHECK( handle );
627
628   MyTestCustomActor customHandle = MyTestCustomActor::DownCast( handle );
629   DALI_TEST_CHECK( customHandle );
630
631   TypeInfo::NameContainer names;
632   type.GetActions(names);
633   TypeInfo::NameContainer baseNames;
634   baseType.GetActions(baseNames);
635   DALI_TEST_EQUALS( names.size(), TEST_ACTION_COUNT + baseNames.size(), TEST_LOCATION );
636
637   names.clear();
638   type.GetSignals(names);
639
640   baseNames.clear();
641   baseType.GetSignals(baseNames);
642
643   DALI_TEST_EQUALS( names.size(), TEST_SIGNAL_COUNT + baseNames.size(), TEST_LOCATION );
644
645   {
646     TestConnectionTracker tracker;
647
648     bool connected = handle.ConnectSignal( &tracker, "sig1", CustomTestFunctor() );
649     DALI_TEST_EQUALS( connected, true, TEST_LOCATION );
650     DALI_TEST_CHECK( lastSignalConnectionCustom == "sig1" );
651     DALI_TEST_EQUALS( CustomTestFunctor::mTotalInstanceCount, 2/*temporary copy + FunctorDelegate copy*/, TEST_LOCATION );
652     DALI_TEST_EQUALS( CustomTestFunctor::mCurrentInstanceCount, 1, TEST_LOCATION );
653
654     DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 0, TEST_LOCATION );
655     customHandle.GetCustomSignal().Emit();
656     DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 1, TEST_LOCATION );
657     DALI_TEST_EQUALS( CustomTestFunctor::mTotalInstanceCount, 2/*temporary copy + FunctorDelegate copy*/, TEST_LOCATION );
658     DALI_TEST_EQUALS( CustomTestFunctor::mCurrentInstanceCount, 1, TEST_LOCATION );
659   }
660   // tracker should automatically disconnect here
661   DALI_TEST_EQUALS( CustomTestFunctor::mTotalInstanceCount, 2/*temporary copy + FunctorDelegate copy*/, TEST_LOCATION );
662   DALI_TEST_EQUALS( CustomTestFunctor::mCurrentInstanceCount, 0, TEST_LOCATION );
663
664   // Test that functor is disconnected
665   DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 1, TEST_LOCATION );
666   customHandle.GetCustomSignal().Emit();
667   DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 1/*not incremented*/, TEST_LOCATION );
668   DALI_TEST_EQUALS( CustomTestFunctor::mTotalInstanceCount, 2/*temporary copy + FunctorDelegate copy*/, TEST_LOCATION );
669   DALI_TEST_EQUALS( CustomTestFunctor::mCurrentInstanceCount, 0, TEST_LOCATION );
670
671   std::vector<Property::Value> attributes;
672   handle.DoAction("act1", attributes);
673   DALI_TEST_CHECK( lastActionCustom == "act1" );
674   END_TEST;
675 }
676
677 int UtcDaliTypeRegistryCustomSignalFailure(void)
678 {
679   // Test what happens when signal connnector (DoConnectSignalFailure method) returns false
680
681   ResetFunctorCounts();
682
683   TestApplication application;
684
685   TypeInfo type = TypeRegistry::Get().GetTypeInfo( "MyTestCustomActor" );
686   DALI_TEST_CHECK( type );
687
688   TypeInfo baseType = TypeRegistry::Get().GetTypeInfo( "CustomActor" );
689   DALI_TEST_CHECK( baseType );
690
691   BaseHandle handle = type.CreateInstance();
692   DALI_TEST_CHECK( handle );
693
694   MyTestCustomActor customHandle = MyTestCustomActor::DownCast( handle );
695   DALI_TEST_CHECK( customHandle );
696
697   TypeInfo::NameContainer names;
698   TypeInfo::NameContainer baseNames;
699
700   type.GetActions(names);
701   baseType.GetActions(baseNames);
702
703   DALI_TEST_EQUALS( names.size(), TEST_ACTION_COUNT + baseNames.size(), TEST_LOCATION );
704
705   names.clear();
706   baseNames.clear();
707
708   type.GetSignals(names);
709   baseType.GetSignals(baseNames);
710
711   DALI_TEST_EQUALS( names.size(), TEST_SIGNAL_COUNT + baseNames.size(), TEST_LOCATION );
712
713   {
714     TestConnectionTracker tracker;
715
716     bool connected = handle.ConnectSignal( &tracker, "sig2", CustomTestFunctor() );
717     DALI_TEST_EQUALS( connected, false/*This is supposed to fail*/, TEST_LOCATION );
718     DALI_TEST_CHECK( lastSignalConnectionCustom == "failed" );
719     DALI_TEST_EQUALS( CustomTestFunctor::mTotalInstanceCount, 2/*temporary copy + FunctorDelegate copy*/, TEST_LOCATION );
720     DALI_TEST_EQUALS( CustomTestFunctor::mCurrentInstanceCount, 0/*deleted along with FunctorDelegate*/, TEST_LOCATION );
721
722     // Should be a NOOP
723     DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 0, TEST_LOCATION );
724     customHandle.GetCustomSignal().Emit();
725     DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 0/*never called*/, TEST_LOCATION );
726   }
727   // tracker should have nothing to disconnect here
728
729   // Should be a NOOP
730   DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 0, TEST_LOCATION );
731   customHandle.GetCustomSignal().Emit();
732   DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 0/*never called*/, TEST_LOCATION );
733   END_TEST;
734 }
735
736 int UtcDaliTypeRegistryInitFunctions(void)
737 {
738   TestApplication application;
739
740   DALI_TEST_CHECK( "MyTestCustomActor2" == customTypeInit.RegisteredName() );
741
742   DALI_TEST_CHECK( true == CreateCustomInitCalled );
743   TypeInfo type = TypeRegistry::Get().GetTypeInfo( "MyTestCustomActor2" );
744   DALI_TEST_CHECK( type );
745   END_TEST;
746 }
747
748
749 int UtcDaliTypeRegistryNameInitFunctions(void)
750 {
751   TestApplication application;
752
753   DALI_TEST_CHECK( scriptedName == scriptedType.RegisteredName() );
754
755   TypeInfo baseType = TypeRegistry::Get().GetTypeInfo( scriptedName );
756   DALI_TEST_CHECK( baseType );
757
758   BaseHandle handle = baseType.CreateInstance();
759
760   DALI_TEST_CHECK( true == CreateCustomNamedInitCalled );
761   TypeInfo type = TypeRegistry::Get().GetTypeInfo( scriptedName );
762   DALI_TEST_CHECK( type );
763   END_TEST;
764 }
765
766
767 int UtcDaliPropertyRegistration(void)
768 {
769   TestApplication application;
770   TypeRegistry typeRegistry = TypeRegistry::Get();
771
772   // Check property count before property registration
773   TypeInfo typeInfo = typeRegistry.GetTypeInfo( typeid(MyTestCustomActor) );
774   DALI_TEST_CHECK( typeInfo );
775   BaseHandle handle = typeInfo.CreateInstance();
776   DALI_TEST_CHECK( handle );
777   Actor customActor = Actor::DownCast( handle );
778   DALI_TEST_CHECK( customActor );
779   unsigned int initialPropertyCount( customActor.GetPropertyCount() );
780
781   std::string propertyName( "prop-1" );
782   int propertyIndex( PROPERTY_REGISTRATION_START_INDEX );
783   Property::Type propertyType( Property::BOOLEAN );
784   PropertyRegistration property1( customType1, propertyName, propertyIndex, propertyType, &SetProperty, &GetProperty );
785
786   // Check property count after registration
787   unsigned int postRegistrationPropertyCount( customActor.GetPropertyCount() );
788   DALI_TEST_EQUALS( initialPropertyCount + 1u, postRegistrationPropertyCount, TEST_LOCATION );
789
790   // Add custom property and check property count
791   customActor.RegisterProperty( "custom-prop-1", true );
792   unsigned int customPropertyCount( customActor.GetPropertyCount() );
793   DALI_TEST_EQUALS( postRegistrationPropertyCount + 1u, customPropertyCount, TEST_LOCATION );
794
795   // Set the property, ensure SetProperty called
796   DALI_TEST_CHECK( !setPropertyCalled );
797   customActor.SetProperty( propertyIndex, false );
798   DALI_TEST_CHECK( setPropertyCalled );
799
800   // Get the property, ensure GetProperty called
801   DALI_TEST_CHECK( !getPropertyCalled );
802   (void)customActor.GetProperty< bool >( propertyIndex );
803   DALI_TEST_CHECK( getPropertyCalled );
804
805   // Check the property name
806   DALI_TEST_EQUALS( customActor.GetPropertyName( propertyIndex ), propertyName, TEST_LOCATION );
807   DALI_TEST_EQUALS( typeInfo.GetPropertyName( propertyIndex ), propertyName, TEST_LOCATION );
808
809   // Check the property index
810   DALI_TEST_EQUALS( customActor.GetPropertyIndex( propertyName ), propertyIndex, TEST_LOCATION );
811
812   // Check the property type
813   DALI_TEST_EQUALS( customActor.GetPropertyType( propertyIndex ), propertyType, TEST_LOCATION );
814
815   // Check property count of type-info is 1
816   Property::IndexContainer indices;
817   typeInfo.GetPropertyIndices( indices );
818   DALI_TEST_EQUALS( indices.size(), 1u, TEST_LOCATION );
819
820   // Register animatable property
821   std::string animatablePropertyName( "animatable-prop-1" );
822   int animatablePropertyIndex( ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX );
823   Property::Type animatablePropertyType( Property::FLOAT );
824   AnimatablePropertyRegistration animatableProperty1( customType1, animatablePropertyName, animatablePropertyIndex, animatablePropertyType );
825
826   // Check property count after registration
827   DALI_TEST_EQUALS( customPropertyCount + 1u, customActor.GetPropertyCount(), TEST_LOCATION );
828
829   // Set the animatable property value
830   customActor.SetProperty( animatablePropertyIndex, 25.0f );
831
832   // Render and notify
833   application.SendNotification();
834   application.Render();
835
836   // Check the animatable property value
837   DALI_TEST_EQUALS( customActor.GetProperty< float >( animatablePropertyIndex ), 25.0f, TEST_LOCATION );
838
839   // Check the animatable property name
840   DALI_TEST_EQUALS( customActor.GetPropertyName( animatablePropertyIndex ), animatablePropertyName, TEST_LOCATION );
841 //  DALI_TEST_EQUALS( typeInfo.GetPropertyName( animatablePropertyIndex ), animatablePropertyName, TEST_LOCATION );
842
843   // Check the animatable property index
844   DALI_TEST_EQUALS( customActor.GetPropertyIndex( animatablePropertyName ), animatablePropertyIndex, TEST_LOCATION );
845
846   // Check the animatable property type
847   DALI_TEST_EQUALS( customActor.GetPropertyType( animatablePropertyIndex ), animatablePropertyType, TEST_LOCATION );
848
849   // Ensure indices returned from actor and customActor differ by three
850   Actor actor = Actor::New();
851   actor.GetPropertyIndices( indices );
852   unsigned int actorIndices = indices.size();
853   customActor.GetPropertyIndices( indices );
854   unsigned int customActorIndices = indices.size();
855   DALI_TEST_EQUALS( actorIndices + 3u, customActorIndices, TEST_LOCATION ); // Custom property + registered property
856   END_TEST;
857 }
858
859 int UtcDaliPropertyRegistrationIndexOutOfBounds(void)
860 {
861   TestApplication application;
862   TypeRegistry typeRegistry = TypeRegistry::Get();
863
864   // Attempt to register a property type out-of-bounds index (less than)
865   try
866   {
867     PropertyRegistration property1( customType1, "prop-name", PROPERTY_REGISTRATION_START_INDEX - 1, Property::BOOLEAN, &SetProperty, &GetProperty );
868     tet_result( TET_FAIL );
869   }
870   catch ( DaliException& e )
871   {
872     DALI_TEST_ASSERT( e, "( index >= PROPERTY_REGISTRATION_START_INDEX ) && ( index <= PROPERTY_REGISTRATION_MAX_INDEX )", TEST_LOCATION );
873   }
874
875   // Attempt to register a property type out-of-bounds index (greater than)
876   try
877   {
878     PropertyRegistration property1( customType1, "prop-name", PROPERTY_REGISTRATION_MAX_INDEX + 1, Property::BOOLEAN, &SetProperty, &GetProperty );
879     tet_result( TET_FAIL );
880   }
881   catch ( DaliException& e )
882   {
883     DALI_TEST_ASSERT( e, "( index >= PROPERTY_REGISTRATION_START_INDEX ) && ( index <= PROPERTY_REGISTRATION_MAX_INDEX )", TEST_LOCATION );
884   }
885
886   // Attempt to register an animatable property type out-of-bounds index (less than)
887   try
888   {
889     AnimatablePropertyRegistration property1( customType1, "anim-prop-name", ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX - 1, Property::BOOLEAN );
890     tet_result( TET_FAIL );
891   }
892   catch ( DaliException& e )
893   {
894     DALI_TEST_ASSERT( e, "( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX )", TEST_LOCATION );
895   }
896
897   // Attempt to register an animatable property type out-of-bounds index (greater than)
898   try
899   {
900     AnimatablePropertyRegistration property1( customType1, "anim-prop-name", ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX + 1, Property::BOOLEAN );
901     tet_result( TET_FAIL );
902   }
903   catch ( DaliException& e )
904   {
905     DALI_TEST_ASSERT( e, "( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX )", TEST_LOCATION );
906   }
907   END_TEST;
908 }
909
910 int UtcDaliPropertyRegistrationFunctions(void)
911 {
912   TestApplication application;
913   int propertyIndex = PROPERTY_REGISTRATION_START_INDEX + 10;
914
915   // Attempt to register a property without a setter
916   try
917   {
918     PropertyRegistration property1( customType1, "prop-name", propertyIndex++, Property::BOOLEAN, NULL, &GetProperty );
919     tet_result( TET_PASS );
920   }
921   catch ( DaliException& e )
922   {
923     tet_result( TET_FAIL );
924   }
925
926   // Attempt to register a property without a getter
927   try
928   {
929     PropertyRegistration property1( customType1, "prop-name", propertyIndex++, Property::BOOLEAN, NULL, NULL );
930     tet_result( TET_FAIL );
931   }
932   catch ( DaliException& e )
933   {
934     DALI_TEST_ASSERT( e, "! \"GetProperty", TEST_LOCATION );
935   }
936   END_TEST;
937 }
938
939 int UtcDaliPropertyRegistrationAddSameIndex(void)
940 {
941   TestApplication application;
942   int propertyIndex = PROPERTY_REGISTRATION_START_INDEX + 100;
943
944   // Add one property with a valid property index
945   PropertyRegistration property1( customType1, "prop-name", propertyIndex, Property::BOOLEAN, &SetProperty, &GetProperty );
946
947   // Attempt to add another property with the same index
948   try
949   {
950     PropertyRegistration property2( customType1, "prop-name-2", propertyIndex, Property::BOOLEAN, &SetProperty, &GetProperty );
951   }
952   catch ( DaliException& e )
953   {
954     DALI_TEST_ASSERT( e, "! \"Property index already added", TEST_LOCATION );
955   }
956
957   int animatablePropertyIndex = ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX + 100;
958
959   // Add one property with a valid property index
960   AnimatablePropertyRegistration property3( customType1, "anim-prop-name", animatablePropertyIndex, Property::BOOLEAN );
961
962   // Attempt to add another property with the same index
963   try
964   {
965     AnimatablePropertyRegistration property4( customType1, "anim-prop-name-2", animatablePropertyIndex, Property::BOOLEAN );
966   }
967   catch ( DaliException& e )
968   {
969     DALI_TEST_ASSERT( e, "! \"Property index already added", TEST_LOCATION );
970   }
971   END_TEST;
972 }
973
974 int UtcDaliPropertyRegistrationPropertyWritable(void)
975 {
976   TestApplication application;
977   int propertyIndex1 = PROPERTY_REGISTRATION_START_INDEX + 200;
978   int propertyIndex2 = PROPERTY_REGISTRATION_START_INDEX + 201;
979
980   // Add two properties, one with SetProperty, one without
981   PropertyRegistration property1( customType1, "prop-name-readwrite", propertyIndex1, Property::BOOLEAN, &SetProperty, &GetProperty );
982   PropertyRegistration property2( customType1, "prop-name-readonly",  propertyIndex2, Property::BOOLEAN, NULL, &GetProperty );
983
984   // Create custom-actor
985   TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( typeid(MyTestCustomActor) );
986   DALI_TEST_CHECK( typeInfo );
987   BaseHandle handle = typeInfo.CreateInstance();
988   DALI_TEST_CHECK( handle );
989   Actor customActor = Actor::DownCast( handle );
990   DALI_TEST_CHECK( customActor );
991
992   // Check whether properties are writable
993   DALI_TEST_CHECK(   customActor.IsPropertyWritable( propertyIndex1 ) );
994   DALI_TEST_CHECK( ! customActor.IsPropertyWritable( propertyIndex2 ) );
995   END_TEST;
996 }
997
998 int UtcDaliPropertyRegistrationPropertyAnimatable(void)
999 {
1000   TestApplication application;
1001   int propertyIndex = PROPERTY_REGISTRATION_START_INDEX + 400;
1002   int animatablePropertyIndex = ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX + 400;
1003
1004   // These properties are not animatable
1005   PropertyRegistration property1( customType1, "prop-name", propertyIndex, Property::BOOLEAN, &SetProperty, &GetProperty );
1006
1007   // These properties are animatable
1008   AnimatablePropertyRegistration property2( customType1, "anim-prop-name", animatablePropertyIndex, Property::BOOLEAN );
1009
1010   // Create custom-actor
1011   TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( typeid(MyTestCustomActor) );
1012   DALI_TEST_CHECK( typeInfo );
1013   BaseHandle handle = typeInfo.CreateInstance();
1014   DALI_TEST_CHECK( handle );
1015   Actor customActor = Actor::DownCast( handle );
1016   DALI_TEST_CHECK( customActor );
1017
1018   // Check if animatable
1019   DALI_TEST_CHECK( ! customActor.IsPropertyAnimatable( propertyIndex ) );
1020   DALI_TEST_CHECK( customActor.IsPropertyAnimatable( animatablePropertyIndex ) );
1021
1022   // Create another instance of custom-actor
1023   BaseHandle handle2 = typeInfo.CreateInstance();
1024   DALI_TEST_CHECK( handle2 );
1025   Actor customActor2 = Actor::DownCast( handle2 );
1026   DALI_TEST_CHECK( customActor2 );
1027
1028   // Check if animatable
1029   DALI_TEST_CHECK( ! customActor2.IsPropertyAnimatable( propertyIndex ) );
1030   DALI_TEST_CHECK( customActor2.IsPropertyAnimatable( animatablePropertyIndex ) );
1031   END_TEST;
1032 }
1033
1034 int UtcDaliPropertyRegistrationInvalidGetAndSet(void)
1035 {
1036   TestApplication application;
1037   int propertyIndex = PROPERTY_REGISTRATION_START_INDEX + 2000;
1038   int animatablePropertyIndex = ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX + 2000;
1039
1040   // Create custom-actor
1041   TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( typeid(MyTestCustomActor) );
1042   DALI_TEST_CHECK( typeInfo );
1043   BaseHandle handle = typeInfo.CreateInstance();
1044   DALI_TEST_CHECK( handle );
1045   Actor customActor = Actor::DownCast( handle );
1046   DALI_TEST_CHECK( customActor );
1047
1048   // Try to set an index that hasn't been added
1049   try
1050   {
1051     customActor.SetProperty( propertyIndex, true );
1052     tet_result( TET_FAIL );
1053   }
1054   catch ( DaliException& e )
1055   {
1056     DALI_TEST_ASSERT( e, "! \"Cannot find property index", TEST_LOCATION );
1057   }
1058
1059   try
1060   {
1061     customActor.SetProperty( animatablePropertyIndex, true );
1062     tet_result( TET_FAIL );
1063   }
1064   catch ( DaliException& e )
1065   {
1066     DALI_TEST_ASSERT( e, "! \"Cannot find property index", TEST_LOCATION );
1067   }
1068
1069   // Try to get an index that hasn't been added
1070   try
1071   {
1072     (void) customActor.GetProperty< bool >( propertyIndex );
1073     tet_result( TET_FAIL );
1074   }
1075   catch ( DaliException& e )
1076   {
1077     DALI_TEST_ASSERT( e, "! \"Cannot find property index", TEST_LOCATION );
1078   }
1079
1080   try
1081   {
1082     (void) customActor.GetProperty< bool >( animatablePropertyIndex );
1083     tet_result( TET_FAIL );
1084   }
1085   catch ( DaliException& e )
1086   {
1087     DALI_TEST_ASSERT( e, "! \"Cannot find property index", TEST_LOCATION );
1088   }
1089   END_TEST;
1090 }
1091
1092
1093 int UtcDaliLongPressGestureDetectorTypeRegistry(void)
1094 {
1095   TestApplication application;
1096
1097   Actor actor = Actor::New();
1098   actor.SetSize(100.0f, 100.0f);
1099   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1100   Stage::GetCurrent().Add(actor);
1101
1102   // Register Type
1103   TypeInfo type;
1104   type = TypeRegistry::Get().GetTypeInfo( "LongPressGestureDetector" );
1105   DALI_TEST_CHECK( type );
1106   BaseHandle handle = type.CreateInstance();
1107   DALI_TEST_CHECK( handle );
1108   LongPressGestureDetector detector = LongPressGestureDetector::DownCast( handle );
1109   DALI_TEST_CHECK( detector );
1110
1111   // Attach actor to detector
1112   SignalData data;
1113   GestureReceivedFunctor functor( data );
1114   detector.Attach(actor);
1115
1116   // Connect to signal through type
1117   handle.ConnectSignal( &application, "long-press-detected", functor );
1118
1119   // Render and notify
1120   application.SendNotification();
1121   application.Render();
1122
1123   // Emit gesture
1124   application.ProcessEvent(GenerateLongPress(Gesture::Possible, 1u, Vector2(50.0f, 10.0f)));
1125   application.ProcessEvent(GenerateLongPress(Gesture::Started, 1u, Vector2(50.0f, 10.0f)));
1126   application.ProcessEvent(GenerateLongPress(Gesture::Finished, 1u, Vector2(50.0f, 10.0f)));
1127   DALI_TEST_EQUALS(true, data.voidFunctorCalled, TEST_LOCATION);
1128   END_TEST;
1129 }
1130
1131 int UtcDaliPanGestureDetectorTypeRegistry(void)
1132 {
1133   TestApplication application;
1134
1135   Actor actor = Actor::New();
1136   actor.SetSize(100.0f, 100.0f);
1137   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1138   Stage::GetCurrent().Add(actor);
1139
1140   // Register Type
1141   TypeInfo type;
1142   type = TypeRegistry::Get().GetTypeInfo( "PanGestureDetector" );
1143   DALI_TEST_CHECK( type );
1144   BaseHandle handle = type.CreateInstance();
1145   DALI_TEST_CHECK( handle );
1146   PanGestureDetector detector = PanGestureDetector::DownCast( handle );
1147   DALI_TEST_CHECK( detector );
1148
1149   // Attach actor to detector
1150   SignalData data;
1151   GestureReceivedFunctor functor( data );
1152   detector.Attach(actor);
1153
1154   // Connect to signal through type
1155   handle.ConnectSignal( &application, "pan-detected", functor );
1156
1157   // Render and notify
1158   application.SendNotification();
1159   application.Render();
1160
1161   // Emit gesture
1162   application.ProcessEvent(GeneratePan(Gesture::Possible, Vector2(10.0f, 20.0f), Vector2(20.0f, 20.0f), 10));
1163   application.ProcessEvent(GeneratePan(Gesture::Started, Vector2(10.0f, 20.0f), Vector2(20.0f, 20.0f), 10));
1164   application.ProcessEvent(GeneratePan(Gesture::Finished, Vector2(10.0f, 20.0f), Vector2(20.0f, 20.0f), 10));
1165   DALI_TEST_EQUALS(true, data.voidFunctorCalled, TEST_LOCATION);
1166   END_TEST;
1167 }
1168
1169 int UtcDaliPinchGestureDetectorTypeRegistry(void)
1170 {
1171   TestApplication application;
1172
1173   Actor actor = Actor::New();
1174   actor.SetSize(100.0f, 100.0f);
1175   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1176   Stage::GetCurrent().Add(actor);
1177
1178   // Register Type
1179   TypeInfo type;
1180   type = TypeRegistry::Get().GetTypeInfo( "PinchGestureDetector" );
1181   DALI_TEST_CHECK( type );
1182   BaseHandle handle = type.CreateInstance();
1183   DALI_TEST_CHECK( handle );
1184   PinchGestureDetector detector = PinchGestureDetector::DownCast( handle );
1185   DALI_TEST_CHECK( detector );
1186
1187   // Attach actor to detector
1188   SignalData data;
1189   GestureReceivedFunctor functor( data );
1190   detector.Attach(actor);
1191
1192   // Connect to signal through type
1193   handle.ConnectSignal( &application, "pinch-detected", functor );
1194
1195   // Render and notify
1196   application.SendNotification();
1197   application.Render();
1198
1199   // Emit gesture
1200   application.ProcessEvent(GeneratePinch(Gesture::Started, 10.0f, 50.0f, Vector2(20.0f, 10.0f)));
1201   DALI_TEST_EQUALS(true, data.voidFunctorCalled, TEST_LOCATION);
1202   END_TEST;
1203 }
1204
1205 int UtcDaliTapGestureDetectorTypeRegistry(void)
1206 {
1207   TestApplication application;
1208
1209   Actor actor = Actor::New();
1210   actor.SetSize(100.0f, 100.0f);
1211   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1212   Stage::GetCurrent().Add(actor);
1213
1214   // Register Type
1215   TypeInfo type;
1216   type = TypeRegistry::Get().GetTypeInfo( "TapGestureDetector" );
1217   DALI_TEST_CHECK( type );
1218   BaseHandle handle = type.CreateInstance();
1219   DALI_TEST_CHECK( handle );
1220   TapGestureDetector detector = TapGestureDetector::DownCast( handle );
1221   DALI_TEST_CHECK( detector );
1222
1223   // Attach actor to detector
1224   SignalData data;
1225   GestureReceivedFunctor functor( data );
1226   detector.Attach(actor);
1227
1228   // Connect to signal through type
1229   handle.ConnectSignal( &application, "tap-detected", functor );
1230
1231   // Render and notify
1232   application.SendNotification();
1233   application.Render();
1234
1235   // Emit gesture
1236   application.ProcessEvent(GenerateTap(Gesture::Possible, 1u, 1u, Vector2(50.0f, 10.0f)));
1237   application.ProcessEvent(GenerateTap(Gesture::Started, 1u, 1u, Vector2(50.0f, 10.0f)));
1238   DALI_TEST_EQUALS(true, data.voidFunctorCalled, TEST_LOCATION);
1239   END_TEST;
1240 }
1241
1242 int UtcDaliTypeRegistryNamedType(void)
1243 {
1244   TestApplication application;
1245   TypeRegistry typeRegistry = TypeRegistry::Get();
1246
1247   // Create a normal actor
1248   BaseHandle actorHandle = typeRegistry.GetTypeInfo( "Actor" ).CreateInstance();
1249   DALI_TEST_CHECK( actorHandle );
1250   Actor actor( Actor::DownCast( actorHandle ) );
1251   DALI_TEST_CHECK( actor );
1252   unsigned int actorPropertyCount( actor.GetPropertyCount() );
1253
1254   // Create Named Actor Type
1255   BaseHandle namedHandle = typeRegistry.GetTypeInfo( "MyNamedActor" ).CreateInstance();
1256   DALI_TEST_CHECK( namedHandle );
1257   Actor namedActor( Actor::DownCast( namedHandle ) );
1258   DALI_TEST_CHECK( namedActor );
1259   unsigned int namedActorPropertyCount( namedActor.GetPropertyCount() );
1260
1261   DALI_TEST_CHECK( namedActorPropertyCount > actorPropertyCount );
1262   END_TEST;
1263 }