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