Browse Source

Compare assemblies tab actually renders the diff tree

The "Compare..." right-click flow opened a tab that came up blank because
two stacked bugs masked each other.

Assisted-by: Claude:claude-opus-4-7:Claude Code
pull/3755/head
Siegfried Pammer 2 months ago
parent
commit
12adc2f256
  1. 109
      ILSpy.Tests/Compare/CompareViewRenderTests.cs
  2. 3
      ILSpy/Views/CompareView.axaml
  3. 64
      ILSpy/Views/CompareView.axaml.cs
  4. 2
      ILSpy/Views/ContentTabPageView.axaml
  5. 14
      ILSpy/Views/ContentTabPageView.axaml.cs

109
ILSpy.Tests/Compare/CompareViewRenderTests.cs

@ -0,0 +1,109 @@ @@ -0,0 +1,109 @@
// Copyright (c) 2026 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Headless.NUnit;
using Avalonia.VisualTree;
using AwesomeAssertions;
using ICSharpCode.ILSpyX.TreeView;
using ILSpy;
using ILSpy.AppEnv;
using ILSpy.Commands;
using ILSpy.Compare;
using ILSpy.TreeNodes;
using ILSpy.ViewModels;
using ILSpy.Views;
using NUnit.Framework;
namespace ICSharpCode.ILSpy.Tests.Compare;
/// <summary>
/// End-to-end render checks for the assembly-comparison view. Pure context-menu and engine
/// tests don't catch the case where the new tab opens but renders empty because the
/// hierarchical DataGrid was never wired with a model — these tests mount the view, walk
/// the visual tree, and assert rows materialise.
/// </summary>
[TestFixture]
public class CompareViewRenderTests
{
[AvaloniaTest]
public async Task CompareView_Binds_A_HierarchicalModel_When_Opened()
{
var window = AppComposition.Current.GetExport<MainWindow>();
window.Show();
var vm = (MainWindowViewModel)window.DataContext!;
await vm.AssemblyTreeModel.WaitForAssembliesAsync(minimumCount: 2);
var entry = AppComposition.Current.GetExport<global::ILSpy.ContextMenuEntryRegistry>()
.Entries.Single(e => e.Metadata.Header == "Compare...").Value;
var assemblies = vm.AssemblyTreeModel.AssemblyList!.GetAssemblies()
.Where(a => a.IsLoadedAsValidAssembly).Take(2).ToList();
var nodes = assemblies.Select(a =>
(SharpTreeNode)vm.AssemblyTreeModel.FindNode<AssemblyTreeNode>(a.ShortName)).ToArray();
entry.Execute(new TextViewContext { SelectedTreeNodes = nodes });
var view = await window.WaitForComponent<CompareView>();
var grid = await view.WaitForComponent<DataGrid>();
await Waiters.WaitForAsync(() => grid.HierarchicalModel != null,
description: "CompareView must populate the DataGrid's HierarchicalModel — without it "
+ "the hierarchical column is rendered over an empty source and the tab shows nothing");
grid.HierarchicalModel.Should().NotBeNull();
}
[AvaloniaTest]
public async Task CompareView_Renders_Rows_For_The_Merged_Tree()
{
var window = AppComposition.Current.GetExport<MainWindow>();
window.Show();
var vm = (MainWindowViewModel)window.DataContext!;
await vm.AssemblyTreeModel.WaitForAssembliesAsync(minimumCount: 2);
var entry = AppComposition.Current.GetExport<global::ILSpy.ContextMenuEntryRegistry>()
.Entries.Single(e => e.Metadata.Header == "Compare...").Value;
var assemblies = vm.AssemblyTreeModel.AssemblyList!.GetAssemblies()
.Where(a => a.IsLoadedAsValidAssembly).Take(2).ToList();
var nodes = assemblies.Select(a =>
(SharpTreeNode)vm.AssemblyTreeModel.FindNode<AssemblyTreeNode>(a.ShortName)).ToArray();
entry.Execute(new TextViewContext { SelectedTreeNodes = nodes });
var view = await window.WaitForComponent<CompareView>();
var grid = await view.WaitForComponent<DataGrid>();
// Default state: ShowIdentical=false, so we expect at least one diff row to be present
// when comparing two non-empty assemblies against themselves *or* against a sibling.
// (Comparing an assembly with itself yields zero diffs; if the two assemblies are the
// same instance we toggle ShowIdentical to force the tree to surface rows.)
var page = (CompareTabPageModel)view.DataContext!;
if (ReferenceEquals(page.LeftAssembly, page.RightAssembly))
page.ShowIdentical = true;
await Waiters.WaitForAsync(
() => grid.GetVisualDescendants().OfType<DataGridRow>().Any(),
description: "the diff grid must render at least one row once the merged tree is bound");
}
}

3
ILSpy/Views/CompareView.axaml

