ToplevelTests.cs 31 KB

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