From 4e22e89d6e4448bbdb2a78740bc4be0132604108 Mon Sep 17 00:00:00 2001 From: Kevin Pilch-Bisson Date: Sat, 15 Aug 2015 20:27:04 -0700 Subject: [PATCH] Load assemblies from command line on startup Also, display a dialog while loading Fixes #10 --- xunit.runner.wpf/LoadingDialog.xaml | 30 +++++++++++ xunit.runner.wpf/LoadingDialog.xaml.cs | 36 +++++++++++++ xunit.runner.wpf/MainWindow.xaml | 8 +++ xunit.runner.wpf/MainWindow.xaml.cs | 4 ++ xunit.runner.wpf/ViewModel/MainViewModel.cs | 58 ++++++++++++++++----- xunit.runner.wpf/xunit.runner.wpf.csproj | 10 ++++ 6 files changed, 134 insertions(+), 12 deletions(-) create mode 100644 xunit.runner.wpf/LoadingDialog.xaml create mode 100644 xunit.runner.wpf/LoadingDialog.xaml.cs diff --git a/xunit.runner.wpf/LoadingDialog.xaml b/xunit.runner.wpf/LoadingDialog.xaml new file mode 100644 index 0000000..6045205 --- /dev/null +++ b/xunit.runner.wpf/LoadingDialog.xaml @@ -0,0 +1,30 @@ + + + + + + + + + + + diff --git a/xunit.runner.wpf/LoadingDialog.xaml.cs b/xunit.runner.wpf/LoadingDialog.xaml.cs new file mode 100644 index 0000000..f01161a --- /dev/null +++ b/xunit.runner.wpf/LoadingDialog.xaml.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Shapes; + +namespace xunit.runner.wpf +{ + /// + /// Interaction logic for LoadingDialog.xaml + /// + public partial class LoadingDialog : Window + { + public LoadingDialog() + { + InitializeComponent(); + } + + public string AssemblyFileName + { + get { return (string)GetValue(AssemblyFileNameProperty); } + set { SetValue(AssemblyFileNameProperty, value); } + } + + public static readonly DependencyProperty AssemblyFileNameProperty = + DependencyProperty.Register(nameof(AssemblyFileName), typeof(string), typeof(LoadingDialog)); + } +} diff --git a/xunit.runner.wpf/MainWindow.xaml b/xunit.runner.wpf/MainWindow.xaml index 50eb805..dce75e3 100644 --- a/xunit.runner.wpf/MainWindow.xaml +++ b/xunit.runner.wpf/MainWindow.xaml @@ -6,6 +6,8 @@ xmlns:local="clr-namespace:xunit.runner.wpf" xmlns:converters="clr-namespace:xunit.runner.wpf.Converters" xmlns:vm="clr-namespace:xunit.runner.wpf.ViewModel" + xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" + xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform" mc:Ignorable="d" DataContext="{Binding Main, Source={StaticResource Locator}}" Title="xUnit.net Test Runner" @@ -14,6 +16,12 @@ Height="600" Width="525"> + + + + + + diff --git a/xunit.runner.wpf/MainWindow.xaml.cs b/xunit.runner.wpf/MainWindow.xaml.cs index 2f08116..7361ef3 100644 --- a/xunit.runner.wpf/MainWindow.xaml.cs +++ b/xunit.runner.wpf/MainWindow.xaml.cs @@ -22,7 +22,11 @@ namespace xunit.runner.wpf { public MainWindow() { + Instance = this; + InitializeComponent(); } + + public static Window Instance { get; private set; } } } diff --git a/xunit.runner.wpf/ViewModel/MainViewModel.cs b/xunit.runner.wpf/ViewModel/MainViewModel.cs index 817c257..a7678ba 100644 --- a/xunit.runner.wpf/ViewModel/MainViewModel.cs +++ b/xunit.runner.wpf/ViewModel/MainViewModel.cs @@ -22,6 +22,7 @@ namespace xunit.runner.wpf.ViewModel private bool isBusy; private bool isCancelRequested; + public MainViewModel() { if (IsInDesignMode) @@ -36,6 +37,7 @@ namespace xunit.runner.wpf.ViewModel allTestCases, TestCaseMatches, Tuple.Create(string.Empty, TestState.All), TestComparer.Instance); this.TestCases.CollectionChanged += TestCases_CollectionChanged; + this.WindowLoadedCommand = new RelayCommand(OnExecuteWindowLoaded); this.RunCommand = new RelayCommand(OnExecuteRun, CanExecuteRun); this.CancelCommand = new RelayCommand(OnExecuteCancel, CanExecuteCancel); } @@ -50,6 +52,7 @@ namespace xunit.runner.wpf.ViewModel } public ICommand ExitCommand { get; } = new RelayCommand(OnExecuteExit); + public ICommand WindowLoadedCommand { get; } public RelayCommand RunCommand { get; } public RelayCommand CancelCommand { get; } @@ -162,7 +165,7 @@ namespace xunit.runner.wpf.ViewModel public ObservableCollection Assemblies { get; } = new ObservableCollection(); public FilteredCollectionView> TestCases { get; } - private void OnExecuteOpen(object sender, ExecutedRoutedEventArgs e) + private async void OnExecuteOpen(object sender, ExecutedRoutedEventArgs e) { var fileDialog = new OpenFileDialog { @@ -175,30 +178,48 @@ namespace xunit.runner.wpf.ViewModel } var fileName = fileDialog.FileName; + await AddAssemblies(new[] { fileName }); + } + private async Task AddAssemblies(IEnumerable fileNames) + { + var loadingDialog = new LoadingDialog { Owner = MainWindow.Instance }; try { using (AssemblyHelper.SubscribeResolve()) { - using (var xunit = new XunitFrontController( - useAppDomain: true, - assemblyFileName: fileName, - diagnosticMessageSink: new DiagnosticMessageVisitor(), - shadowCopy: false)) - using (var testDiscoveryVisitor = new TestDiscoveryVisitor(xunit)) + loadingDialog.Show(); + foreach (var fileName in fileNames) { - xunit.Find(includeSourceInformation: false, messageSink: testDiscoveryVisitor, discoveryOptions: TestFrameworkOptions.ForDiscovery()); - testDiscoveryVisitor.Finished.WaitOne(); - allTestCases.AddRange(testDiscoveryVisitor.TestCases); + loadingDialog.AssemblyFileName = fileName; + + using (var xunit = new XunitFrontController( + useAppDomain: true, + assemblyFileName: fileName, + diagnosticMessageSink: new DiagnosticMessageVisitor(), + shadowCopy: false)) + using (var testDiscoveryVisitor = new TestDiscoveryVisitor(xunit)) + { + await Task.Run(() => + { + xunit.Find(includeSourceInformation: false, messageSink: testDiscoveryVisitor, discoveryOptions: TestFrameworkOptions.ForDiscovery()); + testDiscoveryVisitor.Finished.WaitOne(); + }); + + allTestCases.AddRange(testDiscoveryVisitor.TestCases); + Assemblies.Add(new TestAssemblyViewModel(fileName)); + } } } - - Assemblies.Add(new TestAssemblyViewModel(fileName)); } catch (Exception ex) { MessageBox.Show(Application.Current.MainWindow, ex.ToString()); } + finally + { + loadingDialog.Close(); + } } private class DiagnosticMessageVisitor : TestMessageVisitor @@ -307,6 +328,19 @@ namespace xunit.runner.wpf.ViewModel Application.Current.Shutdown(); } + private async void OnExecuteWindowLoaded() + { + try + { + IsBusy = true; + await AddAssemblies(Environment.GetCommandLineArgs().Skip(1)); + } + finally + { + IsBusy = false; + } + } + private bool CanExecuteRun() => !IsBusy && TestCases.Any(); diff --git a/xunit.runner.wpf/xunit.runner.wpf.csproj b/xunit.runner.wpf/xunit.runner.wpf.csproj index aedb083..6655be9 100644 --- a/xunit.runner.wpf/xunit.runner.wpf.csproj +++ b/xunit.runner.wpf/xunit.runner.wpf.csproj @@ -89,11 +89,18 @@ + + LoadingDialog.xaml + + + Designer + MSBuild:Compile + MSBuild:Compile Designer @@ -148,6 +155,9 @@ false + + +