e1882ea0ee44b399ecbbe6ca23bc7f3d83c59d48
[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 std::string ToString(int x);
29 std::string ToString(unsigned int x);
30 std::string ToString(float x);
31
32 /**
33  * Helper class to track method calls in the abstraction and search for them in test cases
34  */
35 class TraceCallStack
36 {
37 public:
38
39   /// Typedef for passing and storing named parameters
40   typedef std::map< std::string, std::string > NamedParams;
41
42   /**
43    * Constructor
44    */
45   TraceCallStack();
46
47   /**
48    * Destructor
49    */
50   ~TraceCallStack();
51
52   /**
53    * Turn on / off tracing
54    */
55   void Enable(bool enable);
56
57   bool IsEnabled();
58
59   /**
60    * Push a call onto the stack if the trace is active
61    * @param[in] method The name of the method
62    * @param[in] params A comma separated list of parameter values
63    */
64   void PushCall(std::string method, std::string params);
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    * @param[in] altParams A map of named parameter values
71    */
72   void PushCall(std::string method, std::string params, const NamedParams& altParams);
73
74   /**
75    * Search for a method in the stack
76    * @param[in] method The name of the method
77    * @return true if the method was in the stack
78    */
79   bool FindMethod(std::string method) const;
80
81   /**
82    * Count how many times a method was called
83    * @param[in] method The name of the method
84    * @return The number of times it was called
85    */
86   int CountMethod(std::string method) const;
87
88   /**
89    * Search for a method in the stack with the given parameter list
90    * @param[in] method The name of the method
91    * @param[in] params A comma separated list of parameter values
92    * @return true if the method was in the stack
93    */
94   bool FindMethodAndParams(std::string method, std::string params) const;
95
96   /**
97    * Search for a method in the stack with the given parameter list
98    * @param[in] method The name of the method
99    * @param[in] params A map of named parameters to test for
100    * @return true if the method was in the stack
101    */
102   bool FindMethodAndParams(std::string method, const NamedParams& params) const;
103
104   /**
105    * Search for a method in the stack with the given parameter list.
106    * The search is done from a given index.
107    * This allows the order of methods and parameters to be checked.
108    * @param[in] method The name of the method
109    * @param[in] params A comma separated list of parameter values
110    * @param[in/out] startIndex The method index to start looking from.
111    *                This is updated if a method is found so subsequent
112    *                calls can search for methods occuring after this one.
113    * @return True if the method was in the stack
114    */
115   bool FindMethodAndParamsFromStartIndex( std::string method, std::string params, size_t& startIndex ) const;
116
117   /**
118    * Search for a method in the stack with the given parameter list
119    * @param[in] method The name of the method
120    * @param[in] params A comma separated list of parameter values
121    * @return index in the stack where the method was found or -1 otherwise
122    */
123   int FindIndexFromMethodAndParams(std::string method, std::string params) const;
124
125   /**
126    * Search for a method in the stack with the given parameter list
127    * @param[in] method The name of the method
128    * @param[in] params A map of named parameter values to match
129    * @return index in the stack where the method was found or -1 otherwise
130    */
131   int FindIndexFromMethodAndParams(std::string method, const NamedParams& params) const;
132
133   /**
134    * Test if the given method and parameters are at a given index in the stack
135    * @param[in] index Index in the call stack
136    * @param[in] method Name of method to test
137    * @param[in] params A comma separated list of parameter values to test
138    */
139   bool TestMethodAndParams(int index, std::string method, std::string params) const;
140
141   /**
142    * Reset the call stack
143    */
144   void Reset();
145
146   /**
147    * Method to display contents of the TraceCallStack.
148    * @return A string containing a list of function calls and parameters (may contain newline characters)
149    */
150   std::string GetTraceString()
151   {
152     std::stringstream traceStream;
153     int functionCount = mCallStack.size();
154     for( int i = 0; i < functionCount; ++i )
155     {
156       Dali::TraceCallStack::FunctionCall functionCall = mCallStack[ i ];
157       traceStream << "StackTrace: Index:" << i << ",  Function:" << functionCall.method << ",  ParamList:" << functionCall.paramList << std::endl;
158     }
159
160     return traceStream.str();
161   }
162
163 private:
164   bool mTraceActive; ///< True if the trace is active
165
166   struct FunctionCall
167   {
168     std::string method;
169     std::string paramList;
170     NamedParams namedParams;
171     FunctionCall( const std::string& aMethod, const std::string& aParamList )
172     : method( aMethod ), paramList( aParamList )
173     {
174     }
175     FunctionCall( const std::string& aMethod, const std::string& aParamList, const NamedParams& altParams )
176     : method( aMethod ), paramList( aParamList ), namedParams( altParams )
177     {
178     }
179   };
180
181   std::vector< FunctionCall > mCallStack; ///< The call stack
182 };
183
184 } // namespace dali
185
186 #endif // TEST_TRACE_CALL_STACK_H