Syncing Test harness updates for Native Image
[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
78     auto find(const std::string& param) const
79     {
80       auto iter = mParams.begin();
81       for(; iter != mParams.end(); ++iter)
82       {
83         if(!iter->parameterName.compare(param))
84         {
85           break;
86         }
87       }
88       return iter;
89     }
90
91     auto begin() const
92     {
93       return mParams.begin();
94     }
95     auto end() const
96     {
97       return mParams.end();
98     }
99
100     std::ostringstream& operator[](std::string name)
101     {
102       auto iter = mParams.begin();
103       for(; iter != mParams.end(); ++iter)
104       {
105         if(!iter->parameterName.compare(name))
106         {
107           break;
108         }
109       }
110
111       if(iter != mParams.end())
112       {
113         return iter->value;
114       }
115       else
116       {
117         mParams.push_back(NameValue(name, ""));
118         return mParams.back().value;
119       }
120     }
121
122     std::string str() const
123     {
124       std::ostringstream out;
125       bool               first = true;
126       for(auto& elem : mParams)
127       {
128         out << (first ? "" : " ") << elem.parameterName << ": " << elem.value.str();
129         first = false;
130       }
131       return out.str();
132     }
133     std::vector<NameValue> mParams{};
134   };
135
136   /**
137    * Constructor
138    */
139   TraceCallStack(bool logging, std::string prefix);
140
141   TraceCallStack(const TraceCallStack&) = delete;
142   TraceCallStack(TraceCallStack&&)      = delete;
143
144   /**
145    * Destructor
146    */
147   ~TraceCallStack();
148
149   /**
150    * Turn on / off tracing
151    */
152   void Enable(bool enable);
153
154   bool IsEnabled();
155
156   void EnableLogging(bool enable);
157
158   /**
159    * Push a call onto the stack if the trace is active
160    * @param[in] method The name of the method
161    * @param[in] params A comma separated list of parameter values
162    */
163   void PushCall(std::string method, std::string params);
164
165   /**
166    * Push a call onto the stack if the trace is active
167    * @param[in] method The name of the method
168    * @param[in] params A comma separated list of parameter values
169    * @param[in] altParams A map of named parameter values
170    */
171   void PushCall(std::string method, std::string params, const NamedParams& altParams);
172
173   /**
174    * Search for a method in the stack
175    * @param[in] method The name of the method
176    * @return true if the method was in the stack
177    */
178   bool FindMethod(std::string method) const;
179
180   /**
181    * Search for a method in the stack and return its parameters if found
182    * @param[in] method The name of the method
183    * @param[out] params of the method
184    * @return true if the method was in the stack
185    */
186   bool FindMethodAndGetParameters(std::string method, std::string& params) const;
187
188   /**
189    * Count how many times a method was called
190    * @param[in] method The name of the method
191    * @return The number of times it was called
192    */
193   int CountMethod(std::string method) const;
194
195   /**
196    * Search for a method in the stack with the given parameter list
197    * @param[in] method The name of the method
198    * @param[in] params A comma separated list of parameter values
199    * @return true if the method was in the stack
200    */
201   bool FindMethodAndParams(std::string method, std::string params) const;
202
203   /**
204    * Search for a method in the stack with the given parameter list
205    * @param[in] method The name of the method
206    * @param[in] params A map of named parameters to test for
207    * @return true if the method was in the stack
208    */
209   bool FindMethodAndParams(std::string method, const NamedParams& params) const;
210
211   /**
212    * Search for a method in the stack with the given parameter list.
213    * The search is done from a given index.
214    * This allows the order of methods and parameters to be checked.
215    * @param[in] method The name of the method
216    * @param[in] params A comma separated list of parameter values
217    * @param[in/out] startIndex The method index to start looking from.
218    *                This is updated if a method is found so subsequent
219    *                calls can search for methods occuring after this one.
220    * @return True if the method was in the stack
221    */
222   bool FindMethodAndParamsFromStartIndex(std::string method, std::string params, size_t& startIndex) const;
223
224   /**
225    * Search for a method in the stack with the given parameter list
226    * @param[in] method The name of the method
227    * @param[in] params A comma separated list of parameter values
228    * @return index in the stack where the method was found or -1 otherwise
229    */
230   int FindIndexFromMethodAndParams(std::string method, std::string params) const;
231
232   /**
233    * Search for a method in the stack with the given parameter list
234    * @param[in] method The name of the method
235    * @param[in] params A map of named parameter values to match
236    * @return index in the stack where the method was found or -1 otherwise
237    */
238   int FindIndexFromMethodAndParams(std::string method, const NamedParams& params) const;
239
240   /**
241    * Test if the given method and parameters are at a given index in the stack
242    * @param[in] index Index in the call stack
243    * @param[in] method Name of method to test
244    * @param[in] params A comma separated list of parameter values to test
245    */
246   bool TestMethodAndParams(int index, std::string method, std::string params) const;
247
248   /**
249    * Reset the call stack
250    */
251   void Reset();
252
253   /**
254    * Method to display contents of the TraceCallStack.
255    * @return A string containing a list of function calls and parameters (may contain newline characters)
256    */
257   std::string GetTraceString()
258   {
259     std::stringstream traceStream;
260     std::size_t       functionCount = mCallStack.size();
261     for(std::size_t i = 0; i < functionCount; ++i)
262     {
263       Dali::TraceCallStack::FunctionCall functionCall = mCallStack[i];
264       traceStream << "StackTrace: Index:" << i << ",  Function:" << functionCall.method << ",  ParamList:" << functionCall.paramList << std::endl;
265     }
266
267     return traceStream.str();
268   }
269
270 private:
271   bool        mTraceActive{false}; ///< True if the trace is active
272   bool        mLogging{false};     ///< True if the trace is logged to stdout
273   std::string mPrefix;
274
275   struct FunctionCall
276   {
277     std::string method;
278     std::string paramList;
279     NamedParams namedParams;
280     FunctionCall(const std::string& aMethod, const std::string& aParamList)
281     : method(aMethod),
282       paramList(aParamList)
283     {
284     }
285     FunctionCall(const std::string& aMethod, const std::string& aParamList, const NamedParams& altParams)
286     : method(aMethod),
287       paramList(aParamList),
288       namedParams(altParams)
289     {
290     }
291   };
292
293   std::vector<FunctionCall> mCallStack; ///< The call stack
294 };
295
296 } // namespace Dali
297
298 #endif // TEST_TRACE_CALL_STACK_H