Merge "C# CustomView Implementation (C++ wrappers, manual bindings, C# wrappers)...
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-trace-call-stack.cpp
1 /*
2  * Copyright (c) 2016 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 "test-trace-call-stack.h"
19 #include <sstream>
20
21 namespace Dali
22 {
23
24 std::string ToString(int x)
25 {
26   std::stringstream out;
27   out << x;
28   return out.str();
29 }
30
31 std::string ToString(unsigned int x)
32 {
33   std::stringstream out;
34   out << x;
35   return out.str();
36 }
37
38 std::string ToString(float x)
39 {
40   std::stringstream out;
41   out << x;
42   return out.str();
43 }
44
45 /**
46  * Constructor
47  */
48 TraceCallStack::TraceCallStack() : mTraceActive(false) { }
49
50 /**
51  * Destructor
52  */
53 TraceCallStack::~TraceCallStack() { }
54
55 /**
56  * Turn on / off tracing
57  */
58 void TraceCallStack::Enable(bool enable) { mTraceActive = enable; }
59
60 bool TraceCallStack::IsEnabled() { return mTraceActive; }
61
62 /**
63  * Push a call onto the stack if the trace is active
64  * @param[in] method The name of the method
65  * @param[in] params A comma separated list of parameter values
66  */
67 void TraceCallStack::PushCall(std::string method, std::string params)
68 {
69   if(mTraceActive)
70   {
71     FunctionCall stackFrame(method, params);
72     mCallStack.push_back( stackFrame );
73   }
74 }
75
76 void TraceCallStack::PushCall(std::string method, std::string params, const TraceCallStack::NamedParams& altParams)
77 {
78   if(mTraceActive)
79   {
80     FunctionCall stackFrame(method, params, altParams);
81     mCallStack.push_back( stackFrame );
82   }
83 }
84
85 /**
86  * Search for a method in the stack
87  * @param[in] method The name of the method
88  * @return true if the method was in the stack
89  */
90 bool TraceCallStack::FindMethod(std::string method) const
91 {
92   bool found = false;
93   for( size_t i=0; i < mCallStack.size(); i++ )
94   {
95     if( 0 == mCallStack[i].method.compare(method) )
96     {
97       found = true;
98       break;
99     }
100   }
101   return found;
102 }
103
104 bool TraceCallStack::FindMethodAndGetParameters(std::string method, std::string& params ) const
105 {
106   bool found = false;
107   for( size_t i=0; i < mCallStack.size(); i++ )
108   {
109     if( 0 == mCallStack[i].method.compare(method) )
110     {
111       found = true;
112       params = mCallStack[i].paramList;
113       break;
114     }
115   }
116   return found;
117 }
118
119 int TraceCallStack::CountMethod(std::string method) const
120 {
121   int numCalls = 0;
122   for( size_t i=0; i < mCallStack.size(); i++ )
123   {
124     if( 0 == mCallStack[i].method.compare(method) )
125     {
126       numCalls++;
127     }
128   }
129   return numCalls;
130 }
131
132 /**
133  * Search for a method in the stack with the given parameter list
134  * @param[in] method The name of the method
135  * @param[in] params A comma separated list of parameter values
136  * @return true if the method was in the stack
137  */
138 bool TraceCallStack::FindMethodAndParams(std::string method, std::string params) const
139 {
140   return FindIndexFromMethodAndParams( method, params ) > -1;
141 }
142
143 bool TraceCallStack::FindMethodAndParams(std::string method, const NamedParams& params) const
144 {
145   return FindIndexFromMethodAndParams( method, params ) > -1;
146 }
147
148 bool TraceCallStack::FindMethodAndParamsFromStartIndex( std::string method, std::string params, size_t& startIndex ) const
149 {
150   for( size_t i = startIndex; i < mCallStack.size(); ++i )
151   {
152     if( ( mCallStack[i].method.compare( method ) == 0 ) && ( mCallStack[i].paramList.compare( params ) == 0 ) )
153     {
154       startIndex = i;
155       return true;
156     }
157   }
158   return false;
159 }
160
161 /**
162  * Search for a method in the stack with the given parameter list
163  * @param[in] method The name of the method
164  * @param[in] params A comma separated list of parameter values
165  * @return index in the stack where the method was found or -1 otherwise
166  */
167 int TraceCallStack::FindIndexFromMethodAndParams(std::string method, std::string params) const
168 {
169   int index = -1;
170   for( size_t i=0; i < mCallStack.size(); i++ )
171   {
172     if( 0 == mCallStack[i].method.compare(method) && 0 == mCallStack[i].paramList.compare(params) )
173     {
174       index = i;
175       break;
176     }
177   }
178   return index;
179 }
180
181 int TraceCallStack::FindIndexFromMethodAndParams(std::string method, const TraceCallStack::NamedParams& params) const
182 {
183   int index = -1;
184   for( size_t i=0; i < mCallStack.size(); i++ )
185   {
186     if( 0 == mCallStack[i].method.compare(method) )
187     {
188       // Test each of the passed in parameters:
189       bool match = true;
190       for( NamedParams::const_iterator iter = params.begin() ; iter != params.end() ; ++iter )
191       {
192         NamedParams::const_iterator paramIter = mCallStack[i].namedParams.find(iter->first);
193         if( paramIter == params.end() || paramIter->second.compare(iter->second) != 0 )
194         {
195           match = false;
196           break;
197         }
198       }
199       if( match == true )
200       {
201         index = i;
202         break;
203       }
204     }
205   }
206   return index;
207 }
208
209
210 /**
211  * Test if the given method and parameters are at a given index in the stack
212  * @param[in] index Index in the call stack
213  * @param[in] method Name of method to test
214  * @param[in] params A comma separated list of parameter values to test
215  */
216 bool TraceCallStack::TestMethodAndParams(int index, std::string method, std::string params) const
217 {
218   return ( 0 == mCallStack[index].method.compare(method) && 0 == mCallStack[index].paramList.compare(params) );
219 }
220
221 /**
222  * Reset the call stack
223  */
224 void TraceCallStack::Reset()
225 {
226   mCallStack.clear();
227 }
228
229
230 } // namespace Dali