TextBoxTests.cs 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  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 Control_Backspace_Should_Set_Caret_Position_To_The_Start_Of_The_Deletion()
  129. {
  130. using (UnitTestApplication.Start(Services))
  131. {
  132. var target = new TextBox
  133. {
  134. Template = CreateTemplate(),
  135. Text = "First Second Third",
  136. SelectionStart = 13,
  137. SelectionEnd = 13
  138. };
  139. target.CaretIndex = 10;
  140. target.ApplyTemplate();
  141. // (First Second |Third)
  142. RaiseKeyEvent(target, Key.Back, KeyModifiers.Control);
  143. // (First |Third)
  144. Assert.Equal(6, target.CaretIndex);
  145. }
  146. }
  147. [Fact]
  148. public void Control_Backspace_Should_Remove_The_Double_Whitespace_If_Caret_Index_Was_At_The_End_Of_A_Word()
  149. {
  150. using (UnitTestApplication.Start(Services))
  151. {
  152. var target = new TextBox
  153. {
  154. Template = CreateTemplate(),
  155. Text = "First Second Third",
  156. SelectionStart = 12,
  157. SelectionEnd = 12
  158. };
  159. target.ApplyTemplate();
  160. // (First Second| Third)
  161. RaiseKeyEvent(target, Key.Back, KeyModifiers.Control);
  162. // (First| Third)
  163. Assert.Equal("First Third", target.Text);
  164. }
  165. }
  166. [Fact]
  167. public void Control_Backspace_Undo_Should_Return_Caret_Position()
  168. {
  169. using (UnitTestApplication.Start(Services))
  170. {
  171. var target = new TextBox
  172. {
  173. Template = CreateTemplate(),
  174. Text = "First Second Third",
  175. SelectionStart = 9,
  176. SelectionEnd = 9
  177. };
  178. target.ApplyTemplate();
  179. // (First Second| Third)
  180. RaiseKeyEvent(target, Key.Back, KeyModifiers.Control);
  181. // (First| Third)
  182. target.Undo();
  183. // (First Second| Third)
  184. Assert.Equal(9, target.CaretIndex);
  185. }
  186. }
  187. [Fact]
  188. public void Press_Ctrl_A_Select_All_Text()
  189. {
  190. using (UnitTestApplication.Start(Services))
  191. {
  192. var target = new TextBox
  193. {
  194. Template = CreateTemplate(),
  195. Text = "1234"
  196. };
  197. target.ApplyTemplate();
  198. RaiseKeyEvent(target, Key.A, KeyModifiers.Control);
  199. Assert.Equal(0, target.SelectionStart);
  200. Assert.Equal(4, target.SelectionEnd);
  201. }
  202. }
  203. [Fact]
  204. public void Press_Ctrl_A_Select_All_Null_Text()
  205. {
  206. using (UnitTestApplication.Start(Services))
  207. {
  208. var target = new TextBox
  209. {
  210. Template = CreateTemplate()
  211. };
  212. RaiseKeyEvent(target, Key.A, KeyModifiers.Control);
  213. Assert.Equal(0, target.SelectionStart);
  214. Assert.Equal(0, target.SelectionEnd);
  215. }
  216. }
  217. [Fact]
  218. public void Press_Ctrl_Z_Will_Not_Modify_Text()
  219. {
  220. using (UnitTestApplication.Start(Services))
  221. {
  222. var target = new TextBox
  223. {
  224. Template = CreateTemplate(),
  225. Text = "1234"
  226. };
  227. RaiseKeyEvent(target, Key.Z, KeyModifiers.Control);
  228. Assert.Equal("1234", target.Text);
  229. }
  230. }
  231. [Fact]
  232. public void Control_Backspace_Should_Remove_The_Word_Before_The_Caret_If_There_Is_No_Selection()
  233. {
  234. using (UnitTestApplication.Start(Services))
  235. {
  236. TextBox textBox = new TextBox
  237. {
  238. Template = CreateTemplate(),
  239. Text = "First Second Third Fourth",
  240. SelectionStart = 5,
  241. SelectionEnd = 5
  242. };
  243. textBox.ApplyTemplate();
  244. // (First| Second Third Fourth)
  245. RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
  246. Assert.Equal(" Second Third Fourth", textBox.Text);
  247. // ( Second |Third Fourth)
  248. textBox.CaretIndex = 8;
  249. RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
  250. Assert.Equal(" Third Fourth", textBox.Text);
  251. // ( Thi|rd Fourth)
  252. textBox.CaretIndex = 4;
  253. RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
  254. Assert.Equal(" rd Fourth", textBox.Text);
  255. // ( rd F[ou]rth)
  256. textBox.SelectionStart = 5;
  257. textBox.SelectionEnd = 7;
  258. RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
  259. Assert.Equal(" rd Frth", textBox.Text);
  260. // ( |rd Frth)
  261. textBox.CaretIndex = 1;
  262. RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
  263. Assert.Equal("rd Frth", textBox.Text);
  264. }
  265. }
  266. [Fact]
  267. public void Control_Delete_Should_Remove_The_Word_After_The_Caret_If_There_Is_No_Selection()
  268. {
  269. using (UnitTestApplication.Start(Services))
  270. {
  271. TextBox textBox = new TextBox
  272. {
  273. Template = CreateTemplate(),
  274. Text = "First Second Third Fourth",
  275. CaretIndex = 19,
  276. };
  277. textBox.ApplyTemplate();
  278. // (First Second Third |Fourth)
  279. RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
  280. Assert.Equal("First Second Third ", textBox.Text);
  281. // (First Second |Third )
  282. textBox.CaretIndex = 13;
  283. RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
  284. Assert.Equal("First Second ", textBox.Text);
  285. // (First Sec|ond )
  286. textBox.CaretIndex = 9;
  287. RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
  288. Assert.Equal("First Sec", textBox.Text);
  289. // (Fi[rs]t Sec )
  290. textBox.SelectionStart = 2;
  291. textBox.SelectionEnd = 4;
  292. RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
  293. Assert.Equal("Fit Sec", textBox.Text);
  294. // (Fit Sec| )
  295. textBox.Text += " ";
  296. textBox.CaretIndex = 7;
  297. RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
  298. Assert.Equal("Fit Sec", textBox.Text);
  299. }
  300. }
  301. [Fact]
  302. public void Setting_SelectionStart_To_SelectionEnd_Sets_CaretPosition_To_SelectionStart()
  303. {
  304. using (UnitTestApplication.Start(Services))
  305. {
  306. var textBox = new TextBox
  307. {
  308. Text = "0123456789"
  309. };
  310. textBox.SelectionStart = 2;
  311. textBox.SelectionEnd = 2;
  312. Assert.Equal(2, textBox.CaretIndex);
  313. }
  314. }
  315. [Fact]
  316. public void Setting_Text_Updates_CaretPosition()
  317. {
  318. using (UnitTestApplication.Start(Services))
  319. {
  320. var target = new TextBox
  321. {
  322. Text = "Initial Text",
  323. CaretIndex = 11
  324. };
  325. var invoked = false;
  326. target.GetObservable(TextBox.TextProperty).Skip(1).Subscribe(_ =>
  327. {
  328. // Caret index should be set before Text changed notification, as we don't want
  329. // to notify with an invalid CaretIndex.
  330. Assert.Equal(7, target.CaretIndex);
  331. invoked = true;
  332. });
  333. target.Text = "Changed";
  334. Assert.True(invoked);
  335. }
  336. }
  337. [Fact]
  338. public void Press_Enter_Does_Not_Accept_Return()
  339. {
  340. using (UnitTestApplication.Start(Services))
  341. {
  342. var target = new TextBox
  343. {
  344. Template = CreateTemplate(),
  345. AcceptsReturn = false,
  346. Text = "1234"
  347. };
  348. target.ApplyTemplate();
  349. RaiseKeyEvent(target, Key.Enter, 0);
  350. Assert.Equal("1234", target.Text);
  351. }
  352. }
  353. [Fact]
  354. public void Press_Enter_Add_Default_Newline()
  355. {
  356. using (UnitTestApplication.Start(Services))
  357. {
  358. var target = new TextBox
  359. {
  360. Template = CreateTemplate(),
  361. AcceptsReturn = true
  362. };
  363. target.ApplyTemplate();
  364. RaiseKeyEvent(target, Key.Enter, 0);
  365. Assert.Equal(Environment.NewLine, target.Text);
  366. }
  367. }
  368. [Fact]
  369. public void Press_Enter_Add_Custom_Newline()
  370. {
  371. using (UnitTestApplication.Start(Services))
  372. {
  373. var target = new TextBox
  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 (UnitTestApplication.Start(Services))
  395. {
  396. var target = new TextBox
  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 (UnitTestApplication.Start(Services))
  408. {
  409. var target = new TextBox
  410. {
  411. Template = CreateTemplate(),
  412. Text = "0123456789"
  413. };
  414. target.ApplyTemplate();
  415. target.SelectionStart = 0;
  416. target.SelectionEnd = 9;
  417. target.Text = "123";
  418. RaiseTextEvent(target, "456");
  419. Assert.True(true);
  420. }
  421. }
  422. [Fact]
  423. public void SelectionStart_Doesnt_Cause_Exception()
  424. {
  425. using (UnitTestApplication.Start(Services))
  426. {
  427. var target = new TextBox
  428. {
  429. Template = CreateTemplate(),
  430. Text = "0123456789"
  431. };
  432. target.ApplyTemplate();
  433. target.SelectionStart = 8;
  434. target.SelectionEnd = 9;
  435. target.Text = "123";
  436. RaiseTextEvent(target, "456");
  437. Assert.True(true);
  438. }
  439. }
  440. [Fact]
  441. public void SelectionStartEnd_Are_Valid_AterTextChange()
  442. {
  443. using (UnitTestApplication.Start(Services))
  444. {
  445. var target = new TextBox
  446. {
  447. Template = CreateTemplate(),
  448. Text = "0123456789"
  449. };
  450. target.SelectionStart = 8;
  451. target.SelectionEnd = 9;
  452. target.Text = "123";
  453. Assert.True(target.SelectionStart <= "123".Length);
  454. Assert.True(target.SelectionEnd <= "123".Length);
  455. }
  456. }
  457. [Fact]
  458. public void SelectedText_Changes_OnSelectionChange()
  459. {
  460. using (UnitTestApplication.Start(Services))
  461. {
  462. var target = new TextBox
  463. {
  464. Template = CreateTemplate(),
  465. Text = "0123456789"
  466. };
  467. target.ApplyTemplate();
  468. Assert.True(target.SelectedText == "");
  469. target.SelectionStart = 2;
  470. target.SelectionEnd = 4;
  471. Assert.True(target.SelectedText == "23");
  472. }
  473. }
  474. [Fact]
  475. public void SelectedText_EditsText()
  476. {
  477. using (UnitTestApplication.Start(Services))
  478. {
  479. var target = new TextBox
  480. {
  481. Template = CreateTemplate(),
  482. Text = "0123"
  483. };
  484. target.ApplyTemplate();
  485. target.SelectedText = "AA";
  486. Assert.True(target.Text == "AA0123");
  487. target.SelectionStart = 1;
  488. target.SelectionEnd = 3;
  489. target.SelectedText = "BB";
  490. Assert.True(target.Text == "ABB123");
  491. }
  492. }
  493. [Fact]
  494. public void SelectedText_CanClearText()
  495. {
  496. using (UnitTestApplication.Start(Services))
  497. {
  498. var target = new TextBox
  499. {
  500. Template = CreateTemplate(),
  501. Text = "0123"
  502. };
  503. target.SelectionStart = 1;
  504. target.SelectionEnd = 3;
  505. target.SelectedText = "";
  506. Assert.True(target.Text == "03");
  507. }
  508. }
  509. [Fact]
  510. public void SelectedText_NullClearsText()
  511. {
  512. using (UnitTestApplication.Start(Services))
  513. {
  514. var target = new TextBox
  515. {
  516. Template = CreateTemplate(),
  517. Text = "0123"
  518. };
  519. target.SelectionStart = 1;
  520. target.SelectionEnd = 3;
  521. target.SelectedText = null;
  522. Assert.True(target.Text == "03");
  523. }
  524. }
  525. [Fact]
  526. public void CoerceCaretIndex_Doesnt_Cause_Exception_with_malformed_line_ending()
  527. {
  528. using (UnitTestApplication.Start(Services))
  529. {
  530. var target = new TextBox
  531. {
  532. Template = CreateTemplate(),
  533. Text = "0123456789\r"
  534. };
  535. target.CaretIndex = 11;
  536. Assert.True(true);
  537. }
  538. }
  539. [Theory]
  540. [InlineData(Key.Up)]
  541. [InlineData(Key.Down)]
  542. [InlineData(Key.Home)]
  543. [InlineData(Key.End)]
  544. public void Textbox_doesnt_crash_when_Receives_input_and_template_not_applied(Key key)
  545. {
  546. using (UnitTestApplication.Start(FocusServices))
  547. {
  548. var target1 = new TextBox
  549. {
  550. Template = CreateTemplate(),
  551. Text = "1234",
  552. };
  553. var root = new TestRoot { Child = target1 };
  554. target1.Focus();
  555. Assert.True(target1.IsFocused);
  556. RaiseKeyEvent(target1, key, KeyModifiers.None);
  557. }
  558. }
  559. [Fact]
  560. public void TextBox_GotFocus_And_LostFocus_Work_Properly()
  561. {
  562. using (UnitTestApplication.Start(FocusServices))
  563. {
  564. var target1 = new TextBox
  565. {
  566. Template = CreateTemplate(),
  567. Text = "1234"
  568. };
  569. var target2 = new TextBox
  570. {
  571. Template = CreateTemplate(),
  572. Text = "5678"
  573. };
  574. var sp = new StackPanel();
  575. sp.Children.Add(target1);
  576. sp.Children.Add(target2);
  577. target1.ApplyTemplate();
  578. target2.ApplyTemplate();
  579. var root = new TestRoot { Child = sp };
  580. var gfcount = 0;
  581. var lfcount = 0;
  582. target1.GotFocus += (s, e) => gfcount++;
  583. target2.LostFocus += (s, e) => lfcount++;
  584. target2.Focus();
  585. Assert.False(target1.IsFocused);
  586. Assert.True(target2.IsFocused);
  587. target1.Focus();
  588. Assert.False(target2.IsFocused);
  589. Assert.True(target1.IsFocused);
  590. Assert.Equal(1, gfcount);
  591. Assert.Equal(1, lfcount);
  592. }
  593. }
  594. [Fact]
  595. public void TextBox_CaretIndex_Persists_When_Focus_Lost()
  596. {
  597. using (UnitTestApplication.Start(FocusServices))
  598. {
  599. var target1 = new TextBox
  600. {
  601. Template = CreateTemplate(),
  602. Text = "1234"
  603. };
  604. var target2 = new TextBox
  605. {
  606. Template = CreateTemplate(),
  607. Text = "5678"
  608. };
  609. var sp = new StackPanel();
  610. sp.Children.Add(target1);
  611. sp.Children.Add(target2);
  612. target1.ApplyTemplate();
  613. target2.ApplyTemplate();
  614. var root = new TestRoot { Child = sp };
  615. target2.Focus();
  616. target2.CaretIndex = 2;
  617. Assert.False(target1.IsFocused);
  618. Assert.True(target2.IsFocused);
  619. target1.Focus();
  620. Assert.Equal(2, target2.CaretIndex);
  621. }
  622. }
  623. [Fact]
  624. public void TextBox_Reveal_Password_Reset_When_Lost_Focus()
  625. {
  626. using (UnitTestApplication.Start(FocusServices))
  627. {
  628. var target1 = new TextBox
  629. {
  630. Template = CreateTemplate(),
  631. Text = "1234",
  632. PasswordChar = '*'
  633. };
  634. var target2 = new TextBox
  635. {
  636. Template = CreateTemplate(),
  637. Text = "5678"
  638. };
  639. var sp = new StackPanel();
  640. sp.Children.Add(target1);
  641. sp.Children.Add(target2);
  642. target1.ApplyTemplate();
  643. target2.ApplyTemplate();
  644. var root = new TestRoot { Child = sp };
  645. target1.Focus();
  646. target1.RevealPassword = true;
  647. target2.Focus();
  648. Assert.False(target1.RevealPassword);
  649. }
  650. }
  651. [Fact]
  652. public void Setting_Bound_Text_To_Null_Works()
  653. {
  654. using (UnitTestApplication.Start(Services))
  655. {
  656. var source = new Class1 { Bar = "bar" };
  657. var target = new TextBox { Template = CreateTemplate(), DataContext = source };
  658. target.ApplyTemplate();
  659. target.Bind(TextBox.TextProperty, new Binding("Bar"));
  660. Assert.Equal("bar", target.Text);
  661. source.Bar = null;
  662. Assert.Null(target.Text);
  663. }
  664. }
  665. [Theory]
  666. [InlineData("abc", "d", 3, 0, 0, false, "abc")]
  667. [InlineData("abc", "dd", 4, 3, 3, false, "abcd")]
  668. [InlineData("abc", "ddd", 3, 0, 2, true, "ddc")]
  669. [InlineData("abc", "dddd", 4, 1, 3, true, "addd")]
  670. [InlineData("abc", "ddddd", 5, 3, 3, true, "abcdd")]
  671. public void MaxLength_Works_Properly(
  672. string initalText,
  673. string textInput,
  674. int maxLength,
  675. int selectionStart,
  676. int selectionEnd,
  677. bool fromClipboard,
  678. string expected)
  679. {
  680. using (UnitTestApplication.Start(Services))
  681. {
  682. var target = new TextBox
  683. {
  684. Template = CreateTemplate(),
  685. Text = initalText,
  686. MaxLength = maxLength,
  687. SelectionStart = selectionStart,
  688. SelectionEnd = selectionEnd
  689. };
  690. var impl = CreateMockTopLevelImpl();
  691. var topLevel = new TestTopLevel(impl.Object)
  692. {
  693. Template = CreateTopLevelTemplate()
  694. };
  695. topLevel.Content = target;
  696. topLevel.ApplyTemplate();
  697. topLevel.LayoutManager.ExecuteInitialLayoutPass();
  698. target.Measure(Size.Infinity);
  699. if (fromClipboard)
  700. {
  701. topLevel.Clipboard?.SetTextAsync(textInput).GetAwaiter().GetResult();
  702. RaiseKeyEvent(target, Key.V, KeyModifiers.Control);
  703. topLevel.Clipboard?.ClearAsync().GetAwaiter().GetResult();
  704. }
  705. else
  706. {
  707. RaiseTextEvent(target, textInput);
  708. }
  709. Assert.Equal(expected, target.Text);
  710. }
  711. }
  712. [Theory]
  713. [InlineData(Key.X, KeyModifiers.Control)]
  714. [InlineData(Key.Back, KeyModifiers.None)]
  715. [InlineData(Key.Delete, KeyModifiers.None)]
  716. [InlineData(Key.Tab, KeyModifiers.None)]
  717. [InlineData(Key.Enter, KeyModifiers.None)]
  718. public void Keys_Allow_Undo(Key key, KeyModifiers modifiers)
  719. {
  720. using (UnitTestApplication.Start(Services))
  721. {
  722. var target = new TextBox
  723. {
  724. Template = CreateTemplate(),
  725. Text = "0123",
  726. AcceptsReturn = true,
  727. AcceptsTab = true
  728. };
  729. var impl = CreateMockTopLevelImpl();
  730. var topLevel = new TestTopLevel(impl.Object)
  731. {
  732. Template = CreateTopLevelTemplate()
  733. };
  734. topLevel.Content = target;
  735. topLevel.ApplyTemplate();
  736. topLevel.LayoutManager.ExecuteInitialLayoutPass();
  737. target.ApplyTemplate();
  738. target.SelectionStart = 1;
  739. target.SelectionEnd = 3;
  740. RaiseKeyEvent(target, key, modifiers);
  741. RaiseKeyEvent(target, Key.Z, KeyModifiers.Control); // undo
  742. Assert.True(target.Text == "0123");
  743. }
  744. }
  745. [Fact]
  746. public void Setting_SelectedText_Should_Fire_Single_Text_Changed_Notification()
  747. {
  748. using (UnitTestApplication.Start(Services))
  749. {
  750. var target = new TextBox
  751. {
  752. Template = CreateTemplate(),
  753. Text = "0123",
  754. AcceptsReturn = true,
  755. AcceptsTab = true,
  756. SelectionStart = 1,
  757. SelectionEnd = 3,
  758. };
  759. var values = new List<string>();
  760. target.GetObservable(TextBox.TextProperty).Subscribe(x => values.Add(x));
  761. target.SelectedText = "A";
  762. Assert.Equal(new[] { "0123", "0A3" }, values);
  763. }
  764. }
  765. [Fact]
  766. public void Entering_Text_With_SelectedText_Should_Fire_Single_Text_Changed_Notification()
  767. {
  768. using (UnitTestApplication.Start(Services))
  769. {
  770. var target = new TextBox
  771. {
  772. Template = CreateTemplate(),
  773. Text = "0123",
  774. AcceptsReturn = true,
  775. AcceptsTab = true,
  776. SelectionStart = 1,
  777. SelectionEnd = 3,
  778. };
  779. var values = new List<string>();
  780. target.GetObservable(TextBox.TextProperty).Subscribe(x => values.Add(x));
  781. RaiseTextEvent(target, "A");
  782. Assert.Equal(new[] { "0123", "0A3" }, values);
  783. }
  784. }
  785. [Fact]
  786. public void Insert_Multiline_Text_Should_Accept_Extra_Lines_When_AcceptsReturn_Is_True()
  787. {
  788. using (UnitTestApplication.Start(Services))
  789. {
  790. var target = new TextBox
  791. {
  792. AcceptsReturn = true
  793. };
  794. RaiseTextEvent(target, $"123 {Environment.NewLine}456");
  795. Assert.Equal($"123 {Environment.NewLine}456", target.Text);
  796. }
  797. }
  798. [Fact]
  799. public void Insert_Multiline_Text_Should_Discard_Extra_Lines_When_AcceptsReturn_Is_False()
  800. {
  801. using (UnitTestApplication.Start(Services))
  802. {
  803. var target = new TextBox
  804. {
  805. AcceptsReturn = false
  806. };
  807. RaiseTextEvent(target, $"123 {"\r"}456");
  808. Assert.Equal("123 ", target.Text);
  809. target.Text = "";
  810. RaiseTextEvent(target, $"123 {"\r\n"}456");
  811. Assert.Equal("123 ", target.Text);
  812. }
  813. }
  814. [Fact]
  815. public void Should_Fullfill_MaxLines_Contraint()
  816. {
  817. using (UnitTestApplication.Start(Services))
  818. {
  819. var target = new TextBox
  820. {
  821. Template = CreateTemplate(),
  822. Text = "ABC",
  823. MaxLines = 1,
  824. AcceptsReturn = true
  825. };
  826. var impl = CreateMockTopLevelImpl();
  827. var topLevel = new TestTopLevel(impl.Object)
  828. {
  829. Template = CreateTopLevelTemplate()
  830. };
  831. topLevel.Content = target;
  832. topLevel.ApplyTemplate();
  833. topLevel.LayoutManager.ExecuteInitialLayoutPass();
  834. target.ApplyTemplate();
  835. target.Measure(Size.Infinity);
  836. var initialHeight = target.DesiredSize.Height;
  837. topLevel.Clipboard?.SetTextAsync(Environment.NewLine).GetAwaiter().GetResult();
  838. RaiseKeyEvent(target, Key.V, KeyModifiers.Control);
  839. topLevel.Clipboard?.ClearAsync().GetAwaiter().GetResult();
  840. RaiseTextEvent(target, Environment.NewLine);
  841. target.InvalidateMeasure();
  842. target.Measure(Size.Infinity);
  843. Assert.Equal(initialHeight, target.DesiredSize.Height);
  844. }
  845. }
  846. [Theory]
  847. [InlineData(1)]
  848. [InlineData(2)]
  849. [InlineData(3)]
  850. public void MaxLines_Sets_ScrollViewer_MaxHeight(int maxLines)
  851. {
  852. using (UnitTestApplication.Start(Services))
  853. {
  854. var target = new TextBox
  855. {
  856. Template = CreateTemplate(),
  857. MaxLines = maxLines,
  858. // Define explicit whole number line height for predictable calculations
  859. LineHeight = 20
  860. };
  861. var impl = CreateMockTopLevelImpl();
  862. var topLevel = new TestTopLevel(impl.Object)
  863. {
  864. Template = CreateTopLevelTemplate(),
  865. Content = target
  866. };
  867. topLevel.ApplyTemplate();
  868. topLevel.LayoutManager.ExecuteInitialLayoutPass();
  869. var textPresenter = target.FindDescendantOfType<TextPresenter>();
  870. Assert.Equal("PART_TextPresenter", textPresenter.Name);
  871. Assert.Equal(new Thickness(0), textPresenter.Margin); // Test assumes no margin on TextPresenter
  872. var scrollViewer = target.FindDescendantOfType<ScrollViewer>();
  873. Assert.Equal("PART_ScrollViewer", scrollViewer.Name);
  874. Assert.Equal(maxLines * target.LineHeight, scrollViewer.MaxHeight);
  875. }
  876. }
  877. [Theory]
  878. [InlineData(1)]
  879. [InlineData(2)]
  880. [InlineData(3)]
  881. public void MaxLines_Sets_ScrollViewer_MaxHeight_With_TextPresenter_Margin(int maxLines)
  882. {
  883. using (UnitTestApplication.Start(Services))
  884. {
  885. var target = new TextBox
  886. {
  887. Template = CreateTemplate(),
  888. MaxLines = maxLines,
  889. // Define explicit whole number line height for predictable calculations
  890. LineHeight = 20
  891. };
  892. var impl = CreateMockTopLevelImpl();
  893. var topLevel = new TestTopLevel(impl.Object)
  894. {
  895. Template = CreateTopLevelTemplate(),
  896. Content = target
  897. };
  898. topLevel.ApplyTemplate();
  899. topLevel.LayoutManager.ExecuteInitialLayoutPass();
  900. var textPresenter = target.FindDescendantOfType<TextPresenter>();
  901. Assert.Equal("PART_TextPresenter", textPresenter.Name);
  902. var textPresenterMargin = new Thickness(horizontal: 0, vertical: 3);
  903. textPresenter.Margin = textPresenterMargin;
  904. target.InvalidateMeasure();
  905. target.Measure(Size.Infinity);
  906. var scrollViewer = target.FindDescendantOfType<ScrollViewer>();
  907. Assert.Equal("PART_ScrollViewer", scrollViewer.Name);
  908. Assert.Equal((maxLines * target.LineHeight) + textPresenterMargin.Top + textPresenterMargin.Bottom, scrollViewer.MaxHeight);
  909. }
  910. }
  911. [Fact]
  912. public void Should_Fullfill_MinLines_Contraint()
  913. {
  914. using (UnitTestApplication.Start(Services))
  915. {
  916. var target = new TextBox
  917. {
  918. Template = CreateTemplate(),
  919. Text = "ABC \n DEF \n GHI",
  920. MinLines = 3,
  921. AcceptsReturn = true
  922. };
  923. var impl = CreateMockTopLevelImpl();
  924. var topLevel = new TestTopLevel(impl.Object)
  925. {
  926. Template = CreateTopLevelTemplate()
  927. };
  928. topLevel.Content = target;
  929. topLevel.ApplyTemplate();
  930. topLevel.LayoutManager.ExecuteInitialLayoutPass();
  931. target.ApplyTemplate();
  932. target.Measure(Size.Infinity);
  933. var initialHeight = target.DesiredSize.Height;
  934. target.Text = "";
  935. target.InvalidateMeasure();
  936. target.Measure(Size.Infinity);
  937. Assert.Equal(initialHeight, target.DesiredSize.Height);
  938. }
  939. }
  940. [Theory]
  941. [InlineData(1)]
  942. [InlineData(2)]
  943. [InlineData(3)]
  944. public void MinLines_Sets_ScrollViewer_MinHeight(int minLines)
  945. {
  946. using (UnitTestApplication.Start(Services))
  947. {
  948. var target = new TextBox
  949. {
  950. Template = CreateTemplate(),
  951. MinLines = minLines,
  952. // Define explicit whole number line height for predictable calculations
  953. LineHeight = 20
  954. };
  955. var impl = CreateMockTopLevelImpl();
  956. var topLevel = new TestTopLevel(impl.Object)
  957. {
  958. Template = CreateTopLevelTemplate(),
  959. Content = target
  960. };
  961. topLevel.ApplyTemplate();
  962. topLevel.LayoutManager.ExecuteInitialLayoutPass();
  963. var textPresenter = target.FindDescendantOfType<TextPresenter>();
  964. Assert.Equal("PART_TextPresenter", textPresenter.Name);
  965. Assert.Equal(new Thickness(0), textPresenter.Margin); // Test assumes no margin on TextPresenter
  966. var scrollViewer = target.FindDescendantOfType<ScrollViewer>();
  967. Assert.Equal("PART_ScrollViewer", scrollViewer.Name);
  968. Assert.Equal(minLines * target.LineHeight, scrollViewer.MinHeight);
  969. }
  970. }
  971. [Theory]
  972. [InlineData(1)]
  973. [InlineData(2)]
  974. [InlineData(3)]
  975. public void MinLines_Sets_ScrollViewer_MinHeight_With_TextPresenter_Margin(int minLines)
  976. {
  977. using (UnitTestApplication.Start(Services))
  978. {
  979. var target = new TextBox
  980. {
  981. Template = CreateTemplate(),
  982. MinLines = minLines,
  983. // Define explicit whole number line height for predictable calculations
  984. LineHeight = 20
  985. };
  986. var impl = CreateMockTopLevelImpl();
  987. var topLevel = new TestTopLevel(impl.Object)
  988. {
  989. Template = CreateTopLevelTemplate(),
  990. Content = target
  991. };
  992. topLevel.ApplyTemplate();
  993. topLevel.LayoutManager.ExecuteInitialLayoutPass();
  994. var textPresenter = target.FindDescendantOfType<TextPresenter>();
  995. Assert.Equal("PART_TextPresenter", textPresenter.Name);
  996. var textPresenterMargin = new Thickness(horizontal: 0, vertical: 3);
  997. textPresenter.Margin = textPresenterMargin;
  998. target.InvalidateMeasure();
  999. target.Measure(Size.Infinity);
  1000. var scrollViewer = target.FindDescendantOfType<ScrollViewer>();
  1001. Assert.Equal("PART_ScrollViewer", scrollViewer.Name);
  1002. Assert.Equal((minLines * target.LineHeight) + textPresenterMargin.Top + textPresenterMargin.Bottom, scrollViewer.MinHeight);
  1003. }
  1004. }
  1005. [Fact]
  1006. public void CanUndo_CanRedo_Is_False_When_Initialized()
  1007. {
  1008. using (UnitTestApplication.Start(Services))
  1009. {
  1010. var tb = new TextBox
  1011. {
  1012. Template = CreateTemplate(),
  1013. Text = "New Text"
  1014. };
  1015. tb.Measure(Size.Infinity);
  1016. Assert.False(tb.CanUndo);
  1017. Assert.False(tb.CanRedo);
  1018. }
  1019. }
  1020. [Fact]
  1021. public void CanUndo_CanRedo_and_Programmatic_Undo_Redo_Works()
  1022. {
  1023. using (UnitTestApplication.Start(Services))
  1024. {
  1025. var tb = new TextBox
  1026. {
  1027. Template = CreateTemplate(),
  1028. };
  1029. tb.Measure(Size.Infinity);
  1030. // See GH #6024 for a bit more insight on when Undo/Redo snapshots are taken:
  1031. // - Every 'Space', but only when space is handled in OnKeyDown - Spaces in TextInput event won't work
  1032. // - Every 7 chars in a long word
  1033. RaiseTextEvent(tb, "ABC");
  1034. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  1035. RaiseTextEvent(tb, "DEF");
  1036. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  1037. RaiseTextEvent(tb, "123");
  1038. // NOTE: the spaces won't actually add spaces b/c they're sent only as key events and not Text events
  1039. // so our final text is without spaces
  1040. Assert.Equal("ABCDEF123", tb.Text);
  1041. Assert.True(tb.CanUndo);
  1042. tb.Undo();
  1043. // Undo will take us back one step
  1044. Assert.Equal("ABCDEF", tb.Text);
  1045. Assert.True(tb.CanRedo);
  1046. tb.Redo();
  1047. // Redo should restore us
  1048. Assert.Equal("ABCDEF123", tb.Text);
  1049. }
  1050. }
  1051. [Fact]
  1052. public void Setting_UndoLimit_Clears_Undo_Redo()
  1053. {
  1054. using (UnitTestApplication.Start(Services))
  1055. {
  1056. var tb = new TextBox
  1057. {
  1058. Template = CreateTemplate(),
  1059. };
  1060. tb.Measure(Size.Infinity);
  1061. // This is all the same as the above test (CanUndo_CanRedo_and_Programmatic_Undo_Redo_Works)
  1062. // We do this to get the undo/redo stacks in a state where both are active
  1063. RaiseTextEvent(tb, "ABC");
  1064. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  1065. RaiseTextEvent(tb, "DEF");
  1066. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  1067. RaiseTextEvent(tb, "123");
  1068. Assert.Equal("ABCDEF123", tb.Text);
  1069. Assert.True(tb.CanUndo);
  1070. tb.Undo();
  1071. // Undo will take us back one step
  1072. Assert.Equal("ABCDEF", tb.Text);
  1073. Assert.True(tb.CanRedo);
  1074. tb.Redo();
  1075. // Redo should restore us
  1076. Assert.Equal("ABCDEF123", tb.Text);
  1077. // Change the undo limit, this should clear both stacks setting CanUndo and CanRedo to false
  1078. tb.UndoLimit = 1;
  1079. Assert.False(tb.CanUndo);
  1080. Assert.False(tb.CanRedo);
  1081. }
  1082. }
  1083. [Fact]
  1084. public void Setting_IsUndoEnabled_To_False_Clears_Undo_Redo()
  1085. {
  1086. using (UnitTestApplication.Start(Services))
  1087. {
  1088. var tb = new TextBox
  1089. {
  1090. Template = CreateTemplate(),
  1091. };
  1092. tb.Measure(Size.Infinity);
  1093. // This is all the same as the above test (CanUndo_CanRedo_and_Programmatic_Undo_Redo_Works)
  1094. // We do this to get the undo/redo stacks in a state where both are active
  1095. RaiseTextEvent(tb, "ABC");
  1096. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  1097. RaiseTextEvent(tb, "DEF");
  1098. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  1099. RaiseTextEvent(tb, "123");
  1100. Assert.Equal("ABCDEF123", tb.Text);
  1101. Assert.True(tb.CanUndo);
  1102. tb.Undo();
  1103. // Undo will take us back one step
  1104. Assert.Equal("ABCDEF", tb.Text);
  1105. Assert.True(tb.CanRedo);
  1106. tb.Redo();
  1107. // Redo should restore us
  1108. Assert.Equal("ABCDEF123", tb.Text);
  1109. // Disable Undo/Redo, this should clear both stacks setting CanUndo and CanRedo to false
  1110. tb.IsUndoEnabled = false;
  1111. Assert.False(tb.CanUndo);
  1112. Assert.False(tb.CanRedo);
  1113. }
  1114. }
  1115. [Fact]
  1116. public void UndoLimit_Count_Is_Respected()
  1117. {
  1118. using (UnitTestApplication.Start(Services))
  1119. {
  1120. var tb = new TextBox
  1121. {
  1122. Template = CreateTemplate(),
  1123. UndoLimit = 3 // Something small for this test
  1124. };
  1125. tb.Measure(Size.Infinity);
  1126. // Push 3 undoable actions, we should only be able to recover 2
  1127. RaiseTextEvent(tb, "ABC");
  1128. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  1129. RaiseTextEvent(tb, "DEF");
  1130. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  1131. RaiseTextEvent(tb, "123");
  1132. Assert.Equal("ABCDEF123", tb.Text);
  1133. // Undo will take us back one step
  1134. tb.Undo();
  1135. Assert.Equal("ABCDEF", tb.Text);
  1136. // Undo again
  1137. tb.Undo();
  1138. Assert.Equal("ABC", tb.Text);
  1139. // We now should not be able to undo again
  1140. Assert.False(tb.CanUndo);
  1141. }
  1142. }
  1143. [Fact]
  1144. public void Should_Move_Caret_To_EndOfLine()
  1145. {
  1146. using (UnitTestApplication.Start(Services))
  1147. {
  1148. var tb = new TextBox
  1149. {
  1150. Template = CreateTemplate(),
  1151. Text = "AB\nAB"
  1152. };
  1153. tb.Measure(Size.Infinity);
  1154. RaiseKeyEvent(tb, Key.End, KeyModifiers.Shift);
  1155. Assert.Equal(2, tb.CaretIndex);
  1156. }
  1157. }
  1158. [Fact]
  1159. public void TextBox_In_AdornerLayer_Will_Not_Cause_Collection_Modified_In_VisualLayerManager()
  1160. {
  1161. using (UnitTestApplication.Start(Services))
  1162. {
  1163. var button = new Button();
  1164. var root = new TestRoot()
  1165. {
  1166. Child = new VisualLayerManager()
  1167. {
  1168. Child = button
  1169. }
  1170. };
  1171. var adorner = new TextBox { Template = CreateTemplate(), Text = "a" };
  1172. var adornerLayer = AdornerLayer.GetAdornerLayer(button);
  1173. adornerLayer.Children.Add(adorner);
  1174. AdornerLayer.SetAdornedElement(adorner, button);
  1175. root.Measure(Size.Infinity);
  1176. }
  1177. }
  1178. [Theory]
  1179. [InlineData("A\nBB\nCCC\nDDDD", 0, 0)]
  1180. [InlineData("A\nBB\nCCC\nDDDD", 1, 2)]
  1181. [InlineData("A\nBB\nCCC\nDDDD", 2, 5)]
  1182. [InlineData("A\nBB\nCCC\nDDDD", 3, 9)]
  1183. [InlineData("واحد\nاثنين\nثلاثة\nأربعة", 0, 0)]
  1184. [InlineData("واحد\nاثنين\nثلاثة\nأربعة", 1, 5)]
  1185. [InlineData("واحد\nاثنين\nثلاثة\nأربعة", 2, 11)]
  1186. [InlineData("واحد\nاثنين\nثلاثة\nأربعة", 3, 17)]
  1187. public void Should_Scroll_Caret_To_Line(string text, int targetLineIndex, int expectedCaretIndex)
  1188. {
  1189. using (UnitTestApplication.Start(Services))
  1190. {
  1191. var tb = new TextBox
  1192. {
  1193. Template = CreateTemplate(),
  1194. Text = text
  1195. };
  1196. tb.ApplyTemplate();
  1197. tb.ScrollToLine(targetLineIndex);
  1198. Assert.Equal(expectedCaretIndex, tb.CaretIndex);
  1199. }
  1200. }
  1201. [Fact]
  1202. public void Should_Throw_ArgumentOutOfRange()
  1203. {
  1204. using (UnitTestApplication.Start(Services))
  1205. {
  1206. var tb = new TextBox
  1207. {
  1208. Template = CreateTemplate(),
  1209. Text = string.Empty
  1210. };
  1211. tb.ApplyTemplate();
  1212. Assert.Throws<ArgumentOutOfRangeException>(() => tb.ScrollToLine(-1));
  1213. Assert.Throws<ArgumentOutOfRangeException>(() => tb.ScrollToLine(1));
  1214. }
  1215. }
  1216. private static TestServices FocusServices => TestServices.MockThreadingInterface.With(
  1217. focusManager: new FocusManager(),
  1218. keyboardDevice: () => new KeyboardDevice(),
  1219. keyboardNavigation: () => new KeyboardNavigationHandler(),
  1220. inputManager: new InputManager(),
  1221. standardCursorFactory: Mock.Of<ICursorFactory>(),
  1222. textShaperImpl: new HeadlessTextShaperStub(),
  1223. fontManagerImpl: new HeadlessFontManagerStub());
  1224. private static TestServices Services => TestServices.MockThreadingInterface.With(
  1225. standardCursorFactory: Mock.Of<ICursorFactory>(),
  1226. renderInterface: new HeadlessPlatformRenderInterface(),
  1227. textShaperImpl: new HeadlessTextShaperStub(),
  1228. fontManagerImpl: new HeadlessFontManagerStub());
  1229. internal static IControlTemplate CreateTemplate()
  1230. {
  1231. return new FuncControlTemplate<TextBox>((control, scope) =>
  1232. new ScrollViewer
  1233. {
  1234. Name = "PART_ScrollViewer",
  1235. Template = new FuncControlTemplate<ScrollViewer>(ScrollViewerTests.CreateTemplate),
  1236. Content = new TextPresenter
  1237. {
  1238. Name = "PART_TextPresenter",
  1239. [!!TextPresenter.TextProperty] = new Binding
  1240. {
  1241. Path = nameof(TextPresenter.Text),
  1242. Mode = BindingMode.TwoWay,
  1243. Priority = BindingPriority.Template,
  1244. RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),
  1245. },
  1246. [!!TextPresenter.CaretIndexProperty] = new Binding
  1247. {
  1248. Path = nameof(TextPresenter.CaretIndex),
  1249. Mode = BindingMode.TwoWay,
  1250. Priority = BindingPriority.Template,
  1251. RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),
  1252. }
  1253. }.RegisterInNameScope(scope)
  1254. }.RegisterInNameScope(scope));
  1255. }
  1256. private static void RaiseKeyEvent(TextBox textBox, Key key, KeyModifiers inputModifiers)
  1257. {
  1258. textBox.RaiseEvent(new KeyEventArgs
  1259. {
  1260. RoutedEvent = InputElement.KeyDownEvent,
  1261. KeyModifiers = inputModifiers,
  1262. Key = key
  1263. });
  1264. }
  1265. private static void RaiseTextEvent(TextBox textBox, string text)
  1266. {
  1267. textBox.RaiseEvent(new TextInputEventArgs
  1268. {
  1269. RoutedEvent = InputElement.TextInputEvent,
  1270. Text = text
  1271. });
  1272. }
  1273. private class Class1 : NotifyingBase
  1274. {
  1275. private int _foo;
  1276. private string _bar;
  1277. public int Foo
  1278. {
  1279. get { return _foo; }
  1280. set { _foo = value; RaisePropertyChanged(); }
  1281. }
  1282. public string Bar
  1283. {
  1284. get { return _bar; }
  1285. set { _bar = value; RaisePropertyChanged(); }
  1286. }
  1287. }
  1288. private class ClipboardStub : IClipboard // in order to get tests working that use the clipboard
  1289. {
  1290. private string _text;
  1291. public Task<string> GetTextAsync() => Task.FromResult(_text);
  1292. public Task SetTextAsync(string text)
  1293. {
  1294. _text = text;
  1295. return Task.CompletedTask;
  1296. }
  1297. public Task ClearAsync()
  1298. {
  1299. _text = null;
  1300. return Task.CompletedTask;
  1301. }
  1302. public Task SetDataObjectAsync(IDataObject data) => Task.CompletedTask;
  1303. public Task<string[]> GetFormatsAsync() => Task.FromResult(Array.Empty<string>());
  1304. public Task<object> GetDataAsync(string format) => Task.FromResult((object)null);
  1305. }
  1306. private class TestTopLevel : TopLevel
  1307. {
  1308. private readonly ILayoutManager _layoutManager;
  1309. public TestTopLevel(ITopLevelImpl impl, ILayoutManager layoutManager = null)
  1310. : base(impl)
  1311. {
  1312. _layoutManager = layoutManager ?? new LayoutManager(this);
  1313. }
  1314. private protected override ILayoutManager CreateLayoutManager() => _layoutManager;
  1315. }
  1316. private static Mock<ITopLevelImpl> CreateMockTopLevelImpl()
  1317. {
  1318. var clipboard = new Mock<ITopLevelImpl>();
  1319. clipboard.Setup(x => x.Compositor).Returns(RendererMocks.CreateDummyCompositor());
  1320. clipboard.Setup(r => r.TryGetFeature(typeof(IClipboard)))
  1321. .Returns(new ClipboardStub());
  1322. clipboard.SetupGet(x => x.RenderScaling).Returns(1);
  1323. return clipboard;
  1324. }
  1325. private static FuncControlTemplate<TestTopLevel> CreateTopLevelTemplate()
  1326. {
  1327. return new FuncControlTemplate<TestTopLevel>((x, scope) =>
  1328. new ContentPresenter
  1329. {
  1330. Name = "PART_ContentPresenter",
  1331. [!ContentPresenter.ContentProperty] = x[!ContentControl.ContentProperty],
  1332. }.RegisterInNameScope(scope));
  1333. }
  1334. private class TestContextMenu : ContextMenu
  1335. {
  1336. public TestContextMenu()
  1337. {
  1338. IsOpen = true;
  1339. }
  1340. }
  1341. }
  1342. }