Skip to content

Commit aa719d1

Browse files
authored
Add code coverage for TabRenderer (#13482)
* Add code coverage for TabRenderer
1 parent b2e4489 commit aa719d1

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
using System.Drawing;
5+
using System.Windows.Forms.VisualStyles;
6+
7+
namespace System.Windows.Forms.Tests;
8+
9+
public class TabRendererTests : IDisposable
10+
{
11+
private readonly Bitmap _bitmap;
12+
private readonly Graphics _graphics;
13+
private readonly Rectangle _bounds;
14+
15+
public TabRendererTests()
16+
{
17+
_bitmap = new(100, 30);
18+
_graphics = Graphics.FromImage(_bitmap);
19+
_bounds = new(0, 0, 100, 30);
20+
}
21+
22+
public void Dispose()
23+
{
24+
_graphics.Dispose();
25+
_bitmap.Dispose();
26+
}
27+
28+
[Fact]
29+
public void IsSupported_Matches_VisualStyleRenderer_IsSupported() =>
30+
TabRenderer.IsSupported.Should().Be(VisualStyleRenderer.IsSupported);
31+
32+
[WinFormsFact]
33+
public void DrawTabItem_Basic_ChangesTabStateSuccessfully()
34+
{
35+
Color colorBeforeDraw = _bitmap.GetPixel(10, 10);
36+
37+
foreach (TabItemState state in Enum.GetValues<TabItemState>())
38+
{
39+
TabRenderer.DrawTabItem(_graphics, _bounds, state);
40+
TabRenderer.DrawTabItem(_graphics, _bounds, focused: true, state);
41+
}
42+
43+
Color colorAfterDraw = _bitmap.GetPixel(10, 10);
44+
colorAfterDraw.Should().NotBe(colorBeforeDraw);
45+
}
46+
47+
[WinFormsFact]
48+
public void DrawTabItem_WithText_ChangesTabStateSuccessfully()
49+
{
50+
using Font font = new("Arial", 8);
51+
Color colorBeforeDraw = _bitmap.GetPixel(10, 10);
52+
53+
foreach (TabItemState state in Enum.GetValues<TabItemState>())
54+
{
55+
TabRenderer.DrawTabItem(_graphics, _bounds, "TabText", font, state);
56+
}
57+
58+
Color colorAfterDraw = _bitmap.GetPixel(10, 10);
59+
colorAfterDraw.Should().NotBe(colorBeforeDraw);
60+
}
61+
62+
[WinFormsFact]
63+
public void DrawTabItem_WithText_Focused_ChangesTabStateSuccessfully()
64+
{
65+
using Font font = new("Arial", 8);
66+
Color colorBeforeDraw = _bitmap.GetPixel(10, 10);
67+
68+
foreach (TabItemState state in Enum.GetValues<TabItemState>())
69+
{
70+
TabRenderer.DrawTabItem(_graphics, _bounds, "TabText", font, focused: true, state);
71+
}
72+
73+
Color colorAfterDraw = _bitmap.GetPixel(10, 10);
74+
colorAfterDraw.Should().NotBe(colorBeforeDraw);
75+
}
76+
77+
[WinFormsFact]
78+
public void DrawTabItem_WithText_TextFormatFlags_Focused_ChangesTabStateSuccessfully()
79+
{
80+
using Font font = new("Arial", 8);
81+
Color colorBeforeDraw = _bitmap.GetPixel(10, 10);
82+
83+
foreach (TabItemState state in Enum.GetValues<TabItemState>())
84+
{
85+
TabRenderer.DrawTabItem(_graphics, _bounds, "TabText", font, TextFormatFlags.Default, focused: true, state);
86+
}
87+
88+
Color colorAfterDraw = _bitmap.GetPixel(10, 10);
89+
colorAfterDraw.Should().NotBe(colorBeforeDraw);
90+
}
91+
92+
[WinFormsFact]
93+
public void DrawTabItem_WithImage_ChangesTabStateSuccessfully()
94+
{
95+
using Bitmap image = new(16, 16);
96+
Rectangle imageRect = new(5, 5, 16, 16);
97+
Color colorBeforeDraw = _bitmap.GetPixel(10, 10);
98+
99+
foreach (TabItemState state in Enum.GetValues<TabItemState>())
100+
{
101+
TabRenderer.DrawTabItem(_graphics, _bounds, image, imageRect, focused: false, state);
102+
TabRenderer.DrawTabItem(_graphics, _bounds, image, imageRect, focused: true, state);
103+
}
104+
105+
Color colorAfterDraw = _bitmap.GetPixel(10, 10);
106+
colorAfterDraw.Should().NotBe(colorBeforeDraw);
107+
}
108+
109+
[WinFormsFact]
110+
public void DrawTabItem_WithTextAndImage_ChangesTabStateSuccessfully()
111+
{
112+
using Font font = new("Arial", 8);
113+
using Bitmap image = new(16, 16);
114+
Rectangle imageRect = new(5, 5, 16, 16);
115+
Color colorBeforeDraw = _bitmap.GetPixel(10, 10);
116+
117+
foreach (TabItemState state in Enum.GetValues<TabItemState>())
118+
{
119+
TabRenderer.DrawTabItem(_graphics, _bounds, "TabText", font, image, imageRect, focused: false, state);
120+
TabRenderer.DrawTabItem(_graphics, _bounds, "TabText", font, TextFormatFlags.Default, image, imageRect, focused: true, state);
121+
}
122+
123+
Color colorAfterDraw = _bitmap.GetPixel(10, 10);
124+
colorAfterDraw.Should().NotBe(colorBeforeDraw);
125+
}
126+
127+
[WinFormsFact]
128+
public void DrawTabPage_ChangesTabPageStateSuccessfully()
129+
{
130+
using Bitmap bmp = new(100, 100);
131+
using Graphics g = Graphics.FromImage(bmp);
132+
Rectangle bounds = new(0, 0, 100, 100);
133+
Color colorBeforeDraw = bmp.GetPixel(10, 10);
134+
135+
TabRenderer.DrawTabPage(g, bounds);
136+
137+
Color colorAfterDraw = bmp.GetPixel(10, 10);
138+
colorAfterDraw.Should().NotBe(colorBeforeDraw);
139+
}
140+
}

0 commit comments

Comments
 (0)