Merge "C# CustomView Implementation (C++ wrappers, manual bindings, C# wrappers)...
[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    * Search for a method in the stack and return its parameters if found
83    * @param[in] method The name of the method
84    * @param[out] params of the method
85    * @return true if the method was in the stack
86    */
87   bool FindMethodAndGetParameters(std::string method, std::string& params ) const;
88
89   /**
90    * Count how many times a method was called
91    * @param[in] method The name of the method
92    * @return The number of times it was called
93    */
94   int CountMethod(std::string method) 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 comma separated list of parameter values
100    * @return true if the method was in the stack
101    */
102   bool FindMethodAndParams(std::string method, std::string 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 map of named parameters to test for
108    * @return true if the method was in the stack
109    */
110   bool FindMethodAndParams(std::string method, const NamedParams& params) const;
111
112   /**
113    * Search for a method in the stack with the given parameter list.
114    * The search is done from a given index.
115    * This allows the order of methods and parameters to be checked.
116    * @param[in] method The name of the method
117    * @param[in] params A comma separated list of parameter values
118    * @param[in/out] startIndex The method index to start looking from.
119    *                This is updated if a method is found so subsequent
120    *                calls can search for methods occuring after this one.
121    * @return True if the method was in the stack
122    */
123   bool FindMethodAndParamsFromStartIndex( std::string method, std::string params, size_t& startIndex ) 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 comma separated list of parameter values
129    * @return index in the stack where the method was found or -1 otherwise
130    */
131   int FindIndexFromMethodAndParams(std::string method, std::string params) const;
132
133   /**
134    * Search for a method in the stack with the given parameter list
135    * @param[in] method The name of the method
136    * @param[in] params A map of named parameter values to match
137    * @return index in the stack where the method was found or -1 otherwise
138    */
139   int FindIndexFromMethodAndParams(std::string method, const NamedParams& params) const;
140
141   /**
142    * Test if the given method and parameters are at a given index in the stack
143    * @param[in] index Index in the call stack
144    * @param[in] method Name of method to test
145    * @param[in] params A comma separated list of parameter values to test
146    */
147   bool TestMethodAndParams(int index, std::string method, std::string params) const;
148
149   /**
150    * Reset the call stack
151    */
152   void Reset();
153
154   /**
155    * Method to display contents of the TraceCallStack.
156    * @return A string containing a list of function calls and parameters (may contain newline characters)
157    */
158   std::string GetTraceString()
159   {
160     std::stringstream traceStream;
161     int functionCount = mCallStack.size();
162     for( int i = 0; i < functionCount; ++i )
163     {
164       Dali::TraceCallStack::FunctionCall functionCall = mCallStack[ i ];
165       traceStream << "StackTrace: Index:" << i << ",  Function:" << functionCall.method << ",  ParamList:" << functionCall.paramList << std::endl;
166     }
167
168     return traceStream.str();
169   }
170
171 private:
172   bool mTraceActive; ///< True if the trace is active
173
174   struct FunctionCall
175   {
176     std::string method;
177     std::string paramList;
178     NamedParams namedParams;
179     FunctionCall( const std::string& aMethod, const std::string& aParamList )
180     : method( aMethod ), paramList( aParamList )
181     {
182     }
183     FunctionCall( const std::string& aMethod, const std::string& aParamList, const NamedParams& altParams )
184     : method( aMethod ), paramList( aParamList ), namedParams( altParams )
185     {
186     }
187   };
188
189   std::vector< FunctionCall > mCallStack; ///< The call stack
190 };
191
192 } // namespace dali
193
194 #endif // TEST_TRACE_CALL_STACK_H