AutoCompleteBoxTests.cs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Avalonia.Controls.Primitives;
  5. using Avalonia.Controls.Templates;
  6. using Avalonia.Data;
  7. using Avalonia.Threading;
  8. using Avalonia.UnitTests;
  9. using Xunit;
  10. using System.Collections.ObjectModel;
  11. using System.Reactive.Subjects;
  12. using Avalonia.Headless;
  13. using Avalonia.Input;
  14. using Avalonia.Platform;
  15. using Moq;
  16. namespace Avalonia.Controls.UnitTests
  17. {
  18. public class AutoCompleteBoxTests : ScopedTestBase
  19. {
  20. [Fact]
  21. public void Search_Filters()
  22. {
  23. Assert.True(GetFilter(AutoCompleteFilterMode.Contains)("am", "name"));
  24. Assert.True(GetFilter(AutoCompleteFilterMode.Contains)("AME", "name"));
  25. Assert.False(GetFilter(AutoCompleteFilterMode.Contains)("hello", "name"));
  26. Assert.True(GetFilter(AutoCompleteFilterMode.ContainsCaseSensitive)("na", "name"));
  27. Assert.False(GetFilter(AutoCompleteFilterMode.ContainsCaseSensitive)("AME", "name"));
  28. Assert.False(GetFilter(AutoCompleteFilterMode.ContainsCaseSensitive)("hello", "name"));
  29. Assert.Null(GetFilter(AutoCompleteFilterMode.Custom));
  30. Assert.Null(GetFilter(AutoCompleteFilterMode.None));
  31. Assert.True(GetFilter(AutoCompleteFilterMode.Equals)("na", "na"));
  32. Assert.True(GetFilter(AutoCompleteFilterMode.Equals)("na", "NA"));
  33. Assert.False(GetFilter(AutoCompleteFilterMode.Equals)("hello", "name"));
  34. Assert.True(GetFilter(AutoCompleteFilterMode.EqualsCaseSensitive)("na", "na"));
  35. Assert.False(GetFilter(AutoCompleteFilterMode.EqualsCaseSensitive)("na", "NA"));
  36. Assert.False(GetFilter(AutoCompleteFilterMode.EqualsCaseSensitive)("hello", "name"));
  37. Assert.True(GetFilter(AutoCompleteFilterMode.StartsWith)("na", "name"));
  38. Assert.True(GetFilter(AutoCompleteFilterMode.StartsWith)("NAM", "name"));
  39. Assert.False(GetFilter(AutoCompleteFilterMode.StartsWith)("hello", "name"));
  40. Assert.True(GetFilter(AutoCompleteFilterMode.StartsWithCaseSensitive)("na", "name"));
  41. Assert.False(GetFilter(AutoCompleteFilterMode.StartsWithCaseSensitive)("NAM", "name"));
  42. Assert.False(GetFilter(AutoCompleteFilterMode.StartsWithCaseSensitive)("hello", "name"));
  43. }
  44. [Fact]
  45. public void Ordinal_Search_Filters()
  46. {
  47. Assert.True(GetFilter(AutoCompleteFilterMode.ContainsOrdinal)("am", "name"));
  48. Assert.True(GetFilter(AutoCompleteFilterMode.ContainsOrdinal)("AME", "name"));
  49. Assert.False(GetFilter(AutoCompleteFilterMode.ContainsOrdinal)("hello", "name"));
  50. Assert.True(GetFilter(AutoCompleteFilterMode.ContainsOrdinalCaseSensitive)("na", "name"));
  51. Assert.False(GetFilter(AutoCompleteFilterMode.ContainsOrdinalCaseSensitive)("AME", "name"));
  52. Assert.False(GetFilter(AutoCompleteFilterMode.ContainsOrdinalCaseSensitive)("hello", "name"));
  53. Assert.True(GetFilter(AutoCompleteFilterMode.EqualsOrdinal)("na", "na"));
  54. Assert.True(GetFilter(AutoCompleteFilterMode.EqualsOrdinal)("na", "NA"));
  55. Assert.False(GetFilter(AutoCompleteFilterMode.EqualsOrdinal)("hello", "name"));
  56. Assert.True(GetFilter(AutoCompleteFilterMode.EqualsOrdinalCaseSensitive)("na", "na"));
  57. Assert.False(GetFilter(AutoCompleteFilterMode.EqualsOrdinalCaseSensitive)("na", "NA"));
  58. Assert.False(GetFilter(AutoCompleteFilterMode.EqualsOrdinalCaseSensitive)("hello", "name"));
  59. Assert.True(GetFilter(AutoCompleteFilterMode.StartsWithOrdinal)("na", "name"));
  60. Assert.True(GetFilter(AutoCompleteFilterMode.StartsWithOrdinal)("NAM", "name"));
  61. Assert.False(GetFilter(AutoCompleteFilterMode.StartsWithOrdinal)("hello", "name"));
  62. Assert.True(GetFilter(AutoCompleteFilterMode.StartsWithOrdinalCaseSensitive)("na", "name"));
  63. Assert.False(GetFilter(AutoCompleteFilterMode.StartsWithOrdinalCaseSensitive)("NAM", "name"));
  64. Assert.False(GetFilter(AutoCompleteFilterMode.StartsWithOrdinalCaseSensitive)("hello", "name"));
  65. }
  66. [Fact]
  67. public void Fires_DropDown_Events()
  68. {
  69. RunTest((control, textbox) =>
  70. {
  71. bool openEvent = false;
  72. bool closeEvent = false;
  73. control.DropDownOpened += (s, e) => openEvent = true;
  74. control.DropDownClosed += (s, e) => closeEvent = true;
  75. control.ItemsSource = CreateSimpleStringArray();
  76. textbox.Text = "a";
  77. Dispatcher.UIThread.RunJobs();
  78. Assert.True(control.SearchText == "a");
  79. Assert.True(control.IsDropDownOpen);
  80. Assert.True(openEvent);
  81. textbox.Text = String.Empty;
  82. Dispatcher.UIThread.RunJobs();
  83. Assert.True(control.SearchText == String.Empty);
  84. Assert.False(control.IsDropDownOpen);
  85. Assert.True(closeEvent);
  86. });
  87. }
  88. [Fact]
  89. public void Custom_FilterMode_Without_ItemFilter_Setting_Throws_Exception()
  90. {
  91. RunTest((control, textbox) =>
  92. {
  93. control.FilterMode = AutoCompleteFilterMode.Custom;
  94. Assert.Throws<Exception>(() => { control.Text = "a"; });
  95. });
  96. }
  97. [Fact]
  98. public void Text_Completion_Via_Text_Property()
  99. {
  100. RunTest((control, textbox) =>
  101. {
  102. control.IsTextCompletionEnabled = true;
  103. Assert.Equal(String.Empty, control.Text);
  104. control.Text = "close";
  105. Assert.NotNull(control.SelectedItem);
  106. });
  107. }
  108. [Fact]
  109. public void Text_Completion_Selects_Text()
  110. {
  111. RunTest((control, textbox) =>
  112. {
  113. control.IsTextCompletionEnabled = true;
  114. textbox.Text = "ac";
  115. textbox.SelectionEnd = textbox.SelectionStart = 2;
  116. Dispatcher.UIThread.RunJobs();
  117. Assert.True(control.IsDropDownOpen);
  118. Assert.True(Math.Abs(textbox.SelectionEnd - textbox.SelectionStart) > 2);
  119. });
  120. }
  121. [Fact]
  122. public void TextChanged_Event_Fires()
  123. {
  124. RunTest((control, textbox) =>
  125. {
  126. bool textChanged = false;
  127. control.TextChanged += (s, e) => textChanged = true;
  128. textbox.Text = "a";
  129. Dispatcher.UIThread.RunJobs();
  130. Assert.True(textChanged);
  131. textChanged = false;
  132. control.Text = "conversati";
  133. Dispatcher.UIThread.RunJobs();
  134. Assert.True(textChanged);
  135. textChanged = false;
  136. control.Text = null;
  137. Dispatcher.UIThread.RunJobs();
  138. Assert.True(textChanged);
  139. });
  140. }
  141. [Fact]
  142. public void MinimumPrefixLength_Works()
  143. {
  144. RunTest((control, textbox) =>
  145. {
  146. textbox.Text = "a";
  147. Dispatcher.UIThread.RunJobs();
  148. Assert.True(control.IsDropDownOpen);
  149. textbox.Text = String.Empty;
  150. Dispatcher.UIThread.RunJobs();
  151. Assert.False(control.IsDropDownOpen);
  152. control.MinimumPrefixLength = 3;
  153. textbox.Text = "a";
  154. Dispatcher.UIThread.RunJobs();
  155. Assert.False(control.IsDropDownOpen);
  156. textbox.Text = "acc";
  157. Dispatcher.UIThread.RunJobs();
  158. Assert.True(control.IsDropDownOpen);
  159. });
  160. }
  161. [Fact]
  162. public void Can_Cancel_DropDown_Opening()
  163. {
  164. RunTest((control, textbox) =>
  165. {
  166. control.DropDownOpening += (s, e) => e.Cancel = true;
  167. textbox.Text = "a";
  168. Dispatcher.UIThread.RunJobs();
  169. Assert.False(control.IsDropDownOpen);
  170. });
  171. }
  172. [Fact]
  173. public void Can_Cancel_DropDown_Closing()
  174. {
  175. RunTest((control, textbox) =>
  176. {
  177. control.DropDownClosing += (s, e) => e.Cancel = true;
  178. textbox.Text = "a";
  179. Dispatcher.UIThread.RunJobs();
  180. Assert.True(control.IsDropDownOpen);
  181. control.IsDropDownOpen = false;
  182. Assert.True(control.IsDropDownOpen);
  183. });
  184. }
  185. [Fact]
  186. public void Can_Cancel_Population()
  187. {
  188. RunTest((control, textbox) =>
  189. {
  190. bool populating = false;
  191. bool populated = false;
  192. control.FilterMode = AutoCompleteFilterMode.None;
  193. control.Populating += (s, e) =>
  194. {
  195. e.Cancel = true;
  196. populating = true;
  197. };
  198. control.Populated += (s, e) => populated = true;
  199. textbox.Text = "accounti";
  200. Dispatcher.UIThread.RunJobs();
  201. Assert.True(populating);
  202. Assert.False(populated);
  203. });
  204. }
  205. [Fact]
  206. public void Custom_Population_Supported()
  207. {
  208. RunTest((control, textbox) =>
  209. {
  210. string custom = "Custom!";
  211. string search = "accounti";
  212. bool populated = false;
  213. bool populatedOk = false;
  214. control.FilterMode = AutoCompleteFilterMode.None;
  215. control.Populating += (s, e) =>
  216. {
  217. control.ItemsSource = new string[] { custom };
  218. Assert.Equal(search, e.Parameter);
  219. };
  220. control.Populated += (s, e) =>
  221. {
  222. populated = true;
  223. ReadOnlyCollection<object> collection = e.Data as ReadOnlyCollection<object>;
  224. populatedOk = collection != null && collection.Count == 1;
  225. };
  226. textbox.Text = search;
  227. Dispatcher.UIThread.RunJobs();
  228. Assert.True(populated);
  229. Assert.True(populatedOk);
  230. });
  231. }
  232. [Fact]
  233. public void Text_Completion()
  234. {
  235. RunTest((control, textbox) =>
  236. {
  237. control.IsTextCompletionEnabled = true;
  238. textbox.Text = "accounti";
  239. textbox.SelectionStart = textbox.SelectionEnd = textbox.Text.Length;
  240. Dispatcher.UIThread.RunJobs();
  241. Assert.Equal("accounti", control.SearchText);
  242. Assert.Equal("accounting", textbox.Text);
  243. });
  244. }
  245. [Fact]
  246. public void String_Search()
  247. {
  248. RunTest((control, textbox) =>
  249. {
  250. textbox.Text = "a";
  251. Dispatcher.UIThread.RunJobs();
  252. Assert.Equal(textbox.Text, control.Text);
  253. textbox.Text = "acc";
  254. Dispatcher.UIThread.RunJobs();
  255. Assert.Equal(textbox.Text, control.Text);
  256. textbox.Text = "a";
  257. Dispatcher.UIThread.RunJobs();
  258. Assert.Equal(textbox.Text, control.Text);
  259. textbox.Text = "";
  260. Dispatcher.UIThread.RunJobs();
  261. Assert.Equal(textbox.Text, control.Text);
  262. textbox.Text = "cook";
  263. Dispatcher.UIThread.RunJobs();
  264. Assert.Equal(textbox.Text, control.Text);
  265. textbox.Text = "accept";
  266. Dispatcher.UIThread.RunJobs();
  267. Assert.Equal(textbox.Text, control.Text);
  268. textbox.Text = "cook";
  269. Dispatcher.UIThread.RunJobs();
  270. Assert.Equal(textbox.Text, control.Text);
  271. });
  272. }
  273. [Fact]
  274. public void Item_Search()
  275. {
  276. RunTest((control, textbox) =>
  277. {
  278. control.FilterMode = AutoCompleteFilterMode.Custom;
  279. control.ItemFilter = (search, item) =>
  280. {
  281. string s = item as string;
  282. return s == null ? false : true;
  283. };
  284. // Just set to null briefly to exercise that code path
  285. AutoCompleteFilterPredicate<object> filter = control.ItemFilter;
  286. Assert.NotNull(filter);
  287. control.ItemFilter = null;
  288. Assert.Null(control.ItemFilter);
  289. control.ItemFilter = filter;
  290. Assert.NotNull(control.ItemFilter);
  291. textbox.Text = "a";
  292. Dispatcher.UIThread.RunJobs();
  293. Assert.Equal(textbox.Text, control.Text);
  294. textbox.Text = "acc";
  295. Dispatcher.UIThread.RunJobs();
  296. Assert.Equal(textbox.Text, control.Text);
  297. textbox.Text = "a";
  298. Dispatcher.UIThread.RunJobs();
  299. Assert.Equal(textbox.Text, control.Text);
  300. textbox.Text = "";
  301. Dispatcher.UIThread.RunJobs();
  302. Assert.Equal(textbox.Text, control.Text);
  303. textbox.Text = "cook";
  304. Dispatcher.UIThread.RunJobs();
  305. Assert.Equal(textbox.Text, control.Text);
  306. textbox.Text = "accept";
  307. Dispatcher.UIThread.RunJobs();
  308. Assert.Equal(textbox.Text, control.Text);
  309. textbox.Text = "cook";
  310. Dispatcher.UIThread.RunJobs();
  311. Assert.Equal(textbox.Text, control.Text);
  312. });
  313. }
  314. [Fact]
  315. public void Custom_TextSelector()
  316. {
  317. RunTest((control, textbox) =>
  318. {
  319. object selectedItem = control.ItemsSource.Cast<object>().First();
  320. string input = "42";
  321. control.TextSelector = (text, item) => text + item;
  322. Assert.Equal(control.TextSelector("4", "2"), "42");
  323. control.Text = input;
  324. control.SelectedItem = selectedItem;
  325. Assert.Equal(control.Text, control.TextSelector(input, selectedItem.ToString()));
  326. });
  327. }
  328. [Fact]
  329. public void Custom_ItemSelector()
  330. {
  331. RunTest((control, textbox) =>
  332. {
  333. object selectedItem = control.ItemsSource.Cast<object>().First();
  334. string input = "42";
  335. control.ItemSelector = (text, item) => text + item;
  336. Assert.Equal(control.ItemSelector("4", 2), "42");
  337. control.Text = input;
  338. control.SelectedItem = selectedItem;
  339. Assert.Equal(control.Text, control.ItemSelector(input, selectedItem));
  340. });
  341. }
  342. [Fact]
  343. public void Text_Validation()
  344. {
  345. RunTest((control, textbox) =>
  346. {
  347. var exception = new InvalidCastException("failed validation");
  348. var textObservable = new BehaviorSubject<BindingNotification>(new BindingNotification(exception, BindingErrorType.DataValidationError));
  349. control.Bind(AutoCompleteBox.TextProperty, textObservable);
  350. Dispatcher.UIThread.RunJobs();
  351. Assert.Equal(DataValidationErrors.GetHasErrors(control), true);
  352. Assert.Equal(DataValidationErrors.GetErrors(control).SequenceEqual(new[] { exception }), true);
  353. });
  354. }
  355. [Fact]
  356. public void Text_Validation_TextBox_Errors_Binding()
  357. {
  358. RunTest((control, textbox) =>
  359. {
  360. // simulate the TemplateBinding that would be used within the AutoCompleteBox control theme for the inner PART_TextBox
  361. // DataValidationErrors.Errors="{TemplateBinding (DataValidationErrors.Errors)}"
  362. textbox.Bind(DataValidationErrors.ErrorsProperty, control.GetBindingObservable(DataValidationErrors.ErrorsProperty));
  363. var exception = new InvalidCastException("failed validation");
  364. var textObservable = new BehaviorSubject<BindingNotification>(new BindingNotification(exception, BindingErrorType.DataValidationError));
  365. control.Bind(AutoCompleteBox.TextProperty, textObservable);
  366. Dispatcher.UIThread.RunJobs();
  367. Assert.True(DataValidationErrors.GetHasErrors(control));
  368. Assert.Equal([exception], DataValidationErrors.GetErrors(control));
  369. Assert.True(DataValidationErrors.GetHasErrors(textbox));
  370. Assert.Equal([exception], DataValidationErrors.GetErrors(textbox));
  371. });
  372. }
  373. [Fact]
  374. public void SelectedItem_Validation()
  375. {
  376. RunTest((control, textbox) =>
  377. {
  378. var exception = new InvalidCastException("failed validation");
  379. var itemObservable = new BehaviorSubject<BindingNotification>(new BindingNotification(exception, BindingErrorType.DataValidationError));
  380. control.Bind(AutoCompleteBox.SelectedItemProperty, itemObservable);
  381. Dispatcher.UIThread.RunJobs();
  382. Assert.Equal(DataValidationErrors.GetHasErrors(control), true);
  383. Assert.Equal(DataValidationErrors.GetErrors(control).SequenceEqual(new[] { exception }), true);
  384. });
  385. }
  386. [Fact]
  387. public void Explicit_Dropdown_Open_Request_MinimumPrefixLength_0()
  388. {
  389. RunTest((control, textbox) =>
  390. {
  391. control.Text = "";
  392. control.MinimumPrefixLength = 0;
  393. Dispatcher.UIThread.RunJobs();
  394. Assert.False(control.IsDropDownOpen);
  395. control.RaiseEvent(new KeyEventArgs
  396. {
  397. RoutedEvent = InputElement.KeyDownEvent,
  398. Key = Key.Down
  399. });
  400. Dispatcher.UIThread.RunJobs();
  401. Assert.True(control.IsDropDownOpen);
  402. });
  403. }
  404. [Fact]
  405. public void CaretIndex_Changes()
  406. {
  407. string text = "Sample text";
  408. string expectedText = "Saple text";
  409. RunTest((control, textbox) =>
  410. {
  411. control.Text = text;
  412. control.Measure(Size.Infinity);
  413. Dispatcher.UIThread.RunJobs();
  414. textbox.RaiseEvent(new KeyEventArgs
  415. {
  416. RoutedEvent = InputElement.KeyDownEvent,
  417. Key = Key.Right
  418. });
  419. Dispatcher.UIThread.RunJobs();
  420. Assert.Equal(1, control.CaretIndex);
  421. Assert.Equal(textbox.CaretIndex, control.CaretIndex);
  422. control.CaretIndex = 3;
  423. Assert.Equal(3, control.CaretIndex);
  424. Assert.Equal(textbox.CaretIndex, control.CaretIndex);
  425. textbox.RaiseEvent(new KeyEventArgs
  426. {
  427. RoutedEvent = InputElement.KeyDownEvent,
  428. Key = Key.Back
  429. });
  430. Dispatcher.UIThread.RunJobs();
  431. Assert.Equal(2, control.CaretIndex);
  432. Assert.Equal(textbox.CaretIndex, control.CaretIndex);
  433. Assert.True(control.Text == expectedText && textbox.Text == expectedText);
  434. });
  435. }
  436. [Fact]
  437. public void Attempting_To_Open_Without_Items_Does_Not_Prevent_Future_Opening_With_Items()
  438. {
  439. RunTest((control, textbox) =>
  440. {
  441. // Allow the drop down to open without anything entered.
  442. control.MinimumPrefixLength = 0;
  443. // Clear the items.
  444. var source = control.ItemsSource;
  445. control.ItemsSource = null;
  446. control.IsDropDownOpen = true;
  447. // DropDown was not actually opened because there are no items.
  448. Assert.False(control.IsDropDownOpen);
  449. // Set the items and try to open the drop down again.
  450. control.ItemsSource = source;
  451. control.IsDropDownOpen = true;
  452. // DropDown can now be opened.
  453. Assert.True(control.IsDropDownOpen);
  454. });
  455. }
  456. [Fact]
  457. public void Opening_Context_Menu_Does_not_Lose_Selection()
  458. {
  459. using (UnitTestApplication.Start(FocusServices))
  460. {
  461. var target1 = CreateControl();
  462. target1.ContextMenu = new TestContextMenu();
  463. var textBox1 = GetTextBox(target1);
  464. textBox1.Text = "1234";
  465. var target2 = CreateControl();
  466. var textBox2 = GetTextBox(target2);
  467. textBox2.Text = "5678";
  468. var sp = new StackPanel();
  469. sp.Children.Add(target1);
  470. sp.Children.Add(target2);
  471. target1.ApplyTemplate();
  472. target2.ApplyTemplate();
  473. var root = new TestRoot() { Child = sp };
  474. textBox1.SelectionStart = 0;
  475. textBox1.SelectionEnd = 3;
  476. target1.Focus();
  477. Assert.False(target2.IsFocused);
  478. Assert.True(target1.IsFocused);
  479. target2.Focus();
  480. Assert.Equal("123", textBox1.SelectedText);
  481. }
  482. }
  483. /// <summary>
  484. /// Retrieves a defined predicate filter through a new AutoCompleteBox
  485. /// control instance.
  486. /// </summary>
  487. /// <param name="mode">The FilterMode of interest.</param>
  488. /// <returns>Returns the predicate instance.</returns>
  489. private static AutoCompleteFilterPredicate<string> GetFilter(AutoCompleteFilterMode mode)
  490. {
  491. return new AutoCompleteBox { FilterMode = mode }
  492. .TextFilter;
  493. }
  494. /// <summary>
  495. /// Creates a large list of strings for AutoCompleteBox testing.
  496. /// </summary>
  497. /// <returns>Returns a new List of string values.</returns>
  498. private static IList<string> CreateSimpleStringArray()
  499. {
  500. return new List<string>
  501. {
  502. "a",
  503. "abide",
  504. "able",
  505. "about",
  506. "above",
  507. "absence",
  508. "absurd",
  509. "accept",
  510. "acceptance",
  511. "accepted",
  512. "accepting",
  513. "access",
  514. "accessed",
  515. "accessible",
  516. "accident",
  517. "accidentally",
  518. "accordance",
  519. "account",
  520. "accounting",
  521. "accounts",
  522. "accusation",
  523. "accustomed",
  524. "ache",
  525. "across",
  526. "act",
  527. "active",
  528. "actual",
  529. "actually",
  530. "ada",
  531. "added",
  532. "adding",
  533. "addition",
  534. "additional",
  535. "additions",
  536. "address",
  537. "addressed",
  538. "addresses",
  539. "addressing",
  540. "adjourn",
  541. "adoption",
  542. "advance",
  543. "advantage",
  544. "adventures",
  545. "advice",
  546. "advisable",
  547. "advise",
  548. "affair",
  549. "affectionately",
  550. "afford",
  551. "afore",
  552. "afraid",
  553. "after",
  554. "afterwards",
  555. "again",
  556. "against",
  557. "age",
  558. "aged",
  559. "agent",
  560. "ago",
  561. "agony",
  562. "agree",
  563. "agreed",
  564. "agreement",
  565. "ah",
  566. "ahem",
  567. "air",
  568. "airs",
  569. "ak",
  570. "alarm",
  571. "alarmed",
  572. "alas",
  573. "alice",
  574. "alive",
  575. "all",
  576. "allow",
  577. "almost",
  578. "alone",
  579. "along",
  580. "aloud",
  581. "already",
  582. "also",
  583. "alteration",
  584. "altered",
  585. "alternate",
  586. "alternately",
  587. "altogether",
  588. "always",
  589. "am",
  590. "ambition",
  591. "among",
  592. "an",
  593. "ancient",
  594. "and",
  595. "anger",
  596. "angrily",
  597. "angry",
  598. "animal",
  599. "animals",
  600. "ann",
  601. "annoy",
  602. "annoyed",
  603. "another",
  604. "answer",
  605. "answered",
  606. "answers",
  607. "antipathies",
  608. "anxious",
  609. "anxiously",
  610. "any",
  611. "anyone",
  612. "anything",
  613. "anywhere",
  614. "appealed",
  615. "appear",
  616. "appearance",
  617. "appeared",
  618. "appearing",
  619. "appears",
  620. "applause",
  621. "apple",
  622. "apples",
  623. "applicable",
  624. "apply",
  625. "approach",
  626. "arch",
  627. "archbishop",
  628. "arches",
  629. "archive",
  630. "are",
  631. "argue",
  632. "argued",
  633. "argument",
  634. "arguments",
  635. "arise",
  636. "arithmetic",
  637. "arm",
  638. "arms",
  639. "around",
  640. "arranged",
  641. "array",
  642. "arrived",
  643. "arrow",
  644. "arrum",
  645. "as",
  646. "ascii",
  647. "ashamed",
  648. "ask",
  649. "askance",
  650. "asked",
  651. "asking",
  652. "asleep",
  653. "assembled",
  654. "assistance",
  655. "associated",
  656. "at",
  657. "ate",
  658. "atheling",
  659. "atom",
  660. "attached",
  661. "attempt",
  662. "attempted",
  663. "attempts",
  664. "attended",
  665. "attending",
  666. "attends",
  667. "audibly",
  668. "australia",
  669. "author",
  670. "authority",
  671. "available",
  672. "avoid",
  673. "away",
  674. "awfully",
  675. "axes",
  676. "axis",
  677. "b",
  678. "baby",
  679. "back",
  680. "backs",
  681. "bad",
  682. "bag",
  683. "baked",
  684. "balanced",
  685. "bank",
  686. "banks",
  687. "banquet",
  688. "bark",
  689. "barking",
  690. "barley",
  691. "barrowful",
  692. "based",
  693. "bat",
  694. "bathing",
  695. "bats",
  696. "bawled",
  697. "be",
  698. "beak",
  699. "bear",
  700. "beast",
  701. "beasts",
  702. "beat",
  703. "beating",
  704. "beau",
  705. "beauti",
  706. "beautiful",
  707. "beautifully",
  708. "beautify",
  709. "became",
  710. "because",
  711. "become",
  712. "becoming",
  713. "bed",
  714. "beds",
  715. "bee",
  716. "been",
  717. "before",
  718. "beg",
  719. "began",
  720. "begged",
  721. "begin",
  722. "beginning",
  723. "begins",
  724. "begun",
  725. "behead",
  726. "beheaded",
  727. "beheading",
  728. "behind",
  729. "being",
  730. "believe",
  731. "believed",
  732. "bells",
  733. "belong",
  734. "belongs",
  735. "beloved",
  736. "below",
  737. "belt",
  738. "bend",
  739. "bent",
  740. "besides",
  741. "best",
  742. "better",
  743. "between",
  744. "bill",
  745. "binary",
  746. "bird",
  747. "birds",
  748. "birthday",
  749. "bit",
  750. "bite",
  751. "bitter",
  752. "blacking",
  753. "blades",
  754. "blame",
  755. "blasts",
  756. "bleeds",
  757. "blew",
  758. "blow",
  759. "blown",
  760. "blows",
  761. "body",
  762. "boldly",
  763. "bone",
  764. "bones",
  765. "book",
  766. "books",
  767. "boon",
  768. "boots",
  769. "bore",
  770. "both",
  771. "bother",
  772. "bottle",
  773. "bottom",
  774. "bough",
  775. "bound",
  776. "bowed",
  777. "bowing",
  778. "box",
  779. "boxed",
  780. "boy",
  781. "brain",
  782. "branch",
  783. "branches",
  784. "brandy",
  785. "brass",
  786. "brave",
  787. "breach",
  788. "bread",
  789. "break",
  790. "breath",
  791. "breathe",
  792. "breeze",
  793. "bright",
  794. "brightened",
  795. "bring",
  796. "bringing",
  797. "bristling",
  798. "broke",
  799. "broken",
  800. "brother",
  801. "brought",
  802. "brown",
  803. "brush",
  804. "brushing",
  805. "burn",
  806. "burning",
  807. "burnt",
  808. "burst",
  809. "bursting",
  810. "busily",
  811. "business",
  812. "business@pglaf",
  813. "busy",
  814. "but",
  815. "butter",
  816. "buttercup",
  817. "buttered",
  818. "butterfly",
  819. "buttons",
  820. "by",
  821. "bye",
  822. "c",
  823. "cackled",
  824. "cake",
  825. "cakes",
  826. "calculate",
  827. "calculated",
  828. "call",
  829. "called",
  830. "calling",
  831. "calmly",
  832. "came",
  833. "camomile",
  834. "can",
  835. "canary",
  836. "candle",
  837. "cannot",
  838. "canterbury",
  839. "canvas",
  840. "capering",
  841. "capital",
  842. "card",
  843. "cardboard",
  844. "cards",
  845. "care",
  846. "carefully",
  847. "cares",
  848. "carried",
  849. "carrier",
  850. "carroll",
  851. "carry",
  852. "carrying",
  853. "cart",
  854. "cartwheels",
  855. "case",
  856. "cat",
  857. "catch",
  858. "catching",
  859. "caterpillar",
  860. "cats",
  861. "cattle",
  862. "caucus",
  863. "caught",
  864. "cauldron",
  865. "cause",
  866. "caused",
  867. "cautiously",
  868. "cease",
  869. "ceiling",
  870. "centre",
  871. "certain",
  872. "certainly",
  873. "chain",
  874. "chains",
  875. "chair",
  876. "chance",
  877. "chanced",
  878. "change",
  879. "changed",
  880. "changes",
  881. "changing",
  882. "chapter",
  883. "character",
  884. "charge",
  885. "charges",
  886. "charitable",
  887. "charities",
  888. "chatte",
  889. "cheap",
  890. "cheated",
  891. "check",
  892. "checked",
  893. "checks",
  894. "cheeks",
  895. "cheered",
  896. "cheerfully",
  897. "cherry",
  898. "cheshire",
  899. "chief",
  900. "child",
  901. "childhood",
  902. "children",
  903. "chimney",
  904. "chimneys",
  905. "chin",
  906. "choice",
  907. "choke",
  908. "choked",
  909. "choking",
  910. "choose",
  911. "choosing",
  912. "chop",
  913. "chorus",
  914. "chose",
  915. "christmas",
  916. "chrysalis",
  917. "chuckled",
  918. "circle",
  919. "circumstances",
  920. "city",
  921. "civil",
  922. "claim",
  923. "clamour",
  924. "clapping",
  925. "clasped",
  926. "classics",
  927. "claws",
  928. "clean",
  929. "clear",
  930. "cleared",
  931. "clearer",
  932. "clearly",
  933. "clever",
  934. "climb",
  935. "clinging",
  936. "clock",
  937. "close",
  938. "closed",
  939. "closely",
  940. "closer",
  941. "clubs",
  942. "coast",
  943. "coaxing",
  944. "codes",
  945. "coils",
  946. "cold",
  947. "collar",
  948. "collected",
  949. "collection",
  950. "come",
  951. "comes",
  952. "comfits",
  953. "comfort",
  954. "comfortable",
  955. "comfortably",
  956. "coming",
  957. "commercial",
  958. "committed",
  959. "common",
  960. "commotion",
  961. "company",
  962. "compilation",
  963. "complained",
  964. "complaining",
  965. "completely",
  966. "compliance",
  967. "comply",
  968. "complying",
  969. "compressed",
  970. "computer",
  971. "computers",
  972. "concept",
  973. "concerning",
  974. "concert",
  975. "concluded",
  976. "conclusion",
  977. "condemn",
  978. "conduct",
  979. "confirmation",
  980. "confirmed",
  981. "confused",
  982. "confusing",
  983. "confusion",
  984. "conger",
  985. "conqueror",
  986. "conquest",
  987. "consented",
  988. "consequential",
  989. "consider",
  990. "considerable",
  991. "considered",
  992. "considering",
  993. "constant",
  994. "consultation",
  995. "contact",
  996. "contain",
  997. "containing",
  998. "contempt",
  999. "contemptuous",
  1000. "contemptuously",
  1001. "content",
  1002. "continued",
  1003. "contract",
  1004. "contradicted",
  1005. "contributions",
  1006. "conversation",
  1007. "conversations",
  1008. "convert",
  1009. "cook",
  1010. "cool",
  1011. "copied",
  1012. "copies",
  1013. "copy",
  1014. "copying",
  1015. "copyright",
  1016. "corner",
  1017. "corners",
  1018. "corporation",
  1019. "corrupt",
  1020. "cost",
  1021. "costs",
  1022. "could",
  1023. "couldn",
  1024. "counting",
  1025. "countries",
  1026. "country",
  1027. "couple",
  1028. "couples",
  1029. "courage",
  1030. "course",
  1031. "court",
  1032. "courtiers",
  1033. "coward",
  1034. "crab",
  1035. "crash",
  1036. "crashed",
  1037. "crawled",
  1038. "crawling",
  1039. "crazy",
  1040. "created",
  1041. "creating",
  1042. "creation",
  1043. "creature",
  1044. "creatures",
  1045. "credit",
  1046. "creep",
  1047. "crept",
  1048. "cried",
  1049. "cries",
  1050. "crimson",
  1051. "critical",
  1052. "crocodile",
  1053. "croquet",
  1054. "croqueted",
  1055. "croqueting",
  1056. "cross",
  1057. "crossed",
  1058. "crossly",
  1059. "crouched",
  1060. "crowd",
  1061. "crowded",
  1062. "crown",
  1063. "crumbs",
  1064. "crust",
  1065. "cry",
  1066. "crying",
  1067. "cucumber",
  1068. "cunning",
  1069. "cup",
  1070. "cupboards",
  1071. "cur",
  1072. "curiosity",
  1073. "curious",
  1074. "curiouser",
  1075. "curled",
  1076. "curls",
  1077. "curly",
  1078. "currants",
  1079. "current",
  1080. "curtain",
  1081. "curtsey",
  1082. "curtseying",
  1083. "curving",
  1084. "cushion",
  1085. "custard",
  1086. "custody",
  1087. "cut",
  1088. "cutting",
  1089. };
  1090. }
  1091. private void RunTest(Action<AutoCompleteBox, TextBox> test)
  1092. {
  1093. using (UnitTestApplication.Start(Services))
  1094. {
  1095. AutoCompleteBox control = CreateControl();
  1096. control.ItemsSource = CreateSimpleStringArray();
  1097. TextBox textBox = GetTextBox(control);
  1098. var window = new Window {Content = control};
  1099. window.ApplyStyling();
  1100. window.ApplyTemplate();
  1101. window.Presenter.ApplyTemplate();
  1102. Dispatcher.UIThread.RunJobs();
  1103. test.Invoke(control, textBox);
  1104. }
  1105. }
  1106. private static TestServices Services => TestServices.StyledWindow;
  1107. /*private static TestServices Services => TestServices.MockThreadingInterface.With(
  1108. standardCursorFactory: Mock.Of<IStandardCursorFactory>(),
  1109. windowingPlatform: new MockWindowingPlatform());*/
  1110. private AutoCompleteBox CreateControl()
  1111. {
  1112. var autoCompleteBox =
  1113. new AutoCompleteBox
  1114. {
  1115. Template = CreateTemplate()
  1116. };
  1117. autoCompleteBox.ApplyTemplate();
  1118. return autoCompleteBox;
  1119. }
  1120. private TextBox GetTextBox(AutoCompleteBox control)
  1121. {
  1122. return control.GetTemplateChildren()
  1123. .OfType<TextBox>()
  1124. .First();
  1125. }
  1126. private IControlTemplate CreateTemplate()
  1127. {
  1128. return new FuncControlTemplate<AutoCompleteBox>((control, scope) =>
  1129. {
  1130. var textBox =
  1131. new TextBox
  1132. {
  1133. Name = "PART_TextBox",
  1134. [!!TextBox.CaretIndexProperty] = control[!!AutoCompleteBox.CaretIndexProperty]
  1135. }.RegisterInNameScope(scope);
  1136. var listbox =
  1137. new ListBox
  1138. {
  1139. Name = "PART_SelectingItemsControl"
  1140. }.RegisterInNameScope(scope);
  1141. var popup =
  1142. new Popup
  1143. {
  1144. Name = "PART_Popup",
  1145. PlacementTarget = control
  1146. }.RegisterInNameScope(scope);
  1147. var panel = new Panel();
  1148. panel.Children.Add(textBox);
  1149. panel.Children.Add(popup);
  1150. panel.Children.Add(listbox);
  1151. return panel;
  1152. });
  1153. }
  1154. private static TestServices FocusServices => TestServices.MockThreadingInterface.With(
  1155. keyboardDevice: () => new KeyboardDevice(),
  1156. keyboardNavigation: () => new KeyboardNavigationHandler(),
  1157. inputManager: new InputManager(),
  1158. standardCursorFactory: Mock.Of<ICursorFactory>(),
  1159. textShaperImpl: new HeadlessTextShaperStub(),
  1160. fontManagerImpl: new HeadlessFontManagerStub());
  1161. private class TestContextMenu : ContextMenu
  1162. {
  1163. public TestContextMenu()
  1164. {
  1165. IsOpen = true;
  1166. }
  1167. }
  1168. }
  1169. }