From fdb32ff5f26345b82f558960ef88ecc952cd3f6a Mon Sep 17 00:00:00 2001 From: dbuechel Date: Wed, 11 Oct 2017 09:18:55 +0200 Subject: [PATCH] SEBWIN-183: Implemented unit tests for notification controllers. --- .../AboutNotificationControllerTests.cs | 79 +++++++++++++++++++ .../LogNotificationControllerTests.cs | 79 +++++++++++++++++++ .../Notifications/NotificationButtonMock.cs | 39 +++++++++ .../SafeExamBrowser.Core.UnitTests.csproj | 3 + 4 files changed, 200 insertions(+) create mode 100644 SafeExamBrowser.Core.UnitTests/Notifications/AboutNotificationControllerTests.cs create mode 100644 SafeExamBrowser.Core.UnitTests/Notifications/LogNotificationControllerTests.cs create mode 100644 SafeExamBrowser.Core.UnitTests/Notifications/NotificationButtonMock.cs diff --git a/SafeExamBrowser.Core.UnitTests/Notifications/AboutNotificationControllerTests.cs b/SafeExamBrowser.Core.UnitTests/Notifications/AboutNotificationControllerTests.cs new file mode 100644 index 00000000..fa26cdf7 --- /dev/null +++ b/SafeExamBrowser.Core.UnitTests/Notifications/AboutNotificationControllerTests.cs @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2017 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using SafeExamBrowser.Contracts.Configuration.Settings; +using SafeExamBrowser.Contracts.I18n; +using SafeExamBrowser.Contracts.UserInterface; +using SafeExamBrowser.Core.Notifications; + +namespace SafeExamBrowser.Core.UnitTests.Notifications +{ + [TestClass] + public class AboutNotificationControllerTests + { + private Mock settingsMock; + private Mock textMock; + private Mock uiFactoryMock; + + [TestInitialize] + public void Initialize() + { + settingsMock = new Mock(); + textMock = new Mock(); + uiFactoryMock = new Mock(); + } + + [TestMethod] + public void MustCloseWindowWhenTerminating() + { + var button = new NotificationButtonMock(); + var window = new Mock(); + var sut = new AboutNotificationController(settingsMock.Object, textMock.Object, uiFactoryMock.Object); + + uiFactoryMock.Setup(u => u.CreateAboutWindow(It.IsAny(), It.IsAny())).Returns(window.Object); + sut.RegisterNotification(button); + button.Click(); + sut.Terminate(); + + window.Verify(w => w.Close()); + } + + [TestMethod] + public void MustOpenOnlyOneWindow() + { + var button = new NotificationButtonMock(); + var window = new Mock(); + var sut = new AboutNotificationController(settingsMock.Object, textMock.Object, uiFactoryMock.Object); + + uiFactoryMock.Setup(u => u.CreateAboutWindow(It.IsAny(), It.IsAny())).Returns(window.Object); + sut.RegisterNotification(button); + button.Click(); + button.Click(); + button.Click(); + button.Click(); + button.Click(); + + uiFactoryMock.Verify(u => u.CreateAboutWindow(It.IsAny(), It.IsAny()), Times.Once); + window.Verify(u => u.Show(), Times.Once); + window.Verify(u => u.BringToForeground(), Times.Exactly(4)); + } + + [TestMethod] + public void MustSubscribeToClickEvent() + { + var button = new NotificationButtonMock(); + var sut = new AboutNotificationController(settingsMock.Object, textMock.Object, uiFactoryMock.Object); + + sut.RegisterNotification(button); + + Assert.IsTrue(button.HasSubscribed); + } + } +} diff --git a/SafeExamBrowser.Core.UnitTests/Notifications/LogNotificationControllerTests.cs b/SafeExamBrowser.Core.UnitTests/Notifications/LogNotificationControllerTests.cs new file mode 100644 index 00000000..3279a541 --- /dev/null +++ b/SafeExamBrowser.Core.UnitTests/Notifications/LogNotificationControllerTests.cs @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2017 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using SafeExamBrowser.Contracts.I18n; +using SafeExamBrowser.Contracts.Logging; +using SafeExamBrowser.Contracts.UserInterface; +using SafeExamBrowser.Core.Notifications; + +namespace SafeExamBrowser.Core.UnitTests.Notifications +{ + [TestClass] + public class LogNotificationControllerTests + { + private Mock loggerMock; + private Mock textMock; + private Mock uiFactoryMock; + + [TestInitialize] + public void Initialize() + { + loggerMock = new Mock(); + textMock = new Mock(); + uiFactoryMock = new Mock(); + } + + [TestMethod] + public void MustCloseWindowWhenTerminating() + { + var button = new NotificationButtonMock(); + var window = new Mock(); + var sut = new LogNotificationController(loggerMock.Object, textMock.Object, uiFactoryMock.Object); + + uiFactoryMock.Setup(u => u.CreateLogWindow(It.IsAny(), It.IsAny())).Returns(window.Object); + sut.RegisterNotification(button); + button.Click(); + sut.Terminate(); + + window.Verify(w => w.Close()); + } + + [TestMethod] + public void MustOpenOnlyOneWindow() + { + var button = new NotificationButtonMock(); + var window = new Mock(); + var sut = new LogNotificationController(loggerMock.Object, textMock.Object, uiFactoryMock.Object); + + uiFactoryMock.Setup(u => u.CreateLogWindow(It.IsAny(), It.IsAny())).Returns(window.Object); + sut.RegisterNotification(button); + button.Click(); + button.Click(); + button.Click(); + button.Click(); + button.Click(); + + uiFactoryMock.Verify(u => u.CreateLogWindow(It.IsAny(), It.IsAny()), Times.Once); + window.Verify(u => u.Show(), Times.Once); + window.Verify(u => u.BringToForeground(), Times.Exactly(4)); + } + + [TestMethod] + public void MustSubscribeToClickEvent() + { + var button = new NotificationButtonMock(); + var sut = new LogNotificationController(loggerMock.Object, textMock.Object, uiFactoryMock.Object); + + sut.RegisterNotification(button); + + Assert.IsTrue(button.HasSubscribed); + } + } +} diff --git a/SafeExamBrowser.Core.UnitTests/Notifications/NotificationButtonMock.cs b/SafeExamBrowser.Core.UnitTests/Notifications/NotificationButtonMock.cs new file mode 100644 index 00000000..b0b13938 --- /dev/null +++ b/SafeExamBrowser.Core.UnitTests/Notifications/NotificationButtonMock.cs @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2017 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using SafeExamBrowser.Contracts.UserInterface.Taskbar; + +namespace SafeExamBrowser.Core.UnitTests.Notifications +{ + class NotificationButtonMock : INotificationButton + { + private NotificationButtonClickedEventHandler clicked; + + public bool HasSubscribed; + public bool HasUnsubscribed; + + public event NotificationButtonClickedEventHandler Clicked + { + add + { + clicked += value; + HasSubscribed = true; + } + remove + { + clicked -= value; + HasUnsubscribed = true; + } + } + + public void Click() + { + clicked?.Invoke(); + } + } +} diff --git a/SafeExamBrowser.Core.UnitTests/SafeExamBrowser.Core.UnitTests.csproj b/SafeExamBrowser.Core.UnitTests/SafeExamBrowser.Core.UnitTests.csproj index 3f26e552..e358bb1b 100644 --- a/SafeExamBrowser.Core.UnitTests/SafeExamBrowser.Core.UnitTests.csproj +++ b/SafeExamBrowser.Core.UnitTests/SafeExamBrowser.Core.UnitTests.csproj @@ -81,6 +81,9 @@ + + +