[4.0] Add VisualEventSignal to Control and a property to AnimatedVectorImageVisual
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / dummy-control.cpp
1 /*
2  * Copyright (c) 2017 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 "dummy-control.h"
19
20 #include <dali-toolkit/dali-toolkit.h>
21 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
22 #include <dali-toolkit/devel-api/controls/control-devel.h>
23
24 namespace Dali
25 {
26
27 namespace Toolkit
28 {
29
30 DummyControl::DummyControl()
31 {
32 }
33
34 DummyControl::DummyControl(const DummyControl& control)
35 : Control( control )
36 {
37 }
38
39 DummyControl::~DummyControl()
40 {
41 }
42
43 DummyControl DummyControl::DownCast( BaseHandle handle )
44 {
45   return Control::DownCast<DummyControl, DummyControlImpl>(handle);
46 }
47
48 DummyControl& DummyControl::operator=(const DummyControl& control)
49 {
50   Control::operator=( control );
51   return *this;
52 }
53
54 // Used to test signal connections
55 void DummyControlImpl::CustomSlot1( Actor actor )
56 {
57   mCustomSlot1Called = true;
58 }
59
60 namespace {
61
62 BaseHandle Create()
63 {
64   return DummyControlImpl::New();
65 }
66
67 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::DummyControl, Toolkit::Control, Create );
68 DALI_TYPE_REGISTRATION_END()
69
70 Dali::PropertyRegistration dummyControlVisualProperty01(
71   typeRegistration, "testVisual", Dali::Toolkit::DummyControl::Property::TEST_VISUAL, Dali::Property::MAP, &Dali::Toolkit::DummyControlImpl::SetProperty, &Dali::Toolkit::DummyControlImpl::GetProperty );
72
73 Dali::PropertyRegistration dummyControlVisualProperty02(
74   typeRegistration, "testVisual2", Dali::Toolkit::DummyControl::Property::TEST_VISUAL2, Dali::Property::MAP, &Dali::Toolkit::DummyControlImpl::SetProperty, &Dali::Toolkit::DummyControlImpl::GetProperty );
75
76 Dali::PropertyRegistration dummyControlVisualProperty03(
77   typeRegistration, "foregroundVisual", Dali::Toolkit::DummyControl::Property::FOREGROUND_VISUAL, Dali::Property::MAP, &Dali::Toolkit::DummyControlImpl::SetProperty, &Dali::Toolkit::DummyControlImpl::GetProperty );
78
79 Dali::PropertyRegistration dummyControlVisualProperty04(
80   typeRegistration, "focusVisual", Dali::Toolkit::DummyControl::Property::FOCUS_VISUAL, Dali::Property::MAP, &Dali::Toolkit::DummyControlImpl::SetProperty, &Dali::Toolkit::DummyControlImpl::GetProperty );
81
82 Dali::PropertyRegistration dummyControlVisualProperty05(
83   typeRegistration, "labelVisual", Dali::Toolkit::DummyControl::Property::LABEL_VISUAL, Dali::Property::MAP, &Dali::Toolkit::DummyControlImpl::SetProperty, &Dali::Toolkit::DummyControlImpl::GetProperty );
84
85 }
86
87 DummyControl DummyControlImpl::New()
88 {
89   IntrusivePtr< DummyControlImpl > impl = new DummyControlImpl;
90   DummyControl control( *impl );
91   impl->Initialize();
92   return control;
93 }
94
95 DummyControlImpl::DummyControlImpl()
96 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_HOVER_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
97   mCustomSlot1Called(false)
98 {
99 }
100
101 DummyControlImpl::~DummyControlImpl()
102 {
103 }
104
105 void DummyControlImpl::RegisterVisual( Property::Index index, Toolkit::Visual::Base visual )
106 {
107   DevelControl::RegisterVisual( *this, index, visual );
108
109   VisualIndices::iterator iter = std::find( mRegisteredVisualIndices.begin(), mRegisteredVisualIndices.end(), index );
110   if( iter == mRegisteredVisualIndices.end() )
111   {
112     mRegisteredVisualIndices.push_back(index);
113   }
114 }
115
116 void DummyControlImpl::RegisterVisual( Property::Index index, Toolkit::Visual::Base visual, bool enabled )
117 {
118   DevelControl::RegisterVisual( *this, index, visual, enabled );
119
120   VisualIndices::iterator iter = std::find( mRegisteredVisualIndices.begin(), mRegisteredVisualIndices.end(), index );
121   if( iter == mRegisteredVisualIndices.end() )
122   {
123     mRegisteredVisualIndices.push_back(index);
124   }
125 }
126
127 void DummyControlImpl::UnregisterVisual( Property::Index index )
128 {
129   DevelControl::UnregisterVisual( *this, index );
130
131   VisualIndices::iterator iter = std::find( mRegisteredVisualIndices.begin(), mRegisteredVisualIndices.end(), index );
132   if( iter != mRegisteredVisualIndices.end() )
133   {
134     mRegisteredVisualIndices.erase(iter);
135   }
136 }
137
138 Toolkit::Visual::Base DummyControlImpl::GetVisual( Property::Index index )
139 {
140   return DevelControl::GetVisual( *this, index );
141 }
142
143 void DummyControlImpl::EnableVisual( Property::Index index, bool enabled )
144 {
145   DevelControl::EnableVisual( *this, index, enabled );
146 }
147
148 bool DummyControlImpl::IsVisualEnabled( Property::Index index )
149 {
150   return DevelControl::IsVisualEnabled( *this, index );
151 }
152
153 Animation DummyControlImpl::CreateTransition( const Toolkit::TransitionData& transition )
154 {
155   return DevelControl::CreateTransition( *this, transition );
156 }
157
158 void DummyControlImpl::DoAction( Dali::Property::Index index, Dali::Property::Index action, const Dali::Property::Value attributes )
159 {
160   DummyControl control( *this );
161   DevelControl::DoAction(  control, index, action, attributes);
162 }
163
164 void DummyControlImpl::SetProperty( BaseObject* object, Dali::Property::Index index, const Dali::Property::Value& value )
165 {
166   Toolkit::DummyControl control = Toolkit::DummyControl::DownCast( Dali::BaseHandle( object ) );
167   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(control.GetImplementation());
168
169   switch(index)
170   {
171     case Toolkit::DummyControl::Property::TEST_VISUAL:
172     case Toolkit::DummyControl::Property::TEST_VISUAL2:
173     case Toolkit::DummyControl::Property::FOREGROUND_VISUAL:
174     case Toolkit::DummyControl::Property::FOCUS_VISUAL:
175     case Toolkit::DummyControl::Property::LABEL_VISUAL:
176     {
177       Property::Map* map = value.GetMap();
178       if( map != NULL )
179       {
180         VisualFactory visualFactory = VisualFactory::Get();
181         Visual::Base visual = visualFactory.CreateVisual(*map);
182         dummyImpl.RegisterVisual(index, visual);
183       }
184       break;
185     }
186   }
187 }
188
189 Property::Value DummyControlImpl::GetProperty( BaseObject* object, Dali::Property::Index propertyIndex )
190 {
191   Toolkit::DummyControl control = Toolkit::DummyControl::DownCast( Dali::BaseHandle( object ) );
192   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>( control.GetImplementation() );
193
194   Visual::Base visual = dummyImpl.GetVisual( propertyIndex );
195   Property::Map map;
196   if( visual )
197   {
198     visual.CreatePropertyMap( map );
199   }
200   Dali::Property::Value value = map;
201
202   return value;
203 }
204
205
206 Toolkit::DummyControl Impl::DummyControl::New()
207 {
208   IntrusivePtr< Toolkit::Impl::DummyControl > impl = new Toolkit::Impl::DummyControl;
209   Toolkit::DummyControl control( *impl );
210   impl->Initialize();
211   return control;
212 }
213
214
215 Impl::DummyControl::DummyControl()
216 : DummyControlImpl(),
217   initializeCalled(false),
218   activatedCalled(false),
219   onAccTouchedCalled(false),
220   onAccValueChangeCalled(false),
221   themeChangeCalled(false),
222   fontChangeCalled(false),
223   pinchCalled(false),
224   panCalled(false),
225   tapCalled(false),
226   longPressCalled(false),
227   stageConnectionCalled(false),
228   stageDisconnectionCalled(false),
229   childAddCalled(false),
230   childRemoveCalled(false),
231   sizeSetCalled(false),
232   sizeAnimationCalled(false),
233   touchEventCalled(false),
234   hoverEventCalled(false),
235   wheelEventCalled(false),
236   keyEventCalled(false),
237   keyInputFocusGained(false),
238   keyInputFocusLost(false)
239 {
240 }
241
242 Impl::DummyControl::~DummyControl() { }
243
244
245 void Impl::DummyControl::OnInitialize() { initializeCalled = true; }
246 bool Impl::DummyControl::OnAccessibilityActivated() { activatedCalled = true; return true; }
247 bool Impl::DummyControl::OnAccessibilityTouch(const TouchEvent& touchEvent) { onAccTouchedCalled = true; return true; }
248 bool Impl::DummyControl::OnAccessibilityValueChange( bool isIncrease )
249 {
250   onAccValueChangeCalled = true; return true;
251 }
252
253 void Impl::DummyControl::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
254 {
255   themeChangeCalled = change == StyleChange::THEME_CHANGE;
256   fontChangeCalled = change == StyleChange::DEFAULT_FONT_SIZE_CHANGE;
257 }
258 void Impl::DummyControl::OnPinch(const PinchGesture& pinch) { pinchCalled = true; }
259 void Impl::DummyControl::OnPan(const PanGesture& pan) { panCalled = true; }
260 void Impl::DummyControl::OnTap(const TapGesture& tap) { tapCalled = true; }
261 void Impl::DummyControl::OnLongPress(const LongPressGesture& longPress) { longPressCalled = true; }
262 void Impl::DummyControl::OnStageConnection( int depth ) { Control::OnStageConnection( depth ); stageConnectionCalled = true; }
263 void Impl::DummyControl::OnStageDisconnection() { stageDisconnectionCalled = true; Control::OnStageDisconnection(); }
264 void Impl::DummyControl::OnChildAdd(Actor& child) { childAddCalled = true; }
265 void Impl::DummyControl::OnChildRemove(Actor& child) { childRemoveCalled = true; }
266 void Impl::DummyControl::OnSizeSet(const Vector3& targetSize) { Control::OnSizeSet( targetSize ); sizeSetCalled = true; }
267 void Impl::DummyControl::OnSizeAnimation(Animation& animation, const Vector3& targetSize) { Control::OnSizeAnimation( animation, targetSize ); sizeAnimationCalled = true; }
268 bool Impl::DummyControl::OnTouchEvent(const TouchEvent& event) { touchEventCalled = true; return false; }
269 bool Impl::DummyControl::OnHoverEvent(const HoverEvent& event) { hoverEventCalled = true; return false; }
270 bool Impl::DummyControl::OnWheelEvent(const WheelEvent& event) { wheelEventCalled = true; return false; }
271 bool Impl::DummyControl::OnKeyEvent(const KeyEvent& event) { keyEventCalled = true; return false;}
272 void Impl::DummyControl::OnKeyInputFocusGained() { keyInputFocusGained = true; }
273 void Impl::DummyControl::OnKeyInputFocusLost() { keyInputFocusLost = true; }
274
275 void Impl::DummyControl::SetLayout( Property::Index visualIndex, Property::Map& map )
276 {
277   Property::Value value( map );
278   mLayouts[visualIndex] = value;
279 }
280
281 void Impl::DummyControl::OnRelayout( const Vector2& size, RelayoutContainer& container )
282 {
283   if ( mRelayoutCallback )
284   {
285     mRelayoutCallback( size );  // Execute callback if set
286   }
287
288   Property::Map emptyMap;
289
290   for( VisualIndices::iterator iter = mRegisteredVisualIndices.begin(); iter != mRegisteredVisualIndices.end() ; ++iter )
291   {
292     Visual::Base visual = GetVisual(*iter);
293     Property::Value value = mLayouts[*iter];
294     Property::Map* map = NULL;
295
296     if( value.GetType() != Property::NONE )
297     {
298       map = value.GetMap();
299     }
300     if( map == NULL )
301     {
302       map = &emptyMap;
303     }
304
305     visual.SetTransformAndSize( *map, size );
306   }
307 }
308
309 void Impl::DummyControl::SetRelayoutCallback( RelayoutCallbackFunc callback  )
310 {
311   mRelayoutCallback = callback;
312 }
313
314 Vector3 Impl::DummyControl::GetNaturalSize()
315 {
316   Vector2 currentSize;
317
318   for( auto elem : mRegisteredVisualIndices )
319   {
320     Vector2 naturalSize;
321     Visual::Base visual = GetVisual(elem);
322     visual.GetNaturalSize( naturalSize );
323     currentSize.width = std::max( naturalSize.width, currentSize.width );
324     currentSize.height = std::max( naturalSize.height, currentSize.height );
325   }
326
327   return Vector3( currentSize );
328 }
329
330
331
332 DummyControl DummyControl::New( bool override )
333 {
334   DummyControl control;
335
336   if (override)
337   {
338     control = Impl::DummyControl::New();
339   }
340   else
341   {
342     control = DummyControlImpl::New();
343   }
344
345   return control;
346 }
347
348 DummyControl::DummyControl( DummyControlImpl& implementation )
349 : Control( implementation )
350 {
351 }
352
353 DummyControl::DummyControl( Dali::Internal::CustomActor* internal )
354 : Control( internal )
355 {
356   VerifyCustomActorPointer<DummyControlImpl>(internal);
357 }
358
359 } // namespace Toolkit
360
361 } // namespace Dali