Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // @file PBASE-T_USBDI-1231.cpp
19 #include "PBASE-T_USBDI-1231.h"
21 #include "testdebug.h"
22 #include "modelleddevices.h"
23 #include "TestPolicy.h"
25 namespace NUnitTesting_USBDI
28 _LIT(KTestCaseId,"PBASE-T_USBDI-1231");
29 // the name is very important
30 const TFunctorTestCase<CUT_PBASE_T_USBDI_1231,TBool>
31 CUT_PBASE_T_USBDI_1231::iFunctor(KTestCaseId);
33 CUT_PBASE_T_USBDI_1231* CUT_PBASE_T_USBDI_1231::NewL(TBool aHostRole)
35 CUT_PBASE_T_USBDI_1231* self = new (ELeave) CUT_PBASE_T_USBDI_1231(aHostRole);
36 CleanupStack::PushL(self);
38 CleanupStack::Pop(self);
42 CUT_PBASE_T_USBDI_1231::CUT_PBASE_T_USBDI_1231(TBool aHostRole) :
43 CBaseTestCase(KTestCaseId, aHostRole)
47 void CUT_PBASE_T_USBDI_1231::ConstructL()
49 RDebug::Printf("====> Constructor entry priority = %d", RThread().Priority());
51 // Collect existing thread priority (to reinstate later)
52 iPriority = RThread().Priority();
54 iTestDevice = new RUsbDeviceA(this);
58 CUT_PBASE_T_USBDI_1231::~CUT_PBASE_T_USBDI_1231()
62 RDebug::Printf("====> Destructor entry priority = %d", RThread().Priority());
64 // Reinstate original priority
66 RThread().SetPriority(iPriority);
68 // Cancel any async operations
70 Cancel(); // Cancel host timer
72 // Destroy the watchers
73 // they still use opened interfaces to cancel the suspend if active
74 delete iInterface1Watcher;
75 delete iInterface0Watcher;
77 // Close the interfaces
78 iUsbInterface1.Close();
79 iUsbInterface0.Close();
83 if (!IsHost() && iTestDevice)
90 void CUT_PBASE_T_USBDI_1231::ExecuteHostTestCaseL()
94 RDebug::Printf("====> ExecuteHostTestCaseL entry priority = %d",
95 RThread().Priority());
97 // Bump thread priority for this test only
99 RThread().SetPriority(EPriorityAbsoluteHigh);
100 RDebug::Printf("Thread priority raised %d->%d", iPriority, RThread().Priority());
102 iCaseStep = EInProcess;
103 iActorFDF = CActorFDF::NewL(*this);
104 iControlEp0 = new (ELeave) CEp0Transfer(iUsbInterface0);
105 iInterface0Watcher = new (ELeave) CInterfaceWatcher(iUsbInterface0,TCallBack(CUT_PBASE_T_USBDI_1231::Interface0ResumedL,this));
106 iInterface1Watcher = new (ELeave) CInterfaceWatcher(iUsbInterface1,TCallBack(CUT_PBASE_T_USBDI_1231::Interface1ResumedL,this));
108 // Monitor for device connections
109 iActorFDF->Monitor();
111 // Start the connection timeout
115 void CUT_PBASE_T_USBDI_1231::ExecuteDeviceTestCaseL()
119 // Construct the device for the test case
120 iTestDevice->OpenL(TestCaseId());
121 iTestDevice->SubscribeToReports(iStatus);
124 // Connect the test device
125 iTestDevice->SoftwareConnect();
128 void CUT_PBASE_T_USBDI_1231::HostDoCancel()
132 RDebug::Printf("====> HostDoCancel entry priority = %d", RThread().Priority());
134 // Cancel the timeout timer
138 void CUT_PBASE_T_USBDI_1231::DeviceDoCancel()
143 iTestDevice->CancelSubscriptionToReports();
146 void CUT_PBASE_T_USBDI_1231::DeviceInsertedL(TUint aDeviceHandle)
150 RDebug::Printf("====> DeviceInsertedL entry priority = %d", RThread().Priority());
152 Cancel(); // Cancel the timer
154 iDeviceHandle = aDeviceHandle;
155 iActorFDF->Monitor();
157 // Validate that device is as expected
158 CUsbTestDevice& testDevice = iActorFDF->DeviceL(aDeviceHandle);
159 if (testDevice.SerialNumber().Compare(TestCaseId()) != 0)
161 // Incorrect device for this test case
164 "<Warning %d> Incorrect device serial number (%S) connected for this test case (%S)",
165 KErrNotFound, &testDevice.SerialNumber(), &TestCaseId());
167 // Start the connection timeout again
172 CHECK(CheckTreeAfterDeviceInsertion(testDevice, _L("RDeviceA")) == KErrNone);
174 // Perform the correct test step
182 RDebug::Printf("Obtaining token for interface 0");
183 err = testDevice.Device().GetTokenForInterface(0, token1);
187 "<Error %d> Token for interface 0 could not be retrieved",
189 return TestFailed(err);
191 RDebug::Printf("Token 1 (%d) retrieved", token1);
192 RDebug::Printf("Opening interface 0");
193 err = iUsbInterface0.Open(token1); // Alternate interface setting 0
197 "<Error %d> Interface 0 could not be opened", err);
198 return TestFailed(err);
200 RDebug::Printf("Interface 0 opened");
202 RDebug::Printf("Obtaining token for interface 1");
203 err = testDevice.Device().GetTokenForInterface(1, token2);
207 "<Error %d> Token for interface 1 could not be retrieved",
209 return TestFailed(err);
211 RDebug::Printf("Opening interface 1");
212 err = iUsbInterface1.Open(token2); // Alternate interface setting 0
216 "<Error %d> Interface 1 could not be opened", err);
217 return TestFailed(err);
219 RDebug::Printf("Interface 1 opened");
221 ResumeWhenSuspending();
227 TestFailed(KErrCorrupt);
232 TInt CUT_PBASE_T_USBDI_1231::Interface0ResumedL(TAny* aPtr)
236 RDebug::Printf("====> Interface0ResumedL entry priority = %d", RThread().Priority());
238 RDebug::Printf("-Interface 0 resumed");
239 CUT_PBASE_T_USBDI_1231* self =
240 reinterpret_cast<CUT_PBASE_T_USBDI_1231*>(aPtr);
242 TInt completionCode=self->iInterface0Watcher->CompletionCode();
244 TInt testStep = self->iCaseStep;
245 RDebug::Printf(" -watcher 0 iStatus = %d <teststep %d>",completionCode, testStep);
248 switch (self->iCaseStep)
251 case EValidResumeWhenSuspending:
253 if (completionCode == KErrNone)
255 RDebug::Printf("Device resume while suspending succeed!");
257 self->iCaseStep = EPassed;
258 self->SendEp0Request();
263 "Device resume while suspending failed,<err %d>",
265 self->iCaseStep = EFailed;
266 self->SendEp0Request();
278 TInt CUT_PBASE_T_USBDI_1231::Interface1ResumedL(TAny* aPtr)
282 RDebug::Printf("====> Interface1ResumedL entry priority = %d", RThread().Priority());
284 RDebug::Printf("Interface 1 resumed");
285 CUT_PBASE_T_USBDI_1231* self =
286 reinterpret_cast<CUT_PBASE_T_USBDI_1231*>(aPtr);
287 RDebug::Printf("watcher 1 iStatus=%d",
288 self->iInterface1Watcher->CompletionCode());
292 void CUT_PBASE_T_USBDI_1231::DeviceRemovedL(TUint aDeviceHandle)
296 // The test device should not be removed until the test case has passed
297 // so this test case has not completed, and state this event as an error
299 TestFailed(KErrDisconnected);
302 void CUT_PBASE_T_USBDI_1231::BusErrorL(TInt aError)
306 // This test case handles no failiures on the bus
311 void CUT_PBASE_T_USBDI_1231::DeviceStateChangeL(
312 RUsbDevice::TDeviceState aPreviousState,
313 RUsbDevice::TDeviceState aNewState, TInt aCompletionCode)
318 // test RInterface , the RUsbDevice notification logic not used .
319 RDebug::Printf(" -Device State change from %d to %d err=%d",
320 aPreviousState, aNewState, aCompletionCode);
324 case EValidDeviceSuspend:
325 if (aNewState == RUsbDevice::EDeviceSuspended)
327 RDebug::Printf("Device suspend!");
328 iCaseStep = EValidDeviceResume;
332 RDebug::Printf("Device suspend failed!");
337 case EValidDeviceResume:
339 if (aNewState == RUsbDevice::EDeviceActive)
341 RDebug::Printf("Device resume!");
342 iCaseStep = EValidResumeWhenSuspending;
358 void CUT_PBASE_T_USBDI_1231::Ep0TransferCompleteL(TInt aCompletionCode)
361 RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d",
368 TestFailed(KErrCompletion);
377 void CUT_PBASE_T_USBDI_1231::HostRunL()
381 RDebug::Printf("====> HostRunL entry priority = %d", RThread().Priority());
383 // Obtain the completion code
384 TInt completionCode(iStatus.Int());
386 if (completionCode == KErrNone)
389 RDebug::Printf("<Error> Action timeout");
390 TestFailed(KErrTimedOut);
394 RDebug::Printf("<Error %d> Timeout timer could not complete",
396 TestFailed(completionCode);
400 void CUT_PBASE_T_USBDI_1231::DeviceRunL()
404 // Disconnect the device
406 iTestDevice->SoftwareDisconnect();
408 // Complete the test case request
410 TestPolicy().SignalTestComplete(iStatus.Int());
413 void CUT_PBASE_T_USBDI_1231::ResumeWhenSuspending()
415 RDebug::Printf("====> ResumeWhenSuspending entry priority = %d",
416 RThread().Priority());
418 // Suspend interface 0
419 RDebug::Printf("Suspending interface 0");
420 iInterface0Watcher->SuspendAndWatch();
422 // Suspend interface 1
423 RDebug::Printf("Suspending interface 1");
424 iInterface1Watcher->SuspendAndWatch();
426 // Cancel suspend-in-progress
427 RDebug::Printf("Cancel Suspend interface 0");
428 iUsbInterface0.CancelPermitSuspend();
430 iCaseStep = EValidDeviceSuspend;
434 void CUT_PBASE_T_USBDI_1231::SendEp0Request()
436 TTestCasePassed request;
437 iControlEp0->SendRequest(request, this);