TextBoxTests.cs 43 KB

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