HasFocusChangeEventTests.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewTests;
  3. public class HasFocusChangeEventTests (ITestOutputHelper _output) : TestsAllViews
  4. {
  5. [Fact]
  6. public void HasFocusChanging_SetFocus_Raises ()
  7. {
  8. var hasFocusTrueCount = 0;
  9. var hasFocusFalseCount = 0;
  10. var view = new View
  11. {
  12. Id = "view",
  13. CanFocus = true
  14. };
  15. view.HasFocusChanging += (s, e) =>
  16. {
  17. if (e.NewValue)
  18. {
  19. hasFocusTrueCount++;
  20. }
  21. else
  22. {
  23. hasFocusFalseCount++;
  24. }
  25. };
  26. Assert.True (view.CanFocus);
  27. Assert.False (view.HasFocus);
  28. view.SetFocus ();
  29. Assert.True (view.HasFocus);
  30. Assert.Equal (1, hasFocusTrueCount);
  31. Assert.Equal (0, hasFocusFalseCount);
  32. }
  33. [Fact]
  34. public void HasFocusChanging_SetFocus_SubView_SetFocus_Raises ()
  35. {
  36. var hasFocusTrueCount = 0;
  37. var hasFocusFalseCount = 0;
  38. var view = new View
  39. {
  40. Id = "view",
  41. CanFocus = true
  42. };
  43. view.HasFocusChanging += (s, e) =>
  44. {
  45. if (e.NewValue)
  46. {
  47. hasFocusTrueCount++;
  48. }
  49. else
  50. {
  51. hasFocusFalseCount++;
  52. }
  53. };
  54. var subviewHasFocusTrueCount = 0;
  55. var subviewHasFocusFalseCount = 0;
  56. var subview = new View
  57. {
  58. Id = "subview",
  59. CanFocus = true
  60. };
  61. subview.HasFocusChanging += (s, e) =>
  62. {
  63. if (e.NewValue)
  64. {
  65. subviewHasFocusTrueCount++;
  66. }
  67. else
  68. {
  69. subviewHasFocusFalseCount++;
  70. }
  71. };
  72. view.Add (subview);
  73. view.SetFocus ();
  74. Assert.Equal (1, hasFocusTrueCount);
  75. Assert.Equal (0, hasFocusFalseCount);
  76. Assert.Equal (1, subviewHasFocusTrueCount);
  77. Assert.Equal (0, subviewHasFocusFalseCount);
  78. }
  79. [Fact]
  80. public void HasFocusChanging_SetFocus_On_SubView_SubView_SetFocus_Raises ()
  81. {
  82. var hasFocusTrueCount = 0;
  83. var hasFocusFalseCount = 0;
  84. var view = new View
  85. {
  86. Id = "view",
  87. CanFocus = true
  88. };
  89. view.HasFocusChanging += (s, e) =>
  90. {
  91. if (e.NewValue)
  92. {
  93. hasFocusTrueCount++;
  94. }
  95. else
  96. {
  97. hasFocusFalseCount++;
  98. }
  99. };
  100. var subviewHasFocusTrueCount = 0;
  101. var subviewHasFocusFalseCount = 0;
  102. var subview = new View
  103. {
  104. Id = "subview",
  105. CanFocus = true
  106. };
  107. subview.HasFocusChanging += (s, e) =>
  108. {
  109. if (e.NewValue)
  110. {
  111. subviewHasFocusTrueCount++;
  112. }
  113. else
  114. {
  115. subviewHasFocusFalseCount++;
  116. }
  117. };
  118. view.Add (subview);
  119. subview.SetFocus ();
  120. Assert.Equal (1, hasFocusTrueCount);
  121. Assert.Equal (0, hasFocusFalseCount);
  122. Assert.Equal (1, subviewHasFocusTrueCount);
  123. Assert.Equal (0, subviewHasFocusFalseCount);
  124. }
  125. [Fact]
  126. public void HasFocusChanging_SetFocus_CompoundSubView_Raises ()
  127. {
  128. var hasFocusTrueCount = 0;
  129. var hasFocusFalseCount = 0;
  130. var view = new View
  131. {
  132. Id = "view",
  133. CanFocus = true
  134. };
  135. view.HasFocusChanging += (s, e) =>
  136. {
  137. if (e.NewValue)
  138. {
  139. hasFocusTrueCount++;
  140. }
  141. else
  142. {
  143. hasFocusFalseCount++;
  144. }
  145. };
  146. var subViewEnterCount = 0;
  147. var subViewLeaveCount = 0;
  148. var subView = new View
  149. {
  150. Id = "subView",
  151. CanFocus = true
  152. };
  153. subView.HasFocusChanging += (s, e) =>
  154. {
  155. if (e.NewValue)
  156. {
  157. subViewEnterCount++;
  158. }
  159. else
  160. {
  161. subViewLeaveCount++;
  162. }
  163. };
  164. var subviewSubView1EnterCount = 0;
  165. var subviewSubView1LeaveCount = 0;
  166. var subViewSubView1 = new View
  167. {
  168. Id = "subViewSubView1",
  169. CanFocus = false
  170. };
  171. subViewSubView1.HasFocusChanging += (s, e) =>
  172. {
  173. if (e.NewValue)
  174. {
  175. subviewSubView1EnterCount++;
  176. }
  177. else
  178. {
  179. subviewSubView1LeaveCount++;
  180. }
  181. };
  182. var subviewSubView2EnterCount = 0;
  183. var subviewSubView2LeaveCount = 0;
  184. var subViewSubView2 = new View
  185. {
  186. Id = "subViewSubView2",
  187. CanFocus = true
  188. };
  189. subViewSubView2.HasFocusChanging += (s, e) =>
  190. {
  191. if (e.NewValue)
  192. {
  193. subviewSubView2EnterCount++;
  194. }
  195. else
  196. {
  197. subviewSubView2EnterCount++;
  198. }
  199. };
  200. var subviewSubView3EnterCount = 0;
  201. var subviewSubView3LeaveCount = 0;
  202. var subViewSubView3 = new View
  203. {
  204. Id = "subViewSubView3",
  205. CanFocus = false
  206. };
  207. subViewSubView3.HasFocusChanging += (s, e) =>
  208. {
  209. if (e.NewValue)
  210. {
  211. subviewSubView3EnterCount++;
  212. }
  213. else
  214. {
  215. subviewSubView3LeaveCount++;
  216. }
  217. };
  218. subView.Add (subViewSubView1, subViewSubView2, subViewSubView3);
  219. view.Add (subView);
  220. view.SetFocus ();
  221. Assert.True (view.HasFocus);
  222. Assert.True (subView.HasFocus);
  223. Assert.False (subViewSubView1.HasFocus);
  224. Assert.True (subViewSubView2.HasFocus);
  225. Assert.False (subViewSubView3.HasFocus);
  226. Assert.Equal (1, hasFocusTrueCount);
  227. Assert.Equal (0, hasFocusFalseCount);
  228. Assert.Equal (1, subViewEnterCount);
  229. Assert.Equal (0, subViewLeaveCount);
  230. Assert.Equal (0, subviewSubView1EnterCount);
  231. Assert.Equal (0, subviewSubView1LeaveCount);
  232. Assert.Equal (1, subviewSubView2EnterCount);
  233. Assert.Equal (0, subviewSubView2LeaveCount);
  234. Assert.Equal (0, subviewSubView3EnterCount);
  235. Assert.Equal (0, subviewSubView3LeaveCount);
  236. }
  237. [Fact]
  238. public void HasFocusChanging_Can_Cancel ()
  239. {
  240. var hasFocusTrueCount = 0;
  241. var hasFocusFalseCount = 0;
  242. var view = new View
  243. {
  244. Id = "view",
  245. CanFocus = true
  246. };
  247. view.HasFocusChanging += (s, e) =>
  248. {
  249. if (e.NewValue)
  250. {
  251. hasFocusTrueCount++;
  252. e.Cancel = true;
  253. }
  254. else
  255. {
  256. hasFocusFalseCount++;
  257. }
  258. };
  259. var subviewHasFocusTrueCount = 0;
  260. var subviewHasFocusFalseCount = 0;
  261. var subview = new View
  262. {
  263. Id = "subview",
  264. CanFocus = true
  265. };
  266. subview.HasFocusChanging += (s, e) =>
  267. {
  268. if (e.NewValue)
  269. {
  270. subviewHasFocusTrueCount++;
  271. }
  272. else
  273. {
  274. subviewHasFocusFalseCount++;
  275. }
  276. };
  277. view.Add (subview);
  278. view.SetFocus ();
  279. Assert.False (view.HasFocus);
  280. Assert.False (subview.HasFocus);
  281. Assert.Equal (1, hasFocusTrueCount);
  282. Assert.Equal (0, hasFocusFalseCount);
  283. Assert.Equal (0, subviewHasFocusTrueCount);
  284. Assert.Equal (0, subviewHasFocusFalseCount);
  285. }
  286. [Fact]
  287. public void HasFocusChanging_SetFocus_On_SubView_Can_Cancel ()
  288. {
  289. var hasFocusTrueCount = 0;
  290. var hasFocusFalseCount = 0;
  291. var view = new View
  292. {
  293. Id = "view",
  294. CanFocus = true
  295. };
  296. view.HasFocusChanging += (s, e) =>
  297. {
  298. if (e.NewValue)
  299. {
  300. hasFocusTrueCount++;
  301. e.Cancel = true;
  302. }
  303. else
  304. {
  305. hasFocusFalseCount++;
  306. }
  307. };
  308. var subviewHasFocusTrueCount = 0;
  309. var subviewHasFocusFalseCount = 0;
  310. var subview = new View
  311. {
  312. Id = "subview",
  313. CanFocus = true
  314. };
  315. subview.HasFocusChanging += (s, e) =>
  316. {
  317. if (e.NewValue)
  318. {
  319. subviewHasFocusTrueCount++;
  320. }
  321. else
  322. {
  323. subviewHasFocusFalseCount++;
  324. }
  325. };
  326. view.Add (subview);
  327. subview.SetFocus ();
  328. Assert.False (view.HasFocus);
  329. Assert.False (subview.HasFocus);
  330. Assert.Equal (1, hasFocusTrueCount);
  331. Assert.Equal (0, hasFocusFalseCount);
  332. Assert.Equal (1, subviewHasFocusTrueCount);
  333. Assert.Equal (0, subviewHasFocusFalseCount);
  334. }
  335. [Fact]
  336. public void HasFocusChanging_SubView_Can_Cancel ()
  337. {
  338. var hasFocusTrueCount = 0;
  339. var hasFocusFalseCount = 0;
  340. var view = new View
  341. {
  342. Id = "view",
  343. CanFocus = true
  344. };
  345. view.HasFocusChanging += (s, e) =>
  346. {
  347. if (e.NewValue)
  348. {
  349. hasFocusTrueCount++;
  350. }
  351. else
  352. {
  353. hasFocusFalseCount++;
  354. }
  355. };
  356. var subviewHasFocusTrueCount = 0;
  357. var subviewHasFocusFalseCount = 0;
  358. var subview = new View
  359. {
  360. Id = "subview",
  361. CanFocus = true
  362. };
  363. subview.HasFocusChanging += (s, e) =>
  364. {
  365. if (e.NewValue)
  366. {
  367. subviewHasFocusTrueCount++;
  368. e.Cancel = true;
  369. }
  370. else
  371. {
  372. subviewHasFocusFalseCount++;
  373. }
  374. };
  375. view.Add (subview);
  376. view.SetFocus ();
  377. Assert.True (view.HasFocus);
  378. Assert.False (subview.HasFocus);
  379. Assert.Equal (1, hasFocusTrueCount);
  380. Assert.Equal (0, hasFocusFalseCount);
  381. Assert.Equal (1, subviewHasFocusTrueCount);
  382. Assert.Equal (0, subviewHasFocusFalseCount);
  383. }
  384. [Fact]
  385. public void HasFocusChanging_SetFocus_On_Subview_SubView_Can_Cancel ()
  386. {
  387. var hasFocusTrueCount = 0;
  388. var hasFocusFalseCount = 0;
  389. var view = new View
  390. {
  391. Id = "view",
  392. CanFocus = true
  393. };
  394. view.HasFocusChanging += (s, e) =>
  395. {
  396. if (e.NewValue)
  397. {
  398. hasFocusTrueCount++;
  399. }
  400. else
  401. {
  402. hasFocusFalseCount++;
  403. }
  404. };
  405. var subviewHasFocusTrueCount = 0;
  406. var subviewHasFocusFalseCount = 0;
  407. var subview = new View
  408. {
  409. Id = "subview",
  410. CanFocus = true
  411. };
  412. subview.HasFocusChanging += (s, e) =>
  413. {
  414. if (e.NewValue)
  415. {
  416. subviewHasFocusTrueCount++;
  417. e.Cancel = true;
  418. }
  419. else
  420. {
  421. subviewHasFocusFalseCount++;
  422. }
  423. };
  424. view.Add (subview);
  425. subview.SetFocus ();
  426. Assert.False (view.HasFocus);
  427. Assert.False (subview.HasFocus);
  428. Assert.Equal (0, hasFocusTrueCount);
  429. Assert.Equal (0, hasFocusFalseCount);
  430. Assert.Equal (1, subviewHasFocusTrueCount);
  431. Assert.Equal (0, subviewHasFocusFalseCount);
  432. }
  433. [Fact]
  434. public void RemoveFocus_Raises_HasFocusChanged ()
  435. {
  436. var nEnter = 0;
  437. var nLeave = 0;
  438. var view = new View
  439. {
  440. Id = "view",
  441. CanFocus = true
  442. };
  443. view.HasFocusChanging += (s, e) => nEnter++;
  444. view.HasFocusChanged += (s, e) => nLeave++;
  445. Assert.True (view.CanFocus);
  446. Assert.False (view.HasFocus);
  447. view.SetFocus ();
  448. Assert.True (view.HasFocus);
  449. Assert.Equal (1, nEnter);
  450. Assert.Equal (0, nLeave);
  451. view.HasFocus = false;
  452. Assert.Equal (1, nEnter);
  453. Assert.Equal (1, nLeave);
  454. }
  455. [Fact]
  456. public void RemoveFocus_SubView_Raises_HasFocusChanged ()
  457. {
  458. var viewEnterCount = 0;
  459. var viewLeaveCount = 0;
  460. var view = new View
  461. {
  462. Id = "view",
  463. CanFocus = true
  464. };
  465. view.HasFocusChanging += (s, e) => viewEnterCount++;
  466. view.HasFocusChanged += (s, e) => viewLeaveCount++;
  467. var subviewEnterCount = 0;
  468. var subviewLeaveCount = 0;
  469. var subview = new View
  470. {
  471. Id = "subview",
  472. CanFocus = true
  473. };
  474. subview.HasFocusChanging += (s, e) => subviewEnterCount++;
  475. subview.HasFocusChanged += (s, e) => subviewLeaveCount++;
  476. view.Add (subview);
  477. view.SetFocus ();
  478. view.HasFocus = false;
  479. Assert.Equal (1, viewEnterCount);
  480. Assert.Equal (1, viewLeaveCount);
  481. Assert.Equal (1, subviewEnterCount);
  482. Assert.Equal (1, subviewLeaveCount);
  483. view.SetFocus ();
  484. Assert.Equal (2, viewEnterCount);
  485. Assert.Equal (1, viewLeaveCount);
  486. Assert.Equal (2, subviewEnterCount);
  487. Assert.Equal (1, subviewLeaveCount);
  488. subview.HasFocus = false;
  489. Assert.Equal (2, viewEnterCount);
  490. Assert.Equal (2, viewLeaveCount);
  491. Assert.Equal (2, subviewEnterCount);
  492. Assert.Equal (2, subviewLeaveCount);
  493. }
  494. [Fact]
  495. public void RemoveFocus_CompoundSubView_Raises_HasFocusChanged ()
  496. {
  497. var viewEnterCount = 0;
  498. var viewLeaveCount = 0;
  499. var view = new View
  500. {
  501. Id = "view",
  502. CanFocus = true
  503. };
  504. view.HasFocusChanging += (s, e) => viewEnterCount++;
  505. view.HasFocusChanged += (s, e) => viewLeaveCount++;
  506. var subViewEnterCount = 0;
  507. var subViewLeaveCount = 0;
  508. var subView = new View
  509. {
  510. Id = "subView",
  511. CanFocus = true
  512. };
  513. subView.HasFocusChanging += (s, e) => subViewEnterCount++;
  514. subView.HasFocusChanged += (s, e) => subViewLeaveCount++;
  515. var subviewSubView1EnterCount = 0;
  516. var subviewSubView1LeaveCount = 0;
  517. var subViewSubView1 = new View
  518. {
  519. Id = "subViewSubView1",
  520. CanFocus = false
  521. };
  522. subViewSubView1.HasFocusChanging += (s, e) => subviewSubView1EnterCount++;
  523. subViewSubView1.HasFocusChanged += (s, e) => subviewSubView1LeaveCount++;
  524. var subviewSubView2EnterCount = 0;
  525. var subviewSubView2LeaveCount = 0;
  526. var subViewSubView2 = new View
  527. {
  528. Id = "subViewSubView2",
  529. CanFocus = true
  530. };
  531. subViewSubView2.HasFocusChanging += (s, e) => subviewSubView2EnterCount++;
  532. subViewSubView2.HasFocusChanged += (s, e) => subviewSubView2LeaveCount++;
  533. var subviewSubView3EnterCount = 0;
  534. var subviewSubView3LeaveCount = 0;
  535. var subViewSubView3 = new View
  536. {
  537. Id = "subViewSubView3",
  538. CanFocus = false
  539. };
  540. subViewSubView3.HasFocusChanging += (s, e) => subviewSubView3EnterCount++;
  541. subViewSubView3.HasFocusChanged += (s, e) => subviewSubView3LeaveCount++;
  542. subView.Add (subViewSubView1, subViewSubView2, subViewSubView3);
  543. view.Add (subView);
  544. view.SetFocus ();
  545. Assert.True (view.HasFocus);
  546. Assert.True (subView.HasFocus);
  547. Assert.False (subViewSubView1.HasFocus);
  548. Assert.True (subViewSubView2.HasFocus);
  549. Assert.False (subViewSubView3.HasFocus);
  550. view.HasFocus = false;
  551. Assert.False (view.HasFocus);
  552. Assert.False (subView.HasFocus);
  553. Assert.False (subViewSubView1.HasFocus);
  554. Assert.False (subViewSubView2.HasFocus);
  555. Assert.False (subViewSubView3.HasFocus);
  556. Assert.Equal (1, viewEnterCount);
  557. Assert.Equal (1, viewLeaveCount);
  558. Assert.Equal (1, subViewEnterCount);
  559. Assert.Equal (1, subViewLeaveCount);
  560. Assert.Equal (0, subviewSubView1EnterCount);
  561. Assert.Equal (0, subviewSubView1LeaveCount);
  562. Assert.Equal (1, subviewSubView2EnterCount);
  563. Assert.Equal (1, subviewSubView2LeaveCount);
  564. Assert.Equal (0, subviewSubView3EnterCount);
  565. Assert.Equal (0, subviewSubView3LeaveCount);
  566. }
  567. [Fact]
  568. public void HasFocus_False_Leave_Raised ()
  569. {
  570. var view = new View
  571. {
  572. Id = "view",
  573. CanFocus = true
  574. };
  575. Assert.True (view.CanFocus);
  576. Assert.False (view.HasFocus);
  577. var leaveInvoked = 0;
  578. view.HasFocusChanged += (s, e) => leaveInvoked++;
  579. view.SetFocus ();
  580. Assert.True (view.HasFocus);
  581. Assert.Equal (0, leaveInvoked);
  582. view.HasFocus = false;
  583. Assert.False (view.HasFocus);
  584. Assert.Equal (1, leaveInvoked);
  585. }
  586. [Fact]
  587. public void HasFocus_False_Leave_Raised_ForAllSubViews ()
  588. {
  589. var view = new View
  590. {
  591. Id = "view",
  592. CanFocus = true
  593. };
  594. var subview = new View
  595. {
  596. Id = "subview",
  597. CanFocus = true
  598. };
  599. view.Add (subview);
  600. var leaveInvoked = 0;
  601. view.HasFocusChanged += (s, e) => leaveInvoked++;
  602. subview.HasFocusChanged += (s, e) => leaveInvoked++;
  603. view.SetFocus ();
  604. Assert.True (view.HasFocus);
  605. Assert.Equal (0, leaveInvoked);
  606. view.HasFocus = false;
  607. Assert.False (view.HasFocus);
  608. Assert.False (subview.HasFocus);
  609. Assert.Equal (2, leaveInvoked);
  610. }
  611. }