Skip to content

Commit

Permalink
Improve dropdown menu (#164)
Browse files Browse the repository at this point in the history
* Improve dropdown menu creation.

* Improve unit test description

* Added unit tests for ToolStripMenuItemEx
  • Loading branch information
dannoe committed May 17, 2020
1 parent b4d865e commit 5a35769
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 37 deletions.
4 changes: 2 additions & 2 deletions KeeTrayTOTP.Tests/Menu/LegacyTrayMenuTests.cs
Expand Up @@ -16,7 +16,7 @@ public void LegacyTrayMenuItemProvider_ShouldReturnNull()

var sut = legacyTrayMenuItemProvider.ProvideMenuItem();

sut.Should().BeNull();
sut.Should().BeNull("because we do not provide an official tray menu item in legacy mode.");
}

[TestMethod]
Expand All @@ -30,7 +30,7 @@ public void LegacyTrayMenuItemProvider_ShouldAddItemsDirectlyToMainWindowsTrayCo

var sut = host.Object.MainWindow.TrayContextMenu.Items.Count;

sut.Should().Be(oldItemCount + 2);
sut.Should().Be(oldItemCount + 2, "because we inject two menu items into the official KeePass tray menu");
}
}
}
2 changes: 1 addition & 1 deletion KeeTrayTOTP.Tests/Menu/MenuItemProviderTests.cs
Expand Up @@ -51,7 +51,7 @@ public void MenuItemProvider_ShouldReturnTheCorrectTrayMenuEntries()

trayMenuItem.Should().NotBeNull();
trayMenuItem.HasDropDownItems.Should().BeTrue();
trayMenuItem.DropDownItems.Should().HaveCount(1, "because, there should be a pseudo entry.");
trayMenuItem.DropDownItems.Should().HaveCount(0, "because, the entries are added at opening of the menu.");
}

[TestMethod]
Expand Down
47 changes: 47 additions & 0 deletions KeeTrayTOTP.Tests/Menu/ToolStripMenuItemExTests.cs
@@ -0,0 +1,47 @@
using FluentAssertions;
using KeePass.UI;
using KeeTrayTOTP.Menu;
using KeeTrayTOTP.Tests.Extensions;
using KeeTrayTOTP.Tests.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using KeeTrayTOTP.Helpers;

namespace KeeTrayTOTP.Tests.Menu
{
[TestClass]
public class ToolStripMenuItemExTests
{
[TestMethod]
public void ToolStripMenuItemEx_ShouldReturnHasDropDownItems_EvenIfEmpty()
{
var sut = new ToolStripMenuItemEx("foo", new Bitmap(1,1));
sut.ForceDropDownArrow = true;

sut.HasDropDownItems.Should()
.BeTrue("because we want to display the dropdown arrow even if there are no items present");
}

[TestMethod]
public void ToolStripMenuItemEx_ShouldHaveDefaultBehavior_IfForceDropDownArrowIsNotSet()
{
var sut = new ToolStripMenuItemEx("foo", new Bitmap(1, 1));

sut.HasDropDownItems.Should()
.BeFalse("because it's the default behavior");
}

[TestMethod]
public void ToolStripMenuItemEx_ShouldHaveDefaultBehavior_IfForceDropDownArrowIsNotSet2()
{
var sut = new ToolStripMenuItemEx("foo", new Bitmap(1, 1));
sut.DropDownItems.Add("bar");

sut.HasDropDownItems.Should()
.BeTrue("because it's the default behavior");
}

}
}
21 changes: 13 additions & 8 deletions KeeTrayTOTP/Helpers/ToolStripMenuItemEx.cs
Expand Up @@ -8,14 +8,6 @@ namespace KeeTrayTOTP.Helpers
[DesignerCategory("")]
public class ToolStripMenuItemEx : ToolStripMenuItem
{
public ToolStripMenuItemEx()
{
}

public ToolStripMenuItemEx(string text) : base(text)
{
}

public ToolStripMenuItemEx(string text, Image image) : base(text, image)
{
}
Expand Down Expand Up @@ -45,5 +37,18 @@ protected override Point DropDownLocation
return dropDownLocation;
}
}

/// <summary>
/// Controls whether the drop-down arrow is displayed even if there are no visible drop-down items
/// </summary>
public bool ForceDropDownArrow { get; set; }

public override bool HasDropDownItems
{
get
{
return ForceDropDownArrow || base.HasDropDownItems;
}
}
}
}
28 changes: 2 additions & 26 deletions KeeTrayTOTP/Menu/TrayMenuItemProvider.cs
Expand Up @@ -38,11 +38,9 @@ private void TrayContextMenu_Opened(object sender, EventArgs e)
public virtual ToolStripMenuItem ProvideMenuItem()
{
var rootTrayMenuItem = new ToolStripMenuItemEx(Localization.Strings.TrayTOTPPlugin, Resources.TOTP);

rootTrayMenuItem.DropDownItems.Add(CreatePseudoToolStripMenuItem());
rootTrayMenuItem.ForceDropDownArrow = true;
rootTrayMenuItem.DropDownOpening += OnRootDropDownOpening;
rootTrayMenuItem.DropDownOpening += MenuItemHelper.OnDatabaseDropDownOpening;
rootTrayMenuItem.DropDownClosed += OnDropDownClosed;

return rootTrayMenuItem;
}
Expand Down Expand Up @@ -96,10 +94,9 @@ private ToolStripMenuItem CreateDatabaseMenuItemForDocument(PwDocument document)
{
var documentName = UrlUtil.GetFileName(document.Database.IOConnectionInfo.Path);
mainDropDownItem = new ToolStripMenuItemEx(documentName, ImageExtensions.CreateImageFromColor(document.Database.Color));
mainDropDownItem.ForceDropDownArrow = true;
mainDropDownItem.DropDownOpening += OnDatabaseDropDownOpening;
mainDropDownItem.DropDownOpening += MenuItemHelper.OnDatabaseDropDownOpening;
mainDropDownItem.DropDownClosed += OnDropDownClosed;
mainDropDownItem.DropDownItems.Add(CreatePseudoToolStripMenuItem());
}

mainDropDownItem.Tag = document;
Expand Down Expand Up @@ -188,27 +185,6 @@ private IEnumerable<ToolStripMenuItem> NoTOTPEntriesFoundMenuItem(PwDocument pwD
};
}

/// <summary>
/// This menu item is required to show the dropdown arrow in the tray context menu,
/// even if the menu is still empty. (because we don't fill it until the opening event)
/// </summary>
private static ToolStripMenuItem CreatePseudoToolStripMenuItem()
{
return new ToolStripMenuItem();
}

private void OnDropDownClosed(object sender, EventArgs e)
{
var rootMenuItem = sender as ToolStripMenuItem;
if (rootMenuItem == null)
{
return;
}

rootMenuItem.DropDownItems.Clear();
rootMenuItem.DropDownItems.Add(new ToolStripMenuItem());
}

protected ToolStripMenuItem CreateMenuItemFromPwEntry(PwEntry entry, PwDatabase pwDatabase)
{
var context = new SprContext(entry, pwDatabase, SprCompileFlags.All, false, false);
Expand Down

0 comments on commit 5a35769

Please sign in to comment.