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