Skip to content

Commit e3b2e51

Browse files
test code cleanup (#13309)
renamed a test input class, added exception message verification, and did a minor clean up
1 parent 8199f84 commit e3b2e51

File tree

6 files changed

+85
-74
lines changed

6 files changed

+85
-74
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Text.RegularExpressions;
5+
6+
namespace System.Windows.Forms.Tests;
7+
8+
internal static partial class ResourceStrings
9+
{
10+
[GeneratedRegex(@"{[0-9]}")]
11+
private static partial Regex PlaceholdersPattern();
12+
13+
internal static string InvalidTypeFormatCombinationMessage =>
14+
PlaceholdersPattern().Replace(SR.ClipboardOrDragDrop_InvalidFormatTypeCombination, "*");
15+
internal static string TypeRequiresResolver => PlaceholdersPattern().Replace(SR.ClipboardOrDragDrop_InvalidType, "*");
16+
internal static string UseTryGetDataWithResolver => PlaceholdersPattern().Replace(SR.ClipboardOrDragDrop_UseTypedAPI, "*");
17+
internal static string TypedInterfaceNotImplemented => PlaceholdersPattern().Replace(SR.ITypeDataObject_Not_Implemented, "*");
18+
}

src/test/unit/System.Windows.Forms/System/Windows/Forms/ClipboardTests.cs

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -566,13 +566,13 @@ public unsafe void GetReturnsProxy()
566566
PInvokeCore.OleGetClipboard(proxy).Succeeded.Should().BeTrue();
567567
((nint)proxy.Value).Should().NotBe((nint)dataScope.Value);
568568

569-
using var dataUnknown = dataScope.Query<Com.IUnknown>();
570-
using var proxyUnknown = proxy.Query<Com.IUnknown>();
569+
using var dataUnknown = dataScope.Query<IUnknown>();
570+
using var proxyUnknown = proxy.Query<IUnknown>();
571571
((nint)proxyUnknown.Value).Should().NotBe((nint)dataUnknown.Value);
572572

573573
// The proxy does not know about this interface, it should give back the real pointer.
574-
using var realDataPointer = proxy.Query<Com.IComCallableWrapper>();
575-
using var realDataPointerUnknown = realDataPointer.Query<Com.IUnknown>();
574+
using var realDataPointer = proxy.Query<IComCallableWrapper>();
575+
using var realDataPointerUnknown = realDataPointer.Query<IUnknown>();
576576
((nint)proxyUnknown.Value).Should().NotBe((nint)realDataPointerUnknown.Value);
577577
((nint)dataUnknown.Value).Should().Be((nint)realDataPointerUnknown.Value);
578578
}
@@ -1311,52 +1311,19 @@ public void RoundTrip_TypedDataObject_SupportsTypedInterface(bool copy) =>
13111311

13121312
[WinFormsTheory]
13131313
[BoolData]
1314-
public void RoundTrip_ManagedDataObject_SupportsTypedInterface(bool copy) =>
1315-
CustomDataObject_RoundTrip_SupportsTypedInterface<ManagedDataObject>(copy);
1316-
1317-
[WinFormsTheory]
1318-
[BoolData]
1319-
public void RoundTrip_Object_SupportsTypedInterface(bool copy)
1320-
{
1321-
SerializableTestData data = new();
1322-
string format = typeof(SerializableTestData).FullName!;
1323-
1324-
// Opt-in into access to the binary formatted stream.
1325-
using BinaryFormatterInClipboardDragDropScope clipboardScope = new(enable: copy);
1326-
1327-
// We need the BinaryFormatter to flush the data from the managed object to the HGLOBAL
1328-
// and to write data to HGLOBAL as a binary formatted stream now if it hadn't been flushed.
1329-
using BinaryFormatterScope scope = new(enable: copy);
1330-
1331-
Clipboard.SetDataObject(data, copy);
1332-
1333-
DataObject received = Clipboard.GetDataObject().Should().BeAssignableTo<DataObject>().Subject;
1334-
1335-
received.TryGetData(
1336-
format,
1337-
name => name.FullName == typeof(SerializableTestData).FullName ? typeof(SerializableTestData) : null,
1338-
autoConvert: false,
1339-
out SerializableTestData? result).Should().BeTrue();
1340-
1341-
result.Should().BeEquivalentTo(data);
1342-
1343-
Clipboard.TryGetData(
1344-
format,
1345-
(TypeName name) => name.FullName == typeof(SerializableTestData).FullName ? typeof(SerializableTestData) : null,
1346-
out result).Should().BeTrue();
1347-
1348-
result.Should().BeEquivalentTo(data);
1349-
}
1314+
public void RoundTrip_UntypedDataObject_SupportsTypedInterface(bool copy) =>
1315+
CustomDataObject_RoundTrip_SupportsTypedInterface<UntypedDataObject>(copy);
13501316

13511317
private static void CustomDataObject_RoundTrip_SupportsTypedInterface<T>(bool copy) where T : IDataObject, new()
13521318
{
13531319
SerializableTestData data = new();
13541320
T testDataObject = new();
1355-
string format = ManagedDataObject.s_format;
1321+
string format = UntypedDataObject.s_format;
13561322
testDataObject.SetData(format, data);
13571323

13581324
// Opt-in into access the binary formatted stream.
13591325
using BinaryFormatterInClipboardDragDropScope clipboardScope = new(enable: copy);
1326+
13601327
// We need the BinaryFormatter to flush the data from the managed object to the HGLOBAL.
13611328
using (BinaryFormatterScope scope = new(enable: copy))
13621329
{
@@ -1371,7 +1338,7 @@ public void RoundTrip_Object_SupportsTypedInterface(bool copy)
13711338
using BinaryFormatterScope scope = new(enable: copy);
13721339
ITypedDataObject received = Clipboard.GetDataObject().Should().BeAssignableTo<ITypedDataObject>().Subject;
13731340

1374-
// Need an explict resolver to hit the BinaryFormatter path if the data was copied out.
1341+
// Need an explicit resolver to hit the BinaryFormatter path if the data was copied out.
13751342
received.TryGetData(format, out SerializableTestData? result).Should().Be(!copy);
13761343
if (copy)
13771344
{
@@ -1384,7 +1351,7 @@ public void RoundTrip_Object_SupportsTypedInterface(bool copy)
13841351

13851352
received.TryGetData(
13861353
format,
1387-
(TypeName name) => name.FullName == typeof(SerializableTestData).FullName ? typeof(SerializableTestData) : null,
1354+
name => name.FullName == typeof(SerializableTestData).FullName ? typeof(SerializableTestData) : null,
13881355
autoConvert: false,
13891356
out result).Should().BeTrue();
13901357

@@ -1402,7 +1369,7 @@ public void RoundTrip_Object_SupportsTypedInterface(bool copy)
14021369

14031370
Clipboard.TryGetData(
14041371
format,
1405-
(TypeName name) => name.FullName == typeof(SerializableTestData).FullName ? typeof(SerializableTestData) : null,
1372+
name => name.FullName == typeof(SerializableTestData).FullName ? typeof(SerializableTestData) : null,
14061373
out result).Should().BeTrue();
14071374

14081375
result.Should().BeEquivalentTo(data);
@@ -1411,12 +1378,47 @@ public void RoundTrip_Object_SupportsTypedInterface(bool copy)
14111378
{
14121379
T received = Clipboard.GetDataObject().Should().BeOfType<T>().Subject;
14131380
received.Should().Be(testDataObject);
1414-
// When we are not flushing the data to the HGLOBAL, we are reading from our DataStore or the managed test data object.
1381+
// When we are not flushing the data to the HGLOBAL, we are reading from our managed test data object,
1382+
// which might not support the types interface.
14151383
Action tryGetData = () => received.TryGetData(format, out SerializableTestData? result);
1416-
tryGetData.Should().Throw<NotSupportedException>();
1384+
tryGetData.Should().Throw<NotSupportedException>()
1385+
.WithMessage(expectedWildcardPattern: ResourceStrings.TypedInterfaceNotImplemented);
14171386
}
14181387
}
14191388

1389+
[WinFormsTheory]
1390+
[BoolData]
1391+
public void RoundTrip_Object_SupportsTypedInterface(bool copy)
1392+
{
1393+
SerializableTestData data = new();
1394+
string format = typeof(SerializableTestData).FullName!;
1395+
1396+
// Opt-in into access to the binary formatted stream.
1397+
using BinaryFormatterInClipboardDragDropScope clipboardScope = new(enable: copy);
1398+
1399+
// We need the BinaryFormatter to flush the data from the managed object to the HGLOBAL.
1400+
using BinaryFormatterScope scope = new(enable: copy);
1401+
1402+
Clipboard.SetDataObject(data, copy);
1403+
1404+
DataObject received = Clipboard.GetDataObject().Should().BeAssignableTo<DataObject>().Subject;
1405+
1406+
received.TryGetData(
1407+
format,
1408+
name => name.FullName == typeof(SerializableTestData).FullName ? typeof(SerializableTestData) : null,
1409+
autoConvert: false,
1410+
out SerializableTestData? result).Should().BeTrue();
1411+
1412+
result.Should().BeEquivalentTo(data);
1413+
1414+
Clipboard.TryGetData(
1415+
format,
1416+
name => name.FullName == typeof(SerializableTestData).FullName ? typeof(SerializableTestData) : null,
1417+
out result).Should().BeTrue();
1418+
1419+
result.Should().BeEquivalentTo(data);
1420+
}
1421+
14201422
[WinFormsFact]
14211423
public unsafe void OleGetClipboard_ProxyBehavior()
14221424
{

src/test/unit/System.Windows.Forms/System/Windows/Forms/NativeToWinFormsAdapterTests.cs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Drawing;
55
using System.Reflection.Metadata;
6-
using System.Text.RegularExpressions;
76
using System.Windows.Forms.TestUtilities;
87
using Com = Windows.Win32.System.Com;
98

109
namespace System.Windows.Forms.Tests;
1110

1211
public unsafe partial class NativeToWinFormsAdapterTests
1312
{
14-
[GeneratedRegex(@"{[0-9]}")]
15-
private static partial Regex PlaceholdersPattern();
16-
17-
private static string InvalidTypeFormatCombinationMessage =>
18-
PlaceholdersPattern().Replace(SR.ClipboardOrDragDrop_InvalidFormatTypeCombination, "*");
19-
private static string TypeRequiresResolverMessage => PlaceholdersPattern().Replace(SR.ClipboardOrDragDrop_InvalidType, "*");
20-
private static string UseTryGetDataWithResolver => PlaceholdersPattern().Replace(SR.ClipboardOrDragDrop_UseTypedAPI, "*");
21-
2213
private const string FormatterDisabledMessage =
2314
"BinaryFormatter serialization and deserialization are disabled within this application. See https://aka.ms/binaryformatter for more information.";
2415

@@ -58,7 +49,7 @@ public void TryGetData_AsObject_Primitive_RequiresResolver(string format)
5849
DataObject dataObject = new(comDataObject.Value);
5950

6051
Action tryGetData = () => dataObject.TryGetData(format, out object? value);
61-
tryGetData.Should().Throw<NotSupportedException>().WithMessage(expectedWildcardPattern: TypeRequiresResolverMessage);
52+
tryGetData.Should().Throw<NotSupportedException>().WithMessage(expectedWildcardPattern: ResourceStrings.TypeRequiresResolver);
6253

6354
dataObject.TryGetData(format, Resolver, autoConvert: false, out object? value).Should().BeTrue();
6455
value.Should().Be(1);
@@ -79,7 +70,7 @@ public void TryGetData_AsObject_Primitive_InvalidTypeFormatCombination(string fo
7970
// Throw when validating arguments, as these formats allow exactly strings or bitmaps only.
8071
Action tryGetData = () => dataObject.TryGetData(format, out object? _);
8172
tryGetData.Should().Throw<NotSupportedException>()
82-
.WithMessage(expectedWildcardPattern: InvalidTypeFormatCombinationMessage);
73+
.WithMessage(expectedWildcardPattern: ResourceStrings.InvalidTypeFormatCombinationMessage);
8374
}
8475

8576
private static (DataObject dataObject, TestData value) SetDataObject(string format)
@@ -101,7 +92,7 @@ public void TryGetData_AsObject_Custom_RequiresResolver(string format)
10192
(DataObject dataObject, TestData _) = SetDataObject(format);
10293
Action tryGetData = () => dataObject.TryGetData(format, out object? _);
10394

104-
tryGetData.Should().Throw<NotSupportedException>().WithMessage(expectedWildcardPattern: TypeRequiresResolverMessage);
95+
tryGetData.Should().Throw<NotSupportedException>().WithMessage(expectedWildcardPattern: ResourceStrings.TypeRequiresResolver);
10596
}
10697

10798
[WinFormsTheory]
@@ -114,7 +105,7 @@ public void TryGetData_AsObject_Custom_FormatterEnabled_RequiresResolver(string
114105
using BinaryFormatterScope scope = new(enable: true);
115106
using BinaryFormatterInClipboardDragDropScope clipboardScope = new(enable: true);
116107

117-
tryGetData.Should().Throw<NotSupportedException>().WithMessage(expectedWildcardPattern: TypeRequiresResolverMessage);
108+
tryGetData.Should().Throw<NotSupportedException>().WithMessage(expectedWildcardPattern: ResourceStrings.TypeRequiresResolver);
118109
}
119110

120111
[WinFormsTheory]
@@ -127,7 +118,7 @@ public void TryGetData_AsObject_Custom_InvalidTypeFormatCombination(string forma
127118

128119
// Type-Format combination is validated before the we attempt to serialize data.
129120
tryGetData.Should().Throw<NotSupportedException>()
130-
.WithMessage(expectedWildcardPattern: InvalidTypeFormatCombinationMessage);
121+
.WithMessage(expectedWildcardPattern: ResourceStrings.InvalidTypeFormatCombinationMessage);
131122
}
132123

133124
[WinFormsTheory]
@@ -180,7 +171,7 @@ public void TryGetData_AsInterface_ListOfPrimitives_RequiresResolver(string form
180171
// Theoretically we don't require a resolver here, but this is an exception. In the more common cases resolver
181172
// is required to instantiate non-concrete types.
182173
Action tryGetData = () => dataObject.TryGetData(format, out IList<int>? _);
183-
tryGetData.Should().Throw<NotSupportedException>().WithMessage(expectedWildcardPattern: TypeRequiresResolverMessage);
174+
tryGetData.Should().Throw<NotSupportedException>().WithMessage(expectedWildcardPattern: ResourceStrings.TypeRequiresResolver);
184175
}
185176

186177
[WinFormsTheory]
@@ -196,7 +187,7 @@ public void TryGetData_AsInterface_ListOfPrimitives_InvalidTypeFormatCombination
196187

197188
Action tryGetData = () => dataObject.TryGetData(format, out IList<int>? _);
198189
tryGetData.Should().Throw<NotSupportedException>()
199-
.WithMessage(expectedWildcardPattern: InvalidTypeFormatCombinationMessage);
190+
.WithMessage(expectedWildcardPattern: ResourceStrings.InvalidTypeFormatCombinationMessage);
200191
}
201192

202193
[WinFormsTheory]
@@ -227,7 +218,7 @@ public void TryGetData_AsConcreteType_ListOfPrimitives_InvalidTypeFormatCombinat
227218

228219
Action tryGetData = () => dataObject.TryGetData(format, out List<int>? _);
229220
tryGetData.Should().Throw<NotSupportedException>()
230-
.WithMessage(expectedWildcardPattern: InvalidTypeFormatCombinationMessage);
221+
.WithMessage(expectedWildcardPattern: ResourceStrings.InvalidTypeFormatCombinationMessage);
231222
}
232223

233224
[WinFormsTheory]
@@ -277,7 +268,7 @@ public void TryGetData_AsConcreteType_Custom_InvalidTypeFormatCombination(string
277268

278269
Action tryGetData = () => dataObject.TryGetData(format, out TestData? _);
279270
tryGetData.Should().Throw<NotSupportedException>()
280-
.WithMessage(expectedWildcardPattern: InvalidTypeFormatCombinationMessage);
271+
.WithMessage(expectedWildcardPattern: ResourceStrings.InvalidTypeFormatCombinationMessage);
281272
}
282273

283274
[WinFormsTheory]
@@ -302,7 +293,7 @@ public void TryGetData_WithResolver_AsConcreteType_Custom_InvalidTypeFormatCombi
302293
Action tryGetData = () => dataObject.TryGetData(format, TestData.Resolver, autoConvert: true, out TestData? _);
303294

304295
tryGetData.Should().Throw<NotSupportedException>()
305-
.WithMessage(expectedWildcardPattern: InvalidTypeFormatCombinationMessage);
296+
.WithMessage(expectedWildcardPattern: ResourceStrings.InvalidTypeFormatCombinationMessage);
306297
}
307298

