MdiFormTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. //
  2. // MdiFormTest.cs: Test cases for MDI Forms.
  3. //
  4. // Author:
  5. // Rolf Bjarne Kvinge ([email protected])
  6. //
  7. // (C) 2006 Novell, Inc. (http://www.novell.com)
  8. //
  9. using System;
  10. using System.Drawing;
  11. using System.Reflection;
  12. using System.Windows.Forms;
  13. using NUnit.Framework;
  14. namespace MonoTests.System.Windows.Forms
  15. {
  16. [TestFixture]
  17. public class MdiFormTest
  18. {
  19. private Form main;
  20. private Form child1;
  21. private Form child2;
  22. private Form child3;
  23. [TearDown]
  24. public void TearDown ()
  25. {
  26. if (main != null)
  27. main.Dispose ();
  28. if (child1 != null)
  29. child1.Dispose ();
  30. if (child2 != null)
  31. child2.Dispose ();
  32. if (child3 != null)
  33. child3.Dispose ();
  34. main = null;
  35. child1 = null;
  36. child2 = null;
  37. child3 = null;
  38. }
  39. // No attribute here since this is supposed to be called from
  40. // each test directly, not by nunit.
  41. public void SetUp (bool only_create, bool only_text)
  42. {
  43. TearDown ();
  44. main = new Form ();
  45. child1 = new Form ();
  46. child2 = new Form ();
  47. child3 = new Form ();
  48. if (only_create)
  49. return;
  50. main.Text = main.Name = "main";
  51. child1.Text = child1.Name = "child1";
  52. child2.Text = child2.Name = "child2";
  53. child3.Text = child3.Name = "child3";
  54. if (only_text)
  55. return;
  56. main.IsMdiContainer = true;
  57. }
  58. [Test]
  59. public void IsMdiContainerTest ()
  60. {
  61. SetUp (false, true);
  62. main.Visible = true;
  63. main.Visible = false;
  64. main.IsMdiContainer = true;
  65. child1.MdiParent = main;
  66. main.IsMdiContainer = false;
  67. Assert.AreSame (null, main.ActiveMdiChild, "#1");
  68. main.Visible = true;
  69. Assert.AreSame (null, main.ActiveMdiChild, "#2");
  70. Assert.AreSame (null, main.MdiParent, "#3");
  71. TearDown ();
  72. }
  73. [Category("NotWorking")]
  74. [Test]
  75. [ExpectedException(typeof(ArgumentException), "Cannot add a top level control to a control.")]
  76. public void AddToControlsTest ()
  77. {
  78. SetUp (false, true);
  79. main.Visible = true;
  80. main.Visible = false;
  81. main.Controls.Add (child1);
  82. TearDown ();
  83. }
  84. [Test]
  85. public void Text ()
  86. {
  87. Form main = null, child1 = null, child2 = null, child3 = null;
  88. main = new Form ();
  89. main.IsMdiContainer = true;
  90. main.Name = "main";
  91. main.Text = main.Name;
  92. main.Show();
  93. Assert.AreEqual ("main", main.Text, "#1");
  94. child1 = new Form ();
  95. child1.Name = "child1";
  96. child1.MdiParent = main;
  97. child1.Text = child1.Name;
  98. child1.WindowState = FormWindowState.Maximized;
  99. child1.Show ();
  100. Assert.AreEqual ("main - [child1]", main.Text, "#2");
  101. child2 = new Form ();
  102. child2.Name = "child2";
  103. child1.MdiParent = main;
  104. child2.Text = string.Empty;
  105. child2.WindowState = FormWindowState.Maximized;
  106. child2.Show();
  107. Assert.AreEqual ("main - [child1]", main.Text, "#3");
  108. child1.Activate();
  109. Assert.AreEqual ("main - [child1]", main.Text, "#4");
  110. child1.WindowState = FormWindowState.Minimized;
  111. Assert.AreEqual ("main", main.Text, "#5");
  112. child2.Activate ();
  113. Assert.AreEqual ("main", main.Text, "#6");
  114. child2.WindowState = FormWindowState.Maximized;
  115. Assert.AreEqual ("main", main.Text, "#7");
  116. child3 = new Form ();
  117. child3.Name = "child3";
  118. child3.MdiParent = main;
  119. child3.Text = child3.Name;
  120. child3.WindowState = FormWindowState.Maximized;
  121. child3.Show ();
  122. Assert.AreEqual ("main - [child3]", main.Text, "#8");
  123. child3.WindowState = FormWindowState.Normal;
  124. Assert.AreEqual ("main", main.Text, "#9");
  125. main.Text = string.Empty;
  126. child3.WindowState = FormWindowState.Maximized;
  127. Assert.AreEqual (" - [child3]", main.Text, "#10");
  128. child3.Text = string.Empty;
  129. Assert.AreEqual (string.Empty, main.Text, "#11");
  130. child3.Dispose ();
  131. child2.Dispose ();
  132. child1.Dispose ();
  133. main.Dispose ();
  134. }
  135. [Test]
  136. public void Text_MdiContainer ()
  137. {
  138. Form main = new Form ();
  139. main.ShowInTaskbar = false;
  140. main.Text = "main";
  141. main.IsMdiContainer = true;
  142. main.Show ();
  143. Assert.AreEqual ("main", main.Text, "#1");
  144. Form child = new Form ();
  145. child.Name = "child";
  146. child.MdiParent = main;
  147. child.Text = child.Name;
  148. child.WindowState = FormWindowState.Maximized;
  149. child.Show ();
  150. Assert.AreEqual ("main - [child]", main.Text, "#2");
  151. }
  152. [Test] // bug 80038
  153. public void Text_ChildClose ()
  154. {
  155. Form main = new Form ();
  156. main.ShowInTaskbar = false;
  157. main.IsMdiContainer = true;
  158. main.Text = "main";
  159. main.Show ();
  160. Assert.AreEqual ("main", main.Text, "#1");
  161. Form child = new Form ();
  162. child.Name = "child";
  163. child.MdiParent = main;
  164. child.Text = child.Name;
  165. child.WindowState = FormWindowState.Maximized;
  166. child.Show ();
  167. Assert.AreEqual ("main - [child]", main.Text, "#2");
  168. child.Close ();
  169. Assert.AreEqual ("main", main.Text, "#3");
  170. }
  171. [Test]
  172. public void Text_Maximized ()
  173. {
  174. Form main = new Form ();
  175. main.IsMdiContainer = true;
  176. main.Name = "main";
  177. main.Text = main.Name;
  178. main.Show ();
  179. Assert.AreEqual ("main", main.Text, "#1");
  180. Form child1 = new Form ();
  181. child1.Name = "child1";
  182. child1.MdiParent = main;
  183. child1.Text = child1.Name;
  184. child1.WindowState = FormWindowState.Maximized;
  185. child1.Show ();
  186. Assert.AreEqual ("main - [child1]", main.Text, "#2");
  187. Form child2 = new Form ();
  188. child2.Name = "child2";
  189. child2.MdiParent = main;
  190. child2.Text = child2.Name;
  191. child2.WindowState = FormWindowState.Maximized;
  192. child2.Show ();
  193. Assert.AreEqual ("main - [child2]", main.Text, "#3");
  194. child1.WindowState = FormWindowState.Maximized;
  195. Assert.AreEqual ("main - [child1]", main.Text, "#4");
  196. }
  197. [Test]
  198. public void ActiveMdiChild ()
  199. {
  200. Form main, child1, child2;
  201. main = new Form ();
  202. main.IsMdiContainer = true;
  203. main.Name = "main";
  204. child1 = new Form ();
  205. child1.Name = "child1";
  206. child1.MdiParent = main;
  207. child1.WindowState = FormWindowState.Maximized;
  208. child1.Show ();
  209. child2 = new Form ();
  210. child2.Name = "child2";
  211. child2.MdiParent = main;
  212. child2.Show();
  213. Assert.IsNull (main.ActiveMdiChild, "#1");
  214. main.Show ();
  215. Assert.AreSame (child2, main.ActiveMdiChild, "#2");
  216. child1.WindowState = FormWindowState.Maximized;
  217. Assert.AreSame (child1, main.ActiveMdiChild, "#3");
  218. child2.WindowState = FormWindowState.Maximized;
  219. Assert.AreSame (child2, main.ActiveMdiChild, "#4");
  220. main.Visible = false;
  221. #if NET_2_0
  222. Assert.IsNull (main.ActiveMdiChild, "#5");
  223. #else
  224. Assert.AreSame (child2, main.ActiveMdiChild, "#5");
  225. #endif
  226. child2.Dispose ();
  227. child1.Dispose ();
  228. main.Dispose ();
  229. main.Close();
  230. }
  231. [Test]
  232. public void ActiveMdiChild2 ()
  233. {
  234. SetUp (false, false);
  235. child1.MdiParent = main;
  236. child2.MdiParent = main;
  237. main.Show ();
  238. child1.Show ();
  239. child2.Show ();
  240. child1.Activate ();
  241. child1.Visible = false;
  242. Assert.AreSame (child2, main.ActiveMdiChild, "#1");
  243. TearDown ();
  244. }
  245. [Test]
  246. public void ActiveMdiChild3 ()
  247. {
  248. SetUp (false, false);
  249. child1.MdiParent = main;
  250. child2.MdiParent = main;
  251. main.Visible = true;
  252. main.Visible = false;
  253. Assert.AreSame (null, main.ActiveMdiChild, "#1");
  254. //child2.Visible = true; This will cause StackOverflowException on MS.
  255. main.Visible = true;
  256. Assert.AreSame (null, main.ActiveMdiChild, "#2");
  257. TearDown ();
  258. }
  259. [Test]
  260. public void MdiChild_WindowState1 ()
  261. {
  262. Form main = null, child1 = null, child2 = null;
  263. try {
  264. main = new Form ();
  265. main.IsMdiContainer = true;
  266. main.Name = "main";
  267. child1 = new Form ();
  268. child1.Name = "child1";
  269. child1.MdiParent = main;
  270. child1.WindowState = FormWindowState.Maximized;
  271. child1.Show ();
  272. child2 = new Form ();
  273. child2.Name = "child2";
  274. child2.MdiParent = main;
  275. child2.Show();
  276. Assert.AreEqual (FormWindowState.Maximized, child1.WindowState, "#1");
  277. Assert.AreEqual (FormWindowState.Normal, child2.WindowState, "#2");
  278. main.Show ();
  279. Assert.AreEqual (FormWindowState.Normal, child1.WindowState, "#3");
  280. Assert.AreEqual (FormWindowState.Maximized, child2.WindowState, "#4");
  281. } finally {
  282. child2.Dispose ();
  283. child1.Dispose ();
  284. main.Dispose ();
  285. main.Close();
  286. }
  287. }
  288. [Test]
  289. public void MdiChild_WindowState2 ()
  290. {
  291. Form main = null, child1 = null, child2 = null;
  292. try{
  293. main = new Form ();
  294. main.Name = "main";
  295. main.IsMdiContainer = true;
  296. main.Show ();
  297. child1 = new Form ();
  298. child1.Name = "child1";
  299. child1.MdiParent = main;
  300. child1.WindowState = FormWindowState.Maximized;
  301. child1.Show ();
  302. child2 = new Form ();
  303. child2.Name = "child2";
  304. child2.MdiParent = main;
  305. child2.Show();
  306. Assert.AreEqual (FormWindowState.Normal, child1.WindowState, "#1");
  307. Assert.AreEqual (FormWindowState.Maximized, child2.WindowState, "#2");
  308. } finally {
  309. child2.Dispose ();
  310. child1.Dispose ();
  311. main.Dispose ();
  312. main.Close();
  313. }
  314. }
  315. [Test]
  316. public void MdiChild_WindowState3 ()
  317. {
  318. Form main = null, child1 = null, child2 = null;
  319. try {
  320. main = new Form ();
  321. main.IsMdiContainer = true;
  322. main.Show ();
  323. child1 = new Form ();
  324. child1.MdiParent = main;
  325. child1.Show ();
  326. child2 = new Form ();
  327. child2.MdiParent = main;
  328. child2.WindowState = FormWindowState.Maximized;
  329. child2.Show();
  330. Assert.AreEqual (FormWindowState.Normal, child1.WindowState, "#1");
  331. Assert.AreEqual (FormWindowState.Maximized, child2.WindowState, "#2");
  332. } finally {
  333. child2.Dispose ();
  334. child1.Dispose ();
  335. main.Dispose ();
  336. main.Close();
  337. }
  338. }
  339. [Test]
  340. public void MdiChild_WindowState4 ()
  341. {
  342. Form main = null, child1 = null, child2 = null;
  343. try {
  344. main = new Form ();
  345. main.IsMdiContainer = true;
  346. main.Show ();
  347. child1 = new Form ();
  348. child1.MdiParent = main;
  349. child1.WindowState = FormWindowState.Maximized;
  350. child1.Show ();
  351. child2 = new Form ();
  352. child2.MdiParent = main;
  353. child2.WindowState = FormWindowState.Maximized;
  354. Assert.AreEqual (FormWindowState.Maximized, child1.WindowState, "#1");
  355. Assert.AreEqual (FormWindowState.Maximized, child2.WindowState, "#2");
  356. child2.Show();
  357. Assert.AreEqual (FormWindowState.Normal, child1.WindowState, "#3");
  358. Assert.AreEqual (FormWindowState.Maximized, child2.WindowState, "#4");
  359. child2.WindowState = FormWindowState.Normal;
  360. Assert.AreEqual (FormWindowState.Normal, child1.WindowState, "#5");
  361. Assert.AreEqual (FormWindowState.Normal, child2.WindowState, "#6");
  362. } finally {
  363. child2.Dispose ();
  364. child1.Dispose ();
  365. main.Dispose ();
  366. main.Close();
  367. }
  368. }
  369. [Test]
  370. public void MdiChild_WindowState5 ()
  371. {
  372. Form main = null, child1 = null, child2 = null;
  373. try {
  374. main = new Form ();
  375. main.Name = "main";
  376. main.IsMdiContainer = true;
  377. main.Show ();
  378. child1 = new Form ();
  379. child1.Name = "child1";
  380. child1.MdiParent = main;
  381. child1.WindowState = FormWindowState.Maximized;
  382. child1.Show ();
  383. child2 = new Form ();
  384. child2.Name = "child2";
  385. child2.MdiParent = main;
  386. child2.WindowState = FormWindowState.Maximized;
  387. Assert.AreEqual (FormWindowState.Maximized, child1.WindowState, "#1");
  388. Assert.AreEqual (FormWindowState.Maximized, child2.WindowState, "#2");
  389. child2.Show();
  390. Assert.AreEqual (FormWindowState.Normal, child1.WindowState, "#3");
  391. Assert.AreEqual (FormWindowState.Maximized, child2.WindowState, "#4");
  392. child1.Activate ();
  393. Assert.AreEqual (FormWindowState.Maximized, child1.WindowState, "#5");
  394. Assert.AreEqual (FormWindowState.Normal, child2.WindowState, "#6");
  395. } finally {
  396. child2.Dispose ();
  397. child1.Dispose ();
  398. main.Dispose ();
  399. main.Close();
  400. }
  401. }
  402. [Test]
  403. public void MdiChild_WindowState6 ()
  404. {
  405. Form main = null, child1 = null, child2 = null;
  406. try {
  407. main = new Form ();
  408. main.Name = "main";
  409. main.IsMdiContainer = true;
  410. main.Show ();
  411. child1 = new Form ();
  412. child1.Name = "child1";
  413. child1.MdiParent = main;
  414. child1.WindowState = FormWindowState.Minimized;
  415. child1.Show ();
  416. child2 = new Form ();
  417. child2.Name = "child2";
  418. child2.MdiParent = main;
  419. child2.WindowState = FormWindowState.Maximized;
  420. Assert.AreEqual (FormWindowState.Minimized, child1.WindowState, "#1");
  421. Assert.AreEqual (FormWindowState.Maximized, child2.WindowState, "#2");
  422. child2.Show();
  423. Assert.AreEqual (FormWindowState.Minimized, child1.WindowState, "#3");
  424. Assert.AreEqual (FormWindowState.Maximized, child2.WindowState, "#4");
  425. child1.Activate ();
  426. Assert.AreEqual (FormWindowState.Maximized, child1.WindowState, "#5");
  427. Assert.AreEqual (FormWindowState.Normal, child2.WindowState, "#6");
  428. child2.Activate ();
  429. Assert.AreEqual (FormWindowState.Minimized, child1.WindowState, "#7");
  430. Assert.AreEqual (FormWindowState.Maximized, child2.WindowState, "#8");
  431. } finally {
  432. child2.Dispose ();
  433. child1.Dispose ();
  434. main.Dispose ();
  435. main.Close();
  436. }
  437. }
  438. }
  439. }