ToplevelTests.cs 57 KB

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