308299
[WinFormsTheory]
@@ -317,7 +308,7 @@ public void TryGetData_WithResolver_AsConcreteType_Custom_FormatterEnabled_Inval
317308
using BinaryFormatterInClipboardDragDropScope clipboardScope = new(enable: true);
318309

319310
tryGetData.Should().Throw<NotSupportedException>()
320-
.WithMessage(expectedWildcardPattern: InvalidTypeFormatCombinationMessage);
311+
.WithMessage(expectedWildcardPattern: ResourceStrings.InvalidTypeFormatCombinationMessage);
321312
}
322313

323314
[WinFormsTheory]
@@ -354,7 +345,7 @@ public void TryGetData_AsAbstract_Custom_InvalidTypeFormatCombination(string for
354345

355346
Action tryGetData = () => dataObject.TryGetData(format, out AbstractBase? _);
356347
tryGetData.Should().Throw<NotSupportedException>()
357-
.WithMessage(expectedWildcardPattern: InvalidTypeFormatCombinationMessage);
348+
.WithMessage(expectedWildcardPattern: ResourceStrings.InvalidTypeFormatCombinationMessage);
358349
}
359350

360351
[WinFormsTheory]
@@ -364,7 +355,7 @@ public void TryGetData_AsAbstract_Custom_RequiresResolver(string format)
364355
(DataObject dataObject, TestData _) = SetDataObject(format);
365356
Action tryGetData = () => dataObject.TryGetData(format, out AbstractBase? _);
366357

