TextBoxTests.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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.Input;
  10. using Avalonia.Input.Platform;
  11. using Avalonia.Media;
  12. using Avalonia.Platform;
  13. using Avalonia.UnitTests;
  14. using Moq;
  15. using Xunit;
  16. namespace Avalonia.Controls.UnitTests
  17. {
  18. public class TextBoxTests
  19. {
  20. [Fact]
  21. public void Opening_Context_Menu_Does_not_Lose_Selection()
  22. {
  23. using (UnitTestApplication.Start(FocusServices))
  24. {
  25. var target1 = new TextBox
  26. {
  27. Template = CreateTemplate(),
  28. Text = "1234",
  29. ContextMenu = new TestContextMenu()
  30. };
  31. var target2 = new TextBox
  32. {
  33. Template = CreateTemplate(),
  34. Text = "5678"
  35. };
  36. var sp = new StackPanel();
  37. sp.Children.Add(target1);
  38. sp.Children.Add(target2);
  39. target1.ApplyTemplate();
  40. target2.ApplyTemplate();
  41. var root = new TestRoot() { Child = sp };
  42. target1.SelectionStart = 0;
  43. target1.SelectionEnd = 3;
  44. target1.Focus();
  45. Assert.False(target2.IsFocused);
  46. Assert.True(target1.IsFocused);
  47. target2.Focus();
  48. Assert.Equal("123", target1.SelectedText);
  49. }
  50. }
  51. [Fact]
  52. public void Opening_Context_Flyout_Does_not_Lose_Selection()
  53. {
  54. using (UnitTestApplication.Start(FocusServices))
  55. {
  56. var target1 = new TextBox
  57. {
  58. Template = CreateTemplate(),
  59. Text = "1234",
  60. ContextFlyout = new MenuFlyout
  61. {
  62. Items = new List<MenuItem>
  63. {
  64. new MenuItem { Header = "Item 1" },
  65. new MenuItem {Header = "Item 2" },
  66. new MenuItem {Header = "Item 3" }
  67. }
  68. }
  69. };
  70. target1.ApplyTemplate();
  71. var root = new TestRoot() { Child = target1 };
  72. target1.SelectionStart = 0;
  73. target1.SelectionEnd = 3;
  74. target1.Focus();
  75. Assert.True(target1.IsFocused);
  76. target1.ContextFlyout.ShowAt(target1);
  77. Assert.Equal("123", target1.SelectedText);
  78. }
  79. }
  80. [Fact]
  81. public void DefaultBindingMode_Should_Be_TwoWay()
  82. {
  83. Assert.Equal(
  84. BindingMode.TwoWay,
  85. TextBox.TextProperty.GetMetadata(typeof(TextBox)).DefaultBindingMode);
  86. }
  87. [Fact]
  88. public void CaretIndex_Can_Moved_To_Position_After_The_End_Of_Text_With_Arrow_Key()
  89. {
  90. using (UnitTestApplication.Start(Services))
  91. {
  92. var target = new TextBox
  93. {
  94. Template = CreateTemplate(),
  95. Text = "1234"
  96. };
  97. target.CaretIndex = 3;
  98. RaiseKeyEvent(target, Key.Right, 0);
  99. Assert.Equal(4, target.CaretIndex);
  100. }
  101. }
  102. [Fact]
  103. public void Press_Ctrl_A_Select_All_Text()
  104. {
  105. using (UnitTestApplication.Start(Services))
  106. {
  107. var target = new TextBox
  108. {
  109. Template = CreateTemplate(),
  110. Text = "1234"
  111. };
  112. RaiseKeyEvent(target, Key.A, KeyModifiers.Control);
  113. Assert.Equal(0, target.SelectionStart);
  114. Assert.Equal(4, target.SelectionEnd);
  115. }
  116. }
  117. [Fact]
  118. public void Press_Ctrl_A_Select_All_Null_Text()
  119. {
  120. using (UnitTestApplication.Start(Services))
  121. {
  122. var target = new TextBox
  123. {
  124. Template = CreateTemplate()
  125. };
  126. RaiseKeyEvent(target, Key.A, KeyModifiers.Control);
  127. Assert.Equal(0, target.SelectionStart);
  128. Assert.Equal(0, target.SelectionEnd);
  129. }
  130. }
  131. [Fact]
  132. public void Press_Ctrl_Z_Will_Not_Modify_Text()
  133. {
  134. using (UnitTestApplication.Start(Services))
  135. {
  136. var target = new TextBox
  137. {
  138. Template = CreateTemplate(),
  139. Text = "1234"
  140. };
  141. RaiseKeyEvent(target, Key.Z, KeyModifiers.Control);
  142. Assert.Equal("1234", target.Text);
  143. }
  144. }
  145. [Fact]
  146. public void Typing_Beginning_With_0_Should_Not_Modify_Text_When_Bound_To_Int()
  147. {
  148. using (UnitTestApplication.Start(Services))
  149. {
  150. var source = new Class1();
  151. var target = new TextBox
  152. {
  153. DataContext = source,
  154. Template = CreateTemplate(),
  155. };
  156. target.ApplyTemplate();
  157. target.Bind(TextBox.TextProperty, new Binding(nameof(Class1.Foo), BindingMode.TwoWay));
  158. Assert.Equal("0", target.Text);
  159. target.CaretIndex = 1;
  160. target.RaiseEvent(new TextInputEventArgs
  161. {
  162. RoutedEvent = InputElement.TextInputEvent,
  163. Text = "2",
  164. });
  165. Assert.Equal("02", target.Text);
  166. }
  167. }
  168. [Fact]
  169. public void Control_Backspace_Should_Remove_The_Word_Before_The_Caret_If_There_Is_No_Selection()
  170. {
  171. using (UnitTestApplication.Start(Services))
  172. {
  173. TextBox textBox = new TextBox
  174. {
  175. Text = "First Second Third Fourth",
  176. CaretIndex = 5
  177. };
  178. // (First| Second Third Fourth)
  179. RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
  180. Assert.Equal(" Second Third Fourth", textBox.Text);
  181. // ( Second |Third Fourth)
  182. textBox.CaretIndex = 8;
  183. RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
  184. Assert.Equal(" Third Fourth", textBox.Text);
  185. // ( Thi|rd Fourth)
  186. textBox.CaretIndex = 4;
  187. RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
  188. Assert.Equal(" rd Fourth", textBox.Text);
  189. // ( rd F[ou]rth)
  190. textBox.SelectionStart = 5;
  191. textBox.SelectionEnd = 7;
  192. RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
  193. Assert.Equal(" rd Frth", textBox.Text);
  194. // ( |rd Frth)
  195. textBox.CaretIndex = 1;
  196. RaiseKeyEvent(textBox, Key.Back, KeyModifiers.Control);
  197. Assert.Equal("rd Frth", textBox.Text);
  198. }
  199. }
  200. [Fact]
  201. public void Control_Delete_Should_Remove_The_Word_After_The_Caret_If_There_Is_No_Selection()
  202. {
  203. using (UnitTestApplication.Start(Services))
  204. {
  205. TextBox textBox = new TextBox
  206. {
  207. Text = "First Second Third Fourth",
  208. CaretIndex = 19
  209. };
  210. // (First Second Third |Fourth)
  211. RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
  212. Assert.Equal("First Second Third ", textBox.Text);
  213. // (First Second |Third )
  214. textBox.CaretIndex = 13;
  215. RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
  216. Assert.Equal("First Second ", textBox.Text);
  217. // (First Sec|ond )
  218. textBox.CaretIndex = 9;
  219. RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
  220. Assert.Equal("First Sec", textBox.Text);
  221. // (Fi[rs]t Sec )
  222. textBox.SelectionStart = 2;
  223. textBox.SelectionEnd = 4;
  224. RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
  225. Assert.Equal("Fit Sec", textBox.Text);
  226. // (Fit Sec| )
  227. textBox.Text += " ";
  228. textBox.CaretIndex = 7;
  229. RaiseKeyEvent(textBox, Key.Delete, KeyModifiers.Control);
  230. Assert.Equal("Fit Sec", textBox.Text);
  231. }
  232. }
  233. [Fact]
  234. public void Setting_SelectionStart_To_SelectionEnd_Sets_CaretPosition_To_SelectionStart()
  235. {
  236. using (UnitTestApplication.Start(Services))
  237. {
  238. var textBox = new TextBox
  239. {
  240. Text = "0123456789"
  241. };
  242. textBox.SelectionStart = 2;
  243. textBox.SelectionEnd = 2;
  244. Assert.Equal(2, textBox.CaretIndex);
  245. }
  246. }
  247. [Fact]
  248. public void Setting_Text_Updates_CaretPosition()
  249. {
  250. using (UnitTestApplication.Start(Services))
  251. {
  252. var target = new TextBox
  253. {
  254. Text = "Initial Text",
  255. CaretIndex = 11
  256. };
  257. var invoked = false;
  258. target.GetObservable(TextBox.TextProperty).Skip(1).Subscribe(_ =>
  259. {
  260. // Caret index should be set before Text changed notification, as we don't want
  261. // to notify with an invalid CaretIndex.
  262. Assert.Equal(7, target.CaretIndex);
  263. invoked = true;
  264. });
  265. target.Text = "Changed";
  266. Assert.True(invoked);
  267. }
  268. }
  269. [Fact]
  270. public void Press_Enter_Does_Not_Accept_Return()
  271. {
  272. using (UnitTestApplication.Start(Services))
  273. {
  274. var target = new TextBox
  275. {
  276. Template = CreateTemplate(),
  277. AcceptsReturn = false,
  278. Text = "1234"
  279. };
  280. RaiseKeyEvent(target, Key.Enter, 0);
  281. Assert.Equal("1234", target.Text);
  282. }
  283. }
  284. [Fact]
  285. public void Press_Enter_Add_Default_Newline()
  286. {
  287. using (UnitTestApplication.Start(Services))
  288. {
  289. var target = new TextBox
  290. {
  291. Template = CreateTemplate(),
  292. AcceptsReturn = true
  293. };
  294. RaiseKeyEvent(target, Key.Enter, 0);
  295. Assert.Equal(Environment.NewLine, target.Text);
  296. }
  297. }
  298. [Fact]
  299. public void Press_Enter_Add_Custom_Newline()
  300. {
  301. using (UnitTestApplication.Start(Services))
  302. {
  303. var target = new TextBox
  304. {
  305. Template = CreateTemplate(),
  306. AcceptsReturn = true,
  307. NewLine = "Test"
  308. };
  309. RaiseKeyEvent(target, Key.Enter, 0);
  310. Assert.Equal("Test", target.Text);
  311. }
  312. }
  313. [Theory]
  314. [InlineData(new object[] { false, TextWrapping.NoWrap, ScrollBarVisibility.Hidden })]
  315. [InlineData(new object[] { false, TextWrapping.Wrap, ScrollBarVisibility.Hidden })]
  316. [InlineData(new object[] { true, TextWrapping.NoWrap, ScrollBarVisibility.Auto })]
  317. [InlineData(new object[] { true, TextWrapping.Wrap, ScrollBarVisibility.Disabled })]
  318. public void Has_Correct_Horizontal_ScrollBar_Visibility(
  319. bool acceptsReturn,
  320. TextWrapping wrapping,
  321. ScrollBarVisibility expected)
  322. {
  323. using (UnitTestApplication.Start(Services))
  324. {
  325. var target = new TextBox
  326. {
  327. AcceptsReturn = acceptsReturn,
  328. TextWrapping = wrapping,
  329. };
  330. Assert.Equal(expected, ScrollViewer.GetHorizontalScrollBarVisibility(target));
  331. }
  332. }
  333. [Fact]
  334. public void SelectionEnd_Doesnt_Cause_Exception()
  335. {
  336. using (UnitTestApplication.Start(Services))
  337. {
  338. var target = new TextBox
  339. {
  340. Template = CreateTemplate(),
  341. Text = "0123456789"
  342. };
  343. target.SelectionStart = 0;
  344. target.SelectionEnd = 9;
  345. target.Text = "123";
  346. RaiseTextEvent(target, "456");
  347. Assert.True(true);
  348. }
  349. }
  350. [Fact]
  351. public void SelectionStart_Doesnt_Cause_Exception()
  352. {
  353. using (UnitTestApplication.Start(Services))
  354. {
  355. var target = new TextBox
  356. {
  357. Template = CreateTemplate(),
  358. Text = "0123456789"
  359. };
  360. target.SelectionStart = 8;
  361. target.SelectionEnd = 9;
  362. target.Text = "123";
  363. RaiseTextEvent(target, "456");
  364. Assert.True(true);
  365. }
  366. }
  367. [Fact]
  368. public void SelectionStartEnd_Are_Valid_AterTextChange()
  369. {
  370. using (UnitTestApplication.Start(Services))
  371. {
  372. var target = new TextBox
  373. {
  374. Template = CreateTemplate(),
  375. Text = "0123456789"
  376. };
  377. target.SelectionStart = 8;
  378. target.SelectionEnd = 9;
  379. target.Text = "123";
  380. Assert.True(target.SelectionStart <= "123".Length);
  381. Assert.True(target.SelectionEnd <= "123".Length);
  382. }
  383. }
  384. [Fact]
  385. public void SelectedText_Changes_OnSelectionChange()
  386. {
  387. using (UnitTestApplication.Start(Services))
  388. {
  389. var target = new TextBox
  390. {
  391. Template = CreateTemplate(),
  392. Text = "0123456789"
  393. };
  394. Assert.True(target.SelectedText == "");
  395. target.SelectionStart = 2;
  396. target.SelectionEnd = 4;
  397. Assert.True(target.SelectedText == "23");
  398. }
  399. }
  400. [Fact]
  401. public void SelectedText_EditsText()
  402. {
  403. using (UnitTestApplication.Start(Services))
  404. {
  405. var target = new TextBox
  406. {
  407. Template = CreateTemplate(),
  408. Text = "0123"
  409. };
  410. target.SelectedText = "AA";
  411. Assert.True(target.Text == "AA0123");
  412. target.SelectionStart = 1;
  413. target.SelectionEnd = 3;
  414. target.SelectedText = "BB";
  415. Assert.True(target.Text == "ABB123");
  416. }
  417. }
  418. [Fact]
  419. public void SelectedText_CanClearText()
  420. {
  421. using (UnitTestApplication.Start(Services))
  422. {
  423. var target = new TextBox
  424. {
  425. Template = CreateTemplate(),
  426. Text = "0123"
  427. };
  428. target.SelectionStart = 1;
  429. target.SelectionEnd = 3;
  430. target.SelectedText = "";
  431. Assert.True(target.Text == "03");
  432. }
  433. }
  434. [Fact]
  435. public void SelectedText_NullClearsText()
  436. {
  437. using (UnitTestApplication.Start(Services))
  438. {
  439. var target = new TextBox
  440. {
  441. Template = CreateTemplate(),
  442. Text = "0123"
  443. };
  444. target.SelectionStart = 1;
  445. target.SelectionEnd = 3;
  446. target.SelectedText = null;
  447. Assert.True(target.Text == "03");
  448. }
  449. }
  450. [Fact]
  451. public void CoerceCaretIndex_Doesnt_Cause_Exception_with_malformed_line_ending()
  452. {
  453. using (UnitTestApplication.Start(Services))
  454. {
  455. var target = new TextBox
  456. {
  457. Template = CreateTemplate(),
  458. Text = "0123456789\r"
  459. };
  460. target.CaretIndex = 11;
  461. Assert.True(true);
  462. }
  463. }
  464. [Fact]
  465. public void TextBox_GotFocus_And_LostFocus_Work_Properly()
  466. {
  467. using (UnitTestApplication.Start(FocusServices))
  468. {
  469. var target1 = new TextBox
  470. {
  471. Template = CreateTemplate(),
  472. Text = "1234"
  473. };
  474. var target2 = new TextBox
  475. {
  476. Template = CreateTemplate(),
  477. Text = "5678"
  478. };
  479. var sp = new StackPanel();
  480. sp.Children.Add(target1);
  481. sp.Children.Add(target2);
  482. target1.ApplyTemplate();
  483. target2.ApplyTemplate();
  484. var root = new TestRoot { Child = sp };
  485. var gfcount = 0;
  486. var lfcount = 0;
  487. target1.GotFocus += (s, e) => gfcount++;
  488. target2.LostFocus += (s, e) => lfcount++;
  489. target2.Focus();
  490. Assert.False(target1.IsFocused);
  491. Assert.True(target2.IsFocused);
  492. target1.Focus();
  493. Assert.False(target2.IsFocused);
  494. Assert.True(target1.IsFocused);
  495. Assert.Equal(1, gfcount);
  496. Assert.Equal(1, lfcount);
  497. }
  498. }
  499. [Fact]
  500. public void TextBox_CaretIndex_Persists_When_Focus_Lost()
  501. {
  502. using (UnitTestApplication.Start(FocusServices))
  503. {
  504. var target1 = new TextBox
  505. {
  506. Template = CreateTemplate(),
  507. Text = "1234"
  508. };
  509. var target2 = new TextBox
  510. {
  511. Template = CreateTemplate(),
  512. Text = "5678"
  513. };
  514. var sp = new StackPanel();
  515. sp.Children.Add(target1);
  516. sp.Children.Add(target2);
  517. target1.ApplyTemplate();
  518. target2.ApplyTemplate();
  519. var root = new TestRoot { Child = sp };
  520. target2.Focus();
  521. target2.CaretIndex = 2;
  522. Assert.False(target1.IsFocused);
  523. Assert.True(target2.IsFocused);
  524. target1.Focus();
  525. Assert.Equal(2, target2.CaretIndex);
  526. }
  527. }
  528. [Fact]
  529. public void TextBox_Reveal_Password_Reset_When_Lost_Focus()
  530. {
  531. using (UnitTestApplication.Start(FocusServices))
  532. {
  533. var target1 = new TextBox
  534. {
  535. Template = CreateTemplate(),
  536. Text = "1234",
  537. PasswordChar = '*'
  538. };
  539. var target2 = new TextBox
  540. {
  541. Template = CreateTemplate(),
  542. Text = "5678"
  543. };
  544. var sp = new StackPanel();
  545. sp.Children.Add(target1);
  546. sp.Children.Add(target2);
  547. target1.ApplyTemplate();
  548. target2.ApplyTemplate();
  549. var root = new TestRoot { Child = sp };
  550. target1.Focus();
  551. target1.RevealPassword = true;
  552. target2.Focus();
  553. Assert.False(target1.RevealPassword);
  554. }
  555. }
  556. [Fact]
  557. public void Setting_Bound_Text_To_Null_Works()
  558. {
  559. using (UnitTestApplication.Start(Services))
  560. {
  561. var source = new Class1 { Bar = "bar" };
  562. var target = new TextBox { DataContext = source };
  563. target.Bind(TextBox.TextProperty, new Binding("Bar"));
  564. Assert.Equal("bar", target.Text);
  565. source.Bar = null;
  566. Assert.Null(target.Text);
  567. }
  568. }
  569. [Theory]
  570. [InlineData("abc", "d", 3, 0, 0, false, "abc")]
  571. [InlineData("abc", "dd", 4, 3, 3, false, "abcd")]
  572. [InlineData("abc", "ddd", 3, 0, 2, true, "ddc")]
  573. [InlineData("abc", "dddd", 4, 1, 3, true, "addd")]
  574. [InlineData("abc", "ddddd", 5, 3, 3, true, "abcdd")]
  575. public void MaxLength_Works_Properly(
  576. string initalText,
  577. string textInput,
  578. int maxLength,
  579. int selectionStart,
  580. int selectionEnd,
  581. bool fromClipboard,
  582. string expected)
  583. {
  584. using (UnitTestApplication.Start(Services))
  585. {
  586. var target = new TextBox
  587. {
  588. Template = CreateTemplate(),
  589. Text = initalText,
  590. MaxLength = maxLength,
  591. SelectionStart = selectionStart,
  592. SelectionEnd = selectionEnd
  593. };
  594. if (fromClipboard)
  595. {
  596. AvaloniaLocator.CurrentMutable.Bind<IClipboard>().ToSingleton<ClipboardStub>();
  597. var clipboard = AvaloniaLocator.CurrentMutable.GetService<IClipboard>();
  598. clipboard.SetTextAsync(textInput).GetAwaiter().GetResult();
  599. RaiseKeyEvent(target, Key.V, KeyModifiers.Control);
  600. clipboard.ClearAsync().GetAwaiter().GetResult();
  601. }
  602. else
  603. {
  604. RaiseTextEvent(target, textInput);
  605. }
  606. Assert.Equal(expected, target.Text);
  607. }
  608. }
  609. [Theory]
  610. [InlineData(Key.X, KeyModifiers.Control)]
  611. [InlineData(Key.Back, KeyModifiers.None)]
  612. [InlineData(Key.Delete, KeyModifiers.None)]
  613. [InlineData(Key.Tab, KeyModifiers.None)]
  614. [InlineData(Key.Enter, KeyModifiers.None)]
  615. public void Keys_Allow_Undo(Key key, KeyModifiers modifiers)
  616. {
  617. using (UnitTestApplication.Start(Services))
  618. {
  619. var target = new TextBox
  620. {
  621. Template = CreateTemplate(),
  622. Text = "0123",
  623. AcceptsReturn = true,
  624. AcceptsTab = true
  625. };
  626. target.SelectionStart = 1;
  627. target.SelectionEnd = 3;
  628. AvaloniaLocator.CurrentMutable
  629. .Bind<Input.Platform.IClipboard>().ToSingleton<ClipboardStub>();
  630. RaiseKeyEvent(target, key, modifiers);
  631. RaiseKeyEvent(target, Key.Z, KeyModifiers.Control); // undo
  632. Assert.True(target.Text == "0123");
  633. }
  634. }
  635. private static TestServices FocusServices => TestServices.MockThreadingInterface.With(
  636. focusManager: new FocusManager(),
  637. keyboardDevice: () => new KeyboardDevice(),
  638. keyboardNavigation: new KeyboardNavigationHandler(),
  639. inputManager: new InputManager(),
  640. standardCursorFactory: Mock.Of<ICursorFactory>());
  641. private static TestServices Services => TestServices.MockThreadingInterface.With(
  642. standardCursorFactory: Mock.Of<ICursorFactory>());
  643. private IControlTemplate CreateTemplate()
  644. {
  645. return new FuncControlTemplate<TextBox>((control, scope) =>
  646. new TextPresenter
  647. {
  648. Name = "PART_TextPresenter",
  649. [!!TextPresenter.TextProperty] = new Binding
  650. {
  651. Path = "Text",
  652. Mode = BindingMode.TwoWay,
  653. Priority = BindingPriority.TemplatedParent,
  654. RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),
  655. },
  656. }.RegisterInNameScope(scope));
  657. }
  658. private void RaiseKeyEvent(TextBox textBox, Key key, KeyModifiers inputModifiers)
  659. {
  660. textBox.RaiseEvent(new KeyEventArgs
  661. {
  662. RoutedEvent = InputElement.KeyDownEvent,
  663. KeyModifiers = inputModifiers,
  664. Key = key
  665. });
  666. }
  667. private void RaiseTextEvent(TextBox textBox, string text)
  668. {
  669. textBox.RaiseEvent(new TextInputEventArgs
  670. {
  671. RoutedEvent = InputElement.TextInputEvent,
  672. Text = text
  673. });
  674. }
  675. private class Class1 : NotifyingBase
  676. {
  677. private int _foo;
  678. private string _bar;
  679. public int Foo
  680. {
  681. get { return _foo; }
  682. set { _foo = value; RaisePropertyChanged(); }
  683. }
  684. public string Bar
  685. {
  686. get { return _bar; }
  687. set { _bar = value; RaisePropertyChanged(); }
  688. }
  689. }
  690. private class ClipboardStub : IClipboard // in order to get tests working that use the clipboard
  691. {
  692. private string _text;
  693. public Task<string> GetTextAsync() => Task.FromResult(_text);
  694. public Task SetTextAsync(string text)
  695. {
  696. _text = text;
  697. return Task.CompletedTask;
  698. }
  699. public Task ClearAsync()
  700. {
  701. _text = null;
  702. return Task.CompletedTask;
  703. }
  704. public Task SetDataObjectAsync(IDataObject data) => Task.CompletedTask;
  705. public Task<string[]> GetFormatsAsync() => Task.FromResult(Array.Empty<string>());
  706. public Task<object> GetDataAsync(string format) => Task.FromResult((object)null);
  707. }
  708. private class TestContextMenu : ContextMenu
  709. {
  710. public TestContextMenu()
  711. {
  712. IsOpen = true;
  713. }
  714. }
  715. }
  716. }