diff --git a/src/test/unit/System.Windows.Forms/ComDisabled/ClipboardComTests.cs b/src/test/unit/System.Windows.Forms/ComDisabled/ClipboardComTests.cs index 58ed6e9930e..14da486adcb 100644 --- a/src/test/unit/System.Windows.Forms/ComDisabled/ClipboardComTests.cs +++ b/src/test/unit/System.Windows.Forms/ComDisabled/ClipboardComTests.cs @@ -75,6 +75,48 @@ public void Clipboard_SetDataObject_DerivedDataObject_ReturnsExpected() private class DerivedDataObject : DataObject { } + [WinFormsTheory] + [BoolData] + public void Clipboard_TryGetData_MemoryStream_ReturnsRawData(bool copy) + { + // Prepare test data + byte[] testBytes = [1, 2, 3, 4, 5]; + string format = "CustomFormat"; + + // Set data on clipboard + DataObject dataObject = new(); + dataObject.SetData(format, testBytes); + Clipboard.SetDataObject(dataObject, copy); + + // Ask for raw data as MemoryStream + Clipboard.TryGetData(format, out MemoryStream? stream).Should().BeTrue(); + stream.Should().NotBeNull(); + + // Verify we get the exact raw data + byte[] retrievedBytes = new byte[stream!.Length]; + stream.Position = 0; + stream.Read(retrievedBytes, 0, retrievedBytes.Length); + retrievedBytes.Should().BeEquivalentTo(testBytes); + } + + [WinFormsFact] + public void Clipboard_TryGetData_MemoryStream_FromJsonData_ReturnsRawData() + { + // Prepare test data - JSON serialized data + SimpleTestData testData = new() { X = 30, Y = 40 }; + string format = "JsonData"; + + // Set JSON data on clipboard + Clipboard.SetDataAsJson(format, testData); + + // Ask for the raw memory stream + Clipboard.TryGetData(format, out MemoryStream? stream).Should().BeTrue(); + stream.Should().NotBeNull(); + + // The stream should contain valid data + stream!.Length.Should().BeGreaterThan(0); + } + private class CustomDataObject : IDataObject, ComTypes.IDataObject { [DllImport("shell32.dll")] diff --git a/src/test/unit/System.Windows.Forms/System/Windows/Forms/ClipboardTests.cs b/src/test/unit/System.Windows.Forms/System/Windows/Forms/ClipboardTests.cs index 1c6b09dfb2e..58b545a47c5 100644 --- a/src/test/unit/System.Windows.Forms/System/Windows/Forms/ClipboardTests.cs +++ b/src/test/unit/System.Windows.Forms/System/Windows/Forms/ClipboardTests.cs @@ -1377,4 +1377,95 @@ internal class SerializablePerson public string Name { get; set; } = "DefaultName"; public int Age { get; set; } } + + [WinFormsTheory] + [BoolData] + public void TryGetData_MemoryStream_ReturnsRawData(bool copy) + { + // Prepare test data + byte[] testBytes = [1, 2, 3, 4, 5]; + string format = "CustomFormat"; + + // Set data on clipboard + DataObject dataObject = new(); + dataObject.SetData(format, testBytes); + Clipboard.SetDataObject(dataObject, copy); + + // Ask for raw data as MemoryStream + Clipboard.TryGetData(format, out MemoryStream? stream).Should().BeTrue(); + stream.Should().NotBeNull(); + + // Verify we get the exact raw data + byte[] retrievedBytes = new byte[stream!.Length]; + stream.Position = 0; + stream.Read(retrievedBytes, 0, retrievedBytes.Length); + retrievedBytes.Should().BeEquivalentTo(testBytes); + } + + [WinFormsTheory] + [BoolData] + public void TryGetData_SerializationRecord_DecodesNrbfData(bool copy) + { + // Prepare test data - this will be serialized with NRBF by the framework + SimpleTestData testData = new() { X = 10, Y = 20 }; + string format = "SerializedData"; + + // Set data on clipboard with BinaryFormatter enabled to ensure NRBF serialization + using BinaryFormatterScope scope = new(enable: true); + Clipboard.SetData(format, testData); + + // Ask for the SerializationRecord directly + Clipboard.TryGetData(format, out SerializationRecord? record).Should().BeTrue(); + record.Should().NotBeNull(); + + // Verify the SerializationRecord contains the expected data + record!.TypeName.FullName.Should().Be(typeof(SimpleTestData).FullName); + } + + [WinFormsTheory] + [BoolData] + public void TryGetData_MemoryStream_FromJsonData_ReturnsRawData(bool copy) + { + // Prepare test data - JSON serialized data + SimpleTestData testData = new() { X = 30, Y = 40 }; + string format = "JsonData"; + + // Set JSON data on clipboard + Clipboard.SetDataAsJson(format, testData); + + // Ask for the raw memory stream + Clipboard.TryGetData(format, out MemoryStream? stream).Should().BeTrue(); + stream.Should().NotBeNull(); + + // The stream should contain the binary format of JsonData + stream!.Position = 0; + SerializationRecord record = stream.DecodeNrbf(); + record.Should().NotBeNull(); + + // Verify it's a JsonData type + record.TypeName.AssemblyName?.FullName.Should().Be(IJsonData.CustomAssemblyName); + } + + [WinFormsTheory] + [BoolData] + public void SetDataObject_WithMemoryStream_TryGetDataReturnsOriginalStream(bool copy) + { + // Prepare a MemoryStream to store on clipboard + byte[] testBytes = [10, 20, 30, 40, 50]; + MemoryStream originalStream = new(testBytes); + string format = "StreamData"; + + // Set data on clipboard + DataObject dataObject = new(); + dataObject.SetData(format, originalStream); + Clipboard.SetDataObject(dataObject, copy); + + // Try to get back the data as the original MemoryStream + Clipboard.TryGetData(format, out MemoryStream? retrievedStream).Should().BeTrue(); + retrievedStream.Should().NotBeNull(); + + // Verify the stream contains the original data + byte[] retrievedBytes = retrievedStream!.ToArray(); + retrievedBytes.Should().BeEquivalentTo(testBytes); + } } diff --git a/src/test/unit/System.Windows.Forms/System/Windows/Forms/DataObjectTests.cs b/src/test/unit/System.Windows.Forms/System/Windows/Forms/DataObjectTests.cs index 66760eddfcd..c4b72103419 100644 --- a/src/test/unit/System.Windows.Forms/System/Windows/Forms/DataObjectTests.cs +++ b/src/test/unit/System.Windows.Forms/System/Windows/Forms/DataObjectTests.cs @@ -3130,6 +3130,76 @@ public void SetDataAsJson_NonRestrictedFormat_JsonSerialized(string format) data.GetData(format).Should().Be(testData); } + [WinFormsTheory] + [CommonMemberData(typeof(DataObjectTestHelpers), nameof(DataObjectTestHelpers.UnboundedFormat))] + public void TryGetData_MemoryStream_ReturnsRawData(string format) + { + // Prepare test data + byte[] testData = [1, 2, 3, 4, 5]; + + // Set data in DataObject + DataObject data = new(); + data.SetData(format, testData); + + // Try to get the data as a MemoryStream + data.TryGetData(format, out MemoryStream? stream).Should().BeTrue(); + stream.Should().NotBeNull(); + + // Verify the stream contains the original data + byte[] retrievedData = new byte[stream!.Length]; + stream.Position = 0; + stream.Read(retrievedData, 0, retrievedData.Length); + retrievedData.Should().BeEquivalentTo(testData); + } + + [WinFormsTheory] + [CommonMemberData(typeof(DataObjectTestHelpers), nameof(DataObjectTestHelpers.UnboundedFormat))] + public void TryGetData_SerializationRecord_DecodesNrbfData(string format) + { + // Test only with BinaryFormatter enabled to ensure NRBF serialization + using BinaryFormatterScope scope = new(enable: true); + + // Prepare test data + SimpleTestData testData = new() { X = 10, Y = 20 }; + + // Set data in DataObject + DataObject data = new(); + data.SetData(format, testData); + + // Try to get the data as a SerializationRecord + data.TryGetData(format, out SerializationRecord? record).Should().BeTrue(); + record.Should().NotBeNull(); + + // Verify the SerializationRecord contains the original type info + record!.TypeName.FullName.Should().Be(typeof(SimpleTestData).FullName); + } + + [WinFormsTheory] + [CommonMemberData(typeof(DataObjectTestHelpers), nameof(DataObjectTestHelpers.UnboundedFormat))] + public void SetDataAsJson_TryGetData_MemoryStream_ReturnsJsonRawData(string format) + { + // Prepare test data + SimpleTestData testData = new() { X = 30, Y = 40 }; + + // Set JSON data in DataObject + DataObject data = new(); + data.SetDataAsJson(format, testData); + + // Try to get the data as a MemoryStream + data.TryGetData(format, out MemoryStream? stream).Should().BeTrue(); + stream.Should().NotBeNull(); + + // The stream should contain valid data + stream!.Length.Should().BeGreaterThan(0); + + // Reset position for NRBF decoding + stream.Position = 0; + + // Verify it contains a SerializationRecord with JsonData format + SerializationRecord record = stream.DecodeNrbf(); + record.TypeName.AssemblyName?.FullName.Should().Be(IJsonData.CustomAssemblyName); + } + [WinFormsFact] public void SetDataAsJson_WrongType_ReturnsNull() {