Skip to content

Commit 8f1f3a4

Browse files
MelonWang1KlausLoeffelmann
authored andcommitted
Add coverage for ToolStripContainer_ToolStripContainerTypedContro… (dotnet#13527)
* Add ode coverage for ToolStripContainer_ToolStripContainerTypedControlCollection * Handle feedback! * Updated!
1 parent 3757fb3 commit 8f1f3a4

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
namespace System.Windows.Forms.Tests;
5+
6+
public class ToolStripContainer_ToolStripContainerTypedControlCollectionTests
7+
{
8+
private static ToolStripContainer.ToolStripContainerTypedControlCollection CreateCollection(bool isReadOnly) =>
9+
new(new ToolStripContainer(), isReadOnly);
10+
11+
[WinFormsFact]
12+
public void Add_NullValue_DoesNothing()
13+
{
14+
ToolStripContainer.ToolStripContainerTypedControlCollection collection = CreateCollection(isReadOnly: false);
15+
16+
collection.Add(null);
17+
18+
collection.Cast<Control>().Should().BeEmpty();
19+
}
20+
21+
[WinFormsFact]
22+
public void Add_WhenReadOnly_ThrowsNotSupportedException()
23+
{
24+
ToolStripContainer.ToolStripContainerTypedControlCollection collection = CreateCollection(isReadOnly: true);
25+
using ToolStripPanel panel = new();
26+
27+
Action act = () => collection.Add(panel);
28+
29+
act.Should().Throw<NotSupportedException>();
30+
}
31+
32+
[WinFormsFact]
33+
public void Add_InvalidType_ThrowsArgumentException()
34+
{
35+
ToolStripContainer.ToolStripContainerTypedControlCollection collection = CreateCollection(isReadOnly: false);
36+
using Button button = new();
37+
38+
Action act = () => collection.Add(button);
39+
40+
act.Should().Throw<ArgumentException>();
41+
}
42+
43+
[WinFormsTheory]
44+
[InlineData(typeof(ToolStripPanel))]
45+
[InlineData(typeof(ToolStripContentPanel))]
46+
public void Add_ValidType_AddsSuccessfully(Type type)
47+
{
48+
using Form form = new();
49+
using ToolStripContainer container = new();
50+
form.Controls.Add(container);
51+
52+
ToolStripContainer.ToolStripContainerTypedControlCollection collection = new(container, isReadOnly: false);
53+
54+
Control control = (Control)Activator.CreateInstance(type)!;
55+
56+
collection.Add(control);
57+
58+
collection.Cast<Control>().Should().Contain(control);
59+
60+
collection.Remove(control);
61+
control.Dispose();
62+
}
63+
64+
[WinFormsTheory]
65+
[InlineData(typeof(ToolStripPanel))]
66+
[InlineData(typeof(ToolStripContentPanel))]
67+
public void Remove_WhenReadOnly_ThrowsNotSupportedException(Type type)
68+
{
69+
using ToolStripContainer container = new();
70+
ToolStripContainer.ToolStripContainerTypedControlCollection collection = new(container, isReadOnly: true);
71+
using Control control = (Control)Activator.CreateInstance(type)!;
72+
container.ContentPanel.Controls.Add(control);
73+
74+
Action act = () => collection.Remove(control);
75+
76+
act.Should().Throw<NotSupportedException>();
77+
}
78+
79+
[WinFormsFact]
80+
public void Remove_Panel_WhenNotReadOnly_RemovesPanel()
81+
{
82+
ToolStripContainer.ToolStripContainerTypedControlCollection collection = CreateCollection(isReadOnly: false);
83+
using ToolStripPanel panel = new();
84+
collection.Add(panel);
85+
86+
collection.Remove(panel);
87+
88+
collection.Cast<Control>().Should().NotContain(panel);
89+
}
90+
91+
[WinFormsFact]
92+
public void Remove_NonPanelOrContentPanel_DoesNotThrow()
93+
{
94+
using ToolStripContainer container = new();
95+
ToolStripContainer.ToolStripContainerTypedControlCollection collection = new(container, isReadOnly: false);
96+
using Button button = new();
97+
container.ContentPanel.Controls.Add(button);
98+
99+
Action act = () => collection.Remove(button);
100+
101+
act.Should().NotThrow();
102+
collection.Cast<Control>().Should().NotContain(button);
103+
}
104+
105+
[WinFormsTheory]
106+
[InlineData(typeof(ToolStripPanel))]
107+
[InlineData(typeof(ToolStripContentPanel))]
108+
public void SetChildIndexInternal_WhenReadOnly_ThrowsNotSupportedException(Type type)
109+
{
110+
using ToolStripContainer container = new();
111+
ToolStripContainer.ToolStripContainerTypedControlCollection collection = new(container, isReadOnly: true);
112+
using Control control = (Control)Activator.CreateInstance(type)!;
113+
container.ContentPanel.Controls.Add(control);
114+
115+
Action act = () => collection.SetChildIndexInternal(control, 0);
116+
117+
act.Should().Throw<NotSupportedException>();
118+
}
119+
120+
[WinFormsFact]
121+
public void SetChildIndexInternal_Panel_WhenNotReadOnly_CallsBase()
122+
{
123+
using Form form = new();
124+
using ToolStripContainer container = new();
125+
form.Controls.Add(container);
126+
127+
ToolStripContainer.ToolStripContainerTypedControlCollection collection = new(container, isReadOnly: false);
128+
129+
ToolStripPanel panel = new();
130+
collection.Add(panel);
131+
132+
collection.SetChildIndexInternal(panel, 0);
133+
134+
collection.GetChildIndex(panel).Should().Be(0);
135+
136+
collection.Remove(panel);
137+
panel.Dispose();
138+
}
139+
}

0 commit comments

Comments
 (0)