Commits

Christian Specht committed 22cf43d Draft

extract shutdown to ShutdownService, add test

  • Participants
  • Parent commits 8b5c0e7

Comments (0)

Files changed (7)

File src/MissileSharp.Launcher/MissileSharp.Launcher.csproj

     <Compile Include="Services\ConfigService.cs" />
     <Compile Include="Services\IConfigService.cs" />
     <Compile Include="Services\IMessageService.cs" />
+    <Compile Include="Services\IShutdownService.cs" />
     <Compile Include="Services\MessageService.cs" />
+    <Compile Include="Services\ShutdownService.cs" />
     <Compile Include="ViewModels\MainWindowViewModel.cs" />
     <Compile Include="Views\MainWindow.xaml.cs">
       <DependentUpon>MainWindow.xaml</DependentUpon>

File src/MissileSharp.Launcher/Services/IShutdownService.cs

+
+namespace MissileSharp.Launcher.Services
+{
+    /// <summary>
+    /// helper service to shut down the app
+    /// </summary>
+    public interface IShutdownService
+    {
+        void Shutdown();
+    }
+}

File src/MissileSharp.Launcher/Services/ShutdownService.cs

+using System.Windows;
+
+namespace MissileSharp.Launcher.Services
+{
+    /// <summary>
+    /// helper service to shut down the app
+    /// </summary>
+    public class ShutdownService : IShutdownService
+    {
+        public void Shutdown()
+        {
+            Application.Current.Shutdown();
+        }
+    }
+}

File src/MissileSharp.Launcher/ViewModels/MainWindowViewModel.cs

         private CommandCenter model;
         private IConfigService configService;
         private IMessageService messageService;
+        private IShutdownService shutdownService;
 
         public ObservableCollection<string> CommandSets { get; set; }
 
 
         public event PropertyChangedEventHandler PropertyChanged;
 
-        public MainWindowViewModel(IConfigService configService, IMessageService messageService)
+        public MainWindowViewModel(IConfigService configService, IMessageService messageService, IShutdownService shutdownService)
         {
             this.configService = configService;
             this.messageService = messageService;
+            this.shutdownService = shutdownService;
 
-            var launcher = LauncherModelFactory.GetLauncher(configService.LauncherName, configService.LauncherAssembly);
+            var launcher = LauncherModelFactory.GetLauncher(this.configService.LauncherName, this.configService.LauncherAssembly);
 
             this.model = new CommandCenter(launcher);
 
 
             try
             {
-                model.LoadCommandSets(configService.GetConfig());
+                this.model.LoadCommandSets(this.configService.GetConfig());
             }
             catch (Exception ex)
             {
-                messageService.ShowMessage(string.Format(Resources.ConfigFileError, Environment.NewLine, ex.Message));
-                Application.Current.Shutdown();
+                this.messageService.ShowMessage(string.Format(Resources.ConfigFileError, Environment.NewLine, ex.Message));
+                this.shutdownService.Shutdown();
                 return;
             }
 
-            this.CommandSets = new ObservableCollection<string>(model.GetLoadedCommandSetNames());
+            this.CommandSets = new ObservableCollection<string>(this.model.GetLoadedCommandSetNames());
         }
 
         private void FireMissile(Object obj)

File src/MissileSharp.Tests/Launcher/MainWindowViewModelTests.cs

     public class MainWindowViewModelTests
     {
         private MainWindowViewModel viewmodel;
+        private MockShutdownService shutdownservice;
         
         [SetUp]
         public void Setup()
         {
-            viewmodel = new MainWindowViewModel(GetConfigService(), new StubMessageService());
+            shutdownservice = new MockShutdownService();
+            viewmodel = new MainWindowViewModel(GetConfigService(), new StubMessageService(), shutdownservice);
         }
 
         public IConfigService GetConfigService(string[] config = null)
         public void Constructor_ConfigIsEmpty_ThrowsException()
         {
             var config = new StubConfigService();
-            Assert.Throws<ArgumentNullException>(() => new MainWindowViewModel(config, new StubMessageService()));
+            Assert.Throws<ArgumentNullException>(() => new MainWindowViewModel(config, new StubMessageService(), shutdownservice));
+        }
+
+        [Test]
+        public void Constructor_LoadCommandSetsThrowsException_AppShutsDown()
+        {
+            // make model.LoadCommandSets throw by passing invalid config
+            var config = GetConfigService(new string[] { "invalid" });
+            new MainWindowViewModel(config, new StubMessageService(), shutdownservice);
+
+            Assert.True(shutdownservice.ShutDownWasCalled);
         }
     }
 }

File src/MissileSharp.Tests/Launcher/MockShutdownService.cs

+using MissileSharp.Launcher.Services;
+
+namespace MissileSharp.Tests.Launcher
+{
+    public class MockShutdownService : IShutdownService
+    {
+        public bool ShutDownWasCalled { get; private set; }
+
+        public void Shutdown()
+        {
+            this.ShutDownWasCalled = true;
+        }
+    }
+}

File src/MissileSharp.Tests/MissileSharp.Tests.csproj

     <Compile Include="..\MissileSharp\AssemblyVersion.cs">
       <Link>AssemblyVersion.cs</Link>
     </Compile>
+    <Compile Include="Launcher\MockShutdownService.cs" />
     <Compile Include="Launcher\StubConfigService.cs" />
     <Compile Include="Launcher\StubMessageService.cs" />
     <Compile Include="Library\CommandCenterTests.cs" />