Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions ILSpy.Tests/AssemblyTree/BackgroundLoadSweepTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright (c) 2026 Christoph Wille
//
// 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;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

using Avalonia.Headless.NUnit;
using Avalonia.Threading;

using ICSharpCode.ILSpy.AppEnv;

using NUnit.Framework;

namespace ICSharpCode.ILSpy.Tests.AssemblyTree;

/// <summary>
/// The background sweep that loads every assembly of the shown list ends by asking the commands
/// to re-evaluate, which is what enables "Remove assemblies with load errors". That has to happen
/// when one of the assemblies fails to load, since that is the only case the command is for.
/// </summary>
[TestFixture]
public class BackgroundLoadSweepTests
{
/// <summary>
/// Holds a load open until the test lets go of it, then ends without a single byte, which
/// no loader accepts. The test thereby chooses the moment the assembly fails to load.
/// </summary>
sealed class GatedEmptyStream : Stream
{
public ManualResetEventSlim Gate { get; } = new();

public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length => throw new NotSupportedException();
public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }

public override int Read(byte[] buffer, int offset, int count)
{
Gate.Wait();
return 0;
}

public override void Flush() { }
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
}

[AvaloniaTest]
public async Task Sweep_Requeries_Commands_When_An_Assembly_Fails_To_Load()
{
var (_, vm) = await TestHarness.BootAsync();
await Waiters.WaitForIdleAsync();

// Show a list whose only assembly cannot finish loading before the gate opens, and let
// everything the switch itself queues run first, so that a requery seen afterwards can
// only be the one that ends the sweep.
var stream = new GatedEmptyStream();
var listManager = AppComposition.Current.GetExport<SettingsService>().AssemblyListManager;
var list = listManager.CreateList("gated-list");
var assembly = list.OpenAssembly(Path.Combine(Path.GetTempPath(), "ILSpyGated.dll"), stream);
vm.AssemblyTreeModel.ShowAssemblyList(list);
await Waiters.WaitForAsync(
() => !Dispatcher.UIThread.HasJobsWithPriority(DispatcherPriority.Background),
description: "the list switch to settle");

bool requeried = false;
EventHandler onRequery = (_, _) => requeried = true;
ICSharpCode.ILSpy.Commands.CommandManager.AddRequerySuggested(onRequery);
try
{
stream.Gate.Set();
await Waiters.WaitForAsync(() => assembly.HasLoadError, description: "the gated assembly to fail loading");
await Waiters.WaitForAsync(() => requeried,
description: "a command requery after the assembly failed to load");
}
finally
{
stream.Gate.Set();
ICSharpCode.ILSpy.Commands.CommandManager.RemoveRequerySuggested(onRequery);
}
}
}
15 changes: 15 additions & 0 deletions ILSpy.Tests/Controls/SharpTreeViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

using ICSharpCode.ILSpyX.TreeView;

using ICSharpCode.ILSpy.Controls;
using ICSharpCode.ILSpy.Controls.TreeView;

using NUnit.Framework;
Expand Down Expand Up @@ -65,6 +66,20 @@ public TestNode(string text, params TestNode[] children)
return (window, tree, root);
}

[AvaloniaTest]
public void ShowLines_Reaches_The_Connector_Lines_Of_Every_Row()
{
var (_, tree, _) = Host();
var lines = tree.GetVisualDescendants().OfType<TreeLines>().ToList();
lines.Should().NotBeEmpty("every realized row carries a TreeLines");
lines.Should().OnlyContain(l => l.GetValue(TreeLines.ShowLinesProperty), "lines are shown by default");

tree.ShowLines = false;

lines.Should().OnlyContain(l => !l.GetValue(TreeLines.ShowLinesProperty),
"the rows take the setting from the tree view they are in");
}

[AvaloniaTest]
public void Flattens_Visible_Nodes_And_Expand_Collapse_Updates_Row_Count()
{
Expand Down
75 changes: 75 additions & 0 deletions ILSpy.Tests/MainWindow/StartupBindingErrorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) 2026 Christoph Wille
//
// 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.Collections.Generic;
using System.Threading.Tasks;

using Avalonia.Headless.NUnit;
using Avalonia.Logging;

using AwesomeAssertions;

using NUnit.Framework;

namespace ICSharpCode.ILSpy.Tests;

/// <summary>
/// A binding that fails while the main window comes up is logged by Avalonia and otherwise
/// goes unnoticed: the bound property silently keeps its default. Booting the window with a
/// capturing log sink pins that startup produces no such errors.
/// </summary>
[TestFixture]
public class StartupBindingErrorTests
{
sealed class BindingErrorSink(ILogSink? inner) : ILogSink
{
public List<string> Errors { get; } = new();

public bool IsEnabled(LogEventLevel level, string area)
=> area == LogArea.Binding || (inner?.IsEnabled(level, area) ?? false);

public void Log(LogEventLevel level, string area, object? source, string messageTemplate)
=> Log(level, area, source, messageTemplate, []);

public void Log(LogEventLevel level, string area, object? source, string messageTemplate, params object?[] propertyValues)
{
if (area == LogArea.Binding && level >= LogEventLevel.Warning)
Errors.Add($"{messageTemplate} [{string.Join(", ", propertyValues)}] ({source})");
}
}

[AvaloniaTest]
public async Task Booting_The_Main_Window_Logs_No_Binding_Errors()
{
var previous = Logger.Sink;
var sink = new BindingErrorSink(previous);
Logger.Sink = sink;
try
{
await TestHarness.BootAsync();
await Waiters.WaitForIdleAsync();
}
finally
{
Logger.Sink = previous;
}

sink.Errors.Should().BeEmpty("every binding in the startup UI must resolve against its data context");
}
}
7 changes: 6 additions & 1 deletion ILSpy/AssemblyTree/AssemblyTreeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ void ShowAssemblyList(string name)
ShowAssemblyList(list);
}

