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