NavigationTests.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  1. using System;
  2. using Xunit;
  3. using Xunit.Abstractions;
  4. using System.Text;
  5. // Alias Console to MockConsole so we don't accidentally use Console
  6. using Console = Terminal.Gui.FakeConsole;
  7. namespace Terminal.Gui.ViewTests {
  8. public class NavigationTests {
  9. readonly ITestOutputHelper output;
  10. public NavigationTests (ITestOutputHelper output)
  11. {
  12. this.output = output;
  13. }
  14. [Fact]
  15. public void FocusNearestView_Ensure_Focus_Ordered ()
  16. {
  17. var top = new Toplevel ();
  18. var win = new Window ();
  19. var winSubview = new View ("WindowSubview") {
  20. CanFocus = true
  21. };
  22. win.Add (winSubview);
  23. top.Add (win);
  24. var frm = new FrameView ();
  25. var frmSubview = new View ("FrameSubview") {
  26. CanFocus = true
  27. };
  28. frm.Add (frmSubview);
  29. top.Add (frm);
  30. top.NewKeyDownEvent (new (KeyCode.Tab));
  31. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  32. top.NewKeyDownEvent (new (KeyCode.Tab));
  33. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  34. top.NewKeyDownEvent (new (KeyCode.Tab));
  35. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  36. top.NewKeyDownEvent (new (KeyCode.Tab | KeyCode.ShiftMask));
  37. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  38. top.NewKeyDownEvent (new (KeyCode.Tab | KeyCode.ShiftMask));
  39. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  40. top.Dispose ();
  41. }
  42. [Fact]
  43. public void Subviews_TabIndexes_AreEqual ()
  44. {
  45. var r = new View ();
  46. var v1 = new View () { CanFocus = true };
  47. var v2 = new View () { CanFocus = true };
  48. var v3 = new View () { CanFocus = true };
  49. r.Add (v1, v2, v3);
  50. Assert.True (r.Subviews.IndexOf (v1) == 0);
  51. Assert.True (r.Subviews.IndexOf (v2) == 1);
  52. Assert.True (r.Subviews.IndexOf (v3) == 2);
  53. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  54. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  55. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  56. Assert.Equal (r.Subviews.IndexOf (v1), r.TabIndexes.IndexOf (v1));
  57. Assert.Equal (r.Subviews.IndexOf (v2), r.TabIndexes.IndexOf (v2));
  58. Assert.Equal (r.Subviews.IndexOf (v3), r.TabIndexes.IndexOf (v3));
  59. r.Dispose ();
  60. }
  61. [Fact]
  62. public void BringSubviewToFront_Subviews_vs_TabIndexes ()
  63. {
  64. var r = new View ();
  65. var v1 = new View () { CanFocus = true };
  66. var v2 = new View () { CanFocus = true };
  67. var v3 = new View () { CanFocus = true };
  68. r.Add (v1, v2, v3);
  69. r.BringSubviewToFront (v1);
  70. Assert.True (r.Subviews.IndexOf (v1) == 2);
  71. Assert.True (r.Subviews.IndexOf (v2) == 0);
  72. Assert.True (r.Subviews.IndexOf (v3) == 1);
  73. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  74. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  75. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  76. r.Dispose ();
  77. }
  78. [Fact]
  79. public void BringSubviewForward_Subviews_vs_TabIndexes ()
  80. {
  81. var r = new View ();
  82. var v1 = new View () { CanFocus = true };
  83. var v2 = new View () { CanFocus = true };
  84. var v3 = new View () { CanFocus = true };
  85. r.Add (v1, v2, v3);
  86. r.BringSubviewForward (v1);
  87. Assert.True (r.Subviews.IndexOf (v1) == 1);
  88. Assert.True (r.Subviews.IndexOf (v2) == 0);
  89. Assert.True (r.Subviews.IndexOf (v3) == 2);
  90. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  91. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  92. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  93. r.Dispose ();
  94. }
  95. [Fact]
  96. public void SendSubviewToBack_Subviews_vs_TabIndexes ()
  97. {
  98. var r = new View ();
  99. var v1 = new View () { CanFocus = true };
  100. var v2 = new View () { CanFocus = true };
  101. var v3 = new View () { CanFocus = true };
  102. r.Add (v1, v2, v3);
  103. r.SendSubviewToBack (v3);
  104. Assert.True (r.Subviews.IndexOf (v1) == 1);
  105. Assert.True (r.Subviews.IndexOf (v2) == 2);
  106. Assert.True (r.Subviews.IndexOf (v3) == 0);
  107. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  108. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  109. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  110. r.Dispose ();
  111. }
  112. [Fact]
  113. public void SendSubviewBackwards_Subviews_vs_TabIndexes ()
  114. {
  115. var r = new View ();
  116. var v1 = new View () { CanFocus = true };
  117. var v2 = new View () { CanFocus = true };
  118. var v3 = new View () { CanFocus = true };
  119. r.Add (v1, v2, v3);
  120. r.SendSubviewBackwards (v3);
  121. Assert.True (r.Subviews.IndexOf (v1) == 0);
  122. Assert.True (r.Subviews.IndexOf (v2) == 2);
  123. Assert.True (r.Subviews.IndexOf (v3) == 1);
  124. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  125. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  126. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  127. r.Dispose ();
  128. }
  129. [Fact]
  130. public void TabIndex_Set_CanFocus_ValidValues ()
  131. {
  132. var r = new View ();
  133. var v1 = new View () { CanFocus = true };
  134. var v2 = new View () { CanFocus = true };
  135. var v3 = new View () { CanFocus = true };
  136. r.Add (v1, v2, v3);
  137. v1.TabIndex = 1;
  138. Assert.True (r.Subviews.IndexOf (v1) == 0);
  139. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  140. v1.TabIndex = 2;
  141. Assert.True (r.Subviews.IndexOf (v1) == 0);
  142. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  143. r.Dispose ();
  144. }
  145. [Fact]
  146. public void TabIndex_Set_CanFocus_HigherValues ()
  147. {
  148. var r = new View ();
  149. var v1 = new View () { CanFocus = true };
  150. var v2 = new View () { CanFocus = true };
  151. var v3 = new View () { CanFocus = true };
  152. r.Add (v1, v2, v3);
  153. v1.TabIndex = 3;
  154. Assert.True (r.Subviews.IndexOf (v1) == 0);
  155. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  156. r.Dispose ();
  157. }
  158. [Fact]
  159. public void TabIndex_Set_CanFocus_LowerValues ()
  160. {
  161. var r = new View ();
  162. var v1 = new View () { CanFocus = true };
  163. var v2 = new View () { CanFocus = true };
  164. var v3 = new View () { CanFocus = true };
  165. r.Add (v1, v2, v3);
  166. v1.TabIndex = -1;
  167. Assert.True (r.Subviews.IndexOf (v1) == 0);
  168. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  169. r.Dispose ();
  170. }
  171. [Fact]
  172. public void TabIndex_Set_CanFocus_False ()
  173. {
  174. var r = new View ();
  175. var v1 = new View () { CanFocus = true };
  176. var v2 = new View () { CanFocus = true };
  177. var v3 = new View () { CanFocus = true };
  178. r.Add (v1, v2, v3);
  179. v1.CanFocus = false;
  180. v1.TabIndex = 0;
  181. Assert.True (r.Subviews.IndexOf (v1) == 0);
  182. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  183. Assert.Equal (-1, v1.TabIndex);
  184. r.Dispose ();
  185. }
  186. [Fact]
  187. public void TabIndex_Set_CanFocus_False_To_True ()
  188. {
  189. var r = new View ();
  190. var v1 = new View ();
  191. var v2 = new View () { CanFocus = true };
  192. var v3 = new View () { CanFocus = true };
  193. r.Add (v1, v2, v3);
  194. v1.CanFocus = true;
  195. v1.TabIndex = 1;
  196. Assert.True (r.Subviews.IndexOf (v1) == 0);
  197. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  198. r.Dispose ();
  199. }
  200. [Fact]
  201. public void TabStop_And_CanFocus_Are_All_True ()
  202. {
  203. var r = new View ();
  204. var v1 = new View () { CanFocus = true };
  205. var v2 = new View () { CanFocus = true };
  206. var v3 = new View () { CanFocus = true };
  207. r.Add (v1, v2, v3);
  208. r.FocusNext ();
  209. Assert.True (v1.HasFocus);
  210. Assert.False (v2.HasFocus);
  211. Assert.False (v3.HasFocus);
  212. r.FocusNext ();
  213. Assert.False (v1.HasFocus);
  214. Assert.True (v2.HasFocus);
  215. Assert.False (v3.HasFocus);
  216. r.FocusNext ();
  217. Assert.False (v1.HasFocus);
  218. Assert.False (v2.HasFocus);
  219. Assert.True (v3.HasFocus);
  220. r.Dispose ();
  221. }
  222. [Fact]
  223. public void TabStop_Are_All_True_And_CanFocus_Are_All_False ()
  224. {
  225. var r = new View ();
  226. var v1 = new View ();
  227. var v2 = new View ();
  228. var v3 = new View ();
  229. r.Add (v1, v2, v3);
  230. r.FocusNext ();
  231. Assert.False (v1.HasFocus);
  232. Assert.False (v2.HasFocus);
  233. Assert.False (v3.HasFocus);
  234. r.FocusNext ();
  235. Assert.False (v1.HasFocus);
  236. Assert.False (v2.HasFocus);
  237. Assert.False (v3.HasFocus);
  238. r.FocusNext ();
  239. Assert.False (v1.HasFocus);
  240. Assert.False (v2.HasFocus);
  241. Assert.False (v3.HasFocus);
  242. r.Dispose ();
  243. }
  244. [Fact]
  245. public void TabStop_Are_All_False_And_CanFocus_Are_All_True ()
  246. {
  247. var r = new View ();
  248. var v1 = new View () { CanFocus = true, TabStop = false };
  249. var v2 = new View () { CanFocus = true, TabStop = false };
  250. var v3 = new View () { CanFocus = true, TabStop = false };
  251. r.Add (v1, v2, v3);
  252. r.FocusNext ();
  253. Assert.False (v1.HasFocus);
  254. Assert.False (v2.HasFocus);
  255. Assert.False (v3.HasFocus);
  256. r.FocusNext ();
  257. Assert.False (v1.HasFocus);
  258. Assert.False (v2.HasFocus);
  259. Assert.False (v3.HasFocus);
  260. r.FocusNext ();
  261. Assert.False (v1.HasFocus);
  262. Assert.False (v2.HasFocus);
  263. Assert.False (v3.HasFocus);
  264. r.Dispose ();
  265. }
  266. [Fact]
  267. public void TabStop_And_CanFocus_Mixed_And_BothFalse ()
  268. {
  269. var r = new View ();
  270. var v1 = new View () { CanFocus = true, TabStop = false };
  271. var v2 = new View () { CanFocus = false, TabStop = true };
  272. var v3 = new View () { CanFocus = false, TabStop = false };
  273. r.Add (v1, v2, v3);
  274. r.FocusNext ();
  275. Assert.False (v1.HasFocus);
  276. Assert.False (v2.HasFocus);
  277. Assert.False (v3.HasFocus);
  278. r.FocusNext ();
  279. Assert.False (v1.HasFocus);
  280. Assert.False (v2.HasFocus);
  281. Assert.False (v3.HasFocus);
  282. r.FocusNext ();
  283. Assert.False (v1.HasFocus);
  284. Assert.False (v2.HasFocus);
  285. Assert.False (v3.HasFocus);
  286. r.Dispose ();
  287. }
  288. [Fact]
  289. public void TabStop_All_True_And_Changing_CanFocus_Later ()
  290. {
  291. var r = new View ();
  292. var v1 = new View ();
  293. var v2 = new View ();
  294. var v3 = new View ();
  295. r.Add (v1, v2, v3);
  296. r.FocusNext ();
  297. Assert.False (v1.HasFocus);
  298. Assert.False (v2.HasFocus);
  299. Assert.False (v3.HasFocus);
  300. v1.CanFocus = true;
  301. r.FocusNext ();
  302. Assert.True (v1.HasFocus);
  303. Assert.False (v2.HasFocus);
  304. Assert.False (v3.HasFocus);
  305. v2.CanFocus = true;
  306. r.FocusNext ();
  307. Assert.False (v1.HasFocus);
  308. Assert.True (v2.HasFocus);
  309. Assert.False (v3.HasFocus);
  310. v3.CanFocus = true;
  311. r.FocusNext ();
  312. Assert.False (v1.HasFocus);
  313. Assert.False (v2.HasFocus);
  314. Assert.True (v3.HasFocus);
  315. r.Dispose ();
  316. }
  317. [Fact]
  318. public void TabStop_All_False_And_All_True_And_Changing_TabStop_Later ()
  319. {
  320. var r = new View ();
  321. var v1 = new View () { CanFocus = true, TabStop = false };
  322. var v2 = new View () { CanFocus = true, TabStop = false };
  323. var v3 = new View () { CanFocus = true, TabStop = false };
  324. r.Add (v1, v2, v3);
  325. r.FocusNext ();
  326. Assert.False (v1.HasFocus);
  327. Assert.False (v2.HasFocus);
  328. Assert.False (v3.HasFocus);
  329. v1.TabStop = true;
  330. r.FocusNext ();
  331. Assert.True (v1.HasFocus);
  332. Assert.False (v2.HasFocus);
  333. Assert.False (v3.HasFocus);
  334. v2.TabStop = true;
  335. r.FocusNext ();
  336. Assert.False (v1.HasFocus);
  337. Assert.True (v2.HasFocus);
  338. Assert.False (v3.HasFocus);
  339. v3.TabStop = true;
  340. r.FocusNext ();
  341. Assert.False (v1.HasFocus);
  342. Assert.False (v2.HasFocus);
  343. Assert.True (v3.HasFocus);
  344. r.Dispose ();
  345. }
  346. [Fact]
  347. public void CanFocus_Set_Changes_TabIndex_And_TabStop ()
  348. {
  349. var r = new View ();
  350. var v1 = new View ("1");
  351. var v2 = new View ("2");
  352. var v3 = new View ("3");
  353. r.Add (v1, v2, v3);
  354. v2.CanFocus = true;
  355. Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  356. Assert.Equal (0, v2.TabIndex);
  357. Assert.True (v2.TabStop);
  358. v1.CanFocus = true;
  359. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  360. Assert.Equal (1, v1.TabIndex);
  361. Assert.True (v1.TabStop);
  362. v1.TabIndex = 2;
  363. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  364. Assert.Equal (1, v1.TabIndex);
  365. v3.CanFocus = true;
  366. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  367. Assert.Equal (1, v1.TabIndex);
  368. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  369. Assert.Equal (2, v3.TabIndex);
  370. Assert.True (v3.TabStop);
  371. v2.CanFocus = false;
  372. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  373. Assert.Equal (1, v1.TabIndex);
  374. Assert.True (v1.TabStop);
  375. Assert.NotEqual (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  376. Assert.Equal (-1, v2.TabIndex);
  377. Assert.False (v2.TabStop);
  378. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  379. Assert.Equal (2, v3.TabIndex);
  380. Assert.True (v3.TabStop);
  381. r.Dispose ();
  382. }
  383. [Fact]
  384. [AutoInitShutdown]
  385. public void CanFocus_Faced_With_Container ()
  386. {
  387. var t = new Toplevel ();
  388. var w = new Window ();
  389. var f = new FrameView ();
  390. var v = new View () { CanFocus = true };
  391. f.Add (v);
  392. w.Add (f);
  393. t.Add (w);
  394. Assert.True (t.CanFocus);
  395. Assert.True (w.CanFocus);
  396. Assert.True (f.CanFocus);
  397. Assert.True (v.CanFocus);
  398. f.CanFocus = false;
  399. Assert.False (f.CanFocus);
  400. Assert.True (v.CanFocus);
  401. v.CanFocus = false;
  402. Assert.False (f.CanFocus);
  403. Assert.False (v.CanFocus);
  404. v.CanFocus = true;
  405. Assert.False (f.CanFocus);
  406. Assert.True (v.CanFocus);
  407. t.Dispose ();
  408. }
  409. [Fact]
  410. public void CanFocus_Faced_With_Container_Before_Run ()
  411. {
  412. Application.Init (new FakeDriver ());
  413. var t = Application.Top;
  414. var w = new Window ();
  415. var f = new FrameView ();
  416. var v = new View () { CanFocus = true };
  417. f.Add (v);
  418. w.Add (f);
  419. t.Add (w);
  420. Assert.True (t.CanFocus);
  421. Assert.True (w.CanFocus);
  422. Assert.True (f.CanFocus);
  423. Assert.True (v.CanFocus);
  424. f.CanFocus = false;
  425. Assert.False (f.CanFocus);
  426. Assert.True (v.CanFocus);
  427. v.CanFocus = false;
  428. Assert.False (f.CanFocus);
  429. Assert.False (v.CanFocus);
  430. v.CanFocus = true;
  431. Assert.False (f.CanFocus);
  432. Assert.True (v.CanFocus);
  433. Application.Iteration += (s, a) => Application.RequestStop ();
  434. Application.Run ();
  435. Application.Shutdown ();
  436. }
  437. [Fact]
  438. public void CanFocus_Faced_With_Container_After_Run ()
  439. {
  440. Application.Init (new FakeDriver ());
  441. var t = Application.Top;
  442. var w = new Window ();
  443. var f = new FrameView ();
  444. var v = new View () { CanFocus = true };
  445. f.Add (v);
  446. w.Add (f);
  447. t.Add (w);
  448. t.Ready += (s, e) => {
  449. Assert.True (t.CanFocus);
  450. Assert.True (w.CanFocus);
  451. Assert.True (f.CanFocus);
  452. Assert.True (v.CanFocus);
  453. f.CanFocus = false;
  454. Assert.False (f.CanFocus);
  455. Assert.False (v.CanFocus);
  456. v.CanFocus = false;
  457. Assert.False (f.CanFocus);
  458. Assert.False (v.CanFocus);
  459. Assert.Throws<InvalidOperationException> (() => v.CanFocus = true);
  460. Assert.False (f.CanFocus);
  461. Assert.False (v.CanFocus);
  462. f.CanFocus = true;
  463. Assert.True (f.CanFocus);
  464. Assert.True (v.CanFocus);
  465. };
  466. Application.Iteration += (s, a) => Application.RequestStop ();
  467. Application.Run ();
  468. Application.Shutdown ();
  469. }
  470. [Fact]
  471. public void CanFocus_Container_ToFalse_Turns_All_Subviews_ToFalse_Too ()
  472. {
  473. Application.Init (new FakeDriver ());
  474. var t = Application.Top;
  475. var w = new Window ();
  476. var f = new FrameView ();
  477. var v1 = new View () { CanFocus = true };
  478. var v2 = new View () { CanFocus = true };
  479. f.Add (v1, v2);
  480. w.Add (f);
  481. t.Add (w);
  482. t.Ready += (s, e) => {
  483. Assert.True (t.CanFocus);
  484. Assert.True (w.CanFocus);
  485. Assert.True (f.CanFocus);
  486. Assert.True (v1.CanFocus);
  487. Assert.True (v2.CanFocus);
  488. w.CanFocus = false;
  489. Assert.False (w.CanFocus);
  490. Assert.False (f.CanFocus);
  491. Assert.False (v1.CanFocus);
  492. Assert.False (v2.CanFocus);
  493. };
  494. Application.Iteration += (s, a) => Application.RequestStop ();
  495. Application.Run ();
  496. Application.Shutdown ();
  497. }
  498. [Fact]
  499. public void CanFocus_Container_Toggling_All_Subviews_To_Old_Value_When_Is_True ()
  500. {
  501. Application.Init (new FakeDriver ());
  502. var t = Application.Top;
  503. var w = new Window ();
  504. var f = new FrameView ();
  505. var v1 = new View ();
  506. var v2 = new View () { CanFocus = true };
  507. f.Add (v1, v2);
  508. w.Add (f);
  509. t.Add (w);
  510. t.Ready += (s, e) => {
  511. Assert.True (t.CanFocus);
  512. Assert.True (w.CanFocus);
  513. Assert.True (f.CanFocus);
  514. Assert.False (v1.CanFocus);
  515. Assert.True (v2.CanFocus);
  516. w.CanFocus = false;
  517. Assert.False (w.CanFocus);
  518. Assert.False (f.CanFocus);
  519. Assert.False (v1.CanFocus);
  520. Assert.False (v2.CanFocus);
  521. w.CanFocus = true;
  522. Assert.True (w.CanFocus);
  523. Assert.True (f.CanFocus);
  524. Assert.False (v1.CanFocus);
  525. Assert.True (v2.CanFocus);
  526. };
  527. Application.Iteration += (s, a) => Application.RequestStop ();
  528. Application.Run ();
  529. Application.Shutdown ();
  530. }
  531. [Fact]
  532. public void Navigation_With_Null_Focused_View ()
  533. {
  534. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  535. Application.Init (new FakeDriver ());
  536. Application.Top.Ready += (s, e) => {
  537. Assert.Null (Application.Top.Focused);
  538. };
  539. // Keyboard navigation with tab
  540. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\t', ConsoleKey.Tab, false, false, false));
  541. Application.Iteration += (s, a) => Application.RequestStop ();
  542. Application.Run ();
  543. Application.Shutdown ();
  544. }
  545. [Fact]
  546. [AutoInitShutdown]
  547. public void Enabled_False_Sets_HasFocus_To_False ()
  548. {
  549. var wasClicked = false;
  550. var view = new Button ("Click Me");
  551. view.Clicked += (s, e) => wasClicked = !wasClicked;
  552. Application.Top.Add (view);
  553. view.NewKeyDownEvent (new (KeyCode.Space));
  554. Assert.True (wasClicked);
  555. view.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  556. Assert.False (wasClicked);
  557. Assert.True (view.Enabled);
  558. Assert.True (view.CanFocus);
  559. Assert.True (view.HasFocus);
  560. view.Enabled = false;
  561. view.NewKeyDownEvent (new (KeyCode.Space));
  562. Assert.False (wasClicked);
  563. view.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  564. Assert.False (wasClicked);
  565. Assert.False (view.Enabled);
  566. Assert.True (view.CanFocus);
  567. Assert.False (view.HasFocus);
  568. view.SetFocus ();
  569. Assert.False (view.HasFocus);
  570. }
  571. [Fact]
  572. [AutoInitShutdown]
  573. public void Enabled_Sets_Also_Sets_Subviews ()
  574. {
  575. var wasClicked = false;
  576. var button = new Button ("Click Me");
  577. button.IsDefault = true;
  578. button.Clicked += (s, e) => wasClicked = !wasClicked;
  579. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  580. win.Add (button);
  581. Application.Top.Add (win);
  582. var iterations = 0;
  583. Application.Iteration += (s, a) => {
  584. iterations++;
  585. win.NewKeyDownEvent (new (KeyCode.Enter));
  586. Assert.True (wasClicked);
  587. button.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  588. Assert.False (wasClicked);
  589. Assert.True (button.Enabled);
  590. Assert.True (button.CanFocus);
  591. Assert.True (button.HasFocus);
  592. Assert.True (win.Enabled);
  593. Assert.True (win.CanFocus);
  594. Assert.True (win.HasFocus);
  595. win.Enabled = false;
  596. button.NewKeyDownEvent (new (KeyCode.Enter));
  597. Assert.False (wasClicked);
  598. button.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  599. Assert.False (wasClicked);
  600. Assert.False (button.Enabled);
  601. Assert.True (button.CanFocus);
  602. Assert.False (button.HasFocus);
  603. Assert.False (win.Enabled);
  604. Assert.True (win.CanFocus);
  605. Assert.False (win.HasFocus);
  606. button.SetFocus ();
  607. Assert.False (button.HasFocus);
  608. Assert.False (win.HasFocus);
  609. win.SetFocus ();
  610. Assert.False (button.HasFocus);
  611. Assert.False (win.HasFocus);
  612. win.Enabled = true;
  613. win.FocusFirst ();
  614. Assert.True (button.HasFocus);
  615. Assert.True (win.HasFocus);
  616. Application.RequestStop ();
  617. };
  618. Application.Run ();
  619. Assert.Equal (1, iterations);
  620. }
  621. [Fact]
  622. [AutoInitShutdown]
  623. public void CanFocus_Sets_To_False_Does_Not_Sets_HasFocus_To_True ()
  624. {
  625. var view = new View () { CanFocus = true };
  626. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  627. win.Add (view);
  628. Application.Top.Add (win);
  629. Application.Begin (Application.Top);
  630. Assert.True (view.CanFocus);
  631. Assert.True (view.HasFocus);
  632. view.CanFocus = false;
  633. Assert.False (view.CanFocus);
  634. Assert.False (view.HasFocus);
  635. Assert.Null (Application.Current.Focused);
  636. Assert.Null (Application.Current.MostFocused);
  637. }
  638. [Fact]
  639. [AutoInitShutdown]
  640. public void CanFocus_Sets_To_False_On_Single_View_Focus_View_On_Another_Toplevel ()
  641. {
  642. var view1 = new View () { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  643. var win1 = new Window () { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  644. win1.Add (view1);
  645. var view2 = new View () { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  646. var win2 = new Window () { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  647. win2.Add (view2);
  648. Application.Top.Add (win1, win2);
  649. Application.Begin (Application.Top);
  650. Assert.True (view1.CanFocus);
  651. Assert.True (view1.HasFocus);
  652. Assert.True (view2.CanFocus);
  653. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  654. Assert.True (Application.Top.NewKeyDownEvent (new (KeyCode.Tab)));
  655. Assert.True (view1.CanFocus);
  656. Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
  657. Assert.True (view2.CanFocus);
  658. Assert.True (view2.HasFocus);
  659. Assert.True (Application.Top.NewKeyDownEvent (new (KeyCode.Tab)));
  660. Assert.True (view1.CanFocus);
  661. Assert.True (view1.HasFocus);
  662. Assert.True (view2.CanFocus);
  663. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  664. view1.CanFocus = false;
  665. Assert.False (view1.CanFocus);
  666. Assert.False (view1.HasFocus);
  667. Assert.True (view2.CanFocus);
  668. Assert.True (view2.HasFocus);
  669. Assert.Equal (win2, Application.Current.Focused);
  670. Assert.Equal (view2, Application.Current.MostFocused);
  671. }
  672. [Fact]
  673. [AutoInitShutdown]
  674. public void CanFocus_Sets_To_False_With_Two_Views_Focus_Another_View_On_The_Same_Toplevel ()
  675. {
  676. var view1 = new View () { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  677. var view12 = new View () { Id = "view12", Y = 5, Width = 10, Height = 1, CanFocus = true };
  678. var win1 = new Window () { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  679. win1.Add (view1, view12);
  680. var view2 = new View () { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  681. var win2 = new Window () { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  682. win2.Add (view2);
  683. Application.Top.Add (win1, win2);
  684. Application.Begin (Application.Top);
  685. Assert.True (view1.CanFocus);
  686. Assert.True (view1.HasFocus);
  687. Assert.True (view2.CanFocus);
  688. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  689. Assert.True (Application.Top.NewKeyDownEvent (new (KeyCode.Tab | KeyCode.CtrlMask)));
  690. Assert.True (Application.Top.NewKeyDownEvent (new (KeyCode.Tab | KeyCode.CtrlMask)));
  691. Assert.True (view1.CanFocus);
  692. Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
  693. Assert.True (view2.CanFocus);
  694. Assert.True (view2.HasFocus);
  695. Assert.True (Application.Top.NewKeyDownEvent (new (KeyCode.Tab | KeyCode.CtrlMask)));
  696. Assert.True (view1.CanFocus);
  697. Assert.True (view1.HasFocus);
  698. Assert.True (view2.CanFocus);
  699. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  700. view1.CanFocus = false;
  701. Assert.False (view1.CanFocus);
  702. Assert.False (view1.HasFocus);
  703. Assert.True (view2.CanFocus);
  704. Assert.False (view2.HasFocus);
  705. Assert.Equal (win1, Application.Current.Focused);
  706. Assert.Equal (view12, Application.Current.MostFocused);
  707. }
  708. [Fact]
  709. [AutoInitShutdown]
  710. public void CanFocus_Sets_To_False_On_Toplevel_Focus_View_On_Another_Toplevel ()
  711. {
  712. var view1 = new View () { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  713. var win1 = new Window () { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  714. win1.Add (view1);
  715. var view2 = new View () { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  716. var win2 = new Window () { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  717. win2.Add (view2);
  718. Application.Top.Add (win1, win2);
  719. Application.Begin (Application.Top);
  720. Assert.True (view1.CanFocus);
  721. Assert.True (view1.HasFocus);
  722. Assert.True (view2.CanFocus);
  723. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  724. Assert.True (Application.Top.NewKeyDownEvent (new (KeyCode.Tab | KeyCode.CtrlMask)));
  725. Assert.True (view1.CanFocus);
  726. Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
  727. Assert.True (view2.CanFocus);
  728. Assert.True (view2.HasFocus);
  729. Assert.True (Application.Top.NewKeyDownEvent (new (KeyCode.Tab | KeyCode.CtrlMask)));
  730. Assert.True (view1.CanFocus);
  731. Assert.True (view1.HasFocus);
  732. Assert.True (view2.CanFocus);
  733. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  734. win1.CanFocus = false;
  735. Assert.False (view1.CanFocus);
  736. Assert.False (view1.HasFocus);
  737. Assert.False (win1.CanFocus);
  738. Assert.False (win1.HasFocus);
  739. Assert.True (view2.CanFocus);
  740. Assert.True (view2.HasFocus);
  741. Assert.Equal (win2, Application.Current.Focused);
  742. Assert.Equal (view2, Application.Current.MostFocused);
  743. }
  744. [Fact]
  745. [AutoInitShutdown]
  746. public void HotKey_Will_Invoke_KeyPressed_Only_For_The_MostFocused_With_Top_KeyPress_Event ()
  747. {
  748. var sbQuiting = false;
  749. var tfQuiting = false;
  750. var topQuiting = false;
  751. var sb = new StatusBar (new StatusItem [] {
  752. new StatusItem(KeyCode.CtrlMask | KeyCode.Q, "~^Q~ Quit", () => sbQuiting = true )
  753. });
  754. var tf = new TextField ();
  755. tf.KeyDown += Tf_KeyPressed;
  756. void Tf_KeyPressed (object sender, Key obj)
  757. {
  758. if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask)) {
  759. obj.Handled = tfQuiting = true;
  760. }
  761. }
  762. var win = new Window ();
  763. win.Add (sb, tf);
  764. var top = Application.Top;
  765. top.KeyDown += Top_KeyPress;
  766. void Top_KeyPress (object sender, Key obj)
  767. {
  768. if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask)) {
  769. obj.Handled = topQuiting = true;
  770. }
  771. }
  772. top.Add (win);
  773. Application.Begin (top);
  774. Assert.False (sbQuiting);
  775. Assert.False (tfQuiting);
  776. Assert.False (topQuiting);
  777. Application.Driver.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  778. Assert.False (sbQuiting);
  779. Assert.True (tfQuiting);
  780. Assert.False (topQuiting);
  781. #if BROKE_WITH_2927
  782. tf.KeyPressed -= Tf_KeyPress;
  783. tfQuiting = false;
  784. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  785. Application.MainLoop.RunIteration ();
  786. Assert.True (sbQuiting);
  787. Assert.False (tfQuiting);
  788. Assert.False (topQuiting);
  789. sb.RemoveItem (0);
  790. sbQuiting = false;
  791. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  792. Application.MainLoop.RunIteration ();
  793. Assert.False (sbQuiting);
  794. Assert.False (tfQuiting);
  795. // This test is now invalid because `win` is focused, so it will receive the keypress
  796. Assert.True (topQuiting);
  797. #endif
  798. }
  799. [Fact]
  800. [AutoInitShutdown]
  801. public void HotKey_Will_Invoke_KeyPressed_Only_For_The_MostFocused_Without_Top_KeyPress_Event ()
  802. {
  803. var sbQuiting = false;
  804. var tfQuiting = false;
  805. var sb = new StatusBar (new StatusItem [] {
  806. new StatusItem(KeyCode.CtrlMask | KeyCode.Q, "~^Q~ Quit", () => sbQuiting = true )
  807. });
  808. var tf = new TextField ();
  809. tf.KeyDown += Tf_KeyPressed;
  810. void Tf_KeyPressed (object sender, Key obj)
  811. {
  812. if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask)) {
  813. obj.Handled = tfQuiting = true;
  814. }
  815. }
  816. var win = new Window ();
  817. win.Add (sb, tf);
  818. var top = Application.Top;
  819. top.Add (win);
  820. Application.Begin (top);
  821. Assert.False (sbQuiting);
  822. Assert.False (tfQuiting);
  823. Application.Driver.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  824. Assert.False (sbQuiting);
  825. Assert.True (tfQuiting);
  826. tf.KeyDown -= Tf_KeyPressed;
  827. tfQuiting = false;
  828. Application.Driver.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  829. Application.MainLoop.RunIteration ();
  830. #if BROKE_WITH_2927
  831. Assert.True (sbQuiting);
  832. Assert.False (tfQuiting);
  833. #endif
  834. }
  835. [Fact]
  836. [AutoInitShutdown]
  837. public void WindowDispose_CanFocusProblem ()
  838. {
  839. // Arrange
  840. Application.Init ();
  841. using var top = new Toplevel ();
  842. using var view = new View (
  843. x: 0,
  844. y: 1,
  845. text: nameof (WindowDispose_CanFocusProblem));
  846. using var window = new Window ();
  847. top.Add (window);
  848. window.Add (view);
  849. // Act
  850. var rs = Application.Begin (top);
  851. Application.End (rs);
  852. Application.Shutdown ();
  853. // Assert does Not throw NullReferenceException
  854. top.SetFocus ();
  855. }
  856. [Fact, AutoInitShutdown]
  857. public void SetHasFocus_Do_Not_Throws_If_OnLeave_Remove_Focused_Changing_To_Null ()
  858. {
  859. var view1Leave = false;
  860. var subView1Leave = false;
  861. var subView1subView1Leave = false;
  862. var top = Application.Top;
  863. var view1 = new View { CanFocus = true };
  864. var subView1 = new View { CanFocus = true };
  865. var subView1subView1 = new View { CanFocus = true };
  866. view1.Leave += (s, e) => {
  867. view1Leave = true;
  868. };
  869. subView1.Leave += (s, e) => {
  870. subView1.Remove (subView1subView1);
  871. subView1Leave = true;
  872. };
  873. view1.Add (subView1);
  874. subView1subView1.Leave += (s, e) => {
  875. // This is never invoked
  876. subView1subView1Leave = true;
  877. };
  878. subView1.Add (subView1subView1);
  879. var view2 = new View { CanFocus = true };
  880. top.Add (view1, view2);
  881. var rs = Application.Begin (top);
  882. view2.SetFocus ();
  883. Assert.True (view1Leave);
  884. Assert.True (subView1Leave);
  885. Assert.False (subView1subView1Leave);
  886. Application.End (rs);
  887. subView1subView1.Dispose ();
  888. }
  889. [Fact, AutoInitShutdown]
  890. public void Remove_Does_Not_Change_Focus ()
  891. {
  892. Assert.True (Application.Top.CanFocus);
  893. Assert.False (Application.Top.HasFocus);
  894. var container = new View () { Width = 10, Height = 10 };
  895. var leave = false;
  896. container.Leave += (s, e) => leave = true;
  897. Assert.False (container.CanFocus);
  898. var child = new View () { Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  899. container.Add (child);
  900. Assert.True (container.CanFocus);
  901. Assert.False (container.HasFocus);
  902. Assert.True (child.CanFocus);
  903. Assert.False (child.HasFocus);
  904. Application.Top.Add (container);
  905. Application.Begin (Application.Top);
  906. Assert.True (Application.Top.CanFocus);
  907. Assert.True (Application.Top.HasFocus);
  908. Assert.True (container.CanFocus);
  909. Assert.True (container.HasFocus);
  910. Assert.True (child.CanFocus);
  911. Assert.True (child.HasFocus);
  912. container.Remove (child);
  913. child.Dispose ();
  914. child = null;
  915. Assert.True (Application.Top.HasFocus);
  916. Assert.True (container.CanFocus);
  917. Assert.True (container.HasFocus);
  918. Assert.Null (child);
  919. Assert.False (leave);
  920. }
  921. [Fact, AutoInitShutdown]
  922. public void SetFocus_View_With_Null_Superview_Does_Not_Throw_Exception ()
  923. {
  924. Assert.True (Application.Top.CanFocus);
  925. Assert.False (Application.Top.HasFocus);
  926. var exception = Record.Exception (Application.Top.SetFocus);
  927. Assert.Null (exception);
  928. Assert.True (Application.Top.CanFocus);
  929. Assert.True (Application.Top.HasFocus);
  930. }
  931. [Fact, AutoInitShutdown]
  932. public void FocusNext_Does_Not_Throws_If_A_View_Was_Removed_From_The_Collection ()
  933. {
  934. var top1 = Application.Top;
  935. var view1 = new View () { Id = "view1", Width = 10, Height = 5, CanFocus = true };
  936. var top2 = new Toplevel () { Id = "top2", Y = 1, Width = 10, Height = 5 };
  937. var view2 = new View () { Id = "view2", Y = 1, Width = 10, Height = 5, CanFocus = true };
  938. View view3 = null;
  939. var removed = false;
  940. view2.Enter += (s, e) => {
  941. if (!removed) {
  942. removed = true;
  943. view3 = new View () { Id = "view3", Y = 1, Width = 10, Height = 5 };
  944. Application.Current.Add (view3);
  945. Application.Current.BringSubviewToFront (view3);
  946. Assert.False (view3.HasFocus);
  947. }
  948. };
  949. view2.Leave += (s, e) => {
  950. Application.Current.Remove (view3);
  951. view3.Dispose ();
  952. view3 = null;
  953. };
  954. top2.Add (view2);
  955. top1.Add (view1, top2);
  956. Application.Begin (top1);
  957. Assert.True (top1.HasFocus);
  958. Assert.True (view1.HasFocus);
  959. Assert.False (view2.HasFocus);
  960. Assert.False (removed);
  961. Assert.Null (view3);
  962. Assert.True (top1.NewKeyDownEvent (new (KeyCode.Tab | KeyCode.CtrlMask)));
  963. Assert.True (top1.HasFocus);
  964. Assert.False (view1.HasFocus);
  965. Assert.True (view2.HasFocus);
  966. Assert.True (removed);
  967. Assert.NotNull (view3);
  968. var exception = Record.Exception (() => top1.NewKeyDownEvent (new (KeyCode.Tab | KeyCode.CtrlMask)));
  969. Assert.Null (exception);
  970. Assert.True (removed);
  971. Assert.Null (view3);
  972. }
  973. [Fact, AutoInitShutdown]
  974. public void ScreenToView_ViewToScreen_FindDeepestView_Full_Top ()
  975. {
  976. var top = Application.Current;
  977. top.BorderStyle = LineStyle.Single;
  978. var view = new View () { X = 3, Y = 2, Width = 10, Height = 1, Text = "0123456789" };
  979. top.Add (view);
  980. Application.Begin (top);
  981. Assert.Equal (Application.Current, top);
  982. Assert.Equal (new Rect (0, 0, 80, 25), new Rect (0, 0, View.Driver.Cols, View.Driver.Rows));
  983. Assert.Equal (new Rect (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  984. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  985. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  986. Assert.Equal (new Rect (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  987. Assert.Equal (new Rect (0, 0, 20, 10), top.Frame);
  988. _ = TestHelpers.AssertDriverContentsWithFrameAre (@"
  989. ┌──────────────────┐
  990. │ │
  991. │ │
  992. │ 0123456789 │
  993. │ │
  994. │ │
  995. │ │
  996. │ │
  997. │ │
  998. └──────────────────┘", output);
  999. // top
  1000. Assert.Equal (Point.Empty, top.ScreenToFrame (0, 0));
  1001. top.Margin.BoundsToScreen (0, 0, out int col, out int row);
  1002. Assert.Equal (0, col);
  1003. Assert.Equal (0, row);
  1004. top.Border.BoundsToScreen (0, 0, out col, out row);
  1005. Assert.Equal (0, col);
  1006. Assert.Equal (0, row);
  1007. top.Padding.BoundsToScreen (0, 0, out col, out row);
  1008. Assert.Equal (0, col);
  1009. Assert.Equal (0, row);
  1010. top.BoundsToScreen (0, 0, out col, out row);
  1011. Assert.Equal (1, col);
  1012. Assert.Equal (1, row);
  1013. top.BoundsToScreen (-1, -1, out col, out row);
  1014. Assert.Equal (0, col);
  1015. Assert.Equal (0, row);
  1016. Assert.Equal (top, View.FindDeepestView (top, 0, 0, out int rx, out int ry));
  1017. Assert.Equal (0, rx);
  1018. Assert.Equal (0, ry);
  1019. Assert.Equal (new Point (3, 2), top.ScreenToFrame (3, 2));
  1020. top.BoundsToScreen (3, 2, out col, out row);
  1021. Assert.Equal (4, col);
  1022. Assert.Equal (3, row);
  1023. Assert.Equal (view, View.FindDeepestView (top, col, row, out rx, out ry));
  1024. Assert.Equal (0, rx);
  1025. Assert.Equal (0, ry);
  1026. Assert.Equal (top, View.FindDeepestView (top, 3, 2, out rx, out ry));
  1027. Assert.Equal (3, rx);
  1028. Assert.Equal (2, ry);
  1029. Assert.Equal (new Point (13, 2), top.ScreenToFrame (13, 2));
  1030. top.BoundsToScreen (12, 2, out col, out row);
  1031. Assert.Equal (13, col);
  1032. Assert.Equal (3, row);
  1033. Assert.Equal (view, View.FindDeepestView (top, col, row, out rx, out ry));
  1034. Assert.Equal (9, rx);
  1035. Assert.Equal (0, ry);
  1036. top.BoundsToScreen (13, 2, out col, out row);
  1037. Assert.Equal (14, col);
  1038. Assert.Equal (3, row);
  1039. Assert.Equal (top, View.FindDeepestView (top, 13, 2, out rx, out ry));
  1040. Assert.Equal (13, rx);
  1041. Assert.Equal (2, ry);
  1042. Assert.Equal (new Point (14, 3), top.ScreenToFrame (14, 3));
  1043. top.BoundsToScreen (14, 3, out col, out row);
  1044. Assert.Equal (15, col);
  1045. Assert.Equal (4, row);
  1046. Assert.Equal (top, View.FindDeepestView (top, 14, 3, out rx, out ry));
  1047. Assert.Equal (14, rx);
  1048. Assert.Equal (3, ry);
  1049. // view
  1050. Assert.Equal (new Point (-4, -3), view.ScreenToFrame (0, 0));
  1051. view.Margin.BoundsToScreen (-3, -2, out col, out row);
  1052. Assert.Equal (1, col);
  1053. Assert.Equal (1, row);
  1054. view.Border.BoundsToScreen (-3, -2, out col, out row);
  1055. Assert.Equal (1, col);
  1056. Assert.Equal (1, row);
  1057. view.Padding.BoundsToScreen (-3, -2, out col, out row);
  1058. Assert.Equal (1, col);
  1059. Assert.Equal (1, row);
  1060. view.BoundsToScreen (-3, -2, out col, out row);
  1061. Assert.Equal (1, col);
  1062. Assert.Equal (1, row);
  1063. view.BoundsToScreen (-4, -3, out col, out row);
  1064. Assert.Equal (0, col);
  1065. Assert.Equal (0, row);
  1066. Assert.Equal (top, View.FindDeepestView (top, 0, 0, out rx, out ry));
  1067. Assert.Equal (0, rx);
  1068. Assert.Equal (0, ry);
  1069. Assert.Equal (new Point (-1, -1), view.ScreenToFrame (3, 2));
  1070. view.BoundsToScreen (0, 0, out col, out row);
  1071. Assert.Equal (4, col);
  1072. Assert.Equal (3, row);
  1073. Assert.Equal (view, View.FindDeepestView (top, 4, 3, out rx, out ry));
  1074. Assert.Equal (0, rx);
  1075. Assert.Equal (0, ry);
  1076. Assert.Equal (new Point (9, -1), view.ScreenToFrame (13, 2));
  1077. view.BoundsToScreen (10, 0, out col, out row);
  1078. Assert.Equal (14, col);
  1079. Assert.Equal (3, row);
  1080. Assert.Equal (top, View.FindDeepestView (top, 14, 3, out rx, out ry));
  1081. Assert.Equal (14, rx);
  1082. Assert.Equal (3, ry);
  1083. Assert.Equal (new Point (10, 0), view.ScreenToFrame (14, 3));
  1084. view.BoundsToScreen (11, 1, out col, out row);
  1085. Assert.Equal (15, col);
  1086. Assert.Equal (4, row);
  1087. Assert.Equal (top, View.FindDeepestView (top, 15, 4, out rx, out ry));
  1088. Assert.Equal (15, rx);
  1089. Assert.Equal (4, ry);
  1090. }
  1091. [Fact, AutoInitShutdown]
  1092. public void ScreenToView_ViewToScreen_FindDeepestView_Smaller_Top ()
  1093. {
  1094. var top = new Toplevel () { X = 3, Y = 2, Width = 20, Height = 10, BorderStyle = LineStyle.Single };
  1095. var view = new View () { X = 3, Y = 2, Width = 10, Height = 1, Text = "0123456789" };
  1096. top.Add (view);
  1097. Application.Begin (top);
  1098. Assert.Equal (Application.Current, top);
  1099. Assert.Equal (new Rect (0, 0, 80, 25), new Rect (0, 0, View.Driver.Cols, View.Driver.Rows));
  1100. Assert.NotEqual (new Rect (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  1101. Assert.Equal (new Rect (3, 2, 20, 10), top.Frame);
  1102. ((FakeDriver)Application.Driver).SetBufferSize (30, 20);
  1103. Assert.Equal (new Rect (0, 0, 30, 20), new Rect (0, 0, View.Driver.Cols, View.Driver.Rows));
  1104. Assert.NotEqual (new Rect (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  1105. Assert.Equal (new Rect (3, 2, 20, 10), top.Frame);
  1106. var frame = TestHelpers.AssertDriverContentsWithFrameAre (@"
  1107. ┌──────────────────┐
  1108. │ │
  1109. │ │
  1110. │ 0123456789 │
  1111. │ │
  1112. │ │
  1113. │ │
  1114. │ │
  1115. │ │
  1116. └──────────────────┘", output);
  1117. // mean the output started at col 3 and line 2
  1118. // which result with a width of 23 and a height of 10 on the output
  1119. Assert.Equal (new Rect (3, 2, 23, 10), frame);
  1120. // top
  1121. Assert.Equal (new Point (-3, -2), top.ScreenToFrame (0, 0));
  1122. top.Margin.BoundsToScreen (-3, -2, out int col, out int row);
  1123. Assert.Equal (0, col);
  1124. Assert.Equal (0, row);
  1125. top.Border.BoundsToScreen (-3, -2, out col, out row);
  1126. Assert.Equal (0, col);
  1127. Assert.Equal (0, row);
  1128. top.Padding.BoundsToScreen (-3, -2, out col, out row);
  1129. Assert.Equal (0, col);
  1130. Assert.Equal (0, row);
  1131. top.BoundsToScreen (-3, -2, out col, out row);
  1132. Assert.Equal (1, col);
  1133. Assert.Equal (1, row);
  1134. top.BoundsToScreen (-4, -3, out col, out row);
  1135. Assert.Equal (0, col);
  1136. Assert.Equal (0, row);
  1137. Assert.Null (View.FindDeepestView (top, -4, -3, out int rx, out int ry));
  1138. Assert.Equal (0, rx);
  1139. Assert.Equal (0, ry);
  1140. Assert.Equal (Point.Empty, top.ScreenToFrame (3, 2));
  1141. top.BoundsToScreen (0, 0, out col, out row);
  1142. Assert.Equal (4, col);
  1143. Assert.Equal (3, row);
  1144. Assert.Equal (top, View.FindDeepestView (top, 3, 2, out rx, out ry));
  1145. Assert.Equal (0, rx);
  1146. Assert.Equal (0, ry);
  1147. Assert.Equal (new Point (10, 0), top.ScreenToFrame (13, 2));
  1148. top.BoundsToScreen (10, 0, out col, out row);
  1149. Assert.Equal (14, col);
  1150. Assert.Equal (3, row);
  1151. Assert.Equal (top, View.FindDeepestView (top, 13, 2, out rx, out ry));
  1152. Assert.Equal (10, rx);
  1153. Assert.Equal (0, ry);
  1154. Assert.Equal (new Point (11, 1), top.ScreenToFrame (14, 3));
  1155. top.BoundsToScreen (11, 1, out col, out row);
  1156. Assert.Equal (15, col);
  1157. Assert.Equal (4, row);
  1158. Assert.Equal (top, View.FindDeepestView (top, 14, 3, out rx, out ry));
  1159. Assert.Equal (11, rx);
  1160. Assert.Equal (1, ry);
  1161. // view
  1162. Assert.Equal (new Point (-7, -5), view.ScreenToFrame (0, 0));
  1163. view.Margin.BoundsToScreen (-6, -4, out col, out row);
  1164. Assert.Equal (1, col);
  1165. Assert.Equal (1, row);
  1166. view.Border.BoundsToScreen (-6, -4, out col, out row);
  1167. Assert.Equal (1, col);
  1168. Assert.Equal (1, row);
  1169. view.Padding.BoundsToScreen (-6, -4, out col, out row);
  1170. Assert.Equal (1, col);
  1171. Assert.Equal (1, row);
  1172. view.BoundsToScreen (-6, -4, out col, out row);
  1173. Assert.Equal (1, col);
  1174. Assert.Equal (1, row);
  1175. Assert.Null (View.FindDeepestView (top, 1, 1, out rx, out ry));
  1176. Assert.Equal (0, rx);
  1177. Assert.Equal (0, ry);
  1178. Assert.Equal (new Point (-4, -3), view.ScreenToFrame (3, 2));
  1179. view.BoundsToScreen (-3, -2, out col, out row);
  1180. Assert.Equal (4, col);
  1181. Assert.Equal (3, row);
  1182. Assert.Equal (top, View.FindDeepestView (top, 4, 3, out rx, out ry));
  1183. Assert.Equal (1, rx);
  1184. Assert.Equal (1, ry);
  1185. Assert.Equal (new Point (-1, -1), view.ScreenToFrame (6, 4));
  1186. view.BoundsToScreen (0, 0, out col, out row);
  1187. Assert.Equal (7, col);
  1188. Assert.Equal (5, row);
  1189. Assert.Equal (view, View.FindDeepestView (top, 7, 5, out rx, out ry));
  1190. Assert.Equal (0, rx);
  1191. Assert.Equal (0, ry);
  1192. Assert.Equal (new Point (6, -1), view.ScreenToFrame (13, 4));
  1193. view.BoundsToScreen (7, 0, out col, out row);
  1194. Assert.Equal (14, col);
  1195. Assert.Equal (5, row);
  1196. Assert.Equal (view, View.FindDeepestView (top, 14, 5, out rx, out ry));
  1197. Assert.Equal (7, rx);
  1198. Assert.Equal (0, ry);
  1199. Assert.Equal (new Point (7, -2), view.ScreenToFrame (14, 3));
  1200. view.BoundsToScreen (8, -1, out col, out row);
  1201. Assert.Equal (15, col);
  1202. Assert.Equal (4, row);
  1203. Assert.Equal (top, View.FindDeepestView (top, 15, 4, out rx, out ry));
  1204. Assert.Equal (12, rx);
  1205. Assert.Equal (2, ry);
  1206. Assert.Equal (new Point (16, -2), view.ScreenToFrame (23, 3));
  1207. view.BoundsToScreen (17, -1, out col, out row);
  1208. Assert.Equal (24, col);
  1209. Assert.Equal (4, row);
  1210. Assert.Null (View.FindDeepestView (top, 24, 4, out rx, out ry));
  1211. Assert.Equal (0, rx);
  1212. Assert.Equal (0, ry);
  1213. }
  1214. }
  1215. }