Skip to content

Commit

Permalink
Augmented ShellDataObject to confirm equivalent streams (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Hall committed Feb 3, 2025
1 parent 4ca8630 commit 06ceb27
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions UnitTests/Windows.Shell/ShellDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using Vanara.PInvoke.Tests;
Expand Down Expand Up @@ -93,19 +94,32 @@ public void ShellItemDataTest()
[Test]
public void GetDataTest()
{
var obj = new ShellDataObject(new[] { new ShellItem(TestCaseSources.WordDoc) });
const int cnt = 512;

byte[] orig;
using (var fs = File.OpenRead(TestCaseSources.WordDoc))
orig = GetFileBytes(fs, cnt);
var obj = new ShellDataObject([new ShellItem(TestCaseSources.WordDoc)]);

Assert.That(obj.GetDataPresent(ShellClipboardFormat.CFSTR_FILEDESCRIPTORW));
var fd = obj.GetData(ShellClipboardFormat.CFSTR_FILEDESCRIPTORW, true);
Assert.That(fd is ShellFileDescriptor[]);
Assert.That((ShellFileDescriptor[])fd, Has.Exactly(1).Items);
Assert.That(((ShellFileDescriptor[])fd)[0].Info.Name, Is.EqualTo(System.IO.Path.GetFileName(TestCaseSources.WordDoc)));
Assert.That(((ShellFileDescriptor[])fd)[0].Info.Name, Is.EqualTo(Path.GetFileName(TestCaseSources.WordDoc)));

Assert.That(obj.GetDataPresent(ShellClipboardFormat.CFSTR_FILECONTENTS));
var fc = obj.GetData(ShellClipboardFormat.CFSTR_FILECONTENTS, true);
Assert.That(fc is System.IO.Stream[]);
Assert.That((System.IO.Stream[])fc, Has.Exactly(1).Items);
Assert.That(((System.IO.Stream[])fc)[0].Length, Is.EqualTo(new System.IO.FileInfo(TestCaseSources.WordDoc).Length));
if (fc is not Stream[] fcs) { Assert.Fail(); return; }
Assert.That(fcs, Has.Exactly(1).Items);
Assert.That(fcs[0].Length, Is.EqualTo(new FileInfo(TestCaseSources.WordDoc).Length));
Assert.That(GetFileBytes(fcs[0], cnt), Is.EquivalentTo(orig));

static byte[] GetFileBytes(Stream fs, int count)
{
var ret = new byte[count];
fs.Read(ret, 0, count);
return ret;
}
}

[TestCase("<a href=\"http://www.contoso.com\">Contoso</a>")]
Expand Down

0 comments on commit 06ceb27

Please sign in to comment.