Updating test-suite to match 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) 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    * @param[in] method The name of the method
107    * @param[in] params A comma separated list of parameter values
108    * @return index in the stack where the method was found or -1 otherwise
109    */
110   int FindIndexFromMethodAndParams(std::string method, std::string params) const;
111
112   /**
113    * Search for a method in the stack with the given parameter list
114    * @param[in] method The name of the method
115    * @param[in] params A map of named parameter values to match
116    * @return index in the stack where the method was found or -1 otherwise
117    */
118   int FindIndexFromMethodAndParams(std::string method, const NamedParams& params) const;
119
120   /**
121    * Test if the given method and parameters are at a given index in the stack
122    * @param[in] index Index in the call stack
123    * @param[in] method Name of method to test
124    * @param[in] params A comma separated list of parameter values to test
125    */
126   bool TestMethodAndParams(int index, std::string method, std::string params) const;
127
128   /**
129    * Reset the call stack
130    */
131   void Reset();
132
133   /**
134    * Method to display contents of the TraceCallStack.
135    * @return A string containing a list of function calls and parameters (may contain newline characters)
136    */
137   std::string GetTraceString()
138   {
139     std::stringstream traceStream;
140     int functionCount = mCallStack.size();
141     for( int i = 0; i < functionCount; ++i )
142     {
143       Dali::TraceCallStack::FunctionCall functionCall = mCallStack[ i ];
144       traceStream << "StackTrace: Index:" << i << ",  Function:" << functionCall.method << ",  ParamList:" << functionCall.paramList << std::endl;
145     }
146
147     return traceStream.str();
148   }
149
150 private:
151   bool mTraceActive; ///< True if the trace is active
152
153   struct FunctionCall
154   {
155     std::string method;
156     std::string paramList;
157     NamedParams namedParams;
158     FunctionCall( const std::string& aMethod, const std::string& aParamList )
159     : method( aMethod ), paramList( aParamList )
160     {
161     }
162     FunctionCall( const std::string& aMethod, const std::string& aParamList, const NamedParams& altParams )
163     : method( aMethod ), paramList( aParamList ), namedParams( altParams )
164     {
165     }
166   };
167
168   std::vector< FunctionCall > mCallStack; ///< The call stack
169 };
170
171 } // namespace dali
172
173 #endif // TEST_TRACE_CALL_STACK_H