|
| 1 | +// |
| 2 | +// Copyright (c) .NET Foundation and Contributors |
| 3 | +// See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +using nanoFramework.System.IO.FileSystem; |
| 7 | +using nanoFramework.TestFramework; |
| 8 | +using System.Threading; |
| 9 | + |
| 10 | +namespace System.IO.FileSystem.UnitTests |
| 11 | +{ |
| 12 | + [TestClass] |
| 13 | + public abstract class FileSystemUnitTestsBase |
| 14 | + { |
| 15 | + ///////////////////////////////////////////////////////////////////// |
| 16 | + // The test execution can be configured using the following fields // |
| 17 | + ///////////////////////////////////////////////////////////////////// |
| 18 | + |
| 19 | + // set to the number of drives available in the target |
| 20 | + internal const int _numberOfDrives = 1; |
| 21 | + |
| 22 | + // set to the root of the drive to use for the tests |
| 23 | + // D: SD card |
| 24 | + // E: USB mass storage |
| 25 | + // I: and J: internal flash |
| 26 | + internal const string Root = @"I:\"; |
| 27 | + |
| 28 | + // set to true to wait for removable drive(s) to be mounted |
| 29 | + internal const bool _waitForRemovableDrive = false; |
| 30 | + |
| 31 | + // set to true to have SPI SD card mounted |
| 32 | + internal const bool _configAndMountSdCard = false; |
| 33 | + |
| 34 | + ////////////////////////////////////////////////// |
| 35 | + |
| 36 | + private SDCard _mycardBacking; |
| 37 | + |
| 38 | + internal SDCard MyCard |
| 39 | + { |
| 40 | + set |
| 41 | + { |
| 42 | + _mycardBacking = value; |
| 43 | + } |
| 44 | + |
| 45 | + get |
| 46 | + { |
| 47 | + _mycardBacking ??= InitializeSDCard(); |
| 48 | + |
| 49 | + return _mycardBacking; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Initializes the SD card. Can be overridden in the derived class to provide specific initialization. |
| 55 | + /// </summary> |
| 56 | + /// <returns></returns> |
| 57 | + protected SDCard InitializeSDCard() |
| 58 | + { |
| 59 | + // Example initialization logic |
| 60 | + SDCard.SDCardMmcParameters parameters = new SDCard.SDCardMmcParameters |
| 61 | + { |
| 62 | + dataWidth = SDCard.SDDataWidth._4_bit, |
| 63 | + enableCardDetectPin = true, |
| 64 | + cardDetectPin = 21 |
| 65 | + }; |
| 66 | + |
| 67 | + return new SDCard(parameters); |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Helper method to be called from the tests to handle removable drives. |
| 72 | + /// </summary> |
| 73 | + internal void RemovableDrivesHelper() |
| 74 | + { |
| 75 | + if (_configAndMountSdCard) |
| 76 | + { |
| 77 | + TryToMountAgain: |
| 78 | + |
| 79 | + try |
| 80 | + { |
| 81 | + MyCard.Mount(); |
| 82 | + } |
| 83 | + catch (Exception ex) |
| 84 | + { |
| 85 | + OutputHelper.WriteLine($"SDCard mount failed: {ex.Message}"); |
| 86 | + |
| 87 | + Thread.Sleep(TimeSpan.FromSeconds(2)); |
| 88 | + |
| 89 | + MyCard = null; |
| 90 | + |
| 91 | + goto TryToMountAgain; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if (_waitForRemovableDrive) |
| 96 | + { |
| 97 | + // wait until all removable drives are mounted |
| 98 | + while (DriveInfo.GetDrives().Length < _numberOfDrives) |
| 99 | + { |
| 100 | + Thread.Sleep(1000); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments