Skip to content

Commit

Permalink
Vbe add-in optimize test explorer (#64)
Browse files Browse the repository at this point in the history
VBE add-in:
* show source code
* show detail test result
  • Loading branch information
josef-poetzl authored May 24, 2024
1 parent e2cf4c9 commit 4c7d5cc
Show file tree
Hide file tree
Showing 12 changed files with 490 additions and 38 deletions.
28 changes: 0 additions & 28 deletions vbe-add-In/AccUnit.VbeAddIn/About/AboutViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,6 @@ private void Navigate(string url)

}

public class RelayCommand<T> : ICommand
{
private readonly Action<T> _execute;
private readonly Predicate<T> _canExecute;

public RelayCommand(Action<T> execute, Predicate<T> canExecute = null)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}

public bool CanExecute(object parameter)
{
return _canExecute == null || _canExecute((T)parameter);
}

public void Execute(object parameter)
{
_execute((T)parameter);
}

public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
}

public class Contributor
{
public Contributor(string name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@
<Compile Include="CommitMethodNameEventArgs.cs" />
<Compile Include="ComRegistration.cs" />
<Compile Include="Connect.cs" />
<Compile Include="TestExplorer\TestResultDetailView.xaml.cs">
<DependentUpon>TestResultDetailView.xaml</DependentUpon>
</Compile>
<Compile Include="TestExplorer\TestResultViewModel.cs" />
<Compile Include="TestImportExport\ImportExportWindow.xaml.cs">
<DependentUpon>ImportExportWindow.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -331,6 +335,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="TestExplorer\TestResultDetailView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="TestImportExport\ImportExportWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
4 changes: 2 additions & 2 deletions vbe-add-In/AccUnit.VbeAddIn/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// indem Sie "*" wie unten gezeigt eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.9.2.0")]
[assembly: AssemblyFileVersion("0.9.2.0")]
[assembly: AssemblyVersion("0.9.3.0")]
[assembly: AssemblyFileVersion("0.9.3.0")]
28 changes: 28 additions & 0 deletions vbe-add-In/AccUnit.VbeAddIn/RelayCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,32 @@ public void Execute(object parameter)
execute();
}
}

public class RelayCommand<T> : ICommand
{
private readonly Action<T> _execute;
private readonly Predicate<T> _canExecute;

public RelayCommand(Action<T> execute, Predicate<T> canExecute = null)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}

public bool CanExecute(object parameter)
{
return _canExecute == null || _canExecute((T)parameter);
}

public void Execute(object parameter)
{
_execute((T)parameter);
}

public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
}
}
50 changes: 49 additions & 1 deletion vbe-add-In/AccUnit.VbeAddIn/TestExplorer/TestExplorerManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using AccessCodeLib.AccUnit.Interfaces;
using AccessCodeLib.AccUnit.Configuration;
using AccessCodeLib.AccUnit.Interfaces;
using AccessCodeLib.Common.Tools.Logging;
using AccessCodeLib.Common.VBIDETools;
using AccessCodeLib.Common.VBIDETools.Commandbar;
using Microsoft.Office.Core;
using Microsoft.Vbe.Interop;
using System;

namespace AccessCodeLib.AccUnit.VbeAddIn.TestExplorer
Expand Down Expand Up @@ -46,6 +48,52 @@ private void InitViewModel()
{
e.TestClassInfo = VbeIntegrationManager.TestClassManager.GetTestClassInfo(e.ClassName, true);
};
_viewModel.GotoSource += (sender, e) =>
{
try
{
ShowSourceCode(e.FullName);
}
catch { }
};
}

private void ShowSourceCode(string fullName)
{
var nameParts = fullName.Split('.');
var classname = nameParts[0];
var membername = nameParts.Length > 1 ? nameParts[1] : null;
ShowSourceCode(classname, membername);
}

private void ShowSourceCode(string classname, string membername)
{
var codePane = ActivateCodePane(classname, membername);
EnsureTextCursorIsVisible(codePane);
}