367-
tryGetData.Should().Throw<NotSupportedException>().WithMessage(expectedWildcardPattern: TypeRequiresResolverMessage);
358+
tryGetData.Should().Throw<NotSupportedException>().WithMessage(expectedWildcardPattern: ResourceStrings.TypeRequiresResolver);
368359

369360
dataObject.TryGetData(format, out NotSupportedException? ex).Should().BeTrue();
370361
ex.Should().BeOfType<NotSupportedException>().Which.Message.Should().Be(FormatterDisabledMessage);
@@ -380,7 +371,7 @@ public void TryGetData_AsAbstract_Custom_FormatterEnabled_RequiresResolver(strin
380371
using BinaryFormatterScope scope = new(enable: true);
381372
using BinaryFormatterInClipboardDragDropScope clipboardScope = new(enable: true);
382373

383-
tryGetData.Should().Throw<NotSupportedException>().WithMessage(expectedWildcardPattern: TypeRequiresResolverMessage);
374+
tryGetData.Should().Throw<NotSupportedException>().WithMessage(expectedWildcardPattern: ResourceStrings.TypeRequiresResolver);
384375
}
385376

386377
[WinFormsTheory]
@@ -420,7 +411,7 @@ public void TryGetData_WithResolver_AsAbstract_Custom_InvalidTypeFormatCombinati
420411
// Nothing is written to HGLOBAL in this test because format-type combination is invalid.
421412
Action tryGetData = () => dataObject.TryGetData(format, TestData.Resolver, autoConvert: true, out AbstractBase? _);
422413
tryGetData.Should().Throw<NotSupportedException>()
423-
.WithMessage(expectedWildcardPattern: InvalidTypeFormatCombinationMessage);
414+
.WithMessage(expectedWildcardPattern: ResourceStrings.InvalidTypeFormatCombinationMessage);
424415
}
425416

