TextBoxTests.cs 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reactive.Linq;
  4. using System.Threading.Tasks;
  5. using Avalonia.Controls.Presenters;
  6. using Avalonia.Controls.Primitives;
  7. using Avalonia.Controls.Templates;
  8. using Avalonia.Data;
  9. using Avalonia.Input;
  10. using Avalonia.Input.Platform;
  11. using Avalonia.Layout;
  12. using Avalonia.Media;
  13. using Avalonia.Platform;
  14. using Avalonia.UnitTests;
  15. using Moq;
  16. using Xunit;
  17. namespace Avalonia.Controls.UnitTests
  18. {
  19. public class TextBoxTests
  20. {
  21. [Fact]
  22. public void Opening_Context_Menu_Does_not_Lose_Selection()
  23. {
  24. using (UnitTestApplication.Start(FocusServices))
  25. {
  26. var target1 = new TextBox
  27. {
  28. Template = CreateTemplate(),
  29. Text = "1234",
  30. ContextMenu = new TestContextMenu()
  31. };
  32. var target2 = new TextBox
  33. {
  34. Template = CreateTemplate(),
  35. Text = "5678"
  36. };
  37. var sp = new StackPanel();
  38. sp.Children.Add(target1);
  39. sp.Children.Add(target2);
  40. target1.ApplyTemplate();
  41. target2.ApplyTemplate();
  42. var root = new TestRoot() { Child = sp };
  43. target1.SelectionStart = 0;
  44. target1.SelectionEnd = 3;
  45. target1.Focus();
  46. Assert.False(target2.IsFocused);
  47. Assert.True(target1.IsFocused);
  48. target2.Focus();
  49. Assert.Equal("123", target1.SelectedText);
  50. }
  51. }
  52. [Fact]
  53. public void Opening_Context_Flyout_Does_not_Lose_Selection()
  54. {
  55. using (UnitTestApplication.Start(FocusServices))
  56. {
  57. var target1 = new TextBox
  58. {
  59. Template = CreateTemplate(),
  60. Text = "1234",
  61. ContextFlyout = new MenuFlyout
  62. {
  63. Items = new List<MenuItem>
  64. {
  65. new MenuItem { Header = "Item 1" },
  66. new MenuItem {Header = "Item 2" },
  67. new MenuItem {Header = "Item 3" }
  68. }
  69. }
  70. };
  71. target1.ApplyTemplate();
  72. var root = new TestRoot() { Child = target1 };
  73. target1.SelectionStart = 0;
  74. target1.SelectionEnd = 3;
  75. target1.Focus();
  76. Assert.True(target1.IsFocused);
  77. target1.ContextFlyout.ShowAt(target1);
  78. Assert.Equal("123", target1.SelectedText);
  79. }
  80. }
  81. [Fact]
  82. public void DefaultBindingMode_Should_Be_TwoWay()
  83. {
  84. Assert.Equal(
  85. BindingMode.TwoWay,
  86. TextBox.TextProperty.GetMetadata(typeof(TextBox)).DefaultBindingMode);
  87. }
  88. [Fact]
  89. public void CaretIndex_Can_Moved_To_Position_After_The_End_Of_Text_With_Arrow_Key()
  90. {
  91. using (UnitTestApplication.Start(Services))
  92. {
  93. var target = new TextBox
  94. {
  95. Template = CreateTemplate(),
  96. Text = "1234"
  97. };
  98. target.ApplyTemplate();
  99. target.Measure(Size.Infinity);
  100. target.CaretIndex = 3;
  101. RaiseKeyEvent(target, Key.Right, 0);
  102. Assert.Equal(4, target.CaretIndex);
  103. }
  104. }
  105. [Fact]
  106. public void Press_Ctrl_A_Select_All_Text()
  107. {
  108. using (UnitTestApplication.Start(Services))
  109. {
  110. var target = new TextBox
  111. {
  112. Template = CreateTemplate(),
  113. Text = "1234"
  114. };
  115. target.ApplyTemplate();
  116. RaiseKeyEvent(target, Key.A, KeyModifiers.Control);
  117. Assert.Equal(0, target.SelectionStart);
  118. Assert.Equal(4, target.SelectionEnd);
  119. }
  120. }
  121. [Fact]
  122. public void Press_Ctrl_A_Select_All_Null_Text()
  123. {
  124. using (UnitTestApplication.Start(Services))
  125. {
  126. var target = new TextBox
  127. {
  128. Template = CreateTemplate()
  129. };
  130. RaiseKeyEvent(target, Key.A, KeyModifiers.Control);
  131. Assert.Equal(0, target.SelectionStart);
  132. Assert.Equal(0, target.SelectionEnd);
  133. }
  134. }
  135. [Fact]
  136. public void Press_Ctrl_Z_Will_Not_Modify_Text()
  137. {
  138. using (UnitTestApplication.Start(Services))
  139. {
  140. var target = new TextBox
  141. {
  142. Template = CreateTemplate(),
  143. Text = "1234"
  144. };
  145. RaiseKeyEvent(target, Key.Z, KeyModifiers.Control);
  146. Assert.Equal("1234", target.Text);
  147. }
  148. }
  149. [Fact]
  150. public void Control_Backspace_Should_Remove_The_Word_Before_The_Caret_If_There_Is_No_Selection()
  151. {
  152. using (UnitTestApplication.Start(Services))
  153. {
  154. TextBox textBox = new TextBox
  155. {
  156. Template = CreateTemplate(),
  157. Text = "First Second Third Fourth",
  158. SelectionStart = 5,
  159. SelectionEnd = 5
  160. };
  161. textBox.ApplyTemplate();
  162. // (First| Second Third Fourth)
  163. RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
  164. Assert.Equal(" Second Third Fourth", textBox.Text);
  165. // ( Second |Third Fourth)
  166. textBox.CaretIndex = 8;
  167. RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
  168. Assert.Equal(" Third Fourth", textBox.Text);
  169. // ( Thi|rd Fourth)
  170. textBox.CaretIndex = 4;
  171. RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
  172. Assert.Equal(" rd Fourth", textBox.Text);
  173. // ( rd F[ou]rth)
  174. textBox.SelectionStart = 5;
  175. textBox.SelectionEnd = 7;
  176. RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
  177. Assert.Equal(" rd Frth", textBox.Text);
  178. // ( |rd Frth)
  179. textBox.CaretIndex = 1;
  180. RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
  181. Assert.Equal("rd Frth", textBox.Text);
  182. }
  183. }
  184. [Fact]
  185. public void Control_Delete_Should_Remove_The_Word_After_The_Caret_If_There_Is_No_Selection()
  186. {
  187. using (UnitTestApplication.Start(Services))
  188. {
  189. TextBox textBox = new TextBox
  190. {
  191. Template = CreateTemplate(),
  192. Text = "First Second Third Fourth",
  193. CaretIndex = 19,
  194. };
  195. textBox.ApplyTemplate();
  196. // (First Second Third |Fourth)
  197. RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
  198. Assert.Equal("First Second Third ", textBox.Text);
  199. // (First Second |Third )
  200. textBox.CaretIndex = 13;
  201. RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
  202. Assert.Equal("First Second ", textBox.Text);
  203. // (First Sec|ond )
  204. textBox.CaretIndex = 9;
  205. RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
  206. Assert.Equal("First Sec", textBox.Text);
  207. // (Fi[rs]t Sec )
  208. textBox.SelectionStart = 2;
  209. textBox.SelectionEnd = 4;
  210. RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
  211. Assert.Equal("Fit Sec", textBox.Text);
  212. // (Fit Sec| )
  213. textBox.Text += " ";
  214. textBox.CaretIndex = 7;
  215. RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
  216. Assert.Equal("Fit Sec", textBox.Text);
  217. }
  218. }
  219. [Fact]
  220. public void Setting_SelectionStart_To_SelectionEnd_Sets_CaretPosition_To_SelectionStart()
  221. {
  222. using (UnitTestApplication.Start(Services))
  223. {
  224. var textBox = new TextBox
  225. {
  226. Text = "0123456789"
  227. };
  228. textBox.SelectionStart = 2;
  229. textBox.SelectionEnd = 2;
  230. Assert.Equal(2, textBox.CaretIndex);
  231. }
  232. }
  233. [Fact]
  234. public void Setting_Text_Updates_CaretPosition()
  235. {
  236. using (UnitTestApplication.Start(Services))
  237. {
  238. var target = new TextBox
  239. {
  240. Text = "Initial Text",
  241. CaretIndex = 11
  242. };
  243. var invoked = false;
  244. target.GetObservable(TextBox.TextProperty).Skip(1).Subscribe(_ =>
  245. {
  246. // Caret index should be set before Text changed notification, as we don't want
  247. // to notify with an invalid CaretIndex.
  248. Assert.Equal(7, target.CaretIndex);
  249. invoked = true;
  250. });
  251. target.Text = "Changed";
  252. Assert.True(invoked);
  253. }
  254. }
  255. [Fact]
  256. public void Press_Enter_Does_Not_Accept_Return()
  257. {
  258. using (UnitTestApplication.Start(Services))
  259. {
  260. var target = new TextBox
  261. {
  262. Template = CreateTemplate(),
  263. AcceptsReturn = false,
  264. Text = "1234"
  265. };
  266. target.ApplyTemplate();
  267. RaiseKeyEvent(target, Key.Enter, 0);
  268. Assert.Equal("1234", target.Text);
  269. }
  270. }
  271. [Fact]
  272. public void Press_Enter_Add_Default_Newline()
  273. {
  274. using (UnitTestApplication.Start(Services))
  275. {
  276. var target = new TextBox
  277. {
  278. Template = CreateTemplate(),
  279. AcceptsReturn = true
  280. };
  281. target.ApplyTemplate();
  282. RaiseKeyEvent(target, Key.Enter, 0);
  283. Assert.Equal(Environment.NewLine, target.Text);
  284. }
  285. }
  286. [Fact]
  287. public void Press_Enter_Add_Custom_Newline()
  288. {
  289. using (UnitTestApplication.Start(Services))
  290. {
  291. var target = new TextBox
  292. {
  293. Template = CreateTemplate(),
  294. AcceptsReturn = true,
  295. NewLine = "Test"
  296. };
  297. target.ApplyTemplate();
  298. RaiseKeyEvent(target, Key.Enter, 0);
  299. Assert.Equal("Test", target.Text);
  300. }
  301. }
  302. [Theory]
  303. [InlineData(new object[] { false, TextWrapping.NoWrap, ScrollBarVisibility.Hidden })]
  304. [InlineData(new object[] { false, TextWrapping.Wrap, ScrollBarVisibility.Disabled })]
  305. [InlineData(new object[] { true, TextWrapping.NoWrap, ScrollBarVisibility.Auto })]
  306. [InlineData(new object[] { true, TextWrapping.Wrap, ScrollBarVisibility.Disabled })]
  307. public void Has_Correct_Horizontal_ScrollBar_Visibility(
  308. bool acceptsReturn,
  309. TextWrapping wrapping,
  310. ScrollBarVisibility expected)
  311. {
  312. using (UnitTestApplication.Start(Services))
  313. {
  314. var target = new TextBox
  315. {
  316. AcceptsReturn = acceptsReturn,
  317. TextWrapping = wrapping,
  318. };
  319. Assert.Equal(expected, ScrollViewer.GetHorizontalScrollBarVisibility(target));
  320. }
  321. }
  322. [Fact]
  323. public void SelectionEnd_Doesnt_Cause_Exception()
  324. {
  325. using (UnitTestApplication.Start(Services))
  326. {
  327. var target = new TextBox
  328. {
  329. Template = CreateTemplate(),
  330. Text = "0123456789"
  331. };
  332. target.ApplyTemplate();
  333. target.SelectionStart = 0;
  334. target.SelectionEnd = 9;
  335. target.Text = "123";
  336. RaiseTextEvent(target, "456");
  337. Assert.True(true);
  338. }
  339. }
  340. [Fact]
  341. public void SelectionStart_Doesnt_Cause_Exception()
  342. {
  343. using (UnitTestApplication.Start(Services))
  344. {
  345. var target = new TextBox
  346. {
  347. Template = CreateTemplate(),
  348. Text = "0123456789"
  349. };
  350. target.ApplyTemplate();
  351. target.SelectionStart = 8;
  352. target.SelectionEnd = 9;
  353. target.Text = "123";
  354. RaiseTextEvent(target, "456");
  355. Assert.True(true);
  356. }
  357. }
  358. [Fact]
  359. public void SelectionStartEnd_Are_Valid_AterTextChange()
  360. {
  361. using (UnitTestApplication.Start(Services))
  362. {
  363. var target = new TextBox
  364. {
  365. Template = CreateTemplate(),
  366. Text = "0123456789"
  367. };
  368. target.SelectionStart = 8;
  369. target.SelectionEnd = 9;
  370. target.Text = "123";
  371. Assert.True(target.SelectionStart <= "123".Length);
  372. Assert.True(target.SelectionEnd <= "123".Length);
  373. }
  374. }
  375. [Fact]
  376. public void SelectedText_Changes_OnSelectionChange()
  377. {
  378. using (UnitTestApplication.Start(Services))
  379. {
  380. var target = new TextBox
  381. {
  382. Template = CreateTemplate(),
  383. Text = "0123456789"
  384. };
  385. target.ApplyTemplate();
  386. Assert.True(target.SelectedText == "");
  387. target.SelectionStart = 2;
  388. target.SelectionEnd = 4;
  389. Assert.True(target.SelectedText == "23");
  390. }
  391. }
  392. [Fact]
  393. public void SelectedText_EditsText()
  394. {
  395. using (UnitTestApplication.Start(Services))
  396. {
  397. var target = new TextBox
  398. {
  399. Template = CreateTemplate(),
  400. Text = "0123"
  401. };
  402. target.ApplyTemplate();
  403. target.SelectedText = "AA";
  404. Assert.True(target.Text == "AA0123");
  405. target.SelectionStart = 1;
  406. target.SelectionEnd = 3;
  407. target.SelectedText = "BB";
  408. Assert.True(target.Text == "ABB123");
  409. }
  410. }
  411. [Fact]
  412. public void SelectedText_CanClearText()
  413. {
  414. using (UnitTestApplication.Start(Services))
  415. {
  416. var target = new TextBox
  417. {
  418. Template = CreateTemplate(),
  419. Text = "0123"
  420. };
  421. target.SelectionStart = 1;
  422. target.SelectionEnd = 3;
  423. target.SelectedText = "";
  424. Assert.True(target.Text == "03");
  425. }
  426. }
  427. [Fact]
  428. public void SelectedText_NullClearsText()
  429. {
  430. using (UnitTestApplication.Start(Services))
  431. {
  432. var target = new TextBox
  433. {
  434. Template = CreateTemplate(),
  435. Text = "0123"
  436. };
  437. target.SelectionStart = 1;
  438. target.SelectionEnd = 3;
  439. target.SelectedText = null;
  440. Assert.True(target.Text == "03");
  441. }
  442. }
  443. [Fact]
  444. public void CoerceCaretIndex_Doesnt_Cause_Exception_with_malformed_line_ending()
  445. {
  446. using (UnitTestApplication.Start(Services))
  447. {
  448. var target = new TextBox
  449. {
  450. Template = CreateTemplate(),
  451. Text = "0123456789\r"
  452. };
  453. target.CaretIndex = 11;
  454. Assert.True(true);
  455. }
  456. }
  457. [Theory]
  458. [InlineData(Key.Up)]
  459. [InlineData(Key.Down)]
  460. [InlineData(Key.Home)]
  461. [InlineData(Key.End)]
  462. public void Textbox_doesnt_crash_when_Receives_input_and_template_not_applied(Key key)
  463. {
  464. using (UnitTestApplication.Start(FocusServices))
  465. {
  466. var target1 = new TextBox
  467. {
  468. Template = CreateTemplate(),
  469. Text = "1234",
  470. IsVisible = false
  471. };
  472. var root = new TestRoot { Child = target1 };
  473. target1.Focus();
  474. Assert.True(target1.IsFocused);
  475. RaiseKeyEvent(target1, key, KeyModifiers.None);
  476. }
  477. }
  478. [Fact]
  479. public void TextBox_GotFocus_And_LostFocus_Work_Properly()
  480. {
  481. using (UnitTestApplication.Start(FocusServices))
  482. {
  483. var target1 = new TextBox
  484. {
  485. Template = CreateTemplate(),
  486. Text = "1234"
  487. };
  488. var target2 = new TextBox
  489. {
  490. Template = CreateTemplate(),
  491. Text = "5678"
  492. };
  493. var sp = new StackPanel();
  494. sp.Children.Add(target1);
  495. sp.Children.Add(target2);
  496. target1.ApplyTemplate();
  497. target2.ApplyTemplate();
  498. var root = new TestRoot { Child = sp };
  499. var gfcount = 0;
  500. var lfcount = 0;
  501. target1.GotFocus += (s, e) => gfcount++;
  502. target2.LostFocus += (s, e) => lfcount++;
  503. target2.Focus();
  504. Assert.False(target1.IsFocused);
  505. Assert.True(target2.IsFocused);
  506. target1.Focus();
  507. Assert.False(target2.IsFocused);
  508. Assert.True(target1.IsFocused);
  509. Assert.Equal(1, gfcount);
  510. Assert.Equal(1, lfcount);
  511. }
  512. }
  513. [Fact]
  514. public void TextBox_CaretIndex_Persists_When_Focus_Lost()
  515. {
  516. using (UnitTestApplication.Start(FocusServices))
  517. {
  518. var target1 = new TextBox
  519. {
  520. Template = CreateTemplate(),
  521. Text = "1234"
  522. };
  523. var target2 = new TextBox
  524. {
  525. Template = CreateTemplate(),
  526. Text = "5678"
  527. };
  528. var sp = new StackPanel();
  529. sp.Children.Add(target1);
  530. sp.Children.Add(target2);
  531. target1.ApplyTemplate();
  532. target2.ApplyTemplate();
  533. var root = new TestRoot { Child = sp };
  534. target2.Focus();
  535. target2.CaretIndex = 2;
  536. Assert.False(target1.IsFocused);
  537. Assert.True(target2.IsFocused);
  538. target1.Focus();
  539. Assert.Equal(2, target2.CaretIndex);
  540. }
  541. }
  542. [Fact]
  543. public void TextBox_Reveal_Password_Reset_When_Lost_Focus()
  544. {
  545. using (UnitTestApplication.Start(FocusServices))
  546. {
  547. var target1 = new TextBox
  548. {
  549. Template = CreateTemplate(),
  550. Text = "1234",
  551. PasswordChar = '*'
  552. };
  553. var target2 = new TextBox
  554. {
  555. Template = CreateTemplate(),
  556. Text = "5678"
  557. };
  558. var sp = new StackPanel();
  559. sp.Children.Add(target1);
  560. sp.Children.Add(target2);
  561. target1.ApplyTemplate();
  562. target2.ApplyTemplate();
  563. var root = new TestRoot { Child = sp };
  564. target1.Focus();
  565. target1.RevealPassword = true;
  566. target2.Focus();
  567. Assert.False(target1.RevealPassword);
  568. }
  569. }
  570. [Fact]
  571. public void Setting_Bound_Text_To_Null_Works()
  572. {
  573. using (UnitTestApplication.Start(Services))
  574. {
  575. var source = new Class1 { Bar = "bar" };
  576. var target = new TextBox { Template = CreateTemplate(), DataContext = source };
  577. target.ApplyTemplate();
  578. target.Bind(TextBox.TextProperty, new Binding("Bar"));
  579. Assert.Equal("bar", target.Text);
  580. source.Bar = null;
  581. Assert.Null(target.Text);
  582. }
  583. }
  584. [Theory]
  585. [InlineData("abc", "d", 3, 0, 0, false, "abc")]
  586. [InlineData("abc", "dd", 4, 3, 3, false, "abcd")]
  587. [InlineData("abc", "ddd", 3, 0, 2, true, "ddc")]
  588. [InlineData("abc", "dddd", 4, 1, 3, true, "addd")]
  589. [InlineData("abc", "ddddd", 5, 3, 3, true, "abcdd")]
  590. public void MaxLength_Works_Properly(
  591. string initalText,
  592. string textInput,
  593. int maxLength,
  594. int selectionStart,
  595. int selectionEnd,
  596. bool fromClipboard,
  597. string expected)
  598. {
  599. using (UnitTestApplication.Start(Services))
  600. {
  601. var target = new TextBox
  602. {
  603. Template = CreateTemplate(),
  604. Text = initalText,
  605. MaxLength = maxLength,
  606. SelectionStart = selectionStart,
  607. SelectionEnd = selectionEnd
  608. };
  609. target.Measure(Size.Infinity);
  610. if (fromClipboard)
  611. {
  612. AvaloniaLocator.CurrentMutable.Bind<IClipboard>().ToSingleton<ClipboardStub>();
  613. var clipboard = AvaloniaLocator.CurrentMutable.GetService<IClipboard>();
  614. clipboard.SetTextAsync(textInput).GetAwaiter().GetResult();
  615. RaiseKeyEvent(target, Key.V, KeyModifiers.Control);
  616. clipboard.ClearAsync().GetAwaiter().GetResult();
  617. }
  618. else
  619. {
  620. RaiseTextEvent(target, textInput);
  621. }
  622. Assert.Equal(expected, target.Text);
  623. }
  624. }
  625. [Theory]
  626. [InlineData(Key.X, KeyModifiers.Control)]
  627. [InlineData(Key.Back, KeyModifiers.None)]
  628. [InlineData(Key.Delete, KeyModifiers.None)]
  629. [InlineData(Key.Tab, KeyModifiers.None)]
  630. [InlineData(Key.Enter, KeyModifiers.None)]
  631. public void Keys_Allow_Undo(Key key, KeyModifiers modifiers)
  632. {
  633. using (UnitTestApplication.Start(Services))
  634. {
  635. var target = new TextBox
  636. {
  637. Template = CreateTemplate(),
  638. Text = "0123",
  639. AcceptsReturn = true,
  640. AcceptsTab = true
  641. };
  642. target.ApplyTemplate();
  643. target.SelectionStart = 1;
  644. target.SelectionEnd = 3;
  645. AvaloniaLocator.CurrentMutable
  646. .Bind<Input.Platform.IClipboard>().ToSingleton<ClipboardStub>();
  647. RaiseKeyEvent(target, key, modifiers);
  648. RaiseKeyEvent(target, Key.Z, KeyModifiers.Control); // undo
  649. Assert.True(target.Text == "0123");
  650. }
  651. }
  652. [Fact]
  653. public void Setting_SelectedText_Should_Fire_Single_Text_Changed_Notification()
  654. {
  655. using (UnitTestApplication.Start(Services))
  656. {
  657. var target = new TextBox
  658. {
  659. Template = CreateTemplate(),
  660. Text = "0123",
  661. AcceptsReturn = true,
  662. AcceptsTab = true,
  663. SelectionStart = 1,
  664. SelectionEnd = 3,
  665. };
  666. var values = new List<string>();
  667. target.GetObservable(TextBox.TextProperty).Subscribe(x => values.Add(x));
  668. target.SelectedText = "A";
  669. Assert.Equal(new[] { "0123", "0A3" }, values);
  670. }
  671. }
  672. [Fact]
  673. public void Entering_Text_With_SelectedText_Should_Fire_Single_Text_Changed_Notification()
  674. {
  675. using (UnitTestApplication.Start(Services))
  676. {
  677. var target = new TextBox
  678. {
  679. Template = CreateTemplate(),
  680. Text = "0123",
  681. AcceptsReturn = true,
  682. AcceptsTab = true,
  683. SelectionStart = 1,
  684. SelectionEnd = 3,
  685. };
  686. var values = new List<string>();
  687. target.GetObservable(TextBox.TextProperty).Subscribe(x => values.Add(x));
  688. RaiseTextEvent(target, "A");
  689. Assert.Equal(new[] { "0123", "0A3" }, values);
  690. }
  691. }
  692. [Fact]
  693. public void Should_Fullfill_MaxLines_Contraint()
  694. {
  695. using (UnitTestApplication.Start(Services))
  696. {
  697. var target = new TextBox
  698. {
  699. Template = CreateTemplate(),
  700. Text = "ABC",
  701. MaxLines = 1,
  702. AcceptsReturn= true
  703. };
  704. target.Measure(Size.Infinity);
  705. AvaloniaLocator.CurrentMutable.Bind<IClipboard>().ToSingleton<ClipboardStub>();
  706. var clipboard = AvaloniaLocator.CurrentMutable.GetService<IClipboard>();
  707. clipboard.SetTextAsync(Environment.NewLine).GetAwaiter().GetResult();
  708. RaiseKeyEvent(target, Key.V, KeyModifiers.Control);
  709. clipboard.ClearAsync().GetAwaiter().GetResult();
  710. RaiseTextEvent(target, Environment.NewLine);
  711. Assert.Equal("ABC", target.Text);
  712. }
  713. }
  714. [Fact]
  715. public void CanUndo_CanRedo_Is_False_When_Initialized()
  716. {
  717. using (UnitTestApplication.Start(Services))
  718. {
  719. var tb = new TextBox
  720. {
  721. Template = CreateTemplate(),
  722. Text = "New Text"
  723. };
  724. tb.Measure(Size.Infinity);
  725. Assert.False(tb.CanUndo);
  726. Assert.False(tb.CanRedo);
  727. }
  728. }
  729. [Fact]
  730. public void CanUndo_CanRedo_and_Programmatic_Undo_Redo_Works()
  731. {
  732. using (UnitTestApplication.Start(Services))
  733. {
  734. var tb = new TextBox
  735. {
  736. Template = CreateTemplate(),
  737. };
  738. tb.Measure(Size.Infinity);
  739. // See GH #6024 for a bit more insight on when Undo/Redo snapshots are taken:
  740. // - Every 'Space', but only when space is handled in OnKeyDown - Spaces in TextInput event won't work
  741. // - Every 7 chars in a long word
  742. RaiseTextEvent(tb, "ABC");
  743. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  744. RaiseTextEvent(tb, "DEF");
  745. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  746. RaiseTextEvent(tb, "123");
  747. // NOTE: the spaces won't actually add spaces b/c they're sent only as key events and not Text events
  748. // so our final text is without spaces
  749. Assert.Equal("ABCDEF123", tb.Text);
  750. Assert.True(tb.CanUndo);
  751. tb.Undo();
  752. // Undo will take us back one step
  753. Assert.Equal("ABCDEF", tb.Text);
  754. Assert.True(tb.CanRedo);
  755. tb.Redo();
  756. // Redo should restore us
  757. Assert.Equal("ABCDEF123", tb.Text);
  758. }
  759. }
  760. [Fact]
  761. public void Setting_UndoLimit_Clears_Undo_Redo()
  762. {
  763. using (UnitTestApplication.Start(Services))
  764. {
  765. var tb = new TextBox
  766. {
  767. Template = CreateTemplate(),
  768. };
  769. tb.Measure(Size.Infinity);
  770. // This is all the same as the above test (CanUndo_CanRedo_and_Programmatic_Undo_Redo_Works)
  771. // We do this to get the undo/redo stacks in a state where both are active
  772. RaiseTextEvent(tb, "ABC");
  773. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  774. RaiseTextEvent(tb, "DEF");
  775. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  776. RaiseTextEvent(tb, "123");
  777. Assert.Equal("ABCDEF123", tb.Text);
  778. Assert.True(tb.CanUndo);
  779. tb.Undo();
  780. // Undo will take us back one step
  781. Assert.Equal("ABCDEF", tb.Text);
  782. Assert.True(tb.CanRedo);
  783. tb.Redo();
  784. // Redo should restore us
  785. Assert.Equal("ABCDEF123", tb.Text);
  786. // Change the undo limit, this should clear both stacks setting CanUndo and CanRedo to false
  787. tb.UndoLimit = 1;
  788. Assert.False(tb.CanUndo);
  789. Assert.False(tb.CanRedo);
  790. }
  791. }
  792. [Fact]
  793. public void Setting_IsUndoEnabled_To_False_Clears_Undo_Redo()
  794. {
  795. using (UnitTestApplication.Start(Services))
  796. {
  797. var tb = new TextBox
  798. {
  799. Template = CreateTemplate(),
  800. };
  801. tb.Measure(Size.Infinity);
  802. // This is all the same as the above test (CanUndo_CanRedo_and_Programmatic_Undo_Redo_Works)
  803. // We do this to get the undo/redo stacks in a state where both are active
  804. RaiseTextEvent(tb, "ABC");
  805. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  806. RaiseTextEvent(tb, "DEF");
  807. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  808. RaiseTextEvent(tb, "123");
  809. Assert.Equal("ABCDEF123", tb.Text);
  810. Assert.True(tb.CanUndo);
  811. tb.Undo();
  812. // Undo will take us back one step
  813. Assert.Equal("ABCDEF", tb.Text);
  814. Assert.True(tb.CanRedo);
  815. tb.Redo();
  816. // Redo should restore us
  817. Assert.Equal("ABCDEF123", tb.Text);
  818. // Disable Undo/Redo, this should clear both stacks setting CanUndo and CanRedo to false
  819. tb.IsUndoEnabled = false;
  820. Assert.False(tb.CanUndo);
  821. Assert.False(tb.CanRedo);
  822. }
  823. }
  824. [Fact]
  825. public void UndoLimit_Count_Is_Respected()
  826. {
  827. using (UnitTestApplication.Start(Services))
  828. {
  829. var tb = new TextBox
  830. {
  831. Template = CreateTemplate(),
  832. UndoLimit = 3 // Something small for this test
  833. };
  834. tb.Measure(Size.Infinity);
  835. // Push 3 undoable actions, we should only be able to recover 2
  836. RaiseTextEvent(tb, "ABC");
  837. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  838. RaiseTextEvent(tb, "DEF");
  839. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  840. RaiseTextEvent(tb, "123");
  841. Assert.Equal("ABCDEF123", tb.Text);
  842. // Undo will take us back one step
  843. tb.Undo();
  844. Assert.Equal("ABCDEF", tb.Text);
  845. // Undo again
  846. tb.Undo();
  847. Assert.Equal("ABC", tb.Text);
  848. // We now should not be able to undo again
  849. Assert.False(tb.CanUndo);
  850. }
  851. }
  852. private static TestServices FocusServices => TestServices.MockThreadingInterface.With(
  853. focusManager: new FocusManager(),
  854. keyboardDevice: () => new KeyboardDevice(),
  855. keyboardNavigation: new KeyboardNavigationHandler(),
  856. inputManager: new InputManager(),
  857. standardCursorFactory: Mock.Of<ICursorFactory>(),
  858. textShaperImpl: new MockTextShaperImpl(),
  859. fontManagerImpl: new MockFontManagerImpl());
  860. private static TestServices Services => TestServices.MockThreadingInterface.With(
  861. standardCursorFactory: Mock.Of<ICursorFactory>(),
  862. renderInterface: new MockPlatformRenderInterface(),
  863. textShaperImpl: new MockTextShaperImpl(),
  864. fontManagerImpl: new MockFontManagerImpl());
  865. private IControlTemplate CreateTemplate()
  866. {
  867. return new FuncControlTemplate<TextBox>((control, scope) =>
  868. new TextPresenter
  869. {
  870. Name = "PART_TextPresenter",
  871. [!!TextPresenter.TextProperty] = new Binding
  872. {
  873. Path = nameof(TextPresenter.Text),
  874. Mode = BindingMode.TwoWay,
  875. Priority = BindingPriority.Template,
  876. RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),
  877. },
  878. [!!TextPresenter.CaretIndexProperty] = new Binding
  879. {
  880. Path = nameof(TextPresenter.CaretIndex),
  881. Mode = BindingMode.TwoWay,
  882. Priority = BindingPriority.Template,
  883. RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),
  884. }
  885. }.RegisterInNameScope(scope));
  886. }
  887. private void RaiseKeyEvent(TextBox textBox, Key key, KeyModifiers inputModifiers)
  888. {
  889. textBox.RaiseEvent(new KeyEventArgs
  890. {
  891. RoutedEvent = InputElement.KeyDownEvent,
  892. KeyModifiers = inputModifiers,
  893. Key = key
  894. });
  895. }
  896. private void RaiseTextEvent(TextBox textBox, string text)
  897. {
  898. textBox.RaiseEvent(new TextInputEventArgs
  899. {
  900. RoutedEvent = InputElement.TextInputEvent,
  901. Text = text
  902. });
  903. }
  904. private class Class1 : NotifyingBase
  905. {
  906. private int _foo;
  907. private string _bar;
  908. public int Foo
  909. {
  910. get { return _foo; }
  911. set { _foo = value; RaisePropertyChanged(); }
  912. }
  913. public string Bar
  914. {
  915. get { return _bar; }
  916. set { _bar = value; RaisePropertyChanged(); }
  917. }
  918. }
  919. private class ClipboardStub : IClipboard // in order to get tests working that use the clipboard
  920. {
  921. private string _text;
  922. public Task<string> GetTextAsync() => Task.FromResult(_text);
  923. public Task SetTextAsync(string text)
  924. {
  925. _text = text;
  926. return Task.CompletedTask;
  927. }
  928. public Task ClearAsync()
  929. {
  930. _text = null;
  931. return Task.CompletedTask;
  932. }
  933. public Task SetDataObjectAsync(IDataObject data) => Task.CompletedTask;
  934. public Task<string[]> GetFormatsAsync() => Task.FromResult(Array.Empty<string>());
  935. public Task<object> GetDataAsync(string format) => Task.FromResult((object)null);
  936. }
  937. private class TestContextMenu : ContextMenu
  938. {
  939. public TestContextMenu()
  940. {
  941. IsOpen = true;
  942. }
  943. }
  944. }
  945. }