ToplevelTests.cs 57 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. โ”‚ โ–‘
  1006. โ”‚ โ–‘
  1007. โ”‚ โ–ผ
  1008. โ—„โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”คโ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–บ ", _output);
  1009. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1010. X = 6,
  1011. Y = 6,
  1012. Flags = MouseFlags.Button1Pressed
  1013. }));
  1014. Assert.Equal (win, Application.MouseGrabView);
  1015. Assert.Equal (new Rect (3, 3, 194, 94), win.Frame);
  1016. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1017. X = 9,
  1018. Y = 9,
  1019. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1020. }));
  1021. Assert.Equal (win, Application.MouseGrabView);
  1022. top.SetNeedsLayout ();
  1023. top.LayoutSubviews ();
  1024. Assert.Equal (new Rect (6, 6, 191, 91), win.Frame);
  1025. Application.Refresh ();
  1026. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1027. โ–ฒ
  1028. โ”ฌ
  1029. โ”‚
  1030. โ”ด
  1031. โ–‘
  1032. โ–‘
  1033. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–‘
  1034. โ”‚ โ–‘
  1035. โ”‚ โ–‘
  1036. โ”‚ โ–‘
  1037. โ”‚ โ–‘
  1038. โ”‚ โ–‘
  1039. โ”‚ โ–‘
  1040. โ”‚ โ–‘
  1041. โ”‚ โ–ผ
  1042. โ—„โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”คโ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–บ ", _output);
  1043. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1044. X = 5,
  1045. Y = 5,
  1046. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1047. }));
  1048. Assert.Equal (win, Application.MouseGrabView);
  1049. top.SetNeedsLayout ();
  1050. top.LayoutSubviews ();
  1051. Assert.Equal (new Rect (2, 2, 195, 95), win.Frame);
  1052. Application.Refresh ();
  1053. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1054. โ–ฒ
  1055. โ”ฌ
  1056. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚
  1057. โ”‚ โ”ด
  1058. โ”‚ โ–‘
  1059. โ”‚ โ–‘
  1060. โ”‚ โ–‘
  1061. โ”‚ โ–‘
  1062. โ”‚ โ–‘
  1063. โ”‚ โ–‘
  1064. โ”‚ โ–‘
  1065. โ”‚ โ–‘
  1066. โ”‚ โ–‘
  1067. โ”‚ โ–‘
  1068. โ”‚ โ–ผ
  1069. โ—„โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”คโ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–บ ", _output);
  1070. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1071. X = 5,
  1072. Y = 5,
  1073. Flags = MouseFlags.Button1Released
  1074. }));
  1075. Assert.Null (Application.MouseGrabView);
  1076. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1077. X = 4,
  1078. Y = 4,
  1079. Flags = MouseFlags.ReportMousePosition
  1080. }));
  1081. Assert.Equal (scrollView, Application.MouseGrabView);
  1082. }
  1083. [Fact]
  1084. [AutoInitShutdown]
  1085. public void Window_Bounds_Bigger_Than_Driver_Cols_And_Rows_Allow_Drag_Beyond_Left_Right_And_Bottom ()
  1086. {
  1087. var top = Application.Top;
  1088. var window = new Window { Width = 20, Height = 3 };
  1089. Application.Begin (top);
  1090. ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
  1091. Application.Begin (window);
  1092. Application.Refresh ();
  1093. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  1094. Assert.Equal (new Rect (0, 0, 20, 3), window.Frame);
  1095. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1096. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  1097. โ”‚ โ”‚
  1098. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  1099. ", _output);
  1100. Assert.Null (Application.MouseGrabView);
  1101. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1102. X = 0,
  1103. Y = 0,
  1104. Flags = MouseFlags.Button1Pressed
  1105. }));
  1106. Assert.Equal (window, Application.MouseGrabView);
  1107. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1108. X = -11,
  1109. Y = -4,
  1110. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1111. }));
  1112. Application.Refresh ();
  1113. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  1114. Assert.Equal (new Rect (0, 0, 20, 3), window.Frame);
  1115. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1116. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  1117. โ”‚ โ”‚
  1118. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  1119. ", _output);
  1120. // Changes Top size to same size as Dialog more menu and scroll bar
  1121. ((FakeDriver)Application.Driver).SetBufferSize (20, 3);
  1122. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1123. X = -1,
  1124. Y = -1,
  1125. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1126. }));
  1127. Application.Refresh ();
  1128. Assert.Equal (new Rect (0, 0, 20, 3), top.Frame);
  1129. Assert.Equal (new Rect (0, 0, 20, 3), window.Frame);
  1130. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1131. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  1132. โ”‚ โ”‚
  1133. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  1134. ", _output);
  1135. // Changes Top size smaller than Dialog size
  1136. ((FakeDriver)Application.Driver).SetBufferSize (19, 2);
  1137. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1138. X = -1,
  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 (-1, 0, 20, 3), window.Frame);
  1145. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1146. โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  1147. โ”‚
  1148. ", _output);
  1149. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1150. X = 18,
  1151. Y = 1,
  1152. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1153. }));
  1154. Application.Refresh ();
  1155. Assert.Equal (new Rect (0, 0, 19, 2), top.Frame);
  1156. Assert.Equal (new Rect (18, 1, 20, 3), window.Frame);
  1157. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1158. โ”Œ", _output);
  1159. // On a real app we can't go beyond the SuperView bounds
  1160. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1161. X = 19,
  1162. Y = 2,
  1163. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1164. }));
  1165. Application.Refresh ();
  1166. Assert.Equal (new Rect (0, 0, 19, 2), top.Frame);
  1167. Assert.Equal (new Rect (19, 2, 20, 3), window.Frame);
  1168. TestHelpers.AssertDriverContentsWithFrameAre (@"", _output);
  1169. }
  1170. [Fact]
  1171. [AutoInitShutdown]
  1172. public void Modal_As_Top_Will_Drag_Cleanly ()
  1173. {
  1174. // Don't use Dialog as a Top, use a Window instead - dialog has complex layout behavior that is not needed here.
  1175. var window = new Window { Width = 10, Height = 3 };
  1176. window.Add (new Label (
  1177. "Test") {
  1178. X = Pos.Center (),
  1179. Y = Pos.Center (),
  1180. Width = Dim.Fill (),
  1181. Height = Dim.Fill (),
  1182. TextAlignment = TextAlignment.Centered,
  1183. VerticalTextAlignment = VerticalTextAlignment.Middle,
  1184. AutoSize = false
  1185. });
  1186. var rs = Application.Begin (window);
  1187. Assert.Null (Application.MouseGrabView);
  1188. Assert.Equal (new Rect (0, 0, 10, 3), window.Frame);
  1189. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1190. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  1191. โ”‚ Test โ”‚
  1192. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜", _output);
  1193. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1194. X = 0,
  1195. Y = 0,
  1196. Flags = MouseFlags.Button1Pressed
  1197. }));
  1198. var firstIteration = false;
  1199. Application.RunIteration (ref rs, ref firstIteration);
  1200. Assert.Equal (window, Application.MouseGrabView);
  1201. Assert.Equal (new Rect (0, 0, 10, 3), window.Frame);
  1202. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1203. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  1204. โ”‚ Test โ”‚
  1205. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜", _output);
  1206. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1207. X = 1,
  1208. Y = 1,
  1209. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1210. }));
  1211. firstIteration = false;
  1212. Application.RunIteration (ref rs, ref firstIteration);
  1213. Assert.Equal (window, Application.MouseGrabView);
  1214. Assert.Equal (new Rect (1, 1, 10, 3), window.Frame);
  1215. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1216. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  1217. โ”‚ Test โ”‚
  1218. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜", _output);
  1219. Application.End (rs);
  1220. }
  1221. [Fact]
  1222. [AutoInitShutdown]
  1223. public void Begin_With_Window_Sets_Size_Correctly ()
  1224. {
  1225. var top = Application.Top;
  1226. Application.Begin (top);
  1227. ((FakeDriver)Application.Driver).SetBufferSize (20, 20);
  1228. var testWindow = new Window { X = 2, Y = 1, Width = 15, Height = 10 };
  1229. Assert.Equal (new Rect (2, 1, 15, 10), testWindow.Frame);
  1230. var rs = Application.Begin (testWindow);
  1231. Assert.Equal (new Rect (2, 1, 15, 10), testWindow.Frame);
  1232. }
  1233. // Don't use Dialog as a Top, use a Window instead - dialog has complex layout behavior that is not needed here.
  1234. [Fact]
  1235. [AutoInitShutdown]
  1236. public void Draw_A_Top_Subview_On_A_Window ()
  1237. {
  1238. var top = Application.Top;
  1239. var win = new Window ();
  1240. top.Add (win);
  1241. Application.Begin (top);
  1242. ((FakeDriver)Application.Driver).SetBufferSize (20, 20);
  1243. Assert.Equal (new Rect (0, 0, 20, 20), win.Frame);
  1244. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1245. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  1246. โ”‚ โ”‚
  1247. โ”‚ โ”‚
  1248. โ”‚ โ”‚
  1249. โ”‚ โ”‚
  1250. โ”‚ โ”‚
  1251. โ”‚ โ”‚
  1252. โ”‚ โ”‚
  1253. โ”‚ โ”‚
  1254. โ”‚ โ”‚
  1255. โ”‚ โ”‚
  1256. โ”‚ โ”‚
  1257. โ”‚ โ”‚
  1258. โ”‚ โ”‚
  1259. โ”‚ โ”‚
  1260. โ”‚ โ”‚
  1261. โ”‚ โ”‚
  1262. โ”‚ โ”‚
  1263. โ”‚ โ”‚
  1264. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜", _output);
  1265. var btnPopup = new Button ("Popup");
  1266. var testWindow = new Window { X = 2, Y = 1, Width = 15, Height = 10 };
  1267. testWindow.Add (btnPopup);
  1268. btnPopup.Clicked += (s, e) => {
  1269. var viewToScreen = btnPopup.BoundsToScreen (top.Frame);
  1270. var viewAddedToTop = new View {
  1271. Text = "viewAddedToTop",
  1272. X = 1,
  1273. Y = viewToScreen.Y + 1,
  1274. Width = 18,
  1275. Height = 16,
  1276. BorderStyle = LineStyle.Single
  1277. };
  1278. Assert.Equal (testWindow, Application.Current);
  1279. Application.Current.DrawContentComplete += testWindow_DrawContentComplete;
  1280. top.Add (viewAddedToTop);
  1281. void testWindow_DrawContentComplete (object sender, DrawEventArgs e)
  1282. {
  1283. Assert.Equal (new Rect (1, 3, 18, 16), viewAddedToTop.Frame);
  1284. var savedClip = Application.Driver.Clip;
  1285. Application.Driver.Clip = top.Frame;
  1286. viewAddedToTop.Draw ();
  1287. top.Move (2, 15);
  1288. View.Driver.AddStr ("One");
  1289. top.Move (2, 16);
  1290. View.Driver.AddStr ("Two");
  1291. top.Move (2, 17);
  1292. View.Driver.AddStr ("Three");
  1293. Application.Driver.Clip = savedClip;
  1294. Application.Current.DrawContentComplete -= testWindow_DrawContentComplete;
  1295. }
  1296. };
  1297. var rs = Application.Begin (testWindow);
  1298. Assert.Equal (new Rect (2, 1, 15, 10), testWindow.Frame);
  1299. TestHelpers.AssertDriverContentsWithFrameAre (@$"
  1300. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  1301. โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
  1302. โ”‚ โ”‚{CM.Glyphs.LeftBracket} Popup {CM.Glyphs.RightBracket} โ”‚ โ”‚
  1303. โ”‚ โ”‚ โ”‚ โ”‚
  1304. โ”‚ โ”‚ โ”‚ โ”‚
  1305. โ”‚ โ”‚ โ”‚ โ”‚
  1306. โ”‚ โ”‚ โ”‚ โ”‚
  1307. โ”‚ โ”‚ โ”‚ โ”‚
  1308. โ”‚ โ”‚ โ”‚ โ”‚
  1309. โ”‚ โ”‚ โ”‚ โ”‚
  1310. โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
  1311. โ”‚ โ”‚
  1312. โ”‚ โ”‚
  1313. โ”‚ โ”‚
  1314. โ”‚ โ”‚
  1315. โ”‚ โ”‚
  1316. โ”‚ โ”‚
  1317. โ”‚ โ”‚
  1318. โ”‚ โ”‚
  1319. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜", _output);
  1320. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent {
  1321. X = 5,
  1322. Y = 2,
  1323. Flags = MouseFlags.Button1Clicked
  1324. }));
  1325. Application.Top.Draw ();
  1326. var firstIteration = false;
  1327. Application.RunIteration (ref rs, ref firstIteration);
  1328. TestHelpers.AssertDriverContentsWithFrameAre (@$"
  1329. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  1330. โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
  1331. โ”‚ โ”‚{CM.Glyphs.LeftBracket} Popup {CM.Glyphs.RightBracket} โ”‚ โ”‚
  1332. โ”‚โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”โ”‚
  1333. โ”‚โ”‚viewAddedToTop โ”‚โ”‚
  1334. โ”‚โ”‚ โ”‚โ”‚
  1335. โ”‚โ”‚ โ”‚โ”‚
  1336. โ”‚โ”‚ โ”‚โ”‚
  1337. โ”‚โ”‚ โ”‚โ”‚
  1338. โ”‚โ”‚ โ”‚โ”‚
  1339. โ”‚โ”‚ โ”‚โ”‚
  1340. โ”‚โ”‚ โ”‚โ”‚
  1341. โ”‚โ”‚ โ”‚โ”‚
  1342. โ”‚โ”‚ โ”‚โ”‚
  1343. โ”‚โ”‚ โ”‚โ”‚
  1344. โ”‚โ”‚One โ”‚โ”‚
  1345. โ”‚โ”‚Two โ”‚โ”‚
  1346. โ”‚โ”‚Three โ”‚โ”‚
  1347. โ”‚โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜โ”‚
  1348. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜", _output);
  1349. Application.End (rs);
  1350. }
  1351. [Fact]
  1352. [AutoInitShutdown]
  1353. public void Activating_MenuBar_By_Alt_Key_Does_Not_Throw ()
  1354. {
  1355. var menu = new MenuBar (new MenuBarItem [] {
  1356. new ("Child", new MenuItem [] {
  1357. new ("_Create Child", "", null)
  1358. })
  1359. });
  1360. var topChild = new Toplevel ();
  1361. topChild.Add (menu);
  1362. Application.Top.Add (topChild);
  1363. Application.Begin (Application.Top);
  1364. var exception = Record.Exception (() => topChild.NewKeyDownEvent (new Key (KeyCode.AltMask)));
  1365. Assert.Null (exception);
  1366. }
  1367. [Fact]
  1368. [TestRespondersDisposed]
  1369. public void Multi_Thread_Toplevels ()
  1370. {
  1371. Application.Init (new FakeDriver ());
  1372. var t = Application.Top;
  1373. var w = new Window ();
  1374. t.Add (w);
  1375. int count = 0, count1 = 0, count2 = 0;
  1376. bool log = false, log1 = false, log2 = false;
  1377. var fromTopStillKnowFirstIsRunning = false;
  1378. var fromTopStillKnowSecondIsRunning = false;
  1379. var fromFirstStillKnowSecondIsRunning = false;
  1380. Application.AddTimeout (TimeSpan.FromMilliseconds (100), () => {
  1381. count++;
  1382. if (count1 == 5) {
  1383. log1 = true;
  1384. }
  1385. if (count1 == 14 && count2 == 10 && count == 15) {
  1386. // count2 is already stopped
  1387. fromTopStillKnowFirstIsRunning = true;
  1388. }
  1389. if (count1 == 7 && count2 == 7 && count == 8) {
  1390. fromTopStillKnowSecondIsRunning = true;
  1391. }
  1392. if (count == 30) {
  1393. Assert.Equal (30, count);
  1394. Assert.Equal (20, count1);
  1395. Assert.Equal (10, count2);
  1396. Assert.True (log);
  1397. Assert.True (log1);
  1398. Assert.True (log2);
  1399. Assert.True (fromTopStillKnowFirstIsRunning);
  1400. Assert.True (fromTopStillKnowSecondIsRunning);
  1401. Assert.True (fromFirstStillKnowSecondIsRunning);
  1402. Application.RequestStop ();
  1403. return false;
  1404. }
  1405. return true;
  1406. });
  1407. t.Ready += FirstWindow;
  1408. void FirstWindow (object sender, EventArgs args)
  1409. {
  1410. var firstWindow = new Window ();
  1411. firstWindow.Ready += SecondWindow;
  1412. Application.AddTimeout (TimeSpan.FromMilliseconds (100), () => {
  1413. count1++;
  1414. if (count2 == 5) {
  1415. log2 = true;
  1416. }
  1417. if (count2 == 4 && count1 == 5 && count == 5) {
  1418. fromFirstStillKnowSecondIsRunning = true;
  1419. }
  1420. if (count1 == 20) {
  1421. Assert.Equal (20, count1);
  1422. Application.RequestStop ();
  1423. return false;
  1424. }
  1425. return true;
  1426. });
  1427. Application.Run (firstWindow);
  1428. }
  1429. void SecondWindow (object sender, EventArgs args)
  1430. {
  1431. var testWindow = new Window ();
  1432. Application.AddTimeout (TimeSpan.FromMilliseconds (100), () => {
  1433. count2++;
  1434. if (count < 30) {
  1435. log = true;
  1436. }
  1437. if (count2 == 10) {
  1438. Assert.Equal (10, count2);
  1439. Application.RequestStop ();
  1440. return false;
  1441. }
  1442. return true;
  1443. });
  1444. Application.Run (testWindow);
  1445. }
  1446. Application.Run ();
  1447. Application.Shutdown ();
  1448. }
  1449. }