Переглянути джерело

Change unit tests to use ObservableCollection.

BDisp 1 рік тому
батько
коміт
7438f9f420

+ 5 - 4
UnitTests/UICatalog/ScenarioTests.cs

@@ -1,3 +1,4 @@
+using System.Collections.ObjectModel;
 using System.Reflection;
 using Xunit.Abstractions;
 
@@ -150,7 +151,7 @@ public class ScenarioTests : TestsAllViews
             Height = Dim.Fill (),
             AllowsMarking = false,
             ColorScheme = Colors.ColorSchemes ["TopLevel"],
-            Source = new ListWrapper (_viewClasses.Keys.ToList ())
+            Source = new ListWrapper<string> (new (_viewClasses.Keys.ToList ()))
         };
         _leftPane.Add (_classListView);
 
@@ -527,7 +528,7 @@ public class ScenarioTests : TestsAllViews
                 && view.GetType ().GetProperty ("Source") != null
                 && view.GetType ().GetProperty ("Source").PropertyType == typeof (IListDataSource))
             {
-                var source = new ListWrapper (new List<string> { "Test Text #1", "Test Text #2", "Test Text #3" });
+                var source = new ListWrapper<string> (["Test Text #1", "Test Text #2", "Test Text #3"]);
                 view?.GetType ().GetProperty ("Source")?.GetSetMethod ()?.Invoke (view, new [] { source });
             }
 