426417
[WinFormsTheory]

src/test/util/System.Windows.Forms/Data/ManagedAndRuntimeDataObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace System.Windows.Forms.TestUtilities;
99

10-
internal class ManagedAndRuntimeDataObject : ManagedDataObject, ComTypes.IDataObject
10+
internal class ManagedAndRuntimeDataObject : UntypedDataObject, ComTypes.IDataObject
1111
{
1212
public int DAdvise(ref ComTypes.FORMATETC pFormatetc, ComTypes.ADVF advf, ComTypes.IAdviseSink adviseSink, out int connection) => throw new NotImplementedException();
1313
public void DUnadvise(int connection) => throw new NotImplementedException();

src/test/util/System.Windows.Forms/Data/TypedDataObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace System.Windows.Forms.TestUtilities;
1010

11-
internal class TypedDataObject : ManagedDataObject, ITypedDataObject
11+
internal class TypedDataObject : UntypedDataObject, ITypedDataObject
1212
{
1313
public bool TryGetData<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>([MaybeNullWhen(false), NotNullWhen(true)] out T data) =>
1414
throw new NotImplementedException();

src/test/util/System.Windows.Forms/Data/ManagedDataObject.cs renamed to src/test/util/System.Windows.Forms/Data/UntypedDataObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace System.Windows.Forms.TestUtilities;
77

8-
internal class ManagedDataObject : IDataObject
8+
internal class UntypedDataObject : IDataObject
99
{
1010
public static string s_format = nameof(SerializableTestData);
1111
protected SerializableTestData? _data;

0 commit comments

Comments
 (0)