Skip to content

Avalonia: fix exception thrown when UserControl is unloaded#429

Open
ibram-reda wants to merge 2 commits into
videolan:3.xfrom
ibram-reda:avalonia-visualtree-problem
Open

Avalonia: fix exception thrown when UserControl is unloaded#429
ibram-reda wants to merge 2 commits into
videolan:3.xfrom
ibram-reda:avalonia-visualtree-problem

Conversation

@ibram-reda
Copy link
Copy Markdown

Description of Change

i have create a test project LibVLCSharp.Avalonia.Tests which reference and test LibVLCSharp.Avalonia project
i have added only two tests to assure of VideoView removal correctly when it has a content or without a content

[TestCase(true)]
[TestCase(false)]
[AvaloniaTest]
public void VideoView_Should_Be_Removed_From_VisualTree_When_Parent_Is_Removed(bool withContent)
{
    // Setup controls:
    var videoView = new VideoView
    {
        Name = "VideoView",
        Content = withContent ? new TextBox() { Name = "VideoViewTextBox", Text = "" } : null
    };
    var secondView = new TextBox() { Name = "Second", Text = "" };
    var window = new Window { Content = videoView };
    // Open window:
    window.Show();
    try
    {                
        // change Content of window:
        window.Content = secondView;
        // wait for the UI to process the change:
        window.Dispatcher.RunJobs();
    }
    catch
    {
        // fail the test if any exception occurs during the process
        Assert.Fail("An exception occurred while changing the window content.");
    }
    var RemovedVideoView = window.FindDescendantOfType<VideoView>(false, t => t.Name == "VideoView");
    var secondTextBox = window.FindDescendantOfType<TextBox>(false, t => t.Name == "Second");
    Assert.That(RemovedVideoView, Is.Null);
    Assert.That(secondTextBox, Is.Not.Null);
}

this test was pass in contentless VedioView and Fail in the other one .... in order to make it pass in both cases i have added a guard before the problem line PointToScreen:

 if (!this.IsAttachedToVisualTree())
 {
       return;
 }

var newPosition = this.PointToScreen(topLeft);

this test should be the same test for the problem in #371

the other files is just add to make a test environment Taken from Avalonia docs : Headless Testing with NUnit

  • app.axaml
  • app.axaml.cs
  • TestAppBuilder.cs

@mfkl
Copy link
Copy Markdown
Member

mfkl commented Jun 3, 2026

Thanks! Will review and test in coming days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants