From ac1a509eb9794fbcd317dfe1969331c4da8ac1f4 Mon Sep 17 00:00:00 2001 From: Igor Velikorossov Date: Mon, 6 Apr 2020 16:45:49 +1000 Subject: [PATCH 1/2] Correct selected tests --- .../System/Windows/Forms/FormTests.cs | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/FormTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/FormTests.cs index af853e4784b..70d26c9fe5c 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/FormTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/FormTests.cs @@ -13,10 +13,10 @@ namespace System.Windows.Forms.Tests { public class FormTests : IClassFixture { - [Fact] + [WinFormsFact] public void Form_Ctor_Default() { - var form = new Form(); + using var form = new Form(); Assert.False(form.Active); Assert.Null(form.ActiveMdiChild); Assert.False(form.AllowTransparency); @@ -31,10 +31,10 @@ public void Form_Ctor_Default() Assert.False(form.Visible); } - [Fact] + [WinFormsFact] public void Form_AcceptButtonGetSet() { - var form = new Form(); + using var form = new Form(); var mock = new Mock(MockBehavior.Strict); mock.Setup(x => x.NotifyDefault(It.IsAny())); @@ -43,11 +43,11 @@ public void Form_AcceptButtonGetSet() Assert.Equal(mock.Object, form.AcceptButton); } - [Theory] + [WinFormsTheory] [CommonMemberData(nameof(CommonTestHelper.GetBoolTheoryData))] public void Form_Active_Set_GetReturnsExpected(bool value) { - var form = new Form + using var form = new Form { Active = value }; @@ -72,11 +72,11 @@ public void Form_ActiveFormNotSetActive() Assert.False(Form.ActiveForm.Active); }*/ - [Fact] + [WinFormsFact] public void Form_ActiveMdiChildInternalGetSet() { - var form = new Form(); - var child = new Form(); + using var form = new Form(); + using var child = new Form(); form.ActiveMdiChildInternal = child; @@ -84,11 +84,11 @@ public void Form_ActiveMdiChildInternalGetSet() Assert.Equal(child, form.ActiveMdiChildInternal); } - [Fact] + [WinFormsFact] public void Form_ActiveMdiChildGetSet() { - var form = new Form(); - var child = new Form + using var form = new Form(); + using var child = new Form { Visible = true, Enabled = true @@ -100,11 +100,11 @@ public void Form_ActiveMdiChildGetSet() Assert.Equal(child, form.ActiveMdiChild); } - [Fact] + [WinFormsFact] public void Form_ActiveMdiChildGetSetChildNotVisible() { - var form = new Form(); - var child = new Form + using var form = new Form(); + using var child = new Form { Visible = false, Enabled = true @@ -115,11 +115,11 @@ public void Form_ActiveMdiChildGetSetChildNotVisible() Assert.Null(form.ActiveMdiChild); } - [Fact] + [WinFormsFact] public void Form_ActiveMdiChildGetSetChildNotEnabled() { - var form = new Form(); - var child = new Form + using var form = new Form(); + using var child = new Form { Visible = true, Enabled = false From b38d6f47fce23f711b80822a6fb751ced6702d9d Mon Sep 17 00:00:00 2001 From: Igor Velikorossov Date: Mon, 6 Apr 2020 17:03:04 +1000 Subject: [PATCH 2/2] fix: Hide icon when `ShowIcon = false` Resolves #3022 --- .../src/System/Windows/Forms/Form.cs | 10 ++-- .../WinformsControlsTest/MainForm.Designer.cs | 17 ++++++- .../WinformsControlsTest/MainForm.cs | 5 ++ .../System/Windows/Forms/FormTests.cs | 50 +++++++++++++++++++ 4 files changed, 73 insertions(+), 9 deletions(-) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Form.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Form.cs index 007229345e2..355c4c2fceb 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Form.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Form.cs @@ -1928,7 +1928,7 @@ public bool ShowIcon set { formStateEx[FormStateExShowIcon] = value ? 1 : 0; - if (value) + if (!value) { UpdateStyles(); } @@ -4250,7 +4250,7 @@ protected override void OnResize(EventArgs e) /// [Browsable(true)] [EditorBrowsable(EditorBrowsableState.Always)] - [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] protected virtual void OnDpiChanged(DpiChangedEventArgs e) { if (e.DeviceDpiNew != e.DeviceDpiOld) @@ -5867,11 +5867,7 @@ private unsafe void UpdateWindowIcon(bool redrawFrame) if (redrawFrame) { - User32.RedrawWindow( - new HandleRef(this, Handle), - null, - IntPtr.Zero, - User32.RDW.INVALIDATE | User32.RDW.FRAME); + User32.RedrawWindow(new HandleRef(this, Handle), null, IntPtr.Zero, User32.RDW.INVALIDATE | User32.RDW.FRAME); } } } diff --git a/src/System.Windows.Forms/tests/IntegrationTests/WinformsControlsTest/MainForm.Designer.cs b/src/System.Windows.Forms/tests/IntegrationTests/WinformsControlsTest/MainForm.Designer.cs index 3b8354de49d..12a9527fe53 100644 --- a/src/System.Windows.Forms/tests/IntegrationTests/WinformsControlsTest/MainForm.Designer.cs +++ b/src/System.Windows.Forms/tests/IntegrationTests/WinformsControlsTest/MainForm.Designer.cs @@ -32,6 +32,7 @@ protected override void Dispose(bool disposing) /// private void InitializeComponent() { + this.toggleIconButton = new System.Windows.Forms.Button(); this.buttonsButton = new System.Windows.Forms.Button(); this.calendar = new System.Windows.Forms.Button(); this.treeViewButton = new System.Windows.Forms.Button(); @@ -58,6 +59,16 @@ private void InitializeComponent() this.flowLayoutPanelUITypeEditors.SuspendLayout(); this.SuspendLayout(); // + // toggleIconButton + // + this.toggleIconButton.Location = new System.Drawing.Point(3, 3); + this.toggleIconButton.Name = "toggleIconButton"; + this.toggleIconButton.Size = new System.Drawing.Size(259, 23); + this.toggleIconButton.TabIndex = 0; + this.toggleIconButton.Text = "Toggle form icon"; + this.toggleIconButton.UseVisualStyleBackColor = true; + this.toggleIconButton.Click += new System.EventHandler(this.toggleIconButton_Click); + // // buttonsButton // this.buttonsButton.Location = new System.Drawing.Point(3, 3); @@ -230,6 +241,7 @@ private void InitializeComponent() // // flowLayoutPanelUITypeEditors // + this.flowLayoutPanelUITypeEditors.Controls.Add(this.toggleIconButton); this.flowLayoutPanelUITypeEditors.Controls.Add(this.buttonsButton); this.flowLayoutPanelUITypeEditors.Controls.Add(this.calendar); this.flowLayoutPanelUITypeEditors.Controls.Add(this.multipleControls); @@ -313,9 +325,9 @@ private void InitializeComponent() // this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; - this.ClientSize = new System.Drawing.Size(554, 335); + this.ClientSize = new System.Drawing.Size(554, 400); this.Controls.Add(this.flowLayoutPanelUITypeEditors); - this.MinimumSize = new System.Drawing.Size(570, 330); + this.MinimumSize = new System.Drawing.Size(570, 400); this.Name = "MainForm"; this.Padding = new System.Windows.Forms.Padding(8); this.Text = "MenuForm"; @@ -326,6 +338,7 @@ private void InitializeComponent() #endregion + private System.Windows.Forms.Button toggleIconButton; private System.Windows.Forms.Button buttonsButton; private System.Windows.Forms.Button calendar; private System.Windows.Forms.Button treeViewButton; diff --git a/src/System.Windows.Forms/tests/IntegrationTests/WinformsControlsTest/MainForm.cs b/src/System.Windows.Forms/tests/IntegrationTests/WinformsControlsTest/MainForm.cs index 54af625fe31..f349811e85e 100644 --- a/src/System.Windows.Forms/tests/IntegrationTests/WinformsControlsTest/MainForm.cs +++ b/src/System.Windows.Forms/tests/IntegrationTests/WinformsControlsTest/MainForm.cs @@ -21,6 +21,11 @@ public MainForm() flowLayoutPanelUITypeEditors.PerformLayout(); } + private void toggleIconButton_Click(object sender, EventArgs e) + { + this.ShowIcon = !this.ShowIcon; + } + private void button1_Click(object sender, EventArgs e) { new Buttons().Show(); diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/FormTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/FormTests.cs index 70d26c9fe5c..539adcd2e16 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/FormTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/FormTests.cs @@ -8,6 +8,7 @@ using Moq; using Xunit; using WinForms.Common.Tests; +using static Interop; namespace System.Windows.Forms.Tests { @@ -31,6 +32,23 @@ public void Form_Ctor_Default() Assert.False(form.Visible); } + [WinFormsFact] + public static void Form_Ctor_show_icon_by_default() + { + using var form = new Form(); + Assert.True(form.Handle != IntPtr.Zero); + + IntPtr hSmallIcon = User32.SendMessageW(form, User32.WM.GETICON, (IntPtr)User32.ICON.SMALL, IntPtr.Zero); + Assert.True(hSmallIcon != IntPtr.Zero); + + IntPtr hLargeIcon = User32.SendMessageW(form, User32.WM.GETICON, (IntPtr)User32.ICON.BIG, IntPtr.Zero); + Assert.True(hLargeIcon != IntPtr.Zero); + + // normal form doesn't have WS_EX.DLGMODALFRAME set, and show icon + User32.WS_EX extendedStyle = unchecked((User32.WS_EX)(long)User32.GetWindowLong(form, User32.GWL.EXSTYLE)); + Assert.False(extendedStyle.HasFlag(User32.WS_EX.DLGMODALFRAME)); + } + [WinFormsFact] public void Form_AcceptButtonGetSet() { @@ -798,6 +816,38 @@ public void Form_Parent_SetWithNonNullOldParent_GetReturnsExpected(Control value Assert.False(control.IsHandleCreated); } + [WinFormsTheory] + [InlineData(false, true)] + [InlineData(true, false)] + public static void ShowIcon_renders_icon_correctly(bool showIcon, bool expectedIconNull) + { + using var form = new Form(); + Assert.True(form.Handle != IntPtr.Zero); + + form.ShowIcon = showIcon; + + IntPtr hSmallIcon = User32.SendMessageW(form, User32.WM.GETICON, (IntPtr)User32.ICON.SMALL, IntPtr.Zero); + IntPtr hLargeIcon = User32.SendMessageW(form, User32.WM.GETICON, (IntPtr)User32.ICON.BIG, IntPtr.Zero); + Assert.Equal(expectedIconNull, hSmallIcon == IntPtr.Zero); + Assert.Equal(expectedIconNull, hLargeIcon == IntPtr.Zero); + } + + [WinFormsFact] + public static void ShowIcon_false_should_render_no_icon() + { + using var form = new Form(); + Assert.True(form.Handle != IntPtr.Zero); + + User32.WS_EX extendedStyle = unchecked((User32.WS_EX)(long)User32.GetWindowLong(form, User32.GWL.EXSTYLE)); + Assert.False(extendedStyle.HasFlag(User32.WS_EX.DLGMODALFRAME)); + + form.ShowIcon = false; + + // hiding icon sets WS_EX.DLGMODALFRAME + extendedStyle = unchecked((User32.WS_EX)(long)User32.GetWindowLong(form, User32.GWL.EXSTYLE)); + Assert.True(extendedStyle.HasFlag(User32.WS_EX.DLGMODALFRAME)); + } + public static IEnumerable Parent_SetMdiChild_TestData() { yield return new object[] { null };