|
1 // Copyright (c) 2007-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 the License "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 // @file testpolicy.cpp |
|
15 // @internalComponent |
|
16 // |
|
17 // |
|
18 |
|
19 #include "TestPolicy.h" |
|
20 #include "TestCaseFactory.h" |
|
21 #include "BaseTestCase.h" |
|
22 #include "TestCaseFactory.h" |
|
23 #include "testdebug.h" |
|
24 |
|
25 namespace NUnitTesting_USBDI |
|
26 { |
|
27 |
|
28 CBasicTestPolicy* CBasicTestPolicy::NewL() |
|
29 { |
|
30 CBasicTestPolicy* self = new (ELeave) CBasicTestPolicy; |
|
31 CleanupStack::PushL(self); |
|
32 self->ConstructL(); |
|
33 CleanupStack::Pop(self); |
|
34 return self; |
|
35 } |
|
36 |
|
37 |
|
38 CBasicTestPolicy::CBasicTestPolicy() |
|
39 : CActive(EPriorityStandard) |
|
40 { |
|
41 CActiveScheduler::Add(this); |
|
42 } |
|
43 |
|
44 |
|
45 CBasicTestPolicy::~CBasicTestPolicy() |
|
46 { |
|
47 LOG_FUNC |
|
48 |
|
49 Cancel(); |
|
50 } |
|
51 |
|
52 |
|
53 void CBasicTestPolicy::ConstructL() |
|
54 { |
|
55 } |
|
56 |
|
57 |
|
58 void CBasicTestPolicy::RunTestCaseL(const TDesC& aTestCaseId,TRequestStatus& aNotifierStatus) |
|
59 { |
|
60 LOG_FUNC |
|
61 |
|
62 iNotifierStatus = &aNotifierStatus; |
|
63 |
|
64 // Create the specified test case |
|
65 |
|
66 iTestCase = RTestFactory::CreateTestCaseL(aTestCaseId,EFalse); |
|
67 iTestCase->SetTestPolicy(this); |
|
68 iTestCase->PerformTestL(); |
|
69 |
|
70 // Set the request of the notifier to pending |
|
71 |
|
72 *iNotifierStatus = iStatus = KRequestPending; |
|
73 SetActive(); |
|
74 } |
|
75 |
|
76 |
|
77 void CBasicTestPolicy::DoCancel() |
|
78 { |
|
79 LOG_FUNC |
|
80 |
|
81 // Cancel running the test cases |
|
82 |
|
83 iTestCase->Cancel(); |
|
84 |
|
85 // Notify the test case controller that test case execution was cancelled |
|
86 |
|
87 User::RequestComplete(iNotifierStatus,KErrCancel); |
|
88 } |
|
89 |
|
90 |
|
91 void CBasicTestPolicy::SignalTestComplete(TInt aCompletionCode) |
|
92 { |
|
93 LOG_FUNC |
|
94 |
|
95 // Complete the test policy request with the test case completion code |
|
96 // (Basically self completion) |
|
97 |
|
98 TRequestStatus* s = &iStatus; |
|
99 User::RequestComplete(s,aCompletionCode); |
|
100 } |
|
101 |
|
102 |
|
103 void CBasicTestPolicy::RunL() |
|
104 { |
|
105 LOG_FUNC |
|
106 |
|
107 // Complete the request of the notifier with the test case |
|
108 // completion code |
|
109 |
|
110 User::RequestComplete(iNotifierStatus,iStatus.Int()); |
|
111 |
|
112 // Destroy the test case |
|
113 |
|
114 delete iTestCase; |
|
115 } |
|
116 |
|
117 |
|
118 TInt CBasicTestPolicy::RunError(TInt aError) |
|
119 { |
|
120 LOG_FUNC |
|
121 |
|
122 aError = KErrNone; |
|
123 return aError; |
|
124 } |
|
125 |
|
126 CThreadTestPolicy* CThreadTestPolicy::NewL() |
|
127 { |
|
128 CThreadTestPolicy* self = new (ELeave) CThreadTestPolicy; |
|
129 CleanupStack::PushL(self); |
|
130 self->ConstructL(); |
|
131 CleanupStack::Pop(self); |
|
132 return self; |
|
133 } |
|
134 |
|
135 |
|
136 CThreadTestPolicy::CThreadTestPolicy() |
|
137 { |
|
138 } |
|
139 |
|
140 |
|
141 CThreadTestPolicy::~CThreadTestPolicy() |
|
142 { |
|
143 iTestThread.Close(); |
|
144 if(iTestCaseId) |
|
145 { |
|
146 delete iTestCaseId; |
|
147 iTestCaseId = NULL; |
|
148 } |
|
149 } |
|
150 |
|
151 void CThreadTestPolicy::ConstructL() |
|
152 { |
|
153 } |
|
154 |
|
155 void CThreadTestPolicy::RunTestCaseL(const TDesC& aTestCaseId,TRequestStatus& aNotifierStatus) |
|
156 { |
|
157 LOG_FUNC |
|
158 |
|
159 iNotifierStatus = &aNotifierStatus; |
|
160 RDebug::Printf("Creating thread for test case '%S'",&aTestCaseId); |
|
161 |
|
162 if(iTestCaseId) |
|
163 { |
|
164 delete iTestCaseId; |
|
165 iTestCaseId = NULL; |
|
166 } |
|
167 iTestCaseId = HBufC::NewL(aTestCaseId.Length()); |
|
168 *iTestCaseId = aTestCaseId; |
|
169 |
|
170 // Create the thread to run the test case in |
|
171 TInt err(iTestThread.Create(aTestCaseId,CThreadTestPolicy::ThreadFunction, |
|
172 KDefaultStackSize,NULL,reinterpret_cast<TAny*>(iTestCaseId))); |
|
173 |
|
174 if(err != KErrNone) |
|
175 { |
|
176 RDebug::Printf("Test thread creation unsuccessful: %d",err); |
|
177 User::Leave(err); |
|
178 } |
|
179 |
|
180 RDebug::Printf("Test thread '%S' created",&aTestCaseId); |
|
181 // Start the test case in the thread |
|
182 iTestThread.Logon(iStatus); |
|
183 SetActive(); |
|
184 iTestThread.Resume(); |
|
185 *iNotifierStatus = KRequestPending; |
|
186 } |
|
187 |
|
188 |
|
189 void CThreadTestPolicy::SignalTestComplete(TInt aCompletionCode) |
|
190 { |
|
191 LOG_FUNC |
|
192 |
|
193 if(aCompletionCode == KErrNone) |
|
194 { |
|
195 RDebug::Printf("CActiveScheduler::Stop CThreadTestPolicy::SignalTestComplete"); |
|
196 CActiveScheduler::Stop(); |
|
197 } |
|
198 else |
|
199 { |
|
200 RDebug::Printf("Killing thread with: %d",aCompletionCode); |
|
201 iTestThread.Kill(aCompletionCode); |
|
202 } |
|
203 } |
|
204 |
|
205 void CThreadTestPolicy::DoCancel() |
|
206 { |
|
207 LOG_FUNC |
|
208 |
|
209 iTestCase->Cancel(); |
|
210 LOG_POINT(1) |
|
211 TInt err(iTestThread.LogonCancel(iStatus)); |
|
212 if(err != KErrNone) |
|
213 { |
|
214 RDebug::Printf("Unable to cancel thread logon: %d",err); |
|
215 } |
|
216 LOG_POINT(2) |
|
217 TRequestStatus cancelStatus; |
|
218 iTestThread.Logon(cancelStatus); |
|
219 LOG_POINT(3) |
|
220 iTestThread.Kill(KErrCancel); |
|
221 LOG_POINT(4) |
|
222 User::RequestComplete(iNotifierStatus,cancelStatus.Int()); |
|
223 } |
|
224 |
|
225 TInt CThreadTestPolicy::ThreadFunction(TAny* aThreadParameter) |
|
226 { |
|
227 LOG_CFUNC |
|
228 |
|
229 TInt err(KErrNone); |
|
230 TInt leaveCode(KErrNone); |
|
231 |
|
232 HBufC* testCaseId = reinterpret_cast<HBufC*>(aThreadParameter); |
|
233 |
|
234 // Create the cleanup stack for this thread |
|
235 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
236 if(cleanup == NULL) |
|
237 { |
|
238 return KErrNoMemory; |
|
239 } |
|
240 |
|
241 TRAP(leaveCode,err = CThreadTestPolicy::DoTestL(*testCaseId)); |
|
242 if(leaveCode != KErrNone) |
|
243 { |
|
244 RDebug::Printf("<Error %d> Thread '%S' DoTest",leaveCode,&testCaseId); |
|
245 err = leaveCode; |
|
246 } |
|
247 |
|
248 delete cleanup; |
|
249 return err; |
|
250 } |
|
251 |
|
252 |
|
253 TInt CThreadTestPolicy::DoTestL(const TDesC& aTestCaseId) |
|
254 { |
|
255 LOG_CFUNC |
|
256 TInt err(KErrNone); |
|
257 |
|
258 // Create a new active scheduler for this thread |
|
259 CActiveScheduler* sched = new (ELeave) CActiveScheduler; |
|
260 CleanupStack::PushL(sched); |
|
261 CActiveScheduler::Install(sched); |
|
262 |
|
263 CBaseTestCase* testCase = RTestFactory::CreateTestCaseL(aTestCaseId,ETrue); |
|
264 CleanupStack::PushL(testCase); |
|
265 |
|
266 // Do the test |
|
267 testCase->PerformTestL(); |
|
268 |
|
269 if(!testCase->IsHostOnly()) |
|
270 { |
|
271 // Loop for active objects |
|
272 RDebug::Printf("CActiveScheduler::Start in CThreadTestPolicy::DoTestL"); |
|
273 CActiveScheduler::Start(); |
|
274 } |
|
275 // Get the test case execution result |
|
276 err = testCase->TestResult(); |
|
277 |
|
278 // Destroy test case |
|
279 CleanupStack::PopAndDestroy(testCase); |
|
280 |
|
281 // Destroy the active scheduler |
|
282 CleanupStack::PopAndDestroy(sched); |
|
283 |
|
284 return err; |
|
285 } |
|
286 |
|
287 void CThreadTestPolicy::RunL() |
|
288 { |
|
289 LOG_FUNC |
|
290 TInt completionCode(iStatus.Int()); |
|
291 |
|
292 TExitType exitType(iTestThread.ExitType()); |
|
293 TExitCategoryName exitName(iTestThread.ExitCategory()); |
|
294 TInt exitReason(iTestThread.ExitReason()); |
|
295 RDebug::Printf("Test thread '%S' completed with completion code %d",&iTestThread.Name(),completionCode); |
|
296 |
|
297 switch(exitType) |
|
298 { |
|
299 // The test thread has panicked |
|
300 // This will occur if test API panics or RTest expression is false (i.e. test case fails) |
|
301 case EExitPanic: |
|
302 { |
|
303 RDebug::Printf("Test thread '%S' has panicked with category '%S' reason %d", |
|
304 &iTestThread.Name(),&exitName,exitReason); |
|
305 // May require to stop and start host/client USB depending on what panic category it is |
|
306 // can no longer trust RUsbHubDriver/RDevUsbcClient to be in good state |
|
307 completionCode = KErrAbort; |
|
308 } |
|
309 break; |
|
310 |
|
311 // The thread has been terminated |
|
312 case EExitTerminate: |
|
313 { |
|
314 RDebug::Printf("Test thread '%S' terminated with category %s reason %d", |
|
315 &iTestThread.Name(),&exitName,exitReason); |
|
316 } |
|
317 break; |
|
318 |
|
319 // The thread has been killed |
|
320 // This will occur when the test thread executes normally or is cancelled |
|
321 case EExitKill: |
|
322 { |
|
323 RDebug::Printf("Test thread '%S' has been killed with reason %d",&iTestThread.Name(),exitReason); |
|
324 } |
|
325 break; |
|
326 |
|
327 // These cases should not occur at this stage |
|
328 case EExitPending: //follow through |
|
329 default: |
|
330 gtest(EFalse); // Panic main thread |
|
331 break; |
|
332 } |
|
333 |
|
334 // Close the just executed test thread |
|
335 iTestThread.Close(); |
|
336 |
|
337 // Complete the notifier's request status |
|
338 User::RequestComplete(iNotifierStatus,completionCode); |
|
339 } |
|
340 |
|
341 TInt CThreadTestPolicy::RunError(TInt aError) |
|
342 { |
|
343 RDebug::Printf("<Error %d><Test Policy> RunError",aError); |
|
344 return KErrNone; |
|
345 } |
|
346 |
|
347 } |