@ -22,8 +22,7 @@ @@ -22,8 +22,7 @@
HierarchicalRowsEnabled="True"
GridLinesVisibility="None"
CanUserResizeColumns="False"
SelectionMode="Single"
ItemsSource="{Binding RootEntry.Children}">
SelectionMode="Single">
<DataGrid.Columns>
<DataGridHierarchicalColumn Header="Diff" Width="*"
IsReadOnly="True"

64
ILSpy/Views/CompareView.axaml.cs

@ -16,15 +16,79 @@ @@ -16,15 +16,79 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.ComponentModel;
using Avalonia.Controls;
using Avalonia.Controls.DataGridHierarchical;
using ICSharpCode.ILSpyX.TreeView;
namespace ILSpy.Compare
{
public partial class CompareView : UserControl
{
CompareTabPageModel? boundModel;
public CompareView()
{
InitializeComponent();
}
protected override void OnDataContextChanged(EventArgs e)
{
base.OnDataContextChanged(e);
DetachFromModel();
if (DataContext is CompareTabPageModel model)
AttachToModel(model);
}
void AttachToModel(CompareTabPageModel model)
{
boundModel = model;
model.PropertyChanged += OnModelPropertyChanged;
Rebind();
}
void DetachFromModel()
{
if (boundModel == null)
return;
boundModel.PropertyChanged -= OnModelPropertyChanged;
boundModel = null;
}
void OnModelPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
// ShowIdentical toggles which rows are filtered out — re-running the cascade
// through Rebind makes the DataGrid re-evaluate visibility on the next layout
// pass without us having to walk the tree by hand.
if (e.PropertyName == nameof(CompareTabPageModel.ShowIdentical))
Rebind();
}
void Rebind()
{
var root = boundModel?.RootEntry;
if (root == null)
return;
root.EnsureChildrenFiltered();
var options = new HierarchicalOptions<SharpTreeNode> {
ChildrenSelector = node => {
if (node is TreeNodes.ILSpyTreeNode ilspy)
ilspy.EnsureChildrenFiltered();
else
node.EnsureLazyChildren();
return node.Children;
},
IsLeafSelector = node => !node.ShowExpander,
VirtualizeChildren = false,
IsExpandedPropertyPath = nameof(SharpTreeNode.IsExpanded),
};
var model = new HierarchicalModel<SharpTreeNode>(options);
model.SetRoots(root.Children);
DiffGrid.HierarchicalModel = model;
}
}
}

2
ILSpy/Views/ContentTabPageView.axaml

@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
xmlns:textView="using:ILSpy.TextView"
xmlns:metaViews="using:ILSpy.Views"
xmlns:options="using:ILSpy.Options"
xmlns:compare="using:ILSpy.Compare"
mc:Ignorable="d" d:DesignWidth="600" d:DesignHeight="400"
x:Class="ILSpy.Views.ContentTabPageView"
x:DataType="vm:ContentTabPage">
@ -19,6 +20,7 @@ @@ -19,6 +20,7 @@
<textView:DecompilerTextView Name="DecompilerView" />
<metaViews:MetadataTablePage Name="MetadataView" />
<options:OptionsPageView Name="OptionsView" />
<compare:CompareView Name="CompareViewSlot" />
</Panel>
</UserControl>

14
ILSpy/Views/ContentTabPageView.axaml.cs

@ -21,6 +21,7 @@ using System.ComponentModel; @@ -21,6 +21,7 @@ using System.ComponentModel;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using ILSpy.Compare;
using ILSpy.Options;
using ILSpy.TextView;
using ILSpy.ViewModels;
@ -39,6 +40,7 @@ namespace ILSpy.Views @@ -39,6 +40,7 @@ namespace ILSpy.Views
DecompilerTextView decompilerView = null!;
MetadataTablePage metadataView = null!;
OptionsPageView optionsView = null!;
CompareView compareView = null!;
public ContentTabPageView()
{
@ -46,6 +48,7 @@ namespace ILSpy.Views @@ -46,6 +48,7 @@ namespace ILSpy.Views
decompilerView = this.FindControl<DecompilerTextView>("DecompilerView")!;
metadataView = this.FindControl<MetadataTablePage>("MetadataView")!;
optionsView = this.FindControl<OptionsPageView>("OptionsView")!;
compareView = this.FindControl<CompareView>("CompareViewSlot")!;
DataContextChanged += (_, _) => RebindPage();
}
@ -103,6 +106,17 @@ namespace ILSpy.Views @@ -103,6 +106,17 @@ namespace ILSpy.Views
optionsView.IsVisible = false;
optionsView.DataContext = null;
}
if (content is CompareTabPageModel compare)
{
compareView.DataContext = compare;
compareView.IsVisible = true;
}
else
{
compareView.IsVisible = false;
compareView.DataContext = null;
}
}
}
}

Loading…
Cancel
Save