Add post processor
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-trace-call-stack.h
1 #ifndef TEST_TRACE_CALL_STACK_H
2 #define TEST_TRACE_CALL_STACK_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 #include <map>
22 #include <sstream>
23 #include <string>
24 #include <vector>
25
26 namespace Dali
27 {
28 template<typename T>
29 std::string ToString(const T& x)
30 {
31   return "undefined";
32 }
33
34 std::string ToString(int x);
35 std::string ToString(unsigned int x);
36 std::string ToString(float x);
37
38 /**
39  * Helper class to track method calls in the abstraction and search for them in test cases
40  */
41 class TraceCallStack
42 {
43 public:
44   /// Typedef for passing and storing named parameters
45   class NamedParams
46   {
47   public:
48     struct NameValue
49     {
50       std::string        parameterName;
51       std::ostringstream value;
52       NameValue(std::string pname, std::string aValue)
53       : parameterName(pname),
54         value(aValue)
55       {
56       }
57       NameValue(const NameValue& rhs)
58       : parameterName(rhs.parameterName),
59         value(rhs.value.str())
60       {
61       }
62       NameValue& operator=(const NameValue& rhs)
63       {
64         if(this != &rhs)
65         {
66           this->parameterName = rhs.parameterName;
67           this->value.str(rhs.value.str());
68         }
69         return *this;
70       }
71
72       bool operator==(const NameValue& rhs)
73       {
74         return !parameterName.compare(rhs.parameterName) && !value.str().compare(rhs.value.str());
75       }
76
77       bool operator==(int match) const;
78     };
79
80     auto find(const std::string& param) const
81     {
82       auto iter = mParams.begin();
83       for(; iter != mParams.end(); ++iter)
84       {
85         if(!iter->parameterName.compare(param))
86         {
87           break;
88         }
89       }
90       return iter;
91     }
92
93     auto begin() const
94     {
95       return mParams.begin();
96     }
97     auto end() const
98     {
99       return mParams.end();
100     }
101
102     std::ostringstream& operator[](std::string name)
103     {
104       auto iter = mParams.begin();
105       for(; iter != mParams.end(); ++iter)
106       {
107         if(!iter->parameterName.compare(name))
108         {
109           break;
110         }
111       }
112
113       if(iter != mParams.end())
114       {
115         return iter->value;
116       }
117       else
118       {
119         mParams.push_back(NameValue(name, ""));
120         return mParams.back().value;
121       }
122     }
123
124     std::string str() const
125     {
126       std::ostringstream out;
127       bool               first = true;
128       for(auto& elem : mParams)
129       {
130         out << (first ? "" : " ") << elem.parameterName << ": " << elem.value.str();
131         first = false;
132       }
133       return out.str();
134     }
135     std::vector<NameValue> mParams{};
136   };
137
138   /**
139    * Constructor
140    */
141   TraceCallStack(bool logging, std::string prefix);
142
143   TraceCallStack(const TraceCallStack&) = delete;
144   TraceCallStack(TraceCallStack&&)      = delete;
145
146   /**
147    * Destructor
148    */
149   ~TraceCallStack();
150
151   /**
152    * Turn on / off tracing
153    */
154   void Enable(bool enable);
155
156   bool IsEnabled();
157
158   void EnableLogging(bool enable);
159
160   /**
161    * Push a call onto the stack if the trace is active
162    * @param[in] method The name of the method
163    * @param[in] params A comma separated list of parameter values
164    */
165   void PushCall(std::string method, std::string params);
166
167   /**
168    * Push a call onto the stack if the trace is active
169    * @param[in] method The name of the method
170    * @param[in] params A comma separated list of parameter values
171    * @param[in] altParams A map of named parameter values
172    */
173   void PushCall(std::string method, std::string params, const NamedParams& altParams);
174
175   /**
176    * Search for a method in the stack
177    * @param[in] method The name of the method
178    * @return true if the method was in the stack
179    */
180   bool FindMethod(std::string method) const;
181
182   /**
183    * Search for a method in the stack and return its parameters if found
184    * @param[in] method The name of the method
185    * @param[out] params of the method
186    * @return true if the method was in the stack
187    */
188   bool FindMethodAndGetParameters(std::string method, std::string& params) const;
189
190   /**
191    * Count how many times a method was called
192    * @param[in] method The name of the method
193    * @return The number of times it was called
194    */
195   int CountMethod(std::string method) const;
196
197   /**
198    * Search for a method in the stack with the given parameter list
199    * @param[in] method The name of the method
200    * @param[in] params A comma separated list of parameter values
201    * @return true if the method was in the stack
202    */
203   bool FindMethodAndParams(std::string method, std::string params) const;
204
205   /**
206    * Search for a method in the stack with the given parameter list
207    * @param[in] method The name of the method
208    * @param[in] params A map of named parameters to test for
209    * @return true if the method was in the stack
210    */
211   bool FindMethodAndParams(std::string method, const NamedParams& params) const;
212
213   /**
214    * Search for a method in the stack with the given parameter list.
215    * The search is done from a given index.
216    * This allows the order of methods and parameters to be checked.
217    * @param[in] method The name of the method
218    * @param[in] params A comma separated list of parameter values
219    * @param[in/out] startIndex The method index to start looking from.
220    *                This is updated if a method is found so subsequent
221    *                calls can search for methods occuring after this one.
222    * @return True if the method was in the stack
223    */
224   bool FindMethodAndParamsFromStartIndex(std::string method, std::string params, size_t& startIndex) const;
225
226   /**
227    * Search for a method in the stack with the given parameter list
228    * @param[in] method The name of the method
229    * @param[in] params A comma separated list of parameter values
230    * @return index in the stack where the method was found or -1 otherwise
231    */
232   int FindIndexFromMethodAndParams(std::string method, std::string params) const;
233
234   /**
235    * Search for a method in the stack with the given parameter list
236    * @param[in] method The name of the method
237    * @param[in] params A map of named parameter values to match
238    * @return index in the stack where the method was found or -1 otherwise
239    */
240   int FindIndexFromMethodAndParams(std::string method, const NamedParams& params) const;
241
242   /**
243    * Search for the most recent occurrence of the method with the given (partial) parameters.
244    * @param[in] method The name of the method
245    * @param[in] params A map of named parameter values to match
246    * @return The full named parameters of the matching call.
247    */
248   const NamedParams* FindLastMatch(std::string method, const TraceCallStack::NamedParams& params) const;
249
250   /**
251    * Test if the given method and parameters are at a given index in the stack
252    * @param[in] index Index in the call stack
253    * @param[in] method Name of method to test
254    * @param[in] params A comma separated list of parameter values to test
255    */
256   bool TestMethodAndParams(int index, std::string method, std::string params) const;
257
258   /**
259    * Reset the call stack
260    */
261   void Reset();
262
263   /**
264    * Method to display contents of the TraceCallStack.
265    * @return A string containing a list of function calls and parameters (may contain newline characters)
266    */
267   std::string GetTraceString()
268   {
269     std::stringstream traceStream;
270     std::size_t       functionCount = mCallStack.size();
271     for(std::size_t i = 0; i < functionCount; ++i)
272     {
273       Dali::TraceCallStack::FunctionCall functionCall = mCallStack[i];
274       traceStream << "StackTrace: Index:" << i << ",  Function:" << functionCall.method << ",  ParamList:" << functionCall.paramList << std::endl;
275     }
276
277     return traceStream.str();
278   }
279
280 public:
281   bool        mTraceActive{false}; ///< True if the trace is active
282   bool        mLogging{false};     ///< True if the trace is logged to stdout
283   std::string mPrefix;
284
285   struct FunctionCall
286   {
287     std::string method;
288     std::string paramList;
289     NamedParams namedParams;
290     FunctionCall(const std::string& aMethod, const std::string& aParamList)
291     : method(aMethod),
292       paramList(aParamList)
293     {
294     }
295     FunctionCall(const std::string& aMethod, const std::string& aParamList, const NamedParams& altParams)
296     : method(aMethod),
297       paramList(aParamList),
298       namedParams(altParams)
299     {
300     }
301   };
302
303   std::vector<FunctionCall> mCallStack; ///< The call stack
304 };
305
306 } // namespace Dali
307
308 #endif // TEST_TRACE_CALL_STACK_H