From 94d118bd3f98d5686cf75d0eb780376e6524eb63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klaus=20L=C3=B6ffelmann?= Date: Tue, 12 Mar 2019 16:34:43 +0100 Subject: [PATCH] Port the ArrayEditor from the classic framework and add unit test. The ArrayEditor is needed to enable runtime designer functionality for the Chart control (WinForms Core DataVisualization). --- .../ComponentModel/Design/ArrayEditor.cs | 60 ++++++++++++++++ .../tests/UnitTests/ArrayEditorTests.cs | 70 +++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 src/System.Windows.Forms.Design.Editors/src/System/ComponentModel/Design/ArrayEditor.cs create mode 100644 src/System.Windows.Forms.Design.Editors/tests/UnitTests/ArrayEditorTests.cs diff --git a/src/System.Windows.Forms.Design.Editors/src/System/ComponentModel/Design/ArrayEditor.cs b/src/System.Windows.Forms.Design.Editors/src/System/ComponentModel/Design/ArrayEditor.cs new file mode 100644 index 00000000000..96f37093d15 --- /dev/null +++ b/src/System.Windows.Forms.Design.Editors/src/System/ComponentModel/Design/ArrayEditor.cs @@ -0,0 +1,60 @@ +// 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; + +namespace System.ComponentModel.Design +{ + /// + /// Edits an array of values. + /// + public class ArrayEditor : CollectionEditor + { + + /// + /// Initializes a new instance of + /// using the specified type for the array. + /// + public ArrayEditor(Type type) : base(type) + { + } + + /// + /// Gets or sets the data type this collection contains. + /// + protected override Type CreateCollectionItemType() => CollectionType.GetElementType(); + + /// + /// Gets the items in the array. + /// + /// + protected override object[] GetItems(object editValue) + { + if (editValue is Array) + { + Array valueArray = (Array)editValue; + object[] items = new object[valueArray.GetLength(0)]; + Array.Copy(valueArray, items, items.Length); + return items; + } + else + { + return new object[0]; + } + } + + /// + /// Sets the items in the array. + /// + protected override object SetItems(object editValue, object[] value) + { + if (editValue is Array || editValue == null) + { + Array newArray = Array.CreateInstance(CollectionItemType, value.Length); + Array.Copy(value, newArray, value.Length); + return newArray; + } + return editValue; + } + } +} diff --git a/src/System.Windows.Forms.Design.Editors/tests/UnitTests/ArrayEditorTests.cs b/src/System.Windows.Forms.Design.Editors/tests/UnitTests/ArrayEditorTests.cs new file mode 100644 index 00000000000..93b3a8b4465 --- /dev/null +++ b/src/System.Windows.Forms.Design.Editors/tests/UnitTests/ArrayEditorTests.cs @@ -0,0 +1,70 @@ +// 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.ComponentModel; +using System.Globalization; +using System.Reflection; +using Xunit; + +namespace System.ComponentModel.Design.Tests +{ + public class ArrayEditorTests + { + [Fact] + public void ArrayEditor_Constructor() + { + var underTest = GetNewEditor(); + Assert.NotNull(underTest); + } + + [Fact] + public void ArrayEditor_Getters() + { + var underTest = GetNewEditor(); + Assert.Equal(typeof(string), underTest.GetCollectionType()); + Assert.True(underTest.CanSelectMultiple()); + Assert.True(underTest.CanRemove("some string")); + Assert.True(underTest.CanRemove(1234)); + Assert.Equal("net.ComponentModel.CollectionEditor", underTest.GetHelpTopic()); + Assert.Equal("my string", underTest.GetItemDisplayText("my string")); + } + + private TestArrayEditor GetNewEditor() + { + return new TestArrayEditor(typeof(string)); + } + + private class TestArrayEditor : ArrayEditor + { + public TestArrayEditor(Type type) : base(type) + { + } + + public Type GetCollectionType() + { + return base.CollectionType; + } + + public bool CanSelectMultiple() + { + return base.CanSelectMultipleInstances(); + } + + public bool CanRemove(object value) + { + return base.CanRemoveInstance(value); + } + + public string GetHelpTopic() + { + return base.HelpTopic; + } + + public string GetItemDisplayText(object value) + { + return base.GetDisplayText(value); + } + } + } +}