From f77848e3d97301f2d386b20cb8853e0e344981d4 Mon Sep 17 00:00:00 2001 From: Ian Dunkerly Date: Thu, 26 Aug 2021 22:59:35 +0100 Subject: [PATCH] Fixing up unit tests --- mRemoteNGTests/ListViewTester.cs | 75 +++++-------------- .../NUnitExtensions/SecureTextBoxTester.cs | 28 ------- mRemoteNGTests/Properties/Resources.resx | 9 ++- mRemoteNGTests/TestHelpers/FormExtensions.cs | 20 +++++ .../UI/Controls/SecureTextBoxTests.cs | 11 +-- .../UI/Controls/TextBoxExtensionsTests.cs | 15 ++-- mRemoteNGTests/UI/Forms/OptionsFormTests.cs | 29 +++---- .../OptionsPages/OptionsAdvancedPageTests.cs | 22 +++--- .../OptionsAppearancePageTests.cs | 15 ++-- .../OptionsConnectionsPageTests.cs | 19 ++--- .../Forms/OptionsPages/OptionsSQLPageTests.cs | 19 ++--- .../OptionsStartupExitPageTests.cs | 15 ++-- .../OptionsPages/OptionsTabsPanelPageTests.cs | 13 ++-- .../OptionsPages/OptionsThemePageTests.cs | 19 ++--- .../OptionsPages/OptionsUpdatesPageTests.cs | 19 +++-- mRemoteNGTests/UI/Forms/PasswordFormTests.cs | 14 ++-- .../ConfigWindowGeneralTests.cs | 4 +- .../ConfigWindowRdpSpecialTests.cs | 4 +- .../ConfigWindowVncSpecialTests.cs | 4 +- .../UI/Window/ConnectionTreeWindowTests.cs | 1 + mRemoteNGTests/mRemoteNGTests.csproj | 13 +++- 21 files changed, 176 insertions(+), 192 deletions(-) delete mode 100644 mRemoteNGTests/NUnitExtensions/SecureTextBoxTester.cs create mode 100644 mRemoteNGTests/TestHelpers/FormExtensions.cs diff --git a/mRemoteNGTests/ListViewTester.cs b/mRemoteNGTests/ListViewTester.cs index dfe01873..e82265a8 100644 --- a/mRemoteNGTests/ListViewTester.cs +++ b/mRemoteNGTests/ListViewTester.cs @@ -32,9 +32,12 @@ //Contributed by: Ian Cooper +using System; using System.Collections; +using System.Linq; +using System.Reflection; using System.Windows.Forms; -using NUnit.Extensions.Forms; +using mRemoteNGTests.TestHelpers; namespace mRemoteNGTests { @@ -44,8 +47,11 @@ namespace mRemoteNGTests /// /// It includes helper methods for selecting items from the list /// and for clearing those selections. - public class ListViewTester : ControlTester + public class ListViewTester { + private readonly string _name; + private readonly Form _form; + /// /// Creates a ControlTester from the control name and the form instance. /// @@ -56,62 +62,11 @@ namespace mRemoteNGTests /// The Control name. /// The Form instance. public ListViewTester(string name, Form form) - : base(name, form) { + _name = name; + _form = form; } - /// - /// Creates a ControlTester from the control name and the form name. - /// - /// - /// It is best to use the overloaded Constructor that requires just the name - /// parameter if possible. - /// - /// The Control name. - /// The Form name.. - public ListViewTester(string name, string formName) - : base(name, formName) - { - } - - /// - /// Creates a ControlTester from the control name. - /// - /// - /// This is the best constructor. - /// The Control name. - public ListViewTester(string name) - : base(name) - { - } - - /// - /// Creates a ControlTester from a ControlTester and an index where the - /// original tester's name is not unique. - /// - /// - /// It is best to use the overloaded Constructor that requires just the name - /// parameter if possible. - /// - /// The ControlTester. - /// The index to test. - public ListViewTester(ControlTester tester, int index) - : base(tester, index) - { - } - - /// - /// Allows you to find a ListViewTester by index where the name is not unique. - /// - /// - /// This was added to support the ability to find controls where their name is - /// not unique. If all of your controls are uniquely named (I recommend this) then - /// you will not need this. - /// - /// The ControlTester at the specified index. - /// The index of the ListViewTester. - public new ListViewTester this[int index] => new ListViewTester(this, index); - /// /// Provides access to all of the Properties of the ListBox. /// @@ -119,7 +74,7 @@ namespace mRemoteNGTests /// Allows typed access to all of the properties of the underlying control. /// /// The underlying control. - public ListView Properties => (ListView)Control; + public ListView Properties => _form.FindControl(_name); /// /// Helper method to return the List View's Items property @@ -149,9 +104,17 @@ namespace mRemoteNGTests public void Select(int i) { Properties.Items[i].Selected = true; + FireEvent("ItemActivate"); } + private void FireEvent(string eventName) + { + var ctrl = Properties; + MethodInfo method = typeof(ListView).GetMethod("On" + eventName, BindingFlags.Instance | BindingFlags.NonPublic); + method.Invoke(ctrl, new object[] { EventArgs.Empty }); + } + /// /// Selects an item in the list according to its string value. /// diff --git a/mRemoteNGTests/NUnitExtensions/SecureTextBoxTester.cs b/mRemoteNGTests/NUnitExtensions/SecureTextBoxTester.cs deleted file mode 100644 index 93e6cf9c..00000000 --- a/mRemoteNGTests/NUnitExtensions/SecureTextBoxTester.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System.Windows.Forms; -using mRemoteNG.UI.Controls; -using NUnit.Extensions.Forms; - - -namespace mRemoteNGTests.NUnitExtensions -{ - public class SecureTextBoxTester : ControlTester - { - public SecureTextBoxTester(string name, string formName) : base(name, formName) - { - } - - public SecureTextBoxTester(string name, Form form) : base(name, form) - { - } - - public SecureTextBoxTester(string name) : base(name) - { - } - - public SecureTextBoxTester(ControlTester tester, int index) : base(tester, index) - { - } - - public SecureTextBox Properties => (SecureTextBox) Control; - } -} \ No newline at end of file diff --git a/mRemoteNGTests/Properties/Resources.resx b/mRemoteNGTests/Properties/Resources.resx index eb403a4a..e35a0465 100644 --- a/mRemoteNGTests/Properties/Resources.resx +++ b/mRemoteNGTests/Properties/Resources.resx @@ -112,12 +112,12 @@ 2.0 - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + System.Resources.ResXResourceReader, System.Windows.Forms, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - + ..\Resources\beta-update.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 @@ -154,6 +154,9 @@ ..\Resources\dev-update-portable.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 + + ..\resources\testimage.bmp;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + ..\Resources\test_puttyConnectionManager_database.dat;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-16 diff --git a/mRemoteNGTests/TestHelpers/FormExtensions.cs b/mRemoteNGTests/TestHelpers/FormExtensions.cs new file mode 100644 index 00000000..b07e5470 --- /dev/null +++ b/mRemoteNGTests/TestHelpers/FormExtensions.cs @@ -0,0 +1,20 @@ +using System.Linq; +using System.Windows.Forms; + +namespace mRemoteNGTests.TestHelpers +{ + public static class FormExtensions + { + /// + /// Finds a control with the specified name on a form. + /// + /// The type of control to find. + /// The form. + /// The name of the control to find. + /// The control or null if not found or the wrong type. + public static T FindControl(this Form form, string name) where T : Control + { + return form.Controls.Find(name, true).SingleOrDefault() as T; + } + } +} \ No newline at end of file diff --git a/mRemoteNGTests/UI/Controls/SecureTextBoxTests.cs b/mRemoteNGTests/UI/Controls/SecureTextBoxTests.cs index b3d5da7d..3a04c7df 100644 --- a/mRemoteNGTests/UI/Controls/SecureTextBoxTests.cs +++ b/mRemoteNGTests/UI/Controls/SecureTextBoxTests.cs @@ -1,9 +1,10 @@ -using mRemoteNG.Security; -using mRemoteNGTests.NUnitExtensions; +using System.Threading; +using mRemoteNG.Security; using NUnit.Framework; namespace mRemoteNGTests.UI.Controls { + [Apartment(ApartmentState.STA)] public class SecureTextBoxTests { private SecureTextBoxTestForm _testForm; @@ -26,10 +27,10 @@ namespace mRemoteNGTests.UI.Controls [Test] public void TextboxInputGetsAddedToSecureString() { - var textBox = new SecureTextBoxTester(_testForm.secureTextBox1.Name); + var textBox = _testForm.secureTextBox1; const string textToSend = "abc123"; - textBox.Properties.Text = textToSend; - Assert.That(textBox.Properties.SecString.ConvertToUnsecureString(), Is.EqualTo(textToSend)); + textBox.Text = textToSend; + Assert.That(textBox.SecString.ConvertToUnsecureString(), Is.EqualTo(textToSend)); } } } \ No newline at end of file diff --git a/mRemoteNGTests/UI/Controls/TextBoxExtensionsTests.cs b/mRemoteNGTests/UI/Controls/TextBoxExtensionsTests.cs index f976a8ef..96ca6fa0 100644 --- a/mRemoteNGTests/UI/Controls/TextBoxExtensionsTests.cs +++ b/mRemoteNGTests/UI/Controls/TextBoxExtensionsTests.cs @@ -1,10 +1,11 @@ -using mRemoteNG.UI; -using NUnit.Extensions.Forms; +using System.Threading; +using mRemoteNG.UI; using NUnit.Framework; namespace mRemoteNGTests.UI.Controls { [TestFixture] + [Apartment(ApartmentState.STA)] public class TextBoxExtensionsTests { private TextBoxExtensionsTestForm _textBoxExtensionsTestForm; @@ -29,17 +30,17 @@ namespace mRemoteNGTests.UI.Controls public void SetCueBannerSetsTheBannerText() { const string text = "Type Here"; - var textBox = new TextBoxTester(_textBoxExtensionsTestForm.textBox1.Name); - Assert.That(textBox.Properties.SetCueBannerText(text), Is.True); + var textBox = _textBoxExtensionsTestForm.textBox1; + Assert.That(textBox.SetCueBannerText(text), Is.True); } [Test] public void GetCueBannerReturnsCorrectValue() { const string text = "Type Here"; - var textBox = new TextBoxTester(_textBoxExtensionsTestForm.textBox1.Name); - textBox.Properties.SetCueBannerText(text); - Assert.That(textBox.Properties.GetCueBannerText(), Is.EqualTo(text)); + var textBox = _textBoxExtensionsTestForm.textBox1; + textBox.SetCueBannerText(text); + Assert.That(textBox.GetCueBannerText(), Is.EqualTo(text)); } } } \ No newline at end of file diff --git a/mRemoteNGTests/UI/Forms/OptionsFormTests.cs b/mRemoteNGTests/UI/Forms/OptionsFormTests.cs index a6052061..8fe5de8f 100644 --- a/mRemoteNGTests/UI/Forms/OptionsFormTests.cs +++ b/mRemoteNGTests/UI/Forms/OptionsFormTests.cs @@ -1,15 +1,12 @@ -using mRemoteNG.App; -using mRemoteNG.Messages; -using mRemoteNG.UI.Window; -using NUnit.Extensions.Forms; -using NUnit.Framework; -using System; +using NUnit.Framework; using System.Threading; -using WeifenLuo.WinFormsUI.Docking; +using System.Windows.Forms; +using mRemoteNGTests.TestHelpers; namespace mRemoteNGTests.UI.Forms { [TestFixture] + [Apartment(ApartmentState.STA)] public class OptionsFormTests : OptionsFormSetupAndTeardown { [Test] @@ -17,26 +14,24 @@ namespace mRemoteNGTests.UI.Forms { bool eventFired = false; _optionsForm.FormClosed += (o, e) => eventFired = true; - ButtonTester cancelButton = new ButtonTester("CancelButtonControl", _optionsForm); - cancelButton.Click(); + Button cancelButton = _optionsForm.FindControl