diff --git a/SafeExamBrowser.Core.UnitTests/Behaviour/ShutdownControllerTests.cs b/SafeExamBrowser.Core.UnitTests/Behaviour/ShutdownControllerTests.cs index a30f9e8b..12bb66ee 100644 --- a/SafeExamBrowser.Core.UnitTests/Behaviour/ShutdownControllerTests.cs +++ b/SafeExamBrowser.Core.UnitTests/Behaviour/ShutdownControllerTests.cs @@ -6,6 +6,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +using System; using System.Collections.Generic; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; @@ -84,6 +85,29 @@ namespace SafeExamBrowser.Core.UnitTests.Behaviour Assert.IsTrue(c == 3); } + [TestMethod] + public void MustContinueToRevertOperationsInCaseOfError() + { + var operationA = new Mock(); + var operationB = new Mock(); + var operationC = new Mock(); + var operations = new Queue(); + + operationA.Setup(o => o.Revert()).Throws(); + operationB.Setup(o => o.Revert()).Throws(); + operationC.Setup(o => o.Revert()).Throws(); + + operations.Enqueue(operationA.Object); + operations.Enqueue(operationB.Object); + operations.Enqueue(operationC.Object); + + sut.FinalizeApplication(operations); + + operationA.Verify(o => o.Revert(), Times.Once); + operationB.Verify(o => o.Revert(), Times.Once); + operationC.Verify(o => o.Revert(), Times.Once); + } + [TestMethod] public void MustNotFailWithEmptyQueue() { diff --git a/SafeExamBrowser.Core.UnitTests/Behaviour/StartupControllerTests.cs b/SafeExamBrowser.Core.UnitTests/Behaviour/StartupControllerTests.cs index 2454f00d..1bf7e7b7 100644 --- a/SafeExamBrowser.Core.UnitTests/Behaviour/StartupControllerTests.cs +++ b/SafeExamBrowser.Core.UnitTests/Behaviour/StartupControllerTests.cs @@ -149,6 +149,33 @@ namespace SafeExamBrowser.Core.UnitTests.Behaviour Assert.IsTrue(a == 3); } + [TestMethod] + public void MustContinueToRevertOperationsInCaseOfError() + { + var operationA = new Mock(); + var operationB = new Mock(); + var operationC = new Mock(); + var operations = new Queue(); + + operationC.Setup(o => o.Perform()).Throws(); + operationC.Setup(o => o.Revert()).Throws(); + operationB.Setup(o => o.Revert()).Throws(); + operationA.Setup(o => o.Revert()).Throws(); + + operations.Enqueue(operationA.Object); + operations.Enqueue(operationB.Object); + operations.Enqueue(operationC.Object); + + var result = sut.TryInitializeApplication(operations); + + operationA.Verify(o => o.Perform(), Times.Once); + operationA.Verify(o => o.Revert(), Times.Once); + operationB.Verify(o => o.Perform(), Times.Once); + operationB.Verify(o => o.Revert(), Times.Once); + operationC.Verify(o => o.Perform(), Times.Once); + operationC.Verify(o => o.Revert(), Times.Once); + } + [TestMethod] public void MustSucceedWithEmptyQueue() { diff --git a/SafeExamBrowser.Core/Behaviour/ShutdownController.cs b/SafeExamBrowser.Core/Behaviour/ShutdownController.cs index 8a3881b5..1df95664 100644 --- a/SafeExamBrowser.Core/Behaviour/ShutdownController.cs +++ b/SafeExamBrowser.Core/Behaviour/ShutdownController.cs @@ -52,7 +52,15 @@ namespace SafeExamBrowser.Core.Behaviour foreach (var operation in operations) { operation.SplashScreen = splashScreen; - operation.Revert(); + + try + { + operation.Revert(); + } + catch (Exception e) + { + logger.Error($"Failed to revert operation '{operation.GetType().Name}'!", e); + } } } diff --git a/SafeExamBrowser.Core/Behaviour/StartupController.cs b/SafeExamBrowser.Core/Behaviour/StartupController.cs index f71015e3..c2e9de12 100644 --- a/SafeExamBrowser.Core/Behaviour/StartupController.cs +++ b/SafeExamBrowser.Core/Behaviour/StartupController.cs @@ -77,7 +77,15 @@ namespace SafeExamBrowser.Core.Behaviour { var operation = stack.Pop(); - operation.Revert(); + try + { + operation.Revert(); + } + catch (Exception e) + { + logger.Error($"Failed to revert operation '{operation.GetType().Name}'!", e); + } + splashScreen.Regress(); } } diff --git a/SafeExamBrowser.sln b/SafeExamBrowser.sln index a6117992..b3db513a 100644 --- a/SafeExamBrowser.sln +++ b/SafeExamBrowser.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26730.8 +VisualStudioVersion = 15.0.26730.16 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SafeExamBrowser", "SafeExamBrowser\SafeExamBrowser.csproj", "{E3AED2F8-B5DF-45D1-AC19-48066923D6D8}" EndProject @@ -10,7 +10,6 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{0A9E6674-2FB4-42EA-85DE-B2445B9AE2D9}" ProjectSection(SolutionItems) = preProject LICENSE.txt = LICENSE.txt - README.md = README.md EndProjectSection EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SafeExamBrowser.UserInterface.Windows10", "SafeExamBrowser.UserInterface.Windows10\SafeExamBrowser.UserInterface.Windows10.csproj", "{E1BE031A-4354-41E7-83E8-843DED4489FF}" diff --git a/SafeExamBrowser/App.cs b/SafeExamBrowser/App.cs index 94fdce27..69a309ed 100644 --- a/SafeExamBrowser/App.cs +++ b/SafeExamBrowser/App.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Threading; using System.Windows; @@ -65,7 +66,7 @@ namespace SafeExamBrowser if (success) { MainWindow = instances.Taskbar; - MainWindow.Closing += (o, args) => ShutdownApplication(); + MainWindow.Closing += MainWindow_Closing; MainWindow.Show(); } else @@ -74,7 +75,7 @@ namespace SafeExamBrowser } } - private void ShutdownApplication() + private void MainWindow_Closing(object sender, CancelEventArgs e) { var operations = new Queue(instances.StartupOperations.Reverse());