MaskedTextBoxTests.cs 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. using System;
  2. using System.Globalization;
  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.Headless;
  10. using Avalonia.Input;
  11. using Avalonia.Input.Platform;
  12. using Avalonia.Layout;
  13. using Avalonia.Media;
  14. using Avalonia.Platform;
  15. using Avalonia.UnitTests;
  16. using Moq;
  17. using Xunit;
  18. namespace Avalonia.Controls.UnitTests
  19. {
  20. public class MaskedTextBoxTests : ScopedTestBase
  21. {
  22. [Fact]
  23. public void Opening_Context_Menu_Does_not_Lose_Selection()
  24. {
  25. using (Start(FocusServices))
  26. {
  27. var target1 = new MaskedTextBox
  28. {
  29. Template = CreateTemplate(),
  30. Text = "1234",
  31. ContextMenu = new TestContextMenu()
  32. };
  33. var target2 = new MaskedTextBox
  34. {
  35. Template = CreateTemplate(),
  36. Text = "5678"
  37. };
  38. var sp = new StackPanel();
  39. sp.Children.Add(target1);
  40. sp.Children.Add(target2);
  41. target1.ApplyTemplate();
  42. target2.ApplyTemplate();
  43. var root = new TestRoot() { Child = sp };
  44. target1.SelectionStart = 0;
  45. target1.SelectionEnd = 3;
  46. target1.Focus();
  47. Assert.False(target2.IsFocused);
  48. Assert.True(target1.IsFocused);
  49. target2.Focus();
  50. Assert.Equal("123", target1.SelectedText);
  51. }
  52. }
  53. [Fact]
  54. public void Opening_Context_Flyout_Does_not_Lose_Selection()
  55. {
  56. using (Start(FocusServices))
  57. {
  58. var target1 = new MaskedTextBox
  59. {
  60. Template = CreateTemplate(),
  61. Text = "1234",
  62. ContextFlyout = new MenuFlyout
  63. {
  64. Items =
  65. {
  66. new MenuItem { Header = "Item 1" },
  67. new MenuItem {Header = "Item 2" },
  68. new MenuItem {Header = "Item 3" }
  69. }
  70. }
  71. };
  72. target1.ApplyTemplate();
  73. var root = new TestRoot() { Child = target1 };
  74. target1.SelectionStart = 0;
  75. target1.SelectionEnd = 3;
  76. target1.Focus();
  77. Assert.True(target1.IsFocused);
  78. target1.ContextFlyout.ShowAt(target1);
  79. Assert.Equal("123", target1.SelectedText);
  80. }
  81. }
  82. [Fact]
  83. public void DefaultBindingMode_Should_Be_TwoWay()
  84. {
  85. Assert.Equal(
  86. BindingMode.TwoWay,
  87. TextBox.TextProperty.GetMetadata(typeof(MaskedTextBox)).DefaultBindingMode);
  88. }
  89. [Fact]
  90. public void CaretIndex_Can_Moved_To_Position_After_The_End_Of_Text_With_Arrow_Key()
  91. {
  92. using (Start())
  93. {
  94. var target = new MaskedTextBox
  95. {
  96. Template = CreateTemplate(),
  97. Text = "1234"
  98. };
  99. target.ApplyTemplate();
  100. target.CaretIndex = 3;
  101. target.Measure(Size.Infinity);
  102. RaiseKeyEvent(target, Key.Right, 0);
  103. Assert.Equal(4, target.CaretIndex);
  104. }
  105. }
  106. [Fact]
  107. public void Press_Ctrl_A_Select_All_Text()
  108. {
  109. using (Start())
  110. {
  111. var target = new MaskedTextBox
  112. {
  113. Template = CreateTemplate(),
  114. Text = "1234"
  115. };
  116. target.ApplyTemplate();
  117. RaiseKeyEvent(target, Key.A, KeyModifiers.Control);
  118. Assert.Equal(0, target.SelectionStart);
  119. Assert.Equal(4, target.SelectionEnd);
  120. }
  121. }
  122. [Fact]
  123. public void Press_Ctrl_A_Select_All_Null_Text()
  124. {
  125. using (Start())
  126. {
  127. var target = new MaskedTextBox
  128. {
  129. Template = CreateTemplate()
  130. };
  131. RaiseKeyEvent(target, Key.A, KeyModifiers.Control);
  132. Assert.Equal(0, target.SelectionStart);
  133. Assert.Equal(0, target.SelectionEnd);
  134. }
  135. }
  136. [Fact]
  137. public void Press_Ctrl_Z_Will_Not_Modify_Text()
  138. {
  139. using (Start())
  140. {
  141. var target = new MaskedTextBox
  142. {
  143. Template = CreateTemplate(),
  144. Text = "1234"
  145. };
  146. RaiseKeyEvent(target, Key.Z, KeyModifiers.Control);
  147. Assert.Equal("1234", target.Text);
  148. }
  149. }
  150. [Fact]
  151. public void Control_Backspace_Should_Remove_The_Word_Before_The_Caret_If_There_Is_No_Selection()
  152. {
  153. using (Start())
  154. {
  155. MaskedTextBox textBox = new MaskedTextBox
  156. {
  157. Template = CreateTemplate(),
  158. Text = "First Second Third Fourth",
  159. CaretIndex = 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 (Start())
  188. {
  189. var textBox = new MaskedTextBox
  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 (Start())
  223. {
  224. var textBox = new MaskedTextBox
  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 (Start())
  237. {
  238. var target = new MaskedTextBox
  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 (Start())
  259. {
  260. var target = new MaskedTextBox
  261. {
  262. Template = CreateTemplate(),
  263. AcceptsReturn = false,
  264. Text = "1234"
  265. };
  266. RaiseKeyEvent(target, Key.Enter, 0);
  267. Assert.Equal("1234", target.Text);
  268. }
  269. }
  270. [Fact]
  271. public void Press_Enter_Add_Default_Newline()
  272. {
  273. using (Start())
  274. {
  275. var target = new MaskedTextBox
  276. {
  277. Template = CreateTemplate(),
  278. AcceptsReturn = true
  279. };
  280. target.ApplyTemplate();
  281. RaiseKeyEvent(target, Key.Enter, 0);
  282. Assert.Equal(Environment.NewLine, target.Text);
  283. }
  284. }
  285. [Theory]
  286. [InlineData("00/00/0000", "12102000", "12/10/2000")]
  287. [InlineData("LLLL", "дбs", "____")]
  288. [InlineData("AA", "Ü1", "__")]
  289. public void AsciiOnly_Should_Not_Accept_Non_Ascii(string mask, string textEventArg, string expected)
  290. {
  291. using (Start())
  292. {
  293. var target = new MaskedTextBox
  294. {
  295. Template = CreateTemplate(),
  296. Mask = mask,
  297. AsciiOnly = true
  298. };
  299. RaiseTextEvent(target, textEventArg);
  300. Assert.Equal(expected, target.Text);
  301. }
  302. }
  303. [Fact]
  304. public void Programmatically_Set_Text_Should_Not_Be_Removed_On_Key_Press()
  305. {
  306. using (Start())
  307. {
  308. var target = new MaskedTextBox
  309. {
  310. Template = CreateTemplate(),
  311. Mask = "00:00:00.000",
  312. Text = "12:34:56.000"
  313. };
  314. target.CaretIndex = target.Text.Length;
  315. RaiseKeyEvent(target, Key.Back, 0);
  316. Assert.Equal("12:34:56.00_", target.Text);
  317. }
  318. }
  319. [Fact]
  320. public void Invalid_Programmatically_Set_Text_Should_Be_Rejected()
  321. {
  322. using (Start())
  323. {
  324. var target = new MaskedTextBox
  325. {
  326. Template = CreateTemplate(),
  327. Mask = "00:00:00.000",
  328. Text = "12:34:560000"
  329. };
  330. Assert.Equal("__:__:__.___", target.Text);
  331. }
  332. }
  333. [Theory]
  334. [InlineData("00/00/0000", "12102000", "**/**/****")]
  335. [InlineData("LLLL", "дбs", "***_")]
  336. [InlineData("AA#00", "S2 33", "**_**")]
  337. public void PasswordChar_Should_Hide_User_Input(string mask, string textEventArg, string expected)
  338. {
  339. using (Start())
  340. {
  341. var target = new MaskedTextBox
  342. {
  343. Template = CreateTemplate(),
  344. Mask = mask,
  345. PasswordChar = '*'
  346. };
  347. RaiseTextEvent(target, textEventArg);
  348. Assert.Equal(expected, target.Text);
  349. }
  350. }
  351. [Theory]
  352. [InlineData("00/00/0000", "12102000", "12/10/2000")]
  353. [InlineData("LLLL", "дбs", "дбs_")]
  354. [InlineData("AA#00", "S2 33", "S2_33")]
  355. public void Mask_Should_Work_Correctly(string mask, string textEventArg, string expected)
  356. {
  357. using (Start())
  358. {
  359. var target = new MaskedTextBox
  360. {
  361. Template = CreateTemplate(),
  362. Mask = mask
  363. };
  364. RaiseTextEvent(target, textEventArg);
  365. Assert.Equal(expected, target.Text);
  366. }
  367. }
  368. [Fact]
  369. public void Press_Enter_Add_Custom_Newline()
  370. {
  371. using (Start())
  372. {
  373. var target = new MaskedTextBox
  374. {
  375. Template = CreateTemplate(),
  376. AcceptsReturn = true,
  377. NewLine = "Test"
  378. };
  379. target.ApplyTemplate();
  380. RaiseKeyEvent(target, Key.Enter, 0);
  381. Assert.Equal("Test", target.Text);
  382. }
  383. }
  384. [Theory]
  385. [InlineData(new object[] { false, TextWrapping.NoWrap, ScrollBarVisibility.Hidden })]
  386. [InlineData(new object[] { false, TextWrapping.Wrap, ScrollBarVisibility.Disabled })]
  387. [InlineData(new object[] { true, TextWrapping.NoWrap, ScrollBarVisibility.Auto })]
  388. [InlineData(new object[] { true, TextWrapping.Wrap, ScrollBarVisibility.Disabled })]
  389. public void Has_Correct_Horizontal_ScrollBar_Visibility(
  390. bool acceptsReturn,
  391. TextWrapping wrapping,
  392. ScrollBarVisibility expected)
  393. {
  394. using (Start())
  395. {
  396. var target = new MaskedTextBox
  397. {
  398. AcceptsReturn = acceptsReturn,
  399. TextWrapping = wrapping,
  400. };
  401. Assert.Equal(expected, ScrollViewer.GetHorizontalScrollBarVisibility(target));
  402. }
  403. }
  404. [Fact]
  405. public void SelectionEnd_Doesnt_Cause_Exception()
  406. {
  407. using (Start())
  408. {
  409. var target = new MaskedTextBox
  410. {
  411. Template = CreateTemplate(),
  412. Text = "0123456789"
  413. };
  414. target.SelectionStart = 0;
  415. target.SelectionEnd = 9;
  416. target.Text = "123";
  417. RaiseTextEvent(target, "456");
  418. Assert.True(true);
  419. }
  420. }
  421. [Fact]
  422. public void SelectionStart_Doesnt_Cause_Exception()
  423. {
  424. using (Start())
  425. {
  426. var target = new MaskedTextBox
  427. {
  428. Template = CreateTemplate(),
  429. Text = "0123456789"
  430. };
  431. target.SelectionStart = 8;
  432. target.SelectionEnd = 9;
  433. target.Text = "123";
  434. RaiseTextEvent(target, "456");
  435. Assert.True(true);
  436. }
  437. }
  438. [Fact]
  439. public void SelectionStartEnd_Are_Valid_AterTextChange()
  440. {
  441. using (Start())
  442. {
  443. var target = new MaskedTextBox
  444. {
  445. Template = CreateTemplate(),
  446. Text = "0123456789"
  447. };
  448. target.SelectionStart = 8;
  449. target.SelectionEnd = 9;
  450. target.Text = "123";
  451. Assert.True(target.SelectionStart <= "123".Length);
  452. Assert.True(target.SelectionEnd <= "123".Length);
  453. }
  454. }
  455. [Fact]
  456. public void SelectedText_Changes_OnSelectionChange()
  457. {
  458. using (Start())
  459. {
  460. var target = new MaskedTextBox
  461. {
  462. Template = CreateTemplate(),
  463. Text = "0123456789"
  464. };
  465. Assert.True(target.SelectedText == "");
  466. target.SelectionStart = 2;
  467. target.SelectionEnd = 4;
  468. Assert.True(target.SelectedText == "23");
  469. }
  470. }
  471. [Fact]
  472. public void SelectedText_EditsText()
  473. {
  474. using (Start())
  475. {
  476. var target = new MaskedTextBox
  477. {
  478. Template = CreateTemplate(),
  479. Text = "0123"
  480. };
  481. target.SelectedText = "AA";
  482. Assert.True(target.Text == "AA0123");
  483. target.SelectionStart = 1;
  484. target.SelectionEnd = 3;
  485. target.SelectedText = "BB";
  486. Assert.True(target.Text == "ABB123");
  487. }
  488. }
  489. [Fact]
  490. public void SelectedText_CanClearText()
  491. {
  492. using (Start())
  493. {
  494. var target = new MaskedTextBox
  495. {
  496. Template = CreateTemplate(),
  497. Text = "0123"
  498. };
  499. target.SelectionStart = 1;
  500. target.SelectionEnd = 3;
  501. target.SelectedText = "";
  502. Assert.True(target.Text == "03");
  503. }
  504. }
  505. [Fact]
  506. public void SelectedText_NullClearsText()
  507. {
  508. using (Start())
  509. {
  510. var target = new MaskedTextBox
  511. {
  512. Template = CreateTemplate(),
  513. Text = "0123"
  514. };
  515. target.SelectionStart = 1;
  516. target.SelectionEnd = 3;
  517. target.SelectedText = null;
  518. Assert.True(target.Text == "03");
  519. }
  520. }
  521. [Fact]
  522. public void CoerceCaretIndex_Doesnt_Cause_Exception_with_malformed_line_ending()
  523. {
  524. using (Start())
  525. {
  526. var target = new MaskedTextBox
  527. {
  528. Template = CreateTemplate(),
  529. Text = "0123456789\r"
  530. };
  531. target.CaretIndex = 11;
  532. Assert.True(true);
  533. }
  534. }
  535. [Theory]
  536. [InlineData(Key.Up)]
  537. [InlineData(Key.Down)]
  538. [InlineData(Key.Home)]
  539. [InlineData(Key.End)]
  540. public void Textbox_doesnt_crash_when_Receives_input_and_template_not_applied(Key key)
  541. {
  542. using (Start(FocusServices))
  543. {
  544. var target1 = new MaskedTextBox
  545. {
  546. Template = CreateTemplate(),
  547. Text = "1234",
  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 async Task 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. var impl = CreateMockTopLevelImpl();
  686. var topLevel = new TestTopLevel(impl.Object)
  687. {
  688. Template = CreateTopLevelTemplate()
  689. };
  690. topLevel.Content = target;
  691. topLevel.ApplyTemplate();
  692. topLevel.LayoutManager.ExecuteInitialLayoutPass();
  693. target.ApplyTemplate();
  694. if (fromClipboard)
  695. {
  696. await topLevel.Clipboard!.SetTextAsync(textInput);
  697. RaiseKeyEvent(target, Key.V, KeyModifiers.Control);
  698. await topLevel.Clipboard!.ClearAsync();
  699. }
  700. else
  701. {
  702. RaiseTextEvent(target, textInput);
  703. }
  704. Assert.Equal(expected, target.Text);
  705. }
  706. }
  707. [Theory]
  708. [InlineData(Key.X, KeyModifiers.Control)]
  709. [InlineData(Key.Back, KeyModifiers.None)]
  710. [InlineData(Key.Delete, KeyModifiers.None)]
  711. [InlineData(Key.Tab, KeyModifiers.None)]
  712. [InlineData(Key.Enter, KeyModifiers.None)]
  713. public void Keys_Allow_Undo(Key key, KeyModifiers modifiers)
  714. {
  715. using (Start())
  716. {
  717. var target = new MaskedTextBox
  718. {
  719. Template = CreateTemplate(),
  720. Text = "0123",
  721. AcceptsReturn = true,
  722. AcceptsTab = true
  723. };
  724. var impl = CreateMockTopLevelImpl();
  725. var topLevel = new TestTopLevel(impl.Object)
  726. {
  727. Template = CreateTopLevelTemplate()
  728. };
  729. topLevel.Content = target;
  730. topLevel.ApplyTemplate();
  731. topLevel.LayoutManager.ExecuteInitialLayoutPass();
  732. target.SelectionStart = 1;
  733. target.SelectionEnd = 3;
  734. RaiseKeyEvent(target, key, modifiers);
  735. RaiseKeyEvent(target, Key.Z, KeyModifiers.Control); // undo
  736. Assert.Equal("0123", target.Text);
  737. }
  738. }
  739. [Fact]
  740. public void Invalid_Text_Is_Coerced_Without_Raising_Intermediate_Change()
  741. {
  742. using (Start())
  743. {
  744. var target = new MaskedTextBox
  745. {
  746. Template = CreateTemplate()
  747. };
  748. var impl = CreateMockTopLevelImpl();
  749. var topLevel = new TestTopLevel(impl.Object) {
  750. Template = CreateTopLevelTemplate(),
  751. Content = target
  752. };
  753. topLevel.ApplyTemplate();
  754. topLevel.LayoutManager.ExecuteInitialLayoutPass();
  755. var texts = new System.Collections.Generic.List<string>();
  756. target.PropertyChanged += (_, e) =>
  757. {
  758. if (e.Property == TextBox.TextProperty)
  759. texts.Add(e.GetNewValue<string>());
  760. };
  761. target.Mask = "000";
  762. target.Text = "123";
  763. target.Text = "abc";
  764. Assert.Equal(["___", "123"], texts);
  765. }
  766. }
  767. private static TestServices FocusServices => TestServices.MockThreadingInterface.With(
  768. keyboardDevice: () => new KeyboardDevice(),
  769. keyboardNavigation: () => new KeyboardNavigationHandler(),
  770. inputManager: new InputManager(),
  771. renderInterface: new HeadlessPlatformRenderInterface(),
  772. fontManagerImpl: new HeadlessFontManagerStub(),
  773. textShaperImpl: new HeadlessTextShaperStub(),
  774. standardCursorFactory: Mock.Of<ICursorFactory>());
  775. private static TestServices Services => TestServices.MockThreadingInterface.With(
  776. renderInterface: new HeadlessPlatformRenderInterface(),
  777. standardCursorFactory: Mock.Of<ICursorFactory>(),
  778. textShaperImpl: new HeadlessTextShaperStub(),
  779. fontManagerImpl: new HeadlessFontManagerStub());
  780. private static IControlTemplate CreateTemplate()
  781. {
  782. return new FuncControlTemplate<MaskedTextBox>((control, scope) =>
  783. new TextPresenter
  784. {
  785. Name = "PART_TextPresenter",
  786. [!!TextPresenter.TextProperty] = new Binding
  787. {
  788. Path = nameof(TextPresenter.Text),
  789. Mode = BindingMode.TwoWay,
  790. Priority = BindingPriority.Template,
  791. RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),
  792. },
  793. [!!TextPresenter.CaretIndexProperty] = new Binding
  794. {
  795. Path = nameof(TextPresenter.CaretIndex),
  796. Mode = BindingMode.TwoWay,
  797. Priority = BindingPriority.Template,
  798. RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),
  799. }
  800. }.RegisterInNameScope(scope));
  801. }
  802. private static void RaiseKeyEvent(MaskedTextBox textBox, Key key, KeyModifiers inputModifiers)
  803. {
  804. textBox.RaiseEvent(new KeyEventArgs
  805. {
  806. RoutedEvent = InputElement.KeyDownEvent,
  807. KeyModifiers = inputModifiers,
  808. Key = key
  809. });
  810. }
  811. private void RaiseTextEvent(MaskedTextBox textBox, string text)
  812. {
  813. textBox.RaiseEvent(new TextInputEventArgs
  814. {
  815. RoutedEvent = InputElement.TextInputEvent,
  816. Text = text
  817. });
  818. }
  819. private static IDisposable Start(TestServices services = null)
  820. {
  821. CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
  822. return UnitTestApplication.Start(services ?? Services);
  823. }
  824. private class Class1 : NotifyingBase
  825. {
  826. private int _foo;
  827. private string _bar;
  828. public int Foo
  829. {
  830. get { return _foo; }
  831. set { _foo = value; RaisePropertyChanged(); }
  832. }
  833. public string Bar
  834. {
  835. get { return _bar; }
  836. set { _bar = value; RaisePropertyChanged(); }
  837. }
  838. }
  839. private class TestTopLevel : TopLevel
  840. {
  841. private readonly ILayoutManager _layoutManager;
  842. public TestTopLevel(ITopLevelImpl impl, ILayoutManager layoutManager = null)
  843. : base(impl)
  844. {
  845. _layoutManager = layoutManager ?? new LayoutManager(this);
  846. }
  847. private protected override ILayoutManager CreateLayoutManager() => _layoutManager;
  848. }
  849. private static Mock<ITopLevelImpl> CreateMockTopLevelImpl()
  850. {
  851. var clipboard = new Mock<ITopLevelImpl>();
  852. clipboard.Setup(x => x.Compositor).Returns(RendererMocks.CreateDummyCompositor());
  853. clipboard.Setup(r => r.TryGetFeature(typeof(IClipboard)))
  854. .Returns(new HeadlessClipboardStub());
  855. clipboard.SetupGet(x => x.RenderScaling).Returns(1);
  856. return clipboard;
  857. }
  858. private static FuncControlTemplate<TestTopLevel> CreateTopLevelTemplate()
  859. {
  860. return new FuncControlTemplate<TestTopLevel>((x, scope) =>
  861. new ContentPresenter
  862. {
  863. Name = "PART_ContentPresenter",
  864. [!ContentPresenter.ContentProperty] = x[!ContentControl.ContentProperty],
  865. }.RegisterInNameScope(scope));
  866. }
  867. private class TestContextMenu : ContextMenu
  868. {
  869. public TestContextMenu()
  870. {
  871. IsOpen = true;
  872. }
  873. }
  874. }
  875. }