Updated test files to match dali-core
[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   typedef std::map<std::string, std::string> NamedParams;
46
47   /**
48    * Constructor
49    */
50   TraceCallStack(std::string prefix = "");
51
52   /**
53    * Destructor
54    */
55   ~TraceCallStack();
56
57   /**
58    * Turn on / off tracing
59    */
60   void Enable(bool enable);
61
62   bool IsEnabled();
63
64   void EnableLogging(bool enable);
65
66   /**
67    * Push a call onto the stack if the trace is active
68    * @param[in] method The name of the method
69    * @param[in] params A comma separated list of parameter values
70    */
71   void PushCall(std::string method, std::string params);
72
73   /**
74    * Push a call onto the stack if the trace is active
75    * @param[in] method The name of the method
76    * @param[in] params A comma separated list of parameter values
77    * @param[in] altParams A map of named parameter values
78    */
79   void PushCall(std::string method, std::string params, const NamedParams& altParams);
80
81   /**
82    * Search for a method in the stack
83    * @param[in] method The name of the method
84    * @return true if the method was in the stack
85    */
86   bool FindMethod(std::string method) const;
87
88   /**
89    * Search for a method in the stack and return its parameters if found
90    * @param[in] method The name of the method
91    * @param[out] params of the method
92    * @return true if the method was in the stack
93    */
94   bool FindMethodAndGetParameters(std::string method, std::string& params) const;
95
96   /**
97    * Count how many times a method was called
98    * @param[in] method The name of the method
99    * @return The number of times it was called
100    */
101   int CountMethod(std::string method) const;
102
103   /**
104    * Search for a method in the stack with the given parameter list
105    * @param[in] method The name of the method
106    * @param[in] params A comma separated list of parameter values
107    * @return true if the method was in the stack
108    */
109   bool FindMethodAndParams(std::string method, std::string params) const;
110
111   /**
112    * Search for a method in the stack with the given parameter list
113    * @param[in] method The name of the method
114    * @param[in] params A map of named parameters to test for
115    * @return true if the method was in the stack
116    */
117   bool FindMethodAndParams(std::string method, const NamedParams& params) const;
118
119   /**
120    * Search for a method in the stack with the given parameter list.
121    * The search is done from a given index.
122    * This allows the order of methods and parameters to be checked.
123    * @param[in] method The name of the method
124    * @param[in] params A comma separated list of parameter values
125    * @param[in/out] startIndex The method index to start looking from.
126    *                This is updated if a method is found so subsequent
127    *                calls can search for methods occuring after this one.
128    * @return True if the method was in the stack
129    */
130   bool FindMethodAndParamsFromStartIndex(std::string method, std::string params, size_t& startIndex) const;
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 index in the stack where the method was found or -1 otherwise
137    */
138   int FindIndexFromMethodAndParams(std::string method, std::string params) const;
139
140   /**
141    * Search for a method in the stack with the given parameter list
142    * @param[in] method The name of the method
143    * @param[in] params A map of named parameter values to match
144    * @return index in the stack where the method was found or -1 otherwise
145    */
146   int FindIndexFromMethodAndParams(std::string method, const NamedParams& params) const;
147
148   /**
149    * Test if the given method and parameters are at a given index in the stack
150    * @param[in] index Index in the call stack
151    * @param[in] method Name of method to test
152    * @param[in] params A comma separated list of parameter values to test
153    */
154   bool TestMethodAndParams(int index, std::string method, std::string params) const;
155
156   /**
157    * Reset the call stack
158    */
159   void Reset();
160
161   /**
162    * Method to display contents of the TraceCallStack.
163    * @return A string containing a list of function calls and parameters (may contain newline characters)
164    */
165   std::string GetTraceString()
166   {
167     std::stringstream traceStream;
168     std::size_t       functionCount = mCallStack.size();
169     for(std::size_t i = 0; i < functionCount; ++i)
170     {
171       Dali::TraceCallStack::FunctionCall functionCall = mCallStack[i];
172       traceStream << "StackTrace: Index:" << i << ",  Function:" << functionCall.method << ",  ParamList:" << functionCall.paramList << std::endl;
173     }
174
175     return traceStream.str();
176   }
177
178 private:
179   bool        mTraceActive{false}; ///< True if the trace is active
180   bool        mLogging{false};     ///< True if the trace is logged to stdout
181   std::string mPrefix;
182
183   struct FunctionCall
184   {
185     std::string method;
186     std::string paramList;
187     NamedParams namedParams;
188     FunctionCall(const std::string& aMethod, const std::string& aParamList)
189     : method(aMethod),
190       paramList(aParamList)
191     {
192     }
193     FunctionCall(const std::string& aMethod, const std::string& aParamList, const NamedParams& altParams)
194     : method(aMethod),
195       paramList(aParamList),
196       namedParams(altParams)
197     {
198     }
199   };
200
201   std::vector<FunctionCall> mCallStack; ///< The call stack
202 };
203
204 } // namespace Dali
205
206 #endif // TEST_TRACE_CALL_STACK_H