ToplevelTests.cs 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  1. using System;
  2. using Xunit;
  3. using Xunit.Abstractions;
  4. namespace Terminal.Gui.ViewsTests;
  5. public class ToplevelTests {
  6. readonly ITestOutputHelper _output;
  7. public ToplevelTests (ITestOutputHelper output) => _output = output;
  8. [Fact]
  9. [AutoInitShutdown]
  10. public void Constructor_Default ()
  11. {
  12. var top = new Toplevel ();
  13. Assert.Equal (Colors.TopLevel, top.ColorScheme);
  14. Assert.Equal ("Fill(0)", top.Width.ToString ());
  15. Assert.Equal ("Fill(0)", top.Height.ToString ());
  16. Assert.False (top.Running);
  17. Assert.False (top.Modal);
  18. Assert.Null (top.MenuBar);
  19. Assert.Null (top.StatusBar);
  20. Assert.False (top.IsOverlappedContainer);
  21. Assert.False (top.IsOverlapped);
  22. }
  23. #if BROKE_IN_2927
  24. // BUGBUG: The name of this test does not match what it does.
  25. [Fact]
  26. [AutoInitShutdown]
  27. public void Application_Top_GetLocationThatFits_To_Driver_Rows_And_Cols ()
  28. {
  29. var iterations = 0;
  30. Application.Iteration += (s, a) => {
  31. switch (iterations) {
  32. case 0:
  33. Assert.False (Application.Top.AutoSize);
  34. Assert.Equal ("Top1", Application.Top.Text);
  35. Assert.Equal (0, Application.Top.Frame.X);
  36. Assert.Equal (0, Application.Top.Frame.Y);
  37. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  38. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  39. Application.OnKeyPressed (new (Key.CtrlMask | Key.R));
  40. break;
  41. case 1:
  42. Assert.Equal ("Top2", Application.Top.Text);
  43. Assert.Equal (0, Application.Top.Frame.X);
  44. Assert.Equal (0, Application.Top.Frame.Y);
  45. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  46. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  47. Application.OnKeyPressed (new (Key.CtrlMask | Key.C));
  48. break;
  49. case 3:
  50. Assert.Equal ("Top1", Application.Top.Text);
  51. Assert.Equal (0, Application.Top.Frame.X);
  52. Assert.Equal (0, Application.Top.Frame.Y);
  53. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  54. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  55. Application.OnKeyPressed (new (Key.CtrlMask | Key.R));
  56. break;
  57. case 4:
  58. Assert.Equal ("Top2", Application.Top.Text);
  59. Assert.Equal (0, Application.Top.Frame.X);
  60. Assert.Equal (0, Application.Top.Frame.Y);
  61. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  62. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  63. Application.OnKeyPressed (new (Key.CtrlMask | Key.C));
  64. break;
  65. case 6:
  66. Assert.Equal ("Top1", Application.Top.Text);
  67. Assert.Equal (0, Application.Top.Frame.X);
  68. Assert.Equal (0, Application.Top.Frame.Y);
  69. Assert.Equal (Application.Driver.Cols, Application.Top.Frame.Width);
  70. Assert.Equal (Application.Driver.Rows, Application.Top.Frame.Height);
  71. Application.OnKeyPressed (new (Key.CtrlMask | Key.Q));
  72. break;
  73. }
  74. iterations++;
  75. };
  76. Application.Run (Top1 ());
  77. Toplevel Top1 ()
  78. {
  79. var top = Application.Top;
  80. top.Text = "Top1";
  81. var menu = new MenuBar (new MenuBarItem [] {
  82. new MenuBarItem ("_Options", new MenuItem [] {
  83. new MenuItem ("_Run Top2", "", () => Application.Run (Top2 ()), null, null, Key.CtrlMask | Key.R),
  84. new MenuItem ("_Quit", "", () => Application.RequestStop(), null, null, Key.CtrlMask | Key.Q)
  85. })
  86. });
  87. top.Add (menu);
  88. var statusBar = new StatusBar (new [] {
  89. new StatusItem(Key.CtrlMask | Key.R, "~^R~ Run Top2", () => Application.Run (Top2 ())),
  90. new StatusItem(Application.QuitKey, $"{Application.QuitKey} to Quit", () => Application.RequestStop())
  91. });
  92. top.Add (statusBar);
  93. var t1 = new Toplevel ();
  94. top.Add (t1);
  95. return top;
  96. }
  97. Toplevel Top2 ()
  98. {
  99. var top = new Toplevel (Application.Top.Frame);
  100. top.Text = "Top2";
  101. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  102. var menu = new MenuBar (new MenuBarItem [] {
  103. new MenuBarItem ("_Stage", new MenuItem [] {
  104. new MenuItem ("_Close", "", () => Application.RequestStop(), null, null, Key.CtrlMask | Key.C)
  105. })
  106. });
  107. top.Add (menu);
  108. var statusBar = new StatusBar (new [] {
  109. new StatusItem(Key.CtrlMask | Key.C, "~^C~ Close", () => Application.RequestStop()),
  110. });
  111. top.Add (statusBar);
  112. win.Add (new ListView () {
  113. X = 0,
  114. Y = 0,
  115. Width = Dim.Fill (),
  116. Height = Dim.Fill ()
  117. });
  118. top.Add (win);
  119. return top;
  120. }
  121. }
  122. #endif
  123. [Fact]
  124. [AutoInitShutdown]
  125. public void Internal_Tests ()
  126. {
  127. var top = new Toplevel ();
  128. var eventInvoked = "";
  129. top.ChildUnloaded += (s, e) => eventInvoked = "ChildUnloaded";
  130. top.OnChildUnloaded (top);
  131. Assert.Equal ("ChildUnloaded", eventInvoked);
  132. top.ChildLoaded += (s, e) => eventInvoked = "ChildLoaded";
  133. top.OnChildLoaded (top);
  134. Assert.Equal ("ChildLoaded", eventInvoked);
  135. top.Closed += (s, e) => eventInvoked = "Closed";
  136. top.OnClosed (top);
  137. Assert.Equal ("Closed", eventInvoked);
  138. top.Closing += (s, e) => eventInvoked = "Closing";
  139. top.OnClosing (new ToplevelClosingEventArgs (top));
  140. Assert.Equal ("Closing", eventInvoked);
  141. top.AllChildClosed += (s, e) => eventInvoked = "AllChildClosed";
  142. top.OnAllChildClosed ();
  143. Assert.Equal ("AllChildClosed", eventInvoked);
  144. top.ChildClosed += (s, e) => eventInvoked = "ChildClosed";
  145. top.OnChildClosed (top);
  146. Assert.Equal ("ChildClosed", eventInvoked);
  147. top.Deactivate += (s, e) => eventInvoked = "Deactivate";
  148. top.OnDeactivate (top);
  149. Assert.Equal ("Deactivate", eventInvoked);
  150. top.Activate += (s, e) => eventInvoked = "Activate";
  151. top.OnActivate (top);
  152. Assert.Equal ("Activate", eventInvoked);
  153. top.Loaded += (s, e) => eventInvoked = "Loaded";
  154. top.OnLoaded ();
  155. Assert.Equal ("Loaded", eventInvoked);
  156. top.Ready += (s, e) => eventInvoked = "Ready";
  157. top.OnReady ();
  158. Assert.Equal ("Ready", eventInvoked);
  159. top.Unloaded += (s, e) => eventInvoked = "Unloaded";
  160. top.OnUnloaded ();
  161. Assert.Equal ("Unloaded", eventInvoked);
  162. top.AddMenuStatusBar (new MenuBar ());
  163. Assert.NotNull (top.MenuBar);
  164. top.AddMenuStatusBar (new StatusBar ());
  165. Assert.NotNull (top.StatusBar);
  166. top.RemoveMenuStatusBar (top.MenuBar);
  167. Assert.Null (top.MenuBar);
  168. top.RemoveMenuStatusBar (top.StatusBar);
  169. Assert.Null (top.StatusBar);
  170. Application.Begin (top);
  171. Assert.Equal (top, Application.Top);
  172. // Application.Top without menu and status bar.
  173. var supView = top.GetLocationThatFits (top, 2, 2, out var nx, out var ny, out var mb, out var sb);
  174. Assert.Equal (Application.Top, supView);
  175. Assert.Equal (0, nx);
  176. Assert.Equal (0, ny);
  177. Assert.Null (mb);
  178. Assert.Null (sb);
  179. top.AddMenuStatusBar (new MenuBar ());
  180. Assert.NotNull (top.MenuBar);
  181. // Application.Top with a menu and without status bar.
  182. top.GetLocationThatFits (top, 2, 2, out nx, out ny, out mb, out sb);
  183. Assert.Equal (0, nx);
  184. Assert.Equal (1, ny);
  185. Assert.NotNull (mb);
  186. Assert.Null (sb);
  187. top.AddMenuStatusBar (new StatusBar ());
  188. Assert.NotNull (top.StatusBar);
  189. // Application.Top with a menu and status bar.
  190. top.GetLocationThatFits (top, 2, 2, out nx, out ny, out mb, out sb);
  191. Assert.Equal (0, nx);
  192. // The available height is lower than the Application.Top height minus
  193. // the menu bar and status bar, then the top can go beyond the bottom
  194. Assert.Equal (2, ny);
  195. Assert.NotNull (mb);
  196. Assert.NotNull (sb);
  197. top.RemoveMenuStatusBar (top.MenuBar);
  198. Assert.Null (top.MenuBar);
  199. // Application.Top without a menu and with a status bar.
  200. top.GetLocationThatFits (top, 2, 2, out nx, out ny, out mb, out sb);
  201. Assert.Equal (0, nx);
  202. // The available height is lower than the Application.Top height minus
  203. // the status bar, then the top can go beyond the bottom
  204. Assert.Equal (2, ny);
  205. Assert.Null (mb);
  206. Assert.NotNull (sb);
  207. top.RemoveMenuStatusBar (top.StatusBar);
  208. Assert.Null (top.StatusBar);
  209. Assert.Null (top.MenuBar);
  210. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  211. top.Add (win);
  212. top.LayoutSubviews ();
  213. // The SuperView is always the same regardless of the caller.
  214. supView = top.GetLocationThatFits (win, 0, 0, out nx, out ny, out mb, out sb);
  215. Assert.Equal (Application.Top, supView);
  216. supView = win.GetLocationThatFits (win, 0, 0, out nx, out ny, out mb, out sb);
  217. Assert.Equal (Application.Top, supView);
  218. // Application.Top without menu and status bar.
  219. top.GetLocationThatFits (win, 0, 0, out nx, out ny, out mb, out sb);
  220. Assert.Equal (0, nx);
  221. Assert.Equal (0, ny);
  222. Assert.Null (mb);
  223. Assert.Null (sb);
  224. top.AddMenuStatusBar (new MenuBar ());
  225. Assert.NotNull (top.MenuBar);
  226. // Application.Top with a menu and without status bar.
  227. top.GetLocationThatFits (win, 2, 2, out nx, out ny, out mb, out sb);
  228. Assert.Equal (0, nx);
  229. Assert.Equal (1, ny);
  230. Assert.NotNull (mb);
  231. Assert.Null (sb);
  232. top.AddMenuStatusBar (new StatusBar ());
  233. Assert.NotNull (top.StatusBar);
  234. // Application.Top with a menu and status bar.
  235. top.GetLocationThatFits (win, 30, 20, out nx, out ny, out mb, out sb);
  236. Assert.Equal (0, nx);
  237. // The available height is lower than the Application.Top height minus
  238. // the menu bar and status bar, then the top can go beyond the bottom
  239. Assert.Equal (20, ny);
  240. Assert.NotNull (mb);
  241. Assert.NotNull (sb);
  242. top.RemoveMenuStatusBar (top.MenuBar);
  243. top.RemoveMenuStatusBar (top.StatusBar);
  244. Assert.Null (top.StatusBar);
  245. Assert.Null (top.MenuBar);
  246. top.Remove (win);
  247. win = new Window { Width = 60, Height = 15 };
  248. top.Add (win);
  249. // Application.Top without menu and status bar.
  250. top.GetLocationThatFits (win, 0, 0, out nx, out ny, out mb, out sb);
  251. Assert.Equal (0, nx);
  252. Assert.Equal (0, ny);
  253. Assert.Null (mb);
  254. Assert.Null (sb);
  255. top.AddMenuStatusBar (new MenuBar ());
  256. Assert.NotNull (top.MenuBar);
  257. // Application.Top with a menu and without status bar.
  258. top.GetLocationThatFits (win, 2, 2, out nx, out ny, out mb, out sb);
  259. Assert.Equal (2, nx);
  260. Assert.Equal (2, ny);
  261. Assert.NotNull (mb);
  262. Assert.Null (sb);
  263. top.AddMenuStatusBar (new StatusBar ());
  264. Assert.NotNull (top.StatusBar);
  265. // Application.Top with a menu and status bar.
  266. top.GetLocationThatFits (win, 30, 20, out nx, out ny, out mb, out sb);
  267. Assert.Equal (20, nx); // 20+60=80
  268. Assert.Equal (9, ny); // 9+15+1(mb)=25
  269. Assert.NotNull (mb);
  270. Assert.NotNull (sb);
  271. top.PositionToplevels ();
  272. Assert.Equal (new Rect (0, 1, 60, 15), win.Frame);
  273. Assert.Null (Toplevel._dragPosition);
  274. win.MouseEvent (new MouseEvent { X = 6, Y = 0, Flags = MouseFlags.Button1Pressed });
  275. Assert.Equal (new Point (6, 0), Toplevel._dragPosition);
  276. win.MouseEvent (new MouseEvent { X = 6, Y = 0, Flags = MouseFlags.Button1Released });
  277. Assert.Null (Toplevel._dragPosition);
  278. win.CanFocus = false;
  279. win.MouseEvent (new MouseEvent { X = 6, Y = 0, Flags = MouseFlags.Button1Pressed });
  280. Assert.Null (Toplevel._dragPosition);
  281. }
  282. [Fact]
  283. [AutoInitShutdown]
  284. public void KeyBindings_Command ()
  285. {
  286. var isRunning = false;
  287. var win1 = new Window { Id = "win1", Width = Dim.Percent (50f), Height = Dim.Fill () };
  288. var lblTf1W1 = new Label ("Enter text in TextField on Win1:") { Id = "lblTf1W1" };
  289. var tf1W1 = new TextField ("Text1 on Win1") { Id = "tf1W1", X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill () };
  290. var lblTvW1 = new Label ("Enter text in TextView on Win1:") { Id = "lblTvW1", Y = Pos.Bottom (lblTf1W1) + 1 };
  291. var tvW1 = new TextView { Id = "tvW1", X = Pos.Left (tf1W1), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win1" };
  292. var lblTf2W1 = new Label ("Enter text in TextField on Win1:") { Id = "lblTf2W1", Y = Pos.Bottom (lblTvW1) + 1 };
  293. var tf2W1 = new TextField ("Text2 on Win1") { Id = "tf2W1", X = Pos.Left (tf1W1), Width = Dim.Fill () };
  294. win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
  295. var win2 = new Window { Id = "win2", X = Pos.Right (win1) + 1, Width = Dim.Percent (50f), Height = Dim.Fill () };
  296. var lblTf1W2 = new Label ("Enter text in TextField on Win2:") { Id = "lblTf1W2" };
  297. var tf1W2 = new TextField ("Text1 on Win2") { Id = "tf1W2", X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill () };
  298. var lblTvW2 = new Label ("Enter text in TextView on Win2:") { Id = "lblTvW2", Y = Pos.Bottom (lblTf1W2) + 1 };
  299. var tvW2 = new TextView { Id = "tvW2", X = Pos.Left (tf1W2), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win2" };
  300. var lblTf2W2 = new Label ("Enter text in TextField on Win2:") { Id = "lblTf2W2", Y = Pos.Bottom (lblTvW2) + 1 };
  301. var tf2W2 = new TextField ("Text2 on Win2") { Id = "tf2W2", X = Pos.Left (tf1W2), Width = Dim.Fill () };
  302. win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
  303. var top = Application.Top;
  304. top.Add (win1, win2);
  305. top.Loaded += (s, e) => isRunning = true;
  306. top.Closing += (s, e) => isRunning = false;
  307. Application.Begin (top);
  308. top.Running = true;
  309. Assert.Equal (new Rect (0, 0, 40, 25), win1.Frame);
  310. Assert.Equal (new Rect (41, 0, 40, 25), win2.Frame);
  311. Assert.Equal (win1, top.Focused);
  312. Assert.Equal (tf1W1, top.MostFocused);
  313. Assert.True (isRunning);
  314. Assert.True (Application.OnKeyDown (Application.QuitKey));
  315. Assert.False (isRunning);
  316. Assert.True (Application.OnKeyDown (new Key (KeyCode.Z | KeyCode.CtrlMask)));
  317. Assert.True (Application.OnKeyDown (new Key (KeyCode.F5))); // refresh
  318. Assert.True (Application.OnKeyDown (new Key (KeyCode.Tab)));
  319. Assert.Equal (win1, top.Focused);
  320. Assert.Equal (tvW1, top.MostFocused);
  321. Assert.True (Application.OnKeyDown (new Key (KeyCode.Tab)));
  322. Assert.Equal ($"\tFirst line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  323. Assert.True (Application.OnKeyDown (new Key (KeyCode.Tab | KeyCode.ShiftMask)));
  324. Assert.Equal ($"First line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  325. Assert.True (Application.OnKeyDown (new Key (KeyCode.Tab | KeyCode.CtrlMask)));
  326. Assert.Equal (win1, top.Focused);
  327. Assert.Equal (tf2W1, top.MostFocused);
  328. Assert.True (Application.OnKeyDown (new Key (KeyCode.Tab)));
  329. Assert.Equal (win1, top.Focused);
  330. Assert.Equal (tf1W1, top.MostFocused);
  331. Assert.True (Application.OnKeyDown (new Key (KeyCode.CursorRight)));
  332. Assert.Equal (win1, top.Focused);
  333. Assert.Equal (tf1W1, top.MostFocused);
  334. Assert.True (Application.OnKeyDown (new Key (KeyCode.CursorDown)));
  335. Assert.Equal (win1, top.Focused);
  336. Assert.Equal (tvW1, top.MostFocused);
  337. #if UNIX_KEY_BINDINGS
  338. Assert.True (Application.OnKeyDown (new (Key.I | Key.CtrlMask)));
  339. Assert.Equal (win1, top.Focused);
  340. Assert.Equal (tf2W1, top.MostFocused);
  341. #endif
  342. Assert.True (Application.OnKeyDown (new Key (KeyCode.Tab | KeyCode.ShiftMask)));
  343. Assert.Equal (win1, top.Focused);
  344. Assert.Equal (tvW1, top.MostFocused);
  345. Assert.True (Application.OnKeyDown (new Key (KeyCode.CursorLeft)));
  346. Assert.Equal (win1, top.Focused);
  347. Assert.Equal (tf1W1, top.MostFocused);
  348. Assert.True (Application.OnKeyDown (new Key (KeyCode.CursorUp)));
  349. Assert.Equal (win1, top.Focused);
  350. Assert.Equal (tf2W1, top.MostFocused);
  351. Assert.True (Application.OnKeyDown (new Key (KeyCode.Tab | KeyCode.CtrlMask)));
  352. Assert.Equal (win2, top.Focused);
  353. Assert.Equal (tf1W2, top.MostFocused);
  354. Assert.True (Application.OnKeyDown (new Key (KeyCode.Tab | KeyCode.CtrlMask | KeyCode.ShiftMask)));
  355. Assert.Equal (win1, top.Focused);
  356. Assert.Equal (tf2W1, top.MostFocused);
  357. Assert.True (Application.OnKeyDown (Application.AlternateForwardKey));
  358. Assert.Equal (win2, top.Focused);
  359. Assert.Equal (tf1W2, top.MostFocused);
  360. Assert.True (Application.OnKeyDown (Application.AlternateBackwardKey));
  361. Assert.Equal (win1, top.Focused);
  362. Assert.Equal (tf2W1, top.MostFocused);
  363. Assert.True (Application.OnKeyDown (new Key (KeyCode.CursorUp)));
  364. Assert.Equal (win1, top.Focused);
  365. Assert.Equal (tvW1, top.MostFocused);
  366. #if UNIX_KEY_BINDINGS
  367. Assert.True (Application.OnKeyDown (new (Key.B | Key.CtrlMask)));
  368. #else
  369. Assert.True (Application.OnKeyDown (new Key (KeyCode.CursorLeft)));
  370. #endif
  371. Assert.Equal (win1, top.Focused);
  372. Assert.Equal (tf1W1, top.MostFocused);
  373. Assert.True (Application.OnKeyDown (new Key (KeyCode.CursorDown)));
  374. Assert.Equal (win1, top.Focused);
  375. Assert.Equal (tvW1, top.MostFocused);
  376. Assert.Equal (new Point (0, 0), tvW1.CursorPosition);
  377. Assert.True (Application.OnKeyDown (new Key (KeyCode.End | KeyCode.CtrlMask)));
  378. Assert.Equal (win1, top.Focused);
  379. Assert.Equal (tvW1, top.MostFocused);
  380. Assert.Equal (new Point (16, 1), tvW1.CursorPosition);
  381. #if UNIX_KEY_BINDINGS
  382. Assert.True (Application.OnKeyDown (new (Key.F | Key.CtrlMask)));
  383. #else
  384. Assert.True (Application.OnKeyDown (new Key (KeyCode.CursorRight)));
  385. #endif
  386. Assert.Equal (win1, top.Focused);
  387. Assert.Equal (tf2W1, top.MostFocused);
  388. #if UNIX_KEY_BINDINGS
  389. Assert.True (Application.OnKeyDown (new (Key.L | Key.CtrlMask)));
  390. #else
  391. Assert.True (Application.OnKeyDown (new Key (KeyCode.F5)));
  392. #endif
  393. }
  394. [Fact]
  395. [AutoInitShutdown]
  396. public void KeyBindings_Command_With_OverlappedTop ()
  397. {
  398. var top = Application.Top;
  399. Assert.Null (Application.OverlappedTop);
  400. top.IsOverlappedContainer = true;
  401. Application.Begin (top);
  402. Assert.Equal (Application.Top, Application.OverlappedTop);
  403. var isRunning = true;
  404. var win1 = new Window { Id = "win1", Width = Dim.Percent (50f), Height = Dim.Fill () };
  405. var lblTf1W1 = new Label ("Enter text in TextField on Win1:");
  406. var tf1W1 = new TextField ("Text1 on Win1") { X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill () };
  407. var lblTvW1 = new Label ("Enter text in TextView on Win1:") { Y = Pos.Bottom (lblTf1W1) + 1 };
  408. var tvW1 = new TextView { X = Pos.Left (tf1W1), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win1" };
  409. var lblTf2W1 = new Label ("Enter text in TextField on Win1:") { Y = Pos.Bottom (lblTvW1) + 1 };
  410. var tf2W1 = new TextField ("Text2 on Win1") { X = Pos.Left (tf1W1), Width = Dim.Fill () };
  411. win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
  412. var win2 = new Window { Id = "win2", Width = Dim.Percent (50f), Height = Dim.Fill () };
  413. var lblTf1W2 = new Label ("Enter text in TextField on Win2:");
  414. var tf1W2 = new TextField ("Text1 on Win2") { X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill () };
  415. var lblTvW2 = new Label ("Enter text in TextView on Win2:") { Y = Pos.Bottom (lblTf1W2) + 1 };
  416. var tvW2 = new TextView { X = Pos.Left (tf1W2), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win2" };
  417. var lblTf2W2 = new Label ("Enter text in TextField on Win2:") { Y = Pos.Bottom (lblTvW2) + 1 };
  418. var tf2W2 = new TextField ("Text2 on Win2") { X = Pos.Left (tf1W2), Width = Dim.Fill () };
  419. win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
  420. win1.Closing += (s, e) => isRunning = false;
  421. Assert.Null (top.Focused);
  422. Assert.Equal (top, Application.Current);
  423. Assert.True (top.IsCurrentTop);
  424. Assert.Equal (top, Application.OverlappedTop);
  425. Application.Begin (win1);
  426. Assert.Equal (new Rect (0, 0, 40, 25), win1.Frame);
  427. Assert.NotEqual (top, Application.Current);
  428. Assert.False (top.IsCurrentTop);
  429. Assert.Equal (win1, Application.Current);
  430. Assert.True (win1.IsCurrentTop);
  431. Assert.True (win1.IsOverlapped);
  432. Assert.Null (top.Focused);
  433. Assert.Null (top.MostFocused);
  434. Assert.Equal (tf1W1, win1.MostFocused);
  435. Assert.True (win1.IsOverlapped);
  436. Assert.Single (Application.OverlappedChildren);
  437. Application.Begin (win2);
  438. Assert.Equal (new Rect (0, 0, 40, 25), win2.Frame);
  439. Assert.NotEqual (top, Application.Current);
  440. Assert.False (top.IsCurrentTop);
  441. Assert.Equal (win2, Application.Current);
  442. Assert.True (win2.IsCurrentTop);
  443. Assert.True (win2.IsOverlapped);
  444. Assert.Null (top.Focused);
  445. Assert.Null (top.MostFocused);
  446. Assert.Equal (tf1W2, win2.MostFocused);
  447. Assert.Equal (2, Application.OverlappedChildren.Count);
  448. Application.MoveToOverlappedChild (win1);
  449. Assert.Equal (win1, Application.Current);
  450. Assert.Equal (win1, Application.OverlappedChildren [0]);
  451. win1.Running = true;
  452. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (Application.QuitKey));
  453. Assert.False (isRunning);
  454. Assert.False (win1.Running);
  455. Assert.Equal (win1, Application.OverlappedChildren [0]);
  456. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (new Key (KeyCode.Z | KeyCode.CtrlMask)));
  457. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (new Key (KeyCode.F5))); // refresh
  458. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (new Key (KeyCode.Tab)));
  459. Assert.True (win1.IsCurrentTop);
  460. Assert.Equal (tvW1, win1.MostFocused);
  461. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (new Key (KeyCode.Tab)));
  462. Assert.Equal ($"\tFirst line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  463. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (new Key (KeyCode.Tab | KeyCode.ShiftMask)));
  464. Assert.Equal ($"First line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  465. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (new Key (KeyCode.Tab | KeyCode.CtrlMask)));
  466. Assert.Equal (win1, Application.OverlappedChildren [0]);
  467. Assert.Equal (tf2W1, win1.MostFocused);
  468. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (new Key (KeyCode.Tab)));
  469. Assert.Equal (win1, Application.OverlappedChildren [0]);
  470. Assert.Equal (tf1W1, win1.MostFocused);
  471. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (new Key (KeyCode.CursorRight)));
  472. Assert.Equal (win1, Application.OverlappedChildren [0]);
  473. Assert.Equal (tf1W1, win1.MostFocused);
  474. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (new Key (KeyCode.CursorDown)));
  475. Assert.Equal (win1, Application.OverlappedChildren [0]);
  476. Assert.Equal (tvW1, win1.MostFocused);
  477. #if UNIX_KEY_BINDINGS
  478. Assert.True (Application.OverlappedChildren [0].ProcessKeyDown (new (Key.I | Key.CtrlMask)));
  479. Assert.Equal (win1, Application.OverlappedChildren [0]);
  480. Assert.Equal (tf2W1, win1.MostFocused);
  481. #endif
  482. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (new Key (KeyCode.Tab | KeyCode.ShiftMask)));
  483. Assert.Equal (win1, Application.OverlappedChildren [0]);
  484. Assert.Equal (tvW1, win1.MostFocused);
  485. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (new Key (KeyCode.CursorLeft)));
  486. Assert.Equal (win1, Application.OverlappedChildren [0]);
  487. Assert.Equal (tf1W1, win1.MostFocused);
  488. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (new Key (KeyCode.CursorUp)));
  489. Assert.Equal (win1, Application.OverlappedChildren [0]);
  490. Assert.Equal (tf2W1, win1.MostFocused);
  491. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (new Key (KeyCode.Tab)));
  492. Assert.Equal (win1, Application.OverlappedChildren [0]);
  493. Assert.Equal (tf1W1, win1.MostFocused);
  494. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (new Key (KeyCode.Tab | KeyCode.CtrlMask)));
  495. Assert.Equal (win2, Application.OverlappedChildren [0]);
  496. Assert.Equal (tf1W2, win2.MostFocused);
  497. tf2W2.SetFocus ();
  498. Assert.True (tf2W2.HasFocus);
  499. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (new Key (KeyCode.Tab | KeyCode.CtrlMask | KeyCode.ShiftMask)));
  500. Assert.Equal (win1, Application.OverlappedChildren [0]);
  501. Assert.Equal (tf1W1, win1.MostFocused);
  502. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (Application.AlternateForwardKey));
  503. Assert.Equal (win2, Application.OverlappedChildren [0]);
  504. Assert.Equal (tf2W2, win2.MostFocused);
  505. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (Application.AlternateBackwardKey));
  506. Assert.Equal (win1, Application.OverlappedChildren [0]);
  507. Assert.Equal (tf1W1, win1.MostFocused);
  508. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (new Key (KeyCode.CursorDown)));
  509. Assert.Equal (win1, Application.OverlappedChildren [0]);
  510. Assert.Equal (tvW1, win1.MostFocused);
  511. #if UNIX_KEY_BINDINGS
  512. Assert.True (Application.OverlappedChildren [0].ProcessKeyDown (new (Key.B | Key.CtrlMask)));
  513. #else
  514. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (new Key (KeyCode.CursorLeft)));
  515. #endif
  516. Assert.Equal (win1, Application.OverlappedChildren [0]);
  517. Assert.Equal (tf1W1, win1.MostFocused);
  518. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (new Key (KeyCode.CursorDown)));
  519. Assert.Equal (win1, Application.OverlappedChildren [0]);
  520. Assert.Equal (tvW1, win1.MostFocused);
  521. Assert.Equal (new Point (0, 0), tvW1.CursorPosition);
  522. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (new Key (KeyCode.End | KeyCode.CtrlMask)));
  523. Assert.Equal (win1, Application.OverlappedChildren [0]);
  524. Assert.Equal (tvW1, win1.MostFocused);
  525. Assert.Equal (new Point (16, 1), tvW1.CursorPosition);
  526. #if UNIX_KEY_BINDINGS
  527. Assert.True (Application.OverlappedChildren [0].ProcessKeyDown (new (Key.F | Key.CtrlMask)));
  528. #else
  529. Assert.True (Application.OverlappedChildren [0].NewKeyDownEvent (new Key (KeyCode.CursorRight)));
  530. #endif
  531. Assert.Equal (win1, Application.OverlappedChildren [0]);
  532. Assert.Equal (tf2W1, win1.MostFocused);
  533. #if UNIX_KEY_BINDINGS
  534. Assert.True (Application.OverlappedChildren [0].ProcessKeyDown (new (Key.L | Key.CtrlMask)));
  535. #endif
  536. }
  537. [Fact]
  538. public void Added_Event_Should_Not_Be_Used_To_Initialize_Toplevel_Events ()
  539. {
  540. Key alternateForwardKey = default;
  541. Key alternateBackwardKey = default;
  542. Key quitKey = default;
  543. var wasAdded = false;
  544. var view = new View ();
  545. view.Added += View_Added;
  546. void View_Added (object sender, SuperViewChangedEventArgs e)
  547. {
  548. Assert.Throws<NullReferenceException> (() => Application.Top.AlternateForwardKeyChanged += (s, e) => alternateForwardKey = (KeyCode)e.OldKey);
  549. Assert.Throws<NullReferenceException> (() => Application.Top.AlternateBackwardKeyChanged += (s, e) => alternateBackwardKey = (KeyCode)e.OldKey);
  550. Assert.Throws<NullReferenceException> (() => Application.Top.QuitKeyChanged += (s, e) => quitKey = (KeyCode)e.OldKey);
  551. Assert.False (wasAdded);
  552. wasAdded = true;
  553. view.Added -= View_Added;
  554. }
  555. var win = new Window ();
  556. win.Add (view);
  557. Application.Init (new FakeDriver ());
  558. var top = Application.Top;
  559. top.Add (win);
  560. Assert.True (wasAdded);
  561. Application.Shutdown ();
  562. }
  563. [Fact]
  564. [AutoInitShutdown]
  565. public void AlternateForwardKeyChanged_AlternateBackwardKeyChanged_QuitKeyChanged_Events ()
  566. {
  567. Key alternateForwardKey = KeyCode.Null;
  568. Key alternateBackwardKey = KeyCode.Null;
  569. Key quitKey = KeyCode.Null;
  570. var view = new View ();
  571. view.Initialized += View_Initialized;
  572. void View_Initialized (object sender, EventArgs e)
  573. {
  574. Application.Top.AlternateForwardKeyChanged += (s, e) => alternateForwardKey = e.OldKey;
  575. Application.Top.AlternateBackwardKeyChanged += (s, e) => alternateBackwardKey = e.OldKey;
  576. Application.Top.QuitKeyChanged += (s, e) => quitKey = e.OldKey;
  577. }
  578. var win = new Window ();
  579. win.Add (view);
  580. var top = Application.Top;
  581. top.Add (win);
  582. Application.Begin (top);
  583. Assert.Equal (KeyCode.Null, alternateForwardKey);
  584. Assert.Equal (KeyCode.Null, alternateBackwardKey);
  585. Assert.Equal (KeyCode.Null, quitKey);
  586. Assert.Equal (KeyCode.PageDown | KeyCode.CtrlMask, Application.AlternateForwardKey);
  587. Assert.Equal (KeyCode.PageUp | KeyCode.CtrlMask, Application.AlternateBackwardKey);
  588. Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, Application.QuitKey);
  589. Application.AlternateForwardKey = KeyCode.A;
  590. Application.AlternateBackwardKey = KeyCode.B;
  591. Application.QuitKey = KeyCode.C;
  592. Assert.Equal (KeyCode.PageDown | KeyCode.CtrlMask, alternateForwardKey);
  593. Assert.Equal (KeyCode.PageUp | KeyCode.CtrlMask, alternateBackwardKey);
  594. Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, quitKey);
  595. Assert.Equal (KeyCode.A, Application.AlternateForwardKey);
  596. Assert.Equal (KeyCode.B, Application.AlternateBackwardKey);
  597. Assert.Equal (KeyCode.C, Application.QuitKey);
  598. // Replacing the defaults keys to avoid errors on others unit tests that are using it.
  599. Application.AlternateForwardKey = KeyCode.PageDown | KeyCode.CtrlMask;
  600. Application.AlternateBackwardKey = KeyCode.PageUp | KeyCode.CtrlMask;
  601. Application.QuitKey = KeyCode.Q | KeyCode.CtrlMask;
  602. Assert.Equal (KeyCode.PageDown | KeyCode.CtrlMask, Application.AlternateForwardKey);
  603. Assert.Equal (KeyCode.PageUp | KeyCode.CtrlMask, Application.AlternateBackwardKey);
  604. Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, Application.QuitKey);
  605. }
  606. [Fact]
  607. [AutoInitShutdown]
  608. public void Mouse_Drag_On_Top_With_Superview_Null ()
  609. {
  610. var win = new Window ();
  611. var top = Application.Top;
  612. top.Add (win);
  613. var iterations = -1;
  614. Window testWindow;
  615. Application.Iteration += (s, a) => {
  616. iterations++;
  617. if (iterations == 0) {
  618. ((FakeDriver)Application.Driver).SetBufferSize (15, 7);
  619. // Don't use MessageBox here; it's too complicated for this unit test; just use Window
  620. testWindow = new Window {
  621. Text = "Hello",
  622. X = 2,
  623. Y = 2,
  624. Width = 10,
  625. Height = 3
  626. };
  627. Application.Run (testWindow);
  628. } else if (iterations == 1) {
  629. TestHelpers.AssertDriverContentsWithFrameAre (@"
  630. ┌─────────────┐
  631. │ │
  632. │ ┌────────┐ │
  633. │ │Hello │ │
  634. │ └────────┘ │
  635. │ │
  636. └─────────────┘
  637. ", _output);
  638. } else if (iterations == 2) {
  639. Assert.Null (Application.MouseGrabView);
  640. // Grab the mouse
  641. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  642. X = 3,
  643. Y = 2,
  644. Flags = MouseFlags.Button1Pressed
  645. }));
  646. Assert.Equal (Application.Current, Application.MouseGrabView);
  647. Assert.Equal (new Rect (2, 2, 10, 3), Application.MouseGrabView.Frame);
  648. } else if (iterations == 3) {
  649. Assert.Equal (Application.Current, Application.MouseGrabView);
  650. // Drag to left
  651. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  652. X = 2,
  653. Y = 2,
  654. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  655. }));
  656. Application.Refresh ();
  657. Assert.Equal (Application.Current, Application.MouseGrabView);
  658. Assert.Equal (new Rect (1, 2, 10, 3), Application.MouseGrabView.Frame);
  659. } else if (iterations == 4) {
  660. Assert.Equal (Application.Current, Application.MouseGrabView);
  661. TestHelpers.AssertDriverContentsWithFrameAre (@"
  662. ┌─────────────┐
  663. │ │
  664. │┌────────┐ │
  665. ││Hello │ │
  666. │└────────┘ │
  667. │ │
  668. └─────────────┘", _output);
  669. Assert.Equal (Application.Current, Application.MouseGrabView);
  670. } else if (iterations == 5) {
  671. Assert.Equal (Application.Current, Application.MouseGrabView);
  672. // Drag up
  673. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  674. X = 2,
  675. Y = 1,
  676. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  677. }));
  678. Application.Refresh ();
  679. Assert.Equal (Application.Current, Application.MouseGrabView);
  680. Assert.Equal (new Rect (1, 1, 10, 3), Application.MouseGrabView.Frame);
  681. } else if (iterations == 6) {
  682. Assert.Equal (Application.Current, Application.MouseGrabView);
  683. TestHelpers.AssertDriverContentsWithFrameAre (@"
  684. ┌─────────────┐
  685. │┌────────┐ │
  686. ││Hello │ │
  687. │└────────┘ │
  688. │ │
  689. │ │
  690. └─────────────┘", _output);
  691. Assert.Equal (Application.Current, Application.MouseGrabView);
  692. Assert.Equal (new Rect (1, 1, 10, 3), Application.MouseGrabView.Frame);
  693. } else if (iterations == 7) {
  694. Assert.Equal (Application.Current, Application.MouseGrabView);
  695. // Ungrab the mouse
  696. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  697. X = 2,
  698. Y = 1,
  699. Flags = MouseFlags.Button1Released
  700. }));
  701. Application.Refresh ();
  702. Assert.Null (Application.MouseGrabView);
  703. } else if (iterations == 8) {
  704. Application.RequestStop ();
  705. } else if (iterations == 9) {
  706. Application.RequestStop ();
  707. }
  708. };
  709. Application.Run ();
  710. }
  711. [Fact]
  712. [AutoInitShutdown]
  713. public void Mouse_Drag_On_Top_With_Superview_Not_Null ()
  714. {
  715. var win = new Window {
  716. X = 3,
  717. Y = 2,
  718. Width = 10,
  719. Height = 5
  720. };
  721. var top = Application.Top;
  722. top.Add (win);
  723. var iterations = -1;
  724. var movex = 0;
  725. var movey = 0;
  726. var location = new Rect (win.Frame.X, win.Frame.Y, 7, 3);
  727. Application.Iteration += (s, a) => {
  728. iterations++;
  729. if (iterations == 0) {
  730. ((FakeDriver)Application.Driver).SetBufferSize (30, 10);
  731. } else if (iterations == 1) {
  732. location = win.Frame;
  733. Assert.Null (Application.MouseGrabView);
  734. // Grab the mouse
  735. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  736. X = win.Frame.X,
  737. Y = win.Frame.Y,
  738. Flags = MouseFlags.Button1Pressed
  739. }));
  740. Assert.Equal (win, Application.MouseGrabView);
  741. Assert.Equal (location, Application.MouseGrabView.Frame);
  742. } else if (iterations == 2) {
  743. Assert.Equal (win, Application.MouseGrabView);
  744. // Drag to left
  745. movex = 1;
  746. movey = 0;
  747. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  748. X = win.Frame.X + movex,
  749. Y = win.Frame.Y + movey,
  750. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  751. }));
  752. Assert.Equal (win, Application.MouseGrabView);
  753. } else if (iterations == 3) {
  754. // we should have moved +1, +0
  755. Assert.Equal (win, Application.MouseGrabView);
  756. Assert.Equal (win, Application.MouseGrabView);
  757. location.Offset (movex, movey);
  758. Assert.Equal (location, Application.MouseGrabView.Frame);
  759. } else if (iterations == 4) {
  760. Assert.Equal (win, Application.MouseGrabView);
  761. // Drag up
  762. movex = 0;
  763. movey = -1;
  764. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  765. X = win.Frame.X + movex,
  766. Y = win.Frame.Y + movey,
  767. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  768. }));
  769. Assert.Equal (win, Application.MouseGrabView);
  770. } else if (iterations == 5) {
  771. // we should have moved +0, -1
  772. Assert.Equal (win, Application.MouseGrabView);
  773. location.Offset (movex, movey);
  774. Assert.Equal (location, Application.MouseGrabView.Frame);
  775. } else if (iterations == 6) {
  776. Assert.Equal (win, Application.MouseGrabView);
  777. // Ungrab the mouse
  778. movex = 0;
  779. movey = 0;
  780. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  781. X = win.Frame.X + movex,
  782. Y = win.Frame.Y + movey,
  783. Flags = MouseFlags.Button1Released
  784. }));
  785. Assert.Null (Application.MouseGrabView);
  786. } else if (iterations == 7) {
  787. Application.RequestStop ();
  788. }
  789. };
  790. Application.Run ();
  791. }
  792. [Fact]
  793. [AutoInitShutdown]
  794. public void GetLocationThatFits_With_Border_Null_Not_Throws ()
  795. {
  796. var top = new Toplevel ();
  797. Application.Begin (top);
  798. var exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (0, 10));
  799. Assert.Null (exception);
  800. exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (10, 0));
  801. Assert.Null (exception);
  802. }
  803. [Fact]
  804. [AutoInitShutdown]
  805. public void OnEnter_OnLeave_Triggered_On_Application_Begin_End ()
  806. {
  807. var isEnter = false;
  808. var isLeave = false;
  809. var v = new View ();
  810. v.Enter += (s, _) => isEnter = true;
  811. v.Leave += (s, _) => isLeave = true;
  812. var top = Application.Top;
  813. top.Add (v);
  814. Assert.False (v.CanFocus);
  815. var exception = Record.Exception (() => top.OnEnter (top));
  816. Assert.Null (exception);
  817. exception = Record.Exception (() => top.OnLeave (top));
  818. Assert.Null (exception);
  819. v.CanFocus = true;
  820. Application.Begin (top);
  821. Assert.True (isEnter);
  822. Assert.False (isLeave);
  823. isEnter = false;
  824. var d = new Dialog ();
  825. var rs = Application.Begin (d);
  826. Assert.False (isEnter);
  827. Assert.True (isLeave);
  828. isLeave = false;
  829. Application.End (rs);
  830. Assert.True (isEnter);
  831. Assert.False (isLeave); // Leave event cannot be trigger because it v.Enter was performed and v is focused
  832. Assert.True (v.HasFocus);
  833. }
  834. [Fact]
  835. [AutoInitShutdown]
  836. public void OnEnter_OnLeave_Triggered_On_Application_Begin_End_With_More_Toplevels ()
  837. {
  838. var iterations = 0;
  839. var steps = new int [5];
  840. var isEnterTop = false;
  841. var isLeaveTop = false;
  842. var vt = new View ();
  843. var top = Application.Top;
  844. var diag = new Dialog ();
  845. vt.Enter += (s, e) => {
  846. iterations++;
  847. isEnterTop = true;
  848. if (iterations == 1) {
  849. steps [0] = iterations;
  850. Assert.Null (e.View);
  851. } else {
  852. steps [4] = iterations;
  853. Assert.Equal (diag, e.View);
  854. }
  855. };
  856. vt.Leave += (s, e) => {
  857. iterations++;
  858. steps [1] = iterations;
  859. isLeaveTop = true;
  860. Assert.Equal (diag, e.View);
  861. };
  862. top.Add (vt);
  863. Assert.False (vt.CanFocus);
  864. var exception = Record.Exception (() => top.OnEnter (top));
  865. Assert.Null (exception);
  866. exception = Record.Exception (() => top.OnLeave (top));
  867. Assert.Null (exception);
  868. vt.CanFocus = true;
  869. Application.Begin (top);
  870. Assert.True (isEnterTop);
  871. Assert.False (isLeaveTop);
  872. isEnterTop = false;
  873. var isEnterDiag = false;
  874. var isLeaveDiag = false;
  875. var vd = new View ();
  876. vd.Enter += (s, e) => {
  877. iterations++;
  878. steps [2] = iterations;
  879. isEnterDiag = true;
  880. Assert.Null (e.View);
  881. };
  882. vd.Leave += (s, e) => {
  883. iterations++;
  884. steps [3] = iterations;
  885. isLeaveDiag = true;
  886. Assert.Equal (top, e.View);
  887. };
  888. diag.Add (vd);
  889. Assert.False (vd.CanFocus);
  890. exception = Record.Exception (() => diag.OnEnter (diag));
  891. Assert.Null (exception);
  892. exception = Record.Exception (() => diag.OnLeave (diag));
  893. Assert.Null (exception);
  894. vd.CanFocus = true;
  895. var rs = Application.Begin (diag);
  896. Assert.True (isEnterDiag);
  897. Assert.False (isLeaveDiag);
  898. Assert.False (isEnterTop);
  899. Assert.True (isLeaveTop);
  900. isEnterDiag = false;
  901. isLeaveTop = false;
  902. Application.End (rs);
  903. Assert.False (isEnterDiag);
  904. Assert.True (isLeaveDiag);
  905. Assert.True (isEnterTop);
  906. Assert.False (isLeaveTop); // Leave event cannot be trigger because it v.Enter was performed and v is focused
  907. Assert.True (vt.HasFocus);
  908. Assert.Equal (1, steps [0]);
  909. Assert.Equal (2, steps [1]);
  910. Assert.Equal (3, steps [2]);
  911. Assert.Equal (4, steps [3]);
  912. Assert.Equal (5, steps [^1]);
  913. }
  914. [Fact]
  915. [AutoInitShutdown]
  916. public void PositionCursor_SetCursorVisibility_To_Invisible_If_Focused_Is_Null ()
  917. {
  918. var tf = new TextField ("test") { Width = 5 };
  919. var view = new View { Width = 10, Height = 10 };
  920. view.Add (tf);
  921. Application.Top.Add (view);
  922. Application.Begin (Application.Top);
  923. Assert.True (tf.HasFocus);
  924. Application.Driver.GetCursorVisibility (out var cursor);
  925. Assert.Equal (CursorVisibility.Default, cursor);
  926. view.Enabled = false;
  927. Assert.False (tf.HasFocus);
  928. Application.Refresh ();
  929. Application.Driver.GetCursorVisibility (out cursor);
  930. Assert.Equal (CursorVisibility.Invisible, cursor);
  931. }
  932. [Fact]
  933. [AutoInitShutdown]
  934. public void IsLoaded_Application_Begin ()
  935. {
  936. var top = Application.Top;
  937. Assert.False (top.IsLoaded);
  938. Application.Begin (top);
  939. Assert.True (top.IsLoaded);
  940. }
  941. [Fact]
  942. [AutoInitShutdown]
  943. public void IsLoaded_With_Sub_Toplevel_Application_Begin_NeedDisplay ()
  944. {
  945. var top = Application.Top;
  946. var subTop = new Toplevel ();
  947. var view = new View (new Rect (0, 0, 20, 10));
  948. subTop.Add (view);
  949. top.Add (subTop);
  950. Assert.False (top.IsLoaded);
  951. Assert.False (subTop.IsLoaded);
  952. Assert.Equal (new Rect (0, 0, 20, 10), view.Frame);
  953. view.LayoutStarted += view_LayoutStarted;
  954. void view_LayoutStarted (object sender, LayoutEventArgs e)
  955. {
  956. Assert.Equal (new Rect (0, 0, 20, 10), view._needsDisplayRect);
  957. view.LayoutStarted -= view_LayoutStarted;
  958. }
  959. Application.Begin (top);
  960. Assert.True (top.IsLoaded);
  961. Assert.True (subTop.IsLoaded);
  962. Assert.Equal (new Rect (0, 0, 20, 10), view.Frame);
  963. view.Frame = new Rect (1, 3, 10, 5);
  964. Assert.Equal (new Rect (1, 3, 10, 5), view.Frame);
  965. Assert.Equal (new Rect (0, 0, 10, 5), view._needsDisplayRect);
  966. view.OnDrawContent (view.Bounds);
  967. view.Frame = new Rect (1, 3, 10, 5);
  968. Assert.Equal (new Rect (1, 3, 10, 5), view.Frame);
  969. Assert.Equal (new Rect (0, 0, 10, 5), view._needsDisplayRect);
  970. }
  971. // BUGBUG: Broke this test with #2483 - @bdisp I need your help figuring out why
  972. [Fact]
  973. [AutoInitShutdown]
  974. public void Toplevel_Inside_ScrollView_MouseGrabView ()
  975. {
  976. var scrollView = new ScrollView {
  977. X = 3,
  978. Y = 3,
  979. Width = 40,
  980. Height = 16,
  981. ContentSize = new Size (200, 100)
  982. };
  983. var win = new Window { X = 3, Y = 3, Width = Dim.Fill (3), Height = Dim.Fill (3) };
  984. scrollView.Add (win);
  985. var top = Application.Top;
  986. top.Add (scrollView);
  987. Application.Begin (top);
  988. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  989. Assert.Equal (new Rect (3, 3, 40, 16), scrollView.Frame);
  990. Assert.Equal (new Rect (0, 0, 200, 100), scrollView.Subviews [0].Frame);
  991. Assert.Equal (new Rect (3, 3, 194, 94), win.Frame);
  992. TestHelpers.AssertDriverContentsWithFrameAre (@"
  993. ┌───────────────────────────────────┴
  994. │ ░
  995. │ ░
  996. │ ░
  997. │ ░
  998. │ ░
  999. │ ░
  1000. │ ░
  1001. │ ░
  1002. │ ░
  1003. │ ░
  1004. │ ▼
  1005. ◄├──────┤░░░░░░░░░░░░░░░░░░░░░░░░░░░░░► ", _output);
  1006. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1007. X = 6,
  1008. Y = 6,
  1009. Flags = MouseFlags.Button1Pressed
  1010. }));
  1011. Assert.Equal (win, Application.MouseGrabView);
  1012. Assert.Equal (new Rect (3, 3, 194, 94), win.Frame);
  1013. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1014. X = 9,
  1015. Y = 9,
  1016. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1017. }));
  1018. Assert.Equal (win, Application.MouseGrabView);
  1019. top.SetNeedsLayout ();
  1020. top.LayoutSubviews ();
  1021. Assert.Equal (new Rect (6, 6, 191, 91), win.Frame);
  1022. Application.Refresh ();
  1023. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1024. ┌────────────────────────────────░
  1025. │ ░
  1026. │ ░
  1027. │ ░
  1028. │ ░
  1029. │ ░
  1030. │ ░
  1031. │ ░
  1032. │ ▼
  1033. ◄├──────┤░░░░░░░░░░░░░░░░░░░░░░░░░░░░░► ", _output);
  1034. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1035. X = 5,
  1036. Y = 5,
  1037. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1038. }));
  1039. Assert.Equal (win, Application.MouseGrabView);
  1040. top.SetNeedsLayout ();
  1041. top.LayoutSubviews ();
  1042. Assert.Equal (new Rect (2, 2, 195, 95), win.Frame);
  1043. Application.Refresh ();
  1044. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1045. ┌────────────────────────────────────│
  1046. │ ┴
  1047. │ ░
  1048. │ ░
  1049. │ ░
  1050. │ ░
  1051. │ ░
  1052. │ ░
  1053. │ ░
  1054. │ ░
  1055. │ ░
  1056. │ ░
  1057. │ ▼
  1058. ◄├──────┤░░░░░░░░░░░░░░░░░░░░░░░░░░░░░► ", _output);
  1059. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1060. X = 5,
  1061. Y = 5,
  1062. Flags = MouseFlags.Button1Released
  1063. }));
  1064. Assert.Null (Application.MouseGrabView);
  1065. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1066. X = 4,
  1067. Y = 4,
  1068. Flags = MouseFlags.ReportMousePosition
  1069. }));
  1070. Assert.Equal (scrollView, Application.MouseGrabView);
  1071. }
  1072. [Fact]
  1073. [AutoInitShutdown]
  1074. public void Window_Bounds_Bigger_Than_Driver_Cols_And_Rows_Allow_Drag_Beyond_Left_Right_And_Bottom ()
  1075. {
  1076. var top = Application.Top;
  1077. var window = new Window { Width = 20, Height = 3 };
  1078. Application.Begin (top);
  1079. ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
  1080. Application.Begin (window);
  1081. Application.Refresh ();
  1082. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  1083. Assert.Equal (new Rect (0, 0, 20, 3), window.Frame);
  1084. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1085. ┌──────────────────┐
  1086. │ │
  1087. └──────────────────┘
  1088. ", _output);
  1089. Assert.Null (Application.MouseGrabView);
  1090. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1091. X = 0,
  1092. Y = 0,
  1093. Flags = MouseFlags.Button1Pressed
  1094. }));
  1095. Assert.Equal (window, Application.MouseGrabView);
  1096. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1097. X = -11,
  1098. Y = -4,
  1099. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1100. }));
  1101. Application.Refresh ();
  1102. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  1103. Assert.Equal (new Rect (0, 0, 20, 3), window.Frame);
  1104. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1105. ┌──────────────────┐
  1106. │ │
  1107. └──────────────────┘
  1108. ", _output);
  1109. // Changes Top size to same size as Dialog more menu and scroll bar
  1110. ((FakeDriver)Application.Driver).SetBufferSize (20, 3);
  1111. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1112. X = -1,
  1113. Y = -1,
  1114. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1115. }));
  1116. Application.Refresh ();
  1117. Assert.Equal (new Rect (0, 0, 20, 3), top.Frame);
  1118. Assert.Equal (new Rect (0, 0, 20, 3), window.Frame);
  1119. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1120. ┌──────────────────┐
  1121. │ │
  1122. └──────────────────┘
  1123. ", _output);
  1124. // Changes Top size smaller than Dialog size
  1125. ((FakeDriver)Application.Driver).SetBufferSize (19, 2);
  1126. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1127. X = -1,
  1128. Y = -1,
  1129. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1130. }));
  1131. Application.Refresh ();
  1132. Assert.Equal (new Rect (0, 0, 19, 2), top.Frame);
  1133. Assert.Equal (new Rect (-1, 0, 20, 3), window.Frame);
  1134. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1135. ──────────────────┐
  1136. ", _output);
  1137. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1138. X = 18,
  1139. Y = 1,
  1140. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1141. }));
  1142. Application.Refresh ();
  1143. Assert.Equal (new Rect (0, 0, 19, 2), top.Frame);
  1144. Assert.Equal (new Rect (18, 1, 20, 3), window.Frame);
  1145. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1146. ┌", _output);
  1147. // On a real app we can't go beyond the SuperView bounds
  1148. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1149. X = 19,
  1150. Y = 2,
  1151. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1152. }));
  1153. Application.Refresh ();
  1154. Assert.Equal (new Rect (0, 0, 19, 2), top.Frame);
  1155. Assert.Equal (new Rect (19, 2, 20, 3), window.Frame);
  1156. TestHelpers.AssertDriverContentsWithFrameAre (@"", _output);
  1157. }
  1158. [Fact]
  1159. [AutoInitShutdown]
  1160. public void Modal_As_Top_Will_Drag_Cleanly ()
  1161. {
  1162. // Don't use Dialog as a Top, use a Window instead - dialog has complex layout behavior that is not needed here.
  1163. var window = new Window { Width = 10, Height = 3 };
  1164. window.Add (new Label (
  1165. "Test") {
  1166. X = Pos.Center (),
  1167. Y = Pos.Center (),
  1168. Width = Dim.Fill (),
  1169. Height = Dim.Fill (),
  1170. TextAlignment = TextAlignment.Centered,
  1171. VerticalTextAlignment = VerticalTextAlignment.Middle,
  1172. AutoSize = false
  1173. });
  1174. var rs = Application.Begin (window);
  1175. Assert.Null (Application.MouseGrabView);
  1176. Assert.Equal (new Rect (0, 0, 10, 3), window.Frame);
  1177. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1178. ┌────────┐
  1179. │ Test │
  1180. └────────┘", _output);
  1181. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1182. X = 0,
  1183. Y = 0,
  1184. Flags = MouseFlags.Button1Pressed
  1185. }));
  1186. var firstIteration = false;
  1187. Application.RunIteration (ref rs, ref firstIteration);
  1188. Assert.Equal (window, Application.MouseGrabView);
  1189. Assert.Equal (new Rect (0, 0, 10, 3), window.Frame);
  1190. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1191. ┌────────┐
  1192. │ Test │
  1193. └────────┘", _output);
  1194. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1195. X = 1,
  1196. Y = 1,
  1197. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1198. }));
  1199. firstIteration = false;
  1200. Application.RunIteration (ref rs, ref firstIteration);
  1201. Assert.Equal (window, Application.MouseGrabView);
  1202. Assert.Equal (new Rect (1, 1, 10, 3), window.Frame);
  1203. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1204. ┌────────┐
  1205. │ Test │
  1206. └────────┘", _output);
  1207. Application.End (rs);
  1208. }
  1209. [Fact]
  1210. [AutoInitShutdown]
  1211. public void Begin_With_Window_Sets_Size_Correctly ()
  1212. {
  1213. var top = Application.Top;
  1214. Application.Begin (top);
  1215. ((FakeDriver)Application.Driver).SetBufferSize (20, 20);
  1216. var testWindow = new Window { X = 2, Y = 1, Width = 15, Height = 10 };
  1217. Assert.Equal (new Rect (2, 1, 15, 10), testWindow.Frame);
  1218. var rs = Application.Begin (testWindow);
  1219. Assert.Equal (new Rect (2, 1, 15, 10), testWindow.Frame);
  1220. }
  1221. // Don't use Dialog as a Top, use a Window instead - dialog has complex layout behavior that is not needed here.
  1222. [Fact]
  1223. [AutoInitShutdown]
  1224. public void Draw_A_Top_Subview_On_A_Window ()
  1225. {
  1226. var top = Application.Top;
  1227. var win = new Window ();
  1228. top.Add (win);
  1229. Application.Begin (top);
  1230. ((FakeDriver)Application.Driver).SetBufferSize (20, 20);
  1231. Assert.Equal (new Rect (0, 0, 20, 20), win.Frame);
  1232. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1233. ┌──────────────────┐
  1234. │ │
  1235. │ │
  1236. │ │
  1237. │ │
  1238. │ │
  1239. │ │
  1240. │ │
  1241. │ │
  1242. │ │
  1243. │ │
  1244. │ │
  1245. │ │
  1246. │ │
  1247. │ │
  1248. │ │
  1249. │ │
  1250. │ │
  1251. │ │
  1252. └──────────────────┘", _output);
  1253. var btnPopup = new Button ("Popup");
  1254. var testWindow = new Window { X = 2, Y = 1, Width = 15, Height = 10 };
  1255. testWindow.Add (btnPopup);
  1256. btnPopup.Clicked += (s, e) => {
  1257. var viewToScreen = btnPopup.BoundsToScreen (top.Frame);
  1258. var viewAddedToTop = new View {
  1259. Text = "viewAddedToTop",
  1260. X = 1,
  1261. Y = viewToScreen.Y + 1,
  1262. Width = 18,
  1263. Height = 16,
  1264. BorderStyle = LineStyle.Single
  1265. };
  1266. Assert.Equal (testWindow, Application.Current);
  1267. Application.Current.DrawContentComplete += testWindow_DrawContentComplete;
  1268. top.Add (viewAddedToTop);
  1269. void testWindow_DrawContentComplete (object sender, DrawEventArgs e)
  1270. {
  1271. Assert.Equal (new Rect (1, 3, 18, 16), viewAddedToTop.Frame);
  1272. var savedClip = Application.Driver.Clip;
  1273. Application.Driver.Clip = top.Frame;
  1274. viewAddedToTop.Draw ();
  1275. top.Move (2, 15);
  1276. View.Driver.AddStr ("One");
  1277. top.Move (2, 16);
  1278. View.Driver.AddStr ("Two");
  1279. top.Move (2, 17);
  1280. View.Driver.AddStr ("Three");
  1281. Application.Driver.Clip = savedClip;
  1282. Application.Current.DrawContentComplete -= testWindow_DrawContentComplete;
  1283. }
  1284. };
  1285. var rs = Application.Begin (testWindow);
  1286. Assert.Equal (new Rect (2, 1, 15, 10), testWindow.Frame);
  1287. TestHelpers.AssertDriverContentsWithFrameAre (@$"
  1288. ┌──────────────────┐
  1289. │ ┌─────────────┐ │
  1290. │ │{CM.Glyphs.LeftBracket} Popup {CM.Glyphs.RightBracket} │ │
  1291. │ │ │ │
  1292. │ │ │ │
  1293. │ │ │ │
  1294. │ │ │ │
  1295. │ │ │ │
  1296. │ │ │ │
  1297. │ │ │ │
  1298. │ └─────────────┘ │
  1299. │ │
  1300. │ │
  1301. │ │
  1302. │ │
  1303. │ │
  1304. │ │
  1305. │ │
  1306. │ │
  1307. └──────────────────┘", _output);
  1308. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1309. X = 5,
  1310. Y = 2,
  1311. Flags = MouseFlags.Button1Clicked
  1312. }));
  1313. Application.Top.Draw ();
  1314. var firstIteration = false;
  1315. Application.RunIteration (ref rs, ref firstIteration);
  1316. TestHelpers.AssertDriverContentsWithFrameAre (@$"
  1317. ┌──────────────────┐
  1318. │ ┌─────────────┐ │
  1319. │ │{CM.Glyphs.LeftBracket} Popup {CM.Glyphs.RightBracket} │ │
  1320. │┌────────────────┐│
  1321. ││viewAddedToTop ││
  1322. ││ ││
  1323. ││ ││
  1324. ││ ││
  1325. ││ ││
  1326. ││ ││
  1327. ││ ││
  1328. ││ ││
  1329. ││ ││
  1330. ││ ││
  1331. ││ ││
  1332. ││One ││
  1333. ││Two ││
  1334. ││Three ││
  1335. │└────────────────┘│
  1336. └──────────────────┘", _output);
  1337. Application.End (rs);
  1338. }
  1339. [Fact]
  1340. [AutoInitShutdown]
  1341. public void Activating_MenuBar_By_Alt_Key_Does_Not_Throw ()
  1342. {
  1343. var menu = new MenuBar (new MenuBarItem [] {
  1344. new ("Child", new MenuItem [] {
  1345. new ("_Create Child", "", null)
  1346. })
  1347. });
  1348. var topChild = new Toplevel ();
  1349. topChild.Add (menu);
  1350. Application.Top.Add (topChild);
  1351. Application.Begin (Application.Top);
  1352. var exception = Record.Exception (() => topChild.NewKeyDownEvent (new Key (KeyCode.AltMask)));
  1353. Assert.Null (exception);
  1354. }
  1355. [Fact]
  1356. [TestRespondersDisposed]
  1357. public void Multi_Thread_Toplevels ()
  1358. {
  1359. Application.Init (new FakeDriver ());
  1360. var t = Application.Top;
  1361. var w = new Window ();
  1362. t.Add (w);
  1363. int count = 0, count1 = 0, count2 = 0;
  1364. bool log = false, log1 = false, log2 = false;
  1365. var fromTopStillKnowFirstIsRunning = false;
  1366. var fromTopStillKnowSecondIsRunning = false;
  1367. var fromFirstStillKnowSecondIsRunning = false;
  1368. Application.AddTimeout (TimeSpan.FromMilliseconds (100), () => {
  1369. count++;
  1370. if (count1 == 5) {
  1371. log1 = true;
  1372. }
  1373. if (count1 == 14 && count2 == 10 && count == 15) {
  1374. // count2 is already stopped
  1375. fromTopStillKnowFirstIsRunning = true;
  1376. }
  1377. if (count1 == 7 && count2 == 7 && count == 8) {
  1378. fromTopStillKnowSecondIsRunning = true;
  1379. }
  1380. if (count == 30) {
  1381. Assert.Equal (30, count);
  1382. Assert.Equal (20, count1);
  1383. Assert.Equal (10, count2);
  1384. Assert.True (log);
  1385. Assert.True (log1);
  1386. Assert.True (log2);
  1387. Assert.True (fromTopStillKnowFirstIsRunning);
  1388. Assert.True (fromTopStillKnowSecondIsRunning);
  1389. Assert.True (fromFirstStillKnowSecondIsRunning);
  1390. Application.RequestStop ();
  1391. return false;
  1392. }
  1393. return true;
  1394. });
  1395. t.Ready += FirstWindow;
  1396. void FirstWindow (object sender, EventArgs args)
  1397. {
  1398. var firstWindow = new Window ();
  1399. firstWindow.Ready += SecondWindow;
  1400. Application.AddTimeout (TimeSpan.FromMilliseconds (100), () => {
  1401. count1++;
  1402. if (count2 == 5) {
  1403. log2 = true;
  1404. }
  1405. if (count2 == 4 && count1 == 5 && count == 5) {
  1406. fromFirstStillKnowSecondIsRunning = true;
  1407. }
  1408. if (count1 == 20) {
  1409. Assert.Equal (20, count1);
  1410. Application.RequestStop ();
  1411. return false;
  1412. }
  1413. return true;
  1414. });
  1415. Application.Run (firstWindow);
  1416. }
  1417. void SecondWindow (object sender, EventArgs args)
  1418. {
  1419. var testWindow = new Window ();
  1420. Application.AddTimeout (TimeSpan.FromMilliseconds (100), () => {
  1421. count2++;
  1422. if (count < 30) {
  1423. log = true;
  1424. }
  1425. if (count2 == 10) {
  1426. Assert.Equal (10, count2);
  1427. Application.RequestStop ();
  1428. return false;
  1429. }
  1430. return true;
  1431. });
  1432. Application.Run (testWindow);
  1433. }
  1434. Application.Run ();
  1435. Application.Shutdown ();
  1436. }
  1437. }