private CodePane ActivateCodePane(string classname, string membername = null)
{
var modul = VbeIntegrationManager.TestClassManager.ActiveVBProject.VBComponents.Item(classname).CodeModule;
var pane = modul.CodePane;
pane.Show();
pane.Window.SetFocus();
var procLine = 1;
if (!string.IsNullOrEmpty(membername))
{
// TODO: Determine upfront if the member does not exist and throw appropriate exception (including name of the missing member)
procLine = modul.ProcBodyLine[membername, vbext_ProcKind.vbext_pk_Proc];
}
pane.SetSelection(procLine, 1, procLine, 1);
return pane;
}

private static void EnsureTextCursorIsVisible(_CodePane codePane)
{
var window = codePane.Window;
window.Visible = false;
window.Visible = true;
window.SetFocus();
}

public VbeIntegrationManager VbeIntegrationManager { get; set; }
Expand Down
25 changes: 23 additions & 2 deletions vbe-add-In/AccUnit.VbeAddIn/TestExplorer/TestExplorerTreeView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
d:DataContext="{d:DesignInstance Type=local:TestItem}"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<local:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</UserControl.Resources>
<Grid>
<TreeView d:DataContext="{d:DesignInstance Type=local:TestExplorerViewModel}" ItemsSource="{Binding TestItems}">
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
<EventSetter Event="GotFocus" Handler="TreeViewItem_GotFocus"/>
<EventSetter Event="LostFocus" Handler="TreeViewItem_LostFocus"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
Expand All @@ -21,12 +26,28 @@
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" IsChecked="{Binding IsChecked}" Margin="0,0,10,0" />
<Image Grid.Column="1" Source="{Binding ImageSource}" Margin="0,0,10,0" />
<TextBlock Grid.Column="2" Text="{Binding Name}" Margin="0,0,10,0" />
<TextBlock Grid.Column="3" Text="{Binding Result}" />
<TextBlock Grid.Column="2" Text="{Binding Name}" />
<Button Grid.Column="3" Content="Source"
Command="{Binding Path=DataContext.GoToSourceCommand, RelativeSource={RelativeSource AncestorType={x:Type local:TestExplorerTreeView}}}"
CommandParameter="{Binding}"
Visibility="{Binding ShowGoToSourceButton, Converter={StaticResource BooleanToVisibilityConverter}}"
VerticalAlignment="Center" Margin="2 0 4 0" Padding="3 0"
Background="Transparent"
/>
<TextBlock Grid.Column="4" Text="{Binding Result}" />
<Button Grid.Column="5" Content="Details"
Command="{Binding Path=DataContext.ShowTestResultDetailCommand, RelativeSource={RelativeSource AncestorType={x:Type local:TestExplorerTreeView}}}"
CommandParameter="{Binding}"
Visibility="{Binding ShowTestDetailButton, Converter={StaticResource BooleanToVisibilityConverter}}"
VerticalAlignment="Center" Margin="2 0 1 0" Padding="3 0"
Background="Transparent"
/>
</Grid>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows.Controls;
using System.Windows;
using System.Windows.Controls;

namespace AccessCodeLib.AccUnit.VbeAddIn.TestExplorer
{
Expand All @@ -9,5 +10,21 @@ public TestExplorerTreeView()
InitializeComponent();
//DataContext = new TestExplorerViewModel();
}

private void TreeViewItem_GotFocus(object sender, RoutedEventArgs e)
{
if (sender is TreeViewItem treeViewItem && treeViewItem.DataContext is TestItem testItem)
{
testItem.IsFocused = true;
}
}

private void TreeViewItem_LostFocus(object sender, RoutedEventArgs e)
{
if (sender is TreeViewItem treeViewItem && treeViewItem.DataContext is TestItem testItem)
{
testItem.IsFocused = false;
}
}
}
}
49 changes: 45 additions & 4 deletions vbe-add-In/AccUnit.VbeAddIn/TestExplorer/TestExplorerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Windows.Data;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

Expand All @@ -20,6 +23,7 @@ public class TestExplorerViewModel : ITestResultReporter, INotifyPropertyChanged
public event EventHandler<RunTestsEventArgs> RunTests;
//public event EventHandler CancelTestRun;
public event EventHandler<GetTestClassInfoEventArgs> GetTestClassInfo;
public event EventHandler<TestItem> GotoSource;

public TestExplorerViewModel()
{
Expand All @@ -29,6 +33,8 @@ public TestExplorerViewModel()
TestItems = new TestClassInfoTestItems();
RefreshCommand = new RelayCommand(Refresh);
CommitCommand = new RelayCommand(Commit);
ShowTestResultDetailCommand = new RelayCommand<TestItem>(ShowTestResultDetail);
GoToSourceCommand = new RelayCommand<TestItem>(GoToSource);
}

private CheckableItems<TestItem> _testItems;
Expand Down Expand Up @@ -332,16 +338,51 @@ public ImageSource RefreshCommandImageSource

protected void Refresh()
{
RefreshList?.Invoke(this, new CheckableTestItemsEventArgs(TestItems));
try
{
RefreshList?.Invoke(this, new CheckableTestItemsEventArgs(TestItems));
}
catch (Exception ex)
{
UITools.ShowException(ex);
}
}

protected virtual void Commit()
{
TestClassList list = new TestClassList();
list.AddRange(TestItems.Where(ti => ti.IsChecked).Select(ti => ((TestClassInfoTestItem)ti).TestClassInfo));
RunTests?.Invoke(this, new RunTestsEventArgs(list));
try
{
TestClassList list = new TestClassList();
list.AddRange(TestItems.Where(ti => ti.IsChecked).Select(ti => ((TestClassInfoTestItem)ti).TestClassInfo));
RunTests?.Invoke(this, new RunTestsEventArgs(list));
}
catch (Exception ex)
{
UITools.ShowException(ex);
}
}

public ICommand ShowTestResultDetailCommand { get; }
protected virtual void ShowTestResultDetail(TestItem testItem)
{
try
{
var testResultViewModel = new TestResultViewModel(testItem.TestResult);
var testResultView = new TestResultDetailView(testResultViewModel);
testResultView.ShowDialog();
}
catch(Exception ex )
{
UITools.ShowException(ex);
}

}

public ICommand GoToSourceCommand { get; }
protected virtual void GoToSource(TestItem testItem)
{
GotoSource?.Invoke(this, testItem);
}
}

public static class TestExplorerInfo
Expand Down
60 changes: 60 additions & 0 deletions vbe-add-In/AccUnit.VbeAddIn/TestExplorer/TestItem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using AccessCodeLib.AccUnit.Interfaces;
using System.Globalization;
using System.Windows.Data;
using System.Windows;
using System;
using System.Windows.Media;
using System.Linq;

namespace AccessCodeLib.AccUnit.VbeAddIn.TestExplorer
{
Expand Down Expand Up @@ -86,5 +91,60 @@ private ImageSource CalculatedImageSource
return null;
}
}

private bool isFocused;
public bool IsFocused
{
get => isFocused;
set { isFocused = value; OnPropertyChanged(nameof(IsFocused)); OnPropertyChanged(nameof(ShowTestDetailButton)); OnPropertyChanged(nameof(ShowGoToSourceButton)); }
}

private bool ChildrenAreFocused
{
get
{
return Children.Count > 0 && Children.Any(c => c.IsFocused);
}


}

public bool ShowTestDetailButton
{
get
{
return IsFocused && TestResult != null && !TestResult.Success && Children.Count == 0;
}
}

public bool ShowGoToSourceButton
{
get
{
return IsFocused && !ChildrenAreFocused;
}
}

}

public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool booleanValue)
{
return booleanValue ? Visibility.Visible : Visibility.Hidden;
}
return Visibility.Hidden;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Visibility visibility)
{
return visibility == Visibility.Visible;
}
return false;
}
}
}
Loading

0 comments on commit 4c7d5cc

Please sign in to comment.