NavigationTests.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  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.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  31. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  32. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  33. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  34. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  35. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  36. top.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ()));
  37. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  38. top.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ()));
  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.ProcessKey (new KeyEvent (Key.Enter, null));
  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.ProcessKey (new KeyEvent (Key.Enter, null));
  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.Clicked += (s, e) => wasClicked = !wasClicked;
  578. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  579. win.Add (button);
  580. Application.Top.Add (win);
  581. var iterations = 0;
  582. Application.Iteration += (s, a) => {
  583. iterations++;
  584. button.ProcessKey (new KeyEvent (Key.Enter, null));
  585. Assert.True (wasClicked);
  586. button.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  587. Assert.False (wasClicked);
  588. Assert.True (button.Enabled);
  589. Assert.True (button.CanFocus);
  590. Assert.True (button.HasFocus);
  591. Assert.True (win.Enabled);
  592. Assert.True (win.CanFocus);
  593. Assert.True (win.HasFocus);
  594. win.Enabled = false;
  595. button.ProcessKey (new KeyEvent (Key.Enter, null));
  596. Assert.False (wasClicked);
  597. button.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  598. Assert.False (wasClicked);
  599. Assert.False (button.Enabled);
  600. Assert.True (button.CanFocus);
  601. Assert.False (button.HasFocus);
  602. Assert.False (win.Enabled);
  603. Assert.True (win.CanFocus);
  604. Assert.False (win.HasFocus);
  605. button.SetFocus ();
  606. Assert.False (button.HasFocus);
  607. Assert.False (win.HasFocus);
  608. win.SetFocus ();
  609. Assert.False (button.HasFocus);
  610. Assert.False (win.HasFocus);
  611. win.Enabled = true;
  612. win.FocusFirst ();
  613. Assert.True (button.HasFocus);
  614. Assert.True (win.HasFocus);
  615. Application.RequestStop ();
  616. };
  617. Application.Run ();
  618. Assert.Equal (1, iterations);
  619. }
  620. [Fact]
  621. [AutoInitShutdown]
  622. public void CanFocus_Sets_To_False_Does_Not_Sets_HasFocus_To_True ()
  623. {
  624. var view = new View () { CanFocus = true };
  625. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  626. win.Add (view);
  627. Application.Top.Add (win);
  628. Application.Begin (Application.Top);
  629. Assert.True (view.CanFocus);
  630. Assert.True (view.HasFocus);
  631. view.CanFocus = false;
  632. Assert.False (view.CanFocus);
  633. Assert.False (view.HasFocus);
  634. Assert.Null (Application.Current.Focused);
  635. Assert.Null (Application.Current.MostFocused);
  636. }
  637. [Fact]
  638. [AutoInitShutdown]
  639. public void CanFocus_Sets_To_False_On_Single_View_Focus_View_On_Another_Toplevel ()
  640. {
  641. var view1 = new View () { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  642. var win1 = new Window () { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  643. win1.Add (view1);
  644. var view2 = new View () { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  645. var win2 = new Window () { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  646. win2.Add (view2);
  647. Application.Top.Add (win1, win2);
  648. Application.Begin (Application.Top);
  649. Assert.True (view1.CanFocus);
  650. Assert.True (view1.HasFocus);
  651. Assert.True (view2.CanFocus);
  652. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  653. Assert.True (Application.Top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  654. Assert.True (view1.CanFocus);
  655. Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
  656. Assert.True (view2.CanFocus);
  657. Assert.True (view2.HasFocus);
  658. Assert.True (Application.Top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  659. Assert.True (view1.CanFocus);
  660. Assert.True (view1.HasFocus);
  661. Assert.True (view2.CanFocus);
  662. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  663. view1.CanFocus = false;
  664. Assert.False (view1.CanFocus);
  665. Assert.False (view1.HasFocus);
  666. Assert.True (view2.CanFocus);
  667. Assert.True (view2.HasFocus);
  668. Assert.Equal (win2, Application.Current.Focused);
  669. Assert.Equal (view2, Application.Current.MostFocused);
  670. }
  671. [Fact]
  672. [AutoInitShutdown]
  673. public void CanFocus_Sets_To_False_With_Two_Views_Focus_Another_View_On_The_Same_Toplevel ()
  674. {
  675. var view1 = new View () { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  676. var view12 = new View () { Id = "view12", Y = 5, Width = 10, Height = 1, CanFocus = true };
  677. var win1 = new Window () { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  678. win1.Add (view1, view12);
  679. var view2 = new View () { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  680. var win2 = new Window () { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  681. win2.Add (view2);
  682. Application.Top.Add (win1, win2);
  683. Application.Begin (Application.Top);
  684. Assert.True (view1.CanFocus);
  685. Assert.True (view1.HasFocus);
  686. Assert.True (view2.CanFocus);
  687. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  688. Assert.True (Application.Top.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  689. Assert.True (Application.Top.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  690. Assert.True (view1.CanFocus);
  691. Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
  692. Assert.True (view2.CanFocus);
  693. Assert.True (view2.HasFocus);
  694. Assert.True (Application.Top.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  695. Assert.True (view1.CanFocus);
  696. Assert.True (view1.HasFocus);
  697. Assert.True (view2.CanFocus);
  698. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  699. view1.CanFocus = false;
  700. Assert.False (view1.CanFocus);
  701. Assert.False (view1.HasFocus);
  702. Assert.True (view2.CanFocus);
  703. Assert.False (view2.HasFocus);
  704. Assert.Equal (win1, Application.Current.Focused);
  705. Assert.Equal (view12, Application.Current.MostFocused);
  706. }
  707. [Fact]
  708. [AutoInitShutdown]
  709. public void CanFocus_Sets_To_False_On_Toplevel_Focus_View_On_Another_Toplevel ()
  710. {
  711. var view1 = new View () { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  712. var win1 = new Window () { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  713. win1.Add (view1);
  714. var view2 = new View () { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  715. var win2 = new Window () { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  716. win2.Add (view2);
  717. Application.Top.Add (win1, win2);
  718. Application.Begin (Application.Top);
  719. Assert.True (view1.CanFocus);
  720. Assert.True (view1.HasFocus);
  721. Assert.True (view2.CanFocus);
  722. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  723. Assert.True (Application.Top.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  724. Assert.True (view1.CanFocus);
  725. Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
  726. Assert.True (view2.CanFocus);
  727. Assert.True (view2.HasFocus);
  728. Assert.True (Application.Top.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ())));
  729. Assert.True (view1.CanFocus);
  730. Assert.True (view1.HasFocus);
  731. Assert.True (view2.CanFocus);
  732. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  733. win1.CanFocus = false;
  734. Assert.False (view1.CanFocus);
  735. Assert.False (view1.HasFocus);
  736. Assert.False (win1.CanFocus);
  737. Assert.False (win1.HasFocus);
  738. Assert.True (view2.CanFocus);
  739. Assert.True (view2.HasFocus);
  740. Assert.Equal (win2, Application.Current.Focused);
  741. Assert.Equal (view2, Application.Current.MostFocused);
  742. }
  743. [Fact]
  744. [AutoInitShutdown]
  745. public void ProcessHotKey_Will_Invoke_ProcessKey_Only_For_The_MostFocused_With_Top_KeyPress_Event ()
  746. {
  747. var sbQuiting = false;
  748. var tfQuiting = false;
  749. var topQuiting = false;
  750. var sb = new StatusBar (new StatusItem [] {
  751. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => sbQuiting = true )
  752. });
  753. var tf = new TextField ();
  754. tf.KeyPressed += Tf_KeyPress;
  755. void Tf_KeyPress (object sender, KeyEventEventArgs obj)
  756. {
  757. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  758. obj.Handled = tfQuiting = true;
  759. }
  760. }
  761. var win = new Window ();
  762. win.Add (sb, tf);
  763. var top = Application.Top;
  764. top.KeyPressed += Top_KeyPress;
  765. void Top_KeyPress (object sender, KeyEventEventArgs obj)
  766. {
  767. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  768. obj.Handled = topQuiting = true;
  769. }
  770. }
  771. top.Add (win);
  772. Application.Begin (top);
  773. Assert.False (sbQuiting);
  774. Assert.False (tfQuiting);
  775. Assert.False (topQuiting);
  776. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  777. Assert.False (sbQuiting);
  778. Assert.True (tfQuiting);
  779. Assert.False (topQuiting);
  780. tf.KeyPressed -= Tf_KeyPress;
  781. tfQuiting = false;
  782. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  783. Application.MainLoop.RunIteration ();
  784. Assert.True (sbQuiting);
  785. Assert.False (tfQuiting);
  786. Assert.False (topQuiting);
  787. sb.RemoveItem (0);
  788. sbQuiting = false;
  789. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  790. Application.MainLoop.RunIteration ();
  791. Assert.False (sbQuiting);
  792. Assert.False (tfQuiting);
  793. Assert.True (topQuiting);
  794. }
  795. [Fact]
  796. [AutoInitShutdown]
  797. public void ProcessHotKey_Will_Invoke_ProcessKey_Only_For_The_MostFocused_Without_Top_KeyPress_Event ()
  798. {
  799. var sbQuiting = false;
  800. var tfQuiting = false;
  801. var sb = new StatusBar (new StatusItem [] {
  802. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => sbQuiting = true )
  803. });
  804. var tf = new TextField ();
  805. tf.KeyPressed += Tf_KeyPress;
  806. void Tf_KeyPress (object sender, KeyEventEventArgs obj)
  807. {
  808. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  809. obj.Handled = tfQuiting = true;
  810. }
  811. }
  812. var win = new Window ();
  813. win.Add (sb, tf);
  814. var top = Application.Top;
  815. top.Add (win);
  816. Application.Begin (top);
  817. Assert.False (sbQuiting);
  818. Assert.False (tfQuiting);
  819. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  820. Assert.False (sbQuiting);
  821. Assert.True (tfQuiting);
  822. tf.KeyPressed -= Tf_KeyPress;
  823. tfQuiting = false;
  824. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  825. Application.MainLoop.RunIteration ();
  826. Assert.True (sbQuiting);
  827. Assert.False (tfQuiting);
  828. }
  829. [Fact]
  830. [AutoInitShutdown]
  831. public void WindowDispose_CanFocusProblem ()
  832. {
  833. // Arrange
  834. Application.Init ();
  835. using var top = Toplevel.Create ();
  836. using var view = new View (
  837. x: 0,
  838. y: 1,
  839. text: nameof (WindowDispose_CanFocusProblem));
  840. using var window = new Window ();
  841. top.Add (window);
  842. window.Add (view);
  843. // Act
  844. var rs = Application.Begin (top);
  845. Application.End (rs);
  846. Application.Shutdown ();
  847. // Assert does Not throw NullReferenceException
  848. top.SetFocus ();
  849. }
  850. [Fact, AutoInitShutdown]
  851. public void SetHasFocus_Do_Not_Throws_If_OnLeave_Remove_Focused_Changing_To_Null ()
  852. {
  853. var view1Leave = false;
  854. var subView1Leave = false;
  855. var subView1subView1Leave = false;
  856. var top = Application.Top;
  857. var view1 = new View { CanFocus = true };
  858. var subView1 = new View { CanFocus = true };
  859. var subView1subView1 = new View { CanFocus = true };
  860. view1.Leave += (s, e) => {
  861. view1Leave = true;
  862. };
  863. subView1.Leave += (s, e) => {
  864. subView1.Remove (subView1subView1);
  865. subView1Leave = true;
  866. };
  867. view1.Add (subView1);
  868. subView1subView1.Leave += (s, e) => {
  869. // This is never invoked
  870. subView1subView1Leave = true;
  871. };
  872. subView1.Add (subView1subView1);
  873. var view2 = new View { CanFocus = true };
  874. top.Add (view1, view2);
  875. var rs = Application.Begin (top);
  876. view2.SetFocus ();
  877. Assert.True (view1Leave);
  878. Assert.True (subView1Leave);
  879. Assert.False (subView1subView1Leave);
  880. Application.End (rs);
  881. subView1subView1.Dispose ();
  882. }
  883. [Fact, AutoInitShutdown]
  884. public void Remove_Does_Not_Change_Focus ()
  885. {
  886. Assert.True (Application.Top.CanFocus);
  887. Assert.False (Application.Top.HasFocus);
  888. var container = new View () { Width = 10, Height = 10 };
  889. var leave = false;
  890. container.Leave += (s, e) => leave = true;
  891. Assert.False (container.CanFocus);
  892. var child = new View () { Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  893. container.Add (child);
  894. Assert.True (container.CanFocus);
  895. Assert.False (container.HasFocus);
  896. Assert.True (child.CanFocus);
  897. Assert.False (child.HasFocus);
  898. Application.Top.Add (container);
  899. Application.Begin (Application.Top);
  900. Assert.True (Application.Top.CanFocus);
  901. Assert.True (Application.Top.HasFocus);
  902. Assert.True (container.CanFocus);
  903. Assert.True (container.HasFocus);
  904. Assert.True (child.CanFocus);
  905. Assert.True (child.HasFocus);
  906. container.Remove (child);
  907. child.Dispose ();
  908. child = null;
  909. Assert.True (Application.Top.HasFocus);
  910. Assert.True (container.CanFocus);
  911. Assert.True (container.HasFocus);
  912. Assert.Null (child);
  913. Assert.False (leave);
  914. }
  915. [Fact, AutoInitShutdown]
  916. public void SetFocus_View_With_Null_Superview_Does_Not_Throw_Exception ()
  917. {
  918. Assert.True (Application.Top.CanFocus);
  919. Assert.False (Application.Top.HasFocus);
  920. var exception = Record.Exception (Application.Top.SetFocus);
  921. Assert.Null (exception);
  922. Assert.True (Application.Top.CanFocus);
  923. Assert.True (Application.Top.HasFocus);
  924. }
  925. [Fact, AutoInitShutdown]
  926. public void FocusNext_Does_Not_Throws_If_A_View_Was_Removed_From_The_Collection ()
  927. {
  928. var top1 = Application.Top;
  929. var view1 = new View () { Id = "view1", Width = 10, Height = 5, CanFocus = true };
  930. var top2 = new Toplevel () { Id = "top2", Y = 1, Width = 10, Height = 5 };
  931. var view2 = new View () { Id = "view2", Y = 1, Width = 10, Height = 5, CanFocus = true };
  932. View view3 = null;
  933. var removed = false;
  934. view2.Enter += (s, e) => {
  935. if (!removed) {
  936. removed = true;
  937. view3 = new View () { Id = "view3", Y = 1, Width = 10, Height = 5 };
  938. Application.Current.Add (view3);
  939. Application.Current.BringSubviewToFront (view3);
  940. Assert.False (view3.HasFocus);
  941. }
  942. };
  943. view2.Leave += (s, e) => {
  944. Application.Current.Remove (view3);
  945. view3.Dispose ();
  946. view3 = null;
  947. };
  948. top2.Add (view2);
  949. top1.Add (view1, top2);
  950. Application.Begin (top1);
  951. Assert.True (top1.HasFocus);
  952. Assert.True (view1.HasFocus);
  953. Assert.False (view2.HasFocus);
  954. Assert.False (removed);
  955. Assert.Null (view3);
  956. Assert.True (top1.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers { Ctrl = true })));
  957. Assert.True (top1.HasFocus);
  958. Assert.False (view1.HasFocus);
  959. Assert.True (view2.HasFocus);
  960. Assert.True (removed);
  961. Assert.NotNull (view3);
  962. var exception = Record.Exception (() => top1.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers { Ctrl = true })));
  963. Assert.Null (exception);
  964. Assert.True (removed);
  965. Assert.Null (view3);
  966. }
  967. [Fact, AutoInitShutdown]
  968. public void ScreenToView_ViewToScreen_FindDeepestView_Full_Top ()
  969. {
  970. var top = Application.Current;
  971. top.BorderStyle = LineStyle.Single;
  972. var view = new View () { X = 3, Y = 2, Width = 10, Height = 1, Text = "0123456789" };
  973. top.Add (view);
  974. Application.Begin (top);
  975. Assert.Equal (Application.Current, top);
  976. Assert.Equal (new Rect (0, 0, 80, 25), new Rect (0, 0, View.Driver.Cols, View.Driver.Rows));
  977. Assert.Equal (new Rect (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  978. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  979. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  980. Assert.Equal (new Rect (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  981. Assert.Equal (new Rect (0, 0, 20, 10), top.Frame);
  982. _ = TestHelpers.AssertDriverContentsWithFrameAre (@"
  983. ┌──────────────────┐
  984. │ │
  985. │ │
  986. │ 0123456789 │
  987. │ │
  988. │ │
  989. │ │
  990. │ │
  991. │ │
  992. └──────────────────┘", output);
  993. // top
  994. Assert.Equal (Point.Empty, top.ScreenToView (0, 0));
  995. top.Margin.ViewToScreen (0, 0, out int col, out int row);
  996. Assert.Equal (0, col);
  997. Assert.Equal (0, row);
  998. top.Border.ViewToScreen (0, 0, out col, out row);
  999. Assert.Equal (0, col);
  1000. Assert.Equal (0, row);
  1001. top.Padding.ViewToScreen (0, 0, out col, out row);
  1002. Assert.Equal (0, col);
  1003. Assert.Equal (0, row);
  1004. top.ViewToScreen (0, 0, out col, out row);
  1005. Assert.Equal (1, col);
  1006. Assert.Equal (1, row);
  1007. top.ViewToScreen (-1, -1, out col, out row);
  1008. Assert.Equal (0, col);
  1009. Assert.Equal (0, row);
  1010. Assert.Equal (top, View.FindDeepestView (top, 0, 0, out int rx, out int ry));
  1011. Assert.Equal (0, rx);
  1012. Assert.Equal (0, ry);
  1013. Assert.Equal (new Point (3, 2), top.ScreenToView (3, 2));
  1014. top.ViewToScreen (3, 2, out col, out row);
  1015. Assert.Equal (4, col);
  1016. Assert.Equal (3, row);
  1017. Assert.Equal (view, View.FindDeepestView (top, col, row, out rx, out ry));
  1018. Assert.Equal (0, rx);
  1019. Assert.Equal (0, ry);
  1020. Assert.Equal (top, View.FindDeepestView (top, 3, 2, out rx, out ry));
  1021. Assert.Equal (3, rx);
  1022. Assert.Equal (2, ry);
  1023. Assert.Equal (new Point (13, 2), top.ScreenToView (13, 2));
  1024. top.ViewToScreen (12, 2, out col, out row);
  1025. Assert.Equal (13, col);
  1026. Assert.Equal (3, row);
  1027. Assert.Equal (view, View.FindDeepestView (top, col, row, out rx, out ry));
  1028. Assert.Equal (9, rx);
  1029. Assert.Equal (0, ry);
  1030. top.ViewToScreen (13, 2, out col, out row);
  1031. Assert.Equal (14, col);
  1032. Assert.Equal (3, row);
  1033. Assert.Equal (top, View.FindDeepestView (top, 13, 2, out rx, out ry));
  1034. Assert.Equal (13, rx);
  1035. Assert.Equal (2, ry);
  1036. Assert.Equal (new Point (14, 3), top.ScreenToView (14, 3));
  1037. top.ViewToScreen (14, 3, out col, out row);
  1038. Assert.Equal (15, col);
  1039. Assert.Equal (4, row);
  1040. Assert.Equal (top, View.FindDeepestView (top, 14, 3, out rx, out ry));
  1041. Assert.Equal (14, rx);
  1042. Assert.Equal (3, ry);
  1043. // view
  1044. Assert.Equal (new Point (-4, -3), view.ScreenToView (0, 0));
  1045. view.Margin.ViewToScreen (-3, -2, out col, out row);
  1046. Assert.Equal (1, col);
  1047. Assert.Equal (1, row);
  1048. view.Border.ViewToScreen (-3, -2, out col, out row);
  1049. Assert.Equal (1, col);
  1050. Assert.Equal (1, row);
  1051. view.Padding.ViewToScreen (-3, -2, out col, out row);
  1052. Assert.Equal (1, col);
  1053. Assert.Equal (1, row);
  1054. view.ViewToScreen (-3, -2, out col, out row);
  1055. Assert.Equal (1, col);
  1056. Assert.Equal (1, row);
  1057. view.ViewToScreen (-4, -3, out col, out row);
  1058. Assert.Equal (0, col);
  1059. Assert.Equal (0, row);
  1060. Assert.Equal (top, View.FindDeepestView (top, 0, 0, out rx, out ry));
  1061. Assert.Equal (0, rx);
  1062. Assert.Equal (0, ry);
  1063. Assert.Equal (new Point (-1, -1), view.ScreenToView (3, 2));
  1064. view.ViewToScreen (0, 0, out col, out row);
  1065. Assert.Equal (4, col);
  1066. Assert.Equal (3, row);
  1067. Assert.Equal (view, View.FindDeepestView (top, 4, 3, out rx, out ry));
  1068. Assert.Equal (0, rx);
  1069. Assert.Equal (0, ry);
  1070. Assert.Equal (new Point (9, -1), view.ScreenToView (13, 2));
  1071. view.ViewToScreen (10, 0, out col, out row);
  1072. Assert.Equal (14, col);
  1073. Assert.Equal (3, row);
  1074. Assert.Equal (top, View.FindDeepestView (top, 14, 3, out rx, out ry));
  1075. Assert.Equal (14, rx);
  1076. Assert.Equal (3, ry);
  1077. Assert.Equal (new Point (10, 0), view.ScreenToView (14, 3));
  1078. view.ViewToScreen (11, 1, out col, out row);
  1079. Assert.Equal (15, col);
  1080. Assert.Equal (4, row);
  1081. Assert.Equal (top, View.FindDeepestView (top, 15, 4, out rx, out ry));
  1082. Assert.Equal (15, rx);
  1083. Assert.Equal (4, ry);
  1084. }
  1085. [Fact, AutoInitShutdown]
  1086. public void ScreenToView_ViewToScreen_FindDeepestView_Smaller_Top ()
  1087. {
  1088. var top = new Toplevel () { X = 3, Y = 2, Width = 20, Height = 10, BorderStyle = LineStyle.Single };
  1089. var view = new View () { X = 3, Y = 2, Width = 10, Height = 1, Text = "0123456789" };
  1090. top.Add (view);
  1091. Application.Begin (top);
  1092. Assert.Equal (Application.Current, top);
  1093. Assert.Equal (new Rect (0, 0, 80, 25), new Rect (0, 0, View.Driver.Cols, View.Driver.Rows));
  1094. Assert.NotEqual (new Rect (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  1095. Assert.Equal (new Rect (3, 2, 20, 10), top.Frame);
  1096. ((FakeDriver)Application.Driver).SetBufferSize (30, 20);
  1097. Assert.Equal (new Rect (0, 0, 30, 20), new Rect (0, 0, View.Driver.Cols, View.Driver.Rows));
  1098. Assert.NotEqual (new Rect (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  1099. Assert.Equal (new Rect (3, 2, 20, 10), top.Frame);
  1100. var frame = TestHelpers.AssertDriverContentsWithFrameAre (@"
  1101. ┌──────────────────┐
  1102. │ │
  1103. │ │
  1104. │ 0123456789 │
  1105. │ │
  1106. │ │
  1107. │ │
  1108. │ │
  1109. │ │
  1110. └──────────────────┘", output);
  1111. // mean the output started at col 3 and line 2
  1112. // which result with a width of 23 and a height of 10 on the output
  1113. Assert.Equal (new Rect (3, 2, 23, 10), frame);
  1114. // top
  1115. Assert.Equal (new Point (-3, -2), top.ScreenToView (0, 0));
  1116. top.Margin.ViewToScreen (-3, -2, out int col, out int row);
  1117. Assert.Equal (0, col);
  1118. Assert.Equal (0, row);
  1119. top.Border.ViewToScreen (-3, -2, out col, out row);
  1120. Assert.Equal (0, col);
  1121. Assert.Equal (0, row);
  1122. top.Padding.ViewToScreen (-3, -2, out col, out row);
  1123. Assert.Equal (0, col);
  1124. Assert.Equal (0, row);
  1125. top.ViewToScreen (-3, -2, out col, out row);
  1126. Assert.Equal (1, col);
  1127. Assert.Equal (1, row);
  1128. top.ViewToScreen (-4, -3, out col, out row);
  1129. Assert.Equal (0, col);
  1130. Assert.Equal (0, row);
  1131. Assert.Null (View.FindDeepestView (top, -4, -3, out int rx, out int ry));
  1132. Assert.Equal (0, rx);
  1133. Assert.Equal (0, ry);
  1134. Assert.Equal (Point.Empty, top.ScreenToView (3, 2));
  1135. top.ViewToScreen (0, 0, out col, out row);
  1136. Assert.Equal (4, col);
  1137. Assert.Equal (3, row);
  1138. Assert.Equal (top, View.FindDeepestView (top, 3, 2, out rx, out ry));
  1139. Assert.Equal (0, rx);
  1140. Assert.Equal (0, ry);
  1141. Assert.Equal (new Point (10, 0), top.ScreenToView (13, 2));
  1142. top.ViewToScreen (10, 0, out col, out row);
  1143. Assert.Equal (14, col);
  1144. Assert.Equal (3, row);
  1145. Assert.Equal (top, View.FindDeepestView (top, 13, 2, out rx, out ry));
  1146. Assert.Equal (10, rx);
  1147. Assert.Equal (0, ry);
  1148. Assert.Equal (new Point (11, 1), top.ScreenToView (14, 3));
  1149. top.ViewToScreen (11, 1, out col, out row);
  1150. Assert.Equal (15, col);
  1151. Assert.Equal (4, row);
  1152. Assert.Equal (top, View.FindDeepestView (top, 14, 3, out rx, out ry));
  1153. Assert.Equal (11, rx);
  1154. Assert.Equal (1, ry);
  1155. // view
  1156. Assert.Equal (new Point (-7, -5), view.ScreenToView (0, 0));
  1157. view.Margin.ViewToScreen (-6, -4, out col, out row);
  1158. Assert.Equal (1, col);
  1159. Assert.Equal (1, row);
  1160. view.Border.ViewToScreen (-6, -4, out col, out row);
  1161. Assert.Equal (1, col);
  1162. Assert.Equal (1, row);
  1163. view.Padding.ViewToScreen (-6, -4, out col, out row);
  1164. Assert.Equal (1, col);
  1165. Assert.Equal (1, row);
  1166. view.ViewToScreen (-6, -4, out col, out row);
  1167. Assert.Equal (1, col);
  1168. Assert.Equal (1, row);
  1169. Assert.Null (View.FindDeepestView (top, 1, 1, out rx, out ry));
  1170. Assert.Equal (0, rx);
  1171. Assert.Equal (0, ry);
  1172. Assert.Equal (new Point (-4, -3), view.ScreenToView (3, 2));
  1173. view.ViewToScreen (-3, -2, out col, out row);
  1174. Assert.Equal (4, col);
  1175. Assert.Equal (3, row);
  1176. Assert.Equal (top, View.FindDeepestView (top, 4, 3, out rx, out ry));
  1177. Assert.Equal (1, rx);
  1178. Assert.Equal (1, ry);
  1179. Assert.Equal (new Point (-1, -1), view.ScreenToView (6, 4));
  1180. view.ViewToScreen (0, 0, out col, out row);
  1181. Assert.Equal (7, col);
  1182. Assert.Equal (5, row);
  1183. Assert.Equal (view, View.FindDeepestView (top, 7, 5, out rx, out ry));
  1184. Assert.Equal (0, rx);
  1185. Assert.Equal (0, ry);
  1186. Assert.Equal (new Point (6, -1), view.ScreenToView (13, 4));
  1187. view.ViewToScreen (7, 0, out col, out row);
  1188. Assert.Equal (14, col);
  1189. Assert.Equal (5, row);
  1190. Assert.Equal (view, View.FindDeepestView (top, 14, 5, out rx, out ry));
  1191. Assert.Equal (7, rx);
  1192. Assert.Equal (0, ry);
  1193. Assert.Equal (new Point (7, -2), view.ScreenToView (14, 3));
  1194. view.ViewToScreen (8, -1, out col, out row);
  1195. Assert.Equal (15, col);
  1196. Assert.Equal (4, row);
  1197. Assert.Equal (top, View.FindDeepestView (top, 15, 4, out rx, out ry));
  1198. Assert.Equal (12, rx);
  1199. Assert.Equal (2, ry);
  1200. Assert.Equal (new Point (16, -2), view.ScreenToView (23, 3));
  1201. view.ViewToScreen (17, -1, out col, out row);
  1202. Assert.Equal (24, col);
  1203. Assert.Equal (4, row);
  1204. Assert.Null (View.FindDeepestView (top, 24, 4, out rx, out ry));
  1205. Assert.Equal (0, rx);
  1206. Assert.Equal (0, ry);
  1207. }
  1208. }
  1209. }