@@ -555,10 +556,10 @@ public class ScenarioTests : TestsAllViews
     [Fact]
     public void Run_Generic ()
     {
-        List<Scenario> scenarios = Scenario.GetScenarios ();
+        ObservableCollection<Scenario> scenarios = Scenario.GetScenarios ();
         Assert.NotEmpty (scenarios);
 
-        int item = scenarios.FindIndex (s => s.GetName ().Equals ("Generic", StringComparison.OrdinalIgnoreCase));
+        int item = scenarios.IndexOf (s => s.GetName ().Equals ("Generic", StringComparison.OrdinalIgnoreCase));
         Scenario generic = scenarios [item];
 
         Application.Init (new FakeDriver ());

+ 20 - 19
UnitTests/Views/ComboBoxTests.cs

@@ -1,4 +1,5 @@
-using Xunit.Abstractions;
+using System.Collections.ObjectModel;
+using Xunit.Abstractions;
 
 namespace Terminal.Gui.ViewsTests;
 
@@ -13,7 +14,7 @@ public class ComboBoxTests
     {
         var cb = new ComboBox
         {
-            Source = new ListWrapper (new List<string> { "One", "Two", "Three" }), SelectedItem = 1
+            Source = new ListWrapper<string> (["One", "Two", "Three"]), SelectedItem = 1
         };
         cb.BeginInit ();
         cb.EndInit ();
@@ -52,7 +53,7 @@ public class ComboBoxTests
             Y = 2,
             Width = 10,
             Height = 20,
-            Source = new ListWrapper (new List<string> { "One", "Two", "Three" })
+            Source = new ListWrapper<string> (["One", "Two", "Three"])
         };
         cb.BeginInit ();
         cb.EndInit ();
@@ -62,7 +63,7 @@ public class ComboBoxTests
         Assert.Equal (new Rectangle (1, 2, 10, 20), cb.Frame);
         Assert.Equal (-1, cb.SelectedItem);
 
-        cb = new ComboBox { Source = new ListWrapper (new List<string> { "One", "Two", "Three" }) };
+        cb = new ComboBox { Source = new ListWrapper<string> (["One", "Two", "Three"]) };
         cb.BeginInit ();
         cb.EndInit ();
         cb.LayoutSubviews ();
@@ -79,7 +80,7 @@ public class ComboBoxTests
         var comboBox = new ComboBox { Text = "0" };
 
         string [] source = Enumerable.Range (0, 15).Select (x => x.ToString ()).ToArray ();
-        comboBox.SetSource (source);
+        comboBox.SetSource (new ObservableCollection<string> (source.ToList ()));
 
         var top = new Toplevel ();
         top.Add (comboBox);
@@ -95,7 +96,7 @@ public class ComboBoxTests
     public void Expanded_Collapsed_Events ()
     {
         var cb = new ComboBox { Height = 4, Width = 5 };
-        List<string> list = new () { "One", "Two", "Three" };
+        ObservableCollection<string> list = ["One", "Two", "Three"];
 
         cb.Expanded += (s, e) => cb.SetSource (list);
         cb.Collapsed += (s, e) => cb.Source = null;
@@ -128,7 +129,7 @@ public class ComboBoxTests
     {
         var selected = "";
         var cb = new ComboBox { Height = 4, Width = 5, HideDropdownListOnClick = false };
-        cb.SetSource (new List<string> { "One", "Two", "Three" });
+        cb.SetSource (["One", "Two", "Three"]);
         cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
         var top = new Toplevel ();
         top.Add (cb);
@@ -182,7 +183,7 @@ public class ComboBoxTests
     {
         var selected = "";
         var cb = new ComboBox { Height = 4, Width = 5, HideDropdownListOnClick = false };
-        cb.SetSource (new List<string> { "One", "Two", "Three" });
+        cb.SetSource (["One", "Two", "Three"]);
         cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
         var top = new Toplevel ();
         top.Add (cb);
@@ -218,7 +219,7 @@ public class ComboBoxTests
     {
         var selected = "";
         var cb = new ComboBox { Height = 4, Width = 5, HideDropdownListOnClick = false, ReadOnly = true };
-        cb.SetSource (new List<string> { "One", "Two", "Three" });
+        cb.SetSource (["One", "Two", "Three"]);
         cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
         var top = new Toplevel ();
         top.Add (cb);
@@ -276,7 +277,7 @@ public class ComboBoxTests
     {
         var selected = "";
         var cb = new ComboBox { Height = 4, Width = 5 };
-        cb.SetSource (new List<string> { "One", "Two", "Three" });
+        cb.SetSource (["One", "Two", "Three"]);
         cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
         var top = new Toplevel ();
         top.Add (cb);
@@ -377,7 +378,7 @@ public class ComboBoxTests
     {
         var selected = "";
         var cb = new ComboBox { Height = 4, Width = 5, HideDropdownListOnClick = true };
-        cb.SetSource (new List<string> { "One", "Two", "Three" });
+        cb.SetSource (["One", "Two", "Three"]);
         cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
         var top = new Toplevel ();
         top.Add (cb);
@@ -495,7 +496,7 @@ public class ComboBoxTests
     {
         var selected = "";
         var cb = new ComboBox { Width = 6, Height = 4, HideDropdownListOnClick = true };
-        cb.SetSource (new List<string> { "One", "Two", "Three" });
+        cb.SetSource (["One", "Two", "Three"]);
         cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
         var top = new Toplevel ();
         top.Add (cb);
@@ -652,7 +653,7 @@ Three ",
     {
         var selected = "";
         var cb = new ComboBox { Height = 4, Width = 5, HideDropdownListOnClick = true };
-        cb.SetSource (new List<string> { "One", "Two", "Three" });
+        cb.SetSource (["One", "Two", "Three"]);
         cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
         var top = new Toplevel ();
         top.Add (cb);
@@ -714,7 +715,7 @@ Three ",
     {
         var selected = "";
         var cb = new ComboBox { Height = 4, Width = 5, HideDropdownListOnClick = true };
-        cb.SetSource (new List<string> { "One", "Two", "Three" });
+        cb.SetSource (["One", "Two", "Three"]);
         cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
         var top = new Toplevel ();
         top.Add (cb);
@@ -771,7 +772,7 @@ Three ",
     {
         var selected = "";
         var cb = new ComboBox { Height = 4, Width = 5, HideDropdownListOnClick = true };
-        cb.SetSource (new List<string> { "One", "Two", "Three" });
+        cb.SetSource (["One", "Two", "Three"]);
         cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
         var top = new Toplevel ();
         top.Add (cb);
@@ -804,7 +805,7 @@ Three ",
     [AutoInitShutdown]
     public void KeyBindings_Command ()
     {
-        List<string> source = new () { "One", "Two", "Three" };
+        ObservableCollection<string> source = ["One", "Two", "Three"];
         var cb = new ComboBox { Width = 10 };
         cb.SetSource (source);
         var top = new Toplevel ();
@@ -821,7 +822,7 @@ Three ",
         Assert.True (opened);
         Assert.Equal ("Tw", cb.Text);
         Assert.False (cb.IsShow);
-        cb.SetSource (null);
+        cb.SetSource<string> (null);
         Assert.False (cb.NewKeyDownEvent (Key.Enter));
         Assert.True (cb.NewKeyDownEvent (Key.F4)); // with no source also expand empty
         Assert.True (cb.IsShow);
@@ -970,7 +971,7 @@ Three
         top.FocusFirst ();
         Assert.Null (cb.Source);
         Assert.Equal (-1, cb.SelectedItem);
-        List<string> source = new ();
+        ObservableCollection<string> source = [];
         cb.SetSource (source);
         Assert.NotNull (cb.Source);
         Assert.Equal (0, cb.Source.Count);
@@ -998,7 +999,7 @@ Three
         Assert.False (cb.IsShow);
         Assert.Equal (-1, cb.SelectedItem); // retains last accept selected item
         Assert.Equal ("", cb.Text); // clear text
-        cb.SetSource (new List<string> ());
+        cb.SetSource (new ObservableCollection<string> ());
         Assert.Equal (0, cb.Source.Count);
         Assert.Equal (-1, cb.SelectedItem);
         Assert.Equal ("", cb.Text);

+ 33 - 29
UnitTests/Views/ListViewTests.cs

@@ -1,4 +1,6 @@
 using System.Collections;
+using System.Collections.ObjectModel;
+using System.Collections.Specialized;
 using System.ComponentModel;
 using Xunit.Abstractions;
 
@@ -17,7 +19,7 @@ public class ListViewTests
         Assert.True (lv.CanFocus);
         Assert.Equal (-1, lv.SelectedItem);
 
-        lv = new ListView { Source = new ListWrapper (new List<string> { "One", "Two", "Three" }) };
+        lv = new ListView { Source = new ListWrapper<string> (["One", "Two", "Three"]) };
         Assert.NotNull (lv.Source);
         Assert.Equal (-1, lv.SelectedItem);
 
@@ -27,7 +29,7 @@ public class ListViewTests
 
         lv = new ListView
         {
-            Y = 1, Width = 10, Height = 20, Source = new ListWrapper (new List<string> { "One", "Two", "Three" })
+            Y = 1, Width = 10, Height = 20, Source = new ListWrapper<string> (["One", "Two", "Three"])
         };
         Assert.NotNull (lv.Source);
         Assert.Equal (-1, lv.SelectedItem);
@@ -43,14 +45,14 @@ public class ListViewTests
     [AutoInitShutdown]
     public void Ensures_Visibility_SelectedItem_On_MoveDown_And_MoveUp ()
     {
-        List<string> source = new ();
+        ObservableCollection<string> source = [];
 
         for (var i = 0; i < 20; i++)
         {
             source.Add ($"Line{i}");
         }
 
-        var lv = new ListView { Width = Dim.Fill (), Height = Dim.Fill (), Source = new ListWrapper (source) };
+        var lv = new ListView { Width = Dim.Fill (), Height = Dim.Fill (), Source = new ListWrapper<string> (source) };
         var win = new Window ();
         win.Add (lv);
         var top = new Toplevel ();
@@ -293,14 +295,14 @@ public class ListViewTests
     [AutoInitShutdown]
     public void EnsureSelectedItemVisible_SelectedItem ()
     {
-        List<string> source = new ();
+        ObservableCollection<string> source = [];
 
         for (var i = 0; i < 10; i++)
         {
             source.Add ($"Item {i}");
         }
 
-        var lv = new ListView { Width = 10, Height = 5, Source = new ListWrapper (source) };
+        var lv = new ListView { Width = 10, Height = 5, Source = new ListWrapper<string> (source) };
         var top = new Toplevel ();
         top.Add (lv);
         Application.Begin (top);
@@ -334,8 +336,8 @@ Item 6",
     [AutoInitShutdown]
     public void EnsureSelectedItemVisible_Top ()
     {
-        List<string> source = new () { "First", "Second" };
-        var lv = new ListView { Width = Dim.Fill (), Height = 1, Source = new ListWrapper (source) };
+        ObservableCollection<string> source = ["First", "Second"];
+        var lv = new ListView { Width = Dim.Fill (), Height = 1, Source = new ListWrapper<string> (source) };
         lv.SelectedItem = 1;
         var top = new Toplevel ();
         top.Add (lv);
@@ -366,8 +368,8 @@ Item 6",
     [Fact]
     public void KeyBindings_Command ()
     {
-        List<string> source = new () { "One", "Two", "Three" };
-        var lv = new ListView { Height = 2, AllowsMarking = true, Source = new ListWrapper (source) };
+        ObservableCollection<string> source = ["One", "Two", "Three"];
+        var lv = new ListView { Height = 2, AllowsMarking = true, Source = new ListWrapper<string> (source) };
         lv.BeginInit ();
         lv.EndInit ();
         Assert.Equal (-1, lv.SelectedItem);
@@ -424,8 +426,8 @@ Item 6",
     [Fact]
     public void Accept_Command_Accepts_and_Opens_Selected_Item ()
     {
-        List<string> source = ["One", "Two", "Three"];
-        var listView = new ListView { Source = new ListWrapper (source) };
+        ObservableCollection<string> source = ["One", "Two", "Three"];
+        var listView = new ListView { Source = new ListWrapper<string> (source) };
         listView.SelectedItem = 0;
 
         var accepted = false;
@@ -454,8 +456,8 @@ Item 6",
     [Fact]
     public void Accept_Cancel_Event_Prevents_OpenSelectedItem ()
     {
-        List<string> source = ["One", "Two", "Three"];
-        var listView = new ListView { Source = new ListWrapper (source) };
+        ObservableCollection<string> source = ["One", "Two", "Three"];
+        var listView = new ListView { Source = new ListWrapper<string> (source) };
         listView.SelectedItem = 0;
 
         var accepted = false;
@@ -493,7 +495,7 @@ Item 6",
     [Fact]
     public void ListViewProcessKeyReturnValue_WithMultipleCommands ()
     {
-        var lv = new ListView { Source = new ListWrapper (new List<string> { "One", "Two", "Three", "Four" }) };
+        var lv = new ListView { Source = new ListWrapper<string> (["One", "Two", "Three", "Four"]) };
 
         Assert.NotNull (lv.Source);
 
@@ -511,7 +513,7 @@ Item 6",
         Assert.Equal (1, lv.SelectedItem);
 
         // clear the items
-        lv.SetSource (null);
+        lv.SetSource<string> (null);
 
         // Press key combo again - return should be false this time as none of the Commands are allowable
         Assert.False (lv.NewKeyDownEvent (ev), "We cannot move down so will not respond to this");
@@ -520,7 +522,7 @@ Item 6",
     [Fact]
     public void ListViewSelectThenDown ()
     {
-        var lv = new ListView { Source = new ListWrapper (new List<string> { "One", "Two", "Three" }) };
+        var lv = new ListView { Source = new ListWrapper<string> (["One", "Two", "Three"]) };
         lv.AllowsMarking = true;
 
         Assert.NotNull (lv.Source);
@@ -584,7 +586,7 @@ Item 6",
     [Fact]
     public void ListWrapper_StartsWith ()
     {
-        var lw = new ListWrapper (new List<string> { "One", "Two", "Three" });
+        var lw = new ListWrapper<string> (["One", "Two", "Three"]);
 
         Assert.Equal (1, lw.StartsWith ("t"));
         Assert.Equal (1, lw.StartsWith ("tw"));
@@ -593,7 +595,7 @@ Item 6",
         Assert.Equal (1, lw.StartsWith ("TW"));
         Assert.Equal (2, lw.StartsWith ("TH"));
 
-        lw = new ListWrapper (new List<string> { "One", "Two", "Three" });
+        lw = new (["One", "Two", "Three"]);
 
         Assert.Equal (1, lw.StartsWith ("t"));
         Assert.Equal (1, lw.StartsWith ("tw"));
@@ -618,7 +620,7 @@ Item 6",
     public void RowRender_Event ()
     {
         var rendered = false;
-        List<string> source = new () { "one", "two", "three" };
+        ObservableCollection<string> source = ["one", "two", "three"];
         var lv = new ListView { Width = Dim.Fill (), Height = Dim.Fill () };
         lv.RowRender += (s, _) => rendered = true;
         var top = new Toplevel ();
@@ -634,7 +636,7 @@ Item 6",
     [Fact]
     public void SelectedItem_Get_Set ()
     {
-        var lv = new ListView { Source = new ListWrapper (new List<string> { "One", "Two", "Three" }) };
+        var lv = new ListView { Source = new ListWrapper<string> (["One", "Two", "Three"]) };
         Assert.Equal (-1, lv.SelectedItem);
         Assert.Throws<ArgumentException> (() => lv.SelectedItem = 3);
         Exception exception = Record.Exception (() => lv.SelectedItem = -1);
@@ -644,32 +646,34 @@ Item 6",
     [Fact]
     public void SetSource_Preserves_ListWrapper_Instance_If_Not_Null ()
     {
-        var lv = new ListView { Source = new ListWrapper (new List<string> { "One", "Two" }) };
+        var lv = new ListView { Source = new ListWrapper<string> (["One", "Two"]) };
 
         Assert.NotNull (lv.Source);
 
-        lv.SetSource (null);
+        lv.SetSource<string> (null);
         Assert.NotNull (lv.Source);
 
         lv.Source = null;
         Assert.Null (lv.Source);
 
-        lv = new ListView { Source = new ListWrapper (new List<string> { "One", "Two" }) };
+        lv = new ListView { Source = new ListWrapper<string> (["One", "Two"]) };
         Assert.NotNull (lv.Source);
 
-        lv.SetSourceAsync (null);
+        lv.SetSourceAsync<string> (null);
         Assert.NotNull (lv.Source);
     }
 
     [Fact]
     public void SettingEmptyKeybindingThrows ()
     {
-        var lv = new ListView { Source = new ListWrapper (new List<string> { "One", "Two", "Three" }) };
+        var lv = new ListView { Source = new ListWrapper<string> (["One", "Two", "Three"]) };
         Assert.Throws<ArgumentException> (() => lv.KeyBindings.Add (Key.Space));
     }
 
     private class NewListDataSource : IListDataSource
     {
+        /// <inheritdoc />
+        public event NotifyCollectionChangedEventHandler CollectionChanged;
         public int Count => 0;
         public int Length => 0;
         public bool IsMarked (int item) { throw new NotImplementedException (); }
@@ -703,7 +707,7 @@ Item 6",
             Width = 7,
             BorderStyle = LineStyle.Single
         };
-        lv.SetSource (new List<string> { "One", "Two", "Three", "Four" });
+        lv.SetSource (["One", "Two", "Three", "Four"]);
         lv.SelectedItemChanged += (s, e) => selected = e.Value.ToString ();
         var top = new Toplevel ();
         top.Add (lv);
@@ -756,7 +760,7 @@ Item 6",
     [AutoInitShutdown]
     public void LeftItem_TopItem_Tests ()
     {
-        var source = new List<string> ();
+        ObservableCollection<string> source = [];
         for (int i = 0; i < 5; i++) {
             source.Add ($"Item {i}");
         }
@@ -764,7 +768,7 @@ Item 6",
             X = 1,
             Width = 10,
             Height = 5,
-            Source = new ListWrapper (source)
+            Source = new ListWrapper<string> (source)
         };
         var top = new Toplevel ();
         top.Add (lv);

+ 6 - 5
UnitTests/Views/ScrollBarViewTests.cs

@@ -1,4 +1,5 @@
-using System.Reflection;
+using System.Collections.ObjectModel;
+using System.Reflection;
 using Xunit.Abstractions;
 
 namespace Terminal.Gui.ViewsTests;
@@ -370,7 +371,7 @@ This is a tes
 
                                               var win = new Window { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
 
-                                              List<string> source = new ();
+                                              ObservableCollection<string> source = [];
 
                                               for (var i = 0; i < 50; i++)
                                               {
@@ -391,7 +392,7 @@ This is a tes
                                                   Y = 0,
                                                   Width = Dim.Fill (),
                                                   Height = Dim.Fill (),
-                                                  Source = new ListWrapper (source)
+                                                  Source = new ListWrapper<string> (source)
                                               };
                                               win.Add (listView);
 
@@ -458,7 +459,7 @@ This is a tes
                                                     Application.Init (new FakeDriver ());
                                                     Toplevel top = new ();
                                                     var win = new Window { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
-                                                    List<string> source = new ();
+                                                    ObservableCollection<string> source = [];
 
                                                     for (var i = 0; i < 50; i++)
                                                     {
@@ -471,7 +472,7 @@ This is a tes
                                                         Y = 0,
                                                         Width = Dim.Fill (),
                                                         Height = Dim.Fill (),
-                                                        Source = new ListWrapper (source)
+                                                        Source = new ListWrapper<string> (source)
                                                     };
                                                     win.Add (listView);
                                                     var newScrollBarView = new ScrollBarView (listView, true, false) { KeepContentAlwaysInViewport = true };