|
1 /* |
|
2 * Copyright (c) 2006-2007 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Test suite. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <eikenv.h> |
|
20 #include <coeaui.h> |
|
21 #include <eikappui.h> |
|
22 #include <eikapp.h> |
|
23 |
|
24 #include "bctestlogger.h" |
|
25 #include "bctestsuite.h" |
|
26 #include "bctestcase.h" |
|
27 #include "bctestendcase.h" |
|
28 |
|
29 _LIT( KLeaveLog, "\r\n\r\nTest case %s leaved. Leave reason is %d." ); |
|
30 _LIT( KNoCommandsInCase, |
|
31 "No automatic test commands defined in test case %s!\r\n" ); |
|
32 _LIT( KUnbalancedIf, |
|
33 "Unbalanced ifs/endifs in test case %s!\r\n\r\n" ); |
|
34 _LIT( KCaseStartLog, ", case %d start " ); |
|
35 _LIT( KCaseEndLog, ", case %d end " ); |
|
36 _LIT( KUnnamedCase, "Unnamed case" ); |
|
37 _LIT( KStartInfo, |
|
38 " Total %d cases will be tested in the application. \r\n\r\n\r\n" ); |
|
39 _LIT( KCompletedTest, "%s completed testing work." ); |
|
40 _LIT( KSummary, "\r\nSummary:\r\nTotal %d cases were tested.\r\nThere are \ |
|
41 %d cases including failed tests.\r\nTotal %d asserted tests and %d \ |
|
42 failed tests.\r\n" ); |
|
43 _LIT( KSummaryFailLog, "Those cases including failed tests are " ); |
|
44 _LIT( KSummaryComma, ", " ); |
|
45 _LIT( KSummaryDot, ". " ); |
|
46 _LIT( KSummaryFailFormat, "case %d"); |
|
47 |
|
48 // ======== MEMBER FUNCTIONS ======== |
|
49 |
|
50 // --------------------------------------------------------------------------- |
|
51 // C++ default Constructor |
|
52 // --------------------------------------------------------------------------- |
|
53 // |
|
54 CBCTestSuite::CBCTestSuite( CBCTestLogger* aLogger ) |
|
55 : iLogger( aLogger ) |
|
56 { |
|
57 } |
|
58 |
|
59 // --------------------------------------------------------------------------- |
|
60 // Destructor |
|
61 // --------------------------------------------------------------------------- |
|
62 // |
|
63 CBCTestSuite::~CBCTestSuite() |
|
64 { |
|
65 for ( TInt i=0; i<iTestCases.Count(); i++ ) |
|
66 { |
|
67 delete iTestCases[i]; |
|
68 } |
|
69 iTestCases.Close(); |
|
70 } |
|
71 |
|
72 // --------------------------------------------------------------------------- |
|
73 // CBCTestSuite::RunL |
|
74 // Pass an command to test case for executing some test work. |
|
75 // --------------------------------------------------------------------------- |
|
76 // |
|
77 void CBCTestSuite::RunL(int aCmd) |
|
78 { |
|
79 for ( TInt i=0; i<iTestCases.Count(); i++ ) |
|
80 { |
|
81 TRAPD( errno, iTestCases[i]->RunL(aCmd) ); |
|
82 if ( errno != KErrNone ) |
|
83 { |
|
84 TDes& buf = iLogger->Buffer(); |
|
85 buf.AppendFormat( |
|
86 TPtrC( KLeaveLog ), iTestCases[i]->Name().Ptr(), errno ); |
|
87 iLogger->CreateTimeStamp( buf ); |
|
88 buf.Append( KLogLine ); |
|
89 iLogger->WriteLogL( buf ); |
|
90 } |
|
91 } |
|
92 } |
|
93 |
|
94 // --------------------------------------------------------------------------- |
|
95 // CBCTestSuite::AddTestCaseL |
|
96 // Add a test case pointer and name of the test case. |
|
97 // --------------------------------------------------------------------------- |
|
98 // |
|
99 void CBCTestSuite::AddTestCaseL( CBCTestCase* aTestCase, const TDesC& aName ) |
|
100 { |
|
101 aTestCase->SetLogger( iLogger ); |
|
102 aTestCase->SetName( aName ); |
|
103 iTestCases.AppendL( aTestCase ); |
|
104 } |
|
105 |
|
106 // --------------------------------------------------------------------------- |
|
107 // CBCTestSuite::AddTestCaseL |
|
108 // Build parameter of automatic test. |
|
109 // --------------------------------------------------------------------------- |
|
110 // |
|
111 void CBCTestSuite::BuildScriptsL( SAutoTest* aAutoTest ) |
|
112 { |
|
113 if ( !aAutoTest ) |
|
114 { |
|
115 return; |
|
116 } |
|
117 TInt type = iTestCases[iTestCases.Count() - 1]->Type(); |
|
118 if ( type != CBCTestCase::EEndCase ) |
|
119 { |
|
120 CBCTestEndCase* endCase = CBCTestEndCase::NewL(); |
|
121 CleanupStack::PushL( endCase ); |
|
122 endCase->SetType( CBCTestCase::EEndCase ); |
|
123 AddTestCaseL( endCase, KNullDesC ); |
|
124 CleanupStack::Pop( endCase ); |
|
125 } |
|
126 delete [] aAutoTest->countArray; |
|
127 aAutoTest->countArray = NULL; |
|
128 delete [] aAutoTest->scripts; |
|
129 aAutoTest->scripts = NULL; |
|
130 |
|
131 aAutoTest->scriptCount = iTestCases.Count(); |
|
132 aAutoTest->countArray = new( ELeave ) TInt[aAutoTest->scriptCount]; |
|
133 aAutoTest->scripts = new( ELeave ) TInt*[aAutoTest->scriptCount]; |
|
134 |
|
135 for ( TInt i=0; i< aAutoTest->scriptCount; i++ ) |
|
136 { |
|
137 aAutoTest->countArray[i] = iTestCases[i]->ScriptCount(); |
|
138 aAutoTest->scripts[i] = iTestCases[i]->TestScripts(); |
|
139 aAutoTest->nameArray.Append( iTestCases[i]->Name() ); |
|
140 } |
|
141 } |
|
142 |
|
143 // ----------------------------------------------------------------------------- |
|
144 // CBCTestSuite::HandleTestEvent |
|
145 // ----------------------------------------------------------------------------- |
|
146 // |
|
147 void CBCTestSuite::HandleTestEventL( TBCTestEvent aEvent, TInt aParam ) |
|
148 { |
|
149 TInt scriptIndex = aParam; |
|
150 if ( scriptIndex < 0 || scriptIndex >= iTestCases.Count() ) |
|
151 { |
|
152 return; |
|
153 } |
|
154 switch ( aEvent ) |
|
155 { |
|
156 case ETestCaseStart: |
|
157 { |
|
158 iTestCases[scriptIndex]->ClearTestSummary(); |
|
159 if ( scriptIndex == 0) |
|
160 { |
|
161 WriteStartLogL(); |
|
162 } |
|
163 WriteTestCaseLogL( scriptIndex, ETrue ); |
|
164 } |
|
165 break; |
|
166 case ETestCaseEnd: |
|
167 { |
|
168 WriteTestCaseLogL( scriptIndex, EFalse ); |
|
169 // The end case is not considered by user. So the test case |
|
170 // before end case is actually the fianl case. |
|
171 TInt actualCaseCount = iTestCases.Count() - 1; |
|
172 // The last index is actualCaseCount - 1 |
|
173 if ( scriptIndex == actualCaseCount - 1 ) |
|
174 { |
|
175 WriteSummaryLogL(); |
|
176 } |
|
177 } |
|
178 break; |
|
179 case ETestCaseNoScript: |
|
180 { |
|
181 TDes& buf = iLogger->Buffer(); |
|
182 buf.Zero(); |
|
183 buf.AppendFormat( TPtrC( KNoCommandsInCase ), |
|
184 iTestCases[scriptIndex]->Name().Ptr() ); |
|
185 iLogger->WriteLogL( buf ); |
|
186 } |
|
187 break; |
|
188 case ETestCaseUnbalancedIf: |
|
189 { |
|
190 TDes& buf = iLogger->Buffer(); |
|
191 buf.Zero(); |
|
192 buf.AppendFormat( TPtrC( KUnbalancedIf ), |
|
193 iTestCases[scriptIndex]->Name().Ptr() ); |
|
194 iLogger->WriteLogL( buf ); |
|
195 } |
|
196 break; |
|
197 default: |
|
198 break; |
|
199 } |
|
200 } |
|
201 |
|
202 // ----------------------------------------------------------------------------- |
|
203 // CBCTestSuite::WriteTestCaseLogL |
|
204 // Creates test case log start (and end) separator. |
|
205 // ----------------------------------------------------------------------------- |
|
206 // |
|
207 void CBCTestSuite::WriteTestCaseLogL( TInt aScriptIndex, TBool aStart ) |
|
208 { |
|
209 if ( !iLogger ) |
|
210 { |
|
211 return; |
|
212 } |
|
213 |
|
214 TDes& buf = iLogger->Buffer(); |
|
215 buf.Zero(); // reset buffer |
|
216 TInt type = iTestCases[aScriptIndex]->Type(); |
|
217 if ( type == CBCTestCase::EEndCase ) |
|
218 { |
|
219 buf.Append( KLogTwoLine ); |
|
220 iLogger->WriteLogL( buf ); |
|
221 return; |
|
222 } |
|
223 TDesC& name = iTestCases[aScriptIndex]->Name(); |
|
224 buf.Append( KLogRow80 ); // first separator row |
|
225 if ( name.Length() > 0 ) |
|
226 { |
|
227 buf.Append( name ); |
|
228 } |
|
229 else |
|
230 { |
|
231 buf.Append( KUnnamedCase ); |
|
232 } |
|
233 |
|
234 if ( aStart ) |
|
235 { |
|
236 buf.AppendFormat( TPtrC( KCaseStartLog ), aScriptIndex + 1 ); |
|
237 } |
|
238 else |
|
239 { |
|
240 buf.AppendFormat( TPtrC( KCaseEndLog ), aScriptIndex + 1 ); |
|
241 } |
|
242 iLogger->CreateTimeStamp( buf ); |
|
243 buf.Append( KLogRow80 ); |
|
244 if ( !aStart ) // insert some empty lines |
|
245 { |
|
246 buf.Append( KLogTwoLine ); |
|
247 } |
|
248 iLogger->WriteLogL( buf ); |
|
249 } |
|
250 |
|
251 // ----------------------------------------------------------------------------- |
|
252 // CBCTestSuite::WriteStartLogL |
|
253 // ----------------------------------------------------------------------------- |
|
254 // |
|
255 void CBCTestSuite::WriteStartLogL() |
|
256 { |
|
257 if ( !iLogger ) |
|
258 { |
|
259 return; |
|
260 } |
|
261 |
|
262 TDes& buf = iLogger->Buffer(); |
|
263 buf.Zero(); |
|
264 buf.AppendFormat( TPtrC( KStartInfo ), iTestCases.Count() - 1 ); |
|
265 iLogger->WriteLogL( buf ); |
|
266 } |
|
267 |
|
268 // ----------------------------------------------------------------------------- |
|
269 // CBCTestSuite::WriteSummaryLogL |
|
270 // ----------------------------------------------------------------------------- |
|
271 // |
|
272 void CBCTestSuite::WriteSummaryLogL() |
|
273 { |
|
274 TDes& buf = iLogger->Buffer(); |
|
275 |
|
276 // write main log |
|
277 buf.Zero(); |
|
278 const TDesC& appCaption = iLogger->AppCaption(); |
|
279 buf.AppendFormat( KCompletedTest, appCaption.Ptr() ); |
|
280 iLogger->CreateTimeStamp( buf ); |
|
281 iLogger->WriteMainLogL( buf ); |
|
282 |
|
283 // write summary log |
|
284 TInt testCount = 0; |
|
285 TInt failCases = 0; |
|
286 TInt failCount = 0; |
|
287 TInt* failedInfo = new( ELeave ) TInt[iTestCases.Count()]; |
|
288 CleanupStack::PushL( failedInfo ); |
|
289 for( TInt i=0; i<iTestCases.Count(); i++ ) |
|
290 { |
|
291 TInt count = 0; |
|
292 iTestCases[i]->GetTestSummary( count, failedInfo[i] ); |
|
293 testCount += count; |
|
294 if ( failedInfo[i] > 0) |
|
295 { |
|
296 failCases++; |
|
297 failCount += failedInfo[i]; |
|
298 } |
|
299 } |
|
300 |
|
301 buf.Zero(); |
|
302 buf.AppendFormat( TPtrC( KSummary ), iTestCases.Count() - 1, failCases, |
|
303 testCount, failCount ); |
|
304 |
|
305 if ( failCases > 0 ) |
|
306 { |
|
307 buf.Append( KSummaryFailLog ); |
|
308 TInt failRecord = 0; |
|
309 for( TInt i=0; i<iTestCases.Count(); i++ ) |
|
310 { |
|
311 if ( failedInfo[i] > 0) |
|
312 { |
|
313 if ( failRecord > 0) |
|
314 { |
|
315 buf.Append( KSummaryComma ); |
|
316 } |
|
317 buf.AppendFormat( TPtrC( KSummaryFailFormat ), i + 1 ); |
|
318 failRecord++; |
|
319 } |
|
320 } |
|
321 buf.Append( KSummaryDot ); |
|
322 buf.Append( KLogTwoLine ); |
|
323 } |
|
324 CleanupStack::PopAndDestroy( failedInfo ); |
|
325 iLogger->WriteLogL( buf ); |
|
326 iLogger->WriteMainLogL( buf ); |
|
327 } |