Updated visuals to use VisualRenderer
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-trace-call-stack.cpp
1 /*
2  * Copyright (c) 2022 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 <iostream>
20 #include <sstream>
21 #include "dali-test-suite-utils.h"
22
23 namespace Dali
24 {
25 std::string ToString(int x)
26 {
27   std::stringstream out;
28   out << x;
29   return out.str();
30 }
31
32 std::string ToString(unsigned int x)
33 {
34   std::stringstream out;
35   out << x;
36   return out.str();
37 }
38
39 std::string ToString(float x)
40 {
41   std::stringstream out;
42   out << x;
43   return out.str();
44 }
45
46 /**
47  * Constructor
48  */
49 TraceCallStack::TraceCallStack(bool logging, std::string prefix)
50 : mTraceActive(false),
51   mLogging(logging),
52   mPrefix(prefix)
53 {
54 }
55
56 /**
57  * Destructor
58  */
59 TraceCallStack::~TraceCallStack()
60 {
61 }
62
63 /**
64  * Turn on / off tracing
65  */
66 void TraceCallStack::Enable(bool enable)
67 {
68   mTraceActive = enable;
69 }
70
71 bool TraceCallStack::IsEnabled()
72 {
73   return mTraceActive;
74 }
75
76 void TraceCallStack::EnableLogging(bool enablelogging)
77 {
78   mLogging = enablelogging;
79 }
80
81 /**
82  * Push a call onto the stack if the trace is active
83  * @param[in] method The name of the method
84  * @param[in] params A comma separated list of parameter values
85  */
86 void TraceCallStack::PushCall(std::string method, std::string params)
87 {
88   if(mTraceActive)
89   {
90     FunctionCall stackFrame(method, params);
91     mCallStack.push_back(stackFrame);
92   }
93   if(mLogging)
94   {
95     fprintf(stderr, "%s%s(%s)\n", mPrefix.c_str(), method.c_str(), params.c_str());
96   }
97 }
98
99 void TraceCallStack::PushCall(std::string method, std::string params, const TraceCallStack::NamedParams& altParams)
100 {
101   if(mTraceActive)
102   {
103     FunctionCall stackFrame(method, params, altParams);
104     mCallStack.push_back(stackFrame);
105   }
106   if(mLogging)
107   {
108     fprintf(stderr, "%s%s(%s)\n", mPrefix.c_str(), method.c_str(), params.c_str());
109   }
110 }
111
112 /**
113  * Search for a method in the stack
114  * @param[in] method The name of the method
115  * @return true if the method was in the stack
116  */
117 bool TraceCallStack::FindMethod(std::string method) const
118 {
119   bool found = false;
120   for(size_t i = 0; i < mCallStack.size(); i++)
121   {
122     if(0 == mCallStack[i].method.compare(method))
123     {
124       found = true;
125       break;
126     }
127   }
128   if(!found)
129   {
130     fprintf(stderr, "Search for %s failed\n", method.c_str());
131   }
132   return found;
133 }
134
135 bool TraceCallStack::FindMethodAndGetParameters(std::string method, std::string& params) const
136 {
137   bool found = false;
138   for(size_t i = 0; i < mCallStack.size(); i++)
139   {
140     if(0 == mCallStack[i].method.compare(method))
141     {
142       found  = true;
143       params = mCallStack[i].paramList;
144       break;
145     }
146   }
147   if(!found)
148   {
149     fprintf(stderr, "Search for %s() failed\n", method.c_str());
150   }
151   return found;
152 }
153
154 bool TraceCallStack::FindMethodAndGetParameters(std::string method, NamedParams& params) const
155 {
156   bool found = false;
157   for(size_t i = 0; i < mCallStack.size(); i++)
158   {
159     if(0 == mCallStack[i].method.compare(method))
160     {
161       found  = true;
162       params = mCallStack[i].namedParams;
163       break;
164     }
165   }
166   if(!found)
167   {
168     fprintf(stderr, "Search for %s() failed\n", method.c_str());
169   }
170   return found;
171 }
172
173 int TraceCallStack::CountMethod(std::string method) const
174 {
175   int numCalls = 0;
176   for(size_t i = 0; i < mCallStack.size(); i++)
177   {
178     if(0 == mCallStack[i].method.compare(method))
179     {
180       numCalls++;
181     }
182   }
183   return numCalls;
184 }
185
186 /**
187  * Search for a method in the stack with the given parameter list
188  * @param[in] method The name of the method
189  * @param[in] params A comma separated list of parameter values
190  * @return true if the method was in the stack
191  */
192 bool TraceCallStack::FindMethodAndParams(std::string method, std::string params) const
193 {
194   return FindIndexFromMethodAndParams(method, params) > -1;
195 }
196
197 bool TraceCallStack::FindMethodAndParams(std::string method, const NamedParams& params) const
198 {
199   return FindIndexFromMethodAndParams(method, params) > -1;
200 }
201
202 bool TraceCallStack::FindMethodAndParamsFromStartIndex(std::string method, std::string params, size_t& startIndex) const
203 {
204   for(size_t i = startIndex; i < mCallStack.size(); ++i)
205   {
206     if((mCallStack[i].method.compare(method) == 0) && (mCallStack[i].paramList.compare(params) == 0))
207     {
208       startIndex = i;
209       return true;
210     }
211   }
212   return false;
213 }
214
215 /**
216  * Search for a method in the stack with the given parameter list
217  * @param[in] method The name of the method
218  * @param[in] params A comma separated list of parameter values
219  * @return index in the stack where the method was found or -1 otherwise
220  */
221 int32_t TraceCallStack::FindIndexFromMethodAndParams(std::string method, std::string params) const
222 {
223   int32_t index = -1;
224   for(size_t i = 0; i < mCallStack.size(); i++)
225   {
226     if(0 == mCallStack[i].method.compare(method) && 0 == mCallStack[i].paramList.compare(params))
227     {
228       index = static_cast<int32_t>(i);
229       break;
230     }
231   }
232   if(index == -1)
233   {
234     fprintf(stderr, "Search for %s(%s) failed\n", method.c_str(), params.c_str());
235   }
236   return index;
237 }
238
239 int TraceCallStack::FindIndexFromMethodAndParams(std::string method, const TraceCallStack::NamedParams& params) const
240 {
241   int32_t index = -1;
242   for(size_t i = 0; i < mCallStack.size(); i++)
243   {
244     if(0 == mCallStack[i].method.compare(method))
245     {
246       // Test each of the passed in parameters:
247       bool match = true;
248
249       for(auto iter = params.mParams.begin(); iter != params.mParams.end(); ++iter)
250       {
251         auto        paramIter = mCallStack[i].namedParams.find(iter->parameterName);
252         std::string value     = paramIter->value.str();
253         std::string iValue    = iter->value.str();
254
255         if(paramIter == mCallStack[i].namedParams.end() || value.compare(iValue))
256         {
257           match = false;
258           break;
259         }
260       }
261       if(match == true)
262       {
263         index = static_cast<int32_t>(i);
264         break;
265       }
266     }
267   }
268
269   if(index == -1)
270   {
271     fprintf(stderr, "Search for %s(%s) failed\n", method.c_str(), params.str().c_str());
272   }
273
274   return index;
275 }
276
277 const TraceCallStack::NamedParams* TraceCallStack::FindLastMatch(std::string method, const TraceCallStack::NamedParams& params) const
278 {
279   int index = -1;
280
281   if(mCallStack.size() > 0)
282   {
283     for(index = static_cast<int>(mCallStack.size() - 1); index >= 0; --index)
284     {
285       if(0 == mCallStack[index].method.compare(method))
286       {
287         // Test each of the passed in parameters:
288         bool match = true;
289
290         for(auto iter = params.mParams.begin(); iter != params.mParams.end(); ++iter)
291         {
292           auto        paramIter = mCallStack[index].namedParams.find(iter->parameterName);
293           std::string value     = paramIter->value.str();
294           std::string iValue    = iter->value.str();
295
296           if(paramIter == mCallStack[index].namedParams.end() || value.compare(iValue))
297           {
298             match = false;
299             break;
300           }
301         }
302         if(match == true)
303         {
304           break;
305         }
306       }
307     }
308   }
309   if(index >= 0)
310   {
311     return &mCallStack[index].namedParams;
312   }
313   return nullptr;
314 }
315
316 /**
317  * Test if the given method and parameters are at a given index in the stack
318  * @param[in] index Index in the call stack
319  * @param[in] method Name of method to test
320  * @param[in] params A comma separated list of parameter values to test
321  */
322 bool TraceCallStack::TestMethodAndParams(int index, std::string method, std::string params) const
323 {
324   return (0 == mCallStack[index].method.compare(method) && 0 == mCallStack[index].paramList.compare(params));
325 }
326
327 /**
328  * Reset the call stack
329  */
330 void TraceCallStack::Reset()
331 {
332   mCallStack.clear();
333 }
334
335 bool TraceCallStack::NamedParams::NameValue::operator==(int match) const
336 {
337   std::ostringstream matchStr;
338   matchStr << match;
339   std::string valueStr = value.str();
340   bool        retval   = !valueStr.compare(matchStr.str());
341   if(!retval)
342   {
343     tet_printf("Comparing parameter \"%s\": %s with %s failed\n", parameterName.c_str(), value.str().c_str(), matchStr.str().c_str());
344   }
345   return retval;
346 }
347
348 } // namespace Dali