d569cba555d4550ad2a31013a9735d6aaa404c71
[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) 2016 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 <string>
22 #include <vector>
23 #include <map>
24 #include <sstream>
25
26 namespace Dali
27 {
28
29 template<typename T>
30 std::string ToString(const T& x)
31 {
32   return "undefined";
33 }
34
35 std::string ToString(int x);
36 std::string ToString(unsigned int x);
37 std::string ToString(float x);
38
39 /**
40  * Helper class to track method calls in the abstraction and search for them in test cases
41  */
42 class TraceCallStack
43 {
44 public:
45
46   /// Typedef for passing and storing named parameters
47   typedef std::map< std::string, std::string > NamedParams;
48
49   /**
50    * Constructor
51    */
52   TraceCallStack();
53
54   /**
55    * Destructor
56    */
57   ~TraceCallStack();
58
59   /**
60    * Turn on / off tracing
61    */
62   void Enable(bool enable);
63
64   bool IsEnabled();
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; ///< True if the trace is active
180
181   struct FunctionCall
182   {
183     std::string method;
184     std::string paramList;
185     NamedParams namedParams;
186     FunctionCall( const std::string& aMethod, const std::string& aParamList )
187     : method( aMethod ), paramList( aParamList )
188     {
189     }
190     FunctionCall( const std::string& aMethod, const std::string& aParamList, const NamedParams& altParams )
191     : method( aMethod ), paramList( aParamList ), namedParams( altParams )
192     {
193     }
194   };
195
196   std::vector< FunctionCall > mCallStack; ///< The call stack
197 };
198
199 } // namespace dali
200
201 #endif // TEST_TRACE_CALL_STACK_H