Update contrib.
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 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 // e32test\pccd\t_medch.cpp
15 // Continuously generate media changes followesd by a remount of the peripheral bus controller.
19 #define __E32TEST_EXTENSION__
25 //#define __SOAK_TEST__ // Define to run until a key is pressed (Automatic tests only)
26 //#define __MANUAL_TEST__ // Define to allow manual control of the door/media
27 //#define __DEVICE_HAS_NO_DOOR__ // Define for devices that have no door (Manual tests only)
29 #if defined(__MANUAL_TEST__) && defined(__WINS__)
30 #define __DEVICE_HAS_NO_DOOR__
33 #if !defined(__MANUAL_TEST__) && defined(__DEVICE_HAS_NO_DOOR__)
34 #undef __DEVICE_HAS_NO_DOOR__
37 #if defined(__MANUAL_TEST__) && defined(__SOAK_TEST__)
43 const TInt KMaxTestTime = 5000000; // Run the test for 5 seconds on emulator
45 const TInt KMaxTestTime = 10000000; // Run the test for 10 seconds on target
49 #define MMC_PDD_NAME _L("MEDMMC")
51 const TInt KPowerUpTimeOut = 5000000; // Give the card 5 seconds to power up
53 LOCAL_D RTest test(_L("Media change test"));
55 LOCAL_D TBusLocalDrive TheDrive;
56 LOCAL_D RMedCh TheMediaChange;
57 LOCAL_D TRequestStatus TheMediaStatus;
58 LOCAL_D TBool TheChangedFlag;
61 LOCAL_C TBool SetupDrivesForPlatform(TInt& aDrive, TInt& aSocket)
63 * Finds a suitable drive for the media change test
65 * @param aDrive The number of the local drive to test
66 * @param aSocket The number of the socket to test
67 * @return TBool ETrue if a suitable drive is found, EFalse otherwise.
71 TDriveInfoV1Buf diBuf;
72 UserHal::DriveInfo(diBuf);
73 TDriveInfoV1 &di=diBuf();
78 for(aDrive=0; aDrive < di.iTotalSupportedDrives; aDrive++)
80 test.Printf(_L(" Drive %d - %S\r\n"), aDrive, &di.iDriveName[aDrive]);
81 if(di.iDriveName[aDrive].MatchF(_L("MultiMediaCard0")) == KErrNone)
85 if(aDrive == di.iTotalSupportedDrives)
87 test.Printf(_L(" MMC Drive Not Found\r\n"));
92 for(aSocket=0; aSocket < di.iTotalSockets; aSocket++)
94 test.Printf(_L("Socket %d - %S\r\n"), aSocket, &di.iSocketName[aSocket]);
95 if(di.iSocketName[aSocket].MatchF(_L("MultiMediaCard0")) == KErrNone)
99 if(aSocket == di.iTotalSockets)
101 test.Printf(_L(" MMC Socket Not Found\r\n"));
108 LOCAL_C void TestMediaAccess(TInt aExpectedError, TBool aExpectedChange)
110 * Tests that the drive is accessable (or not) by issuing a request
111 * to power up the media. Also verifies that the attributes are correct.
113 * @param aExpectedError The expected result of powering up the drive
114 * @param aExpectedChange ETrue if the changed flag is expected to be set
116 * @return ETrue if successful, EFalse otherwise
121 TInt r = rto.CreateLocal();
124 TRequestStatus rtoStat;
125 rto.After(rtoStat, KPowerUpTimeOut);
126 test(rtoStat == KRequestPending);
130 // TheChangedFlag is set when the door is opened if media was present.
131 // The asynch notifier is signalled when media is removed OR inserted.
132 User::WaitForRequest(TheMediaStatus, rtoStat);
133 test(TheMediaStatus != KRequestPending);
136 // ...aChangedFlag's purpose is to notify us of media removal.
137 test_Equal(aExpectedChange,TheChangedFlag);
139 TheDrive.NotifyChange(&TheMediaStatus);
140 TheChangedFlag = EFalse;
142 // Attempt to power up the drive
143 TLocalDriveCapsV2Buf info;
146 r = TheDrive.Caps(info);
148 while(r != aExpectedError && rtoStat == KRequestPending);
153 // ...was the error as expected?
154 test(r == aExpectedError);
156 // ...and are the caps still OK?
158 test(info().iType == EMediaHardDisk);
159 else if(r == KErrNotReady)
160 test(info().iType == EMediaNotPresent);
162 if(aExpectedChange == EFalse)
163 test(TheMediaStatus == KRequestPending);
166 LOCAL_C void NextTest(const TDesC& aTitle, TInt aCycles)
168 * Simply displays a string on the console and the current iteration.
170 * @param aTitle The text to be displayed
171 * @param aCycles The current iteration
174 test.Console()->SetPos(20, 25);
175 test.Printf(_L("%S [%d cycles]"), &aTitle, aCycles);
176 #ifdef __MANUAL_TEST__
177 test.Console()->SetPos(20, 27);
178 test.Printf(_L("<press a key>"));
183 GLDEF_C TInt E32Main()
185 * Test Entry Point for T_MEDCH.
187 * This test uses the associated driver (D_MEDCH) to simulate media removal and
188 * door opening/closing. The media is powered up in each state and verified that
189 * the correct error code and changed count is returned.
196 * Load the associated media driver (MEDMMC by default). This is required to ensure
197 * that the device can be powered up and the capabilities if the media accessed.
199 test.Start(_L("Load Media Driver"));
201 r=User::LoadPhysicalDevice(MMC_PDD_NAME);
204 test.Printf(_L("Test not supported on this platform \n"));
208 test(r==KErrNone || r==KErrAlreadyExists);
211 * Connect to the required local drive.
212 * TheChangedFlag is used for detection of media removal.
216 if(SetupDrivesForPlatform(drive, socket))
218 b.Format(_L("Connect to local drive %d"), drive);
220 TheDrive.Connect(drive, TheChangedFlag);
223 * Read the drive capabilities to ensure that this test may be run.
225 test.Next(_L("Get drive capabilities"));
226 TLocalDriveCapsV2Buf info;
227 r = TheDrive.Caps(info);
228 if(r == KErrNotReady || r == KErrNotSupported)
230 test.Next(_L("\r\nTest requires media to be present and the door closed - Disconnecting"));
231 TheDrive.Disconnect();
237 test(TheDrive.Caps(info) == KErrNone);
238 test(info().iType == EMediaHardDisk);
241 * Load the media simulation test driver
243 test.Next(_L("Load media change logical device"));
244 r=User::LoadLogicalDevice(_L("D_MEDCH"));
245 test(r == KErrNone || r == KErrAlreadyExists);
247 test.Next(_L("Open device"));
248 r=TheMediaChange.Open(socket, TheMediaChange.VersionRequired());
249 if(r == KErrNotSupported)
251 test.Next(_L("\r\nTest not supported on this drive - Disconnecting"));
252 r=User::FreeLogicalDevice(_L("MedCh"));
254 TheDrive.Disconnect();
261 * Verify that the system supports simulation of media change events
263 test.Next(_L("Test support for media change simulation"));
264 r = TheMediaChange.DoorNormal();
265 test(r == KErrNone || r == KErrNotSupported);
268 * Now for the real testing...
273 * Test0 - Simulate 2 consecutive door open interrupts
275 test.Next(_L("Test that the pbus can handle 2 consecutive door open interrupts"));
276 TheDrive.NotifyChange(&TheMediaStatus);
277 r = TheMediaChange.DoubleDoorOpen();
278 test(r == KErrNone || r == KErrNotSupported);
279 TestMediaAccess(KErrNone, ETrue);
283 #if defined(__SOAK_TEST__)
284 TRequestStatus endStat;
285 test.Console()->Read(endStat);
286 while(endStat == KRequestPending)
287 #elif !defined(__MANUAL_TEST__)
291 TRequestStatus endStat;
292 t.After(endStat, KMaxTestTime);
293 test(endStat == KRequestPending);
294 while(endStat == KRequestPending)
297 TheChangedFlag = EFalse;
299 TheDrive.NotifyChange(&TheMediaStatus);
302 * Test1 - Simulate door open
303 * - Power up responds with KErrNotReady
305 NextTest(_L("Open Door......"), cycles);
306 #ifndef __MANUAL_TEST__
307 test(TheMediaChange.DoorOpen() == KErrNone);
309 TestMediaAccess(KErrNotReady, ETrue);
310 TheDrive.NotifyChange(&TheMediaStatus);
313 * Test2 - Simulate door closed (with media removed)
314 * - Power up responds with KErrNotReady
316 #ifndef __DEVICE_HAS_NO_DOOR__
317 NextTest(_L("Remove Media..."), cycles);
318 #ifndef __MANUAL_TEST__
319 test(TheMediaChange.DoorClose(EFalse) == KErrNone);
321 TestMediaAccess(KErrNotReady, EFalse);
323 * Test3 - Simulate door open
324 * - Power up responds with KErrNotReady
326 NextTest(_L("Open Door......"), cycles);
327 #ifndef __MANUAL_TEST__
328 test(TheMediaChange.DoorOpen() == KErrNone);
330 TestMediaAccess(KErrNotReady, EFalse); // Power up responds with KErrNotReady
333 * Test4 - Simulate door closed (with media present)
334 * - Power up responds with KErrNone
336 NextTest(_L("Insert Media..."), cycles);
337 #ifndef __MANUAL_TEST__
338 test(TheMediaChange.DoorClose(ETrue) == KErrNone);
340 TestMediaAccess(KErrNone, ETrue);
344 test.Console()->SetPos(0, 27);
345 #if !defined(__SOAK_TEST__) && !defined(__MANUAL_TEST__)
349 else if(r == KErrNotSupported)
351 test.Printf(_L("Media change simulation not supported"));
357 test.Next(_L("\r\nClose device"));
358 TheMediaChange.Close();
360 test.Next(_L("Free device"));
361 r=User::FreeLogicalDevice(_L("MedCh"));
364 b.Format(_L("\r\nDisconnect from local drive %d "), drive);
366 TheDrive.Disconnect();