1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
+ using System . ComponentModel . Design ;
4
5
using System . Drawing ;
5
6
using System . Drawing . Imaging ;
7
+ using System . Windows . Forms . Design . Behavior ;
8
+ using Moq ;
6
9
7
10
namespace System . Windows . Forms . Design . Tests ;
8
11
@@ -12,17 +15,21 @@ public class DesignerUtilsTests :IDisposable
12
15
private readonly Graphics _graphics ;
13
16
private readonly Rectangle _bounds ;
14
17
18
+ public Button Button { get ; }
19
+
15
20
public DesignerUtilsTests ( )
16
21
{
17
22
_bitmap = new ( 20 , 20 ) ;
18
23
_graphics = Graphics . FromImage ( _bitmap ) ;
19
24
_bounds = new ( 5 , 5 , 20 , 20 ) ;
25
+ Button = new ( ) { Width = 50 , Height = 50 } ;
20
26
}
21
27
22
28
public void Dispose ( )
23
29
{
24
30
_graphics . Dispose ( ) ;
25
31
_bitmap . Dispose ( ) ;
32
+ Button . Dispose ( ) ;
26
33
}
27
34
28
35
[ WinFormsFact ]
@@ -144,4 +151,253 @@ public void DrawSelectionBorder_ShouldFillRectangle_WhenCalled()
144
151
Color pixelColor = _bitmap . GetPixel ( 6 , 6 ) ;
145
152
pixelColor . ToArgb ( ) . Should ( ) . NotBe ( Color . Empty . ToArgb ( ) ) ;
146
153
}
154
+
155
+ [ WinFormsFact ]
156
+ public void LastCursorPoint_ShouldNotThrow_WhenCalled ( )
157
+ {
158
+ Exception exception = Record . Exception ( ( ) =>
159
+ _ = DesignerUtils . LastCursorPoint ) ;
160
+
161
+ exception . Should ( ) . BeNull ( ) ;
162
+ }
163
+
164
+ [ WinFormsFact ]
165
+ public void LastCursorPoint_ShouldReturnValidCoordinates ( )
166
+ {
167
+ Point cursorPoint = DesignerUtils . LastCursorPoint ;
168
+
169
+ cursorPoint . X . Should ( ) . BeLessThanOrEqualTo ( SystemInformation . VirtualScreen . Right ) ;
170
+ cursorPoint . X . Should ( ) . BeGreaterThanOrEqualTo ( SystemInformation . VirtualScreen . Left ) ;
171
+ cursorPoint . Y . Should ( ) . BeLessThanOrEqualTo ( SystemInformation . VirtualScreen . Bottom ) ;
172
+ cursorPoint . Y . Should ( ) . BeGreaterThanOrEqualTo ( SystemInformation . VirtualScreen . Top ) ;
173
+ }
174
+
175
+ [ WinFormsFact ]
176
+ public void SyncBrushes_ShouldRecreateHoverBrush ( )
177
+ {
178
+ DesignerUtils . SyncBrushes ( ) ;
179
+
180
+ DesignerUtils . HoverBrush . Should ( ) . BeOfType < SolidBrush > ( ) ;
181
+ ( ( SolidBrush ) DesignerUtils . HoverBrush ) . Color . Should ( ) . Be ( Color . FromArgb ( 50 , SystemColors . Highlight ) ) ;
182
+ }
183
+
184
+ [ WinFormsTheory ]
185
+ [ InlineData ( FrameStyle . Dashed , true ) ]
186
+ [ InlineData ( FrameStyle . Thick , false ) ]
187
+ public void DrawFrame_ShouldUseCorrectBrushBasedOnStyleAndBackColor ( FrameStyle style , bool useDarkBackground )
188
+ {
189
+ using Region region = new ( new Rectangle ( 0 , 0 , _bounds . Width , _bounds . Height ) ) ;
190
+ Color backColor = useDarkBackground ? Color . Black : Color . White ;
191
+ Color expectedColor = useDarkBackground ? SystemColors . ControlLight : SystemColors . ControlDarkDark ;
192
+
193
+ DesignerUtils . DrawFrame ( _graphics , region , style , backColor ) ;
194
+
195
+ Color pixelColor = _bitmap . GetPixel ( _bounds . Width / 2 , _bounds . Height / 2 ) ;
196
+ pixelColor . ToArgb ( ) . Should ( ) . Be ( expectedColor . ToArgb ( ) ) ;
197
+ }
198
+
199
+ [ WinFormsTheory ]
200
+ [ BoolData ]
201
+ public void DrawNoResizeHandle_ShouldNotThrow_WhenCalledWithValidParameters ( bool isPrimary )
202
+ {
203
+ Exception exception = Record . Exception ( ( ) =>
204
+ DesignerUtils . DrawNoResizeHandle ( _graphics , _bounds , isPrimary ) ) ;
205
+
206
+ exception . Should ( ) . BeNull ( ) ;
207
+ }
208
+
209
+ [ WinFormsTheory ]
210
+ [ BoolData ]
211
+ public void DrawNoResizeHandle_ShouldDrawHandle_BasedOnSelectionType ( bool isPrimary )
212
+ {
213
+ DesignerUtils . DrawNoResizeHandle ( _graphics , _bounds , isPrimary ) ;
214
+
215
+ Color pixelColor = _bitmap . GetPixel ( _bounds . Left + 1 , _bounds . Top + 1 ) ;
216
+ pixelColor . ToArgb ( ) . Should ( ) . NotBe ( Color . Empty . ToArgb ( ) ) ;
217
+ }
218
+
219
+ [ WinFormsFact ]
220
+ public void GenerateSnapShot_ShouldGenerateImage_WhenControlSupportsWM_PRINT ( )
221
+ {
222
+ int borderSize = 2 ;
223
+ double opacity = 0.5 ;
224
+ Color backColor = Color . White ;
225
+
226
+ DesignerUtils . GenerateSnapShot ( Button , out Bitmap image , borderSize , opacity , backColor ) ;
227
+
228
+ using ( image )
229
+ {
230
+ image . Should ( ) . BeOfType < Bitmap > ( ) ;
231
+ image . Width . Should ( ) . BeGreaterThan ( 0 ) ;
232
+ image . Height . Should ( ) . BeGreaterThan ( 0 ) ;
233
+ }
234
+ }
235
+
236
+ [ WinFormsFact ]
237
+ public void GenerateSnapShot_ShouldApplyOpacity ( )
238
+ {
239
+ int borderSize = 0 ;
240
+ double opacity = 0.5 ;
241
+ Color backColor = Color . White ;
242
+
243
+ DesignerUtils . GenerateSnapShot ( Button , out Bitmap image , borderSize , opacity , backColor ) ;
244
+
245
+ using ( image )
246
+ {
247
+ image . Should ( ) . BeOfType < Bitmap > ( ) ;
248
+ Color pixelColor = image . GetPixel ( image . Width / 2 , image . Height / 2 ) ;
249
+ pixelColor . A . Should ( ) . BeLessThan ( 255 ) ;
250
+ }
251
+ }
252
+
253
+ [ WinFormsFact ]
254
+ public void GenerateSnapShot_ShouldDrawBorder_WhenBorderSizeIsGreaterThanZero ( )
255
+ {
256
+ int borderSize = 2 ;
257
+ double opacity = 1.0 ;
258
+ Color backColor = Color . White ;
259
+
260
+ DesignerUtils . GenerateSnapShot ( Button , out Bitmap image , borderSize , opacity , backColor ) ;
261
+
262
+ using ( image )
263
+ {
264
+ image . Should ( ) . BeOfType < Bitmap > ( ) ;
265
+ Color borderPixelColor = image . GetPixel ( 0 , 0 ) ;
266
+ borderPixelColor . Should ( ) . NotBe ( backColor ) ;
267
+ }
268
+ }
269
+
270
+ [ WinFormsTheory ]
271
+ [ InlineData ( AdornmentType . GrabHandle , 7 , 7 ) ]
272
+ [ InlineData ( AdornmentType . ContainerSelector , 15 , 15 ) ]
273
+ [ InlineData ( ( AdornmentType ) 999 , 0 , 0 ) ]
274
+ internal void GetAdornmentDimensions_ShouldReturnExpectedSize ( AdornmentType adornmentType , int expectedWidth , int expectedHeight )
275
+ {
276
+ Size result = DesignerUtils . GetAdornmentDimensions ( adornmentType ) ;
277
+
278
+ result . Width . Should ( ) . Be ( expectedWidth ) ;
279
+ result . Height . Should ( ) . Be ( expectedHeight ) ;
280
+ }
281
+
282
+ [ WinFormsFact ]
283
+ public void UseSnapLines_ShouldReturnTrue_WhenProviderHasNoSnapLinesProperty ( )
284
+ {
285
+ Mock < IServiceProvider > mockProvider = new ( ) ;
286
+ mockProvider . Setup ( p => p . GetService ( typeof ( DesignerOptionService ) ) ) . Returns ( null ! ) ;
287
+
288
+ DesignerUtils . UseSnapLines ( mockProvider . Object ) . Should ( ) . BeTrue ( ) ;
289
+ }
290
+
291
+ [ WinFormsFact ]
292
+ public void GetOptionValue_ShouldReturnNull_WhenProviderIsNull ( )
293
+ {
294
+ object ? result = DesignerUtils . GetOptionValue ( provider : null , name : "SomeOption" ) ;
295
+
296
+ result . Should ( ) . BeNull ( ) ;
297
+ }
298
+
299
+ [ WinFormsFact ]
300
+ public void GetOptionValue_ShouldReturnNull_WhenDesignerOptionServiceIsNotAvailable ( )
301
+ {
302
+ Mock < IServiceProvider > mockProvider = new ( ) ;
303
+ mockProvider . Setup ( p => p . GetService ( typeof ( DesignerOptionService ) ) ) . Returns ( null ! ) ;
304
+
305
+ object ? result = DesignerUtils . GetOptionValue ( mockProvider . Object , "SomeOption" ) ;
306
+
307
+ result . Should ( ) . BeNull ( ) ;
308
+ }
309
+
310
+ [ WinFormsFact ]
311
+ public void GetOptionValue_ShouldReturnNull_WhenIDesignerOptionServiceIsNotAvailable ( )
312
+ {
313
+ Mock < IServiceProvider > mockProvider = new ( ) ;
314
+ mockProvider . Setup ( p => p . GetService ( typeof ( IDesignerOptionService ) ) ) . Returns ( null ! ) ;
315
+
316
+ object ? result = DesignerUtils . GetOptionValue ( mockProvider . Object , "SomeOption" ) ;
317
+
318
+ result . Should ( ) . BeNull ( ) ;
319
+ }
320
+
321
+ [ WinFormsFact ]
322
+ public void GetOptionValue_ShouldReturnValue_WhenIDesignerOptionServiceHasOption ( )
323
+ {
324
+ Mock < IServiceProvider > mockProvider = new ( ) ;
325
+ Mock < IDesignerOptionService > mockOptionService = new ( ) ;
326
+ mockOptionService . Setup ( o => o . GetOptionValue ( "WindowsFormsDesigner\\ General" , "SomeOption" ) ) . Returns ( "ExpectedValue" ) ;
327
+ mockProvider . Setup ( p => p . GetService ( typeof ( IDesignerOptionService ) ) ) . Returns ( mockOptionService . Object ) ;
328
+
329
+ object ? result = DesignerUtils . GetOptionValue ( mockProvider . Object , "SomeOption" ) ;
330
+
331
+ result . Should ( ) . Be ( "ExpectedValue" ) ;
332
+ }
333
+
334
+ [ WinFormsFact ]
335
+ public void GenerateSnapShotWithBitBlt_ShouldGenerateImage_WhenControlIsValid ( )
336
+ {
337
+ DesignerUtils . GenerateSnapShotWithBitBlt ( Button , out Bitmap image ) ;
338
+
339
+ using ( image )
340
+ {
341
+ image . Should ( ) . BeOfType < Bitmap > ( ) ;
342
+ image . Width . Should ( ) . BeGreaterThan ( 0 ) ;
343
+ image . Height . Should ( ) . BeGreaterThan ( 0 ) ;
344
+ }
345
+ }
346
+
347
+ [ WinFormsFact ]
348
+ public void GenerateSnapShotWithBitBlt_ShouldNotThrow_WhenControlIsValid ( )
349
+ {
350
+ Exception exception = Record . Exception ( ( ) =>
351
+ {
352
+ DesignerUtils . GenerateSnapShotWithBitBlt ( Button , out Bitmap image ) ;
353
+ using ( image )
354
+ {
355
+ image . Should ( ) . BeOfType < Bitmap > ( ) ;
356
+ }
357
+ } ) ;
358
+
359
+ exception . Should ( ) . BeNull ( ) ;
360
+ }
361
+
362
+ [ WinFormsFact ]
363
+ public void GenerateSnapShotWithWM_PRINT_ShouldReturnTrue_WhenControlRespondsToWM_PRINT ( )
364
+ {
365
+ bool result = DesignerUtils . GenerateSnapShotWithWM_PRINT ( Button , out Bitmap image ) ;
366
+
367
+ result . Should ( ) . BeTrue ( ) ;
368
+
369
+ using ( image )
370
+ {
371
+ image . Should ( ) . BeOfType < Bitmap > ( ) ;
372
+ image . Width . Should ( ) . BeGreaterThan ( 0 ) ;
373
+ image . Height . Should ( ) . BeGreaterThan ( 0 ) ;
374
+ }
375
+ }
376
+
377
+ [ WinFormsTheory ]
378
+ [ InlineData ( SelectionBorderGlyphType . Top , 30 , 20 , 2 , 28 , 18 , 24 , 2 ) ]
379
+ [ InlineData ( SelectionBorderGlyphType . Bottom , 10 , 20 , 2 , 8 , 40 , 24 , 2 ) ]
380
+ [ InlineData ( SelectionBorderGlyphType . Left , 10 , 20 , 2 , 8 , 18 , 2 , 24 ) ]
381
+ [ InlineData ( SelectionBorderGlyphType . Right , 10 , 20 , 2 , 30 , 18 , 2 , 24 ) ]
382
+ [ InlineData ( SelectionBorderGlyphType . Body , 10 , 20 , 2 , 10 , 20 , 20 , 20 ) ]
383
+ [ InlineData ( ( SelectionBorderGlyphType ) 999 , 10 , 20 , 2 , 0 , 0 , 0 , 0 ) ]
384
+ internal void GetBoundsForSelectionType_ShouldReturnExpectedBounds (
385
+ SelectionBorderGlyphType type ,
386
+ int x ,
387
+ int y ,
388
+ int borderSize ,
389
+ int expectedX ,
390
+ int expectedY ,
391
+ int expectedWidth ,
392
+ int expectedHeight )
393
+ {
394
+ Rectangle originalBounds = new ( x , y , 20 , 20 ) ;
395
+
396
+ Rectangle result = DesignerUtils . GetBoundsForSelectionType ( originalBounds , type , borderSize ) ;
397
+
398
+ result . X . Should ( ) . Be ( expectedX ) ;
399
+ result . Y . Should ( ) . Be ( expectedY ) ;
400
+ result . Width . Should ( ) . Be ( expectedWidth ) ;
401
+ result . Height . Should ( ) . Be ( expectedHeight ) ;
402
+ }
147
403
}
0 commit comments