|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using Microsoft.Toolkit.Uwp; |
| 6 | +using Microsoft.Toolkit.Uwp.UI; |
| 7 | +using Microsoft.Toolkit.Uwp.UI.Controls; |
| 8 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 9 | +using System; |
| 10 | +using System.Collections.ObjectModel; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using Windows.UI.Xaml; |
| 13 | +using Windows.UI.Xaml.Controls; |
| 14 | +using Windows.UI.Xaml.Input; |
| 15 | +using Windows.UI.Xaml.Markup; |
| 16 | + |
| 17 | +namespace UnitTests.UWP.UI.Controls |
| 18 | +{ |
| 19 | + [TestClass] |
| 20 | + public class Test_ListDetailsView_UI : VisualUITestBase |
| 21 | + { |
| 22 | + private const string SampleXaml = @"<controls:ListDetailsView |
| 23 | + xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" |
| 24 | + xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" |
| 25 | + xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"" |
| 26 | + NoSelectionContent=""No item selected"" > |
| 27 | + <controls:ListDetailsView.ItemTemplate> |
| 28 | + <DataTemplate> |
| 29 | + <TextBlock Text=""Item"" /> |
| 30 | + </DataTemplate> |
| 31 | + </controls:ListDetailsView.ItemTemplate> |
| 32 | + <controls:ListDetailsView.DetailsTemplate> |
| 33 | + <DataTemplate> |
| 34 | + <TextBox Text=""{Binding}"" /> |
| 35 | + </DataTemplate> |
| 36 | + </controls:ListDetailsView.DetailsTemplate> |
| 37 | + </controls:ListDetailsView>"; |
| 38 | + |
| 39 | + [TestCategory("ListDetailsView")] |
| 40 | + [TestMethod] |
| 41 | + public async Task Test_LoseFocusOnNoSelection() |
| 42 | + { |
| 43 | + await App.DispatcherQueue.EnqueueAsync(async () => |
| 44 | + { |
| 45 | + var listDetailsView = XamlReader.Load(SampleXaml) as ListDetailsView; |
| 46 | + |
| 47 | + listDetailsView.ItemsSource = new ObservableCollection<string> |
| 48 | + { |
| 49 | + "First", |
| 50 | + }; |
| 51 | + |
| 52 | + listDetailsView.SelectedIndex = 0; |
| 53 | + |
| 54 | + await SetTestContentAsync(listDetailsView); |
| 55 | + |
| 56 | + var firsttb = listDetailsView.FindDescendant<TextBox>(); |
| 57 | + |
| 58 | + await App.DispatcherQueue.EnqueueAsync(() => firsttb.Focus(FocusState.Programmatic)); |
| 59 | + |
| 60 | + Assert.AreEqual(firsttb, FocusManager.GetFocusedElement(), "TextBox didn't get focus"); |
| 61 | + |
| 62 | + var tcs = new TaskCompletionSource<bool>(); |
| 63 | + |
| 64 | + firsttb.LostFocus += (s, e) => tcs.SetResult(true); |
| 65 | + |
| 66 | + listDetailsView.SelectedIndex = -1; |
| 67 | + |
| 68 | + await Task.WhenAny(tcs.Task, Task.Delay(2000)); |
| 69 | + |
| 70 | + Assert.IsTrue(tcs.Task.IsCompleted); |
| 71 | + Assert.IsTrue(tcs.Task.Result, "TextBox in the first item should have lost focus."); |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + [TestCategory("ListDetailsView")] |
| 76 | + [TestMethod] |
| 77 | + public async Task Test_LoseFocusOnSelectOther() |
| 78 | + { |
| 79 | + await App.DispatcherQueue.EnqueueAsync(async () => |
| 80 | + { |
| 81 | + var listDetailsView = XamlReader.Load(SampleXaml) as ListDetailsView; |
| 82 | + |
| 83 | + listDetailsView.ItemsSource = new ObservableCollection<string> |
| 84 | + { |
| 85 | + "First", |
| 86 | + "Second", |
| 87 | + }; |
| 88 | + |
| 89 | + listDetailsView.SelectedIndex = 0; |
| 90 | + |
| 91 | + await SetTestContentAsync(listDetailsView); |
| 92 | + |
| 93 | + var firsttb = listDetailsView.FindDescendant<TextBox>(); |
| 94 | + |
| 95 | + await App.DispatcherQueue.EnqueueAsync(() => firsttb.Focus(FocusState.Programmatic)); |
| 96 | + |
| 97 | + Assert.AreEqual(firsttb, FocusManager.GetFocusedElement(), "TextBox didn't get focus"); |
| 98 | + |
| 99 | + var tcs = new TaskCompletionSource<bool>(); |
| 100 | + |
| 101 | + firsttb.LostFocus += (s, e) => tcs.SetResult(true); |
| 102 | + |
| 103 | + listDetailsView.SelectedIndex = 1; |
| 104 | + |
| 105 | + await Task.WhenAny(tcs.Task, Task.Delay(2000)); |
| 106 | + |
| 107 | + Assert.IsTrue(tcs.Task.IsCompleted); |
| 108 | + Assert.IsTrue(tcs.Task.Result, "TextBox in the first item should have lost focus."); |
| 109 | + }); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments