VolumeInfo reads label, file system, serial, capacity, drive type and DCIM
presence (no "insert disk" prompts) and exposes the card identity
{serial:X8}-{label}-{capacity}. RemovableVolumeWatcher runs a hidden
top-level window on its own message loop for WM_DEVICECHANGE volume unit
masks (message-only windows receive no broadcasts) plus a 3 s DriveInfo poll,
diffing snapshots so each change is raised once. VolumeEjector locks
(with retries), dismounts, allows removal and ejects; FormatDialog shows
SHFormatDrive with quick format preselected and refuses the system drive.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
99 lines
4.0 KiB
C#
99 lines
4.0 KiB
C#
using System.Text.RegularExpressions;
|
|
using Titano.Core.Windows.Devices;
|
|
using Titano.Testing;
|
|
|
|
namespace Titano.Core.Windows.Tests.Devices;
|
|
|
|
public sealed class VolumeTests
|
|
{
|
|
[Test]
|
|
[WindowsOnly]
|
|
public void ListingIncludesTheSystemDriveWithItsSerial()
|
|
{
|
|
var systemRoot = Path.GetPathRoot(Environment.SystemDirectory)!;
|
|
var volumes = VolumeInfo.ListLocal();
|
|
var system = volumes.FirstOrDefault(v => string.Equals(v.RootPath, systemRoot, StringComparison.OrdinalIgnoreCase));
|
|
if (system is null) Assert.Fail($"{systemRoot} missing from [{string.Join(", ", volumes.Select(v => v.RootPath))}]");
|
|
Assert.NotEqual(0u, system!.SerialNumber);
|
|
Assert.Greater(system.TotalBytes, 0);
|
|
Assert.InRange(system.FreeBytes, 0, system.TotalBytes);
|
|
Assert.Equal(VolumeDriveType.Fixed, system.DriveType);
|
|
Assert.False(string.IsNullOrEmpty(system.FileSystem));
|
|
Assert.True(Regex.IsMatch(system.Identity, "^[0-9A-F]{8}-.*-[0-9]+$"), system.Identity);
|
|
}
|
|
|
|
[Test]
|
|
[WindowsOnly]
|
|
public void MissingDriveLetterReadsAsNull()
|
|
{
|
|
var used = DriveInfo.GetDrives().Select(d => char.ToUpperInvariant(d.Name[0])).ToHashSet();
|
|
var free = Enumerable.Range('D', 23).Select(c => (char)c).Where(c => !used.Contains(c)).ToArray();
|
|
if (free.Length == 0) TestContext.Skip("Every drive letter is in use.");
|
|
Assert.Null(VolumeInfo.TryRead($"{free[^1]}:\\"));
|
|
}
|
|
|
|
[Test]
|
|
[WindowsOnly]
|
|
public void RootsAreNormalised()
|
|
{
|
|
Assert.Equal(@"E:\", VolumeInfo.NormalizeRoot("e:"));
|
|
Assert.Equal(@"E:\", VolumeInfo.NormalizeRoot(@"E:\"));
|
|
Assert.Equal(@"E:\", VolumeInfo.NormalizeRoot(@"e:\DCIM\100CANON"));
|
|
}
|
|
|
|
[Test]
|
|
public void IdentityFollowsTheBriefFormat()
|
|
{
|
|
var card = new VolumeInfo(@"E:\", "EOS_DIGITAL", "exFAT", 0x3F2A9C11, 127_865_454_592, 1_000, VolumeDriveType.Removable, true);
|
|
Assert.Equal("3F2A9C11-EOS_DIGITAL-127865454592", card.Identity);
|
|
Assert.Equal('E', card.DriveLetter);
|
|
Assert.Equal("0000000A--1024", (card with { SerialNumber = 10, Label = "", TotalBytes = 1024 }).Identity);
|
|
}
|
|
|
|
[Test]
|
|
public void CandidatesAreRemovableOrCameraVolumes()
|
|
{
|
|
var card = new VolumeInfo(@"E:\", "", "exFAT", 1, 1, 1, VolumeDriveType.Removable, false);
|
|
Assert.True(RemovableVolumeWatcher.IsCandidate(card));
|
|
Assert.False(RemovableVolumeWatcher.IsCandidate(card with { DriveType = VolumeDriveType.Fixed }));
|
|
Assert.True(RemovableVolumeWatcher.IsCandidate(card with { DriveType = VolumeDriveType.Fixed, HasDcim = true }));
|
|
Assert.False(RemovableVolumeWatcher.IsCandidate(card with { DriveType = VolumeDriveType.Remote, HasDcim = true }));
|
|
}
|
|
|
|
[Test]
|
|
[WindowsOnly]
|
|
[Timeout(30_000)]
|
|
public void WatcherStartsAndStops()
|
|
{
|
|
using var watcher = new RemovableVolumeWatcher();
|
|
watcher.Start();
|
|
Assert.True(watcher.Volumes.All(RemovableVolumeWatcher.IsCandidate));
|
|
Assert.Throws<InvalidOperationException>(watcher.Start);
|
|
watcher.Dispose();
|
|
Assert.Throws<ObjectDisposedException>(watcher.Start);
|
|
}
|
|
|
|
[Test]
|
|
[WindowsOnly]
|
|
public void EjectRefusesTheSystemDrive()
|
|
{
|
|
var letter = Path.GetPathRoot(Environment.SystemDirectory)![0];
|
|
Assert.Equal(EjectResult.NotRemovable, VolumeEjector.Eject(letter));
|
|
}
|
|
|
|
[Test]
|
|
public void EjectRejectsNonLetters() => Assert.Throws<ArgumentOutOfRangeException>(() => VolumeEjector.Eject('1'));
|
|
|
|
[Test]
|
|
public void FormatDialogNeedsAnOwnerWindow() => Assert.Throws<ArgumentException>(() => FormatDialog.Show(0, 'E'));
|
|
|
|
[Test]
|
|
[WindowsOnly]
|
|
public void FormatDialogRefusesTheSystemDrive()
|
|
{
|
|
var letter = Path.GetPathRoot(Environment.SystemDirectory)![0];
|
|
// The guard runs before the dialog, so any non-null owner handle exercises it without showing UI.
|
|
Assert.Throws<InvalidOperationException>(() => FormatDialog.Show(1, letter));
|
|
}
|
|
}
|