[dali_1.0.10] Merge branch 'tizen'
[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/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
28 using namespace Dali;
29
30
31 namespace
32 {
33
34 // Stores data that is populated in the callback and will be read by the Test cases
35 struct SignalData
36 {
37   SignalData()
38   : functorCalled( false ),
39     voidFunctorCalled( false ),
40     receivedGesture( Gesture::Clear ),
41     pressedActor()
42   {}
43
44   void Reset()
45   {
46     functorCalled = false;
47     voidFunctorCalled = false;
48
49     receivedGesture.numberOfTouches = 0u;
50     receivedGesture.screenPoint = Vector2(0.0f, 0.0f);
51     receivedGesture.localPoint = Vector2(0.0f, 0.0f);
52
53     pressedActor = NULL;
54   }
55
56   bool functorCalled;
57   bool voidFunctorCalled;
58   LongPressGesture receivedGesture;
59   Actor pressedActor;
60 };
61
62 // Functor that sets the data when called
63 struct GestureReceivedFunctor
64 {
65   GestureReceivedFunctor(SignalData& data) : signalData(data) { }
66
67   void operator()(Actor actor, LongPressGesture longPress)
68   {
69     signalData.functorCalled = true;
70     signalData.receivedGesture = longPress;
71     signalData.pressedActor = actor;
72   }
73
74   void operator()()
75   {
76     signalData.voidFunctorCalled = true;
77   }
78
79   SignalData& signalData;
80 };
81
82 // Generate a LongPressGestureEvent to send to Core
83 Integration::LongPressGestureEvent GenerateLongPress(
84     Gesture::State state,
85     unsigned int numberOfTouches,
86     Vector2 point)
87 {
88   Integration::LongPressGestureEvent longPress( state );
89
90   longPress.numberOfTouches = numberOfTouches;
91   longPress.point = point;
92
93   return longPress;
94 }
95
96 // Generate a PanGestureEvent to send to Core
97 Integration::PanGestureEvent GeneratePan(
98     Gesture::State state,
99     Vector2 previousPosition,
100     Vector2 currentPosition,
101     unsigned long timeDelta,
102     unsigned int numberOfTouches = 1,
103     unsigned int time = 1u)
104 {
105   Integration::PanGestureEvent pan(state);
106
107   pan.previousPosition = previousPosition;
108   pan.currentPosition = currentPosition;
109   pan.timeDelta = timeDelta;
110   pan.numberOfTouches = numberOfTouches;
111   pan.time = time;
112
113   return pan;
114 }
115 // Generate a PinchGestureEvent to send to Core
116 Integration::PinchGestureEvent GeneratePinch(
117     Gesture::State state,
118     float scale,
119     float speed,
120     Vector2 centerpoint)
121 {
122   Integration::PinchGestureEvent pinch(state);
123
124   pinch.scale = scale;
125   pinch.speed = speed;
126   pinch.centerPoint = centerpoint;
127
128   return pinch;
129 }
130 // Generate a TapGestureEvent to send to Core
131 Integration::TapGestureEvent GenerateTap(
132     Gesture::State state,
133     unsigned int numberOfTaps,
134     unsigned int numberOfTouches,
135     Vector2 point)
136 {
137   Integration::TapGestureEvent tap( state );
138
139   tap.numberOfTaps = numberOfTaps;
140   tap.numberOfTouches = numberOfTouches;
141   tap.point = point;
142
143   return tap;
144 }
145
146 //
147 // Create function as Init function called
148 //
149 static bool CreateCustomInitCalled = false;
150 BaseHandle CreateCustomInit(void)
151 {
152   CreateCustomInitCalled = true;
153   return BaseHandle();
154 }
155
156 static bool CreateCustomNamedInitCalled = false;
157 BaseHandle CreateCustomNamedInit(void)
158 {
159   CreateCustomNamedInitCalled = true;
160   return BaseHandle();
161 }
162
163 const std::string scriptedName("PopupStyle");
164 static TypeRegistration scriptedType( scriptedName, typeid(Dali::CustomActor), CreateCustomNamedInit );
165
166 // Property Registration
167 bool setPropertyCalled = false;
168 bool getPropertyCalled = false;
169 void SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
170 {
171   setPropertyCalled = true;
172 }
173 Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex )
174 {
175   getPropertyCalled = true;
176   return Property::Value( true );
177 }
178
179
180
181 /*******************************************************************************
182  *
183  * Custom Actor
184  *
185  ******************************************************************************/
186 namespace Impl
187 {
188 struct MyTestCustomActor : public CustomActorImpl
189 {
190   typedef SignalV2< void ()> SignalType;
191   typedef SignalV2< void (float)> SignalTypeFloat;
192
193   MyTestCustomActor() : CustomActorImpl( true ) // requires touch
194   { }
195
196   virtual ~MyTestCustomActor()
197   { }
198
199   void ResetCallStack()
200   {
201   }
202
203   // From CustomActorImpl
204   virtual void OnStageConnection()
205   {
206   }
207   virtual void OnStageDisconnection()
208   {
209   }
210   virtual void OnChildAdd(Actor& child)
211   {
212   }
213   virtual void OnChildRemove(Actor& child)
214   {
215   }
216   virtual void OnSizeSet(const Vector3& targetSize)
217   {
218   }
219   virtual void OnSizeAnimation(Animation& animation, const Vector3& targetSize)
220   {
221   }
222   virtual bool OnTouchEvent(const TouchEvent& event)
223   {
224     return true;
225   }
226   virtual bool OnMouseWheelEvent(const MouseWheelEvent& event)
227   {
228     return true;
229   }
230   virtual bool OnKeyEvent(const KeyEvent& event)
231   {
232     return true;
233   }
234   virtual void OnKeyInputFocusGained()
235   {
236   }
237   virtual void OnKeyInputFocusLost()
238   {
239   }
240   virtual Actor GetChildByAlias(const std::string& actorAlias)
241   {
242     return Actor::New();
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 SignalV2< void ()> SignalType;
262   typedef SignalV2< 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   type = registry.GetTypeInfo( "LightActor" );
522   DALI_TEST_CHECK( type );
523   LightActor la = LightActor::DownCast(type.CreateInstance());
524   DALI_TEST_CHECK( la );
525   Stage::GetCurrent().Add( la );
526   application.Render();
527
528   // animations
529   type = registry.GetTypeInfo( "Animation" );
530   DALI_TEST_CHECK( type );
531   Animation an = Animation::DownCast(type.CreateInstance());
532   DALI_TEST_CHECK( an );
533   an.Play();
534   application.Render();
535
536   //
537   type = registry.GetTypeInfo( "ShaderEffect" );
538   DALI_TEST_CHECK( type );
539   ShaderEffect ef = ShaderEffect::DownCast(type.CreateInstance());
540   DALI_TEST_CHECK( ef );
541   application.Render();
542
543   END_TEST;
544 }
545
546 /*******************************************************************************
547  *
548  * Action through the base handle
549  *
550  ******************************************************************************/
551 int UtcDaliTypeRegistryActionViaBaseHandle(void)
552 {
553   TestApplication application;
554
555   TypeInfo type;
556
557   type = TypeRegistry::Get().GetTypeInfo( "Actor" );
558   DALI_TEST_CHECK( type );
559
560   BaseHandle hdl = type.CreateInstance();
561   DALI_TEST_CHECK( hdl );
562
563   Actor a = Actor::DownCast(hdl);
564   DALI_TEST_CHECK( a );
565
566   a.SetVisible(false);
567
568   application.SendNotification();
569   application.Render(0);
570   DALI_TEST_CHECK(!a.IsVisible());
571
572   std::vector<Property::Value> attributes;
573
574   DALI_TEST_CHECK(hdl.DoAction(Actor::ACTION_SHOW, attributes));
575
576   application.SendNotification();
577   application.Render(0);
578   DALI_TEST_CHECK(a.IsVisible());
579
580   DALI_TEST_CHECK(!hdl.DoAction("unknown-action", attributes));
581   END_TEST;
582 }
583
584 int UtcDaliTypeRegistryNames(void)
585 {
586   TestApplication application;
587
588   TypeInfo type;
589
590   TypeRegistry::NameContainer names = TypeRegistry::Get().GetTypeNames();
591
592   for(TypeRegistry::NameContainer::iterator iter = names.begin();
593       iter != names.end(); ++iter)
594   {
595     type = TypeRegistry::Get().GetTypeInfo( *iter );
596     DALI_TEST_CHECK( type );
597   }
598
599   END_TEST;
600 }
601
602 // Check named and typeid are equivalent
603 int UtcDaliTypeRegistryNameEquivalence(void)
604 {
605   TypeInfo named_type = TypeRegistry::Get().GetTypeInfo( "TextActor" );
606   TypeInfo typeinfo_type = TypeRegistry::Get().GetTypeInfo( typeid(Dali::TextActor) );
607
608   DALI_TEST_CHECK( named_type );
609   DALI_TEST_CHECK( typeinfo_type );
610
611   DALI_TEST_CHECK( named_type == typeinfo_type );
612
613   DALI_TEST_CHECK( named_type.GetName() == typeinfo_type.GetName() );
614   DALI_TEST_CHECK( named_type.GetBaseName() == typeinfo_type.GetBaseName() );
615
616   END_TEST;
617 }
618
619
620 int UtcDaliTypeRegistryCustomActor(void)
621 {
622   ResetFunctorCounts();
623
624   TestApplication application;
625
626   TypeInfo type = TypeRegistry::Get().GetTypeInfo( "MyTestCustomActor" );
627   DALI_TEST_CHECK( type );
628
629   TypeInfo baseType = TypeRegistry::Get().GetTypeInfo( "CustomActor" );
630   DALI_TEST_CHECK( baseType );
631
632   BaseHandle handle = type.CreateInstance();
633   DALI_TEST_CHECK( handle );
634
635   MyTestCustomActor customHandle = MyTestCustomActor::DownCast( handle );
636   DALI_TEST_CHECK( customHandle );
637
638   TypeInfo::NameContainer names;
639   type.GetActions(names);
640   TypeInfo::NameContainer baseNames;
641   baseType.GetActions(baseNames);
642   DALI_TEST_EQUALS( names.size(), TEST_ACTION_COUNT + baseNames.size(), TEST_LOCATION );
643
644   names.clear();
645   type.GetSignals(names);
646
647   baseNames.clear();
648   baseType.GetSignals(baseNames);
649
650   DALI_TEST_EQUALS( names.size(), TEST_SIGNAL_COUNT + baseNames.size(), TEST_LOCATION );
651
652   {
653     TestConnectionTracker tracker;
654
655     bool connected = handle.ConnectSignal( &tracker, "sig1", CustomTestFunctor() );
656     DALI_TEST_EQUALS( connected, true, TEST_LOCATION );
657     DALI_TEST_CHECK( lastSignalConnectionCustom == "sig1" );
658     DALI_TEST_EQUALS( CustomTestFunctor::mTotalInstanceCount, 2/*temporary copy + FunctorDelegate copy*/, TEST_LOCATION );
659     DALI_TEST_EQUALS( CustomTestFunctor::mCurrentInstanceCount, 1, TEST_LOCATION );
660
661     DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 0, TEST_LOCATION );
662     customHandle.GetCustomSignal().Emit();
663     DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 1, TEST_LOCATION );
664     DALI_TEST_EQUALS( CustomTestFunctor::mTotalInstanceCount, 2/*temporary copy + FunctorDelegate copy*/, TEST_LOCATION );
665     DALI_TEST_EQUALS( CustomTestFunctor::mCurrentInstanceCount, 1, TEST_LOCATION );
666   }
667   // tracker should automatically disconnect here
668   DALI_TEST_EQUALS( CustomTestFunctor::mTotalInstanceCount, 2/*temporary copy + FunctorDelegate copy*/, TEST_LOCATION );
669   DALI_TEST_EQUALS( CustomTestFunctor::mCurrentInstanceCount, 0, TEST_LOCATION );
670
671   // Test that functor is disconnected
672   DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 1, TEST_LOCATION );
673   customHandle.GetCustomSignal().Emit();
674   DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 1/*not incremented*/, TEST_LOCATION );
675   DALI_TEST_EQUALS( CustomTestFunctor::mTotalInstanceCount, 2/*temporary copy + FunctorDelegate copy*/, TEST_LOCATION );
676   DALI_TEST_EQUALS( CustomTestFunctor::mCurrentInstanceCount, 0, TEST_LOCATION );
677
678   std::vector<Property::Value> attributes;
679   handle.DoAction("act1", attributes);
680   DALI_TEST_CHECK( lastActionCustom == "act1" );
681   END_TEST;
682 }
683
684 int UtcDaliTypeRegistryCustomSignalFailure(void)
685 {
686   // Test what happens when signal connnector (DoConnectSignalFailure method) returns false
687
688   ResetFunctorCounts();
689
690   TestApplication application;
691
692   TypeInfo type = TypeRegistry::Get().GetTypeInfo( "MyTestCustomActor" );
693   DALI_TEST_CHECK( type );
694
695   TypeInfo baseType = TypeRegistry::Get().GetTypeInfo( "CustomActor" );
696   DALI_TEST_CHECK( baseType );
697
698   BaseHandle handle = type.CreateInstance();
699   DALI_TEST_CHECK( handle );
700
701   MyTestCustomActor customHandle = MyTestCustomActor::DownCast( handle );
702   DALI_TEST_CHECK( customHandle );
703
704   TypeInfo::NameContainer names;
705   TypeInfo::NameContainer baseNames;
706
707   type.GetActions(names);
708   baseType.GetActions(baseNames);
709
710   DALI_TEST_EQUALS( names.size(), TEST_ACTION_COUNT + baseNames.size(), TEST_LOCATION );
711
712   names.clear();
713   baseNames.clear();
714
715   type.GetSignals(names);
716   baseType.GetSignals(baseNames);
717
718   DALI_TEST_EQUALS( names.size(), TEST_SIGNAL_COUNT + baseNames.size(), TEST_LOCATION );
719
720   {
721     TestConnectionTracker tracker;
722
723     bool connected = handle.ConnectSignal( &tracker, "sig2", CustomTestFunctor() );
724     DALI_TEST_EQUALS( connected, false/*This is supposed to fail*/, TEST_LOCATION );
725     DALI_TEST_CHECK( lastSignalConnectionCustom == "failed" );
726     DALI_TEST_EQUALS( CustomTestFunctor::mTotalInstanceCount, 2/*temporary copy + FunctorDelegate copy*/, TEST_LOCATION );
727     DALI_TEST_EQUALS( CustomTestFunctor::mCurrentInstanceCount, 0/*deleted along with FunctorDelegate*/, TEST_LOCATION );
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   }
734   // tracker should have nothing to disconnect here
735
736   // Should be a NOOP
737   DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 0, TEST_LOCATION );
738   customHandle.GetCustomSignal().Emit();
739   DALI_TEST_EQUALS( CustomTestFunctor::mCallbackCount, 0/*never called*/, TEST_LOCATION );
740   END_TEST;
741 }
742
743
744
745 int UtcDaliTypeRegistryInitFunctions(void)
746 {
747   TestApplication application;
748
749   DALI_TEST_CHECK( "MyTestCustomActor2" == customTypeInit.RegisteredName() );
750
751   DALI_TEST_CHECK( true == CreateCustomInitCalled );
752   TypeInfo type = TypeRegistry::Get().GetTypeInfo( "MyTestCustomActor2" );
753   DALI_TEST_CHECK( type );
754   END_TEST;
755 }
756
757
758
759
760 int UtcDaliTypeRegistryNameInitFunctions(void)
761 {
762   TestApplication application;
763
764   DALI_TEST_CHECK( scriptedName == scriptedType.RegisteredName() );
765
766   TypeInfo baseType = TypeRegistry::Get().GetTypeInfo( scriptedName );
767   DALI_TEST_CHECK( baseType );
768
769   BaseHandle handle = baseType.CreateInstance();
770
771   DALI_TEST_CHECK( true == CreateCustomNamedInitCalled );
772   TypeInfo type = TypeRegistry::Get().GetTypeInfo( scriptedName );
773   DALI_TEST_CHECK( type );
774   END_TEST;
775 }
776
777
778 int UtcDaliPropertyRegistration(void)
779 {
780   TestApplication application;
781   TypeRegistry typeRegistry = TypeRegistry::Get();
782
783   // Check property count before property registration
784   TypeInfo typeInfo = typeRegistry.GetTypeInfo( typeid(MyTestCustomActor) );
785   DALI_TEST_CHECK( typeInfo );
786   BaseHandle handle = typeInfo.CreateInstance();
787   DALI_TEST_CHECK( handle );
788   Actor customActor = Actor::DownCast( handle );
789   DALI_TEST_CHECK( customActor );
790   unsigned int initialPropertyCount( customActor.GetPropertyCount() );
791
792   std::string propertyName( "prop-1" );
793   int propertyIndex( PROPERTY_REGISTRATION_START_INDEX );
794   Property::Type propertyType( Property::BOOLEAN );
795   PropertyRegistration property1( customType1, propertyName, propertyIndex, propertyType, &SetProperty, &GetProperty );
796
797   // Check property count after registration
798   unsigned int postRegistrationPropertyCount( customActor.GetPropertyCount() );
799   DALI_TEST_EQUALS( initialPropertyCount + 1u, postRegistrationPropertyCount, TEST_LOCATION );
800
801   // Add custom property and check property count
802   customActor.RegisterProperty( "custom-prop-1", true );
803   unsigned int customPropertyCount( customActor.GetPropertyCount() );
804   DALI_TEST_EQUALS( postRegistrationPropertyCount + 1u, customPropertyCount, TEST_LOCATION );
805
806   // Set the property, ensure SetProperty called
807   DALI_TEST_CHECK( !setPropertyCalled );
808   customActor.SetProperty( propertyIndex, false );
809   DALI_TEST_CHECK( setPropertyCalled );
810
811   // Get the property, ensure GetProperty called
812   DALI_TEST_CHECK( !getPropertyCalled );
813   (void)customActor.GetProperty< bool >( propertyIndex );
814   DALI_TEST_CHECK( getPropertyCalled );
815
816   // Check the property name
817   DALI_TEST_EQUALS( customActor.GetPropertyName( propertyIndex ), propertyName, TEST_LOCATION );
818   DALI_TEST_EQUALS( typeInfo.GetPropertyName( propertyIndex ), propertyName, TEST_LOCATION );
819
820   // Check the property index
821   DALI_TEST_EQUALS( customActor.GetPropertyIndex( propertyName ), propertyIndex, TEST_LOCATION );
822
823   // Check the property type
824   DALI_TEST_EQUALS( customActor.GetPropertyType( propertyIndex ), propertyType, TEST_LOCATION );
825
826   // Check property count of type-info is 1
827   Property::IndexContainer indices;
828   typeInfo.GetPropertyIndices( indices );
829   DALI_TEST_EQUALS( indices.size(), 1u, TEST_LOCATION );
830
831   // Ensure indices returned from actor and customActor differ by two
832   Actor actor = Actor::New();
833   actor.GetPropertyIndices( indices );
834   unsigned int actorIndices = indices.size();
835   customActor.GetPropertyIndices( indices );
836   unsigned int customActorIndices = indices.size();
837   DALI_TEST_EQUALS( actorIndices + 2u, customActorIndices, TEST_LOCATION ); // Custom property + registered property
838   END_TEST;
839 }
840
841 int UtcDaliPropertyRegistrationIndexOutOfBounds(void)
842 {
843   TestApplication application;
844   TypeRegistry typeRegistry = TypeRegistry::Get();
845
846   // Attempt to register a property type out-of-bounds index (less than)
847   try
848   {
849     PropertyRegistration property1( customType1, "prop-name", PROPERTY_REGISTRATION_START_INDEX - 1, Property::BOOLEAN, &SetProperty, &GetProperty );
850     tet_result( TET_FAIL );
851   }
852   catch ( DaliException& e )
853   {
854     DALI_TEST_ASSERT( e, "( index >= PROPERTY_REGISTRATION_START_INDEX ) && ( index <= PROPERTY_REGISTRATION_MAX_INDEX )", TEST_LOCATION );
855   }
856
857   // Attempt to register a property type out-of-bounds index (greater than)
858   try
859   {
860     PropertyRegistration property1( customType1, "prop-name", PROPERTY_REGISTRATION_MAX_INDEX + 1, Property::BOOLEAN, &SetProperty, &GetProperty );
861     tet_result( TET_FAIL );
862   }
863   catch ( DaliException& e )
864   {
865     DALI_TEST_ASSERT( e, "( index >= PROPERTY_REGISTRATION_START_INDEX ) && ( index <= PROPERTY_REGISTRATION_MAX_INDEX )", TEST_LOCATION );
866   }
867   END_TEST;
868 }
869
870 int UtcDaliPropertyRegistrationFunctions(void)
871 {
872   TestApplication application;
873   int propertyIndex = PROPERTY_REGISTRATION_START_INDEX + 10;
874
875   // Attempt to register a property without a setter
876   try
877   {
878     PropertyRegistration property1( customType1, "prop-name", propertyIndex++, Property::BOOLEAN, NULL, &GetProperty );
879     tet_result( TET_PASS );
880   }
881   catch ( DaliException& e )
882   {
883     tet_result( TET_FAIL );
884   }
885
886   // Attempt to register a property without a getter
887   try
888   {
889     PropertyRegistration property1( customType1, "prop-name", propertyIndex++, Property::BOOLEAN, NULL, NULL );
890     tet_result( TET_FAIL );
891   }
892   catch ( DaliException& e )
893   {
894     DALI_TEST_ASSERT( e, "! \"GetProperty", TEST_LOCATION );
895   }
896   END_TEST;
897 }
898
899 int UtcDaliPropertyRegistrationAddSameIndex(void)
900 {
901   TestApplication application;
902   int propertyIndex = PROPERTY_REGISTRATION_START_INDEX + 100;
903
904   // Add one property with a valid property index
905   PropertyRegistration property1( customType1, "prop-name", propertyIndex, Property::BOOLEAN, &SetProperty, &GetProperty );
906
907   // Attempt to add another property with the same index
908   try
909   {
910     PropertyRegistration property2( customType1, "prop-name-2", propertyIndex, Property::BOOLEAN, &SetProperty, &GetProperty );
911   }
912   catch ( DaliException& e )
913   {
914     DALI_TEST_ASSERT( e, "! \"Property index already added", TEST_LOCATION );
915   }
916   END_TEST;
917 }
918
919 int UtcDaliPropertyRegistrationPropertyWritable(void)
920 {
921   TestApplication application;
922   int propertyIndex1 = PROPERTY_REGISTRATION_START_INDEX + 200;
923   int propertyIndex2 = PROPERTY_REGISTRATION_START_INDEX + 201;
924
925   // Add two properties, one with SetProperty, one without
926   PropertyRegistration property1( customType1, "prop-name-readwrite", propertyIndex1, Property::BOOLEAN, &SetProperty, &GetProperty );
927   PropertyRegistration property2( customType1, "prop-name-readonly",  propertyIndex2, Property::BOOLEAN, NULL, &GetProperty );
928
929   // Create custom-actor
930   TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( typeid(MyTestCustomActor) );
931   DALI_TEST_CHECK( typeInfo );
932   BaseHandle handle = typeInfo.CreateInstance();
933   DALI_TEST_CHECK( handle );
934   Actor customActor = Actor::DownCast( handle );
935   DALI_TEST_CHECK( customActor );
936
937   // Check whether properties are writable
938   DALI_TEST_CHECK(   customActor.IsPropertyWritable( propertyIndex1 ) );
939   DALI_TEST_CHECK( ! customActor.IsPropertyWritable( propertyIndex2 ) );
940   END_TEST;
941 }
942
943 int UtcDaliPropertyRegistrationPropertyAnimatable(void)
944 {
945   TestApplication application;
946   int propertyIndex = PROPERTY_REGISTRATION_START_INDEX + 400;
947
948   // These properties are not animatable
949   PropertyRegistration property1( customType1, "prop-name", propertyIndex, Property::BOOLEAN, &SetProperty, &GetProperty );
950
951   // Create custom-actor
952   TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( typeid(MyTestCustomActor) );
953   DALI_TEST_CHECK( typeInfo );
954   BaseHandle handle = typeInfo.CreateInstance();
955   DALI_TEST_CHECK( handle );
956   Actor customActor = Actor::DownCast( handle );
957   DALI_TEST_CHECK( customActor );
958
959   // Check if animatable
960   DALI_TEST_CHECK( ! customActor.IsPropertyAnimatable( propertyIndex ) );
961   END_TEST;
962 }
963
964 int UtcDaliPropertyRegistrationInvalidGetAndSet(void)
965 {
966   TestApplication application;
967   int propertyIndex = PROPERTY_REGISTRATION_START_INDEX + 2000;
968
969   // Create custom-actor
970   TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( typeid(MyTestCustomActor) );
971   DALI_TEST_CHECK( typeInfo );
972   BaseHandle handle = typeInfo.CreateInstance();
973   DALI_TEST_CHECK( handle );
974   Actor customActor = Actor::DownCast( handle );
975   DALI_TEST_CHECK( customActor );
976
977   // Try to set an index that hasn't been added
978   try
979   {
980     customActor.SetProperty( propertyIndex, true );
981     tet_result( TET_FAIL );
982   }
983   catch ( DaliException& e )
984   {
985     DALI_TEST_ASSERT( e, "! \"Cannot find property index", TEST_LOCATION );
986   }
987
988   // Try to get an index that hasn't been added
989   try
990   {
991     (void) customActor.GetProperty< bool >( propertyIndex );
992     tet_result( TET_FAIL );
993   }
994   catch ( DaliException& e )
995   {
996     DALI_TEST_ASSERT( e, "! \"Cannot find property index", TEST_LOCATION );
997   }
998   END_TEST;
999 }
1000
1001
1002
1003 int UtcDaliLongPressGestureDetectorTypeRegistry(void)
1004 {
1005   TestApplication application;
1006
1007   Actor actor = Actor::New();
1008   actor.SetSize(100.0f, 100.0f);
1009   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1010   Stage::GetCurrent().Add(actor);
1011
1012   // Register Type
1013   TypeInfo type;
1014   type = TypeRegistry::Get().GetTypeInfo( "LongPressGestureDetector" );
1015   DALI_TEST_CHECK( type );
1016   BaseHandle handle = type.CreateInstance();
1017   DALI_TEST_CHECK( handle );
1018   LongPressGestureDetector detector = LongPressGestureDetector::DownCast( handle );
1019   DALI_TEST_CHECK( detector );
1020
1021   // Attach actor to detector
1022   SignalData data;
1023   GestureReceivedFunctor functor( data );
1024   detector.Attach(actor);
1025
1026   // Connect to signal through type
1027   handle.ConnectSignal( &application, LongPressGestureDetector::SIGNAL_LONG_PRESS_DETECTED, functor );
1028
1029   // Render and notify
1030   application.SendNotification();
1031   application.Render();
1032
1033   // Emit gesture
1034   application.ProcessEvent(GenerateLongPress(Gesture::Possible, 1u, Vector2(50.0f, 10.0f)));
1035   application.ProcessEvent(GenerateLongPress(Gesture::Started, 1u, Vector2(50.0f, 10.0f)));
1036   application.ProcessEvent(GenerateLongPress(Gesture::Finished, 1u, Vector2(50.0f, 10.0f)));
1037   DALI_TEST_EQUALS(true, data.voidFunctorCalled, TEST_LOCATION);
1038   END_TEST;
1039 }
1040
1041 int UtcDaliPanGestureDetectorTypeRegistry(void)
1042 {
1043   TestApplication application;
1044
1045   Actor actor = Actor::New();
1046   actor.SetSize(100.0f, 100.0f);
1047   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1048   Stage::GetCurrent().Add(actor);
1049
1050   // Register Type
1051   TypeInfo type;
1052   type = TypeRegistry::Get().GetTypeInfo( "PanGestureDetector" );
1053   DALI_TEST_CHECK( type );
1054   BaseHandle handle = type.CreateInstance();
1055   DALI_TEST_CHECK( handle );
1056   PanGestureDetector detector = PanGestureDetector::DownCast( handle );
1057   DALI_TEST_CHECK( detector );
1058
1059   // Attach actor to detector
1060   SignalData data;
1061   GestureReceivedFunctor functor( data );
1062   detector.Attach(actor);
1063
1064   // Connect to signal through type
1065   handle.ConnectSignal( &application, PanGestureDetector::SIGNAL_PAN_DETECTED, functor );
1066
1067   // Render and notify
1068   application.SendNotification();
1069   application.Render();
1070
1071   // Emit gesture
1072   application.ProcessEvent(GeneratePan(Gesture::Possible, Vector2(10.0f, 20.0f), Vector2(20.0f, 20.0f), 10));
1073   application.ProcessEvent(GeneratePan(Gesture::Started, Vector2(10.0f, 20.0f), Vector2(20.0f, 20.0f), 10));
1074   application.ProcessEvent(GeneratePan(Gesture::Finished, Vector2(10.0f, 20.0f), Vector2(20.0f, 20.0f), 10));
1075   DALI_TEST_EQUALS(true, data.voidFunctorCalled, TEST_LOCATION);
1076   END_TEST;
1077 }
1078
1079 int UtcDaliPinchGestureDetectorTypeRegistry(void)
1080 {
1081   TestApplication application;
1082
1083   Actor actor = Actor::New();
1084   actor.SetSize(100.0f, 100.0f);
1085   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1086   Stage::GetCurrent().Add(actor);
1087
1088   // Register Type
1089   TypeInfo type;
1090   type = TypeRegistry::Get().GetTypeInfo( "PinchGestureDetector" );
1091   DALI_TEST_CHECK( type );
1092   BaseHandle handle = type.CreateInstance();
1093   DALI_TEST_CHECK( handle );
1094   PinchGestureDetector detector = PinchGestureDetector::DownCast( handle );
1095   DALI_TEST_CHECK( detector );
1096
1097   // Attach actor to detector
1098   SignalData data;
1099   GestureReceivedFunctor functor( data );
1100   detector.Attach(actor);
1101
1102   // Connect to signal through type
1103   handle.ConnectSignal( &application, PinchGestureDetector::SIGNAL_PINCH_DETECTED, functor );
1104
1105   // Render and notify
1106   application.SendNotification();
1107   application.Render();
1108
1109   // Emit gesture
1110   application.ProcessEvent(GeneratePinch(Gesture::Started, 10.0f, 50.0f, Vector2(20.0f, 10.0f)));
1111   DALI_TEST_EQUALS(true, data.voidFunctorCalled, TEST_LOCATION);
1112   END_TEST;
1113 }
1114
1115 int UtcDaliTapGestureDetectorTypeRegistry(void)
1116 {
1117   TestApplication application;
1118
1119   Actor actor = Actor::New();
1120   actor.SetSize(100.0f, 100.0f);
1121   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1122   Stage::GetCurrent().Add(actor);
1123
1124   // Register Type
1125   TypeInfo type;
1126   type = TypeRegistry::Get().GetTypeInfo( "TapGestureDetector" );
1127   DALI_TEST_CHECK( type );
1128   BaseHandle handle = type.CreateInstance();
1129   DALI_TEST_CHECK( handle );
1130   TapGestureDetector detector = TapGestureDetector::DownCast( handle );
1131   DALI_TEST_CHECK( detector );
1132
1133   // Attach actor to detector
1134   SignalData data;
1135   GestureReceivedFunctor functor( data );
1136   detector.Attach(actor);
1137
1138   // Connect to signal through type
1139   handle.ConnectSignal( &application, TapGestureDetector::SIGNAL_TAP_DETECTED, functor );
1140
1141   // Render and notify
1142   application.SendNotification();
1143   application.Render();
1144
1145   // Emit gesture
1146   application.ProcessEvent(GenerateTap(Gesture::Possible, 1u, 1u, Vector2(50.0f, 10.0f)));
1147   application.ProcessEvent(GenerateTap(Gesture::Started, 1u, 1u, Vector2(50.0f, 10.0f)));
1148   DALI_TEST_EQUALS(true, data.voidFunctorCalled, TEST_LOCATION);
1149   END_TEST;
1150 }
1151
1152 int UtcDaliTypeRegistryNamedType(void)
1153 {
1154   TestApplication application;
1155   TypeRegistry typeRegistry = TypeRegistry::Get();
1156
1157   // Create a normal actor
1158   BaseHandle actorHandle = typeRegistry.GetTypeInfo( "Actor" ).CreateInstance();
1159   DALI_TEST_CHECK( actorHandle );
1160   Actor actor( Actor::DownCast( actorHandle ) );
1161   DALI_TEST_CHECK( actor );
1162   unsigned int actorPropertyCount( actor.GetPropertyCount() );
1163
1164   // Create Named Actor Type
1165   BaseHandle namedHandle = typeRegistry.GetTypeInfo( "MyNamedActor" ).CreateInstance();
1166   DALI_TEST_CHECK( namedHandle );
1167   Actor namedActor( Actor::DownCast( namedHandle ) );
1168   DALI_TEST_CHECK( namedActor );
1169   unsigned int namedActorPropertyCount( namedActor.GetPropertyCount() );
1170
1171   DALI_TEST_CHECK( namedActorPropertyCount > actorPropertyCount );
1172   END_TEST;
1173 }