From c471cf1aeb7f5156beaa5bc4dc91a5ea3b341189 Mon Sep 17 00:00:00 2001 From: BDisp Date: Sat, 23 Mar 2024 15:54:33 +0000 Subject: [PATCH] Add ObjectDisposedException testing for View.Title and fixes unit test. --- Terminal.Gui/View/View.cs | 17 ++++++++++++- UnitTests/Application/ApplicationTests.cs | 30 +++++++++++++++++------ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/Terminal.Gui/View/View.cs b/Terminal.Gui/View/View.cs index c78d327f5..07235e638 100644 --- a/Terminal.Gui/View/View.cs +++ b/Terminal.Gui/View/View.cs @@ -427,9 +427,24 @@ public partial class View : Responder, ISupportInitializeNotification /// The title. public string Title { - get => _title; + get + { +#if DEBUG_IDISPOSABLE + if (WasDisposed) + { + throw new ObjectDisposedException (GetType ().FullName); + } +#endif + return _title; + } set { +#if DEBUG_IDISPOSABLE + if (WasDisposed) + { + throw new ObjectDisposedException (GetType ().FullName); + } +#endif if (value == _title) { return; diff --git a/UnitTests/Application/ApplicationTests.cs b/UnitTests/Application/ApplicationTests.cs index 71714e5f8..78c390d69 100644 --- a/UnitTests/Application/ApplicationTests.cs +++ b/UnitTests/Application/ApplicationTests.cs @@ -879,37 +879,51 @@ public class ApplicationTests ); Application.End (rs); + w.Dispose (); Application.Shutdown (); } [Fact] - public void End_Disposing_Correctly () + public void End_Does_Not_Dispose () { Init (); - var top = Application.Top; + var top = new Toplevel (); Window w = new (); w.Ready += (s, e) => Application.RequestStop (); // Causes `End` to be called Application.Run(w); #if DEBUG_IDISPOSABLE - Assert.True (w.WasDisposed); + Assert.False (w.WasDisposed); #endif Assert.NotNull (w); - Assert.Equal (string.Empty, w.Title); // Invalid - w has been disposed -> Valid - w isn't Application.Top but the original created by Init + Assert.Equal (string.Empty, w.Title); // Valid - w has not been disposed. The user may want to run it again Assert.NotNull (Application.Top); - Assert.NotEqual(w, Application.Top); - Assert.Equal(top, Application.Top); + Assert.Equal(w, Application.Top); + Assert.NotEqual(top, Application.Top); Assert.Null (Application.Current); - var exception = Record.Exception (() => Application.Run(w)); // Invalid - w has been disposed. Run it in debug mode will throw, otherwise the user may want to run it again + Application.Run(w); // Valid - w has not been disposed. + +#if DEBUG_IDISPOSABLE + Assert.False (w.WasDisposed); + var exception = Record.Exception (() => Application.Shutdown()); // Invalid - w has not been disposed. Assert.NotNull (exception); + w.Dispose (); + Assert.True (w.WasDisposed); + exception = Record.Exception (() => Application.Run (w)); // Invalid - w has been disposed. Run it in debug mode will throw, otherwise the user may want to run it again + Assert.NotNull (exception); + + exception = Record.Exception (() => Assert.Equal (string.Empty, w.Title)); // Invalid - w has been disposed and cannot be accessed + Assert.NotNull (exception); + exception = Record.Exception (() => w.Title = "NewTitle"); // Invalid - w has been disposed and cannot be accessed + Assert.NotNull (exception); +#endif Application.Shutdown (); Assert.NotNull (w); - Assert.Equal (string.Empty, w.Title); // Invalid - w has been disposed -> Valid - w isn't Application.Top but the original created by Init Assert.Null (Application.Current); Assert.NotNull (top); Assert.Null (Application.Top);