Skip to content

Commit 25e6260

Browse files
authored
Port DesignerAction*Collection components (#633)
* port DesignerActionListCollection * port DesignerActionItemCollection * PR comments
1 parent 0cfea26 commit 25e6260

File tree

4 files changed

+195
-64
lines changed

4 files changed

+195
-64
lines changed

src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionItemCollection.cs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,20 @@ public class DesignerActionItemCollection : CollectionBase
1010
{
1111
public DesignerActionItem this[int index]
1212
{
13-
get => throw new NotImplementedException(SR.NotImplementedByDesign);
14-
set => throw new NotImplementedException(SR.NotImplementedByDesign);
13+
get => (DesignerActionItem)(List[index]);
14+
set => List[index] = value;
1515
}
1616

17-
public int Add(DesignerActionItem value)
18-
{
19-
throw new NotImplementedException(SR.NotImplementedByDesign);
20-
}
21-
22-
public bool Contains(DesignerActionItem value)
23-
{
24-
throw new NotImplementedException(SR.NotImplementedByDesign);
25-
}
26-
27-
public void CopyTo(DesignerActionItem[] array, int index)
28-
{
29-
throw new NotImplementedException(SR.NotImplementedByDesign);
30-
}
17+
public int Add(DesignerActionItem value) => List.Add(value);
3118

32-
public int IndexOf(DesignerActionItem value)
33-
{
34-
throw new NotImplementedException(SR.NotImplementedByDesign);
35-
}
19+
public bool Contains(DesignerActionItem value) => List.Contains(value);
3620

37-
public void Insert(int index, DesignerActionItem value)
38-
{
39-
throw new NotImplementedException(SR.NotImplementedByDesign);
40-
}
21+
public void CopyTo(DesignerActionItem[] array, int index) => List.CopyTo(array, index);
4122

42-
public void Remove(DesignerActionItem value)
43-
{
44-
throw new NotImplementedException(SR.NotImplementedByDesign);
45-
}
23+
public int IndexOf(DesignerActionItem value) => List.IndexOf(value);
24+
25+
public void Insert(int index, DesignerActionItem value) => List.Insert(index, value);
26+
27+
public void Remove(DesignerActionItem value) => List.Remove(value);
4628
}
4729
}

src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionListCollection.cs

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Collections;
6+
using System.Diagnostics;
67
using System.Runtime.InteropServices;
78

89
namespace System.ComponentModel.Design
@@ -21,50 +22,50 @@ public DesignerActionListCollection(DesignerActionList[] value)
2122

2223
public DesignerActionList this[int index]
2324
{
24-
get => throw new NotImplementedException(SR.NotImplementedByDesign);
25-
set => throw new NotImplementedException(SR.NotImplementedByDesign);
25+
get => (DesignerActionList)(List[index]);
26+
set => List[index] = value;
2627
}
2728

2829
public int Add(DesignerActionList value)
2930
{
30-
throw new NotImplementedException(SR.NotImplementedByDesign);
31+
return List.Add(value);
3132
}
3233

3334
public void AddRange(DesignerActionList[] value)
3435
{
35-
throw new NotImplementedException(SR.NotImplementedByDesign);
36+
if (value == null)
37+
{
38+
throw new ArgumentNullException(nameof(value));
39+
}
40+
for (int i = 0; i < value.Length; i++)
41+
{
42+
Add(value[i]);
43+
}
3644
}
3745

3846
public void AddRange(DesignerActionListCollection value)
3947
{
40-
throw new NotImplementedException(SR.NotImplementedByDesign);
41-
}
42-
43-
public void Insert(int index, DesignerActionList value)
44-
{
45-
throw new NotImplementedException(SR.NotImplementedByDesign);
46-
}
47-
48-
public int IndexOf(DesignerActionList value)
49-
{
50-
throw new NotImplementedException(SR.NotImplementedByDesign);
51-
}
52-
53-
public bool Contains(DesignerActionList value)
54-
{
55-
throw new NotImplementedException(SR.NotImplementedByDesign);
56-
}
57-
58-
public void Remove(DesignerActionList value)
59-
{
60-
throw new NotImplementedException(SR.NotImplementedByDesign);
61-
}
62-
63-
public void CopyTo(DesignerActionList[] array, int index)
64-
{
65-
throw new NotImplementedException(SR.NotImplementedByDesign);
66-
}
67-
48+
if (value == null)
49+
{
50+
throw new ArgumentNullException(nameof(value));
51+
}
52+
int currentCount = value.Count;
53+
for (int i = 0; i < currentCount; i++)
54+
{
55+
Add(value[i]);
56+
}
57+
}
58+
59+
public void Insert(int index, DesignerActionList value) => List.Insert(index, value);
60+
61+
public int IndexOf(DesignerActionList value) => List.IndexOf(value);
62+
63+
public bool Contains(DesignerActionList value) => List.Contains(value);
64+
65+
public void Remove(DesignerActionList value) => List.Remove(value);
66+
67+
public void CopyTo(DesignerActionList[] array, int index) => List.CopyTo(array, index);
68+
6869
protected override void OnSet(int index, object oldValue, object newValue)
6970
{
7071
}
@@ -81,9 +82,6 @@ protected override void OnRemove(int index, object value)
8182
{
8283
}
8384

84-
protected override void OnValidate(object value)
85-
{
86-
throw new NotImplementedException(SR.NotImplementedByDesign);
87-
}
85+
protected override void OnValidate(object value) => Debug.Assert(value != null, "Don't add null actionlist!");
8886
}
8987
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
// See the LICENSE file in the project root for more information.
4+
5+
using System.ComponentModel.Design;
6+
using Xunit;
7+
8+
namespace System.Windows.Forms.Design.Tests
9+
{
10+
public class DesignerActionItemCollectionTests
11+
{
12+
[Fact]
13+
public void DesignerActionItemCollection_Constructor()
14+
{
15+
DesignerActionItemCollection underTest = new DesignerActionItemCollection();
16+
Assert.NotNull(underTest);
17+
Assert.Empty(underTest);
18+
}
19+
20+
[Fact]
21+
public void DesignerActionItemCollection_Add_Contains_IndexOf()
22+
{
23+
DesignerActionItemCollection underTest = new DesignerActionItemCollection();
24+
25+
DesignerActionItem item1 = new DesignerActionItemTest("name", "category", "description");
26+
underTest.Add(item1);
27+
Assert.True(underTest.Contains(item1));
28+
Assert.Equal(0, underTest.IndexOf(item1));
29+
30+
DesignerActionItem item2 = new DesignerActionItemTest("name1", "category1", "description1");
31+
underTest.Add(item2);
32+
Assert.True(underTest.Contains(item2));
33+
Assert.Equal(1, underTest.IndexOf(item2));
34+
}
35+
36+
[Fact]
37+
public void DesignerActionItemCollection_Insert_Remove_Count()
38+
{
39+
DesignerActionItemCollection underTest = new DesignerActionItemCollection();
40+
DesignerActionItem item1 = new DesignerActionItemTest("name", "category", "description");
41+
DesignerActionItem item2 = new DesignerActionItemTest("name1", "category1", "description1");
42+
DesignerActionItem item3 = new DesignerActionItemTest("name2", "category2", "description2");
43+
DesignerActionItem item4 = new DesignerActionItemTest("name3", "category3", "description3");
44+
45+
underTest.Add(item1);
46+
underTest.Add(item2);
47+
48+
underTest.Add(item3);
49+
Assert.Equal(2, underTest.IndexOf(item3));
50+
51+
underTest.Insert(2, item4);
52+
Assert.Equal(3, underTest.IndexOf(item3));
53+
Assert.Equal(2, underTest.IndexOf(item4));
54+
55+
underTest.Remove(item4);
56+
Assert.False(underTest.Contains(item4));
57+
Assert.Equal(2, underTest.IndexOf(item3));
58+
Assert.Equal(3, underTest.Count);
59+
}
60+
61+
private class DesignerActionItemTest : DesignerActionItem
62+
{
63+
public DesignerActionItemTest(string displayName, string category, string description) : base(displayName, category, description)
64+
{
65+
}
66+
}
67+
}
68+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
// See the LICENSE file in the project root for more information.
4+
5+
using System.ComponentModel.Design;
6+
using WinForms.Common.Tests;
7+
using Xunit;
8+
9+
namespace System.Windows.Forms.Design.Tests
10+
{
11+
public class DesignerActionListCollectionTests
12+
{
13+
[Fact]
14+
public void DesignerActionListCollection_Constructor()
15+
{
16+
DesignerActionListCollection underTest = new DesignerActionListCollection();
17+
Assert.NotNull(underTest);
18+
}
19+
20+
[Fact]
21+
public void DesignerActionListCollection_Constructor_DesignerActionList()
22+
{
23+
DesignerActionListCollection underTest = CreateNewCollectionOfItems();
24+
Assert.NotNull(underTest);
25+
}
26+
27+
[Fact]
28+
public void DesignerActionItemCollection_Add_AddRange()
29+
{
30+
DesignerActionListCollection underTest = CreateNewCollectionOfItems(5);
31+
Assert.Equal(5, underTest.Count);
32+
33+
Button button = new Button();
34+
DesignerActionList list = new DesignerActionList(button);
35+
underTest.Add(list);
36+
Assert.Equal(6, underTest.Count);
37+
38+
DesignerActionListCollection other = CreateNewCollectionOfItems(3);
39+
underTest.AddRange(other);
40+
Assert.Equal(9, underTest.Count);
41+
}
42+
43+
44+
[Fact]
45+
public void DesignerActionItemCollection_Insert_Contains_IndexOf()
46+
{
47+
DesignerActionListCollection underTest = CreateNewCollectionOfItems(5);
48+
49+
Button button = new Button();
50+
DesignerActionList list = new DesignerActionList(button);
51+
underTest.Insert(3, list);
52+
Assert.True(underTest.Contains(list));
53+
Assert.Equal(3, underTest.IndexOf(list));
54+
}
55+
56+
[Fact]
57+
public void DesignerActionItemCollection_Remove()
58+
{
59+
DesignerActionListCollection underTest = CreateNewCollectionOfItems(5);
60+
61+
Button button = new Button();
62+
DesignerActionList list = new DesignerActionList(button);
63+
underTest.Insert(3, list);
64+
underTest.Remove(list);
65+
Assert.False(underTest.Contains(list));
66+
Assert.Equal(5, underTest.Count);
67+
}
68+
69+
private DesignerActionListCollection CreateNewCollectionOfItems(int numberOfItems = 1)
70+
{
71+
Button button = new Button();
72+
DesignerActionList[] list = new DesignerActionList[] { new DesignerActionList(button) };
73+
DesignerActionListCollection underTest = new DesignerActionListCollection(list);
74+
75+
for (int i = 1; i < numberOfItems; i++)
76+
{
77+
underTest.Add(new DesignerActionList(button));
78+
}
79+
80+
return underTest;
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)