|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Collections.ObjectModel; |
| 3 | +using System.Reactive.Linq; |
| 4 | +using System.Windows.Controls; |
| 5 | +using System.Windows.Input; |
| 6 | +using System.Windows.Media.Imaging; |
| 7 | +using GitHub.Tests.TestHelpers; |
| 8 | +using GitHub.UI; |
| 9 | +using GitHub.UI.Helpers; |
| 10 | +using Moq; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +public class AutoCompleteBoxTests |
| 14 | +{ |
| 15 | + public class TheItemsSourceProperty |
| 16 | + { |
| 17 | + [STAFact] |
| 18 | + public void SelectsFirstItemWhenSetToNonEmptyCollection() |
| 19 | + { |
| 20 | + var obs = Observable.Return(new BitmapImage()); |
| 21 | + |
| 22 | + var suggestions = new List<AutoCompleteSuggestion> |
| 23 | + { |
| 24 | + new AutoCompleteSuggestion("aaaa", obs, ":", ":"), |
| 25 | + new AutoCompleteSuggestion("bbbb", obs, ":", ":"), |
| 26 | + new AutoCompleteSuggestion("ccc", obs, ":", ":") |
| 27 | + }; |
| 28 | + var result = new AutoCompleteResult(1, new ReadOnlyCollection<AutoCompleteSuggestion>(suggestions)); |
| 29 | + var advisor = Mock.Of<IAutoCompleteAdvisor>( |
| 30 | + a => a.GetAutoCompletionSuggestions(Args.String, Args.Int32) == Observable.Return(result)); |
| 31 | + var textBox = new TextBox(); |
| 32 | + var autoCompleteBox = new AutoCompleteBox(Mock.Of<IDpiManager>()) |
| 33 | + { |
| 34 | + SelectionAdapter = new SelectorSelectionAdapter(new ListBox()), |
| 35 | + Advisor = advisor, |
| 36 | + TextBox = new TextBoxAutoCompleteTextInput { TextBox = textBox } |
| 37 | + }; |
| 38 | + |
| 39 | + textBox.Text = ":"; |
| 40 | + |
| 41 | + Assert.Equal("aaaa", ((AutoCompleteSuggestion) autoCompleteBox.SelectedItem).Name); |
| 42 | + Assert.Equal(":", autoCompleteBox.Text); // It should not have expanded it yet |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public class TheIsDropDownOpenProperty |
| 47 | + { |
| 48 | + [STAFact] |
| 49 | + public void IsTrueWhenTextBoxChangesWithPrefixedValue() |
| 50 | + { |
| 51 | + var obs = Observable.Return(new BitmapImage()); |
| 52 | + |
| 53 | + var suggestions = new List<AutoCompleteSuggestion> |
| 54 | + { |
| 55 | + new AutoCompleteSuggestion("aaaa", obs, ":", ":"), |
| 56 | + new AutoCompleteSuggestion("bbbb", obs, ":", ":"), |
| 57 | + new AutoCompleteSuggestion("ccc", obs, ":", ":") |
| 58 | + }; |
| 59 | + var result = new AutoCompleteResult(0, new ReadOnlyCollection<AutoCompleteSuggestion>(suggestions)); |
| 60 | + var advisor = Mock.Of<IAutoCompleteAdvisor>( |
| 61 | + a => a.GetAutoCompletionSuggestions(Args.String, Args.Int32) == Observable.Return(result)); |
| 62 | + var textBox = new TextBox(); |
| 63 | + var autoCompleteBox = new AutoCompleteBox(Mock.Of<IDpiManager>()) |
| 64 | + { |
| 65 | + SelectionAdapter = new SelectorSelectionAdapter(new ListBox()), |
| 66 | + Advisor = advisor, |
| 67 | + TextBox = new TextBoxAutoCompleteTextInput { TextBox = textBox } |
| 68 | + }; |
| 69 | + |
| 70 | + textBox.Text = ":"; |
| 71 | + |
| 72 | + Assert.True(autoCompleteBox.IsDropDownOpen); |
| 73 | + } |
| 74 | + |
| 75 | + [STAFact] |
| 76 | + public void IsFalseAfterASuggestionIsSelected() |
| 77 | + { |
| 78 | + var obs = Observable.Return(new BitmapImage()); |
| 79 | + |
| 80 | + var suggestions = new List<AutoCompleteSuggestion> |
| 81 | + { |
| 82 | + new AutoCompleteSuggestion("aaaa", obs, ":", ":"), |
| 83 | + new AutoCompleteSuggestion("bbbb", obs, ":", ":"), |
| 84 | + new AutoCompleteSuggestion("ccc", obs, ":", ":") |
| 85 | + }; |
| 86 | + var result = new AutoCompleteResult(2, new ReadOnlyCollection<AutoCompleteSuggestion>(suggestions)); |
| 87 | + var advisor = Mock.Of<IAutoCompleteAdvisor>( |
| 88 | + a => a.GetAutoCompletionSuggestions(Args.String, Args.Int32) == Observable.Return(result)); |
| 89 | + var selectionAdapter = new TestSelectorSelectionAdapter(); |
| 90 | + var textBox = new TextBox(); |
| 91 | + var autoCompleteBox = new AutoCompleteBox(Mock.Of<IDpiManager>()) |
| 92 | + { |
| 93 | + SelectionAdapter = selectionAdapter, |
| 94 | + Advisor = advisor, |
| 95 | + TextBox = new TextBoxAutoCompleteTextInput {TextBox = textBox} |
| 96 | + }; |
| 97 | + textBox.Text = "A :a"; |
| 98 | + textBox.CaretIndex = 4; |
| 99 | + Assert.Equal(4, textBox.CaretIndex); |
| 100 | + Assert.Equal(4, autoCompleteBox.TextBox.CaretIndex); |
| 101 | + Assert.True(autoCompleteBox.IsDropDownOpen); |
| 102 | + |
| 103 | + selectionAdapter.DoCommit(); |
| 104 | + |
| 105 | + Assert.Equal("A :aaaa: ", textBox.Text); |
| 106 | + Assert.False(autoCompleteBox.IsDropDownOpen); |
| 107 | + } |
| 108 | + |
| 109 | + [STAFact] |
| 110 | + public void IsFalseAfterASuggestionIsCancelled() |
| 111 | + { |
| 112 | + var obs = Observable.Return(new BitmapImage()); |
| 113 | + |
| 114 | + var suggestions = new List<AutoCompleteSuggestion> |
| 115 | + { |
| 116 | + new AutoCompleteSuggestion("aaaa", obs, ":", ":"), |
| 117 | + new AutoCompleteSuggestion("bbbb", obs, ":", ":"), |
| 118 | + new AutoCompleteSuggestion("ccc", obs, ":", ":") |
| 119 | + }; |
| 120 | + var result = new AutoCompleteResult(2, new ReadOnlyCollection<AutoCompleteSuggestion>(suggestions)); |
| 121 | + var advisor = Mock.Of<IAutoCompleteAdvisor>( |
| 122 | + a => a.GetAutoCompletionSuggestions(Args.String, Args.Int32) == Observable.Return(result)); |
| 123 | + var selectionAdapter = new TestSelectorSelectionAdapter(); |
| 124 | + var textBox = new TextBox(); |
| 125 | + var autoCompleteBox = new AutoCompleteBox(Mock.Of<IDpiManager>()) |
| 126 | + { |
| 127 | + SelectionAdapter = selectionAdapter, |
| 128 | + Advisor = advisor, |
| 129 | + TextBox = new TextBoxAutoCompleteTextInput { TextBox = textBox } |
| 130 | + }; |
| 131 | + textBox.Text = "A :a"; |
| 132 | + textBox.CaretIndex = 4; |
| 133 | + Assert.Equal(4, textBox.CaretIndex); |
| 134 | + Assert.Equal(4, autoCompleteBox.TextBox.CaretIndex); |
| 135 | + Assert.True(autoCompleteBox.IsDropDownOpen); |
| 136 | + |
| 137 | + selectionAdapter.DoCancel(); |
| 138 | + |
| 139 | + Assert.Equal("A :a", textBox.Text); |
| 140 | + Assert.False(autoCompleteBox.IsDropDownOpen); |
| 141 | + } |
| 142 | + |
| 143 | + [STAFact] |
| 144 | + public void HandlesKeyPressesToSelectAndCancelSelections() |
| 145 | + { |
| 146 | + var obs = Observable.Return(new BitmapImage()); |
| 147 | + |
| 148 | + var suggestions = new List<AutoCompleteSuggestion> |
| 149 | + { |
| 150 | + new AutoCompleteSuggestion("aaaa", obs, ":", ":"), |
| 151 | + new AutoCompleteSuggestion("bbbb", obs, ":", ":"), |
| 152 | + new AutoCompleteSuggestion("ccc", obs, ":", ":") |
| 153 | + }; |
| 154 | + var result = new AutoCompleteResult(2, new ReadOnlyCollection<AutoCompleteSuggestion>(suggestions)); |
| 155 | + var advisor = Mock.Of<IAutoCompleteAdvisor>( |
| 156 | + a => a.GetAutoCompletionSuggestions(Args.String, Args.Int32) == Observable.Return(result)); |
| 157 | + var selectionAdapter = new TestSelectorSelectionAdapter(); |
| 158 | + var textBox = new TextBox(); |
| 159 | + var autoCompleteBox = new AutoCompleteBox(Mock.Of<IDpiManager>()) |
| 160 | + { |
| 161 | + SelectionAdapter = selectionAdapter, |
| 162 | + Advisor = advisor, |
| 163 | + TextBox = new TextBoxAutoCompleteTextInput { TextBox = textBox } |
| 164 | + }; |
| 165 | + textBox.Text = "A :a"; |
| 166 | + textBox.CaretIndex = 4; |
| 167 | + Assert.Equal(4, textBox.CaretIndex); |
| 168 | + Assert.Equal(4, autoCompleteBox.TextBox.CaretIndex); |
| 169 | + Assert.True(autoCompleteBox.IsDropDownOpen); |
| 170 | + selectionAdapter.SelectorControl.SelectedIndex = 1; // Select the second item |
| 171 | + |
| 172 | + selectionAdapter.DoKeyDown(Key.Enter); |
| 173 | + |
| 174 | + Assert.Equal("A :bbbb: ", textBox.Text); |
| 175 | + Assert.False(autoCompleteBox.IsDropDownOpen); |
| 176 | + |
| 177 | + textBox.Text = "A :bbbb: :"; |
| 178 | + textBox.CaretIndex = 10; |
| 179 | + |
| 180 | + // Ensure we can re-open the dropdown |
| 181 | + Assert.True(autoCompleteBox.IsDropDownOpen); |
| 182 | + |
| 183 | + selectionAdapter.DoKeyDown(Key.Escape); |
| 184 | + Assert.False(autoCompleteBox.IsDropDownOpen); |
| 185 | + Assert.Equal("A :bbbb: :", textBox.Text); |
| 186 | + } |
| 187 | + |
| 188 | + class TestSelectorSelectionAdapter : SelectorSelectionAdapter |
| 189 | + { |
| 190 | + public TestSelectorSelectionAdapter() |
| 191 | + : base(new ListBox()) |
| 192 | + { |
| 193 | + } |
| 194 | + |
| 195 | + public void DoCommit() |
| 196 | + { |
| 197 | + base.OnCommit(); |
| 198 | + } |
| 199 | + |
| 200 | + public void DoCancel() |
| 201 | + { |
| 202 | + base.OnCancel(); |
| 203 | + } |
| 204 | + |
| 205 | + public void DoKeyDown(Key key) |
| 206 | + { |
| 207 | + var keyEventArgs = FakeKeyEventArgs.Create(key, false); |
| 208 | + HandleKeyDown(keyEventArgs); |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments