NavigationTests.cs 41 KB

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