TextBoxTests.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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. private static TestServices FocusServices => TestServices.MockThreadingInterface.With(
  715. focusManager: new FocusManager(),
  716. keyboardDevice: () => new KeyboardDevice(),
  717. keyboardNavigation: new KeyboardNavigationHandler(),
  718. inputManager: new InputManager(),
  719. standardCursorFactory: Mock.Of<ICursorFactory>(),
  720. textShaperImpl: new MockTextShaperImpl(),
  721. fontManagerImpl: new MockFontManagerImpl());
  722. private static TestServices Services => TestServices.MockThreadingInterface.With(
  723. standardCursorFactory: Mock.Of<ICursorFactory>(),
  724. renderInterface: new MockPlatformRenderInterface(),
  725. textShaperImpl: new MockTextShaperImpl(),
  726. fontManagerImpl: new MockFontManagerImpl());
  727. private IControlTemplate CreateTemplate()
  728. {
  729. return new FuncControlTemplate<TextBox>((control, scope) =>
  730. new TextPresenter
  731. {
  732. Name = "PART_TextPresenter",
  733. [!!TextPresenter.TextProperty] = new Binding
  734. {
  735. Path = nameof(TextPresenter.Text),
  736. Mode = BindingMode.TwoWay,
  737. Priority = BindingPriority.TemplatedParent,
  738. RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),
  739. },
  740. [!!TextPresenter.CaretIndexProperty] = new Binding
  741. {
  742. Path = nameof(TextPresenter.CaretIndex),
  743. Mode = BindingMode.TwoWay,
  744. Priority = BindingPriority.TemplatedParent,
  745. RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),
  746. }
  747. }.RegisterInNameScope(scope));
  748. }
  749. private void RaiseKeyEvent(TextBox textBox, Key key, KeyModifiers inputModifiers)
  750. {
  751. textBox.RaiseEvent(new KeyEventArgs
  752. {
  753. RoutedEvent = InputElement.KeyDownEvent,
  754. KeyModifiers = inputModifiers,
  755. Key = key
  756. });
  757. }
  758. private void RaiseTextEvent(TextBox textBox, string text)
  759. {
  760. textBox.RaiseEvent(new TextInputEventArgs
  761. {
  762. RoutedEvent = InputElement.TextInputEvent,
  763. Text = text
  764. });
  765. }
  766. private class Class1 : NotifyingBase
  767. {
  768. private int _foo;
  769. private string _bar;
  770. public int Foo
  771. {
  772. get { return _foo; }
  773. set { _foo = value; RaisePropertyChanged(); }
  774. }
  775. public string Bar
  776. {
  777. get { return _bar; }
  778. set { _bar = value; RaisePropertyChanged(); }
  779. }
  780. }
  781. private class ClipboardStub : IClipboard // in order to get tests working that use the clipboard
  782. {
  783. private string _text;
  784. public Task<string> GetTextAsync() => Task.FromResult(_text);
  785. public Task SetTextAsync(string text)
  786. {
  787. _text = text;
  788. return Task.CompletedTask;
  789. }
  790. public Task ClearAsync()
  791. {
  792. _text = null;
  793. return Task.CompletedTask;
  794. }
  795. public Task SetDataObjectAsync(IDataObject data) => Task.CompletedTask;
  796. public Task<string[]> GetFormatsAsync() => Task.FromResult(Array.Empty<string>());
  797. public Task<object> GetDataAsync(string format) => Task.FromResult((object)null);
  798. }
  799. private class TestContextMenu : ContextMenu
  800. {
  801. public TestContextMenu()
  802. {
  803. IsOpen = true;
  804. }
  805. }
  806. }
  807. }