AutoCompleteBoxTests.cs 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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 SelectedItem_Validation()
  357. {
  358. RunTest((control, textbox) =>
  359. {
  360. var exception = new InvalidCastException("failed validation");
  361. var itemObservable = new BehaviorSubject<BindingNotification>(new BindingNotification(exception, BindingErrorType.DataValidationError));
  362. control.Bind(AutoCompleteBox.SelectedItemProperty, itemObservable);
  363. Dispatcher.UIThread.RunJobs();
  364. Assert.Equal(DataValidationErrors.GetHasErrors(control), true);
  365. Assert.Equal(DataValidationErrors.GetErrors(control).SequenceEqual(new[] { exception }), true);
  366. });
  367. }
  368. [Fact]
  369. public void Explicit_Dropdown_Open_Request_MinimumPrefixLength_0()
  370. {
  371. RunTest((control, textbox) =>
  372. {
  373. control.Text = "";
  374. control.MinimumPrefixLength = 0;
  375. Dispatcher.UIThread.RunJobs();
  376. Assert.False(control.IsDropDownOpen);
  377. control.RaiseEvent(new KeyEventArgs
  378. {
  379. RoutedEvent = InputElement.KeyDownEvent,
  380. Key = Key.Down
  381. });
  382. Dispatcher.UIThread.RunJobs();
  383. Assert.True(control.IsDropDownOpen);
  384. });
  385. }
  386. [Fact]
  387. public void CaretIndex_Changes()
  388. {
  389. string text = "Sample text";
  390. string expectedText = "Saple text";
  391. RunTest((control, textbox) =>
  392. {
  393. control.Text = text;
  394. control.Measure(Size.Infinity);
  395. Dispatcher.UIThread.RunJobs();
  396. textbox.RaiseEvent(new KeyEventArgs
  397. {
  398. RoutedEvent = InputElement.KeyDownEvent,
  399. Key = Key.Right
  400. });
  401. Dispatcher.UIThread.RunJobs();
  402. Assert.Equal(1, control.CaretIndex);
  403. Assert.Equal(textbox.CaretIndex, control.CaretIndex);
  404. control.CaretIndex = 3;
  405. Assert.Equal(3, control.CaretIndex);
  406. Assert.Equal(textbox.CaretIndex, control.CaretIndex);
  407. textbox.RaiseEvent(new KeyEventArgs
  408. {
  409. RoutedEvent = InputElement.KeyDownEvent,
  410. Key = Key.Back
  411. });
  412. Dispatcher.UIThread.RunJobs();
  413. Assert.Equal(2, control.CaretIndex);
  414. Assert.Equal(textbox.CaretIndex, control.CaretIndex);
  415. Assert.True(control.Text == expectedText && textbox.Text == expectedText);
  416. });
  417. }
  418. [Fact]
  419. public void Attempting_To_Open_Without_Items_Does_Not_Prevent_Future_Opening_With_Items()
  420. {
  421. RunTest((control, textbox) =>
  422. {
  423. // Allow the drop down to open without anything entered.
  424. control.MinimumPrefixLength = 0;
  425. // Clear the items.
  426. var source = control.ItemsSource;
  427. control.ItemsSource = null;
  428. control.IsDropDownOpen = true;
  429. // DropDown was not actually opened because there are no items.
  430. Assert.False(control.IsDropDownOpen);
  431. // Set the items and try to open the drop down again.
  432. control.ItemsSource = source;
  433. control.IsDropDownOpen = true;
  434. // DropDown can now be opened.
  435. Assert.True(control.IsDropDownOpen);
  436. });
  437. }
  438. [Fact]
  439. public void Opening_Context_Menu_Does_not_Lose_Selection()
  440. {
  441. using (UnitTestApplication.Start(FocusServices))
  442. {
  443. var target1 = CreateControl();
  444. target1.ContextMenu = new TestContextMenu();
  445. var textBox1 = GetTextBox(target1);
  446. textBox1.Text = "1234";
  447. var target2 = CreateControl();
  448. var textBox2 = GetTextBox(target2);
  449. textBox2.Text = "5678";
  450. var sp = new StackPanel();
  451. sp.Children.Add(target1);
  452. sp.Children.Add(target2);
  453. target1.ApplyTemplate();
  454. target2.ApplyTemplate();
  455. var root = new TestRoot() { Child = sp };
  456. textBox1.SelectionStart = 0;
  457. textBox1.SelectionEnd = 3;
  458. target1.Focus();
  459. Assert.False(target2.IsFocused);
  460. Assert.True(target1.IsFocused);
  461. target2.Focus();
  462. Assert.Equal("123", textBox1.SelectedText);
  463. }
  464. }
  465. /// <summary>
  466. /// Retrieves a defined predicate filter through a new AutoCompleteBox
  467. /// control instance.
  468. /// </summary>
  469. /// <param name="mode">The FilterMode of interest.</param>
  470. /// <returns>Returns the predicate instance.</returns>
  471. private static AutoCompleteFilterPredicate<string> GetFilter(AutoCompleteFilterMode mode)
  472. {
  473. return new AutoCompleteBox { FilterMode = mode }
  474. .TextFilter;
  475. }
  476. /// <summary>
  477. /// Creates a large list of strings for AutoCompleteBox testing.
  478. /// </summary>
  479. /// <returns>Returns a new List of string values.</returns>
  480. private static IList<string> CreateSimpleStringArray()
  481. {
  482. return new List<string>
  483. {
  484. "a",
  485. "abide",
  486. "able",
  487. "about",
  488. "above",
  489. "absence",
  490. "absurd",
  491. "accept",
  492. "acceptance",
  493. "accepted",
  494. "accepting",
  495. "access",
  496. "accessed",
  497. "accessible",
  498. "accident",
  499. "accidentally",
  500. "accordance",
  501. "account",
  502. "accounting",
  503. "accounts",
  504. "accusation",
  505. "accustomed",
  506. "ache",
  507. "across",
  508. "act",
  509. "active",
  510. "actual",
  511. "actually",
  512. "ada",
  513. "added",
  514. "adding",
  515. "addition",
  516. "additional",
  517. "additions",
  518. "address",
  519. "addressed",
  520. "addresses",
  521. "addressing",
  522. "adjourn",
  523. "adoption",
  524. "advance",
  525. "advantage",
  526. "adventures",
  527. "advice",
  528. "advisable",
  529. "advise",
  530. "affair",
  531. "affectionately",
  532. "afford",
  533. "afore",
  534. "afraid",
  535. "after",
  536. "afterwards",
  537. "again",
  538. "against",
  539. "age",
  540. "aged",
  541. "agent",
  542. "ago",
  543. "agony",
  544. "agree",
  545. "agreed",
  546. "agreement",
  547. "ah",
  548. "ahem",
  549. "air",
  550. "airs",
  551. "ak",
  552. "alarm",
  553. "alarmed",
  554. "alas",
  555. "alice",
  556. "alive",
  557. "all",
  558. "allow",
  559. "almost",
  560. "alone",
  561. "along",
  562. "aloud",
  563. "already",
  564. "also",
  565. "alteration",
  566. "altered",
  567. "alternate",
  568. "alternately",
  569. "altogether",
  570. "always",
  571. "am",
  572. "ambition",
  573. "among",
  574. "an",
  575. "ancient",
  576. "and",
  577. "anger",
  578. "angrily",
  579. "angry",
  580. "animal",
  581. "animals",
  582. "ann",
  583. "annoy",
  584. "annoyed",
  585. "another",
  586. "answer",
  587. "answered",
  588. "answers",
  589. "antipathies",
  590. "anxious",
  591. "anxiously",
  592. "any",
  593. "anyone",
  594. "anything",
  595. "anywhere",
  596. "appealed",
  597. "appear",
  598. "appearance",
  599. "appeared",
  600. "appearing",
  601. "appears",
  602. "applause",
  603. "apple",
  604. "apples",
  605. "applicable",
  606. "apply",
  607. "approach",
  608. "arch",
  609. "archbishop",
  610. "arches",
  611. "archive",
  612. "are",
  613. "argue",
  614. "argued",
  615. "argument",
  616. "arguments",
  617. "arise",
  618. "arithmetic",
  619. "arm",
  620. "arms",
  621. "around",
  622. "arranged",
  623. "array",
  624. "arrived",
  625. "arrow",
  626. "arrum",
  627. "as",
  628. "ascii",
  629. "ashamed",
  630. "ask",
  631. "askance",
  632. "asked",
  633. "asking",
  634. "asleep",
  635. "assembled",
  636. "assistance",
  637. "associated",
  638. "at",
  639. "ate",
  640. "atheling",
  641. "atom",
  642. "attached",
  643. "attempt",
  644. "attempted",
  645. "attempts",
  646. "attended",
  647. "attending",
  648. "attends",
  649. "audibly",
  650. "australia",
  651. "author",
  652. "authority",
  653. "available",
  654. "avoid",
  655. "away",
  656. "awfully",
  657. "axes",
  658. "axis",
  659. "b",
  660. "baby",
  661. "back",
  662. "backs",
  663. "bad",
  664. "bag",
  665. "baked",
  666. "balanced",
  667. "bank",
  668. "banks",
  669. "banquet",
  670. "bark",
  671. "barking",
  672. "barley",
  673. "barrowful",
  674. "based",
  675. "bat",
  676. "bathing",
  677. "bats",
  678. "bawled",
  679. "be",
  680. "beak",
  681. "bear",
  682. "beast",
  683. "beasts",
  684. "beat",
  685. "beating",
  686. "beau",
  687. "beauti",
  688. "beautiful",
  689. "beautifully",
  690. "beautify",
  691. "became",
  692. "because",
  693. "become",
  694. "becoming",
  695. "bed",
  696. "beds",
  697. "bee",
  698. "been",
  699. "before",
  700. "beg",
  701. "began",
  702. "begged",
  703. "begin",
  704. "beginning",
  705. "begins",
  706. "begun",
  707. "behead",
  708. "beheaded",
  709. "beheading",
  710. "behind",
  711. "being",
  712. "believe",
  713. "believed",
  714. "bells",
  715. "belong",
  716. "belongs",
  717. "beloved",
  718. "below",
  719. "belt",
  720. "bend",
  721. "bent",
  722. "besides",
  723. "best",
  724. "better",
  725. "between",
  726. "bill",
  727. "binary",
  728. "bird",
  729. "birds",
  730. "birthday",
  731. "bit",
  732. "bite",
  733. "bitter",
  734. "blacking",
  735. "blades",
  736. "blame",
  737. "blasts",
  738. "bleeds",
  739. "blew",
  740. "blow",
  741. "blown",
  742. "blows",
  743. "body",
  744. "boldly",
  745. "bone",
  746. "bones",
  747. "book",
  748. "books",
  749. "boon",
  750. "boots",
  751. "bore",
  752. "both",
  753. "bother",
  754. "bottle",
  755. "bottom",
  756. "bough",
  757. "bound",
  758. "bowed",
  759. "bowing",
  760. "box",
  761. "boxed",
  762. "boy",
  763. "brain",
  764. "branch",
  765. "branches",
  766. "brandy",
  767. "brass",
  768. "brave",
  769. "breach",
  770. "bread",
  771. "break",
  772. "breath",
  773. "breathe",
  774. "breeze",
  775. "bright",
  776. "brightened",
  777. "bring",
  778. "bringing",
  779. "bristling",
  780. "broke",
  781. "broken",
  782. "brother",
  783. "brought",
  784. "brown",
  785. "brush",
  786. "brushing",
  787. "burn",
  788. "burning",
  789. "burnt",
  790. "burst",
  791. "bursting",
  792. "busily",
  793. "business",
  794. "business@pglaf",
  795. "busy",
  796. "but",
  797. "butter",
  798. "buttercup",
  799. "buttered",
  800. "butterfly",
  801. "buttons",
  802. "by",
  803. "bye",
  804. "c",
  805. "cackled",
  806. "cake",
  807. "cakes",
  808. "calculate",
  809. "calculated",
  810. "call",
  811. "called",
  812. "calling",
  813. "calmly",
  814. "came",
  815. "camomile",
  816. "can",
  817. "canary",
  818. "candle",
  819. "cannot",
  820. "canterbury",
  821. "canvas",
  822. "capering",
  823. "capital",
  824. "card",
  825. "cardboard",
  826. "cards",
  827. "care",
  828. "carefully",
  829. "cares",
  830. "carried",
  831. "carrier",
  832. "carroll",
  833. "carry",
  834. "carrying",
  835. "cart",
  836. "cartwheels",
  837. "case",
  838. "cat",
  839. "catch",
  840. "catching",
  841. "caterpillar",
  842. "cats",
  843. "cattle",
  844. "caucus",
  845. "caught",
  846. "cauldron",
  847. "cause",
  848. "caused",
  849. "cautiously",
  850. "cease",
  851. "ceiling",
  852. "centre",
  853. "certain",
  854. "certainly",
  855. "chain",
  856. "chains",
  857. "chair",
  858. "chance",
  859. "chanced",
  860. "change",
  861. "changed",
  862. "changes",
  863. "changing",
  864. "chapter",
  865. "character",
  866. "charge",
  867. "charges",
  868. "charitable",
  869. "charities",
  870. "chatte",
  871. "cheap",
  872. "cheated",
  873. "check",
  874. "checked",
  875. "checks",
  876. "cheeks",
  877. "cheered",
  878. "cheerfully",
  879. "cherry",
  880. "cheshire",
  881. "chief",
  882. "child",
  883. "childhood",
  884. "children",
  885. "chimney",
  886. "chimneys",
  887. "chin",
  888. "choice",
  889. "choke",
  890. "choked",
  891. "choking",
  892. "choose",
  893. "choosing",
  894. "chop",
  895. "chorus",
  896. "chose",
  897. "christmas",
  898. "chrysalis",
  899. "chuckled",
  900. "circle",
  901. "circumstances",
  902. "city",
  903. "civil",
  904. "claim",
  905. "clamour",
  906. "clapping",
  907. "clasped",
  908. "classics",
  909. "claws",
  910. "clean",
  911. "clear",
  912. "cleared",
  913. "clearer",
  914. "clearly",
  915. "clever",
  916. "climb",
  917. "clinging",
  918. "clock",
  919. "close",
  920. "closed",
  921. "closely",
  922. "closer",
  923. "clubs",
  924. "coast",
  925. "coaxing",
  926. "codes",
  927. "coils",
  928. "cold",
  929. "collar",
  930. "collected",
  931. "collection",
  932. "come",
  933. "comes",
  934. "comfits",
  935. "comfort",
  936. "comfortable",
  937. "comfortably",
  938. "coming",
  939. "commercial",
  940. "committed",
  941. "common",
  942. "commotion",
  943. "company",
  944. "compilation",
  945. "complained",
  946. "complaining",
  947. "completely",
  948. "compliance",
  949. "comply",
  950. "complying",
  951. "compressed",
  952. "computer",
  953. "computers",
  954. "concept",
  955. "concerning",
  956. "concert",
  957. "concluded",
  958. "conclusion",
  959. "condemn",
  960. "conduct",
  961. "confirmation",
  962. "confirmed",
  963. "confused",
  964. "confusing",
  965. "confusion",
  966. "conger",
  967. "conqueror",
  968. "conquest",
  969. "consented",
  970. "consequential",
  971. "consider",
  972. "considerable",
  973. "considered",
  974. "considering",
  975. "constant",
  976. "consultation",
  977. "contact",
  978. "contain",
  979. "containing",
  980. "contempt",
  981. "contemptuous",
  982. "contemptuously",
  983. "content",
  984. "continued",
  985. "contract",
  986. "contradicted",
  987. "contributions",
  988. "conversation",
  989. "conversations",
  990. "convert",
  991. "cook",
  992. "cool",
  993. "copied",
  994. "copies",
  995. "copy",
  996. "copying",
  997. "copyright",
  998. "corner",
  999. "corners",
  1000. "corporation",
  1001. "corrupt",
  1002. "cost",
  1003. "costs",
  1004. "could",
  1005. "couldn",
  1006. "counting",
  1007. "countries",
  1008. "country",
  1009. "couple",
  1010. "couples",
  1011. "courage",
  1012. "course",
  1013. "court",
  1014. "courtiers",
  1015. "coward",
  1016. "crab",
  1017. "crash",
  1018. "crashed",
  1019. "crawled",
  1020. "crawling",
  1021. "crazy",
  1022. "created",
  1023. "creating",
  1024. "creation",
  1025. "creature",
  1026. "creatures",
  1027. "credit",
  1028. "creep",
  1029. "crept",
  1030. "cried",
  1031. "cries",
  1032. "crimson",
  1033. "critical",
  1034. "crocodile",
  1035. "croquet",
  1036. "croqueted",
  1037. "croqueting",
  1038. "cross",
  1039. "crossed",
  1040. "crossly",
  1041. "crouched",
  1042. "crowd",
  1043. "crowded",
  1044. "crown",
  1045. "crumbs",
  1046. "crust",
  1047. "cry",
  1048. "crying",
  1049. "cucumber",
  1050. "cunning",
  1051. "cup",
  1052. "cupboards",
  1053. "cur",
  1054. "curiosity",
  1055. "curious",
  1056. "curiouser",
  1057. "curled",
  1058. "curls",
  1059. "curly",
  1060. "currants",
  1061. "current",
  1062. "curtain",
  1063. "curtsey",
  1064. "curtseying",
  1065. "curving",
  1066. "cushion",
  1067. "custard",
  1068. "custody",
  1069. "cut",
  1070. "cutting",
  1071. };
  1072. }
  1073. private void RunTest(Action<AutoCompleteBox, TextBox> test)
  1074. {
  1075. using (UnitTestApplication.Start(Services))
  1076. {
  1077. AutoCompleteBox control = CreateControl();
  1078. control.ItemsSource = CreateSimpleStringArray();
  1079. TextBox textBox = GetTextBox(control);
  1080. var window = new Window {Content = control};
  1081. window.ApplyStyling();
  1082. window.ApplyTemplate();
  1083. window.Presenter.ApplyTemplate();
  1084. Dispatcher.UIThread.RunJobs();
  1085. test.Invoke(control, textBox);
  1086. }
  1087. }
  1088. private static TestServices Services => TestServices.StyledWindow;
  1089. /*private static TestServices Services => TestServices.MockThreadingInterface.With(
  1090. standardCursorFactory: Mock.Of<IStandardCursorFactory>(),
  1091. windowingPlatform: new MockWindowingPlatform());*/
  1092. private AutoCompleteBox CreateControl()
  1093. {
  1094. var autoCompleteBox =
  1095. new AutoCompleteBox
  1096. {
  1097. Template = CreateTemplate()
  1098. };
  1099. autoCompleteBox.ApplyTemplate();
  1100. return autoCompleteBox;
  1101. }
  1102. private TextBox GetTextBox(AutoCompleteBox control)
  1103. {
  1104. return control.GetTemplateChildren()
  1105. .OfType<TextBox>()
  1106. .First();
  1107. }
  1108. private IControlTemplate CreateTemplate()
  1109. {
  1110. return new FuncControlTemplate<AutoCompleteBox>((control, scope) =>
  1111. {
  1112. var textBox =
  1113. new TextBox
  1114. {
  1115. Name = "PART_TextBox",
  1116. [!!TextBox.CaretIndexProperty] = control[!!AutoCompleteBox.CaretIndexProperty]
  1117. }.RegisterInNameScope(scope);
  1118. var listbox =
  1119. new ListBox
  1120. {
  1121. Name = "PART_SelectingItemsControl"
  1122. }.RegisterInNameScope(scope);
  1123. var popup =
  1124. new Popup
  1125. {
  1126. Name = "PART_Popup",
  1127. PlacementTarget = control
  1128. }.RegisterInNameScope(scope);
  1129. var panel = new Panel();
  1130. panel.Children.Add(textBox);
  1131. panel.Children.Add(popup);
  1132. panel.Children.Add(listbox);
  1133. return panel;
  1134. });
  1135. }
  1136. private static TestServices FocusServices => TestServices.MockThreadingInterface.With(
  1137. focusManager: new FocusManager(),
  1138. keyboardDevice: () => new KeyboardDevice(),
  1139. keyboardNavigation: () => new KeyboardNavigationHandler(),
  1140. inputManager: new InputManager(),
  1141. standardCursorFactory: Mock.Of<ICursorFactory>(),
  1142. textShaperImpl: new HeadlessTextShaperStub(),
  1143. fontManagerImpl: new HeadlessFontManagerStub());
  1144. private class TestContextMenu : ContextMenu
  1145. {
  1146. public TestContextMenu()
  1147. {
  1148. IsOpen = true;
  1149. }
  1150. }
  1151. }
  1152. }