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