diff --git a/src/System.Windows.Forms.Design/src/System/Drawing/Design/ToolboxItem.cs b/src/System.Windows.Forms.Design/src/System/Drawing/Design/ToolboxItem.cs
index 7583e8f57ab..65fcde013dc 100644
--- a/src/System.Windows.Forms.Design/src/System/Drawing/Design/ToolboxItem.cs
+++ b/src/System.Windows.Forms.Design/src/System/Drawing/Design/ToolboxItem.cs
@@ -101,7 +101,7 @@ public AssemblyName[] DependentAssemblies
}
set
{
- Properties["DependentAssemblies"] = value.Clone();
+ Properties["DependentAssemblies"] = value?.Clone();
}
}
@@ -277,23 +277,15 @@ public string TypeName
}
///
- /// Gets the version for this toolboxitem. It defaults to AssemblyName.Version unless
- /// overridden in a derived toolboxitem. This can be overridden to return an empty string
- /// to suppress its display in the toolbox tooltip.
+ /// Gets the version for this toolboxitem. It defaults to AssemblyName.Version unless
+ /// overridden in a derived toolboxitem. This can be overridden to
+ /// return an empty string to suppress its display in the toolbox tooltip.
///
public virtual string Version
{
- get
- {
- if (AssemblyName != null)
- {
- return AssemblyName.Version.ToString();
- }
- return string.Empty;
- }
+ get => AssemblyName?.Version?.ToString() ?? string.Empty;
}
-
///
/// Occurs when components are created.
///
@@ -408,7 +400,7 @@ protected virtual IComponent[] CreateComponentsCore(IDesignerHost host, IDiction
{
IComponent[] components = CreateComponentsCore(host);
- if (host != null)
+ if (host != null && components != null)
{
for (int i = 0; i < components.Length; i++)
{
@@ -527,10 +519,9 @@ private static bool AreAssemblyNamesEqual(AssemblyName name1, AssemblyName name2
public override int GetHashCode()
{
- string typeName = TypeName;
- int hash = (typeName != null) ? typeName.GetHashCode() : 0;
-
- return unchecked(hash ^ DisplayName.GetHashCode());
+ int typeHash = TypeName?.GetHashCode() ?? 0;
+ int displayHash = DisplayName?.GetHashCode() ?? 0;
+ return unchecked(typeHash ^ displayHash);
}
///
@@ -542,8 +533,8 @@ protected virtual object FilterPropertyValue(string propertyName, object value)
switch (propertyName)
{
case "AssemblyName":
- if (value != null)
- value = ((AssemblyName)value).Clone();
+ if (value is AssemblyName valueName)
+ value = valueName.Clone();
break;
@@ -598,7 +589,7 @@ protected virtual Type GetType(IDesignerHost host, AssemblyName assemblyName, st
if (host != null)
{
- ts = (ITypeResolutionService)host.GetService(typeof(ITypeResolutionService));
+ ts = host.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService;
}
if (ts != null)
@@ -664,7 +655,7 @@ protected virtual Type GetType(IDesignerHost host, AssemblyName assemblyName, st
{
}
- if (a == null && assemblyName.CodeBase != null && assemblyName.CodeBase.Length > 0)
+ if (a == null && !string.IsNullOrEmpty(assemblyName.CodeBase))
{
try
{
@@ -804,7 +795,8 @@ public virtual void Initialize(Type type)
private AssemblyName GetNonRetargetedAssemblyName(Type type, AssemblyName policiedAssemblyName)
{
- if (type == null || policiedAssemblyName == null)
+ Debug.Assert(type != null);
+ if (policiedAssemblyName == null)
return null;
//if looking for myself, just return it. (not a reference)
@@ -833,17 +825,17 @@ private AssemblyName GetNonRetargetedAssemblyName(Type type, AssemblyName polici
// in assemblyname.
foreach (AssemblyName name in type.Assembly.GetReferencedAssemblies())
{
- Assembly a = null;
-
try
{
- a = Assembly.Load(name);
- if (a != null && a.FullName == policiedAssemblyName.FullName)
+ Assembly a = Assembly.Load(name);
+ if (a.FullName == policiedAssemblyName.FullName)
+ {
return name;
+ }
}
catch
{
- //ignore all exceptions and just fall through if it fails (it shouldn't, but who knows).
+ // Ignore all exceptions and just fall through if it fails (it shouldn't, but who knows).
}
}
@@ -900,10 +892,7 @@ protected virtual void OnComponentsCreating(ToolboxComponentsCreatingEventArgs a
_componentsCreatingEvent?.Invoke(this, args);
}
- public override string ToString()
- {
- return DisplayName;
- }
+ public override string ToString() => DisplayName ?? string.Empty;
///
/// Called as a helper to ValidatePropertyValue to validate that an object
@@ -992,6 +981,10 @@ protected virtual object ValidatePropertyValue(string propertyName, object value
value = filter;
break;
+ case "DependentAssemblies":
+ ValidatePropertyType(propertyName, value, typeof(AssemblyName[]), true);
+ break;
+
case "IsTransient":
ValidatePropertyType(propertyName, value, typeof(bool), false);
break;
@@ -1002,7 +995,6 @@ protected virtual object ValidatePropertyValue(string propertyName, object value
[SuppressMessage("Microsoft.Usage", "CA2240:ImplementISerializableCorrectly")]
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
- //IntSecurity.UnmanagedCode.Demand();
Serialize(info, context);
}
diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/Resources/16x16.bmp b/src/System.Windows.Forms.Design/tests/UnitTests/Resources/16x16.bmp
new file mode 100644
index 00000000000..8bf35136370
Binary files /dev/null and b/src/System.Windows.Forms.Design/tests/UnitTests/Resources/16x16.bmp differ
diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/Resources/16x24.bmp b/src/System.Windows.Forms.Design/tests/UnitTests/Resources/16x24.bmp
new file mode 100644
index 00000000000..9fa2469f870
Binary files /dev/null and b/src/System.Windows.Forms.Design/tests/UnitTests/Resources/16x24.bmp differ
diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/Resources/24x16.bmp b/src/System.Windows.Forms.Design/tests/UnitTests/Resources/24x16.bmp
new file mode 100644
index 00000000000..d3fdc521f1f
Binary files /dev/null and b/src/System.Windows.Forms.Design/tests/UnitTests/Resources/24x16.bmp differ
diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System.Windows.Forms.Design.Tests.csproj b/src/System.Windows.Forms.Design/tests/UnitTests/System.Windows.Forms.Design.Tests.csproj
index 2db090483e3..3c66db51f45 100644
--- a/src/System.Windows.Forms.Design/tests/UnitTests/System.Windows.Forms.Design.Tests.csproj
+++ b/src/System.Windows.Forms.Design/tests/UnitTests/System.Windows.Forms.Design.Tests.csproj
@@ -14,8 +14,20 @@
+
+
+
+ System.Drawing.Design.Tests.16x16.bmp
+
+
+ System.Drawing.Design.Tests.24x16.bmp
+
+
+ System.Drawing.Design.Tests.16x24.bmp
+
+
diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/Drawing/Design/ToolboxItemCollectionTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/Drawing/Design/ToolboxItemCollectionTests.cs
new file mode 100644
index 00000000000..0c65b8cc261
--- /dev/null
+++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/Drawing/Design/ToolboxItemCollectionTests.cs
@@ -0,0 +1,75 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Xunit;
+
+namespace System.Drawing.Design.Tests
+{
+ public class ToolboxItemCollectionTests
+ {
+ [Fact]
+ public void ToolboxItemCollection_Ctor_ToolboxItemArray()
+ {
+ var item = new ToolboxItem();
+ var collection = new ToolboxItemCollection(new ToolboxItem[] { item });
+ Assert.Same(item, Assert.Single(collection));
+ Assert.Same(item, collection[0]);
+ Assert.True(collection.Contains(item));
+ Assert.Equal(0, collection.IndexOf(item));
+ }
+
+ [Fact]
+ public void ToolboxItemCollection_Ctor_NullToolboxItemArray_ThrowsArgumentNullException()
+ {
+ Assert.Throws("c", () => new ToolboxItemCollection((ToolboxItem[])null));
+ }
+
+ [Fact]
+ public void ToolboxItemCollection_Ctor_ToolboxItemCollection()
+ {
+ var item = new ToolboxItem();
+ var value = new ToolboxItemCollection(new ToolboxItem[] { item });
+ var collection = new ToolboxItemCollection(value);
+ Assert.Same(item, Assert.Single(collection));
+ Assert.Same(item, collection[0]);
+ Assert.True(collection.Contains(item));
+ Assert.Equal(0, collection.IndexOf(item));
+ }
+
+ [Fact]
+ public void ToolboxItemCollection_Ctor_NullToolboxItemCollection_ThrowsArgumentNullException()
+ {
+ Assert.Throws("c", () => new ToolboxItemCollection((ToolboxItemCollection)null));
+ }
+
+ [Fact]
+ public void ToolboxItemCollection_Contains_NoSuchValue_ReturnsFalse()
+ {
+ var item = new ToolboxItem();
+ var collection = new ToolboxItemCollection(new ToolboxItem[] { item });
+ Assert.False(collection.Contains(new ToolboxItem { DisplayName = "Other" }));
+ Assert.False(collection.Contains(null));
+ }
+
+ [Fact]
+ public void ToolboxItemCollection_IndexOf_NoSuchValue_ReturnsNegativeOne()
+ {
+ var item = new ToolboxItem();
+ var collection = new ToolboxItemCollection(new ToolboxItem[] { item });
+ Assert.Equal(-1, collection.IndexOf(new ToolboxItem { DisplayName = "Other" }));
+ Assert.Equal(-1, collection.IndexOf(null));
+ }
+
+ [Fact]
+ public void ToolboxItemCollection_CopyTo_Invoke_Success()
+ {
+ var item = new ToolboxItem();
+ var collection = new ToolboxItemCollection(new ToolboxItem[] { item });
+
+ var array = new ToolboxItem[3];
+ collection.CopyTo(array, 1);
+ Assert.Equal(new ToolboxItem[] { null, item, null }, array);
+ }
+ }
+}
diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/Drawing/Design/ToolboxItemTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/Drawing/Design/ToolboxItemTests.cs
new file mode 100644
index 00000000000..56f0e181697
--- /dev/null
+++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/Drawing/Design/ToolboxItemTests.cs
@@ -0,0 +1,1775 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System.Collections;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.ComponentModel.Design;
+using System.Drawing;
+using System.IO;
+using System.Linq;
+using System.Reflection;
+using System.Runtime.Loader;
+using Moq;
+using WinForms.Common.Tests;
+using Xunit;
+
+namespace System.Drawing.Design.Tests
+{
+ public class ToolboxItemTests
+ {
+ [Fact]
+ public void ToolboxItem_Ctor_Default()
+ {
+ var item = new ToolboxItem();
+ Assert.Null(item.AssemblyName);
+ Assert.Null(item.Bitmap);
+ Assert.Null(item.Company);
+ Assert.Equal(".NET Component", item.ComponentType);
+ Assert.Null(item.DependentAssemblies);
+ Assert.Null(item.Description);
+ Assert.Empty(item.DisplayName);
+ Assert.Empty(item.Filter);
+ Assert.Same(item.Filter, item.Filter);
+ Assert.False(item.IsTransient);
+ Assert.False(item.Locked);
+ Assert.Null(item.OriginalBitmap);
+ Assert.Empty(item.Properties);
+ Assert.Same(item.Properties, item.Properties);
+ Assert.False(item.Properties.IsFixedSize);
+ Assert.False(item.Properties.IsReadOnly);
+ Assert.Empty(item.TypeName);
+ Assert.Empty(item.Version);
+ }
+
+ public static IEnumerable