- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.4k
ListDetailsView unfocus content on SelectedIndex changed. #4255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            michael-hawker
  merged 4 commits into
  CommunityToolkit:main
from
Rosuavio:bugfix/listdetailsview-unfocus-content
  
      
      
   
  Sep 16, 2021 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      d21f12d
              
                ListDetialsView: Unit test for item details losing focus
              
              
                Rosuavio 377a884
              
                Rewrite test to be deterministic
              
              
                michael-hawker 9c410d1
              
                ListDetailsView: fix MainList refrence when focusing the list
              
              
                Rosuavio 394fbdb
              
                ListDetailsView: Update the focus based on the ViewSate on SelectedIt…
              
              
                Rosuavio File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            112 changes: 112 additions & 0 deletions
          
          112 
        
  UnitTests/UnitTests.UWP/UI/Controls/Test_ListDetailsView_UI.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|  | ||
| using Microsoft.Toolkit.Uwp; | ||
| using Microsoft.Toolkit.Uwp.UI; | ||
| using Microsoft.Toolkit.Uwp.UI.Controls; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System; | ||
| using System.Collections.ObjectModel; | ||
| using System.Threading.Tasks; | ||
| using Windows.UI.Xaml; | ||
| using Windows.UI.Xaml.Controls; | ||
| using Windows.UI.Xaml.Input; | ||
| using Windows.UI.Xaml.Markup; | ||
|  | ||
| namespace UnitTests.UWP.UI.Controls | ||
| { | ||
| [TestClass] | ||
| public class Test_ListDetailsView_UI : VisualUITestBase | ||
| { | ||
| private const string SampleXaml = @"<controls:ListDetailsView | ||
| xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" | ||
| xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" | ||
| xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"" | ||
| NoSelectionContent=""No item selected"" > | ||
| <controls:ListDetailsView.ItemTemplate> | ||
| <DataTemplate> | ||
| <TextBlock Text=""Item"" /> | ||
| </DataTemplate> | ||
| </controls:ListDetailsView.ItemTemplate> | ||
| <controls:ListDetailsView.DetailsTemplate> | ||
| <DataTemplate> | ||
| <TextBox Text=""{Binding}"" /> | ||
| </DataTemplate> | ||
| </controls:ListDetailsView.DetailsTemplate> | ||
| </controls:ListDetailsView>"; | ||
|  | ||
| [TestCategory("ListDetailsView")] | ||
| [TestMethod] | ||
| public async Task Test_LoseFocusOnNoSelection() | ||
| { | ||
| await App.DispatcherQueue.EnqueueAsync(async () => | ||
| { | ||
| var listDetailsView = XamlReader.Load(SampleXaml) as ListDetailsView; | ||
|  | ||
| listDetailsView.ItemsSource = new ObservableCollection<string> | ||
| { | ||
| "First", | ||
| }; | ||
|  | ||
| listDetailsView.SelectedIndex = 0; | ||
|  | ||
| await SetTestContentAsync(listDetailsView); | ||
|  | ||
| var firsttb = listDetailsView.FindDescendant<TextBox>(); | ||
|  | ||
| await App.DispatcherQueue.EnqueueAsync(() => firsttb.Focus(FocusState.Programmatic)); | ||
|  | ||
| Assert.AreEqual(firsttb, FocusManager.GetFocusedElement(), "TextBox didn't get focus"); | ||
|  | ||
| var tcs = new TaskCompletionSource<bool>(); | ||
|  | ||
| firsttb.LostFocus += (s, e) => tcs.SetResult(true); | ||
|  | ||
| listDetailsView.SelectedIndex = -1; | ||
|  | ||
| await Task.WhenAny(tcs.Task, Task.Delay(2000)); | ||
|  | ||
| Assert.IsTrue(tcs.Task.IsCompleted); | ||
| Assert.IsTrue(tcs.Task.Result, "TextBox in the first item should have lost focus."); | ||
| }); | ||
| } | ||
|  | ||
| [TestCategory("ListDetailsView")] | ||
| [TestMethod] | ||
| public async Task Test_LoseFocusOnSelectOther() | ||
| { | ||
| await App.DispatcherQueue.EnqueueAsync(async () => | ||
| { | ||
| var listDetailsView = XamlReader.Load(SampleXaml) as ListDetailsView; | ||
|  | ||
| listDetailsView.ItemsSource = new ObservableCollection<string> | ||
| { | ||
| "First", | ||
| "Second", | ||
| }; | ||
|  | ||
| listDetailsView.SelectedIndex = 0; | ||
|  | ||
| await SetTestContentAsync(listDetailsView); | ||
|  | ||
| var firsttb = listDetailsView.FindDescendant<TextBox>(); | ||
|  | ||
| await App.DispatcherQueue.EnqueueAsync(() => firsttb.Focus(FocusState.Programmatic)); | ||
|  | ||
| Assert.AreEqual(firsttb, FocusManager.GetFocusedElement(), "TextBox didn't get focus"); | ||
|  | ||
| var tcs = new TaskCompletionSource<bool>(); | ||
|  | ||
| firsttb.LostFocus += (s, e) => tcs.SetResult(true); | ||
|  | ||
| listDetailsView.SelectedIndex = 1; | ||
|  | ||
| await Task.WhenAny(tcs.Task, Task.Delay(2000)); | ||
|  | ||
| Assert.IsTrue(tcs.Task.IsCompleted); | ||
| Assert.IsTrue(tcs.Task.Result, "TextBox in the first item should have lost focus."); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When first running both of these unit tests, they both failed. After running each of them in the Visual Studio debugger, and having them fail, again, they finally did pass, after the next pass.