void ShowAssemblyList(AssemblyList list)
internal void ShowAssemblyList(AssemblyList list)
{
using var _ = AppEnv.AppLog.Phase("ShowAssemblyList(list)");
// Detach the previous list's collection-changed wiring so the MessageBus
Expand Down Expand Up @@ -631,6 +631,11 @@ void ScheduleBackgroundLoadSweep(AssemblyList list)
loadTasks.Add(Task.Run(async () => {
try
{ await assembly.GetLoadResultAsync().ConfigureAwait(false); }
catch (Exception)
{
// A file that cannot be loaded is an expected outcome, recorded on the
// LoadedAssembly itself (HasLoadError). It must not fault the sweep.
}
finally
{ throttle.Release(); }
}));
Expand Down
12 changes: 10 additions & 2 deletions ILSpy/Controls/TreeLines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Avalonia.Controls;
using Avalonia.Media;

using ICSharpCode.ILSpy.Controls.TreeView;
using ICSharpCode.ILSpyX.TreeView;

namespace ICSharpCode.ILSpy.Controls
Expand All @@ -48,12 +49,19 @@ public class TreeLines : Control
public static readonly StyledProperty<int> LevelProperty =
AvaloniaProperty.Register<TreeLines, int>(nameof(Level));

/// <summary>
/// Inherited from the hosting <see cref="SharpTreeView"/>; stays at its default (lines
/// shown) where there is none.
/// </summary>
public static readonly StyledProperty<bool> ShowLinesProperty =
SharpTreeView.ShowLinesProperty.AddOwner<TreeLines>();

static TreeLines()
{
// Classic Windows-Explorer dotted connector lines.
var pen = new Pen(Brushes.Gray, 1) { DashStyle = new DashStyle(new double[] { 1, 1 }, 0) };
Pen = pen.ToImmutable();
AffectsRender<TreeLines>(NodeProperty, LevelProperty);
AffectsRender<TreeLines>(NodeProperty, LevelProperty, ShowLinesProperty);
IsHitTestVisibleProperty.OverrideDefaultValue<TreeLines>(false);
}

Expand All @@ -73,7 +81,7 @@ public int Level {
public override void Render(DrawingContext context)
{
var node = Node;
if (node == null || node.IsRoot)
if (node == null || node.IsRoot || !GetValue(ShowLinesProperty))
return;

double height = Bounds.Height;
Expand Down
3 changes: 1 addition & 2 deletions ILSpy/Controls/TreeView/SharpTreeView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@
<DataTemplate x:CompileBindings="False">
<Panel Background="Transparent" ToolTip.Tip="{Binding ToolTip}">
<!-- Classic tree connector lines, behind the content. -->
<ctrl:TreeLines Node="{Binding}" Level="{Binding Level}"
IsVisible="{Binding ShowLines, RelativeSource={RelativeSource AncestorType=tv:SharpTreeView}}" />
<ctrl:TreeLines Node="{Binding}" Level="{Binding Level}" />
<StackPanel Orientation="Horizontal" Height="20">
<!-- Indent spacer = (Level - 1) * step (top level sits flush left). -->
<Border Width="{Binding Level, Converter={x:Static tv:TreeIndentConverter.Instance}}" />
Expand Down
2 changes: 1 addition & 1 deletion ILSpy/Controls/TreeView/SharpTreeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class SharpTreeView : ListBox
AvaloniaProperty.Register<SharpTreeView, bool>(nameof(ShowRootExpander), defaultValue: false);

public static readonly StyledProperty<bool> ShowLinesProperty =
AvaloniaProperty.Register<SharpTreeView, bool>(nameof(ShowLines), defaultValue: true);
AvaloniaProperty.Register<SharpTreeView, bool>(nameof(ShowLines), defaultValue: true, inherits: true);

TreeFlattener? flattener;
string searchBuffer = string.Empty;
Expand Down
7 changes: 7 additions & 0 deletions ILSpy/Docking/ILSpyDockFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ public override void InitLayout(IDockable layout)
if (layout is IRootDock { Id: { Length: > 0 } rootId } root)
DockableLocator[rootId] = () => root;

// Dock's theme template binds against DockCapabilityOverrides.{CanDrag, CanDrop} on
// every dockable, the docks included. A layout restored from disk carries none, so
// fill them in here, where both fresh and restored layouts pass through.
foreach (var dockable in Flatten(layout))
dockable.DockCapabilityOverrides ??= new DockCapabilityOverrides();

base.InitLayout(layout);
}

Expand Down Expand Up @@ -527,6 +533,7 @@ public void ActivateAndFocus(IDock owner, IDockable dockable)
Alignment = dockAlignment,
VisibleDockables = CreateList(System.Array.Empty<IDockable>()),
DockCapabilityPolicy = new DockCapabilityPolicy(),
DockCapabilityOverrides = new DockCapabilityOverrides(),
};

switch (alignment)
Expand Down
11 changes: 10 additions & 1 deletion ILSpy/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@
<local:MainToolBar DockPanel.Dock="Top" />

<!-- Update banner: docks under the toolbar, IsVisible-bound to the panel viewmodel. -->
<views:UpdatePanel DockPanel.Dock="Top" DataContext="{Binding UpdatePanel}" />
<!-- Hosted through a content template rather than given a DataContext binding of its own:
the panel then never inherits the window's view model, against which its compiled
bindings cannot resolve. -->
<ContentControl DockPanel.Dock="Top" Content="{Binding UpdatePanel}">
<ContentControl.ContentTemplate>
<DataTemplate x:DataType="vm:UpdatePanelViewModel">
<views:UpdatePanel />
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>

<!-- Status bar (hidden by default, like WPF) -->
<Border DockPanel.Dock="Bottom" Height="26" IsVisible="False" Name="statusBar">
Expand Down
Loading