TextBoxTests.cs 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  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 Should_Fullfill_MaxLines_Contraint()
  787. {
  788. using (UnitTestApplication.Start(Services))
  789. {
  790. var target = new TextBox
  791. {
  792. Template = CreateTemplate(),
  793. Text = "ABC",
  794. MaxLines = 1,
  795. AcceptsReturn = true
  796. };
  797. var impl = CreateMockTopLevelImpl();
  798. var topLevel = new TestTopLevel(impl.Object)
  799. {
  800. Template = CreateTopLevelTemplate()
  801. };
  802. topLevel.Content = target;
  803. topLevel.ApplyTemplate();
  804. topLevel.LayoutManager.ExecuteInitialLayoutPass();
  805. target.ApplyTemplate();
  806. target.Measure(Size.Infinity);
  807. var initialHeight = target.DesiredSize.Height;
  808. topLevel.Clipboard?.SetTextAsync(Environment.NewLine).GetAwaiter().GetResult();
  809. RaiseKeyEvent(target, Key.V, KeyModifiers.Control);
  810. topLevel.Clipboard?.ClearAsync().GetAwaiter().GetResult();
  811. RaiseTextEvent(target, Environment.NewLine);
  812. target.InvalidateMeasure();
  813. target.Measure(Size.Infinity);
  814. Assert.Equal(initialHeight, target.DesiredSize.Height);
  815. }
  816. }
  817. [Theory]
  818. [InlineData(1)]
  819. [InlineData(2)]
  820. [InlineData(3)]
  821. public void MaxLines_Sets_ScrollViewer_MaxHeight(int maxLines)
  822. {
  823. using (UnitTestApplication.Start(Services))
  824. {
  825. var target = new TextBox
  826. {
  827. Template = CreateTemplate(),
  828. MaxLines = maxLines,
  829. // Define explicit whole number line height for predictable calculations
  830. LineHeight = 20
  831. };
  832. var impl = CreateMockTopLevelImpl();
  833. var topLevel = new TestTopLevel(impl.Object)
  834. {
  835. Template = CreateTopLevelTemplate(),
  836. Content = target
  837. };
  838. topLevel.ApplyTemplate();
  839. topLevel.LayoutManager.ExecuteInitialLayoutPass();
  840. var textPresenter = target.FindDescendantOfType<TextPresenter>();
  841. Assert.Equal("PART_TextPresenter", textPresenter.Name);
  842. Assert.Equal(new Thickness(0), textPresenter.Margin); // Test assumes no margin on TextPresenter
  843. var scrollViewer = target.FindDescendantOfType<ScrollViewer>();
  844. Assert.Equal("PART_ScrollViewer", scrollViewer.Name);
  845. Assert.Equal(maxLines * target.LineHeight, scrollViewer.MaxHeight);
  846. }
  847. }
  848. [Theory]
  849. [InlineData(1)]
  850. [InlineData(2)]
  851. [InlineData(3)]
  852. public void MaxLines_Sets_ScrollViewer_MaxHeight_With_TextPresenter_Margin(int maxLines)
  853. {
  854. using (UnitTestApplication.Start(Services))
  855. {
  856. var target = new TextBox
  857. {
  858. Template = CreateTemplate(),
  859. MaxLines = maxLines,
  860. // Define explicit whole number line height for predictable calculations
  861. LineHeight = 20
  862. };
  863. var impl = CreateMockTopLevelImpl();
  864. var topLevel = new TestTopLevel(impl.Object)
  865. {
  866. Template = CreateTopLevelTemplate(),
  867. Content = target
  868. };
  869. topLevel.ApplyTemplate();
  870. topLevel.LayoutManager.ExecuteInitialLayoutPass();
  871. var textPresenter = target.FindDescendantOfType<TextPresenter>();
  872. Assert.Equal("PART_TextPresenter", textPresenter.Name);
  873. var textPresenterMargin = new Thickness(horizontal: 0, vertical: 3);
  874. textPresenter.Margin = textPresenterMargin;
  875. target.InvalidateMeasure();
  876. target.Measure(Size.Infinity);
  877. var scrollViewer = target.FindDescendantOfType<ScrollViewer>();
  878. Assert.Equal("PART_ScrollViewer", scrollViewer.Name);
  879. Assert.Equal((maxLines * target.LineHeight) + textPresenterMargin.Top + textPresenterMargin.Bottom, scrollViewer.MaxHeight);
  880. }
  881. }
  882. [Fact]
  883. public void Should_Fullfill_MinLines_Contraint()
  884. {
  885. using (UnitTestApplication.Start(Services))
  886. {
  887. var target = new TextBox
  888. {
  889. Template = CreateTemplate(),
  890. Text = "ABC \n DEF \n GHI",
  891. MinLines = 3,
  892. AcceptsReturn = true
  893. };
  894. var impl = CreateMockTopLevelImpl();
  895. var topLevel = new TestTopLevel(impl.Object)
  896. {
  897. Template = CreateTopLevelTemplate()
  898. };
  899. topLevel.Content = target;
  900. topLevel.ApplyTemplate();
  901. topLevel.LayoutManager.ExecuteInitialLayoutPass();
  902. target.ApplyTemplate();
  903. target.Measure(Size.Infinity);
  904. var initialHeight = target.DesiredSize.Height;
  905. target.Text = "";
  906. target.InvalidateMeasure();
  907. target.Measure(Size.Infinity);
  908. Assert.Equal(initialHeight, target.DesiredSize.Height);
  909. }
  910. }
  911. [Theory]
  912. [InlineData(1)]
  913. [InlineData(2)]
  914. [InlineData(3)]
  915. public void MinLines_Sets_ScrollViewer_MinHeight(int minLines)
  916. {
  917. using (UnitTestApplication.Start(Services))
  918. {
  919. var target = new TextBox
  920. {
  921. Template = CreateTemplate(),
  922. MinLines = minLines,
  923. // Define explicit whole number line height for predictable calculations
  924. LineHeight = 20
  925. };
  926. var impl = CreateMockTopLevelImpl();
  927. var topLevel = new TestTopLevel(impl.Object)
  928. {
  929. Template = CreateTopLevelTemplate(),
  930. Content = target
  931. };
  932. topLevel.ApplyTemplate();
  933. topLevel.LayoutManager.ExecuteInitialLayoutPass();
  934. var textPresenter = target.FindDescendantOfType<TextPresenter>();
  935. Assert.Equal("PART_TextPresenter", textPresenter.Name);
  936. Assert.Equal(new Thickness(0), textPresenter.Margin); // Test assumes no margin on TextPresenter
  937. var scrollViewer = target.FindDescendantOfType<ScrollViewer>();
  938. Assert.Equal("PART_ScrollViewer", scrollViewer.Name);
  939. Assert.Equal(minLines * target.LineHeight, scrollViewer.MinHeight);
  940. }
  941. }
  942. [Theory]
  943. [InlineData(1)]
  944. [InlineData(2)]
  945. [InlineData(3)]
  946. public void MinLines_Sets_ScrollViewer_MinHeight_With_TextPresenter_Margin(int minLines)
  947. {
  948. using (UnitTestApplication.Start(Services))
  949. {
  950. var target = new TextBox
  951. {
  952. Template = CreateTemplate(),
  953. MinLines = minLines,
  954. // Define explicit whole number line height for predictable calculations
  955. LineHeight = 20
  956. };
  957. var impl = CreateMockTopLevelImpl();
  958. var topLevel = new TestTopLevel(impl.Object)
  959. {
  960. Template = CreateTopLevelTemplate(),
  961. Content = target
  962. };
  963. topLevel.ApplyTemplate();
  964. topLevel.LayoutManager.ExecuteInitialLayoutPass();
  965. var textPresenter = target.FindDescendantOfType<TextPresenter>();
  966. Assert.Equal("PART_TextPresenter", textPresenter.Name);
  967. var textPresenterMargin = new Thickness(horizontal: 0, vertical: 3);
  968. textPresenter.Margin = textPresenterMargin;
  969. target.InvalidateMeasure();
  970. target.Measure(Size.Infinity);
  971. var scrollViewer = target.FindDescendantOfType<ScrollViewer>();
  972. Assert.Equal("PART_ScrollViewer", scrollViewer.Name);
  973. Assert.Equal((minLines * target.LineHeight) + textPresenterMargin.Top + textPresenterMargin.Bottom, scrollViewer.MinHeight);
  974. }
  975. }
  976. [Fact]
  977. public void CanUndo_CanRedo_Is_False_When_Initialized()
  978. {
  979. using (UnitTestApplication.Start(Services))
  980. {
  981. var tb = new TextBox
  982. {
  983. Template = CreateTemplate(),
  984. Text = "New Text"
  985. };
  986. tb.Measure(Size.Infinity);
  987. Assert.False(tb.CanUndo);
  988. Assert.False(tb.CanRedo);
  989. }
  990. }
  991. [Fact]
  992. public void CanUndo_CanRedo_and_Programmatic_Undo_Redo_Works()
  993. {
  994. using (UnitTestApplication.Start(Services))
  995. {
  996. var tb = new TextBox
  997. {
  998. Template = CreateTemplate(),
  999. };
  1000. tb.Measure(Size.Infinity);
  1001. // See GH #6024 for a bit more insight on when Undo/Redo snapshots are taken:
  1002. // - Every 'Space', but only when space is handled in OnKeyDown - Spaces in TextInput event won't work
  1003. // - Every 7 chars in a long word
  1004. RaiseTextEvent(tb, "ABC");
  1005. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  1006. RaiseTextEvent(tb, "DEF");
  1007. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  1008. RaiseTextEvent(tb, "123");
  1009. // NOTE: the spaces won't actually add spaces b/c they're sent only as key events and not Text events
  1010. // so our final text is without spaces
  1011. Assert.Equal("ABCDEF123", tb.Text);
  1012. Assert.True(tb.CanUndo);
  1013. tb.Undo();
  1014. // Undo will take us back one step
  1015. Assert.Equal("ABCDEF", tb.Text);
  1016. Assert.True(tb.CanRedo);
  1017. tb.Redo();
  1018. // Redo should restore us
  1019. Assert.Equal("ABCDEF123", tb.Text);
  1020. }
  1021. }
  1022. [Fact]
  1023. public void Setting_UndoLimit_Clears_Undo_Redo()
  1024. {
  1025. using (UnitTestApplication.Start(Services))
  1026. {
  1027. var tb = new TextBox
  1028. {
  1029. Template = CreateTemplate(),
  1030. };
  1031. tb.Measure(Size.Infinity);
  1032. // This is all the same as the above test (CanUndo_CanRedo_and_Programmatic_Undo_Redo_Works)
  1033. // We do this to get the undo/redo stacks in a state where both are active
  1034. RaiseTextEvent(tb, "ABC");
  1035. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  1036. RaiseTextEvent(tb, "DEF");
  1037. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  1038. RaiseTextEvent(tb, "123");
  1039. Assert.Equal("ABCDEF123", tb.Text);
  1040. Assert.True(tb.CanUndo);
  1041. tb.Undo();
  1042. // Undo will take us back one step
  1043. Assert.Equal("ABCDEF", tb.Text);
  1044. Assert.True(tb.CanRedo);
  1045. tb.Redo();
  1046. // Redo should restore us
  1047. Assert.Equal("ABCDEF123", tb.Text);
  1048. // Change the undo limit, this should clear both stacks setting CanUndo and CanRedo to false
  1049. tb.UndoLimit = 1;
  1050. Assert.False(tb.CanUndo);
  1051. Assert.False(tb.CanRedo);
  1052. }
  1053. }
  1054. [Fact]
  1055. public void Setting_IsUndoEnabled_To_False_Clears_Undo_Redo()
  1056. {
  1057. using (UnitTestApplication.Start(Services))
  1058. {
  1059. var tb = new TextBox
  1060. {
  1061. Template = CreateTemplate(),
  1062. };
  1063. tb.Measure(Size.Infinity);
  1064. // This is all the same as the above test (CanUndo_CanRedo_and_Programmatic_Undo_Redo_Works)
  1065. // We do this to get the undo/redo stacks in a state where both are active
  1066. RaiseTextEvent(tb, "ABC");
  1067. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  1068. RaiseTextEvent(tb, "DEF");
  1069. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  1070. RaiseTextEvent(tb, "123");
  1071. Assert.Equal("ABCDEF123", tb.Text);
  1072. Assert.True(tb.CanUndo);
  1073. tb.Undo();
  1074. // Undo will take us back one step
  1075. Assert.Equal("ABCDEF", tb.Text);
  1076. Assert.True(tb.CanRedo);
  1077. tb.Redo();
  1078. // Redo should restore us
  1079. Assert.Equal("ABCDEF123", tb.Text);
  1080. // Disable Undo/Redo, this should clear both stacks setting CanUndo and CanRedo to false
  1081. tb.IsUndoEnabled = false;
  1082. Assert.False(tb.CanUndo);
  1083. Assert.False(tb.CanRedo);
  1084. }
  1085. }
  1086. [Fact]
  1087. public void UndoLimit_Count_Is_Respected()
  1088. {
  1089. using (UnitTestApplication.Start(Services))
  1090. {
  1091. var tb = new TextBox
  1092. {
  1093. Template = CreateTemplate(),
  1094. UndoLimit = 3 // Something small for this test
  1095. };
  1096. tb.Measure(Size.Infinity);
  1097. // Push 3 undoable actions, we should only be able to recover 2
  1098. RaiseTextEvent(tb, "ABC");
  1099. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  1100. RaiseTextEvent(tb, "DEF");
  1101. RaiseKeyEvent(tb, Key.Space, KeyModifiers.None);
  1102. RaiseTextEvent(tb, "123");
  1103. Assert.Equal("ABCDEF123", tb.Text);
  1104. // Undo will take us back one step
  1105. tb.Undo();
  1106. Assert.Equal("ABCDEF", tb.Text);
  1107. // Undo again
  1108. tb.Undo();
  1109. Assert.Equal("ABC", tb.Text);
  1110. // We now should not be able to undo again
  1111. Assert.False(tb.CanUndo);
  1112. }
  1113. }
  1114. [Fact]
  1115. public void Should_Move_Caret_To_EndOfLine()
  1116. {
  1117. using (UnitTestApplication.Start(Services))
  1118. {
  1119. var tb = new TextBox
  1120. {
  1121. Template = CreateTemplate(),
  1122. Text = "AB\nAB"
  1123. };
  1124. tb.Measure(Size.Infinity);
  1125. RaiseKeyEvent(tb, Key.End, KeyModifiers.Shift);
  1126. Assert.Equal(2, tb.CaretIndex);
  1127. }
  1128. }
  1129. [Fact]
  1130. public void TextBox_In_AdornerLayer_Will_Not_Cause_Collection_Modified_In_VisualLayerManager()
  1131. {
  1132. using (UnitTestApplication.Start(Services))
  1133. {
  1134. var button = new Button();
  1135. var root = new TestRoot()
  1136. {
  1137. Child = new VisualLayerManager()
  1138. {
  1139. Child = button
  1140. }
  1141. };
  1142. var adorner = new TextBox { Template = CreateTemplate(), Text = "a" };
  1143. var adornerLayer = AdornerLayer.GetAdornerLayer(button);
  1144. adornerLayer.Children.Add(adorner);
  1145. AdornerLayer.SetAdornedElement(adorner, button);
  1146. root.Measure(Size.Infinity);
  1147. }
  1148. }
  1149. [Theory]
  1150. [InlineData("A\nBB\nCCC\nDDDD", 0, 0)]
  1151. [InlineData("A\nBB\nCCC\nDDDD", 1, 2)]
  1152. [InlineData("A\nBB\nCCC\nDDDD", 2, 5)]
  1153. [InlineData("A\nBB\nCCC\nDDDD", 3, 9)]
  1154. [InlineData("واحد\nاثنين\nثلاثة\nأربعة", 0, 0)]
  1155. [InlineData("واحد\nاثنين\nثلاثة\nأربعة", 1, 5)]
  1156. [InlineData("واحد\nاثنين\nثلاثة\nأربعة", 2, 11)]
  1157. [InlineData("واحد\nاثنين\nثلاثة\nأربعة", 3, 17)]
  1158. public void Should_Scroll_Caret_To_Line(string text, int targetLineIndex, int expectedCaretIndex)
  1159. {
  1160. using (UnitTestApplication.Start(Services))
  1161. {
  1162. var tb = new TextBox
  1163. {
  1164. Template = CreateTemplate(),
  1165. Text = text
  1166. };
  1167. tb.ApplyTemplate();
  1168. tb.ScrollToLine(targetLineIndex);
  1169. Assert.Equal(expectedCaretIndex, tb.CaretIndex);
  1170. }
  1171. }
  1172. [Fact]
  1173. public void Should_Throw_ArgumentOutOfRange()
  1174. {
  1175. using (UnitTestApplication.Start(Services))
  1176. {
  1177. var tb = new TextBox
  1178. {
  1179. Template = CreateTemplate(),
  1180. Text = string.Empty
  1181. };
  1182. tb.ApplyTemplate();
  1183. Assert.Throws<ArgumentOutOfRangeException>(() => tb.ScrollToLine(-1));
  1184. Assert.Throws<ArgumentOutOfRangeException>(() => tb.ScrollToLine(1));
  1185. }
  1186. }
  1187. private static TestServices FocusServices => TestServices.MockThreadingInterface.With(
  1188. focusManager: new FocusManager(),
  1189. keyboardDevice: () => new KeyboardDevice(),
  1190. keyboardNavigation: () => new KeyboardNavigationHandler(),
  1191. inputManager: new InputManager(),
  1192. standardCursorFactory: Mock.Of<ICursorFactory>(),
  1193. textShaperImpl: new HeadlessTextShaperStub(),
  1194. fontManagerImpl: new HeadlessFontManagerStub());
  1195. private static TestServices Services => TestServices.MockThreadingInterface.With(
  1196. standardCursorFactory: Mock.Of<ICursorFactory>(),
  1197. renderInterface: new HeadlessPlatformRenderInterface(),
  1198. textShaperImpl: new HeadlessTextShaperStub(),
  1199. fontManagerImpl: new HeadlessFontManagerStub());
  1200. private IControlTemplate CreateTemplate()
  1201. {
  1202. return new FuncControlTemplate<TextBox>((control, scope) =>
  1203. new ScrollViewer
  1204. {
  1205. Name = "PART_ScrollViewer",
  1206. Template = new FuncControlTemplate<ScrollViewer>(ScrollViewerTests.CreateTemplate),
  1207. Content = new TextPresenter
  1208. {
  1209. Name = "PART_TextPresenter",
  1210. [!!TextPresenter.TextProperty] = new Binding
  1211. {
  1212. Path = nameof(TextPresenter.Text),
  1213. Mode = BindingMode.TwoWay,
  1214. Priority = BindingPriority.Template,
  1215. RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),
  1216. },
  1217. [!!TextPresenter.CaretIndexProperty] = new Binding
  1218. {
  1219. Path = nameof(TextPresenter.CaretIndex),
  1220. Mode = BindingMode.TwoWay,
  1221. Priority = BindingPriority.Template,
  1222. RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),
  1223. }
  1224. }.RegisterInNameScope(scope)
  1225. }.RegisterInNameScope(scope));
  1226. }
  1227. private static void RaiseKeyEvent(TextBox textBox, Key key, KeyModifiers inputModifiers)
  1228. {
  1229. textBox.RaiseEvent(new KeyEventArgs
  1230. {
  1231. RoutedEvent = InputElement.KeyDownEvent,
  1232. KeyModifiers = inputModifiers,
  1233. Key = key
  1234. });
  1235. }
  1236. private static void RaiseTextEvent(TextBox textBox, string text)
  1237. {
  1238. textBox.RaiseEvent(new TextInputEventArgs
  1239. {
  1240. RoutedEvent = InputElement.TextInputEvent,
  1241. Text = text
  1242. });
  1243. }
  1244. private class Class1 : NotifyingBase
  1245. {
  1246. private int _foo;
  1247. private string _bar;
  1248. public int Foo
  1249. {
  1250. get { return _foo; }
  1251. set { _foo = value; RaisePropertyChanged(); }
  1252. }
  1253. public string Bar
  1254. {
  1255. get { return _bar; }
  1256. set { _bar = value; RaisePropertyChanged(); }
  1257. }
  1258. }
  1259. private class ClipboardStub : IClipboard // in order to get tests working that use the clipboard
  1260. {
  1261. private string _text;
  1262. public Task<string> GetTextAsync() => Task.FromResult(_text);
  1263. public Task SetTextAsync(string text)
  1264. {
  1265. _text = text;
  1266. return Task.CompletedTask;
  1267. }
  1268. public Task ClearAsync()
  1269. {
  1270. _text = null;
  1271. return Task.CompletedTask;
  1272. }
  1273. public Task SetDataObjectAsync(IDataObject data) => Task.CompletedTask;
  1274. public Task<string[]> GetFormatsAsync() => Task.FromResult(Array.Empty<string>());
  1275. public Task<object> GetDataAsync(string format) => Task.FromResult((object)null);
  1276. }
  1277. private class TestTopLevel : TopLevel
  1278. {
  1279. private readonly ILayoutManager _layoutManager;
  1280. public TestTopLevel(ITopLevelImpl impl, ILayoutManager layoutManager = null)
  1281. : base(impl)
  1282. {
  1283. _layoutManager = layoutManager ?? new LayoutManager(this);
  1284. }
  1285. private protected override ILayoutManager CreateLayoutManager() => _layoutManager;
  1286. }
  1287. private static Mock<ITopLevelImpl> CreateMockTopLevelImpl()
  1288. {
  1289. var clipboard = new Mock<ITopLevelImpl>();
  1290. clipboard.Setup(x => x.Compositor).Returns(RendererMocks.CreateDummyCompositor());
  1291. clipboard.Setup(r => r.TryGetFeature(typeof(IClipboard)))
  1292. .Returns(new ClipboardStub());
  1293. clipboard.SetupGet(x => x.RenderScaling).Returns(1);
  1294. return clipboard;
  1295. }
  1296. private static FuncControlTemplate<TestTopLevel> CreateTopLevelTemplate()
  1297. {
  1298. return new FuncControlTemplate<TestTopLevel>((x, scope) =>
  1299. new ContentPresenter
  1300. {
  1301. Name = "PART_ContentPresenter",
  1302. [!ContentPresenter.ContentProperty] = x[!ContentControl.ContentProperty],
  1303. }.RegisterInNameScope(scope));
  1304. }
  1305. private class TestContextMenu : ContextMenu
  1306. {
  1307. public TestContextMenu()
  1308. {
  1309. IsOpen = true;
  1310. }
  1311. }
  1312. }
  1313. }