MaskedTextBoxTests.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Reactive.Linq;
  5. using System.Threading.Tasks;
  6. using Avalonia.Controls.Presenters;
  7. using Avalonia.Controls.Primitives;
  8. using Avalonia.Controls.Templates;
  9. using Avalonia.Data;
  10. using Avalonia.Input;
  11. using Avalonia.Input.Platform;
  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 MaskedTextBoxTests
  20. {
  21. [Fact]
  22. public void Opening_Context_Menu_Does_not_Lose_Selection()
  23. {
  24. using (Start(FocusServices))
  25. {
  26. var target1 = new MaskedTextBox
  27. {
  28. Template = CreateTemplate(),
  29. Text = "1234",
  30. ContextMenu = new TestContextMenu()
  31. };
  32. var target2 = new MaskedTextBox
  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 (Start(FocusServices))
  56. {
  57. var target1 = new MaskedTextBox
  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(MaskedTextBox)).DefaultBindingMode);
  87. }
  88. [Fact]
  89. public void CaretIndex_Can_Moved_To_Position_After_The_End_Of_Text_With_Arrow_Key()
  90. {
  91. using (Start())
  92. {
  93. var target = new MaskedTextBox
  94. {
  95. Template = CreateTemplate(),
  96. Text = "1234"
  97. };
  98. target.ApplyTemplate();
  99. target.CaretIndex = 3;
  100. target.Measure(Size.Infinity);
  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 (Start())
  109. {
  110. var target = new MaskedTextBox
  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 (Start())
  125. {
  126. var target = new MaskedTextBox
  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 (Start())
  139. {
  140. var target = new MaskedTextBox
  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 (Start())
  153. {
  154. MaskedTextBox textBox = new MaskedTextBox
  155. {
  156. Template = CreateTemplate(),
  157. Text = "First Second Third Fourth",
  158. CaretIndex = 5
  159. };
  160. textBox.ApplyTemplate();
  161. // (First| Second Third Fourth)
  162. RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
  163. Assert.Equal(" Second Third Fourth", textBox.Text);
  164. // ( Second |Third Fourth)
  165. textBox.CaretIndex = 8;
  166. RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
  167. Assert.Equal(" Third Fourth", textBox.Text);
  168. // ( Thi|rd Fourth)
  169. textBox.CaretIndex = 4;
  170. RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
  171. Assert.Equal(" rd Fourth", textBox.Text);
  172. // ( rd F[ou]rth)
  173. textBox.SelectionStart = 5;
  174. textBox.SelectionEnd = 7;
  175. RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
  176. Assert.Equal(" rd Frth", textBox.Text);
  177. // ( |rd Frth)
  178. textBox.CaretIndex = 1;
  179. RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
  180. Assert.Equal("rd Frth", textBox.Text);
  181. }
  182. }
  183. [Fact]
  184. public void Control_Delete_Should_Remove_The_Word_After_The_Caret_If_There_Is_No_Selection()
  185. {
  186. using (Start())
  187. {
  188. var textBox = new MaskedTextBox
  189. {
  190. Template = CreateTemplate(),
  191. Text = "First Second Third Fourth",
  192. CaretIndex = 19
  193. };
  194. textBox.ApplyTemplate();
  195. // (First Second Third |Fourth)
  196. RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
  197. Assert.Equal("First Second Third ", textBox.Text);
  198. // (First Second |Third )
  199. textBox.CaretIndex = 13;
  200. RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
  201. Assert.Equal("First Second ", textBox.Text);
  202. // (First Sec|ond )
  203. textBox.CaretIndex = 9;
  204. RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
  205. Assert.Equal("First Sec", textBox.Text);
  206. // (Fi[rs]t Sec )
  207. textBox.SelectionStart = 2;
  208. textBox.SelectionEnd = 4;
  209. RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
  210. Assert.Equal("Fit Sec", textBox.Text);
  211. // (Fit Sec| )
  212. textBox.Text += " ";
  213. textBox.CaretIndex = 7;
  214. RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
  215. Assert.Equal("Fit Sec", textBox.Text);
  216. }
  217. }
  218. [Fact]
  219. public void Setting_SelectionStart_To_SelectionEnd_Sets_CaretPosition_To_SelectionStart()
  220. {
  221. using (Start())
  222. {
  223. var textBox = new MaskedTextBox
  224. {
  225. Text = "0123456789"
  226. };
  227. textBox.SelectionStart = 2;
  228. textBox.SelectionEnd = 2;
  229. Assert.Equal(2, textBox.CaretIndex);
  230. }
  231. }
  232. [Fact]
  233. public void Setting_Text_Updates_CaretPosition()
  234. {
  235. using (Start())
  236. {
  237. var target = new MaskedTextBox
  238. {
  239. Text = "Initial Text",
  240. CaretIndex = 11
  241. };
  242. var invoked = false;
  243. target.GetObservable(TextBox.TextProperty).Skip(1).Subscribe(_ =>
  244. {
  245. // Caret index should be set before Text changed notification, as we don't want
  246. // to notify with an invalid CaretIndex.
  247. Assert.Equal(7, target.CaretIndex);
  248. invoked = true;
  249. });
  250. target.Text = "Changed";
  251. Assert.True(invoked);
  252. }
  253. }
  254. [Fact]
  255. public void Press_Enter_Does_Not_Accept_Return()
  256. {
  257. using (Start())
  258. {
  259. var target = new MaskedTextBox
  260. {
  261. Template = CreateTemplate(),
  262. AcceptsReturn = false,
  263. Text = "1234"
  264. };
  265. RaiseKeyEvent(target, Key.Enter, 0);
  266. Assert.Equal("1234", target.Text);
  267. }
  268. }
  269. [Fact]
  270. public void Press_Enter_Add_Default_Newline()
  271. {
  272. using (Start())
  273. {
  274. var target = new MaskedTextBox
  275. {
  276. Template = CreateTemplate(),
  277. AcceptsReturn = true
  278. };
  279. target.ApplyTemplate();
  280. RaiseKeyEvent(target, Key.Enter, 0);
  281. Assert.Equal(Environment.NewLine, target.Text);
  282. }
  283. }
  284. [Theory]
  285. [InlineData("00/00/0000", "12102000", "12/10/2000")]
  286. [InlineData("LLLL", "дбs", "____")]
  287. [InlineData("AA", "Ü1", "__")]
  288. public void AsciiOnly_Should_Not_Accept_Non_Ascii(string mask, string textEventArg, string expected)
  289. {
  290. using (Start())
  291. {
  292. var target = new MaskedTextBox
  293. {
  294. Template = CreateTemplate(),
  295. Mask = mask,
  296. AsciiOnly = true
  297. };
  298. RaiseTextEvent(target, textEventArg);
  299. Assert.Equal(expected, target.Text);
  300. }
  301. }
  302. [Fact]
  303. public void Programmatically_Set_Text_Should_Not_Be_Removed_On_Key_Press()
  304. {
  305. using (Start())
  306. {
  307. var target = new MaskedTextBox
  308. {
  309. Template = CreateTemplate(),
  310. Mask = "00:00:00.000",
  311. Text = "12:34:56.000"
  312. };
  313. target.CaretIndex = target.Text.Length;
  314. RaiseKeyEvent(target, Key.Back, 0);
  315. Assert.Equal("12:34:56.00_", target.Text);
  316. }
  317. }
  318. [Fact]
  319. public void Invalid_Programmatically_Set_Text_Should_Be_Rejected()
  320. {
  321. using (Start())
  322. {
  323. var target = new MaskedTextBox
  324. {
  325. Template = CreateTemplate(),
  326. Mask = "00:00:00.000",
  327. Text = "12:34:560000"
  328. };
  329. Assert.Equal("__:__:__.___", target.Text);
  330. }
  331. }
  332. [Theory]
  333. [InlineData("00/00/0000", "12102000", "**/**/****")]
  334. [InlineData("LLLL", "дбs", "***_")]
  335. [InlineData("AA#00", "S2 33", "**_**")]
  336. public void PasswordChar_Should_Hide_User_Input(string mask, string textEventArg, string expected)
  337. {
  338. using (Start())
  339. {
  340. var target = new MaskedTextBox
  341. {
  342. Template = CreateTemplate(),
  343. Mask = mask,
  344. PasswordChar = '*'
  345. };
  346. RaiseTextEvent(target, textEventArg);
  347. Assert.Equal(expected, target.Text);
  348. }
  349. }
  350. [Theory]
  351. [InlineData("00/00/0000", "12102000", "12/10/2000")]
  352. [InlineData("LLLL", "дбs", "дбs_")]
  353. [InlineData("AA#00", "S2 33", "S2_33")]
  354. public void Mask_Should_Work_Correctly(string mask, string textEventArg, string expected)
  355. {
  356. using (Start())
  357. {
  358. var target = new MaskedTextBox
  359. {
  360. Template = CreateTemplate(),
  361. Mask = mask
  362. };
  363. RaiseTextEvent(target, textEventArg);
  364. Assert.Equal(expected, target.Text);
  365. }
  366. }
  367. [Fact]
  368. public void Press_Enter_Add_Custom_Newline()
  369. {
  370. using (Start())
  371. {
  372. var target = new MaskedTextBox
  373. {
  374. Template = CreateTemplate(),
  375. AcceptsReturn = true,
  376. NewLine = "Test"
  377. };
  378. target.ApplyTemplate();
  379. RaiseKeyEvent(target, Key.Enter, 0);
  380. Assert.Equal("Test", target.Text);
  381. }
  382. }
  383. [Theory]
  384. [InlineData(new object[] { false, TextWrapping.NoWrap, ScrollBarVisibility.Hidden })]
  385. [InlineData(new object[] { false, TextWrapping.Wrap, ScrollBarVisibility.Disabled })]
  386. [InlineData(new object[] { true, TextWrapping.NoWrap, ScrollBarVisibility.Auto })]
  387. [InlineData(new object[] { true, TextWrapping.Wrap, ScrollBarVisibility.Disabled })]
  388. public void Has_Correct_Horizontal_ScrollBar_Visibility(
  389. bool acceptsReturn,
  390. TextWrapping wrapping,
  391. ScrollBarVisibility expected)
  392. {
  393. using (Start())
  394. {
  395. var target = new MaskedTextBox
  396. {
  397. AcceptsReturn = acceptsReturn,
  398. TextWrapping = wrapping,
  399. };
  400. Assert.Equal(expected, ScrollViewer.GetHorizontalScrollBarVisibility(target));
  401. }
  402. }
  403. [Fact]
  404. public void SelectionEnd_Doesnt_Cause_Exception()
  405. {
  406. using (Start())
  407. {
  408. var target = new MaskedTextBox
  409. {
  410. Template = CreateTemplate(),
  411. Text = "0123456789"
  412. };
  413. target.SelectionStart = 0;
  414. target.SelectionEnd = 9;
  415. target.Text = "123";
  416. RaiseTextEvent(target, "456");
  417. Assert.True(true);
  418. }
  419. }
  420. [Fact]
  421. public void SelectionStart_Doesnt_Cause_Exception()
  422. {
  423. using (Start())
  424. {
  425. var target = new MaskedTextBox
  426. {
  427. Template = CreateTemplate(),
  428. Text = "0123456789"
  429. };
  430. target.SelectionStart = 8;
  431. target.SelectionEnd = 9;
  432. target.Text = "123";
  433. RaiseTextEvent(target, "456");
  434. Assert.True(true);
  435. }
  436. }
  437. [Fact]
  438. public void SelectionStartEnd_Are_Valid_AterTextChange()
  439. {
  440. using (Start())
  441. {
  442. var target = new MaskedTextBox
  443. {
  444. Template = CreateTemplate(),
  445. Text = "0123456789"
  446. };
  447. target.SelectionStart = 8;
  448. target.SelectionEnd = 9;
  449. target.Text = "123";
  450. Assert.True(target.SelectionStart <= "123".Length);
  451. Assert.True(target.SelectionEnd <= "123".Length);
  452. }
  453. }
  454. [Fact]
  455. public void SelectedText_Changes_OnSelectionChange()
  456. {
  457. using (Start())
  458. {
  459. var target = new MaskedTextBox
  460. {
  461. Template = CreateTemplate(),
  462. Text = "0123456789"
  463. };
  464. Assert.True(target.SelectedText == "");
  465. target.SelectionStart = 2;
  466. target.SelectionEnd = 4;
  467. Assert.True(target.SelectedText == "23");
  468. }
  469. }
  470. [Fact]
  471. public void SelectedText_EditsText()
  472. {
  473. using (Start())
  474. {
  475. var target = new MaskedTextBox
  476. {
  477. Template = CreateTemplate(),
  478. Text = "0123"
  479. };
  480. target.SelectedText = "AA";
  481. Assert.True(target.Text == "AA0123");
  482. target.SelectionStart = 1;
  483. target.SelectionEnd = 3;
  484. target.SelectedText = "BB";
  485. Assert.True(target.Text == "ABB123");
  486. }
  487. }
  488. [Fact]
  489. public void SelectedText_CanClearText()
  490. {
  491. using (Start())
  492. {
  493. var target = new MaskedTextBox
  494. {
  495. Template = CreateTemplate(),
  496. Text = "0123"
  497. };
  498. target.SelectionStart = 1;
  499. target.SelectionEnd = 3;
  500. target.SelectedText = "";
  501. Assert.True(target.Text == "03");
  502. }
  503. }
  504. [Fact]
  505. public void SelectedText_NullClearsText()
  506. {
  507. using (Start())
  508. {
  509. var target = new MaskedTextBox
  510. {
  511. Template = CreateTemplate(),
  512. Text = "0123"
  513. };
  514. target.SelectionStart = 1;
  515. target.SelectionEnd = 3;
  516. target.SelectedText = null;
  517. Assert.True(target.Text == "03");
  518. }
  519. }
  520. [Fact]
  521. public void CoerceCaretIndex_Doesnt_Cause_Exception_with_malformed_line_ending()
  522. {
  523. using (Start())
  524. {
  525. var target = new MaskedTextBox
  526. {
  527. Template = CreateTemplate(),
  528. Text = "0123456789\r"
  529. };
  530. target.CaretIndex = 11;
  531. Assert.True(true);
  532. }
  533. }
  534. [Theory]
  535. [InlineData(Key.Up)]
  536. [InlineData(Key.Down)]
  537. [InlineData(Key.Home)]
  538. [InlineData(Key.End)]
  539. public void Textbox_doesnt_crash_when_Receives_input_and_template_not_applied(Key key)
  540. {
  541. using (Start(FocusServices))
  542. {
  543. var target1 = new MaskedTextBox
  544. {
  545. Template = CreateTemplate(),
  546. Text = "1234",
  547. IsVisible = false
  548. };
  549. var root = new TestRoot { Child = target1 };
  550. target1.Focus();
  551. Assert.True(target1.IsFocused);
  552. RaiseKeyEvent(target1, key, KeyModifiers.None);
  553. }
  554. }
  555. [Fact]
  556. public void TextBox_GotFocus_And_LostFocus_Work_Properly()
  557. {
  558. using (Start(FocusServices))
  559. {
  560. var target1 = new MaskedTextBox
  561. {
  562. Template = CreateTemplate(),
  563. Text = "1234"
  564. };
  565. var target2 = new MaskedTextBox
  566. {
  567. Template = CreateTemplate(),
  568. Text = "5678"
  569. };
  570. var sp = new StackPanel();
  571. sp.Children.Add(target1);
  572. sp.Children.Add(target2);
  573. target1.ApplyTemplate();
  574. target2.ApplyTemplate();
  575. var root = new TestRoot { Child = sp };
  576. var gfcount = 0;
  577. var lfcount = 0;
  578. target1.GotFocus += (s, e) => gfcount++;
  579. target2.LostFocus += (s, e) => lfcount++;
  580. target2.Focus();
  581. Assert.False(target1.IsFocused);
  582. Assert.True(target2.IsFocused);
  583. target1.Focus();
  584. Assert.False(target2.IsFocused);
  585. Assert.True(target1.IsFocused);
  586. Assert.Equal(1, gfcount);
  587. Assert.Equal(1, lfcount);
  588. }
  589. }
  590. [Fact]
  591. public void TextBox_CaretIndex_Persists_When_Focus_Lost()
  592. {
  593. using (Start(FocusServices))
  594. {
  595. var target1 = new MaskedTextBox
  596. {
  597. Template = CreateTemplate(),
  598. Text = "1234"
  599. };
  600. var target2 = new MaskedTextBox
  601. {
  602. Template = CreateTemplate(),
  603. Text = "5678"
  604. };
  605. var sp = new StackPanel();
  606. sp.Children.Add(target1);
  607. sp.Children.Add(target2);
  608. target1.ApplyTemplate();
  609. target2.ApplyTemplate();
  610. var root = new TestRoot { Child = sp };
  611. target2.Focus();
  612. target2.CaretIndex = 2;
  613. Assert.False(target1.IsFocused);
  614. Assert.True(target2.IsFocused);
  615. target1.Focus();
  616. Assert.Equal(2, target2.CaretIndex);
  617. }
  618. }
  619. [Fact]
  620. public void TextBox_Reveal_Password_Reset_When_Lost_Focus()
  621. {
  622. using (Start(FocusServices))
  623. {
  624. var target1 = new MaskedTextBox
  625. {
  626. Template = CreateTemplate(),
  627. Text = "1234",
  628. PasswordChar = '*'
  629. };
  630. var target2 = new MaskedTextBox
  631. {
  632. Template = CreateTemplate(),
  633. Text = "5678"
  634. };
  635. var sp = new StackPanel();
  636. sp.Children.Add(target1);
  637. sp.Children.Add(target2);
  638. target1.ApplyTemplate();
  639. target2.ApplyTemplate();
  640. var root = new TestRoot { Child = sp };
  641. target1.Focus();
  642. target1.RevealPassword = true;
  643. target2.Focus();
  644. Assert.False(target1.RevealPassword);
  645. }
  646. }
  647. [Fact]
  648. public void Setting_Bound_Text_To_Null_Works()
  649. {
  650. using (Start())
  651. {
  652. var source = new Class1 { Bar = "bar" };
  653. var target = new MaskedTextBox { DataContext = source };
  654. target.Bind(TextBox.TextProperty, new Binding("Bar"));
  655. Assert.Equal("bar", target.Text);
  656. source.Bar = null;
  657. Assert.Null(target.Text);
  658. }
  659. }
  660. [Theory]
  661. [InlineData("abc", "d", 3, 0, 0, false, "abc")]
  662. [InlineData("abc", "dd", 4, 3, 3, false, "abcd")]
  663. [InlineData("abc", "ddd", 3, 0, 2, true, "ddc")]
  664. [InlineData("abc", "dddd", 4, 1, 3, true, "addd")]
  665. [InlineData("abc", "ddddd", 5, 3, 3, true, "abcdd")]
  666. public void MaxLength_Works_Properly(
  667. string initalText,
  668. string textInput,
  669. int maxLength,
  670. int selectionStart,
  671. int selectionEnd,
  672. bool fromClipboard,
  673. string expected)
  674. {
  675. using (Start())
  676. {
  677. var target = new MaskedTextBox
  678. {
  679. Template = CreateTemplate(),
  680. Text = initalText,
  681. MaxLength = maxLength,
  682. SelectionStart = selectionStart,
  683. SelectionEnd = selectionEnd
  684. };
  685. target.ApplyTemplate();
  686. if (fromClipboard)
  687. {
  688. AvaloniaLocator.CurrentMutable.Bind<IClipboard>().ToSingleton<ClipboardStub>();
  689. var clipboard = AvaloniaLocator.CurrentMutable.GetService<IClipboard>();
  690. clipboard.SetTextAsync(textInput).GetAwaiter().GetResult();
  691. RaiseKeyEvent(target, Key.V, KeyModifiers.Control);
  692. clipboard.ClearAsync().GetAwaiter().GetResult();
  693. }
  694. else
  695. {
  696. RaiseTextEvent(target, textInput);
  697. }
  698. Assert.Equal(expected, target.Text);
  699. }
  700. }
  701. [Theory]
  702. [InlineData(Key.X, KeyModifiers.Control)]
  703. [InlineData(Key.Back, KeyModifiers.None)]
  704. [InlineData(Key.Delete, KeyModifiers.None)]
  705. [InlineData(Key.Tab, KeyModifiers.None)]
  706. [InlineData(Key.Enter, KeyModifiers.None)]
  707. public void Keys_Allow_Undo(Key key, KeyModifiers modifiers)
  708. {
  709. using (Start())
  710. {
  711. var target = new MaskedTextBox
  712. {
  713. Template = CreateTemplate(),
  714. Text = "0123",
  715. AcceptsReturn = true,
  716. AcceptsTab = true
  717. };
  718. target.SelectionStart = 1;
  719. target.SelectionEnd = 3;
  720. AvaloniaLocator.CurrentMutable
  721. .Bind<Input.Platform.IClipboard>().ToSingleton<ClipboardStub>();
  722. RaiseKeyEvent(target, key, modifiers);
  723. RaiseKeyEvent(target, Key.Z, KeyModifiers.Control); // undo
  724. Assert.True(target.Text == "0123");
  725. }
  726. }
  727. private static TestServices FocusServices => TestServices.MockThreadingInterface.With(
  728. focusManager: new FocusManager(),
  729. keyboardDevice: () => new KeyboardDevice(),
  730. keyboardNavigation: new KeyboardNavigationHandler(),
  731. inputManager: new InputManager(),
  732. renderInterface: new MockPlatformRenderInterface(),
  733. fontManagerImpl: new MockFontManagerImpl(),
  734. textShaperImpl: new MockTextShaperImpl(),
  735. standardCursorFactory: Mock.Of<ICursorFactory>());
  736. private static TestServices Services => TestServices.MockThreadingInterface.With(
  737. standardCursorFactory: Mock.Of<ICursorFactory>(),
  738. textShaperImpl: new MockTextShaperImpl(),
  739. fontManagerImpl: new MockFontManagerImpl());
  740. private IControlTemplate CreateTemplate()
  741. {
  742. return new FuncControlTemplate<MaskedTextBox>((control, scope) =>
  743. new TextPresenter
  744. {
  745. Name = "PART_TextPresenter",
  746. [!!TextPresenter.TextProperty] = new Binding
  747. {
  748. Path = nameof(TextPresenter.Text),
  749. Mode = BindingMode.TwoWay,
  750. Priority = BindingPriority.TemplatedParent,
  751. RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),
  752. },
  753. [!!TextPresenter.CaretIndexProperty] = new Binding
  754. {
  755. Path = nameof(TextPresenter.CaretIndex),
  756. Mode = BindingMode.TwoWay,
  757. Priority = BindingPriority.TemplatedParent,
  758. RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),
  759. }
  760. }.RegisterInNameScope(scope));
  761. }
  762. private void RaiseKeyEvent(MaskedTextBox textBox, Key key, KeyModifiers inputModifiers)
  763. {
  764. textBox.RaiseEvent(new KeyEventArgs
  765. {
  766. RoutedEvent = InputElement.KeyDownEvent,
  767. KeyModifiers = inputModifiers,
  768. Key = key
  769. });
  770. }
  771. private void RaiseTextEvent(MaskedTextBox textBox, string text)
  772. {
  773. textBox.RaiseEvent(new TextInputEventArgs
  774. {
  775. RoutedEvent = InputElement.TextInputEvent,
  776. Text = text
  777. });
  778. }
  779. private static IDisposable Start(TestServices services = null)
  780. {
  781. CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
  782. return UnitTestApplication.Start(services ?? Services);
  783. }
  784. private class Class1 : NotifyingBase
  785. {
  786. private int _foo;
  787. private string _bar;
  788. public int Foo
  789. {
  790. get { return _foo; }
  791. set { _foo = value; RaisePropertyChanged(); }
  792. }
  793. public string Bar
  794. {
  795. get { return _bar; }
  796. set { _bar = value; RaisePropertyChanged(); }
  797. }
  798. }
  799. private class ClipboardStub : IClipboard // in order to get tests working that use the clipboard
  800. {
  801. private string _text;
  802. public Task<string> GetTextAsync() => Task.FromResult(_text);
  803. public Task SetTextAsync(string text)
  804. {
  805. _text = text;
  806. return Task.CompletedTask;
  807. }
  808. public Task ClearAsync()
  809. {
  810. _text = null;
  811. return Task.CompletedTask;
  812. }
  813. public Task SetDataObjectAsync(IDataObject data) => Task.CompletedTask;
  814. public Task<string[]> GetFormatsAsync() => Task.FromResult(Array.Empty<string>());
  815. public Task<object> GetDataAsync(string format) => Task.FromResult((object)null);
  816. }
  817. private class TestContextMenu : ContextMenu
  818. {
  819. public TestContextMenu()
  820. {
  821. IsOpen = true;
  822. }
  823. }
  824. }
  825. }