|
1 // Copyright (c) 1997-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 #include "ComponentTester.h" |
|
17 #include <ecom/test_bed/datalogger.h> |
|
18 #include <ecom/test_bed/testbeddefinitions.h> |
|
19 |
|
20 EXPORT_C CComponentTester::CComponentTester(CDataLogger& aDataLogger, |
|
21 MComponentTestObserver& aObserver) |
|
22 : CActive(CActive::EPriorityStandard), |
|
23 iDataLogger(aDataLogger), |
|
24 iObserver(aObserver) |
|
25 { |
|
26 CActiveScheduler::Add(this); |
|
27 } |
|
28 |
|
29 |
|
30 EXPORT_C CComponentTester::~CComponentTester() |
|
31 { |
|
32 Cancel(); |
|
33 |
|
34 // This should only be true during if TransitionSetsL() left and we are being |
|
35 // destroyed as part of cleanup |
|
36 if(iTransitionSets) |
|
37 { |
|
38 iTransitionSets->ResetAndDestroy(); |
|
39 delete iTransitionSets; |
|
40 } |
|
41 iParameterizedTests.Reset(); |
|
42 if(iUnitTests) |
|
43 { |
|
44 iUnitTests->ResetAndDestroy(); |
|
45 delete iUnitTests; |
|
46 } |
|
47 if(iUnitTestsToRun) |
|
48 { |
|
49 // We own the list but not the things on it. So reset the list and delete it |
|
50 iUnitTestsToRun->Reset(); |
|
51 delete iUnitTestsToRun; |
|
52 } |
|
53 } |
|
54 |
|
55 EXPORT_C void CComponentTester::ComponentTesterConstructL() |
|
56 { |
|
57 iUnitTests = new(ELeave) RPointerArray<CUnitTest>; |
|
58 } |
|
59 |
|
60 |
|
61 EXPORT_C RPointerArray<CUnitTestInfo>* CComponentTester::TransitionSetsL() const |
|
62 { |
|
63 // Create the array in a member variable to ensure correct cleanup but we do not |
|
64 // own this object. Ownership is passed at the return |
|
65 iTransitionSets = new(ELeave) RPointerArray<CUnitTestInfo>; |
|
66 |
|
67 if(iUnitTests) |
|
68 { |
|
69 TInt numTests = iUnitTests->Count(); |
|
70 for(TInt index = 0; index < numTests; index++) |
|
71 { |
|
72 CUnitTestInfo* newSet = (*iUnitTests)[index]->TransitionSetL(); |
|
73 CleanupStack::PushL(newSet); |
|
74 User::LeaveIfError(iTransitionSets->Append(newSet)); |
|
75 CleanupStack::Pop(newSet); // now owned by iTransitionSets |
|
76 } |
|
77 } |
|
78 |
|
79 // Return the pointer and null our member variable because we don't own it |
|
80 RPointerArray<CUnitTestInfo>* transitionSets = iTransitionSets; |
|
81 iTransitionSets = 0; |
|
82 return transitionSets; |
|
83 } |
|
84 |
|
85 EXPORT_C void CComponentTester::Complete(CUnitTest* aUnitTest) |
|
86 { |
|
87 if(iUnitTestsToRun != 0) |
|
88 { |
|
89 if(iCurrentUnitTest == iUnitTestsToRun->Count()) |
|
90 { |
|
91 iObserver.Complete(this, iUnitTests->Find(aUnitTest)); |
|
92 } |
|
93 else |
|
94 { |
|
95 TRequestStatus* status = &iStatus; |
|
96 User::RequestComplete(status, KErrNone); |
|
97 } |
|
98 } |
|
99 else |
|
100 { |
|
101 if(iCurrentUnitTest == iUnitTests->Count()) |
|
102 { |
|
103 iObserver.Complete(this, iUnitTests->Find(aUnitTest)); |
|
104 } |
|
105 else |
|
106 { |
|
107 TRequestStatus* status = &iStatus; |
|
108 User::RequestComplete(status, KErrNone); |
|
109 } |
|
110 } |
|
111 } |
|
112 |
|
113 EXPORT_C void CComponentTester::AddUnitTestL(const CUnitTest* aUnitTest) |
|
114 { |
|
115 CUnitTest* thisTest = CONST_CAST(CUnitTest*, aUnitTest); |
|
116 CleanupStack::PushL(thisTest); |
|
117 User::LeaveIfError(iUnitTests->Append(thisTest)); |
|
118 CleanupStack::Pop(thisTest); |
|
119 } |
|
120 |
|
121 EXPORT_C void CComponentTester::AddParamUnitTestL(const CUnitTest* aUnitTest) |
|
122 { |
|
123 CUnitTest* thisTest = CONST_CAST(CUnitTest*, aUnitTest); |
|
124 CleanupStack::PushL(thisTest); |
|
125 User::LeaveIfError(iUnitTests->Append(thisTest)); |
|
126 CleanupStack::Pop(thisTest); |
|
127 TInt testId = iUnitTests->Find(thisTest); |
|
128 User::LeaveIfError(iParameterizedTests.Append(testId)); |
|
129 } |
|
130 |
|
131 EXPORT_C void CComponentTester::TestComponent(RPointerArray<TTestInfo>* aTests) |
|
132 { |
|
133 _LIT(KStartingTestMessage, "Starting CComponentTester::TestComponent()..."); |
|
134 iDataLogger.LogInformation(KStartingTestMessage); |
|
135 |
|
136 iUnitTestsToRun = aTests; |
|
137 |
|
138 SetActive(); |
|
139 TRequestStatus* status = &iStatus; |
|
140 User::RequestComplete(status, KErrNone); |
|
141 } |
|
142 |
|
143 EXPORT_C void CComponentTester::RunL() |
|
144 { |
|
145 TBool lastTestRun = EFalse; |
|
146 if(iUnitTestsToRun == 0) |
|
147 { |
|
148 TBool startFromFirst = iCurrentUnitTest == 0; |
|
149 TBool haveRunTest = EFalse; |
|
150 while((iCurrentUnitTest < iUnitTests->Count()) && !haveRunTest) |
|
151 { |
|
152 if(iParameterizedTests.Find(iCurrentUnitTest) == -1) |
|
153 { |
|
154 (*iUnitTests)[iCurrentUnitTest]->PrepareUnitTestL(); |
|
155 (*iUnitTests)[iCurrentUnitTest]->RunTest(0); |
|
156 haveRunTest = ETrue; |
|
157 } |
|
158 ++iCurrentUnitTest; |
|
159 lastTestRun = iCurrentUnitTest == iUnitTests->Count(); |
|
160 } |
|
161 if(startFromFirst && !haveRunTest) |
|
162 iObserver.Complete(this, KErrNotFound); |
|
163 } |
|
164 else |
|
165 { |
|
166 if(iCurrentUnitTest < iUnitTestsToRun->Count()) |
|
167 { |
|
168 TInt testToRun = (*iUnitTestsToRun)[iCurrentUnitTest]->iUnitTestId; |
|
169 TTimeIntervalMicroSeconds32 time = (*iUnitTestsToRun)[iCurrentUnitTest]->iRunTime; |
|
170 (*iUnitTests)[testToRun]->SetParametersL((*iUnitTestsToRun)[iCurrentUnitTest]->iParameters); |
|
171 (*iUnitTests)[testToRun]->PrepareUnitTestL(); |
|
172 (*iUnitTests)[testToRun]->RunTest(time); |
|
173 ++iCurrentUnitTest; |
|
174 lastTestRun = iCurrentUnitTest == iUnitTestsToRun->Count(); |
|
175 } |
|
176 } |
|
177 |
|
178 // We don't need to be active for the last test because we don't RunL again |
|
179 if(!lastTestRun) |
|
180 { |
|
181 iStatus = KRequestPending; |
|
182 SetActive(); |
|
183 } |
|
184 } |
|
185 |
|
186 EXPORT_C void CComponentTester::DoCancel() |
|
187 { |
|
188 // If we have started a test then we have already advanced iCurrentUnitTest so cancel |
|
189 // the previous test |
|
190 if(iCurrentUnitTest > 0) |
|
191 (*iUnitTests)[iCurrentUnitTest - 1]->Cancel(); |
|
192 |
|
193 iObserver.Complete(this, KTestBedTestCancel); |
|
194 } |
|
195 |
|
196 EXPORT_C void CComponentTester::SetRTest(RTest* aRTest) |
|
197 { |
|
198 // Record a handle on the RTest object to use in component testing. |
|
199 iRTest = aRTest; |
|
200 |
|
201 // We have a new RTest, best tell the unit tests we know about |
|
202 for (int ut=0; ut < iUnitTests->Count(); ut++) |
|
203 (*iUnitTests)[ut]->SetRTest(aRTest); |
|
204 } |
|
205 |