1 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 |
|
17 #include "GmxmlTestUtils.h" |
|
18 |
|
19 |
|
20 _LIT(KMsvTestFileOutputBase,"MsgLogs"); |
|
21 |
|
22 // this is used if we can't find MsgLogs anywhere |
|
23 _LIT(KMsvTestFileDefaultOutputBase,"\\MsgLogs\\"); |
|
24 _LIT(KMsvPathSep,"\\"); |
|
25 #define KMaxLogLineLength 256 |
|
26 |
|
27 |
|
28 |
|
29 // |
|
30 // |
|
31 // CTestTimer |
|
32 // |
|
33 // |
|
34 |
|
35 CTestTimer::CTestTimer() |
|
36 : CTimer(EPriorityLow) |
|
37 {} |
|
38 |
|
39 void CTestTimer::RunL() |
|
40 { |
|
41 if (!iReport) |
|
42 { |
|
43 CActiveScheduler::Stop(); |
|
44 } |
|
45 else |
|
46 { |
|
47 User::RequestComplete(iReport, iStatus.Int()); |
|
48 iReport = NULL; |
|
49 } |
|
50 } |
|
51 |
|
52 CTestTimer* CTestTimer::NewL() |
|
53 { |
|
54 CTestTimer* self = new(ELeave) CTestTimer(); |
|
55 CleanupStack::PushL(self); |
|
56 self->ConstructL(); // CTimer |
|
57 CActiveScheduler::Add(self); |
|
58 CleanupStack::Pop(); |
|
59 return self; |
|
60 } |
|
61 |
|
62 void CTestTimer::AfterReq(TTimeIntervalMicroSeconds32 aSec, TRequestStatus& aStatus) |
|
63 { |
|
64 iReport = &aStatus; |
|
65 aStatus = KRequestPending; |
|
66 After(aSec); |
|
67 } |
|
68 |
|
69 |
|
70 // |
|
71 // |
|
72 // CTestUtils |
|
73 // |
|
74 // |
|
75 |
|
76 CTestUtils::CTestUtils(RTest& aRTest) |
|
77 : iRTest(aRTest), iLogToConsole(ETrue), iLogToFile(ETrue) |
|
78 { |
|
79 TBuf<256> command; |
|
80 User::CommandLine(command); |
|
81 |
|
82 command.Trim(); |
|
83 SetRunAuto(command.FindF(KTestShowMenu) == KErrNotFound); |
|
84 |
|
85 if (!RunAuto()) |
|
86 iRTest.Printf(_L("Not Run Auto. Command line: %S\n"), &command); |
|
87 } |
|
88 |
|
89 CTestUtils::~CTestUtils() |
|
90 { |
|
91 CloseMainLog(); |
|
92 iFs.Close(); |
|
93 } |
|
94 |
|
95 void CTestUtils::ConstructL() |
|
96 { |
|
97 User::LeaveIfError(iFs.Connect()); |
|
98 OpenMainLogL(); |
|
99 WriteToLog(KTestHeader); |
|
100 } |
|
101 |
|
102 void CTestUtils::Printf(TRefByValue<const TDesC> aFmt,...) |
|
103 { |
|
104 // Print to the log file. |
|
105 TTestOverflow overflow(*this); |
|
106 VA_LIST list; |
|
107 VA_START(list, aFmt); |
|
108 TBuf<0x100> aBuf; |
|
109 aBuf.AppendFormatList(aFmt, list, &overflow); |
|
110 |
|
111 if (iLogToConsole) |
|
112 { |
|
113 iRTest.Printf(aBuf); |
|
114 } |
|
115 |
|
116 if (iLogToFile) |
|
117 { |
|
118 WriteComment(aBuf); |
|
119 } |
|
120 } |
|
121 |
|
122 |
|
123 void CTestUtils::CreateAllTestDirectories() |
|
124 { |
|
125 TChar driveChar=RFs::GetSystemDriveChar(); |
|
126 TBuf<2> systemDrive; |
|
127 systemDrive.Append(driveChar); |
|
128 systemDrive.Append(KDriveDelimiter); |
|
129 |
|
130 iFs.SetSessionPath(systemDrive); |
|
131 |
|
132 if (!iFs.MkDir(KLogsDir)) |
|
133 { |
|
134 Printf(_L("Created c:\\logs\\ directory\n")); |
|
135 } |
|
136 } |
|
137 |
|
138 TInt CTestUtils::ResolveLogFile(const TDesC& aFileName, TParse& aParseOut) |
|
139 { |
|
140 TFileName* savedPath = new TFileName; |
|
141 TFileName* fileName = new TFileName; |
|
142 if ((savedPath == NULL) || (fileName == NULL)) |
|
143 return KErrNoMemory; |
|
144 |
|
145 fileName->Append(KMsvPathSep); |
|
146 fileName->Append(KMsvTestFileOutputBase); |
|
147 fileName->Append(KMsvPathSep); |
|
148 |
|
149 // file finder will look in the session drive first, then Y->A,Z |
|
150 // so set session drive to Y (save old and restore it afterwards) |
|
151 iFs.SessionPath(*savedPath); |
|
152 _LIT(KTopDrive,"Y:\\"); |
|
153 iFs.SetSessionPath(KTopDrive); |
|
154 TFindFile file_finder(iFs); |
|
155 TInt err = file_finder.FindByDir(*fileName,KNullDesC); |
|
156 |
|
157 if(err==KErrNone) |
|
158 { |
|
159 fileName->Copy(file_finder.File()); |
|
160 AppendTestName(*fileName); |
|
161 fileName->Append(KMsvPathSep); |
|
162 fileName->Append(aFileName); |
|
163 iFs.MkDirAll(*fileName); |
|
164 aParseOut.Set(*fileName,NULL,NULL); |
|
165 } |
|
166 iFs.SetSessionPath(*savedPath); |
|
167 delete savedPath; |
|
168 delete fileName; |
|
169 return(err); |
|
170 } |
|
171 |
|
172 TBool CTestUtils::RunAuto() const |
|
173 { |
|
174 return iRunAuto; |
|
175 } |
|
176 |
|
177 void CTestUtils::SetRunAuto(TBool aRunAuto) |
|
178 { |
|
179 iRunAuto = aRunAuto; |
|
180 } |
|
181 |
|
182 |
|
183 // appends .<PLATFORM>.<VARIANT>.log to aFileName; |
|
184 void CTestUtils::AppendVariantName(TDes& aFileName) |
|
185 { |
|
186 #if (defined(__THUMB__) || defined(__MARM_THUMB__)) |
|
187 aFileName.Append(_L(".THUMB.")); |
|
188 #endif |
|
189 #if (defined(__ARMI__) || defined(__MARM_ARMI__)) |
|
190 aFileName.Append(_L(".ARMI.")); |
|
191 #endif |
|
192 #if (defined(__ARM4__) || defined(__MISA__) || defined(__MARM_ARM4__)) |
|
193 aFileName.Append(_L(".ARM4.")); |
|
194 #endif |
|
195 #if defined(__WINSCW__) |
|
196 aFileName.Append(_L(".WINSCW.")); |
|
197 #else |
|
198 #if defined(__WINS__) |
|
199 aFileName.Append(_L(".WINS.")); |
|
200 #endif |
|
201 #endif |
|
202 |
|
203 #if defined(_DEBUG) |
|
204 aFileName.Append(_L("DEB.")); |
|
205 #else |
|
206 aFileName.Append(_L("REL.")); |
|
207 #endif |
|
208 aFileName.Append(_L("LOG")); |
|
209 } |
|
210 |
|
211 // appends the test harness name to aFileName |
|
212 void CTestUtils::AppendTestName(TDes &aFileName) |
|
213 { |
|
214 TInt start = RProcess().FileName().LocateReverse('\\'); |
|
215 TInt end = RProcess().FileName().LocateReverse('.'); |
|
216 |
|
217 if (end == KErrNotFound) |
|
218 end = RProcess().FileName().Length(); |
|
219 |
|
220 aFileName.Append(RProcess().FileName().Mid(start + 1, end - start - 1)); |
|
221 } |
|
222 |
|
223 |
|
224 |
|
225 TInt CTestUtils::OpenMainLogL() |
|
226 { |
|
227 _LIT(KDisplayLogFile,"Log File %S\n"); |
|
228 TParse loglocation; |
|
229 TFileName logfile; |
|
230 TInt err=ResolveLogFile(KNullDesC, loglocation); |
|
231 if(err!=KErrNone) |
|
232 { |
|
233 TChar driveChar=RFs::GetSystemDriveChar(); |
|
234 TBuf<2> systemDrive; |
|
235 systemDrive.Append(driveChar); |
|
236 systemDrive.Append(KDriveDelimiter); |
|
237 TPath pathName(systemDrive) ; |
|
238 pathName.Append(KMsvTestFileDefaultOutputBase); |
|
239 iFs.MkDirAll(pathName); |
|
240 err=ResolveLogFile(KNullDesC, loglocation); |
|
241 } |
|
242 User::LeaveIfError(err); |
|
243 logfile.Copy(loglocation.FullName()); |
|
244 logfile.Delete(logfile.Length()-1,1); |
|
245 AppendVariantName(logfile); |
|
246 iRTest.Printf(KDisplayLogFile, &logfile); |
|
247 iFs.MkDirAll(logfile); |
|
248 |
|
249 iLogBuf=HBufC::NewL(KMaxLogLineLength); |
|
250 iLogBuf8=HBufC8::NewL(KMaxLogLineLength); |
|
251 return(iFile.Replace(iFs,logfile,EFileWrite|EFileShareAny)); |
|
252 } |
|
253 |
|
254 void CTestUtils::CloseMainLog() |
|
255 { |
|
256 delete iLogBuf; |
|
257 iLogBuf=0; |
|
258 delete iLogBuf8; |
|
259 iLogBuf8=0; |
|
260 |
|
261 iFile.Close(); |
|
262 } |
|
263 |
|
264 |
|
265 void CTestUtils::TestStart(TInt aTest, const TDesC& aTestDescription) |
|
266 { |
|
267 WriteToLog(KTestStartingWithDesc, aTest, &aTestDescription); |
|
268 } |
|
269 |
|
270 |
|
271 void CTestUtils::TestFinish(TInt aTest, TInt aError) |
|
272 { |
|
273 aError ? |
|
274 WriteToLog(KTestFailed, aTest, aError) |
|
275 : WriteToLog(KTestPassed, aTest); |
|
276 } |
|
277 |
|
278 |
|
279 void CTestUtils::TestHarnessCompleted() |
|
280 { |
|
281 WriteToLog(KTestHarnessCompleted); |
|
282 } |
|
283 |
|
284 void CTestUtils::TestHarnessFailed(TInt aError) |
|
285 { |
|
286 WriteToLog(KTestHarnessFailed, aError); |
|
287 } |
|
288 |
|
289 void CTestUtils::WriteComment(const TDesC& aComment) |
|
290 { |
|
291 /* Prepends KTestCommentPrepend to aComment, then writes it to aFlogger */ |
|
292 |
|
293 TBuf<0x100> buf; |
|
294 buf.Copy(aComment); |
|
295 buf.Trim(); |
|
296 buf.Insert(0,KTestCommentPrepend); |
|
297 WriteToLog(buf); |
|
298 } |
|
299 |
|
300 void CTestUtils::WriteToLog(TRefByValue<const TDesC> aFmt,...) |
|
301 { |
|
302 |
|
303 _LIT(KDateFormatString, "%D%M%*Y%1%/1%2%/2%3 %H%:1%T%:2%S "); |
|
304 _LIT(Kcr,"\r\n"); |
|
305 iLogBuf->Des().Zero(); |
|
306 TTime date; |
|
307 date.HomeTime(); |
|
308 TBuf<18> dateString; |
|
309 |
|
310 TRAPD(error,date.FormatL(dateString,(KDateFormatString))); |
|
311 if(error) |
|
312 { |
|
313 dateString.Copy(_L("Invalid Date")); |
|
314 } |
|
315 iLogBuf->Des().Copy(dateString); |
|
316 |
|
317 VA_LIST list; |
|
318 VA_START(list,aFmt); |
|
319 |
|
320 iLogBuf->Des().AppendFormatList(aFmt,list); |
|
321 iLogBuf->Des().Append(Kcr); |
|
322 iLogBuf8->Des().Copy(*iLogBuf); |
|
323 iFile.Write(*iLogBuf8); |
|
324 } |
|