Improved pan gesture prediction
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-unmanaged / utc-Dali-CustomActor.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
20 #include <stdlib.h>
21 #include <dali/public-api/dali-core.h>
22
23 #include <dali/integration-api/events/touch-event-integ.h>
24 #include <dali/integration-api/events/hover-event-integ.h>
25 #include <dali/integration-api/events/mouse-wheel-event-integ.h>
26 #include <dali/integration-api/events/key-event-integ.h>
27
28 #include <dali-test-suite-utils.h>
29
30 using namespace Dali;
31
32 namespace
33 {
34 std::vector< std::string > MasterCallStack;
35 }
36
37 // TypeRegistry needs custom actor Implementations to have the same name (namespaces are ignored so we use one here)
38 namespace Impl
39 {
40
41 struct TestCustomActor : public CustomActorImpl
42 {
43   /**
44    * Constructor
45    */
46   TestCustomActor()
47   : CustomActorImpl( true ), // requires touch
48     mDaliProperty( Property::INVALID_INDEX ),
49     mSizeSet( Vector3::ZERO ),
50     mTargetSize( Vector3::ZERO )
51   {
52     SetRequiresMouseWheelEvents(true);
53   }
54
55   /**
56    * Destructor
57    */
58   virtual ~TestCustomActor()
59   {
60   }
61
62   void Initialize( const char* name = NULL )
63   {
64     mDaliProperty = Self().RegisterProperty( "Dali", std::string("no"), Property::READ_WRITE);
65
66     OnInitialize( name );
67   }
68
69   virtual void OnInitialize( const char* name ) {}
70
71   /**
72    * Resets the call stack
73    */
74   void ResetCallStack()
75   {
76     mSizeSet = Vector3();
77     mTargetSize = Vector3();
78     mMethodsCalled.clear();
79   }
80
81   void AddToCallStacks( const char* method )
82   {
83     mMethodsCalled.push_back( method );
84
85     // Combine Actor name with method string
86     std::string nameAndMethod( Self().GetName() );
87     if ( 0 == nameAndMethod.size() )
88     {
89       nameAndMethod = "Unknown: ";
90     }
91     else
92     {
93       nameAndMethod += ": ";
94     }
95     nameAndMethod += method;
96
97     MasterCallStack.push_back( nameAndMethod );
98   }
99
100   // From CustomActorImpl
101   virtual void OnStageConnection()
102   {
103     AddToCallStacks("OnStageConnection");
104   }
105   virtual void OnStageDisconnection()
106   {
107     AddToCallStacks("OnStageDisconnection");
108   }
109   virtual void OnChildAdd(Actor& child)
110   {
111     AddToCallStacks("OnChildAdd");
112   }
113   virtual void OnChildRemove(Actor& child)
114   {
115     AddToCallStacks("OnChildRemove");
116   }
117   virtual void OnPropertySet( Property::Index index, Property::Value propertyValue )
118   {
119     AddToCallStacks("OnPropertySet");
120   }
121   virtual void OnSizeSet(const Vector3& targetSize)
122   {
123     mSizeSet = targetSize;
124     AddToCallStacks("OnSizeSet");
125   }
126   virtual void OnSizeAnimation(Animation& animation, const Vector3& targetSize)
127   {
128     mTargetSize = targetSize;
129     AddToCallStacks("OnSizeAnimation");
130   }
131   virtual bool OnTouchEvent(const TouchEvent& event)
132   {
133     AddToCallStacks("OnTouchEvent");
134     return true;
135   }
136   virtual bool OnHoverEvent(const HoverEvent& event)
137   {
138     AddToCallStacks("OnHoverEvent");
139     return true;
140   }
141   virtual bool OnMouseWheelEvent(const MouseWheelEvent& event)
142   {
143     AddToCallStacks("OnMouseWheelEvent");
144     return true;
145   }
146   virtual bool OnKeyEvent(const KeyEvent& event)
147   {
148     AddToCallStacks("OnKeyEvent");
149     return true;
150   }
151   virtual void OnKeyInputFocusGained()
152   {
153     AddToCallStacks("OnKeyInputFocusGained");
154   }
155   virtual void OnKeyInputFocusLost()
156   {
157     AddToCallStacks("OnKeyInputFocusLost");
158   }
159   virtual Actor GetChildByAlias(const std::string& actorAlias)
160   {
161     AddToCallStacks("GetChildByAlias");
162
163     if ("found" == actorAlias)
164     {
165       return Actor::New();
166     }
167     else
168     {
169       return Actor();
170     }
171   }
172
173   virtual Vector3 GetNaturalSize()
174   {
175     return Vector3( 0.0f, 0.0f, 0.0f );
176   }
177
178   void SetDaliProperty(std::string s)
179   {
180     Self().SetProperty(mDaliProperty, s) ;
181   }
182
183   Property::Index mDaliProperty;
184   std::vector< std::string > mMethodsCalled;
185   Vector3 mSizeSet;
186   Vector3 mTargetSize;
187 };
188 } // Namespace Impl
189
190
191 namespace
192 {
193 /**
194  * Test custom actor handle
195  */
196 class TestCustomActor : public CustomActor
197 {
198 public:
199
200   static TestCustomActor New()
201   {
202     Impl::TestCustomActor* impl = new Impl::TestCustomActor;
203     TestCustomActor custom( *impl ); // takes ownership
204
205     impl->Initialize();
206
207     return custom;
208   }
209
210   virtual ~TestCustomActor()
211   {
212   }
213
214   Impl::TestCustomActor& GetImpl()
215   {
216     return static_cast<Impl::TestCustomActor&>(GetImplementation());
217   }
218
219   std::vector< std::string >& GetMethodsCalled()
220   {
221     return GetImpl().mMethodsCalled;
222   }
223
224   void ResetCallStack()
225   {
226     GetImpl().ResetCallStack();
227   }
228
229   void SetDaliProperty(std::string s)
230   {
231     GetImpl().SetDaliProperty(s);
232   }
233
234   Vector3 GetSize()
235   {
236     return GetImpl().mSizeSet;
237   }
238
239   Vector3 GetTargetSize()
240   {
241     return GetImpl().mTargetSize;
242   }
243
244 private:
245
246   TestCustomActor( Impl::TestCustomActor& impl ) : CustomActor( impl )
247   {
248   }
249 };
250
251
252
253 using namespace Dali;
254
255 BaseHandle CreateActor()
256 {
257   return TestCustomActor::New();
258 }
259
260 Dali::TypeRegistration mType( typeid(TestCustomActor), typeid(Dali::CustomActor), CreateActor );
261
262 }
263
264
265 int UtcDaliCustomActorDoAction(void)
266 {
267   TestApplication application;
268   tet_infoline("Testing Dali::CustomActor::DoAction()");
269
270   TestCustomActor custom = TestCustomActor::New();
271
272   BaseHandle customActorObject = custom;
273
274   DALI_TEST_CHECK(customActorObject);
275
276   std::vector<Property::Value> attributes;
277
278   // Check that an invalid command is not performed
279   DALI_TEST_CHECK(customActorObject.DoAction("invalidCommand", attributes) == false);
280
281   // Check that the custom actor is visible
282   custom.SetVisible(true);
283   DALI_TEST_CHECK(custom.IsVisible() == true);
284
285   // Check the custom actor performed an action to hide itself
286   DALI_TEST_CHECK(customActorObject.DoAction("hide", attributes) == true);
287
288   // flush the queue and render once
289   application.SendNotification();
290   application.Render();
291
292   // Check that the custom actor is now invisible
293   DALI_TEST_CHECK(custom.IsVisible() == false);
294
295   // Check the custom actor performed an action to show itself
296   DALI_TEST_CHECK(customActorObject.DoAction("show", attributes) == true);
297
298   // flush the queue and render once
299   application.SendNotification();
300   application.Render();
301
302   // Check that the custom actor is now visible
303   DALI_TEST_CHECK(custom.IsVisible() == true);
304   END_TEST;
305 }