/* * Copyright (c) 2025 ETH Zürich, IT Services * * 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 System; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using SafeExamBrowser.Communication.Contracts.Proxies; using SafeExamBrowser.Configuration.Contracts; using SafeExamBrowser.Core.Contracts.ResponsibilityModel; using SafeExamBrowser.I18n.Contracts; using SafeExamBrowser.Logging.Contracts; using SafeExamBrowser.Runtime.Responsibilities; using SafeExamBrowser.Settings; using SafeExamBrowser.Settings.Service; using SafeExamBrowser.UserInterface.Contracts.MessageBox; using SafeExamBrowser.UserInterface.Contracts.Windows; namespace SafeExamBrowser.Runtime.UnitTests.Responsibilities { [TestClass] public class ServiceResponsibilityTests { private RuntimeContext context; private Mock logger; private Mock messageBox; private Mock> responsibilities; private Mock runtimeWindow; private Mock serviceProxy; private Mock shutdown; private SessionConfiguration currentSession; private AppSettings currentSettings; private ServiceResponsibility sut; [TestInitialize] public void Initialize() { context = new RuntimeContext(); logger = new Mock(); messageBox = new Mock(); responsibilities = new Mock>(); runtimeWindow = new Mock(); serviceProxy = new Mock(); shutdown = new Mock(); currentSession = new SessionConfiguration(); currentSettings = new AppSettings(); currentSession.Settings = currentSettings; context.Current = currentSession; context.Responsibilities = responsibilities.Object; sut = new ServiceResponsibility(logger.Object, messageBox.Object, context, runtimeWindow.Object, serviceProxy.Object, shutdown.Object); } [TestMethod] public void ServiceProxy_MustShutdownWhenConnectionLostAndMandatory() { currentSettings.Service.Policy = ServicePolicy.Mandatory; sut.Assume(RuntimeTask.RegisterSessionEvents); serviceProxy.Raise(c => c.ConnectionLost += null); messageBox.Verify(m => m.Show( It.IsAny(), It.IsAny(), It.IsAny(), It.Is(i => i == MessageBoxIcon.Error), It.IsAny()), Times.Once); responsibilities.Verify(r => r.Delegate(RuntimeTask.StopSession), Times.Once); shutdown.Verify(s => s(), Times.Once); } [TestMethod] public void ServiceProxy_MustNotShutdownWhenConnectionLostAndNotMandatory() { currentSettings.Service.Policy = ServicePolicy.Optional; sut.Assume(RuntimeTask.RegisterSessionEvents); serviceProxy.Raise(c => c.ConnectionLost += null); messageBox.Verify(m => m.Show( It.IsAny(), It.IsAny(), It.IsAny(), It.Is(i => i == MessageBoxIcon.Error), It.IsAny()), Times.Never); responsibilities.Verify(r => r.Delegate(RuntimeTask.StopSession), Times.Never); shutdown.Verify(s => s(), Times.Never); } } }