diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripSplitButtonTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripSplitButtonTests.cs index 9c681053735..df1b54e8d89 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripSplitButtonTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripSplitButtonTests.cs @@ -1,72 +1,436 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#nullable enable + +using System.Drawing; + namespace System.Windows.Forms.Tests; -public class ToolStripSplitButtonTests +public class ToolStripSplitButtonTests : IDisposable { - public static IEnumerable ToolStripItem_Set_TestData() + private readonly ToolStripSplitButton _toolStripSplitButton; + + public ToolStripSplitButtonTests() { - yield return new object[] { null }; - yield return new object[] { new SubToolStripItem() }; + _toolStripSplitButton = new(); } + public void Dispose() => _toolStripSplitButton.Dispose(); + + public static TheoryData ToolStripItem_Set_TestData() => new() + { + null, + new SubToolStripItem() + }; + [WinFormsTheory] [MemberData(nameof(ToolStripItem_Set_TestData))] - public void ToolStripSplitButton_DefaultItem_Set_GetReturnsExpected(ToolStripItem value) + public void ToolStripSplitButton_DefaultItem_Set_GetReturnsExpected(ToolStripItem? value) { - using ToolStripSplitButton button = new() - { - DefaultItem = value - }; - Assert.Same(value, button.DefaultItem); - - // Set same. - button.DefaultItem = value; - Assert.Same(value, button.DefaultItem); + _toolStripSplitButton.DefaultItem = value; + _toolStripSplitButton.DefaultItem.Should().Be(value); } [WinFormsFact] public void ToolStripSplitButton_DefaultItem_SetWithHandler_CallsOnDefaultItemChanged() { - using ToolStripSplitButton button = new(); - int callCount = 0; EventHandler handler = (sender, e) => { - Assert.Same(button, sender); - Assert.Same(EventArgs.Empty, e); + sender.Should().Be(_toolStripSplitButton); + e.Should().Be(EventArgs.Empty); callCount++; }; - button.DefaultItemChanged += handler; + _toolStripSplitButton.DefaultItemChanged += handler; // Set non-null. using SubToolStripItem item1 = new(); - button.DefaultItem = item1; - Assert.Same(item1, button.DefaultItem); - Assert.Equal(1, callCount); + _toolStripSplitButton.DefaultItem = item1; + _toolStripSplitButton.DefaultItem.Should().Be(item1); + callCount.Should().Be(1); // Set same. - button.DefaultItem = item1; - Assert.Same(item1, button.DefaultItem); - Assert.Equal(1, callCount); + _toolStripSplitButton.DefaultItem = item1; + _toolStripSplitButton.DefaultItem.Should().Be(item1); + callCount.Should().Be(1); // Set different. using SubToolStripItem item2 = new(); - button.DefaultItem = item2; - Assert.Same(item2, button.DefaultItem); - Assert.Equal(2, callCount); + _toolStripSplitButton.DefaultItem = item2; + _toolStripSplitButton.DefaultItem.Should().Be(item2); + callCount.Should().Be(2); // Set null. - button.DefaultItem = null; - Assert.Null(button.DefaultItem); - Assert.Equal(3, callCount); + _toolStripSplitButton.DefaultItem = null; + _toolStripSplitButton.DefaultItem.Should().BeNull(); + callCount.Should().Be(3); // Remove handler. - button.DefaultItemChanged -= handler; - button.DefaultItem = item1; - Assert.Equal(item1, button.DefaultItem); - Assert.Equal(3, callCount); + _toolStripSplitButton.DefaultItemChanged -= handler; + _toolStripSplitButton.DefaultItem = item1; + _toolStripSplitButton.DefaultItem.Should().Be(item1); + callCount.Should().Be(3); + } + + [WinFormsFact] + public void ToolStripSplitButton_Ctor_Default() + { + using ToolStripSplitButton toolStripSplitButton = new(); + + _toolStripSplitButton.Text.Should().BeEmpty(); + _toolStripSplitButton.Image.Should().BeNull(); + toolStripSplitButton.DropDownButtonWidth.Should().Be(toolStripSplitButton.TestAccessor().Dynamic.DefaultDropDownButtonWidth); + } + + [WinFormsFact] + public void ToolStripSplitButton_Ctor_String() + { + using ToolStripSplitButton toolStripSplitButton = new("Test"); + + toolStripSplitButton.Text.Should().Be("Test"); + _toolStripSplitButton.Image.Should().BeNull(); + toolStripSplitButton.DropDownButtonWidth.Should().Be(toolStripSplitButton.TestAccessor().Dynamic.DefaultDropDownButtonWidth); + } + + [WinFormsFact] + public void ToolStripSplitButton_Ctor_Image() + { + using Bitmap image = new(10, 10); + using ToolStripSplitButton toolStripSplitButton = new(image); + + toolStripSplitButton.Text.Should().BeNull(); + toolStripSplitButton.Image.Should().Be(image); + toolStripSplitButton.DropDownButtonWidth.Should().Be(toolStripSplitButton.TestAccessor().Dynamic.DefaultDropDownButtonWidth); + } + + [WinFormsFact] + public void ToolStripSplitButton_Ctor_String_Image() + { + using Bitmap image = new(10, 10); + using ToolStripSplitButton toolStripSplitButton = new("Test", image); + + toolStripSplitButton.Text.Should().Be("Test"); + toolStripSplitButton.Image.Should().Be(image); + toolStripSplitButton.DropDownButtonWidth.Should().Be(toolStripSplitButton.TestAccessor().Dynamic.DefaultDropDownButtonWidth); + } + + [WinFormsFact] + public void ToolStripSplitButton_Ctor_String_Image_EventHandler() + { + bool clickInvoked = false; + EventHandler onClick = (sender, e) => clickInvoked = true; + + using Bitmap image = new(10, 10); + using ToolStripSplitButton toolStripSplitButton = new("Test", image, onClick); + + toolStripSplitButton.Text.Should().Be("Test"); + toolStripSplitButton.Image.Should().Be(image); + toolStripSplitButton.DropDownButtonWidth.Should().Be(toolStripSplitButton.TestAccessor().Dynamic.DefaultDropDownButtonWidth); + + toolStripSplitButton.PerformClick(); + clickInvoked.Should().BeTrue(); + } + + [WinFormsFact] + public void ToolStripSplitButton_Ctor_String_Image_EventHandler_Name() + { + bool clickInvoked = false; + EventHandler onClick = (sender, e) => clickInvoked = true; + + using Bitmap image = new(10, 10); + using ToolStripSplitButton toolStripSplitButton = new("Test", image, onClick, "TestButton"); + + toolStripSplitButton.Text.Should().Be("Test"); + toolStripSplitButton.Image.Should().Be(image); + toolStripSplitButton.Name.Should().Be("TestButton"); + toolStripSplitButton.DropDownButtonWidth.Should().Be(toolStripSplitButton.TestAccessor().Dynamic.DefaultDropDownButtonWidth); + + toolStripSplitButton.PerformClick(); + clickInvoked.Should().BeTrue(); + } + + [WinFormsFact] + public void ToolStripSplitButton_Ctor_String_Image_DropDownItems() + { + using Bitmap image = new(10, 10); + using ToolStripMenuItem item1 = new("Item1"); + using ToolStripMenuItem item2 = new("Item2"); + + using ToolStripSplitButton toolStripSplitButton = new("Test", image, item1, item2); + + toolStripSplitButton.Text.Should().Be("Test"); + toolStripSplitButton.Image.Should().Be(image); + toolStripSplitButton.DropDownItems.Cast().Should().ContainInOrder(new[] { item1, item2 }); + toolStripSplitButton.DropDownButtonWidth.Should().Be(toolStripSplitButton.TestAccessor().Dynamic.DefaultDropDownButtonWidth); + } + + [WinFormsFact] + public void ToolStripSplitButton_AutoToolTip_DefaultValue() + { + _toolStripSplitButton.AutoToolTip.Should().BeTrue(); + } + + [WinFormsTheory] + [BoolData] + public void ToolStripSplitButton_AutoToolTip_Set_GetReturnsExpected(bool value) + { + _toolStripSplitButton.AutoToolTip.Should().BeTrue(); + + _toolStripSplitButton.AutoToolTip = value; + _toolStripSplitButton.AutoToolTip.Should().Be(value); + + _toolStripSplitButton.AutoToolTip = !value; + _toolStripSplitButton.AutoToolTip.Should().Be(!value); + } + + [WinFormsFact] + public void ToolStripSplitButton_ButtonBounds_ReturnsExpected() + { + _toolStripSplitButton.ButtonBounds.Should().Be(_toolStripSplitButton.TestAccessor().Dynamic.SplitButtonButton.Bounds); + } + + [WinFormsFact] + public void ToolStripSplitButton_ButtonPressed_ReturnsExpected() + { + _toolStripSplitButton.ButtonPressed.Should().Be(_toolStripSplitButton.TestAccessor().Dynamic.SplitButtonButton.Pressed); + } + + [WinFormsFact] + public void ToolStripSplitButton_ButtonSelected_ReturnsExpected() + { + _toolStripSplitButton.ButtonSelected.Should().Be(_toolStripSplitButton.TestAccessor().Dynamic.SplitButtonButton.Selected); + } + + [WinFormsFact] + public void ToolStripSplitButton_ButtonClick_EventTriggered() + { + bool eventTriggered = false; + + EventHandler handler = (sender, e) => + { + eventTriggered = true; + }; + + _toolStripSplitButton.ButtonClick += handler; + + _toolStripSplitButton.PerformButtonClick(); + + eventTriggered.Should().BeTrue(); + } + + [WinFormsFact] + public void ToolStripSplitButton_ButtonClick_EventHandlerCalledExpectedTimes() + { + int callCount = 0; + + EventHandler handler = (sender, e) => + { + callCount++; + }; + + _toolStripSplitButton.ButtonClick += handler; + + _toolStripSplitButton.PerformButtonClick(); + _toolStripSplitButton.PerformButtonClick(); + + callCount.Should().Be(2); + } + + [WinFormsFact] + public void ToolStripSplitButton_ButtonClick_EventHandlerRemoved() + { + int callCount = 0; + + EventHandler handler = (sender, e) => + { + callCount++; + }; + + _toolStripSplitButton.ButtonClick += handler; + _toolStripSplitButton.PerformButtonClick(); + _toolStripSplitButton.ButtonClick -= handler; + _toolStripSplitButton.PerformButtonClick(); + + callCount.Should().Be(1); + } + + [WinFormsFact] + public void ToolStripSplitButton_ButtonDoubleClick_EventTriggered() + { + bool eventTriggered = false; + _toolStripSplitButton.ButtonDoubleClick += (sender, e) => eventTriggered = true; + + _toolStripSplitButton.OnButtonDoubleClick(EventArgs.Empty); + + eventTriggered.Should().BeTrue(); + } + + [WinFormsFact] + public void ToolStripSplitButton_ButtonDoubleClick_EventHandlerCalledExpectedTimes() + { + int callCount = 0; + _toolStripSplitButton.ButtonDoubleClick += (sender, e) => callCount++; + + _toolStripSplitButton.OnButtonDoubleClick(EventArgs.Empty); + _toolStripSplitButton.OnButtonDoubleClick(EventArgs.Empty); + + callCount.Should().Be(2); + } + + [WinFormsFact] + public void ToolStripSplitButton_ButtonDoubleClick_EventHandlerRemoved() + { + int callCount = 0; + EventHandler handler = (sender, e) => callCount++; + _toolStripSplitButton.ButtonDoubleClick += handler; + _toolStripSplitButton.ButtonDoubleClick -= handler; + + _toolStripSplitButton.OnButtonDoubleClick(EventArgs.Empty); + + callCount.Should().Be(0); + } + + [WinFormsFact] + public void ToolStripSplitButton_DropDownButtonArea_ReturnsExpected() + { + _toolStripSplitButton.DropDownButtonArea.Should().Be( _toolStripSplitButton.DropDownButtonBounds); + } + + [WinFormsFact] + public void ToolStripSplitButton_DropDownButtonSelected_DefaultValue() + { + _toolStripSplitButton.DropDownButtonSelected.Should().BeFalse(); + } + + [WinFormsFact] + public void ToolStripSplitButton_DropDownButtonSelected_Selected() + { + _toolStripSplitButton.Select(); + _toolStripSplitButton.DropDownButtonSelected.Should().BeTrue(); + } + + [WinFormsTheory] + [InlineData(0)] + [InlineData(5)] + [InlineData(10)] + public void ToolStripSplitButton_DropDownButtonWidth_Set_GetReturnsExpected(int value) + { + _toolStripSplitButton.DropDownButtonWidth = value; + _toolStripSplitButton.DropDownButtonWidth.Should().Be(value); + } + + [WinFormsFact] + public void ToolStripSplitButton_DropDownButtonWidth_SetInvalidValue_ThrowsArgumentOutOfRangeException() + { + Action action = () => _toolStripSplitButton.DropDownButtonWidth = -1; + action.Should().Throw(); + } + + [WinFormsFact] + public void ToolStripSplitButton_SplitterBounds_DefaultValue() + { + _toolStripSplitButton.SplitterBounds.Should().Be(Rectangle.Empty); + } + + [WinFormsFact] + public void ToolStripSplitButton_OnButtonClick_TriggersButtonClickEvent() + { + bool eventTriggered = false; + _toolStripSplitButton.ButtonClick += (sender, e) => eventTriggered = true; + + _toolStripSplitButton.TestAccessor().Dynamic.OnButtonClick(EventArgs.Empty); + + eventTriggered.Should().BeTrue(); + } + + [WinFormsFact] + public void ToolStripSplitButton_OnButtonClick_RoutesClickToDefaultItem() + { + using ToolStripButton defaultItem = new(); + bool eventTriggered = false; + defaultItem.Click += (sender, e) => eventTriggered = true; + _toolStripSplitButton.DefaultItem = defaultItem; + + _toolStripSplitButton.TestAccessor().Dynamic.OnButtonClick(EventArgs.Empty); + + eventTriggered.Should().BeTrue(); + } + + [WinFormsFact] + public void ToolStripSplitButton_OnButtonDoubleClick_TriggersButtonDoubleClickEvent() + { + bool eventTriggered = false; + _toolStripSplitButton.ButtonDoubleClick += (sender, e) => eventTriggered = true; + + _toolStripSplitButton.OnButtonDoubleClick(EventArgs.Empty); + + eventTriggered.Should().BeTrue(); + } + + [WinFormsFact] + public void ToolStripSplitButton_OnButtonDoubleClick_RoutesDoubleClickToDefaultItem() + { + using ToolStripButton defaultItem = new(); + bool eventTriggered = false; + defaultItem.DoubleClick += (sender, e) => eventTriggered = true; + _toolStripSplitButton.DefaultItem = defaultItem; + + _toolStripSplitButton.OnButtonDoubleClick(EventArgs.Empty); + + eventTriggered.Should().BeTrue(); + } + + [WinFormsFact] + public void ToolStripSplitButton_PerformButtonClick_TriggersButtonClickEvent() + { + bool eventTriggered = false; + _toolStripSplitButton.ButtonClick += (sender, e) => eventTriggered = true; + + _toolStripSplitButton.PerformButtonClick(); + + eventTriggered.Should().BeTrue(); + } + + [WinFormsFact] + public void ToolStripSplitButton_PerformButtonClick_RoutesClickToDefaultItem() + { + using ToolStripButton defaultItem = new(); + bool eventTriggered = false; + defaultItem.Click += (sender, e) => eventTriggered = true; + _toolStripSplitButton.DefaultItem = defaultItem; + + _toolStripSplitButton.PerformButtonClick(); + + eventTriggered.Should().BeTrue(); + } + + [WinFormsTheory] + [InlineData(20)] + [InlineData(11)] + public void ToolStripSplitButton_ResetDropDownButtonWidth_SetsToDefault(int initialWidth) + { + _toolStripSplitButton.DropDownButtonWidth = initialWidth; + + _toolStripSplitButton.ResetDropDownButtonWidth(); + + _toolStripSplitButton.DropDownButtonWidth.Should().Be(_toolStripSplitButton.TestAccessor().Dynamic.DefaultDropDownWidth); + } + + [WinFormsFact] + public void ToolStripSplitButton_ShouldSerializeDropDownButtonWidth_NotDefault_ReturnsTrue() + { + _toolStripSplitButton.DropDownButtonWidth = 20; + + _toolStripSplitButton.ShouldSerializeDropDownButtonWidth().Should().BeTrue(); + } + + [WinFormsFact] + public void ToolStripSplitButton_ShouldSerializeDropDownButtonWidth_Default_ReturnsFalse() + { + _toolStripSplitButton.DropDownButtonWidth = 11; + + _toolStripSplitButton.ShouldSerializeDropDownButtonWidth().Should().BeFalse(); } private class SubToolStripItem : ToolStripItem