License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-unmanaged / 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/dali.h>
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23 #include <dali/integration-api/events/long-press-gesture-event.h>
24 #include <dali/integration-api/events/pan-gesture-event.h>
25 #include <dali/integration-api/events/pinch-gesture-event.h>
26 #include <dali/integration-api/events/tap-gesture-event.h>
27 #include <dali/integration-api/events/touch-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 = NULL;
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 SignalV2< void ()> SignalType;
192   typedef SignalV2< 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 OnMouseWheelEvent(const MouseWheelEvent& event)
228   {
229     return true;
230   }
231   virtual bool OnKeyEvent(const KeyEvent& event)
232   {
233     return true;
234   }
235   virtual void OnKeyInputFocusGained()
236   {
237   }
238   virtual void OnKeyInputFocusLost()
239   {
240   }
241   virtual Actor GetChildByAlias(const std::string& actorAlias)
242   {
243     return Actor::New();
244   }
245
246 public:
247
248   SignalType mSignal;
249 };
250
251 }; // namespace Impl
252
253 class MyTestCustomActor : public CustomActor
254 {
255 public:
256
257   typedef SignalV2< void ()> SignalType;
258   typedef SignalV2< void (float)> SignalTypeFloat;
259
260   MyTestCustomActor()
261   {
262   }
263
264   static MyTestCustomActor New()
265   {
266     Impl::MyTestCustomActor* p = new Impl::MyTestCustomActor;
267     return MyTestCustomActor( *p ); // takes ownership
268   }
269
270   virtual ~MyTestCustomActor()
271   {
272   }
273
274   static MyTestCustomActor DownCast( BaseHandle handle )
275   {
276     MyTestCustomActor result;
277
278     CustomActor custom = Dali::CustomActor::DownCast( handle );
279     if ( custom )
280     {
281       CustomActorImpl& customImpl = custom.GetImplementation();
282
283       Impl::MyTestCustomActor* impl = dynamic_cast<Impl::MyTestCustomActor*>(&customImpl);
284
285       if (impl)
286       {
287         result = MyTestCustomActor(customImpl.GetOwner());
288       }
289     }
290
291     return result;
292   }
293
294   SignalType& GetCustomSignal()
295   {
296     Dali::RefObject& obj = GetImplementation();
297     return static_cast<Impl::MyTestCustomActor&>( obj ).mSignal;
298   }
299
300 private:
301
302   MyTestCustomActor(Internal::CustomActor* internal)
303   : CustomActor(internal)
304   {
305   }
306
307   MyTestCustomActor( Impl::MyTestCustomActor& impl )
308   : CustomActor( impl )
309   {
310   }
311 };
312
313
314 class MyTestCustomActor2 : public CustomActor
315 {
316 public:
317
318   MyTestCustomActor2()
319   {
320   }
321
322   static MyTestCustomActor2 New()
323   {
324     return MyTestCustomActor2(); // takes ownership
325   }
326
327   virtual ~MyTestCustomActor2()
328   {
329   }
330
331   static MyTestCustomActor2 DownCast( BaseHandle handle )
332   {
333     MyTestCustomActor2 result;
334
335     CustomActor custom = Dali::CustomActor::DownCast( handle );
336     if ( custom )
337     {
338       CustomActorImpl& customImpl = custom.GetImplementation();
339
340       Impl::MyTestCustomActor* impl = dynamic_cast<Impl::MyTestCustomActor*>(&customImpl);
341
342       if (impl)
343       {
344         result = MyTestCustomActor2(customImpl.GetOwner());
345       }
346     }
347
348     return result;
349   }
350
351 private:
352
353   MyTestCustomActor2(Internal::CustomActor* internal)
354   : CustomActor(internal)
355   {
356   }
357
358   MyTestCustomActor2( Impl::MyTestCustomActor& impl )
359   : CustomActor( impl )
360   {
361   }
362 };
363
364 static TypeRegistration customTypeInit( typeid(MyTestCustomActor2), typeid(Dali::CustomActor), CreateCustomInit, true );
365
366
367 BaseHandle CreateCustom(void)
368 {
369   return MyTestCustomActor::New();
370 }
371
372 static std::string lastSignalConnectionCustom;
373
374 bool DoConnectSignalCustom( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
375 {
376   lastSignalConnectionCustom = signalName;
377
378   bool connected( true );
379
380   Dali::BaseHandle handle(object);
381   MyTestCustomActor customActor = MyTestCustomActor::DownCast(handle);
382
383   if( "sig1" == signalName )
384   {
385     customActor.GetCustomSignal().Connect( tracker, functor );
386   }
387   else
388   {
389     // signalName does not match any signal
390     connected = false;
391   }
392
393   return connected;
394 }
395
396 bool DoConnectSignalCustomFailure( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
397 {
398   lastSignalConnectionCustom = "failed";
399
400   return false; // This is supposed to fail
401 }
402
403 struct CustomTestFunctor
404 {
405   CustomTestFunctor()
406   {
407     ++mTotalInstanceCount;
408     ++mCurrentInstanceCount;
409   }
410
411   CustomTestFunctor( const CustomTestFunctor& copyMe )
412   {
413     ++mTotalInstanceCount;
414     ++mCurrentInstanceCount;
415   }
416
417   ~CustomTestFunctor()
418   {
419     --mCurrentInstanceCount;
420   }
421
422   void operator()()
423   {
424     ++mCallbackCount;
425   }
426
427   static int mTotalInstanceCount;
428   static int mCurrentInstanceCount;
429   static int mCallbackCount;
430 };
431
432 int CustomTestFunctor::mTotalInstanceCount = 0;
433 int CustomTestFunctor::mCurrentInstanceCount = 0;
434 int CustomTestFunctor::mCallbackCount = 0;
435
436 static void ResetFunctorCounts()
437 {
438   CustomTestFunctor::mTotalInstanceCount   = 0;
439   CustomTestFunctor::mCurrentInstanceCount = 0;
440   CustomTestFunctor::mCallbackCount        = 0;
441 }
442
443 static std::string lastActionCustom;
444 bool DoActionCustom(BaseObject* object, const std::string& actionName, const std::vector<Property::Value>& attributes)
445 {
446   lastActionCustom = actionName;
447   return true;
448 }
449
450 // Custom type registration
451 static TypeRegistration customType1( typeid(MyTestCustomActor), typeid(Dali::CustomActor), CreateCustom );
452
453 // Custom signals
454 static SignalConnectorType customSignalConnector1( customType1, "sig1", DoConnectSignalCustom );
455 static SignalConnectorType customSignalConnector2( customType1, "sig2", DoConnectSignalCustomFailure );
456 static const int TEST_SIGNAL_COUNT = 2;
457
458 // Custom actions
459 static TypeAction customAction1( customType1, "act1", DoActionCustom);
460 static const int TEST_ACTION_COUNT = 1;
461
462 class TestConnectionTracker : public ConnectionTracker
463 {
464 public:
465
466   TestConnectionTracker()
467   {
468   }
469 };
470
471 BaseHandle CreateNamedActorType()
472 {
473   Actor actor = Actor::New();
474   actor.SetName( "NamedActor" );
475   return actor;
476 }
477
478 TypeRegistration namedActorType( "MyNamedActor", typeid(Dali::Actor), CreateNamedActorType );
479 PropertyRegistration namedActorPropertyOne( namedActorType, "prop-name", PROPERTY_REGISTRATION_START_INDEX, Property::BOOLEAN, &SetProperty, &GetProperty );
480
481 } // Anonymous namespace
482
483
484 // Positive test case for a method
485 int UtcDaliTypeRegistryCreateDaliObjects(void)
486 {
487   TestApplication application;
488
489   TypeRegistry registry; // like this for ctor test coverage
490   registry = TypeRegistry::Get();
491
492   TypeInfo type;
493
494   type = registry.GetTypeInfo( "ImageActor" );
495   DALI_TEST_CHECK( type );
496   DALI_TEST_CHECK( type.GetCreator() );
497   DALI_TEST_CHECK( ImageActor::DownCast( type.GetCreator()() ) );
498   ImageActor ia = ImageActor::DownCast(type.CreateInstance());
499   DALI_TEST_CHECK( ia );
500   Stage::GetCurrent().Add( ia );
501   application.Render();
502
503   type = registry.GetTypeInfo( "TextActor" );
504   DALI_TEST_CHECK( type );
505   TextActor ta = TextActor::DownCast(type.CreateInstance());
506   DALI_TEST_CHECK( ta );
507   Stage::GetCurrent().Add( ta );
508   application.Render();
509
510   type = registry.GetTypeInfo( "CameraActor" );
511   DALI_TEST_CHECK( type );
512   CameraActor ca = CameraActor::DownCast(type.CreateInstance());
513   DALI_TEST_CHECK( ca );
514   Stage::GetCurrent().Add( ca );
515   application.Render();
516
517   type = registry.GetTypeInfo( "LightActor" );
518   DALI_TEST_CHECK( type );
519   LightActor la = LightActor::DownCast(type.CreateInstance());
520   DALI_TEST_CHECK( la );
521   Stage::GetCurrent().Add( la );
522   application.Render();
523
524   // animations
525   type = registry.GetTypeInfo( "Animation" );
526   DALI_TEST_CHECK( type );
527   Animation an = Animation::DownCast(type.CreateInstance());
528   DALI_TEST_CHECK( an );
529   an.Play();
530   application.Render();
531
532   //
533   type = registry.GetTypeInfo( "ShaderEffect" );
534   DALI_TEST_CHECK( type );
535   ShaderEffect ef = ShaderEffect::DownCast(type.CreateInstance());
536   DALI_TEST_CHECK( ef );
537   application.Render();
538
539   END_TEST;
540 }
541
542 /*******************************************************************************
543  *
544  * Action through the base handle
545  *
546  ******************************************************************************/
547 int UtcDaliTypeRegistryActionViaBaseHandle(void)
548 {
549   TestApplication application;
550
551   TypeInfo type;
552
553   type = TypeRegistry::Get().GetTypeInfo( "Actor" );
554   DALI_TEST_CHECK( type );
555
556   BaseHandle hdl = type.CreateInstance();
557   DALI_TEST_CHECK( hdl );
558
559   Actor a = Actor::DownCast(hdl);
560   DALI_TEST_CHECK( a );
561
562   a.SetVisible(false);
563
564   application.SendNotification();
565   application.Render(0);
566   DALI_TEST_CHECK(!a.IsVisible());
567
568   std::vector<Property::Value> attributes;
569
570   DALI_TEST_CHECK(hdl.DoAction(Actor::ACTION_SHOW, attributes));
571
572   application.SendNotification();
573   application.Render(0);
574   DALI_TEST_CHECK(a.IsVisible());
575
576   DALI_TEST_CHECK(!hdl.DoAction("unknown-action", attributes));
577   END_TEST;
578 }
579
580 int UtcDaliTypeRegistryNames(void)
581 {
582   TestApplication application;
583
584   TypeInfo type;
585
586   TypeRegistry::NameContainer names = TypeRegistry::Get().GetTypeNames();
587
588   for(TypeRegistry::NameContainer::iterator iter = names.begin();
589       iter != names.end(); ++iter)
590   {
591     type = TypeRegistry::Get().GetTypeInfo( *iter );
592     DALI_TEST_CHECK( type );
593   }
594
595   END_TEST;
596 }
597
598 // Check named and typeid are equivalent
599 int UtcDaliTypeRegistryNameEquivalence(void)
600 {
601   TypeInfo named_type = TypeRegistry::Get().GetTypeInfo( "TextActor" );
602   TypeInfo typeinfo_type = TypeRegistry::Get().GetTypeInfo( typeid(Dali::TextActor) );
603
604   DALI_TEST_CHECK( named_type );
605   DALI_TEST_CHECK( typeinfo_type );
606
607   DALI_TEST_CHECK( named_type == typeinfo_type );
608
609   DALI_TEST_CHECK( named_type.GetName() == typeinfo_type.GetName() );
610   DALI_TEST_CHECK( named_type.GetBaseName() == typeinfo_type.GetBaseName() );
611
612   END_TEST;
613 }
614
615
616 int UtcDaliTypeRegistryCustomActor(void)
617 {
618   ResetFunctorCounts();
619
620   TestApplication application;
621
622   TypeInfo type = TypeRegistry::Get().GetTypeInfo( "MyTestCustomActor" );
623   DALI_TEST_CHECK( type );
624
625   TypeInfo baseType = TypeRegistry::Get().GetTypeInfo( "CustomActor" );
626   DALI_TEST_CHECK( baseType );
627
628   BaseHandle handle = type.CreateInstance();
629   DALI_TEST_CHECK( handle );
630
631   MyTestCustomActor customHandle = MyTestCustomActor::DownCast( handle );
632   DALI_TEST_CHECK( customHandle );
633
634   DALI_TEST_EQUALS( type.GetActions().size(), TEST_ACTION_COUNT + baseType.GetActions().size(), TEST_LOCATION );
635   DALI_TEST_EQUALS( type.GetSignals().size(), TEST_SIGNAL_COUNT + baseType.GetSignals().size(), TEST_LOCATION );
636
637   {
638     TestConnectionTracker tracker;
639
640     bool connected = handle.ConnectSignal( &tracker, "sig1", CustomTestFunctor() );
641     DALI_TEST_EQUALS( connected, true, TEST_LOCATION );
642     DALI_TEST_CHECK( lastSignalConnectionCustom == "sig1" );
643     DALI_TEST_EQUALS( CustomTestFunctor::mTotalInstanceCount, 2/*temporary copy + FunctorDelegate copy*/, TEST_LOCATION );
644     DALI_TEST_EQUALS( CustomTestFunctor::mCurrentInstanceCount, 1, TEST_LOCATION );
645
646     DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 0, TEST_LOCATION );
647     customHandle.GetCustomSignal().Emit();
648     DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 1, TEST_LOCATION );
649     DALI_TEST_EQUALS( CustomTestFunctor::mTotalInstanceCount, 2/*temporary copy + FunctorDelegate copy*/, TEST_LOCATION );
650     DALI_TEST_EQUALS( CustomTestFunctor::mCurrentInstanceCount, 1, TEST_LOCATION );
651   }
652   // tracker should automatically disconnect here
653   DALI_TEST_EQUALS( CustomTestFunctor::mTotalInstanceCount, 2/*temporary copy + FunctorDelegate copy*/, TEST_LOCATION );
654   DALI_TEST_EQUALS( CustomTestFunctor::mCurrentInstanceCount, 0, TEST_LOCATION );
655
656   // Test that functor is disconnected
657   DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 1, TEST_LOCATION );
658   customHandle.GetCustomSignal().Emit();
659   DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 1/*not incremented*/, TEST_LOCATION );
660   DALI_TEST_EQUALS( CustomTestFunctor::mTotalInstanceCount, 2/*temporary copy + FunctorDelegate copy*/, TEST_LOCATION );
661   DALI_TEST_EQUALS( CustomTestFunctor::mCurrentInstanceCount, 0, TEST_LOCATION );
662
663   std::vector<Property::Value> attributes;
664   handle.DoAction("act1", attributes);
665   DALI_TEST_CHECK( lastActionCustom == "act1" );
666   END_TEST;
667 }
668
669 int UtcDaliTypeRegistryCustomSignalFailure(void)
670 {
671   // Test what happens when signal connnector (DoConnectSignalFailure method) returns false
672
673   ResetFunctorCounts();
674
675   TestApplication application;
676
677   TypeInfo type = TypeRegistry::Get().GetTypeInfo( "MyTestCustomActor" );
678   DALI_TEST_CHECK( type );
679
680   TypeInfo baseType = TypeRegistry::Get().GetTypeInfo( "CustomActor" );
681   DALI_TEST_CHECK( baseType );
682
683   BaseHandle handle = type.CreateInstance();
684   DALI_TEST_CHECK( handle );
685
686   MyTestCustomActor customHandle = MyTestCustomActor::DownCast( handle );
687   DALI_TEST_CHECK( customHandle );
688
689   DALI_TEST_EQUALS( type.GetActions().size(), TEST_ACTION_COUNT + baseType.GetActions().size(), TEST_LOCATION );
690   DALI_TEST_EQUALS( type.GetSignals().size(), TEST_SIGNAL_COUNT + baseType.GetSignals().size(), TEST_LOCATION );
691
692   {
693     TestConnectionTracker tracker;
694
695     bool connected = handle.ConnectSignal( &tracker, "sig2", CustomTestFunctor() );
696     DALI_TEST_EQUALS( connected, false/*This is supposed to fail*/, TEST_LOCATION );
697     DALI_TEST_CHECK( lastSignalConnectionCustom == "failed" );
698     DALI_TEST_EQUALS( CustomTestFunctor::mTotalInstanceCount, 2/*temporary copy + FunctorDelegate copy*/, TEST_LOCATION );
699     DALI_TEST_EQUALS( CustomTestFunctor::mCurrentInstanceCount, 0/*deleted along with FunctorDelegate*/, TEST_LOCATION );
700
701     // Should be a NOOP
702     DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 0, TEST_LOCATION );
703     customHandle.GetCustomSignal().Emit();
704     DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 0/*never called*/, TEST_LOCATION );
705   }
706   // tracker should have nothing to disconnect here
707
708   // Should be a NOOP
709   DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 0, TEST_LOCATION );
710   customHandle.GetCustomSignal().Emit();
711   DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 0/*never called*/, TEST_LOCATION );
712   END_TEST;
713 }
714
715
716
717 int UtcDaliTypeRegistryInitFunctions(void)
718 {
719   TestApplication application;
720
721   DALI_TEST_CHECK( "MyTestCustomActor2" == customTypeInit.RegisteredName() );
722
723   DALI_TEST_CHECK( true == CreateCustomInitCalled );
724   TypeInfo type = TypeRegistry::Get().GetTypeInfo( "MyTestCustomActor2" );
725   DALI_TEST_CHECK( type );
726   END_TEST;
727 }
728
729
730
731
732 int UtcDaliTypeRegistryNameInitFunctions(void)
733 {
734   TestApplication application;
735
736   DALI_TEST_CHECK( scriptedName == scriptedType.RegisteredName() );
737
738   TypeInfo baseType = TypeRegistry::Get().GetTypeInfo( scriptedName );
739   DALI_TEST_CHECK( baseType );
740
741   BaseHandle handle = baseType.CreateInstance();
742
743   DALI_TEST_CHECK( true == CreateCustomNamedInitCalled );
744   TypeInfo type = TypeRegistry::Get().GetTypeInfo( scriptedName );
745   DALI_TEST_CHECK( type );
746   END_TEST;
747 }
748
749
750 int UtcDaliPropertyRegistration(void)
751 {
752   TestApplication application;
753   TypeRegistry typeRegistry = TypeRegistry::Get();
754
755   // Check property count before property registration
756   TypeInfo typeInfo = typeRegistry.GetTypeInfo( typeid(MyTestCustomActor) );
757   DALI_TEST_CHECK( typeInfo );
758   BaseHandle handle = typeInfo.CreateInstance();
759   DALI_TEST_CHECK( handle );
760   Actor customActor = Actor::DownCast( handle );
761   DALI_TEST_CHECK( customActor );
762   unsigned int initialPropertyCount( customActor.GetPropertyCount() );
763
764   std::string propertyName( "prop-1" );
765   int propertyIndex( PROPERTY_REGISTRATION_START_INDEX );
766   Property::Type propertyType( Property::BOOLEAN );
767   PropertyRegistration property1( customType1, propertyName, propertyIndex, propertyType, &SetProperty, &GetProperty );
768
769   // Check property count after registration
770   unsigned int postRegistrationPropertyCount( customActor.GetPropertyCount() );
771   DALI_TEST_EQUALS( initialPropertyCount + 1u, postRegistrationPropertyCount, TEST_LOCATION );
772
773   // Add custom property and check property count
774   customActor.RegisterProperty( "custom-prop-1", true );
775   unsigned int customPropertyCount( customActor.GetPropertyCount() );
776   DALI_TEST_EQUALS( postRegistrationPropertyCount + 1u, customPropertyCount, TEST_LOCATION );
777
778   // Set the property, ensure SetProperty called
779   DALI_TEST_CHECK( !setPropertyCalled );
780   customActor.SetProperty( propertyIndex, false );
781   DALI_TEST_CHECK( setPropertyCalled );
782
783   // Get the property, ensure GetProperty called
784   DALI_TEST_CHECK( !getPropertyCalled );
785   (void)customActor.GetProperty< bool >( propertyIndex );
786   DALI_TEST_CHECK( getPropertyCalled );
787
788   // Check the property name
789   DALI_TEST_EQUALS( customActor.GetPropertyName( propertyIndex ), propertyName, TEST_LOCATION );
790   DALI_TEST_EQUALS( typeInfo.GetPropertyName( propertyIndex ), propertyName, TEST_LOCATION );
791
792   // Check the property index
793   DALI_TEST_EQUALS( customActor.GetPropertyIndex( propertyName ), propertyIndex, TEST_LOCATION );
794
795   // Check the property type
796   DALI_TEST_EQUALS( customActor.GetPropertyType( propertyIndex ), propertyType, TEST_LOCATION );
797
798   // Check property count of type-info is 1
799   Property::IndexContainer indices;
800   typeInfo.GetPropertyIndices( indices );
801   DALI_TEST_EQUALS( indices.size(), 1u, TEST_LOCATION );
802
803   // Ensure indices returned from actor and customActor differ by two
804   Actor actor = Actor::New();
805   actor.GetPropertyIndices( indices );
806   unsigned int actorIndices = indices.size();
807   customActor.GetPropertyIndices( indices );
808   unsigned int customActorIndices = indices.size();
809   DALI_TEST_EQUALS( actorIndices + 2u, customActorIndices, TEST_LOCATION ); // Custom property + registered property
810   END_TEST;
811 }
812
813 int UtcDaliPropertyRegistrationIndexOutOfBounds(void)
814 {
815   TestApplication application;
816   TypeRegistry typeRegistry = TypeRegistry::Get();
817
818   // Attempt to register a property type out-of-bounds index (less than)
819   try
820   {
821     PropertyRegistration property1( customType1, "prop-name", PROPERTY_REGISTRATION_START_INDEX - 1, Property::BOOLEAN, &SetProperty, &GetProperty );
822     tet_result( TET_FAIL );
823   }
824   catch ( DaliException& e )
825   {
826     DALI_TEST_ASSERT( e, "( index >= PROPERTY_REGISTRATION_START_INDEX ) && ( index <= PROPERTY_REGISTRATION_MAX_INDEX )", TEST_LOCATION );
827   }
828
829   // Attempt to register a property type out-of-bounds index (greater than)
830   try
831   {
832     PropertyRegistration property1( customType1, "prop-name", PROPERTY_REGISTRATION_MAX_INDEX + 1, Property::BOOLEAN, &SetProperty, &GetProperty );
833     tet_result( TET_FAIL );
834   }
835   catch ( DaliException& e )
836   {
837     DALI_TEST_ASSERT( e, "( index >= PROPERTY_REGISTRATION_START_INDEX ) && ( index <= PROPERTY_REGISTRATION_MAX_INDEX )", TEST_LOCATION );
838   }
839   END_TEST;
840 }
841
842 int UtcDaliPropertyRegistrationFunctions(void)
843 {
844   TestApplication application;
845   int propertyIndex = PROPERTY_REGISTRATION_START_INDEX + 10;
846
847   // Attempt to register a property without a setter
848   try
849   {
850     PropertyRegistration property1( customType1, "prop-name", propertyIndex++, Property::BOOLEAN, NULL, &GetProperty );
851     tet_result( TET_PASS );
852   }
853   catch ( DaliException& e )
854   {
855     tet_result( TET_FAIL );
856   }
857
858   // Attempt to register a property without a getter
859   try
860   {
861     PropertyRegistration property1( customType1, "prop-name", propertyIndex++, Property::BOOLEAN, NULL, NULL );
862     tet_result( TET_FAIL );
863   }
864   catch ( DaliException& e )
865   {
866     DALI_TEST_ASSERT( e, "! \"GetProperty", TEST_LOCATION );
867   }
868   END_TEST;
869 }
870
871 int UtcDaliPropertyRegistrationAddSameIndex(void)
872 {
873   TestApplication application;
874   int propertyIndex = PROPERTY_REGISTRATION_START_INDEX + 100;
875
876   // Add one property with a valid property index
877   PropertyRegistration property1( customType1, "prop-name", propertyIndex, Property::BOOLEAN, &SetProperty, &GetProperty );
878
879   // Attempt to add another property with the same index
880   try
881   {
882     PropertyRegistration property2( customType1, "prop-name-2", propertyIndex, Property::BOOLEAN, &SetProperty, &GetProperty );
883   }
884   catch ( DaliException& e )
885   {
886     DALI_TEST_ASSERT( e, "! \"Property index already added", TEST_LOCATION );
887   }
888   END_TEST;
889 }
890
891 int UtcDaliPropertyRegistrationPropertyWritable(void)
892 {
893   TestApplication application;
894   int propertyIndex1 = PROPERTY_REGISTRATION_START_INDEX + 200;
895   int propertyIndex2 = PROPERTY_REGISTRATION_START_INDEX + 201;
896
897   // Add two properties, one with SetProperty, one without
898   PropertyRegistration property1( customType1, "prop-name-readwrite", propertyIndex1, Property::BOOLEAN, &SetProperty, &GetProperty );
899   PropertyRegistration property2( customType1, "prop-name-readonly",  propertyIndex2, Property::BOOLEAN, NULL, &GetProperty );
900
901   // Create custom-actor
902   TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( typeid(MyTestCustomActor) );
903   DALI_TEST_CHECK( typeInfo );
904   BaseHandle handle = typeInfo.CreateInstance();
905   DALI_TEST_CHECK( handle );
906   Actor customActor = Actor::DownCast( handle );
907   DALI_TEST_CHECK( customActor );
908
909   // Check whether properties are writable
910   DALI_TEST_CHECK(   customActor.IsPropertyWritable( propertyIndex1 ) );
911   DALI_TEST_CHECK( ! customActor.IsPropertyWritable( propertyIndex2 ) );
912   END_TEST;
913 }
914
915 int UtcDaliPropertyRegistrationPropertyAnimatable(void)
916 {
917   TestApplication application;
918   int propertyIndex = PROPERTY_REGISTRATION_START_INDEX + 400;
919
920   // These properties are not animatable
921   PropertyRegistration property1( customType1, "prop-name", propertyIndex, Property::BOOLEAN, &SetProperty, &GetProperty );
922
923   // Create custom-actor
924   TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( typeid(MyTestCustomActor) );
925   DALI_TEST_CHECK( typeInfo );
926   BaseHandle handle = typeInfo.CreateInstance();
927   DALI_TEST_CHECK( handle );
928   Actor customActor = Actor::DownCast( handle );
929   DALI_TEST_CHECK( customActor );
930
931   // Check if animatable
932   DALI_TEST_CHECK( ! customActor.IsPropertyAnimatable( propertyIndex ) );
933   END_TEST;
934 }
935
936 int UtcDaliPropertyRegistrationInvalidGetAndSet(void)
937 {
938   TestApplication application;
939   int propertyIndex = PROPERTY_REGISTRATION_START_INDEX + 2000;
940
941   // Create custom-actor
942   TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( typeid(MyTestCustomActor) );
943   DALI_TEST_CHECK( typeInfo );
944   BaseHandle handle = typeInfo.CreateInstance();
945   DALI_TEST_CHECK( handle );
946   Actor customActor = Actor::DownCast( handle );
947   DALI_TEST_CHECK( customActor );
948
949   // Try to set an index that hasn't been added
950   try
951   {
952     customActor.SetProperty( propertyIndex, true );
953     tet_result( TET_FAIL );
954   }
955   catch ( DaliException& e )
956   {
957     DALI_TEST_ASSERT( e, "! \"Cannot find property index", TEST_LOCATION );
958   }
959
960   // Try to get an index that hasn't been added
961   try
962   {
963     (void) customActor.GetProperty< bool >( propertyIndex );
964     tet_result( TET_FAIL );
965   }
966   catch ( DaliException& e )
967   {
968     DALI_TEST_ASSERT( e, "! \"Cannot find property index", TEST_LOCATION );
969   }
970   END_TEST;
971 }
972
973
974
975 int UtcDaliLongPressGestureDetectorTypeRegistry(void)
976 {
977   TestApplication application;
978
979   Actor actor = Actor::New();
980   actor.SetSize(100.0f, 100.0f);
981   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
982   Stage::GetCurrent().Add(actor);
983
984   // Register Type
985   TypeInfo type;
986   type = TypeRegistry::Get().GetTypeInfo( "LongPressGestureDetector" );
987   DALI_TEST_CHECK( type );
988   BaseHandle handle = type.CreateInstance();
989   DALI_TEST_CHECK( handle );
990   LongPressGestureDetector detector = LongPressGestureDetector::DownCast( handle );
991   DALI_TEST_CHECK( detector );
992
993   // Attach actor to detector
994   SignalData data;
995   GestureReceivedFunctor functor( data );
996   detector.Attach(actor);
997
998   // Connect to signal through type
999   handle.ConnectSignal( &application, LongPressGestureDetector::SIGNAL_LONG_PRESS_DETECTED, functor );
1000
1001   // Render and notify
1002   application.SendNotification();
1003   application.Render();
1004
1005   // Emit gesture
1006   application.ProcessEvent(GenerateLongPress(Gesture::Possible, 1u, Vector2(50.0f, 10.0f)));
1007   application.ProcessEvent(GenerateLongPress(Gesture::Started, 1u, Vector2(50.0f, 10.0f)));
1008   application.ProcessEvent(GenerateLongPress(Gesture::Finished, 1u, Vector2(50.0f, 10.0f)));
1009   DALI_TEST_EQUALS(true, data.voidFunctorCalled, TEST_LOCATION);
1010   END_TEST;
1011 }
1012
1013 int UtcDaliPanGestureDetectorTypeRegistry(void)
1014 {
1015   TestApplication application;
1016
1017   Actor actor = Actor::New();
1018   actor.SetSize(100.0f, 100.0f);
1019   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1020   Stage::GetCurrent().Add(actor);
1021
1022   // Register Type
1023   TypeInfo type;
1024   type = TypeRegistry::Get().GetTypeInfo( "PanGestureDetector" );
1025   DALI_TEST_CHECK( type );
1026   BaseHandle handle = type.CreateInstance();
1027   DALI_TEST_CHECK( handle );
1028   PanGestureDetector detector = PanGestureDetector::DownCast( handle );
1029   DALI_TEST_CHECK( detector );
1030
1031   // Attach actor to detector
1032   SignalData data;
1033   GestureReceivedFunctor functor( data );
1034   detector.Attach(actor);
1035
1036   // Connect to signal through type
1037   handle.ConnectSignal( &application, PanGestureDetector::SIGNAL_PAN_DETECTED, functor );
1038
1039   // Render and notify
1040   application.SendNotification();
1041   application.Render();
1042
1043   // Emit gesture
1044   application.ProcessEvent(GeneratePan(Gesture::Possible, Vector2(10.0f, 20.0f), Vector2(20.0f, 20.0f), 10));
1045   application.ProcessEvent(GeneratePan(Gesture::Started, Vector2(10.0f, 20.0f), Vector2(20.0f, 20.0f), 10));
1046   application.ProcessEvent(GeneratePan(Gesture::Finished, Vector2(10.0f, 20.0f), Vector2(20.0f, 20.0f), 10));
1047   DALI_TEST_EQUALS(true, data.voidFunctorCalled, TEST_LOCATION);
1048   END_TEST;
1049 }
1050
1051 int UtcDaliPinchGestureDetectorTypeRegistry(void)
1052 {
1053   TestApplication application;
1054
1055   Actor actor = Actor::New();
1056   actor.SetSize(100.0f, 100.0f);
1057   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1058   Stage::GetCurrent().Add(actor);
1059
1060   // Register Type
1061   TypeInfo type;
1062   type = TypeRegistry::Get().GetTypeInfo( "PinchGestureDetector" );
1063   DALI_TEST_CHECK( type );
1064   BaseHandle handle = type.CreateInstance();
1065   DALI_TEST_CHECK( handle );
1066   PinchGestureDetector detector = PinchGestureDetector::DownCast( handle );
1067   DALI_TEST_CHECK( detector );
1068
1069   // Attach actor to detector
1070   SignalData data;
1071   GestureReceivedFunctor functor( data );
1072   detector.Attach(actor);
1073
1074   // Connect to signal through type
1075   handle.ConnectSignal( &application, PinchGestureDetector::SIGNAL_PINCH_DETECTED, functor );
1076
1077   // Render and notify
1078   application.SendNotification();
1079   application.Render();
1080
1081   // Emit gesture
1082   application.ProcessEvent(GeneratePinch(Gesture::Started, 10.0f, 50.0f, Vector2(20.0f, 10.0f)));
1083   DALI_TEST_EQUALS(true, data.voidFunctorCalled, TEST_LOCATION);
1084   END_TEST;
1085 }
1086
1087 int UtcDaliTapGestureDetectorTypeRegistry(void)
1088 {
1089   TestApplication application;
1090
1091   Actor actor = Actor::New();
1092   actor.SetSize(100.0f, 100.0f);
1093   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1094   Stage::GetCurrent().Add(actor);
1095
1096   // Register Type
1097   TypeInfo type;
1098   type = TypeRegistry::Get().GetTypeInfo( "TapGestureDetector" );
1099   DALI_TEST_CHECK( type );
1100   BaseHandle handle = type.CreateInstance();
1101   DALI_TEST_CHECK( handle );
1102   TapGestureDetector detector = TapGestureDetector::DownCast( handle );
1103   DALI_TEST_CHECK( detector );
1104
1105   // Attach actor to detector
1106   SignalData data;
1107   GestureReceivedFunctor functor( data );
1108   detector.Attach(actor);
1109
1110   // Connect to signal through type
1111   handle.ConnectSignal( &application, TapGestureDetector::SIGNAL_TAP_DETECTED, functor );
1112
1113   // Render and notify
1114   application.SendNotification();
1115   application.Render();
1116
1117   // Emit gesture
1118   application.ProcessEvent(GenerateTap(Gesture::Possible, 1u, 1u, Vector2(50.0f, 10.0f)));
1119   application.ProcessEvent(GenerateTap(Gesture::Started, 1u, 1u, Vector2(50.0f, 10.0f)));
1120   DALI_TEST_EQUALS(true, data.voidFunctorCalled, TEST_LOCATION);
1121   END_TEST;
1122 }
1123
1124 int UtcDaliTypeRegistryNamedType(void)
1125 {
1126   TestApplication application;
1127   TypeRegistry typeRegistry = TypeRegistry::Get();
1128
1129   // Create a normal actor
1130   BaseHandle actorHandle = typeRegistry.GetTypeInfo( "Actor" ).CreateInstance();
1131   DALI_TEST_CHECK( actorHandle );
1132   Actor actor( Actor::DownCast( actorHandle ) );
1133   DALI_TEST_CHECK( actor );
1134   unsigned int actorPropertyCount( actor.GetPropertyCount() );
1135
1136   // Create Named Actor Type
1137   BaseHandle namedHandle = typeRegistry.GetTypeInfo( "MyNamedActor" ).CreateInstance();
1138   DALI_TEST_CHECK( namedHandle );
1139   Actor namedActor( Actor::DownCast( namedHandle ) );
1140   DALI_TEST_CHECK( namedActor );
1141   unsigned int namedActorPropertyCount( namedActor.GetPropertyCount() );
1142
1143   DALI_TEST_CHECK( namedActorPropertyCount > actorPropertyCount );
1144   END_TEST;
1145 }
1146