-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9632 from dotnet/AddMoreBFRelatedtest
Add unit test to ensure the unsupported data types will use BinaryFormatter
- Loading branch information
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
...crosoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/DataObjectTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using PresentationCore.Tests.TestUtilities; | ||
using PresentationCore.Tests.FluentAssertions; | ||
using System.Formats.Nrbf; | ||
using System.Drawing; | ||
using System.Windows; | ||
using System.Threading; | ||
|
||
namespace PresentationCore.Tests.BinaryFormat; | ||
|
||
public class DataObjectTests | ||
{ | ||
[Theory] | ||
[MemberData(nameof(DataObject_TestData_UnSupportedObjects))] | ||
public void DataObject_UnSupportedObjects_SetData(object value) | ||
{ | ||
|
||
// Create a thread to run the test logic | ||
Thread testThread = new Thread(() => TestLogic(value)); | ||
testThread.SetApartmentState(ApartmentState.STA); // Set the thread to STA | ||
testThread.Start(); | ||
testThread.Join(); // Wait for the thread to complete | ||
|
||
} | ||
|
||
private void TestLogic(object value) | ||
{ | ||
using BinaryFormatterScope formatterScope = new(enable: false); | ||
DataObject dataObject = new(); | ||
dataObject.SetData(DataFormats.Serializable, value); | ||
|
||
Clipboard.SetDataObject(dataObject, true); | ||
IDataObject ClipboardDataObject = Clipboard.GetDataObject(); | ||
try | ||
{ | ||
if (ClipboardDataObject != null) | ||
{ | ||
Assert.Throws<System.Runtime.InteropServices.COMException>(() => ClipboardDataObject.GetData(DataFormats.Serializable)); | ||
} | ||
else | ||
{ | ||
Assert.Fail("ClipboardDataObject is null."); | ||
} | ||
} | ||
finally | ||
{ | ||
Clipboard.Clear(); | ||
} | ||
} | ||
|
||
public static TheoryData<object> DataObject_TestData_UnSupportedObjects => new() | ||
{ | ||
new SerializableData(), | ||
new Dictionary<int, string>(), | ||
new object(), | ||
System.Drawing.Color.DeepSkyBlue, // Use a static property for Color | ||
new System.Drawing.Pen(System.Drawing.Brushes.DeepSkyBlue, 1), | ||
new System.Drawing.Bitmap(1, 1), // Use Bitmap instead of Image | ||
new System.Drawing.Printing.PrintDocument(), | ||
new System.Drawing.Printing.PrinterSettings(), | ||
new System.Drawing.Printing.PageSettings(), | ||
new System.Drawing.Printing.PaperSize(), | ||
new System.Drawing.Printing.PaperSource(), | ||
new System.Drawing.Printing.PrinterResolution(), | ||
}; | ||
} | ||
|
||
|
||
[Serializable] | ||
public class SerializableData | ||
{ | ||
public string Name { get; set; } | ||
public DateTime BirthDate { get; set; } | ||
public int Age { get; set; } | ||
public double Salary { get; set; } | ||
public bool IsEmployed { get; set; } | ||
public char Gender { get; set; } | ||
public byte[] BinaryData { get; set; } | ||
|
||
// Constructor | ||
public SerializableData() | ||
{ | ||
// Initialize properties with default values | ||
Name = "John Doe"; | ||
BirthDate = DateTime.Now.AddYears(-30); | ||
Age = 30; | ||
Salary = 50000.0; | ||
IsEmployed = true; | ||
Gender = 'M'; | ||
BinaryData = new byte[] { 1, 2, 3, 4, 5 }; // Example binary data | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters