3
3
4
4
#nullable disable
5
5
6
+ using System . Collections ;
6
7
using System . ComponentModel ;
7
8
using System . Drawing ;
8
9
using System . Windows . Forms . Design ;
9
10
10
11
namespace System . Windows . Forms . Tests ;
11
12
12
- public class PropertyTabCollectionTests
13
+ public class PropertyTabCollectionTests : IDisposable
13
14
{
15
+ private readonly PropertyGrid _propertyGrid ;
16
+ private readonly PropertyGrid . PropertyTabCollection _propertyTabCollection ;
17
+
18
+ public PropertyTabCollectionTests ( )
19
+ {
20
+ _propertyGrid = new PropertyGrid ( ) ;
21
+ _propertyTabCollection = new PropertyGrid . PropertyTabCollection ( _propertyGrid ) ;
22
+ }
23
+
24
+ public void Dispose ( )
25
+ {
26
+ _propertyGrid . Dispose ( ) ;
27
+ }
28
+
14
29
[ WinFormsTheory ]
15
30
[ InlineData ( typeof ( PropertyGrid ) ) ]
16
31
public void Count_ReturnsCorrectCount ( Type ownerType )
17
32
{
18
- var owner = Activator . CreateInstance ( ownerType ) as PropertyGrid ;
33
+ PropertyGrid owner = Activator . CreateInstance ( ownerType ) as PropertyGrid ;
19
34
TestPropertyTabCollection propertyTabCollection = new ( owner ) ;
20
35
propertyTabCollection . Count . Should ( ) . Be ( 1 ) ; // PropertyGrid initially contains one PropertiesTab
21
36
}
@@ -25,11 +40,11 @@ public void Count_ReturnsCorrectCount(Type ownerType)
25
40
[ InlineData ( typeof ( PropertyGrid ) , 1 , typeof ( TestPropertyTab ) ) ]
26
41
public void Indexer_ReturnsCorrectTab ( Type ownerType , int index , Type expectedTabType )
27
42
{
28
- var owner = Activator . CreateInstance ( ownerType ) as PropertyGrid ;
43
+ PropertyGrid owner = Activator . CreateInstance ( ownerType ) as PropertyGrid ;
29
44
TestPropertyTabCollection propertyTabCollection = new ( owner ) ;
30
45
propertyTabCollection . AddTabType ( typeof ( TestPropertyTab ) ) ;
31
46
32
- var tab = propertyTabCollection [ index ] ;
47
+ PropertyTab tab = propertyTabCollection [ index ] ;
33
48
tab . Should ( ) . BeOfType ( expectedTabType ) ;
34
49
}
35
50
@@ -38,7 +53,7 @@ public void Indexer_ReturnsCorrectTab(Type ownerType, int index, Type expectedTa
38
53
[ InlineData ( typeof ( PropertyGrid ) , typeof ( TestPropertyTab ) , 2 , true ) ]
39
54
public void AddTabType_WithDifferentInputs ( Type ownerType , Type tabType , int expectedCount , bool addTwice )
40
55
{
41
- var owner = Activator . CreateInstance ( ownerType ) as PropertyGrid ;
56
+ PropertyGrid owner = Activator . CreateInstance ( ownerType ) as PropertyGrid ;
42
57
TestPropertyTabCollection propertyTabCollection = new ( owner ) ;
43
58
int initialCount = propertyTabCollection . Count ;
44
59
@@ -55,6 +70,92 @@ public void AddTabType_WithDifferentInputs(Type ownerType, Type tabType, int exp
55
70
}
56
71
}
57
72
73
+ [ WinFormsFact ]
74
+ public void AddTabType_AddsTab ( )
75
+ {
76
+ int initialCount = _propertyTabCollection . Count ;
77
+ _propertyTabCollection . AddTabType ( typeof ( TestPropertyTab ) ) ;
78
+
79
+ _propertyTabCollection . Count . Should ( ) . Be ( initialCount + 1 ) ;
80
+ _propertyTabCollection [ initialCount ] . Should ( ) . BeOfType < TestPropertyTab > ( ) ;
81
+ }
82
+
83
+ [ WinFormsFact ]
84
+ public void AddTabType_WithScope_AddsTab ( )
85
+ {
86
+ int initialCount = _propertyTabCollection . Count ;
87
+ _propertyTabCollection . AddTabType ( typeof ( TestPropertyTab ) , PropertyTabScope . Component ) ;
88
+
89
+ _propertyTabCollection . Count . Should ( ) . Be ( initialCount + 1 ) ;
90
+ _propertyTabCollection [ initialCount ] . Should ( ) . BeOfType < TestPropertyTab > ( ) ;
91
+ }
92
+
93
+ [ WinFormsFact ]
94
+ public void RemoveTabType_RemovesTab ( )
95
+ {
96
+ _propertyTabCollection . AddTabType ( typeof ( TestPropertyTab ) ) ;
97
+ int countAfterAdd = _propertyTabCollection . Count ;
98
+
99
+ _propertyTabCollection . RemoveTabType ( typeof ( TestPropertyTab ) ) ;
100
+
101
+ _propertyTabCollection . Count . Should ( ) . Be ( countAfterAdd - 1 ) ;
102
+ _propertyTabCollection . Cast < PropertyTab > ( ) . Should ( ) . NotContain ( tab => tab is TestPropertyTab ) ;
103
+ }
104
+
105
+ [ WinFormsFact ]
106
+ public void Clear_RemovesTabsOfGivenScope ( )
107
+ {
108
+ _propertyTabCollection . AddTabType ( typeof ( TestPropertyTab ) , PropertyTabScope . Component ) ;
109
+ int countAfterAdd = _propertyTabCollection . Count ;
110
+
111
+ _propertyTabCollection . Clear ( PropertyTabScope . Component ) ;
112
+
113
+ _propertyTabCollection . Count . Should ( ) . BeLessThan ( countAfterAdd ) ;
114
+ }
115
+
116
+ [ WinFormsFact ]
117
+ public void CopyTo_CopiesTabsToArray ( )
118
+ {
119
+ _propertyTabCollection . AddTabType ( typeof ( TestPropertyTab ) ) ;
120
+ PropertyTab [ ] array = new PropertyTab [ _propertyTabCollection . Count ] ;
121
+
122
+ ( ( ICollection ) _propertyTabCollection ) . CopyTo ( array , 0 ) ;
123
+
124
+ array . Should ( ) . ContainItemsAssignableTo < PropertyTab > ( ) ;
125
+ array . Should ( ) . Contain ( tab => tab is TestPropertyTab ) ;
126
+ }
127
+
128
+ [ WinFormsFact ]
129
+ public void GetEnumerator_EnumeratesTabs ( )
130
+ {
131
+ _propertyTabCollection . AddTabType ( typeof ( TestPropertyTab ) ) ;
132
+
133
+ int count = 0 ;
134
+ foreach ( PropertyTab tab in _propertyTabCollection )
135
+ {
136
+ tab . Should ( ) . NotBeNull ( ) ;
137
+ count ++ ;
138
+ }
139
+
140
+ count . Should ( ) . Be ( _propertyTabCollection . Count ) ;
141
+ }
142
+
143
+ [ WinFormsFact ]
144
+ public void SyncRoot_ReturnsSelf ( )
145
+ {
146
+ object syncRoot = ( ( ICollection ) _propertyTabCollection ) . SyncRoot ;
147
+
148
+ syncRoot . Should ( ) . BeSameAs ( _propertyTabCollection ) ;
149
+ }
150
+
151
+ [ WinFormsFact ]
152
+ public void IsSynchronized_ReturnsFalse ( )
153
+ {
154
+ bool isSynchronized = ( ( ICollection ) _propertyTabCollection ) . IsSynchronized ;
155
+
156
+ isSynchronized . Should ( ) . BeFalse ( ) ;
157
+ }
158
+
58
159
public class TestPropertyTabCollection : PropertyGrid . PropertyTabCollection
59
160
{
60
161
public TestPropertyTabCollection ( PropertyGrid ownerPropertyGrid ) : base ( ownerPropertyGrid )
@@ -72,18 +173,7 @@ public class TestPropertyTab : PropertyTab, IDisposable
72
173
73
174
public override PropertyDescriptorCollection GetProperties ( ITypeDescriptorContext context , object component , Attribute [ ] attributes ) => TypeDescriptor . GetProperties ( component , attributes ) ;
74
175
75
- public override Bitmap Bitmap
76
- {
77
- get
78
- {
79
- if ( _bitmap is null )
80
- {
81
- _bitmap = new Bitmap ( 1 , 1 ) ;
82
- }
83
-
84
- return _bitmap ;
85
- }
86
- }
176
+ public override Bitmap Bitmap => _bitmap ??= new ( 1 , 1 ) ;
87
177
88
178
public override void Dispose ( )
89
179
{
0 commit comments