From a0dff5107b0a452b166b7cf1c4aada8f37b7b8db Mon Sep 17 00:00:00 2001 From: v-zhgl <38325459+Zheng-Li01@users.noreply.github.com> Date: Thu, 17 Oct 2024 00:53:14 +0800 Subject: [PATCH] Add code coverage for ToolStripItemCollection (#12316) related #10453 Proposed changes Add unit tests for ToolStripItemCollection.cs to test its properties & methods & events --- .../Forms/ToolStripItemCollectionTests.cs | 238 ++++++++++++++++++ 1 file changed, 238 insertions(+) diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripItemCollectionTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripItemCollectionTests.cs index e238241af65..d42cc30396a 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripItemCollectionTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripItemCollectionTests.cs @@ -1,6 +1,10 @@ // 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 ToolStripItemCollectionTests @@ -120,4 +124,238 @@ public void ToolStripItemCollection_AddRange_ToolStripItemCollection_Success() Assert.Equal("b", contextMenuStrip.Items[1].Text); Assert.Equal("c", contextMenuStrip.Items[2].Text); } + + private ToolStripItem[] CreateToolStripItems(params (string Key, string Name)[] items) => + items.Select(item => new ToolStripButton(item.Name) { Name = item.Key }).ToArray(); + + [WinFormsFact] + public void ToolStripItemCollection_Constructor_ToolStripOwnerAndItems_Success() + { + using ToolStrip toolStrip = new(); + ToolStripItem[] items = CreateToolStripItems(("key1", "Item1"), ("key2", "Item2")); + + ToolStripItemCollection collection = new(toolStrip, items); + + collection.Count.Should().Be(2); + collection[0].Text.Should().Be("Item1"); + collection[1].Text.Should().Be("Item2"); + } + + private ToolStripItemCollection CreateToolStripItemCollectionWithItems(params (string Key, string Name)[] items) + { + using ToolStrip toolStrip = new(); + ToolStripItem[] toolStripItems = items.Select(item => new ToolStripButton(item.Name) { Name = item.Key }).ToArray(); + return new ToolStripItemCollection(toolStrip, toolStripItems); + } + + [WinFormsFact] + public void ToolStripItemCollection_Indexer_ValidKey_ReturnsExpected() + { + ToolStripItemCollection collection = CreateToolStripItemCollectionWithItems( + ("key1", "Item1"), + ("key2", "Item2") + ); + + collection["key1"].Should().Be(collection[0]); + collection["key2"].Should().Be(collection[1]); + } + + [WinFormsFact] + public void ToolStripItemCollection_Indexer_InvalidOrNullOrEmptyKey_ReturnsNull() + { + ToolStripItemCollection collection = CreateToolStripItemCollectionWithItems( + ("key1", "Item1"), + ("key2", "Item2") + ); + + collection["invalidKey"].Should().BeNull(); + collection[null].Should().BeNull(); + collection[string.Empty].Should().BeNull(); + } + + [WinFormsFact] + public void ToolStripItemCollection_ContainsKey_ValidKey_ReturnsTrue() + { + ToolStripItemCollection collection = CreateToolStripItemCollectionWithItems( + ("key1", "Item1"), + ("key2", "Item2") + ); + + collection.ContainsKey("key1").Should().BeTrue(); + collection.ContainsKey("key2").Should().BeTrue(); + } + + [WinFormsFact] + public void ToolStripItemCollection_ContainsKey_InvalidOrNullOrEmptyKey_ReturnsFalse() + { + ToolStripItemCollection collection = CreateToolStripItemCollectionWithItems( + ("key1", "Item1"), + ("key2", "Item2") + ); + + collection.ContainsKey("invalidKey").Should().BeFalse(); + collection.ContainsKey(null).Should().BeFalse(); + collection.ContainsKey(string.Empty).Should().BeFalse(); + } + + [WinFormsTheory] + [InlineData("key1", 0)] + [InlineData("key2", 1)] + [InlineData("key3", 2)] + public void ToolStripItemCollection_IndexOfKey_ValidKey_ReturnsExpected(string key, int expectedIndex) + { + ToolStripItemCollection collection = CreateToolStripItemCollectionWithItems( + ("key1", "Item1"), + ("key2", "Item2"), + ("key3", "Item3") + ); + + collection.IndexOfKey(key).Should().Be(expectedIndex); + } + + [WinFormsTheory] + [InlineData("invalidKey", -1)] + [InlineData("key4", -1)] + [InlineData("", -1)] + public void ToolStripItemCollection_IndexOfKey_InvalidOrEmptyKey_ReturnsMinusOne(string key, int expectedIndex) + { + ToolStripItemCollection collection = CreateToolStripItemCollectionWithItems( + ("key1", "Item1"), + ("key2", "Item2"), + ("key3", "Item3") + ); + + collection.IndexOfKey(key).Should().Be(expectedIndex); + } + + [WinFormsFact] + public void ToolStripItemCollection_IsValidIndex_ValidIndex_ReturnsTrue() + { + using ToolStrip toolStrip = new(); + ToolStripItem[] items = CreateToolStripItems(("key1", "Item1"), ("key2", "Item2")); + ToolStripItemCollection collection = new(toolStrip, items); + + dynamic accessor = collection.TestAccessor().Dynamic; + + ((bool)accessor.IsValidIndex(0)).Should().BeTrue(); + ((bool)accessor.IsValidIndex(1)).Should().BeTrue(); + } + + [WinFormsFact] + public void ToolStripItemCollection_IsValidIndex_InvalidIndex_ReturnsFalse() + { + using ToolStrip toolStrip = new(); + ToolStripItem[] items = CreateToolStripItems(("key1", "Item1"), ("key2", "Item2")); + ToolStripItemCollection collection = new(toolStrip, items); + + dynamic accessor = collection.TestAccessor().Dynamic; + + ((bool)accessor.IsValidIndex(-1)).Should().BeFalse(); + ((bool)accessor.IsValidIndex(2)).Should().BeFalse(); + } + + [WinFormsFact] + public void ToolStripItemCollection_RemoveByKey_ValidKey_RemovesItem() + { + using ToolStrip toolStrip = new(); + ToolStripItem[] items = CreateToolStripItems(("key1", "Item1"), ("key2", "Item2")); + ToolStripItemCollection collection = new(toolStrip, items); + + collection.ContainsKey("key1").Should().BeTrue(); + + collection.RemoveByKey("key1"); + + collection.ContainsKey("key1").Should().BeFalse(); + collection.Count.Should().Be(1); + collection[0].Text.Should().Be("Item2"); + } + + [WinFormsFact] + public void ToolStripItemCollection_RemoveByKey_InvalidKey_DoesNothing() + { + ToolStripItemCollection collection = CreateToolStripItemCollectionWithItems( + ("key1", "Item1"), + ("key2", "Item2") + ); + + collection.RemoveByKey("invalidKey"); + + collection.Contains(collection[0]).Should().BeTrue(); + collection.Contains(collection[1]).Should().BeTrue(); + } + + [WinFormsFact] + public void ToolStripItemCollection_MoveItem_ItemWithParent_MovesItem() + { + using ToolStrip toolStrip1 = new(); + using ToolStrip toolStrip2 = new(); + ToolStripItem[] items = CreateToolStripItems(("key1", "Item1"), ("key2", "Item2")); + toolStrip1.Items.AddRange(items); + + ToolStripItemCollection collection = new(toolStrip2, Array.Empty()); + collection.MoveItem(items[0]); + + toolStrip1.Items.Contains(items[0]).Should().BeFalse(); + toolStrip1.Items.Contains(items[1]).Should().BeTrue(); + collection.Contains(items[0]).Should().BeTrue(); + } + + [WinFormsFact] + public void ToolStripItemCollection_MoveItem_Index_ValidItem_MovesItem() + { + using ToolStrip toolStrip = new(); + ToolStripItem[] items = CreateToolStripItems(("key1", "Item1"), ("key2", "Item2"), ("key3", "Item3")); + ToolStripItemCollection collection = new(toolStrip, items); + + collection.MoveItem(index: 1, items[2]); + + collection.IndexOf(items[2]).Should().Be(1); + collection.IndexOf(items[0]).Should().Be(0); + collection.IndexOf(items[1]).Should().Be(2); + } + + [WinFormsFact] + public void ToolStripItemCollection_MoveItem_MovesItemFromToolStripToCollection() + { + using ToolStrip toolStrip1 = new(); + using ToolStrip toolStrip2 = new(); + ToolStripItem[] items = CreateToolStripItems(("key1", "Item1"), ("key2", "Item2")); + toolStrip1.Items.AddRange(items); + + ToolStripItemCollection collection = new(toolStrip2, Array.Empty()); + collection.MoveItem(index: 0, items[0]); + + toolStrip1.Items.Contains(items[0]).Should().BeFalse(); + toolStrip1.Items.Contains(items[1]).Should().BeTrue(); + collection.Contains(items[0]).Should().BeTrue(); + collection.IndexOf(items[0]).Should().Be(0); + } + + [WinFormsFact] + public void ToolStripItemCollection_Add_Image_Success() + { + using ToolStrip toolStrip = new(); + ToolStripItemCollection collection = new(toolStrip, Array.Empty()); + using Bitmap image = new(10, 10); + + ToolStripItem item = collection.Add(image); + + item.Should().NotBeNull(); + item.Image.Should().Be(image); + } + + [WinFormsFact] + public void ToolStripItemCollection_Add_TextAndImage_Success() + { + using ToolStrip toolStrip = new(); + ToolStripItemCollection collection = new(toolStrip, Array.Empty()); + string text = "Test"; + using Bitmap image = new(10, 10); + + ToolStripItem item = collection.Add(text, image); + + item.Should().NotBeNull(); + item.Text.Should().Be(text); + item.Image.Should().Be(image); + } }