Merge "Clipping API feature in Actor" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-trace-call-stack.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include "test-trace-call-stack.h"
19 #include <sstream>
20
21 namespace Dali
22 {
23
24 std::string ToString(int x)
25 {
26   std::stringstream out;
27   out << x;
28   return out.str();
29 }
30
31 std::string ToString(unsigned int x)
32 {
33   std::stringstream out;
34   out << x;
35   return out.str();
36 }
37
38 std::string ToString(float x)
39 {
40   std::stringstream out;
41   out << x;
42   return out.str();
43 }
44
45 /**
46  * Constructor
47  */
48 TraceCallStack::TraceCallStack() : mTraceActive(false) { }
49
50 /**
51  * Destructor
52  */
53 TraceCallStack::~TraceCallStack() { }
54
55 /**
56  * Turn on / off tracing
57  */
58 void TraceCallStack::Enable(bool enable) { mTraceActive = enable; }
59
60 bool TraceCallStack::IsEnabled() { return mTraceActive; }
61
62 /**
63  * Push a call onto the stack if the trace is active
64  * @param[in] method The name of the method
65  * @param[in] params A comma separated list of parameter values
66  */
67 void TraceCallStack::PushCall(std::string method, std::string params)
68 {
69   if(mTraceActive)
70   {
71     FunctionCall stackFrame(method, params);
72     mCallStack.push_back( stackFrame );
73   }
74 }
75
76 void TraceCallStack::PushCall(std::string method, std::string params, const TraceCallStack::NamedParams& altParams)
77 {
78   if(mTraceActive)
79   {
80     FunctionCall stackFrame(method, params, altParams);
81     mCallStack.push_back( stackFrame );
82   }
83 }
84
85 /**
86  * Search for a method in the stack
87  * @param[in] method The name of the method
88  * @return true if the method was in the stack
89  */
90 bool TraceCallStack::FindMethod(std::string method) const
91 {
92   bool found = false;
93   for( size_t i=0; i < mCallStack.size(); i++ )
94   {
95     if( 0 == mCallStack[i].method.compare(method) )
96     {
97       found = true;
98       break;
99     }
100   }
101   return found;
102 }
103
104 int TraceCallStack::CountMethod(std::string method) const
105 {
106   int numCalls = 0;
107   for( size_t i=0; i < mCallStack.size(); i++ )
108   {
109     if( 0 == mCallStack[i].method.compare(method) )
110     {
111       numCalls++;
112     }
113   }
114   return numCalls;
115 }
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 true if the method was in the stack
122  */
123 bool TraceCallStack::FindMethodAndParams(std::string method, std::string params) const
124 {
125   return FindIndexFromMethodAndParams( method, params ) > -1;
126 }
127
128 bool TraceCallStack::FindMethodAndParams(std::string method, const NamedParams& params) const
129 {
130   return FindIndexFromMethodAndParams( method, params ) > -1;
131 }
132
133 bool TraceCallStack::FindMethodAndParamsFromStartIndex( std::string method, std::string params, size_t& startIndex ) const
134 {
135   for( size_t i = startIndex; i < mCallStack.size(); ++i )
136   {
137     if( ( mCallStack[i].method.compare( method ) == 0 ) && ( mCallStack[i].paramList.compare( params ) == 0 ) )
138     {
139       startIndex = i;
140       return true;
141     }
142   }
143   return false;
144 }
145
146 /**
147  * Search for a method in the stack with the given parameter list
148  * @param[in] method The name of the method
149  * @param[in] params A comma separated list of parameter values
150  * @return index in the stack where the method was found or -1 otherwise
151  */
152 int TraceCallStack::FindIndexFromMethodAndParams(std::string method, std::string params) const
153 {
154   int index = -1;
155   for( size_t i=0; i < mCallStack.size(); i++ )
156   {
157     if( 0 == mCallStack[i].method.compare(method) && 0 == mCallStack[i].paramList.compare(params) )
158     {
159       index = i;
160       break;
161     }
162   }
163   return index;
164 }
165
166 int TraceCallStack::FindIndexFromMethodAndParams(std::string method, const TraceCallStack::NamedParams& params) const
167 {
168   int index = -1;
169   for( size_t i=0; i < mCallStack.size(); i++ )
170   {
171     if( 0 == mCallStack[i].method.compare(method) )
172     {
173       // Test each of the passed in parameters:
174       bool match = true;
175       for( NamedParams::const_iterator iter = params.begin() ; iter != params.end() ; ++iter )
176       {
177         NamedParams::const_iterator paramIter = mCallStack[i].namedParams.find(iter->first);
178         if( paramIter == params.end() || paramIter->second.compare(iter->second) != 0 )
179         {
180           match = false;
181           break;
182         }
183       }
184       if( match == true )
185       {
186         index = i;
187         break;
188       }
189     }
190   }
191   return index;
192 }
193
194
195 /**
196  * Test if the given method and parameters are at a given index in the stack
197  * @param[in] index Index in the call stack
198  * @param[in] method Name of method to test
199  * @param[in] params A comma separated list of parameter values to test
200  */
201 bool TraceCallStack::TestMethodAndParams(int index, std::string method, std::string params) const
202 {
203   return ( 0 == mCallStack[index].method.compare(method) && 0 == mCallStack[index].paramList.compare(params) );
204 }
205
206 /**
207  * Reset the call stack
208  */
209 void TraceCallStack::Reset()
210 {
211   mCallStack.clear();
212 }
213
214
215 } // namespace Dali