From 960cb394142df926363a454bf9c85ad7c3a4b531 Mon Sep 17 00:00:00 2001 From: Julien Lebosquain Date: Tue, 30 Dec 2025 10:10:37 +0100 Subject: [PATCH 1/3] Enable nullability in Controls.UnitTests (#20373) --- src/Avalonia.Controls/HotkeyManager.cs | 2 +- .../ApplicationTests.cs | 8 +- .../AutoCompleteBoxTests.cs | 105 ++++++++------- .../Avalonia.Controls.UnitTests.csproj | 1 + .../ButtonTests.cs | 15 ++- .../CalendarDatePickerTests.cs | 5 +- .../CalendarTests.cs | 22 ++-- .../CarouselTests.cs | 6 +- .../ComboBoxTests.cs | 24 ++-- .../ContentControlTests.cs | 45 +++---- .../ContextMenuTests.cs | 94 +++++++------- .../DatePickerTests.cs | 62 ++++----- .../DesktopStyleApplicationLifetimeTests.cs | 4 +- .../FlyoutTests.cs | 18 ++- .../Avalonia.Controls.UnitTests/GridTests.cs | 6 +- .../HeaderedItemsControlTests .cs | 2 +- .../HotKeyedControlsTests.cs | 16 +-- .../InlineTests.cs | 4 + .../ItemsSourceViewTests.cs | 17 ++- .../LayoutTransformControlTests.cs | 8 ++ .../ListBoxTests.cs | 93 ++++++------- .../ListBoxTests_Multiple.cs | 122 ++++++++++-------- .../ListBoxTests_Single.cs | 32 ++--- .../MaskedTextBoxTests.cs | 8 +- .../MenuItemTests.cs | 22 ++-- .../NameScopeTests.cs | 3 +- .../NumericUpDownTests.cs | 21 ++- .../DefaultMenuInteractionHandlerTests.cs | 17 ++- .../ContentPresenterTests_InTemplate.cs | 16 ++- .../ContentPresenterTests_Standalone.cs | 7 +- .../Presenters/ScrollContentPresenterTests.cs | 6 +- ...ontentPresenterTests_ILogicalScrollable.cs | 6 +- .../Presenters/TextPresenter_Tests.cs | 2 +- .../Primitives/PopupRootTests.cs | 47 +++---- .../Primitives/PopupTests.cs | 36 +++--- .../Primitives/RangeBaseTests.cs | 6 +- .../Primitives/ScrollBarTests.cs | 4 +- .../Primitives/SelectingItemsControlTests.cs | 66 +++++----- .../SelectingItemsControlTests_AutoSelect.cs | 4 +- ...electingItemsControlTests_SelectedValue.cs | 8 +- .../Primitives/TabStripTests.cs | 2 +- .../Primitives/TemplatedControlTests.cs | 6 +- .../ScrollViewerTests.cs | 8 +- .../SelectableTextBlockTests.cs | 4 +- .../Selection/InternalSelectionModelTests.cs | 14 +- .../Shapes/PathTests.cs | 10 ++ .../SplitButtonTests.cs | 6 +- .../SplitViewTests.cs | 10 +- .../TabControlTests.cs | 27 ++-- .../TextBlockTests.cs | 20 +-- .../TextBoxTests_DataValidation.cs | 25 ++-- .../TimePickerTests.cs | 58 +++------ .../ToolTipTests.cs | 34 ++--- .../TopLevelTests.cs | 21 +-- .../Utils/AncestorFinderTests.cs | 7 +- .../CollectionChangedEventManagerTests.cs | 2 +- .../Utils/HotKeyManagerTests.cs | 49 +++---- .../Utils/TestCommand.cs | 14 +- .../WindowBaseTests.cs | 16 +-- .../WindowTests.cs | 22 ++-- .../MockWindowingPlatform.cs | 4 +- 61 files changed, 694 insertions(+), 655 deletions(-) diff --git a/src/Avalonia.Controls/HotkeyManager.cs b/src/Avalonia.Controls/HotkeyManager.cs index 6ad4a8cc762..06ff9cb18c2 100644 --- a/src/Avalonia.Controls/HotkeyManager.cs +++ b/src/Avalonia.Controls/HotkeyManager.cs @@ -159,7 +159,7 @@ static HotKeyManager() new Manager(control).Init(); }); } - public static void SetHotKey(AvaloniaObject target, KeyGesture value) => target.SetValue(HotKeyProperty, value); + public static void SetHotKey(AvaloniaObject target, KeyGesture? value) => target.SetValue(HotKeyProperty, value); public static KeyGesture? GetHotKey(AvaloniaObject target) => target.GetValue(HotKeyProperty); } } diff --git a/tests/Avalonia.Controls.UnitTests/ApplicationTests.cs b/tests/Avalonia.Controls.UnitTests/ApplicationTests.cs index dbfe18f0b88..c69f0f49e25 100644 --- a/tests/Avalonia.Controls.UnitTests/ApplicationTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ApplicationTests.cs @@ -12,7 +12,7 @@ public void Throws_ArgumentNullException_On_Run_If_MainWindow_Is_Null() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { - Assert.Throws(() => { Application.Current.Run(null); }); + Assert.Throws(() => { Application.Current!.Run(null!); }); } } @@ -22,7 +22,7 @@ public void Raises_ResourcesChanged_When_Event_Handler_Added_After_Resources_Has // Test for #1765. using (UnitTestApplication.Start()) { - var resources = Application.Current.Resources; + var resources = Application.Current!.Resources; var raised = false; Application.Current.ResourcesChanged += (s, e) => raised = true; @@ -37,13 +37,13 @@ public void Can_Bind_To_DataContext() { using (UnitTestApplication.Start()) { - var application = Application.Current; + var application = Application.Current!; application.DataContext = "Test"; application.Bind(Application.NameProperty, new Binding(".")); - Assert.Equal("Test", Application.Current.Name); + Assert.Equal("Test", application.Name); } } } diff --git a/tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs b/tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs index 271d38f6a70..94324491fd8 100644 --- a/tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs +++ b/tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs @@ -21,60 +21,60 @@ public class AutoCompleteBoxTests : ScopedTestBase [Fact] public void Search_Filters() { - Assert.True(GetFilter(AutoCompleteFilterMode.Contains)("am", "name")); - Assert.True(GetFilter(AutoCompleteFilterMode.Contains)("AME", "name")); - Assert.False(GetFilter(AutoCompleteFilterMode.Contains)("hello", "name")); + Assert.True(GetNotNullFilter(AutoCompleteFilterMode.Contains)("am", "name")); + Assert.True(GetNotNullFilter(AutoCompleteFilterMode.Contains)("AME", "name")); + Assert.False(GetNotNullFilter(AutoCompleteFilterMode.Contains)("hello", "name")); - Assert.True(GetFilter(AutoCompleteFilterMode.ContainsCaseSensitive)("na", "name")); - Assert.False(GetFilter(AutoCompleteFilterMode.ContainsCaseSensitive)("AME", "name")); - Assert.False(GetFilter(AutoCompleteFilterMode.ContainsCaseSensitive)("hello", "name")); + Assert.True(GetNotNullFilter(AutoCompleteFilterMode.ContainsCaseSensitive)("na", "name")); + Assert.False(GetNotNullFilter(AutoCompleteFilterMode.ContainsCaseSensitive)("AME", "name")); + Assert.False(GetNotNullFilter(AutoCompleteFilterMode.ContainsCaseSensitive)("hello", "name")); Assert.Null(GetFilter(AutoCompleteFilterMode.Custom)); Assert.Null(GetFilter(AutoCompleteFilterMode.None)); - Assert.True(GetFilter(AutoCompleteFilterMode.Equals)("na", "na")); - Assert.True(GetFilter(AutoCompleteFilterMode.Equals)("na", "NA")); - Assert.False(GetFilter(AutoCompleteFilterMode.Equals)("hello", "name")); + Assert.True(GetNotNullFilter(AutoCompleteFilterMode.Equals)("na", "na")); + Assert.True(GetNotNullFilter(AutoCompleteFilterMode.Equals)("na", "NA")); + Assert.False(GetNotNullFilter(AutoCompleteFilterMode.Equals)("hello", "name")); - Assert.True(GetFilter(AutoCompleteFilterMode.EqualsCaseSensitive)("na", "na")); - Assert.False(GetFilter(AutoCompleteFilterMode.EqualsCaseSensitive)("na", "NA")); - Assert.False(GetFilter(AutoCompleteFilterMode.EqualsCaseSensitive)("hello", "name")); + Assert.True(GetNotNullFilter(AutoCompleteFilterMode.EqualsCaseSensitive)("na", "na")); + Assert.False(GetNotNullFilter(AutoCompleteFilterMode.EqualsCaseSensitive)("na", "NA")); + Assert.False(GetNotNullFilter(AutoCompleteFilterMode.EqualsCaseSensitive)("hello", "name")); - Assert.True(GetFilter(AutoCompleteFilterMode.StartsWith)("na", "name")); - Assert.True(GetFilter(AutoCompleteFilterMode.StartsWith)("NAM", "name")); - Assert.False(GetFilter(AutoCompleteFilterMode.StartsWith)("hello", "name")); + Assert.True(GetNotNullFilter(AutoCompleteFilterMode.StartsWith)("na", "name")); + Assert.True(GetNotNullFilter(AutoCompleteFilterMode.StartsWith)("NAM", "name")); + Assert.False(GetNotNullFilter(AutoCompleteFilterMode.StartsWith)("hello", "name")); - Assert.True(GetFilter(AutoCompleteFilterMode.StartsWithCaseSensitive)("na", "name")); - Assert.False(GetFilter(AutoCompleteFilterMode.StartsWithCaseSensitive)("NAM", "name")); - Assert.False(GetFilter(AutoCompleteFilterMode.StartsWithCaseSensitive)("hello", "name")); + Assert.True(GetNotNullFilter(AutoCompleteFilterMode.StartsWithCaseSensitive)("na", "name")); + Assert.False(GetNotNullFilter(AutoCompleteFilterMode.StartsWithCaseSensitive)("NAM", "name")); + Assert.False(GetNotNullFilter(AutoCompleteFilterMode.StartsWithCaseSensitive)("hello", "name")); } [Fact] public void Ordinal_Search_Filters() { - Assert.True(GetFilter(AutoCompleteFilterMode.ContainsOrdinal)("am", "name")); - Assert.True(GetFilter(AutoCompleteFilterMode.ContainsOrdinal)("AME", "name")); - Assert.False(GetFilter(AutoCompleteFilterMode.ContainsOrdinal)("hello", "name")); + Assert.True(GetNotNullFilter(AutoCompleteFilterMode.ContainsOrdinal)("am", "name")); + Assert.True(GetNotNullFilter(AutoCompleteFilterMode.ContainsOrdinal)("AME", "name")); + Assert.False(GetNotNullFilter(AutoCompleteFilterMode.ContainsOrdinal)("hello", "name")); - Assert.True(GetFilter(AutoCompleteFilterMode.ContainsOrdinalCaseSensitive)("na", "name")); - Assert.False(GetFilter(AutoCompleteFilterMode.ContainsOrdinalCaseSensitive)("AME", "name")); - Assert.False(GetFilter(AutoCompleteFilterMode.ContainsOrdinalCaseSensitive)("hello", "name")); + Assert.True(GetNotNullFilter(AutoCompleteFilterMode.ContainsOrdinalCaseSensitive)("na", "name")); + Assert.False(GetNotNullFilter(AutoCompleteFilterMode.ContainsOrdinalCaseSensitive)("AME", "name")); + Assert.False(GetNotNullFilter(AutoCompleteFilterMode.ContainsOrdinalCaseSensitive)("hello", "name")); - Assert.True(GetFilter(AutoCompleteFilterMode.EqualsOrdinal)("na", "na")); - Assert.True(GetFilter(AutoCompleteFilterMode.EqualsOrdinal)("na", "NA")); - Assert.False(GetFilter(AutoCompleteFilterMode.EqualsOrdinal)("hello", "name")); + Assert.True(GetNotNullFilter(AutoCompleteFilterMode.EqualsOrdinal)("na", "na")); + Assert.True(GetNotNullFilter(AutoCompleteFilterMode.EqualsOrdinal)("na", "NA")); + Assert.False(GetNotNullFilter(AutoCompleteFilterMode.EqualsOrdinal)("hello", "name")); - Assert.True(GetFilter(AutoCompleteFilterMode.EqualsOrdinalCaseSensitive)("na", "na")); - Assert.False(GetFilter(AutoCompleteFilterMode.EqualsOrdinalCaseSensitive)("na", "NA")); - Assert.False(GetFilter(AutoCompleteFilterMode.EqualsOrdinalCaseSensitive)("hello", "name")); + Assert.True(GetNotNullFilter(AutoCompleteFilterMode.EqualsOrdinalCaseSensitive)("na", "na")); + Assert.False(GetNotNullFilter(AutoCompleteFilterMode.EqualsOrdinalCaseSensitive)("na", "NA")); + Assert.False(GetNotNullFilter(AutoCompleteFilterMode.EqualsOrdinalCaseSensitive)("hello", "name")); - Assert.True(GetFilter(AutoCompleteFilterMode.StartsWithOrdinal)("na", "name")); - Assert.True(GetFilter(AutoCompleteFilterMode.StartsWithOrdinal)("NAM", "name")); - Assert.False(GetFilter(AutoCompleteFilterMode.StartsWithOrdinal)("hello", "name")); + Assert.True(GetNotNullFilter(AutoCompleteFilterMode.StartsWithOrdinal)("na", "name")); + Assert.True(GetNotNullFilter(AutoCompleteFilterMode.StartsWithOrdinal)("NAM", "name")); + Assert.False(GetNotNullFilter(AutoCompleteFilterMode.StartsWithOrdinal)("hello", "name")); - Assert.True(GetFilter(AutoCompleteFilterMode.StartsWithOrdinalCaseSensitive)("na", "name")); - Assert.False(GetFilter(AutoCompleteFilterMode.StartsWithOrdinalCaseSensitive)("NAM", "name")); - Assert.False(GetFilter(AutoCompleteFilterMode.StartsWithOrdinalCaseSensitive)("hello", "name")); + Assert.True(GetNotNullFilter(AutoCompleteFilterMode.StartsWithOrdinalCaseSensitive)("na", "name")); + Assert.False(GetNotNullFilter(AutoCompleteFilterMode.StartsWithOrdinalCaseSensitive)("NAM", "name")); + Assert.False(GetNotNullFilter(AutoCompleteFilterMode.StartsWithOrdinalCaseSensitive)("hello", "name")); } [Fact] @@ -261,7 +261,7 @@ public void Custom_Population_Supported() control.Populated += (s, e) => { populated = true; - ReadOnlyCollection collection = e.Data as ReadOnlyCollection; + var collection = e.Data as ReadOnlyCollection; populatedOk = collection != null && collection.Count == 1; }; @@ -328,14 +328,10 @@ public void Item_Search() RunTest((control, textbox) => { control.FilterMode = AutoCompleteFilterMode.Custom; - control.ItemFilter = (search, item) => - { - string s = item as string; - return s == null ? false : true; - }; + control.ItemFilter = (_, item) => item is string; // Just set to null briefly to exercise that code path - AutoCompleteFilterPredicate filter = control.ItemFilter; + var filter = control.ItemFilter; Assert.NotNull(filter); control.ItemFilter = null; Assert.Null(control.ItemFilter); @@ -377,6 +373,8 @@ public void Custom_TextSelector() { RunTest((control, textbox) => { + Assert.NotNull(control.ItemsSource); + object selectedItem = control.ItemsSource.Cast().First(); string input = "42"; @@ -394,6 +392,8 @@ public void Custom_ItemSelector() { RunTest((control, textbox) => { + Assert.NotNull(control.ItemsSource); + object selectedItem = control.ItemsSource.Cast().First(); string input = "42"; @@ -416,8 +416,8 @@ public void Text_Validation() control.Bind(AutoCompleteBox.TextProperty, textObservable); Dispatcher.UIThread.RunJobs(); - Assert.Equal(DataValidationErrors.GetHasErrors(control), true); - Assert.Equal(DataValidationErrors.GetErrors(control).SequenceEqual(new[] { exception }), true); + Assert.True(DataValidationErrors.GetHasErrors(control)); + Assert.Equal([exception], DataValidationErrors.GetErrors(control)); }); } @@ -453,8 +453,8 @@ public void SelectedItem_Validation() control.Bind(AutoCompleteBox.SelectedItemProperty, itemObservable); Dispatcher.UIThread.RunJobs(); - Assert.Equal(DataValidationErrors.GetHasErrors(control), true); - Assert.Equal(DataValidationErrors.GetErrors(control).SequenceEqual(new[] { exception }), true); + Assert.True(DataValidationErrors.GetHasErrors(control)); + Assert.Equal([exception], DataValidationErrors.GetErrors(control)); }); } @@ -587,12 +587,19 @@ public void Opening_Context_Menu_Does_not_Lose_Selection() /// /// The FilterMode of interest. /// Returns the predicate instance. - private static AutoCompleteFilterPredicate GetFilter(AutoCompleteFilterMode mode) + private static AutoCompleteFilterPredicate? GetFilter(AutoCompleteFilterMode mode) { return new AutoCompleteBox { FilterMode = mode } .TextFilter; } + private static AutoCompleteFilterPredicate GetNotNullFilter(AutoCompleteFilterMode mode) + { + var filter = GetFilter(mode); + Assert.NotNull(filter); + return filter; + } + /// /// Creates a large list of strings for AutoCompleteBox testing. /// @@ -1200,7 +1207,7 @@ private void RunTest(Action test) var window = new Window {Content = control}; window.ApplyStyling(); window.ApplyTemplate(); - window.Presenter.ApplyTemplate(); + window.Presenter!.ApplyTemplate(); Dispatcher.UIThread.RunJobs(); test.Invoke(control, textBox); } diff --git a/tests/Avalonia.Controls.UnitTests/Avalonia.Controls.UnitTests.csproj b/tests/Avalonia.Controls.UnitTests/Avalonia.Controls.UnitTests.csproj index dcff6ea10ab..8344d6389d8 100644 --- a/tests/Avalonia.Controls.UnitTests/Avalonia.Controls.UnitTests.csproj +++ b/tests/Avalonia.Controls.UnitTests/Avalonia.Controls.UnitTests.csproj @@ -13,6 +13,7 @@ + diff --git a/tests/Avalonia.Controls.UnitTests/ButtonTests.cs b/tests/Avalonia.Controls.UnitTests/ButtonTests.cs index ac6dd42fd3d..5309dc9f844 100644 --- a/tests/Avalonia.Controls.UnitTests/ButtonTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ButtonTests.cs @@ -212,7 +212,7 @@ public void Button_With_RenderTransform_Raises_Click() var pt = new Point(150, 50); renderer.Setup(r => r.HitTest(It.IsAny(), It.IsAny(), It.IsAny>())) .Returns>((p, r, f) => - r.Bounds.Contains(p.Transform(r.RenderTransform.Value.Invert())) ? + r.Bounds.Contains(p.Transform(r.RenderTransform!.Value.Invert())) ? new Visual[] { r } : new Visual[0]); using var _ = UnitTestApplication.Start(TestServices.StyledWindow); @@ -336,9 +336,9 @@ public void Raises_Click_When_AccessKey_Raised() }; root.ApplyTemplate(); - root.Presenter.UpdateChild(); + root.Presenter!.UpdateChild(); target.ApplyTemplate(); - target.Presenter.UpdateChild(); + target.Presenter!.UpdateChild(); kd.SetFocusedElement(target, NavigationMethod.Unspecified, KeyModifiers.None); Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded); @@ -535,7 +535,7 @@ public void Button_IsCancel_Should_Not_Work_When_Button_Is_Not_Effectively_Visib public void Button_CommandParameter_Does_Not_Change_While_Execution() { var target = new Button(); - object lastParamenter = "A"; + object? lastParamenter = "A"; var generator = new Random(); var onlyOnce = false; var command = new TestCommand(parameter => @@ -582,12 +582,12 @@ void Should_Not_Fire_Click_Event_On_Space_Key_When_It_Is_Not_Focus() } } - private KeyEventArgs CreateKeyDownEvent(Key key, Interactive source = null) + private KeyEventArgs CreateKeyDownEvent(Key key, Interactive? source = null) { return new KeyEventArgs { RoutedEvent = InputElement.KeyDownEvent, Key = key, Source = source }; } - private KeyEventArgs CreateKeyUpEvent(Key key, Interactive source = null) + private KeyEventArgs CreateKeyUpEvent(Key key, Interactive? source = null) { return new KeyEventArgs { RoutedEvent = InputElement.KeyUpEvent, Key = key, Source = source }; } @@ -682,6 +682,7 @@ public void Button_LetterSpacing_Updates_ContentPresenter_When_Changed() button.ApplyTemplate(); var presenter = button.Presenter; + Assert.NotNull(presenter); button.LetterSpacing = 5.0; @@ -758,7 +759,7 @@ private class TestTopLevel : TopLevel private readonly ILayoutManager _layoutManager; public bool IsClosed { get; private set; } - public TestTopLevel(ITopLevelImpl impl, ILayoutManager layoutManager = null) + public TestTopLevel(ITopLevelImpl impl, ILayoutManager? layoutManager = null) : base(impl) { _layoutManager = layoutManager ?? new LayoutManager(this); diff --git a/tests/Avalonia.Controls.UnitTests/CalendarDatePickerTests.cs b/tests/Avalonia.Controls.UnitTests/CalendarDatePickerTests.cs index 9e720c66c8a..21ef53d04bc 100644 --- a/tests/Avalonia.Controls.UnitTests/CalendarDatePickerTests.cs +++ b/tests/Avalonia.Controls.UnitTests/CalendarDatePickerTests.cs @@ -44,6 +44,7 @@ public void Setting_Selected_Date_To_Blackout_Date_Should_Throw() using (UnitTestApplication.Start(Services)) { CalendarDatePicker datePicker = CreateControl(); + Assert.NotNull(datePicker.BlackoutDates); datePicker.BlackoutDates.AddDatesInPast(); DateTime goodValue = DateTime.Today.AddDays(1); @@ -65,7 +66,7 @@ public void Adding_Blackout_Dates_Containing_Selected_Date_Should_Throw() datePicker.SelectedDate = DateTime.Today.AddDays(5); Assert.ThrowsAny( - () => datePicker.BlackoutDates.Add(new CalendarDateRange(DateTime.Today, DateTime.Today.AddDays(10)))); + () => datePicker.BlackoutDates!.Add(new CalendarDateRange(DateTime.Today, DateTime.Today.AddDays(10)))); } } @@ -86,7 +87,7 @@ public void Setting_Date_Manually_With_CustomDateFormatString_Should_Be_Accepted RaiseKeyEvent(tb, Key.Enter, KeyModifiers.None); Assert.Equal("17.10.2024", datePicker.Text); - Assert.True(CompareDates(datePicker.SelectedDate.Value, new DateTime(2024, 10, 17))); + Assert.True(CompareDates(datePicker.SelectedDate!.Value, new DateTime(2024, 10, 17))); tb.Clear(); RaiseTextEvent(tb, "12.10.2024"); diff --git a/tests/Avalonia.Controls.UnitTests/CalendarTests.cs b/tests/Avalonia.Controls.UnitTests/CalendarTests.cs index 864f92f5112..5050285b0bd 100644 --- a/tests/Avalonia.Controls.UnitTests/CalendarTests.cs +++ b/tests/Avalonia.Controls.UnitTests/CalendarTests.cs @@ -143,12 +143,12 @@ public void Display_Date_Range_End_Will_Contain_SelectedDate() /// /// The days added to the SelectedDates collection. /// - private IList _selectedDatesChangedAddedDays; + private IList? _selectedDatesChangedAddedDays; /// /// The days removed from the SelectedDates collection. /// - private IList _selectedDateChangedRemovedDays; + private IList? _selectedDatesChangedRemovedDays; /// /// The number of times the SelectedDatesChanged event has been fired. @@ -160,13 +160,13 @@ public void Display_Date_Range_End_Will_Contain_SelectedDate() /// /// The calendar. /// Event arguments. - private void OnSelectedDatesChanged(object sender, SelectionChangedEventArgs e) + private void OnSelectedDatesChanged(object? sender, SelectionChangedEventArgs e) { _selectedDatesChangedAddedDays = e.AddedItems .Cast() .ToList(); - _selectedDateChangedRemovedDays = + _selectedDatesChangedRemovedDays = e.RemovedItems .Cast() .ToList(); @@ -183,9 +183,9 @@ private void ResetSelectedDatesChanged() _selectedDatesChangedAddedDays.Clear(); } - if (_selectedDateChangedRemovedDays != null) + if (_selectedDatesChangedRemovedDays != null) { - _selectedDateChangedRemovedDays.Clear(); + _selectedDatesChangedRemovedDays.Clear(); } _selectedDatesChangedCount = 0; @@ -203,8 +203,10 @@ public void SingleDate_Selection_Behavior() Assert.True(calendar.SelectedDates.Count == 1); Assert.True(CompareDates(calendar.SelectedDates[0], DateTime.Today)); Assert.True(_selectedDatesChangedCount == 1); + Assert.NotNull(_selectedDatesChangedAddedDays); Assert.True(_selectedDatesChangedAddedDays.Count == 1); - Assert.True(_selectedDateChangedRemovedDays.Count == 0); + Assert.NotNull(_selectedDatesChangedRemovedDays); + Assert.True(_selectedDatesChangedRemovedDays.Count == 0); ResetSelectedDatesChanged(); calendar.SelectedDate = DateTime.Today; @@ -241,8 +243,10 @@ public void SingleRange_Selection_Behavior() Assert.True(calendar.SelectedDates.Count == 1); Assert.True(CompareDates(calendar.SelectedDates[0], DateTime.Today)); Assert.True(_selectedDatesChangedCount == 1); + Assert.NotNull(_selectedDatesChangedAddedDays); Assert.True(_selectedDatesChangedAddedDays.Count == 1); - Assert.True(_selectedDateChangedRemovedDays.Count == 0); + Assert.NotNull(_selectedDatesChangedRemovedDays); + Assert.True(_selectedDatesChangedRemovedDays.Count == 0); ResetSelectedDatesChanged(); calendar.SelectedDates.Clear(); @@ -264,7 +268,7 @@ public void SingleRange_Selection_Behavior() Assert.True(calendar.SelectedDates.Count == 21); Assert.True(_selectedDatesChangedCount == 1); Assert.True(_selectedDatesChangedAddedDays.Count == 21); - Assert.True(_selectedDateChangedRemovedDays.Count == 11); + Assert.True(_selectedDatesChangedRemovedDays.Count == 11); ResetSelectedDatesChanged(); calendar.SelectedDates.Add(DateTime.Today.AddDays(100)); diff --git a/tests/Avalonia.Controls.UnitTests/CarouselTests.cs b/tests/Avalonia.Controls.UnitTests/CarouselTests.cs index 4bcfe3009fd..85ba254b5db 100644 --- a/tests/Avalonia.Controls.UnitTests/CarouselTests.cs +++ b/tests/Avalonia.Controls.UnitTests/CarouselTests.cs @@ -257,7 +257,7 @@ public void SelectedItem_Validation() target.Bind(ComboBox.SelectedItemProperty, textObservable); Assert.True(DataValidationErrors.GetHasErrors(target)); - Assert.True(DataValidationErrors.GetErrors(target).SequenceEqual(new[] { exception })); + Assert.Equal([exception], DataValidationErrors.GetErrors(target)); } } @@ -336,7 +336,7 @@ private static void Prepare(Carousel target) private static void Layout(Carousel target) { - ((ILayoutRoot)target.GetVisualRoot()).LayoutManager.ExecuteLayoutPass(); + ((ILayoutRoot)target.GetVisualRoot()!).LayoutManager.ExecuteLayoutPass(); } private static IControlTemplate CarouselTemplate() @@ -371,7 +371,7 @@ private static FuncControlTemplate ScrollViewerTemplate() }); } - private static TextBlock GetContainerTextBlock(object control) + private static TextBlock GetContainerTextBlock(object? control) { var contentPresenter = Assert.IsType(control); return Assert.IsType(contentPresenter.Child); diff --git a/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs b/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs index ef52ea8d9f5..ca1fa66d910 100644 --- a/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs @@ -81,7 +81,7 @@ public void WrapSelection_Should_Work() }; var root = new TestRoot(target); target.ApplyTemplate(); - ((Control)target.Presenter).ApplyTemplate(); + target.Presenter!.ApplyTemplate(); target.Focus(); Assert.Equal(target.SelectedIndex, -1); Assert.True(target.IsFocused); @@ -117,7 +117,7 @@ public void Focuses_Next_Item_On_Key_Down() }; var root = new TestRoot(target); target.ApplyTemplate(); - ((Control)target.Presenter).ApplyTemplate(); + target.Presenter!.ApplyTemplate(); target.Focus(); Assert.Equal(target.SelectedIndex, -1); Assert.True(target.IsFocused); @@ -166,9 +166,10 @@ public void SelectionBoxItem_Rectangle_Is_Removed_From_Logical_Tree() var root = new TestRoot { Child = target }; target.ApplyTemplate(); - ((Control)target.Presenter).ApplyTemplate(); + target.Presenter!.ApplyTemplate(); var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle; + Assert.NotNull(rectangle); Assert.True(((ILogical)target).IsAttachedToLogicalTree); Assert.True(((ILogical)rectangle).IsAttachedToLogicalTree); @@ -202,7 +203,7 @@ private static FuncControlTemplate GetTemplate() Content = new ItemsPresenter { Name = "PART_ItemsPresenter", - ItemsPanel = new FuncTemplate(() => new VirtualizingStackPanel()), + ItemsPanel = new FuncTemplate(() => new VirtualizingStackPanel()), }.RegisterInNameScope(scope) }.RegisterInNameScope(scope) }.RegisterInNameScope(scope), @@ -234,7 +235,7 @@ public void Detaching_Closed_ComboBox_Keeps_Current_Focus() var root = new TestRoot { Child = panel = new StackPanel { Children = { target, other } } }; target.ApplyTemplate(); - target.Presenter.ApplyTemplate(); + target.Presenter!.ApplyTemplate(); other.Focus(); @@ -379,14 +380,14 @@ public void SelectedItem_Validation() }; target.ApplyTemplate(); - target.Presenter.ApplyTemplate(); + target.Presenter!.ApplyTemplate(); var exception = new System.InvalidCastException("failed validation"); var textObservable = new BehaviorSubject(new BindingNotification(exception, BindingErrorType.DataValidationError)); target.Bind(ComboBox.SelectedItemProperty, textObservable); Assert.True(DataValidationErrors.GetHasErrors(target)); - Assert.True(DataValidationErrors.GetErrors(target).SequenceEqual(new[] { exception })); + Assert.Equal([exception], DataValidationErrors.GetErrors(target)); } } @@ -473,7 +474,7 @@ public void FlowDirection_Of_RectangleContent_Should_Be_LeftToRight() target.SelectedIndex = 0; var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle; - + Assert.NotNull(rectangle); Assert.Equal(FlowDirection.LeftToRight, rectangle.FlowDirection); } @@ -503,6 +504,7 @@ public void FlowDirection_Of_RectangleContent_Updated_After_InvalidateMirrorTran target.SelectedIndex = 0; var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle; + Assert.NotNull(rectangle); Assert.Equal(FlowDirection.LeftToRight, rectangle.FlowDirection); parentContent.FlowDirection = FlowDirection.RightToLeft; @@ -539,6 +541,7 @@ public void FlowDirection_Of_RectangleContent_Updated_After_OpenPopup() target.SelectedIndex = 0; var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle; + Assert.NotNull(rectangle); Assert.Equal(FlowDirection.LeftToRight, rectangle.FlowDirection); parentContent.FlowDirection = FlowDirection.RightToLeft; @@ -807,7 +810,7 @@ public void When_Editable_And_Item_Selected_Via_Text_Then_Focus_Swaps_Via_Tab_Sw keyboardNavHandler.SetOwner(root); target.ApplyTemplate(); - target.Presenter.ApplyTemplate(); + target.Presenter!.ApplyTemplate(); var containerPanel = target.GetTemplateChildren().OfType().FirstOrDefault(x => x.Name == "container"); var editableTextBox = containerPanel?.GetVisualDescendants().OfType().FirstOrDefault(x => x.Name == "PART_EditableTextBox"); @@ -815,6 +818,9 @@ public void When_Editable_And_Item_Selected_Via_Text_Then_Focus_Swaps_Via_Tab_Sw var popupScrollViewer = popup?.Child as ScrollViewer; var scrollViewerItemsPresenter = popupScrollViewer?.Content as ItemsPresenter; var popupVirtualizingStackPanel = scrollViewerItemsPresenter?.GetVisualDescendants().OfType().FirstOrDefault(); + + Assert.NotNull(editableTextBox); + Assert.NotNull(scrollViewerItemsPresenter); Assert.NotNull(popupVirtualizingStackPanel); //force the popup to render the ComboBoxItem(s) as they are what get set as "focused" if this test fails diff --git a/tests/Avalonia.Controls.UnitTests/ContentControlTests.cs b/tests/Avalonia.Controls.UnitTests/ContentControlTests.cs index 883f8cfd94e..30d789f08f2 100644 --- a/tests/Avalonia.Controls.UnitTests/ContentControlTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ContentControlTests.cs @@ -24,7 +24,7 @@ public void Template_Should_Be_Instantiated() target.Content = "Foo"; target.Template = GetTemplate(); target.ApplyTemplate(); - target.Presenter.UpdateChild(); + target.Presenter!.UpdateChild(); var child = target.VisualChildren.Single(); Assert.IsType(child); @@ -55,7 +55,7 @@ public void Templated_Children_Should_Be_Styled() root.Child = target; target.ApplyTemplate(); - target.Presenter.ApplyTemplate(); + target.Presenter!.ApplyTemplate(); foreach (Control child in target.GetTemplateChildren()) Assert.Equal("foo", child.Tag); @@ -70,9 +70,10 @@ public void ContentPresenter_Should_Have_TemplatedParent_Set() target.Template = GetTemplate(); target.Content = child; target.ApplyTemplate(); - target.Presenter.UpdateChild(); + target.Presenter!.UpdateChild(); var contentPresenter = child.GetVisualParent(); + Assert.NotNull(contentPresenter); Assert.Equal(target, contentPresenter.TemplatedParent); } @@ -85,7 +86,7 @@ public void Content_Should_Have_TemplatedParent_Set_To_Null() target.Template = GetTemplate(); target.Content = child; target.ApplyTemplate(); - target.Presenter.UpdateChild(); + target.Presenter!.UpdateChild(); Assert.Null(child.TemplatedParent); } @@ -117,7 +118,7 @@ public void Control_Content_Should_Be_Logical_Child_After_ApplyTemplate() var child = new Control(); target.Content = child; target.ApplyTemplate(); - target.Presenter.UpdateChild(); + target.Presenter!.UpdateChild(); Assert.Equal(child.Parent, target); Assert.Equal(child.GetLogicalParent(), target); @@ -135,7 +136,7 @@ public void Should_Use_ContentTemplate_To_Create_Control() target.Content = "Foo"; target.ApplyTemplate(); - target.Presenter.UpdateChild(); + target.Presenter!.UpdateChild(); var child = target.Presenter.Child; @@ -152,7 +153,7 @@ public void DataTemplate_Created_Control_Should_Be_Logical_Child_After_ApplyTemp target.Content = "Foo"; target.ApplyTemplate(); - target.Presenter.UpdateChild(); + target.Presenter!.UpdateChild(); var child = target.Presenter.Child; @@ -192,7 +193,7 @@ public void Setting_Content_Should_Fire_LogicalChildren_CollectionChanged() target.Template = GetTemplate(); target.Content = child; target.ApplyTemplate(); - target.Presenter.UpdateChild(); + target.Presenter!.UpdateChild(); Assert.True(called); } @@ -207,7 +208,7 @@ public void Clearing_Content_Should_Fire_LogicalChildren_CollectionChanged() target.Template = GetTemplate(); target.Content = child; target.ApplyTemplate(); - target.Presenter.UpdateChild(); + target.Presenter!.UpdateChild(); ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true; @@ -228,7 +229,7 @@ public void Changing_Content_Should_Fire_LogicalChildren_CollectionChanged() target.Template = GetTemplate(); target.Content = child1; target.ApplyTemplate(); - target.Presenter.UpdateChild(); + target.Presenter!.UpdateChild(); ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true; @@ -245,14 +246,14 @@ public void Changing_Content_Should_Update_Presenter() target.Template = GetTemplate(); target.ApplyTemplate(); - target.Presenter.UpdateChild(); + target.Presenter!.UpdateChild(); target.Content = "Foo"; target.Presenter.UpdateChild(); - Assert.Equal("Foo", ((TextBlock)target.Presenter.Child).Text); + Assert.Equal("Foo", ((TextBlock)target.Presenter.Child!).Text); target.Content = "Bar"; target.Presenter.UpdateChild(); - Assert.Equal("Bar", ((TextBlock)target.Presenter.Child).Text); + Assert.Equal("Bar", ((TextBlock)target.Presenter.Child!).Text); } [Fact] @@ -263,9 +264,9 @@ public void DataContext_Should_Be_Set_For_DataTemplate_Created_Content() target.Template = GetTemplate(); target.Content = "Foo"; target.ApplyTemplate(); - target.Presenter.UpdateChild(); + target.Presenter!.UpdateChild(); - Assert.Equal("Foo", target.Presenter.Child.DataContext); + Assert.Equal("Foo", target.Presenter.Child!.DataContext); } [Fact] @@ -276,16 +277,16 @@ public void DataContext_Should_Not_Be_Set_For_Control_Content() target.Template = GetTemplate(); target.Content = new TextBlock(); target.ApplyTemplate(); - target.Presenter.UpdateChild(); + target.Presenter!.UpdateChild(); - Assert.Null(target.Presenter.Child.DataContext); + Assert.Null(target.Presenter.Child!.DataContext); } [Fact] public void Binding_ContentTemplate_After_Content_Does_Not_Leave_Orpaned_TextBlock() { // Test for #1271. - var children = new List(); + var children = new List(); var presenter = new ContentPresenter(); // The content and then the content template property need to be bound with delayed bindings @@ -328,7 +329,7 @@ public void Binding_ContentTemplate_After_Content_Does_Not_Leave_Orpaned_TextBlo x => Assert.IsType(x), x => Assert.IsType(x)); - var textBlock = (TextBlock)children[1]; + var textBlock = (TextBlock)children[1]!; // The leak in #1271 was caused by the TextBlock's logical parent not being cleared when // it is replaced by the Canvas. @@ -356,9 +357,9 @@ public void Should_Set_Child_LogicalParent_After_Removing_And_Adding_Back_To_Log target.Content = "Foo"; target.ApplyTemplate(); - target.Presenter.ApplyTemplate(); + target.Presenter!.ApplyTemplate(); - Assert.Equal(target, target.Presenter.Child.GetLogicalParent()); + Assert.Equal(target, target.Presenter!.Child!.GetLogicalParent()); Assert.Equal(new[] { target.Presenter.Child }, target.LogicalChildren); root.Child = null; @@ -370,7 +371,7 @@ public void Should_Set_Child_LogicalParent_After_Removing_And_Adding_Back_To_Log root.Child = target; target.Content = "Bar"; - Assert.Equal(target, target.Presenter.Child.GetLogicalParent()); + Assert.Equal(target, target.Presenter.Child!.GetLogicalParent()); Assert.Equal(new[] { target.Presenter.Child }, target.LogicalChildren); } diff --git a/tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs b/tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs index b487818be69..dcfd7d8ab2c 100644 --- a/tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs @@ -1,11 +1,9 @@ using System; - +using System.Diagnostics.CodeAnalysis; using Avalonia.Controls.Primitives; using Avalonia.Input; using Avalonia.Markup.Xaml; using Avalonia.Platform; -using Avalonia.Rendering; -using Avalonia.Rendering.Composition; using Avalonia.UnitTests; using Moq; @@ -15,7 +13,7 @@ namespace Avalonia.Controls.UnitTests { public class ContextMenuTests : ScopedTestBase { - private Mock popupImpl; + private Mock? _popupImpl; private MouseTestHelper _mouse = new MouseTestHelper(); [Fact] @@ -32,7 +30,7 @@ public void ContextRequested_Opens_ContextMenu() var window = new Window { Content = target }; window.ApplyStyling(); window.ApplyTemplate(); - ((Control)window.Presenter).ApplyTemplate(); + window.Presenter!.ApplyTemplate(); int openedCount = 0; @@ -65,7 +63,7 @@ public void ContextMenu_Is_Opened_When_ContextFlyout_Is_Also_Set() var window = new Window { Content = target }; window.ApplyStyling(); window.ApplyTemplate(); - ((Control)window.Presenter).ApplyTemplate(); + window.Presenter!.ApplyTemplate(); target.RaiseEvent(new ContextRequestedEventArgs()); @@ -135,7 +133,7 @@ public void Opening_Raises_Single_Opened_Event() var window = new Window { Content = target }; window.ApplyStyling(); window.ApplyTemplate(); - ((Control)window.Presenter).ApplyTemplate(); + window.Presenter!.ApplyTemplate(); int openedCount = 0; @@ -164,7 +162,7 @@ public void Open_Should_Use_Default_Control() var window = new Window { Content = target }; window.ApplyStyling(); window.ApplyTemplate(); - ((Control)window.Presenter).ApplyTemplate(); + window.Presenter!.ApplyTemplate(); bool opened = false; @@ -193,7 +191,7 @@ public void Open_Should_Raise_Exception_If_AlreadyDetached() var window = new Window { Content = target }; window.ApplyStyling(); window.ApplyTemplate(); - ((Control)window.Presenter).ApplyTemplate(); + window.Presenter!.ApplyTemplate(); target.ContextMenu = null; @@ -215,7 +213,7 @@ public void Closing_Raises_Single_Closed_Event() var window = new Window { Content = target }; window.ApplyStyling(); window.ApplyTemplate(); - ((Control)window.Presenter).ApplyTemplate(); + window.Presenter!.ApplyTemplate(); sut.Open(target); @@ -237,8 +235,8 @@ public void Cancel_Light_Dismiss_Closing_Keeps_Flyout_Open() { using (Application()) { - popupImpl.Setup(x => x.Show(true, false)).Verifiable(); - popupImpl.Setup(x => x.Hide()).Verifiable(); + _popupImpl.Setup(x => x.Show(true, false)).Verifiable(); + _popupImpl.Setup(x => x.Hide()).Verifiable(); var window = PreparedWindow(); window.Width = 100; @@ -268,14 +266,15 @@ public void Cancel_Light_Dismiss_Closing_Keeps_Flyout_Open() c.Open(button); var overlay = LightDismissOverlayLayer.GetLightDismissOverlayLayer(window); + Assert.NotNull(overlay); _mouse.Down(overlay, MouseButton.Left, new Point(90, 90)); _mouse.Up(button, MouseButton.Left, new Point(90, 90)); Assert.Equal(1, tracker); Assert.True(c.IsOpen); - popupImpl.Verify(x => x.Hide(), Times.Never); - popupImpl.Verify(x => x.Show(true, false), Times.Exactly(1)); + _popupImpl.Verify(x => x.Hide(), Times.Never); + _popupImpl.Verify(x => x.Show(true, false), Times.Exactly(1)); } } @@ -284,8 +283,8 @@ public void Light_Dismiss_Closes_Flyout() { using (Application()) { - popupImpl.Setup(x => x.Show(true, false)).Verifiable(); - popupImpl.Setup(x => x.Hide()).Verifiable(); + _popupImpl.Setup(x => x.Show(true, false)).Verifiable(); + _popupImpl.Setup(x => x.Hide()).Verifiable(); var window = PreparedWindow(); window.Width = 100; @@ -308,12 +307,13 @@ public void Light_Dismiss_Closes_Flyout() c.Open(button); var overlay = LightDismissOverlayLayer.GetLightDismissOverlayLayer(window); + Assert.NotNull(overlay); _mouse.Down(overlay, MouseButton.Left, new Point(90, 90)); _mouse.Up(button, MouseButton.Left, new Point(90, 90)); Assert.False(c.IsOpen); - popupImpl.Verify(x => x.Hide(), Times.Exactly(1)); - popupImpl.Verify(x => x.Show(true, false), Times.Exactly(1)); + _popupImpl.Verify(x => x.Hide(), Times.Exactly(1)); + _popupImpl.Verify(x => x.Show(true, false), Times.Exactly(1)); } } @@ -322,8 +322,8 @@ public void Clicking_On_Control_Toggles_ContextMenu() { using (Application()) { - popupImpl.Setup(x => x.Show(true, false)).Verifiable(); - popupImpl.Setup(x => x.Hide()).Verifiable(); + _popupImpl.Setup(x => x.Show(true, false)).Verifiable(); + _popupImpl.Setup(x => x.Hide()).Verifiable(); var sut = new ContextMenu(); var target = new Panel @@ -334,6 +334,7 @@ public void Clicking_On_Control_Toggles_ContextMenu() var window = PreparedWindow(target); window.Show(); var overlay = LightDismissOverlayLayer.GetLightDismissOverlayLayer(window); + Assert.NotNull(overlay); _mouse.Click(target, MouseButton.Right); @@ -343,8 +344,8 @@ public void Clicking_On_Control_Toggles_ContextMenu() _mouse.Up(target); Assert.False(sut.IsOpen); - popupImpl.Verify(x => x.Show(true, false), Times.Once); - popupImpl.Verify(x => x.Hide(), Times.Once); + _popupImpl.Verify(x => x.Show(true, false), Times.Once); + _popupImpl.Verify(x => x.Hide(), Times.Once); } } @@ -353,8 +354,8 @@ public void Right_Clicking_On_Control_Twice_Re_Opens_ContextMenu() { using (Application()) { - popupImpl.Setup(x => x.Show(true, false)).Verifiable(); - popupImpl.Setup(x => x.Hide()).Verifiable(); + _popupImpl.Setup(x => x.Show(true, false)).Verifiable(); + _popupImpl.Setup(x => x.Hide()).Verifiable(); var sut = new ContextMenu(); var target = new Panel @@ -366,6 +367,7 @@ public void Right_Clicking_On_Control_Twice_Re_Opens_ContextMenu() window.Show(); var overlay = LightDismissOverlayLayer.GetLightDismissOverlayLayer(window); + Assert.NotNull(overlay); _mouse.Click(target, MouseButton.Right); Assert.True(sut.IsOpen); @@ -374,8 +376,8 @@ public void Right_Clicking_On_Control_Twice_Re_Opens_ContextMenu() _mouse.Up(target, MouseButton.Right); Assert.True(sut.IsOpen); - popupImpl.Verify(x => x.Hide(), Times.Once); - popupImpl.Verify(x => x.Show(true, false), Times.Exactly(2)); + _popupImpl.Verify(x => x.Hide(), Times.Once); + _popupImpl.Verify(x => x.Show(true, false), Times.Exactly(2)); } } @@ -400,7 +402,7 @@ public void Context_Menu_Can_Be_Shared_Between_Controls_Even_After_A_Control_Is_ window.ApplyStyling(); window.ApplyTemplate(); - ((Control)window.Presenter).ApplyTemplate(); + window.Presenter!.ApplyTemplate(); _mouse.Click(target1, MouseButton.Right); @@ -421,7 +423,7 @@ public void Cancelling_Opening_Does_Not_Show_ContextMenu() { using (Application()) { - popupImpl.Setup(x => x.Show(true, false)).Verifiable(); + _popupImpl.Setup(x => x.Show(true, false)).Verifiable(); bool eventCalled = false; var sut = new ContextMenu(); @@ -437,7 +439,7 @@ public void Cancelling_Opening_Does_Not_Show_ContextMenu() Assert.True(eventCalled); Assert.False(sut.IsOpen); - popupImpl.Verify(x => x.Show(true, false), Times.Never); + _popupImpl.Verify(x => x.Show(true, false), Times.Never); } } @@ -496,8 +498,8 @@ public void Context_Menu_In_Resources_Can_Be_Shared() "; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); - var target1 = window.Find("target1"); - var target2 = window.Find("target2"); + var target1 = window.Get("target1"); + var target2 = window.Get("target2"); var mouse = new MouseTestHelper(); Assert.NotNull(target1.ContextMenu); @@ -539,8 +541,8 @@ public void Context_Menu_Can_Be_Set_In_Style() "; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); - var target1 = window.Find("target1"); - var target2 = window.Find("target2"); + var target1 = window.Get("target1"); + var target2 = window.Get("target2"); var mouse = new MouseTestHelper(); Assert.NotNull(target1.ContextMenu); @@ -562,8 +564,8 @@ public void Cancelling_Closing_Leaves_ContextMenuOpen() { using (Application()) { - popupImpl.Setup(x => x.Show(true, false)).Verifiable(); - popupImpl.Setup(x => x.Hide()).Verifiable(); + _popupImpl.Setup(x => x.Show(true, false)).Verifiable(); + _popupImpl.Setup(x => x.Hide()).Verifiable(); bool eventCalled = false; var sut = new ContextMenu(); @@ -574,6 +576,7 @@ public void Cancelling_Closing_Leaves_ContextMenuOpen() var window = PreparedWindow(target); var overlay = LightDismissOverlayLayer.GetLightDismissOverlayLayer(window); + Assert.NotNull(overlay); sut.Closing += (c, e) => { eventCalled = true; e.Cancel = true; }; @@ -589,8 +592,8 @@ public void Cancelling_Closing_Leaves_ContextMenuOpen() Assert.True(eventCalled); Assert.True(sut.IsOpen); - popupImpl.Verify(x => x.Show(true, false), Times.Once()); - popupImpl.Verify(x => x.Hide(), Times.Never); + _popupImpl.Verify(x => x.Show(true, false), Times.Once()); + _popupImpl.Verify(x => x.Hide(), Times.Never); } } @@ -599,8 +602,8 @@ public void Closing_Should_Restore_Focus() { using (Application()) { - popupImpl.Setup(x => x.Show(true, false)).Verifiable(); - popupImpl.Setup(x => x.Hide()).Verifiable(); + _popupImpl.Setup(x => x.Show(true, false)).Verifiable(); + _popupImpl.Setup(x => x.Hide()).Verifiable(); var item = new MenuItem(); var sut = new ContextMenu @@ -643,7 +646,7 @@ public void Closing_Should_Restore_Focus() } } - private static Window PreparedWindow(object content = null) + private static Window PreparedWindow(object? content = null) { var platform = AvaloniaLocator.Current.GetRequiredService(); @@ -653,10 +656,11 @@ private static Window PreparedWindow(object content = null) var w = new Window(windowImpl.Object) { Content = content }; w.ApplyStyling(); w.ApplyTemplate(); - ((Control)w.Presenter).ApplyTemplate(); + w.Presenter!.ApplyTemplate(); return w; } + [MemberNotNull(nameof(_popupImpl))] private IDisposable Application() { var screen = new PixelRect(new PixelPoint(), new PixelSize(100, 100)); @@ -665,9 +669,9 @@ private IDisposable Application() screenImpl.Setup(X => X.AllScreens).Returns( new[] { new Screen(1, screen, screen, true) }); var windowImpl = MockWindowingPlatform.CreateWindowMock(); - popupImpl = MockWindowingPlatform.CreatePopupMock(windowImpl.Object); - popupImpl.SetupGet(x => x.RenderScaling).Returns(1); - windowImpl.Setup(x => x.CreatePopup()).Returns(popupImpl.Object); + _popupImpl = MockWindowingPlatform.CreatePopupMock(windowImpl.Object); + _popupImpl.SetupGet(x => x.RenderScaling).Returns(1); + windowImpl.Setup(x => x.CreatePopup()).Returns(_popupImpl.Object); windowImpl.Setup(x => x.TryGetFeature(It.Is(t => t == typeof(IScreenImpl)))).Returns(screenImpl.Object); @@ -675,7 +679,7 @@ private IDisposable Application() keyboardDevice: () => new KeyboardDevice(), inputManager: new InputManager(), windowImpl: windowImpl.Object, - windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object, x => popupImpl.Object)); + windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object, x => _popupImpl.Object)); return UnitTestApplication.Start(services); } diff --git a/tests/Avalonia.Controls.UnitTests/DatePickerTests.cs b/tests/Avalonia.Controls.UnitTests/DatePickerTests.cs index dd34455162e..3ec2a3a55d0 100644 --- a/tests/Avalonia.Controls.UnitTests/DatePickerTests.cs +++ b/tests/Avalonia.Controls.UnitTests/DatePickerTests.cs @@ -50,13 +50,10 @@ public void DayVisible_False_Should_Hide_Day() var desc = datePicker.GetVisualDescendants(); Assert.True(desc.Count() > 1);//Should be layoutroot grid & button - TextBlock dayText = null; - Grid container = null; + TextBlock? dayText = null; - Assert.True(desc.ElementAt(1) is Button); - - container = (desc.ElementAt(1) as Button).Content as Grid; - Assert.True(container != null); + var button = Assert.IsAssignableFrom