Update adaptor stub code in toolkit automated test to include new Adaptor APIs
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-singleton-service.cpp
1 /*
2  * Copyright (c) 2019 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 <toolkit-singleton-service.h>
19
20 #include <dali-toolkit/public-api/dali-toolkit-common.h>
21 #include <dali/public-api/object/base-object.h>
22 #include <dali/devel-api/common/map-wrapper.h>
23 #include <dali/public-api/signals/dali-signal.h>
24 #include <dali/integration-api/processor-interface.h>
25
26 namespace Dali
27 {
28
29
30 namespace Internal
31 {
32 namespace Adaptor
33 {
34
35 namespace
36 {
37 Dali::IntrusivePtr<SingletonService> gSingletonService;
38 } // unnamed namespace
39
40
41 class SingletonService : public Dali::BaseObject
42 {
43 public:
44
45   /**
46    * Create a SingletonService.
47    * This should only be called once by the Application class.
48    * @return A newly created SingletonService.
49    */
50   static Dali::SingletonService New()
51   {
52     DALI_ASSERT_ALWAYS( 0 && "SingletonService New method used");
53     gSingletonService = Dali::IntrusivePtr<SingletonService>( new SingletonService() );
54     return Dali::SingletonService( gSingletonService.Get() );
55   }
56
57   /**
58    * @copydoc Dali::SingletonService::Get()
59    */
60   static Dali::SingletonService Get()
61   {
62     Dali::SingletonService singletonService;
63     if ( !gSingletonService )
64     {
65       gSingletonService = Dali::IntrusivePtr<SingletonService>( new SingletonService() );
66     }
67     return Dali::SingletonService( gSingletonService.Get() );
68   }
69
70   /**
71    * @copydoc Dali::SingletonService::Register()
72    */
73   void Register( const std::type_info& info, BaseHandle singleton )
74   {
75     if( singleton )
76     {
77       mSingletonContainer.insert( SingletonPair( info.name(), singleton ) );
78
79       Integration::Processor* processor = dynamic_cast<Integration::Processor*>( &singleton.GetBaseObject() );
80       if( processor )
81       {
82         Integration::Core& core = mTestApplication->GetCore();
83         core.RegisterProcessor( *processor );
84       }
85     }
86   }
87
88   /**
89    * @copydoc Dali::SingletonService::UnregisterAll()
90    */
91   void UnregisterAll()
92   {
93     mSingletonContainer.clear();
94   }
95
96   /**
97    * @copydoc Dali::SingletonService::GetSingleton()
98    */
99   BaseHandle GetSingleton( const std::type_info& info ) const
100   {
101     BaseHandle object;
102
103     SingletonConstIter iter = mSingletonContainer.find(info.name());
104     if( iter != mSingletonContainer.end() )
105     {
106       object = ( *iter ).second;
107     }
108     return object;
109   }
110
111   void SetApplication( Dali::TestApplication& testApplication )
112   {
113     mTestApplication = &testApplication;
114   }
115
116 private:
117
118   /**
119    * Private Constructor
120    * @see SingletonService::New()
121    */
122   SingletonService()
123   : mSingletonContainer()
124   {
125     // Can only have one instance of SingletonService
126     DALI_ASSERT_ALWAYS( !gSingletonService && "Only one instance of SingletonService is allowed");
127     gSingletonService = this;
128   }
129
130   /**
131    * Virtual Destructor
132    */
133   virtual ~SingletonService()
134   {
135     gSingletonService = 0;
136   }
137
138   // Undefined
139   SingletonService( const SingletonService& );
140   SingletonService& operator=( SingletonService& );
141
142 private:
143   typedef std::pair<std::string, BaseHandle> SingletonPair;
144   typedef std::map<std::string, BaseHandle>  SingletonContainer;
145   typedef SingletonContainer::const_iterator SingletonConstIter;
146
147   SingletonContainer mSingletonContainer; ///< The container to look up singleton by its type name
148   TestApplication* mTestApplication;
149 };
150
151 } // namespace Adaptor
152 } // namespace Internal
153
154 ////////////////////////////////////////////////////////////////////////////////////////////////////
155
156 inline Internal::Adaptor::SingletonService& GetImplementation(Dali::SingletonService& player)
157 {
158   DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
159   BaseObject& handle = player.GetBaseObject();
160   return static_cast<Internal::Adaptor::SingletonService&>(handle);
161 }
162
163 inline const Internal::Adaptor::SingletonService& GetImplementation(const Dali::SingletonService& player)
164 {
165   DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
166   const BaseObject& handle = player.GetBaseObject();
167   return static_cast<const Internal::Adaptor::SingletonService&>(handle);
168 }
169
170 SingletonService::SingletonService()
171 {
172 }
173
174 SingletonService SingletonService::New()
175 {
176   return Internal::Adaptor::SingletonService::New();
177 }
178
179 SingletonService SingletonService::Get()
180 {
181   return Internal::Adaptor::SingletonService::Get();
182 }
183
184 SingletonService::~SingletonService()
185 {
186 }
187
188 void SingletonService::Register( const std::type_info& info, BaseHandle singleton )
189 {
190   GetImplementation( *this ).Register( info, singleton );
191 }
192
193 void SingletonService::UnregisterAll()
194 {
195   GetImplementation( *this ).UnregisterAll();
196 }
197
198 BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
199 {
200   return GetImplementation( *this ).GetSingleton( info );
201 }
202
203 SingletonService::SingletonService( Internal::Adaptor::SingletonService* singletonService )
204 : BaseHandle( singletonService )
205 {
206 }
207
208 } // namespace Dali
209
210
211 namespace Test
212 {
213
214 void SetApplication( Dali::SingletonService singletonService, TestApplication& testApplication )
215 {
216   GetImplementation( singletonService ).SetApplication( testApplication );
217 }
218
219 } // Test