ToplevelTests.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. using UnitTests;
  2. namespace Terminal.Gui.ViewsTests;
  3. public class ToplevelTests
  4. {
  5. [Fact]
  6. public void Constructor_Default ()
  7. {
  8. var top = new Toplevel ();
  9. Assert.Equal (Colors.ColorSchemes ["TopLevel"], top.ColorScheme);
  10. Assert.Equal ("Fill(Absolute(0))", top.Width.ToString ());
  11. Assert.Equal ("Fill(Absolute(0))", top.Height.ToString ());
  12. Assert.False (top.Running);
  13. Assert.False (top.Modal);
  14. Assert.Null (top.MenuBar);
  15. //Assert.Null (top.StatusBar);
  16. }
  17. [Fact]
  18. public void Arrangement_Default_Is_Overlapped ()
  19. {
  20. var top = new Toplevel ();
  21. Assert.Equal (ViewArrangement.Overlapped, top.Arrangement);
  22. }
  23. [Fact]
  24. [AutoInitShutdown]
  25. public void Internal_Tests ()
  26. {
  27. var top = new Toplevel ();
  28. var eventInvoked = "";
  29. top.Loaded += (s, e) => eventInvoked = "Loaded";
  30. top.OnLoaded ();
  31. Assert.Equal ("Loaded", eventInvoked);
  32. top.Ready += (s, e) => eventInvoked = "Ready";
  33. top.OnReady ();
  34. Assert.Equal ("Ready", eventInvoked);
  35. top.Unloaded += (s, e) => eventInvoked = "Unloaded";
  36. top.OnUnloaded ();
  37. Assert.Equal ("Unloaded", eventInvoked);
  38. top.Add (new MenuBar ());
  39. Assert.NotNull (top.MenuBar);
  40. //top.Add (new StatusBar ());
  41. //Assert.NotNull (top.StatusBar);
  42. MenuBar menuBar = top.MenuBar;
  43. top.Remove (top.MenuBar);
  44. Assert.Null (top.MenuBar);
  45. Assert.NotNull (menuBar);
  46. //var statusBar = top.StatusBar;
  47. //top.Remove (top.StatusBar);
  48. //Assert.Null (top.StatusBar);
  49. //Assert.NotNull (statusBar);
  50. #if DEBUG_IDISPOSABLE
  51. Assert.False (menuBar.WasDisposed);
  52. //Assert.False (statusBar.WasDisposed);
  53. menuBar.Dispose ();
  54. //statusBar.Dispose ();
  55. Assert.True (menuBar.WasDisposed);
  56. //Assert.True (statusBar.WasDisposed);
  57. #endif
  58. Application.Begin (top);
  59. Assert.Equal (top, Application.Top);
  60. // Application.Top without menu and status bar.
  61. View supView = View.GetLocationEnsuringFullVisibility (top, 2, 2, out int nx, out int ny /*, out StatusBar sb*/);
  62. Assert.Equal (Application.Top, supView);
  63. Assert.Equal (0, nx);
  64. Assert.Equal (0, ny);
  65. //Assert.Null (sb);
  66. top.Add (new MenuBar ());
  67. Assert.NotNull (top.MenuBar);
  68. // Application.Top with a menu and without status bar.
  69. View.GetLocationEnsuringFullVisibility (top, 2, 2, out nx, out ny /*, out sb*/);
  70. Assert.Equal (0, nx);
  71. Assert.Equal (1, ny);
  72. //Assert.Null (sb);
  73. //top.Add (new StatusBar ());
  74. //Assert.NotNull (top.StatusBar);
  75. // Application.Top with a menu and status bar.
  76. View.GetLocationEnsuringFullVisibility (top, 2, 2, out nx, out ny /*, out sb*/);
  77. Assert.Equal (0, nx);
  78. // The available height is lower than the Application.Top height minus
  79. // the menu bar and status bar, then the top can go beyond the bottom
  80. // Assert.Equal (2, ny);
  81. //Assert.NotNull (sb);
  82. menuBar = top.MenuBar;
  83. top.Remove (top.MenuBar);
  84. Assert.Null (top.MenuBar);
  85. Assert.NotNull (menuBar);
  86. // Application.Top without a menu and with a status bar.
  87. View.GetLocationEnsuringFullVisibility (top, 2, 2, out nx, out ny /*, out sb*/);
  88. Assert.Equal (0, nx);
  89. // The available height is lower than the Application.Top height minus
  90. // the status bar, then the top can go beyond the bottom
  91. // Assert.Equal (2, ny);
  92. //Assert.NotNull (sb);
  93. //statusBar = top.StatusBar;
  94. //top.Remove (top.StatusBar);
  95. //Assert.Null (top.StatusBar);
  96. //Assert.NotNull (statusBar);
  97. Assert.Null (top.MenuBar);
  98. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  99. top.Add (win);
  100. top.LayoutSubViews ();
  101. // The SuperView is always the same regardless of the caller.
  102. supView = View.GetLocationEnsuringFullVisibility (win, 0, 0, out nx, out ny /*, out sb*/);
  103. Assert.Equal (Application.Top, supView);
  104. supView = View.GetLocationEnsuringFullVisibility (win, 0, 0, out nx, out ny /*, out sb*/);
  105. Assert.Equal (Application.Top, supView);
  106. // Application.Top without menu and status bar.
  107. View.GetLocationEnsuringFullVisibility (win, 0, 0, out nx, out ny /*, out sb*/);
  108. Assert.Equal (0, nx);
  109. Assert.Equal (0, ny);
  110. //Assert.Null (sb);
  111. top.Add (new MenuBar ());
  112. Assert.NotNull (top.MenuBar);
  113. // Application.Top with a menu and without status bar.
  114. View.GetLocationEnsuringFullVisibility (win, 2, 2, out nx, out ny /*, out sb*/);
  115. Assert.Equal (0, nx);
  116. Assert.Equal (1, ny);
  117. //Assert.Null (sb);
  118. top.Add (new StatusBar ());
  119. //Assert.NotNull (top.StatusBar);
  120. // Application.Top with a menu and status bar.
  121. View.GetLocationEnsuringFullVisibility (win, 30, 20, out nx, out ny /*, out sb*/);
  122. Assert.Equal (0, nx);
  123. // The available height is lower than the Application.Top height minus
  124. // the menu bar and status bar, then the top can go beyond the bottom
  125. //Assert.Equal (20, ny);
  126. //Assert.NotNull (sb);
  127. menuBar = top.MenuBar;
  128. //statusBar = top.StatusBar;
  129. top.Remove (top.MenuBar);
  130. Assert.Null (top.MenuBar);
  131. Assert.NotNull (menuBar);
  132. //top.Remove (top.StatusBar);
  133. //Assert.Null (top.StatusBar);
  134. //Assert.NotNull (statusBar);
  135. top.Remove (win);
  136. win = new () { Width = 60, Height = 15 };
  137. top.Add (win);
  138. // Application.Top without menu and status bar.
  139. View.GetLocationEnsuringFullVisibility (win, 0, 0, out nx, out ny /*, out sb*/);
  140. Assert.Equal (0, nx);
  141. Assert.Equal (0, ny);
  142. //Assert.Null (sb);
  143. top.Add (new MenuBar ());
  144. Assert.NotNull (top.MenuBar);
  145. // Application.Top with a menu and without status bar.
  146. View.GetLocationEnsuringFullVisibility (win, 2, 2, out nx, out ny /*, out sb*/);
  147. Assert.Equal (2, nx);
  148. Assert.Equal (2, ny);
  149. //Assert.Null (sb);
  150. top.Add (new StatusBar ());
  151. //Assert.NotNull (top.StatusBar);
  152. // Application.Top with a menu and status bar.
  153. View.GetLocationEnsuringFullVisibility (win, 30, 20, out nx, out ny /*, out sb*/);
  154. Assert.Equal (20, nx); // 20+60=80
  155. //Assert.Equal (9, ny); // 9+15+1(mb)=25
  156. //Assert.NotNull (sb);
  157. //Assert.Null (Toplevel._dragPosition);
  158. win.NewMouseEvent (new () { Position = new (6, 0), Flags = MouseFlags.Button1Pressed });
  159. // Assert.Equal (new Point (6, 0), Toplevel._dragPosition);
  160. win.NewMouseEvent (new () { Position = new (6, 0), Flags = MouseFlags.Button1Released });
  161. //Assert.Null (Toplevel._dragPosition);
  162. win.CanFocus = false;
  163. win.NewMouseEvent (new () { Position = new (6, 0), Flags = MouseFlags.Button1Pressed });
  164. //Assert.Null (Toplevel._dragPosition);
  165. #if DEBUG_IDISPOSABLE
  166. Assert.False (top.MenuBar.WasDisposed);
  167. //Assert.False (top.StatusBar.WasDisposed);
  168. #endif
  169. menuBar = top.MenuBar;
  170. //statusBar = top.StatusBar;
  171. top.Dispose ();
  172. Assert.Null (top.MenuBar);
  173. //Assert.Null (top.StatusBar);
  174. Assert.NotNull (menuBar);
  175. //Assert.NotNull (statusBar);
  176. #if DEBUG_IDISPOSABLE
  177. Assert.True (menuBar.WasDisposed);
  178. //Assert.True (statusBar.WasDisposed);
  179. #endif
  180. }
  181. [Fact]
  182. public void SuperViewChanged_Should_Not_Be_Used_To_Initialize_Toplevel_Events ()
  183. {
  184. var wasAdded = false;
  185. var view = new View ();
  186. view.SuperViewChanged += SuperViewChanged;
  187. var win = new Window ();
  188. win.Add (view);
  189. Application.Init (new FakeDriver ());
  190. Toplevel top = new ();
  191. top.Add (win);
  192. Assert.True (wasAdded);
  193. Application.Shutdown ();
  194. return;
  195. void SuperViewChanged (object sender, SuperViewChangedEventArgs _)
  196. {
  197. Assert.False (wasAdded);
  198. wasAdded = true;
  199. view.SuperViewChanged -= SuperViewChanged;
  200. }
  201. }
  202. [Fact]
  203. [AutoInitShutdown]
  204. public void Mouse_Drag_On_Top_With_Superview_Null ()
  205. {
  206. var win = new Window ();
  207. Toplevel top = new ();
  208. top.Add (win);
  209. int iterations = -1;
  210. Window testWindow;
  211. Application.Iteration += (s, a) =>
  212. {
  213. iterations++;
  214. if (iterations == 0)
  215. {
  216. ((FakeDriver)Application.Driver!).SetBufferSize (15, 7);
  217. // Don't use MessageBox here; it's too complicated for this unit test; just use Window
  218. testWindow = new ()
  219. {
  220. Text = "Hello",
  221. X = 2,
  222. Y = 2,
  223. Width = 10,
  224. Height = 3,
  225. Arrangement = ViewArrangement.Movable
  226. };
  227. Application.Run (testWindow);
  228. }
  229. else if (iterations == 1)
  230. {
  231. Assert.Equal (new (2, 2), Application.Top!.Frame.Location);
  232. }
  233. else if (iterations == 2)
  234. {
  235. Assert.Null (Application.MouseGrabView);
  236. // Grab the mouse
  237. Application.RaiseMouseEvent (new () { ScreenPosition = new (3, 2), Flags = MouseFlags.Button1Pressed });
  238. Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
  239. Assert.Equal (new (2, 2, 10, 3), Application.Top.Frame);
  240. }
  241. else if (iterations == 3)
  242. {
  243. Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
  244. // Drag to left
  245. Application.RaiseMouseEvent (
  246. new ()
  247. {
  248. ScreenPosition = new (2, 2), Flags = MouseFlags.Button1Pressed
  249. | MouseFlags.ReportMousePosition
  250. });
  251. Application.LayoutAndDraw ();
  252. Assert.Equal (Application.Top.Border, Application.MouseGrabView);
  253. Assert.Equal (new (1, 2, 10, 3), Application.Top.Frame);
  254. }
  255. else if (iterations == 4)
  256. {
  257. Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
  258. Assert.Equal (new (1, 2), Application.Top.Frame.Location);
  259. Assert.Equal (Application.Top.Border, Application.MouseGrabView);
  260. }
  261. else if (iterations == 5)
  262. {
  263. Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
  264. // Drag up
  265. Application.RaiseMouseEvent (
  266. new ()
  267. {
  268. ScreenPosition = new (2, 1),
  269. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  270. });
  271. Application.LayoutAndDraw ();
  272. Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
  273. Assert.Equal (new (1, 1, 10, 3), Application.Top.Frame);
  274. }
  275. else if (iterations == 6)
  276. {
  277. Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
  278. Assert.Equal (new (1, 1), Application.Top.Frame.Location);
  279. Assert.Equal (Application.Top.Border, Application.MouseGrabView);
  280. Assert.Equal (new (1, 1, 10, 3), Application.Top.Frame);
  281. }
  282. else if (iterations == 7)
  283. {
  284. Assert.Equal (Application.Top!.Border, Application.MouseGrabView);
  285. // Ungrab the mouse
  286. Application.RaiseMouseEvent (new () { ScreenPosition = new (2, 1), Flags = MouseFlags.Button1Released });
  287. Application.LayoutAndDraw ();
  288. Assert.Null (Application.MouseGrabView);
  289. }
  290. else if (iterations == 8)
  291. {
  292. Application.RequestStop ();
  293. }
  294. else if (iterations == 9)
  295. {
  296. Application.RequestStop ();
  297. }
  298. };
  299. Application.Run (top);
  300. top.Dispose ();
  301. }
  302. [Fact]
  303. [AutoInitShutdown]
  304. public void Mouse_Drag_On_Top_With_Superview_Not_Null ()
  305. {
  306. var win = new Window { X = 3, Y = 2, Width = 10, Height = 5, Arrangement = ViewArrangement.Movable };
  307. Toplevel top = new ();
  308. top.Add (win);
  309. int iterations = -1;
  310. var movex = 0;
  311. var movey = 0;
  312. var location = new Rectangle (win.Frame.X, win.Frame.Y, 7, 3);
  313. Application.Iteration += (s, a) =>
  314. {
  315. iterations++;
  316. if (iterations == 0)
  317. {
  318. ((FakeDriver)Application.Driver!).SetBufferSize (30, 10);
  319. }
  320. else if (iterations == 1)
  321. {
  322. location = win.Frame;
  323. Assert.Null (Application.MouseGrabView);
  324. // Grab the mouse
  325. Application.RaiseMouseEvent (
  326. new ()
  327. {
  328. ScreenPosition = new (win.Frame.X, win.Frame.Y), Flags = MouseFlags.Button1Pressed
  329. });
  330. Assert.Equal (win.Border, Application.MouseGrabView);
  331. }
  332. else if (iterations == 2)
  333. {
  334. Assert.Equal (win.Border, Application.MouseGrabView);
  335. // Drag to left
  336. movex = 1;
  337. movey = 0;
  338. Application.RaiseMouseEvent (
  339. new ()
  340. {
  341. ScreenPosition = new (win.Frame.X + movex, win.Frame.Y + movey), Flags =
  342. MouseFlags.Button1Pressed
  343. | MouseFlags.ReportMousePosition
  344. });
  345. Assert.Equal (win.Border, Application.MouseGrabView);
  346. }
  347. else if (iterations == 3)
  348. {
  349. // we should have moved +1, +0
  350. Assert.Equal (win.Border, Application.MouseGrabView);
  351. Assert.Equal (win.Border, Application.MouseGrabView);
  352. location.Offset (movex, movey);
  353. }
  354. else if (iterations == 4)
  355. {
  356. Assert.Equal (win.Border, Application.MouseGrabView);
  357. // Drag up
  358. movex = 0;
  359. movey = -1;
  360. Application.RaiseMouseEvent (
  361. new ()
  362. {
  363. ScreenPosition = new (win.Frame.X + movex, win.Frame.Y + movey), Flags =
  364. MouseFlags.Button1Pressed
  365. | MouseFlags.ReportMousePosition
  366. });
  367. Assert.Equal (win.Border, Application.MouseGrabView);
  368. }
  369. else if (iterations == 5)
  370. {
  371. // we should have moved +0, -1
  372. Assert.Equal (win.Border, Application.MouseGrabView);
  373. location.Offset (movex, movey);
  374. Assert.Equal (location, win.Frame);
  375. }
  376. else if (iterations == 6)
  377. {
  378. Assert.Equal (win.Border, Application.MouseGrabView);
  379. // Ungrab the mouse
  380. movex = 0;
  381. movey = 0;
  382. Application.RaiseMouseEvent (
  383. new ()
  384. {
  385. ScreenPosition = new (win.Frame.X + movex, win.Frame.Y + movey),
  386. Flags = MouseFlags.Button1Released
  387. });
  388. Assert.Null (Application.MouseGrabView);
  389. }
  390. else if (iterations == 7)
  391. {
  392. Application.RequestStop ();
  393. }
  394. };
  395. Application.Run (top);
  396. top.Dispose ();
  397. }
  398. [Fact]
  399. [SetupFakeDriver]
  400. public void GetLocationThatFits_With_Border_Null_Not_Throws ()
  401. {
  402. var top = new Toplevel ();
  403. top.BeginInit ();
  404. top.EndInit ();
  405. Exception exception = Record.Exception (() => ((FakeDriver)Application.Driver!).SetBufferSize (0, 10));
  406. Assert.Null (exception);
  407. exception = Record.Exception (() => ((FakeDriver)Application.Driver!).SetBufferSize (10, 0));
  408. Assert.Null (exception);
  409. }
  410. [Fact]
  411. [AutoInitShutdown]
  412. public void PositionCursor_SetCursorVisibility_To_Invisible_If_Focused_Is_Null ()
  413. {
  414. var tf = new TextField { Width = 5, Text = "test" };
  415. var view = new View { Width = 10, Height = 10, CanFocus = true };
  416. view.Add (tf);
  417. var top = new Toplevel ();
  418. top.Add (view);
  419. Application.Begin (top);
  420. Assert.True (tf.HasFocus);
  421. Application.PositionCursor ();
  422. Application.Driver!.GetCursorVisibility (out CursorVisibility cursor);
  423. Assert.Equal (CursorVisibility.Default, cursor);
  424. view.Enabled = false;
  425. Assert.False (tf.HasFocus);
  426. Application.PositionCursor ();
  427. Application.Driver!.GetCursorVisibility (out cursor);
  428. Assert.Equal (CursorVisibility.Invisible, cursor);
  429. top.Dispose ();
  430. }
  431. [Fact]
  432. [AutoInitShutdown]
  433. public void IsLoaded_Application_Begin ()
  434. {
  435. Toplevel top = new ();
  436. Assert.False (top.IsLoaded);
  437. Application.Begin (top);
  438. Assert.True (top.IsLoaded);
  439. top.Dispose ();
  440. }
  441. [Fact]
  442. [AutoInitShutdown]
  443. public void IsLoaded_With_Sub_Toplevel_Application_Begin_NeedDisplay ()
  444. {
  445. Toplevel top = new ();
  446. var subTop = new Toplevel ();
  447. var view = new View { Frame = new (0, 0, 20, 10) };
  448. subTop.Add (view);
  449. top.Add (subTop);
  450. Assert.False (top.IsLoaded);
  451. Assert.False (subTop.IsLoaded);
  452. Assert.Equal (new (0, 0, 20, 10), view.Frame);
  453. view.SubViewLayout += ViewLayoutStarted;
  454. void ViewLayoutStarted (object sender, LayoutEventArgs e)
  455. {
  456. Assert.Equal (new (0, 0, 20, 10), view._needsDrawRect);
  457. view.SubViewLayout -= ViewLayoutStarted;
  458. }
  459. Application.Begin (top);
  460. Assert.True (top.IsLoaded);
  461. Assert.True (subTop.IsLoaded);
  462. Assert.Equal (new (0, 0, 20, 10), view.Frame);
  463. view.Frame = new (1, 3, 10, 5);
  464. Assert.Equal (new (1, 3, 10, 5), view.Frame);
  465. Assert.Equal (new (0, 0, 10, 5), view._needsDrawRect);
  466. view.Frame = new (1, 3, 10, 5);
  467. top.Layout ();
  468. Assert.Equal (new (1, 3, 10, 5), view.Frame);
  469. Assert.Equal (new (0, 0, 10, 5), view._needsDrawRect);
  470. top.Dispose ();
  471. }
  472. [Fact]
  473. [AutoInitShutdown]
  474. public void Window_Viewport_Bigger_Than_Driver_Cols_And_Rows_Allow_Drag_Beyond_Left_Right_And_Bottom ()
  475. {
  476. Toplevel top = new ();
  477. var window = new Window { Width = 20, Height = 3, Arrangement = ViewArrangement.Movable };
  478. RunState rsTop = Application.Begin (top);
  479. ((FakeDriver)Application.Driver!).SetBufferSize (40, 10);
  480. RunState rsWindow = Application.Begin (window);
  481. Application.LayoutAndDraw ();
  482. Assert.Equal (new (0, 0, 40, 10), top.Frame);
  483. Assert.Equal (new (0, 0, 20, 3), window.Frame);
  484. Assert.Null (Application.MouseGrabView);
  485. Application.RaiseMouseEvent (new () { ScreenPosition = new (0, 0), Flags = MouseFlags.Button1Pressed });
  486. Assert.Equal (window.Border, Application.MouseGrabView);
  487. Application.RaiseMouseEvent (
  488. new ()
  489. {
  490. ScreenPosition = new (-11, -4), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  491. });
  492. Application.LayoutAndDraw ();
  493. Assert.Equal (new (0, 0, 40, 10), top.Frame);
  494. Assert.Equal (new (-11, -4, 20, 3), window.Frame);
  495. // Changes Top size to same size as Dialog more menu and scroll bar
  496. ((FakeDriver)Application.Driver!).SetBufferSize (20, 3);
  497. Application.RaiseMouseEvent (
  498. new ()
  499. {
  500. ScreenPosition = new (-1, -1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  501. });
  502. Application.LayoutAndDraw ();
  503. Assert.Equal (new (0, 0, 20, 3), top.Frame);
  504. Assert.Equal (new (-1, -1, 20, 3), window.Frame);
  505. // Changes Top size smaller than Dialog size
  506. ((FakeDriver)Application.Driver!).SetBufferSize (19, 2);
  507. Application.RaiseMouseEvent (
  508. new ()
  509. {
  510. ScreenPosition = new (-1, -1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  511. });
  512. Application.LayoutAndDraw ();
  513. Assert.Equal (new (0, 0, 19, 2), top.Frame);
  514. Assert.Equal (new (-1, -1, 20, 3), window.Frame);
  515. Application.RaiseMouseEvent (
  516. new ()
  517. {
  518. ScreenPosition = new (18, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  519. });
  520. Application.LayoutAndDraw ();
  521. Assert.Equal (new (0, 0, 19, 2), top.Frame);
  522. Assert.Equal (new (18, 1, 20, 3), window.Frame);
  523. // On a real app we can't go beyond the SuperView bounds
  524. Application.RaiseMouseEvent (
  525. new ()
  526. {
  527. ScreenPosition = new (19, 2), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  528. });
  529. Application.LayoutAndDraw ();
  530. Assert.Equal (new (0, 0, 19, 2), top.Frame);
  531. Assert.Equal (new (19, 2, 20, 3), window.Frame);
  532. //DriverAsserts.AssertDriverContentsWithFrameAre (@"", output);
  533. Application.End (rsWindow);
  534. Application.End (rsTop);
  535. top.Dispose ();
  536. }
  537. [Fact]
  538. [AutoInitShutdown]
  539. public void Modal_As_Top_Will_Drag_Cleanly ()
  540. {
  541. // Don't use Dialog as a Top, use a Window instead - dialog has complex layout behavior that is not needed here.
  542. var window = new Window { Width = 10, Height = 3, Arrangement = ViewArrangement.Movable };
  543. window.Add (
  544. new Label
  545. {
  546. X = Pos.Center (),
  547. Y = Pos.Center (),
  548. Width = Dim.Fill (),
  549. Height = Dim.Fill (),
  550. TextAlignment = Alignment.Center,
  551. VerticalTextAlignment = Alignment.Center,
  552. Text = "Test"
  553. }
  554. );
  555. RunState rs = Application.Begin (window);
  556. Assert.Null (Application.MouseGrabView);
  557. Assert.Equal (new (0, 0, 10, 3), window.Frame);
  558. Application.RaiseMouseEvent (new () { ScreenPosition = new (0, 0), Flags = MouseFlags.Button1Pressed });
  559. var firstIteration = false;
  560. Application.RunIteration (ref rs, firstIteration);
  561. Assert.Equal (window.Border, Application.MouseGrabView);
  562. Assert.Equal (new (0, 0, 10, 3), window.Frame);
  563. Application.RaiseMouseEvent (
  564. new ()
  565. {
  566. ScreenPosition = new (1, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  567. });
  568. firstIteration = false;
  569. Application.RunIteration (ref rs, firstIteration);
  570. Assert.Equal (window.Border, Application.MouseGrabView);
  571. Assert.Equal (new (1, 1, 10, 3), window.Frame);
  572. Application.End (rs);
  573. window.Dispose ();
  574. }
  575. [Fact]
  576. [AutoInitShutdown]
  577. public void Begin_With_Window_Sets_Size_Correctly ()
  578. {
  579. Toplevel top = new ();
  580. RunState rsTop = Application.Begin (top);
  581. ((FakeDriver)Application.Driver!).SetBufferSize (20, 20);
  582. var testWindow = new Window { X = 2, Y = 1, Width = 15, Height = 10 };
  583. Assert.Equal (new (2, 1, 15, 10), testWindow.Frame);
  584. RunState rsTestWindow = Application.Begin (testWindow);
  585. Assert.Equal (new (2, 1, 15, 10), testWindow.Frame);
  586. Application.End (rsTestWindow);
  587. Application.End (rsTop);
  588. top.Dispose ();
  589. }
  590. [Fact]
  591. [AutoInitShutdown]
  592. public void Activating_MenuBar_By_Alt_Key_Does_Not_Throw ()
  593. {
  594. var menu = new MenuBar
  595. {
  596. Menus =
  597. [
  598. new ("Child", new MenuItem [] { new ("_Create Child", "", null) })
  599. ]
  600. };
  601. var topChild = new Toplevel ();
  602. topChild.Add (menu);
  603. var top = new Toplevel ();
  604. top.Add (topChild);
  605. Application.Begin (top);
  606. Exception exception = Record.Exception (() => topChild.NewKeyDownEvent (KeyCode.AltMask));
  607. Assert.Null (exception);
  608. top.Dispose ();
  609. }
  610. [Fact]
  611. [TestRespondersDisposed]
  612. public void Multi_Thread_Toplevels ()
  613. {
  614. Application.Init (new FakeDriver ());
  615. Toplevel t = new ();
  616. var w = new Window ();
  617. t.Add (w);
  618. int count = 0, count1 = 0, count2 = 0;
  619. bool log = false, log1 = false, log2 = false;
  620. var fromTopStillKnowFirstIsRunning = false;
  621. var fromTopStillKnowSecondIsRunning = false;
  622. var fromFirstStillKnowSecondIsRunning = false;
  623. Application.AddTimeout (
  624. TimeSpan.FromMilliseconds (100),
  625. () =>
  626. {
  627. count++;
  628. if (count1 == 5)
  629. {
  630. log1 = true;
  631. }
  632. if (count1 == 14 && count2 == 10 && count == 15)
  633. {
  634. // count2 is already stopped
  635. fromTopStillKnowFirstIsRunning = true;
  636. }
  637. if (count1 == 7 && count2 == 7 && count == 8)
  638. {
  639. fromTopStillKnowSecondIsRunning = true;
  640. }
  641. if (count == 30)
  642. {
  643. Assert.Equal (30, count);
  644. Assert.Equal (20, count1);
  645. Assert.Equal (10, count2);
  646. Assert.True (log);
  647. Assert.True (log1);
  648. Assert.True (log2);
  649. Assert.True (fromTopStillKnowFirstIsRunning);
  650. Assert.True (fromTopStillKnowSecondIsRunning);
  651. Assert.True (fromFirstStillKnowSecondIsRunning);
  652. Application.RequestStop ();
  653. return false;
  654. }
  655. return true;
  656. }
  657. );
  658. t.Ready += FirstWindow;
  659. void FirstWindow (object sender, EventArgs args)
  660. {
  661. var firstWindow = new Window ();
  662. firstWindow.Ready += SecondWindow;
  663. Application.AddTimeout (
  664. TimeSpan.FromMilliseconds (100),
  665. () =>
  666. {
  667. count1++;
  668. if (count2 == 5)
  669. {
  670. log2 = true;
  671. }
  672. if (count2 == 4 && count1 == 5 && count == 5)
  673. {
  674. fromFirstStillKnowSecondIsRunning = true;
  675. }
  676. if (count1 == 20)
  677. {
  678. Assert.Equal (20, count1);
  679. Application.RequestStop ();
  680. return false;
  681. }
  682. return true;
  683. }
  684. );
  685. Application.Run (firstWindow);
  686. firstWindow.Dispose ();
  687. }
  688. void SecondWindow (object sender, EventArgs args)
  689. {
  690. var testWindow = new Window ();
  691. Application.AddTimeout (
  692. TimeSpan.FromMilliseconds (100),
  693. () =>
  694. {
  695. count2++;
  696. if (count < 30)
  697. {
  698. log = true;
  699. }
  700. if (count2 == 10)
  701. {
  702. Assert.Equal (10, count2);
  703. Application.RequestStop ();
  704. return false;
  705. }
  706. return true;
  707. }
  708. );
  709. Application.Run (testWindow);
  710. testWindow.Dispose ();
  711. }
  712. Application.Run (t);
  713. t.Dispose ();
  714. Application.Shutdown ();
  715. }
  716. [Fact]
  717. public void Remove_Do_Not_Dispose_MenuBar_Or_StatusBar ()
  718. {
  719. var mb = new MenuBar ();
  720. var sb = new StatusBar ();
  721. var tl = new Toplevel ();
  722. #if DEBUG
  723. Assert.False (mb.WasDisposed);
  724. Assert.False (sb.WasDisposed);
  725. #endif
  726. tl.Add (mb, sb);
  727. Assert.NotNull (tl.MenuBar);
  728. //Assert.NotNull (tl.StatusBar);
  729. #if DEBUG
  730. Assert.False (mb.WasDisposed);
  731. Assert.False (sb.WasDisposed);
  732. #endif
  733. tl.RemoveAll ();
  734. Assert.Null (tl.MenuBar);
  735. //Assert.Null (tl.StatusBar);
  736. #if DEBUG
  737. Assert.False (mb.WasDisposed);
  738. Assert.False (sb.WasDisposed);
  739. #endif
  740. }
  741. }