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-0473.cpp
19 #include "PBASE-T_USBDI-0473.h"
21 #include "testdebug.h"
22 #include "modelleddevices.h"
23 #include "TestPolicy.h"
25 namespace NUnitTesting_USBDI
28 _LIT(KTestCaseId,"PBASE-T_USBDI-0473");
29 const TFunctorTestCase<CUT_PBASE_T_USBDI_0473,TBool> CUT_PBASE_T_USBDI_0473::iFunctor(KTestCaseId);
31 CUT_PBASE_T_USBDI_0473* CUT_PBASE_T_USBDI_0473::NewL(TBool aHostRole)
33 CUT_PBASE_T_USBDI_0473* self = new (ELeave) CUT_PBASE_T_USBDI_0473(aHostRole);
34 CleanupStack::PushL(self);
36 CleanupStack::Pop(self);
41 CUT_PBASE_T_USBDI_0473::CUT_PBASE_T_USBDI_0473(TBool aHostRole)
42 : CBaseTestCase(KTestCaseId,aHostRole),
45 iDeviceNotificationPending(ETrue)
50 void CUT_PBASE_T_USBDI_0473::ConstructL()
52 iTestDevice = new RUsbDeviceA(this);
57 CUT_PBASE_T_USBDI_0473::~CUT_PBASE_T_USBDI_0473()
61 // Cancel any async operations
63 Cancel(); // Cancel host timer
65 // Destroy the watchers
66 // they still use opened interfaces to cancel the suspend if active
67 delete iInterface1Watcher;
68 delete iInterface0Watcher;
70 // Close the interfaces
71 iUsbInterface1.Close();
72 iUsbInterface0.Close();
76 if(!IsHost() && iTestDevice)
84 void CUT_PBASE_T_USBDI_0473::ExecuteHostTestCaseL()
87 iCaseStep = EStepSuspend;
88 iActorFDF = CActorFDF::NewL(*this);
89 iControlEp0 = new (ELeave) CEp0Transfer(iUsbInterface0);
90 iInterface0Watcher = new (ELeave) CInterfaceWatcher(iUsbInterface0,TCallBack(CUT_PBASE_T_USBDI_0473::Interface0ResumedL,this));
91 iInterface1Watcher = new (ELeave) CInterfaceWatcher(iUsbInterface1,TCallBack(CUT_PBASE_T_USBDI_0473::Interface1ResumedL,this));
93 // Monitor for device connections
96 // Start the connection timeout
100 void CUT_PBASE_T_USBDI_0473::ExecuteDeviceTestCaseL()
104 // Construct the device for the test case
105 iTestDevice->OpenL(TestCaseId());
106 iTestDevice->SubscribeToReports(iStatus);
109 // Connect the test device
110 iTestDevice->SoftwareConnect();
114 void CUT_PBASE_T_USBDI_0473::HostDoCancel()
118 // Cancel the timeout timer
123 void CUT_PBASE_T_USBDI_0473::DeviceDoCancel()
128 iTestDevice->CancelSubscriptionToReports();
131 void CUT_PBASE_T_USBDI_0473::DeviceInsertedL(TUint aDeviceHandle)
135 Cancel(); // Cancel the timer
137 iDeviceHandle = aDeviceHandle;
138 iActorFDF->Monitor();
140 // Validate that device is as expected
141 CUsbTestDevice& testDevice = iActorFDF->DeviceL(aDeviceHandle);
142 if(testDevice.SerialNumber().Compare(TestCaseId()) != 0)
144 // Incorrect device for this test case
146 RDebug::Printf("<Warning %d> Incorrect device serial number (%S) connected for this test case (%S)",
147 KErrNotFound,&testDevice.SerialNumber(),&TestCaseId());
149 // Start the connection timeout again
154 CHECK(CheckTreeAfterDeviceInsertion(testDevice, _L("RDeviceA")) == KErrNone);
156 // Perform the correct test step
164 RDebug::Printf("Obtaining token for interface 0");
165 err = testDevice.Device().GetTokenForInterface(0,token1);
168 RDebug::Printf("<Error %d> Token for interface 0 could not be retrieved",err);
169 return TestFailed(err);
171 RDebug::Printf("Token 1 (%d) retrieved",token1);
172 RDebug::Printf("Opening interface 0");
173 err = iUsbInterface0.Open(token1); // Alternate interface setting 0
176 RDebug::Printf("<Error %d> Interface 0 could not be opened",err);
177 return TestFailed(err);
179 RDebug::Printf("Interface 0 opened");
182 RDebug::Printf("Obtaining token for interface 1");
183 err = testDevice.Device().GetTokenForInterface(1,token2);
186 RDebug::Printf("<Error %d> Token for interface 1 could not be retrieved",err);
187 return TestFailed(err);
189 RDebug::Printf("Opening interface 1");
190 err = iUsbInterface1.Open(token2); // Alternate interface setting 0
193 RDebug::Printf("<Error %d> Interface 1 could not be opened",err);
194 return TestFailed(err);
196 RDebug::Printf("Interface 1 opened");
199 iUsbInterface1.Close();
200 RDebug::Printf("Interface 1 closed");
203 err = iUsbInterface1.Open(token2); // Alternate interface setting 0
206 RDebug::Printf("<Error %d> Interface 1 could not be re-opened",err);
207 return TestFailed(err);
209 RDebug::Printf("Interface 1 re-opened");
212 // Suspend interface 0
213 RDebug::Printf("Suspending interface 0");
214 iInterface0Watcher->SuspendAndWatch();
215 iSuspendedI0 = ETrue;
217 // Suspend interface 1
218 RDebug::Printf("Suspending interface 1");
219 iInterface1Watcher->SuspendAndWatch();
220 iSuspendedI1 = ETrue;
222 iCaseStep = EValidateSuspendingInterfaces;
223 TimeoutIn(10); // Give 10 seconds for device to suspend
228 TestFailed(KErrCorrupt);
234 TInt CUT_PBASE_T_USBDI_0473::Interface0ResumedL(TAny* aPtr)
237 RDebug::Printf("Interface 0 resumed");
238 CUT_PBASE_T_USBDI_0473* self = reinterpret_cast<CUT_PBASE_T_USBDI_0473*>(aPtr);
239 RDebug::Printf("watcher 0 iStatus=%d",self->iInterface0Watcher->CompletionCode());
240 self->iSuspendedI0 = EFalse;
241 return self->CheckForAllResumedNotificationsAndContinueFSM();
245 TInt CUT_PBASE_T_USBDI_0473::Interface1ResumedL(TAny* aPtr)
248 RDebug::Printf("Interface 1 resumed");
249 CUT_PBASE_T_USBDI_0473* self = reinterpret_cast<CUT_PBASE_T_USBDI_0473*>(aPtr);
250 RDebug::Printf("watcher 1 iStatus=%d",self->iInterface1Watcher->CompletionCode());
251 self->iSuspendedI1 = EFalse;
252 return self->CheckForAllResumedNotificationsAndContinueFSM();
256 void CUT_PBASE_T_USBDI_0473::DeviceRemovedL(TUint aDeviceHandle)
260 // The test device should not be removed until the test case has passed
261 // so this test case has not completed, and state this event as an error
263 TestFailed(KErrDisconnected);
267 void CUT_PBASE_T_USBDI_0473::BusErrorL(TInt aError)
271 // This test case handles no failiures on the bus
276 TInt CUT_PBASE_T_USBDI_0473::CheckForAllResumedNotificationsAndContinueFSM()
279 TBool readyToContinueFSM= ETrue;
280 if( iInterface0Watcher->IsActive()
281 || iInterface0Watcher->iStatus == KRequestPending)
283 RDebug::Printf("Interface 0 watcher still pending");
284 readyToContinueFSM= EFalse;
287 if( iInterface1Watcher->IsActive()
288 || iInterface1Watcher->iStatus == KRequestPending)
290 RDebug::Printf("Interface 1 watcher still pending");
291 readyToContinueFSM= EFalse;
294 if( iDeviceNotificationPending)
296 readyToContinueFSM= EFalse;
299 if( readyToContinueFSM)
301 return ContinueFSMAfterAllResumedNotifications();
309 TInt CUT_PBASE_T_USBDI_0473::ContinueFSMAfterAllResumedNotifications()
312 iDeviceNotificationPending= ETrue;
315 RDebug::Printf("<Error %d> Interface 0 still suspended",KErrCompletion);
316 TestFailed(KErrCompletion);
317 return KErrCompletion;
322 RDebug::Printf("<Error %d> Interface 1 still suspended",KErrCompletion);
323 TestFailed(KErrCompletion);
324 return KErrCompletion;
329 case EValidateResumptionAfterInterfaceSuspension:
331 // Device is resumed, send request to client: Remote wake up in 6 secs
332 TInt err= iUsbInterface1.PermitRemoteWakeup(ETrue);
336 RDebug::Printf("<Error %d> Unable to permit remote device wakeup",err);
338 TTestCaseFailed request(err,_L8("Unable to permit remote device wakeup"));
339 iControlEp0->SendRequest(request,this);
343 RDebug::Printf("Device is resumed, send request to client: Remote wake up in 6 secs");
345 TRemoteWakeupRequest request(6);
346 iControlEp0->SendRequest(request,this);
347 iCaseStep = ESuspendForRemoteWakeup;
352 case EValidateResumptionAfterWakeup:
354 if(iStoredNewState == RUsbDevice::EDeviceActive)
356 // Now suspend the device again after resumption from remote wakeup
358 RDebug::Printf("Suspending interface 0");
359 iInterface0Watcher->SuspendAndWatch();
360 iSuspendedI0 = ETrue;
362 RDebug::Printf("Suspending interface 1");
363 iInterface1Watcher->SuspendAndWatch();
364 iSuspendedI1 = ETrue;
366 iCaseStep = EValidateSuspendAfterWakeup;
370 RDebug::Printf("<Error %d> Device is still suspended",KErrCompletion);
371 TestFailed(KErrCompletion);
372 return KErrCompletion;
378 RDebug::Printf("CUT_PBASE_T_USBDI_0473::ContinueFSMAfterAllResumedNotifications: Invalid state %d", iCaseStep);
379 TestFailed(KErrCompletion);
380 return KErrCompletion;
386 void CUT_PBASE_T_USBDI_0473::DeviceStateChangeL(RUsbDevice::TDeviceState aPreviousState,RUsbDevice::TDeviceState aNewState,TInt aCompletionCode)
391 RDebug::Printf("Device State change from %d to %d err=%d",aPreviousState,aNewState,aCompletionCode);
396 // Validate that the device was suspended by individual interface suspension
398 case EValidateSuspendingInterfaces:
400 if(aNewState == RUsbDevice::EDeviceSuspended)
402 // Device state is suspended now resume it by resuming one of the interfaces
404 RDebug::Printf("Device is suspended now resume device by resuming one of the interfaces");
405 iUsbInterface0.CancelPermitSuspend();
406 iCaseStep = EValidateResumptionAfterInterfaceSuspension;
410 RDebug::Printf("<Error %d> State was not suspended",KErrCompletion);
412 // Since the device is not suspended, send test case failed to the device
415 TTestCaseFailed request(KErrCompletion,_L8("The device was not in the expected suspend state"));
416 iControlEp0->SendRequest(request,this);
421 // Validate that device is now active after resuming one of the interfaces
423 case EValidateResumptionAfterInterfaceSuspension:
425 iDeviceNotificationPending= EFalse;
427 if(aNewState == RUsbDevice::EDeviceActive)
429 CheckForAllResumedNotificationsAndContinueFSM();
433 RDebug::Printf("<Error %d> Device is still suspended",KErrCompletion);
434 return TestFailed(KErrCompletion);
439 // Validate that the device is now suspended for the device to remote-wakeup
440 case EValidateSuspendForRemoteWakeup:
442 if(aNewState == RUsbDevice::EDeviceSuspended)
444 // Now awaiting a remote wake up state change notification
446 RDebug::Printf("Now awaiting a remote wake up state change notification");
449 iTimer.After(iStatus,10000000); // Give 10 seconds for device to signal remote wake-up
450 iCaseStep = EValidateResumptionAfterWakeup;
455 RDebug::Printf("<Error %d> State was not suspended",KErrCompletion);
457 // Since the device is not suspended, send test case failed to the device
460 TTestCaseFailed request(KErrCompletion,_L8("State was not suspended"));
461 iControlEp0->SendRequest(request,this);
466 // This step should never be reached as ep0 complete traps this step, but if it does test fails.
467 case ESuspendForRemoteWakeup:
469 RDebug::Printf("Resumed before suspended");
471 TTestCaseFailed request(KErrCompletion,_L8("State was not suspended"));
472 iControlEp0->SendRequest(request,this);
476 // Validate that the device is now active from a remote wakeup
477 case EValidateResumptionAfterWakeup:
479 iDeviceNotificationPending= EFalse;
480 iStoredNewState= aNewState;
481 CheckForAllResumedNotificationsAndContinueFSM();
485 // Validate that the device can be suspended after a remote wakeup
487 case EValidateSuspendAfterWakeup:
489 // Successfully suspended after a remote wake up event
490 if(aNewState == RUsbDevice::EDeviceSuspended)
492 // Device is now suspended, now activate the device again to send test case
493 // completed request to device
495 RDebug::Printf("Device is now suspended, now activate the device again to send test case completed request to device");
497 CUsbTestDevice& testDevice = iActorFDF->DeviceL(iDeviceHandle);
499 RDebug::Printf("Resuming at device level");
500 TInt err(testDevice.Device().Resume());
503 RDebug::Printf("<Error %d> Unable to suspend the device",err);
505 TTestCaseFailed request(err,_L8("Unable to suspend the device"));
506 iControlEp0->SendRequest(request,this);
513 RDebug::Printf("<Error %d> State was not suspended",KErrCompletion);
515 // Since the device is not suspended, send test case failed to the device
518 TTestCaseFailed request(KErrCompletion,_L8("State was not suspended"));
519 iControlEp0->SendRequest(request,this);
524 // Validate that the device is now active again
528 if(aNewState == RUsbDevice::EDeviceActive)
530 RDebug::Printf("Device is active again, test case passed");
531 TTestCasePassed request;
532 iControlEp0->SendRequest(request,this);
536 RDebug::Printf("<Error %d> Device is still suspended",KErrCompletion);
537 return TestFailed(KErrCompletion);
548 void CUT_PBASE_T_USBDI_0473::Ep0TransferCompleteL(TInt aCompletionCode)
551 RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d",aCompletionCode);
554 case ESuspendForRemoteWakeup:
556 // Suspend device again so a remote wakeup can be achieved
557 RDebug::Printf("Suspend device again so a remote wakeup can be achieved");
559 // Suspend interface 0
560 RDebug::Printf("Suspending interface 0");
561 iInterface0Watcher->SuspendAndWatch();
562 iSuspendedI0 = ETrue;
564 // Suspend interface 1
565 RDebug::Printf("Suspending interface 1");
566 iInterface1Watcher->SuspendAndWatch();
567 iSuspendedI1 = ETrue;
569 iCaseStep = EValidateSuspendForRemoteWakeup;
570 TimeoutIn(10); // Give 10 seconds for device to suspend
574 // Fail the test case
578 TestFailed(KErrCompletion);
581 // Pass the test case
590 void CUT_PBASE_T_USBDI_0473::HostRunL()
594 // Obtain the completion code
595 TInt completionCode(iStatus.Int());
597 if(completionCode == KErrNone)
600 RDebug::Printf("<Error> Action timeout");
601 TestFailed(KErrTimedOut);
605 RDebug::Printf("<Error %d> Timeout timer could not complete",completionCode);
606 TestFailed(completionCode);
611 void CUT_PBASE_T_USBDI_0473::DeviceRunL()
615 // Disconnect the device
617 iTestDevice->SoftwareDisconnect();
619 // Complete the test case request
621 TestPolicy().SignalTestComplete(iStatus.Int());