ControlTest.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. //
  2. // Copyright (c) 2005 Novell, Inc.
  3. //
  4. // Authors:
  5. // Ritvik Mayank ([email protected])
  6. //
  7. using System;
  8. using System.Windows.Forms;
  9. using System.Drawing;
  10. using System.Threading;
  11. using System.Reflection;
  12. using System.Runtime.Remoting;
  13. using NUnit.Framework;
  14. namespace MWF.MonoTest
  15. {
  16. [TestFixture]
  17. public class ControlTest
  18. {
  19. internal static void TestAccessibility(Control c, string Default, string Description, string Name, AccessibleRole Role) {
  20. Assert.AreEqual(false, c.AccessibilityObject == null, "Acc1");
  21. Assert.AreEqual(Default, c.AccessibleDefaultActionDescription, "Acc2");
  22. Assert.AreEqual(Description, c.AccessibleDescription, "Acc3");
  23. Assert.AreEqual(Name, c.AccessibleName, "Acc4");
  24. Assert.AreEqual(Role, c.AccessibleRole, "Acc5");
  25. }
  26. [Test]
  27. public void PubPropTest()
  28. {
  29. Control c = new Control();
  30. TestAccessibility(c, null, null, null, AccessibleRole.Default);
  31. // A
  32. Assert.AreEqual(false, c.AllowDrop , "A1");
  33. Assert.AreEqual(AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "A2");
  34. // B
  35. Assert.AreEqual("Control", c.BackColor.Name , "B1");
  36. Assert.AreEqual(null, c.BackgroundImage, "B2");
  37. Assert.AreEqual(null, c.BindingContext, "B3");
  38. Assert.AreEqual(0, c.Bottom, "B4");
  39. Assert.AreEqual (new Rectangle (0,0, 0, 0) , c.Bounds , "B5");
  40. // C
  41. Assert.AreEqual(false, c.CanFocus, "C1");
  42. Assert.AreEqual(true, c.CanSelect, "C2");
  43. Assert.AreEqual(false, c.Capture, "C3");
  44. Assert.AreEqual(true, c.CausesValidation, "C4");
  45. Assert.AreEqual (new Rectangle(0, 0, 0, 0) , c.ClientRectangle , "C5");
  46. Assert.AreEqual (new Size(0, 0), c.ClientSize , "C6");
  47. string name = c.CompanyName;
  48. if (!name.Equals("Mono Project, Novell, Inc.") && !name.Equals("Microsoft Corporation")) {
  49. Assert.Fail("CompanyName property does not match any accepted value - C7");
  50. }
  51. Assert.AreEqual(null, c.Container, "C8");
  52. Assert.AreEqual(false, c.ContainsFocus, "C9");
  53. Assert.AreEqual(null, c.ContextMenu, "C10");
  54. Assert.AreEqual(0, c.Controls.Count, "C11");
  55. Assert.AreEqual(true, c.Created, "C12");
  56. Assert.AreEqual(Cursors.Default, c.Cursor, "C13");
  57. // D
  58. Assert.AreEqual(false, c.DataBindings == null, "D1");
  59. Assert.AreEqual("Control", Control.DefaultBackColor.Name, "D2");
  60. Assert.AreEqual("ControlText", Control.DefaultForeColor.Name, "D3");
  61. Assert.AreEqual(FontStyle.Regular, Control.DefaultFont.Style, "D4");
  62. Assert.AreEqual (new Rectangle(0, 0, 0, 0), c.DisplayRectangle , "D5");
  63. Assert.AreEqual(false, c.Disposing, "D6");
  64. Assert.AreEqual(DockStyle.None, c.Dock, "D7");
  65. // E
  66. Assert.AreEqual(true, c.Enabled, "E1");
  67. // F
  68. Assert.AreEqual(false, c.Focused, "F1");
  69. Assert.AreEqual(FontStyle.Regular, c.Font.Style, "F2");
  70. Assert.AreEqual(SystemColors.ControlText, c.ForeColor, "F3");
  71. // G
  72. // H
  73. Assert.AreEqual (((IWin32Window)c).Handle, c.Handle, "H1");
  74. Assert.AreEqual(false, c.HasChildren, "H2");
  75. Assert.AreEqual(0, c.Height, "H3");
  76. // I
  77. Assert.AreEqual (ImeMode.NoControl, c.ImeMode, "I1");
  78. Assert.AreEqual(false, c.InvokeRequired, "I2");
  79. Assert.AreEqual(false, c.IsAccessible, "I3");
  80. Assert.AreEqual(false, c.IsDisposed, "I4");
  81. Assert.AreEqual(true, c.IsHandleCreated, "I5");
  82. // J
  83. // K
  84. // L
  85. Assert.AreEqual(0, c.Left, "L1");
  86. Assert.AreEqual(Point.Empty, c.Location, "L2");
  87. // M
  88. Assert.AreEqual(Keys.None, Control.ModifierKeys, "M1");
  89. Assert.AreEqual(false, Control.MousePosition.IsEmpty, "M2");
  90. Assert.AreEqual(MouseButtons.None, Control.MouseButtons, "M3");
  91. // N
  92. Assert.AreEqual("", c.Name, "N1");
  93. // O
  94. // P
  95. Assert.AreEqual(null, c.Parent, "P1");
  96. name = c.ProductName;
  97. if (!name.Equals("Novell Mono MWF") && !name.Equals("Microsoft (R) .NET Framework"))
  98. Assert.Fail("ProductName property does not match any accepted value - P2");
  99. name = c.ProductVersion;
  100. if (!name.Equals("1.1.4322.2032")) {
  101. Assert.Fail("This test is being run against the wrong framework version.\nExpected is Net 1.1sp1. - P3");
  102. }
  103. // R
  104. Assert.AreEqual(false, c.RecreatingHandle, "R1");
  105. Assert.AreEqual(null, c.Region, "R2");
  106. Assert.AreEqual(0, c.Right, "R3");
  107. Assert.AreEqual(RightToLeft.No, c.RightToLeft, "R4");
  108. // S
  109. Assert.AreEqual(null, c.Site, "S1");
  110. Assert.AreEqual (new Size(0, 0), c.Size, "S2");
  111. // T
  112. Assert.AreEqual(0, c.TabIndex , "T1");
  113. Assert.AreEqual(true, c.TabStop, "T2");
  114. Assert.AreEqual(null, c.Tag, "T3");
  115. Assert.AreEqual("", c.Text, "T4");
  116. Assert.AreEqual(0, c.Top, "T5");
  117. Assert.AreEqual(null, c.TopLevelControl, "T6");
  118. // U
  119. // V
  120. Assert.AreEqual(true, c.Visible, "V1");
  121. // W
  122. Assert.AreEqual(0, c.Width, "W1");
  123. // XYZ
  124. }
  125. [Test]
  126. public void RelationTest() {
  127. Control c1;
  128. Control c2;
  129. c1 = new Control();
  130. c2 = new Control();
  131. Assert.AreEqual(true , c1.Visible , "Rel1");
  132. Assert.AreEqual(false, c1.Contains(c2) , "Rel2");
  133. Assert.AreEqual("System.Windows.Forms.Control", c1.ToString() , "Rel3");
  134. c1.Controls.Add(c2);
  135. Assert.AreEqual(true , c2.Visible , "Rel4");
  136. Assert.AreEqual(true, c1.Contains(c2) , "Rel5");
  137. c1.Anchor = AnchorStyles.Top;
  138. c1.SuspendLayout ();
  139. c1.Anchor = AnchorStyles.Left ;
  140. c1.ResumeLayout ();
  141. Assert.AreEqual(AnchorStyles.Left , c1.Anchor, "Rel6");
  142. c1.SetBounds(10, 20, 30, 40) ;
  143. Assert.AreEqual(new Rectangle(10, 20, 30, 40), c1.Bounds, "Rel7");
  144. Assert.AreEqual(c1, c2.Parent, "Rel8");
  145. }
  146. private string TestControl(Control container, Control start, bool forward) {
  147. Control ctl;
  148. ctl = container.GetNextControl(start, forward);
  149. if (ctl == null) {
  150. return null;
  151. }
  152. return ctl.Text;
  153. }
  154. [Test]
  155. public void TabOrder() {
  156. Form form;
  157. Control active;
  158. Label label1 = new Label(); // To test non-tabstop items as well
  159. Label label2 = new Label();
  160. GroupBox group1 = new GroupBox();
  161. GroupBox group2 = new GroupBox();
  162. GroupBox group3 = new GroupBox();
  163. TextBox text1 = new TextBox();
  164. RadioButton radio11 = new RadioButton();
  165. RadioButton radio12 = new RadioButton();
  166. RadioButton radio13 = new RadioButton();
  167. RadioButton radio14 = new RadioButton();
  168. RadioButton radio21 = new RadioButton();
  169. RadioButton radio22 = new RadioButton();
  170. RadioButton radio23 = new RadioButton();
  171. RadioButton radio24 = new RadioButton();
  172. RadioButton radio31 = new RadioButton();
  173. RadioButton radio32 = new RadioButton();
  174. RadioButton radio33 = new RadioButton();
  175. RadioButton radio34 = new RadioButton();
  176. form = new Form();
  177. form.ClientSize = new System.Drawing.Size (520, 520);
  178. Assert.AreEqual(new Size(520, 520), form.ClientSize, "Tab1");
  179. form.Text = "SWF Taborder Test App Form";
  180. Assert.AreEqual("SWF Taborder Test App Form", form.Text, "Tab2");
  181. label1.Location = new Point(10, 10);
  182. Assert.AreEqual(new Point(10, 10), label1.Location, "Tab3");
  183. label1.Text = "Label1";
  184. form.Controls.Add(label1);
  185. label2.Location = new Point(200, 10);
  186. label2.Text = "Label2";
  187. form.Controls.Add(label2);
  188. group1.Text = "Group1";
  189. group2.Text = "Group2";
  190. group3.Text = "Group3";
  191. group1.Size = new Size(200, 400);
  192. group2.Size = new Size(200, 400);
  193. group3.Size = new Size(180, 180);
  194. Assert.AreEqual(new Size(180, 180), group3.Size, "Tab4");
  195. group1.Location = new Point(10, 40);
  196. group2.Location = new Point(220, 40);
  197. group3.Location = new Point(10, 210);
  198. group1.TabIndex = 30;
  199. Assert.AreEqual(30, group1.TabIndex, "Tab5");
  200. group1.TabStop = true;
  201. // Don't assign, test automatic assignment
  202. //group2.TabIndex = 0;
  203. group2.TabStop = true;
  204. Assert.AreEqual(0, group2.TabIndex, "Tab6");
  205. group3.TabIndex = 35;
  206. group3.TabStop = true;
  207. // Test default tab index
  208. Assert.AreEqual(0, radio11.TabIndex, "Tab7");
  209. text1.Text = "Edit Control";
  210. radio11.Text = "Radio 1-1 [Tab1]";
  211. radio12.Text = "Radio 1-2 [Tab2]";
  212. radio13.Text = "Radio 1-3 [Tab3]";
  213. radio14.Text = "Radio 1-4 [Tab4]";
  214. radio21.Text = "Radio 2-1 [Tab4]";
  215. radio22.Text = "Radio 2-2 [Tab3]";
  216. radio23.Text = "Radio 2-3 [Tab2]";
  217. radio24.Text = "Radio 2-4 [Tab1]";
  218. radio31.Text = "Radio 3-1 [Tab1]";
  219. radio32.Text = "Radio 3-2 [Tab3]";
  220. radio33.Text = "Radio 3-3 [Tab2]";
  221. radio34.Text = "Radio 3-4 [Tab4]";
  222. // We don't assign TabIndex for radio1X; test automatic assignment
  223. text1.TabStop = true;
  224. radio11.TabStop = true;
  225. radio21.TabIndex = 4;
  226. radio22.TabIndex = 3;
  227. radio23.TabIndex = 2;
  228. radio24.TabIndex = 1;
  229. radio24.TabStop = true;
  230. radio31.TabIndex = 11;
  231. radio31.TabStop = true;
  232. radio32.TabIndex = 13;
  233. radio33.TabIndex = 12;
  234. radio34.TabIndex = 14;
  235. text1.Location = new Point(10, 100);
  236. radio11.Location = new Point(10, 20);
  237. radio12.Location = new Point(10, 40);
  238. radio13.Location = new Point(10, 60);
  239. radio14.Location = new Point(10, 80);
  240. radio21.Location = new Point(10, 20);
  241. radio22.Location = new Point(10, 40);
  242. radio23.Location = new Point(10, 60);
  243. radio24.Location = new Point(10, 80);
  244. radio31.Location = new Point(10, 20);
  245. radio32.Location = new Point(10, 40);
  246. radio33.Location = new Point(10, 60);
  247. radio34.Location = new Point(10, 80);
  248. text1.Size = new Size(150, text1.PreferredHeight);
  249. radio11.Size = new Size(150, 20);
  250. radio12.Size = new Size(150, 20);
  251. radio13.Size = new Size(150, 20);
  252. radio14.Size = new Size(150, 20);
  253. radio21.Size = new Size(150, 20);
  254. radio22.Size = new Size(150, 20);
  255. radio23.Size = new Size(150, 20);
  256. radio24.Size = new Size(150, 20);
  257. radio31.Size = new Size(150, 20);
  258. radio32.Size = new Size(150, 20);
  259. radio33.Size = new Size(150, 20);
  260. radio34.Size = new Size(150, 20);
  261. group1.Controls.Add(text1);
  262. group1.Controls.Add(radio11);
  263. group1.Controls.Add(radio12);
  264. group1.Controls.Add(radio13);
  265. group1.Controls.Add(radio14);
  266. group2.Controls.Add(radio21);
  267. group2.Controls.Add(radio22);
  268. group2.Controls.Add(radio23);
  269. group2.Controls.Add(radio24);
  270. group3.Controls.Add(radio31);
  271. group3.Controls.Add(radio32);
  272. group3.Controls.Add(radio33);
  273. group3.Controls.Add(radio34);
  274. form.Controls.Add(group1);
  275. form.Controls.Add(group2);
  276. group2.Controls.Add(group3);
  277. // Perform some tests, the TabIndex stuff below will alter the outcome
  278. Assert.AreEqual(null, TestControl(group2, radio34, true), "Tab8");
  279. Assert.AreEqual(31, group2.TabIndex, "Tab9");
  280. // Does the taborder of containers and non-selectable things change behaviour?
  281. label1.TabIndex = 5;
  282. label2.TabIndex = 4;
  283. group1.TabIndex = 3;
  284. group2.TabIndex = 1;
  285. // Start verification
  286. Assert.AreEqual(null, TestControl(group2, radio34, true), "Tab10");
  287. Assert.AreEqual(radio24.Text, TestControl(group2, group2, true), "Tab11");
  288. Assert.AreEqual(radio31.Text, TestControl(group2, group3, true), "Tab12");
  289. Assert.AreEqual(null, TestControl(group1, radio14, true), "Tab13");
  290. Assert.AreEqual(radio23.Text, TestControl(group2, radio24, true), "Tab14");
  291. Assert.AreEqual(group3.Text, TestControl(group2, radio21, true), "Tab15");
  292. Assert.AreEqual(radio13.Text, TestControl(form, radio12, true), "Tab16");
  293. Assert.AreEqual(label2.Text, TestControl(form, radio14, true), "Tab17");
  294. Assert.AreEqual(group1.Text, TestControl(form, radio34, true), "Tab18");
  295. Assert.AreEqual(radio23.Text, TestControl(group2, radio24, true), "Tab19");
  296. // Sanity checks
  297. Assert.AreEqual(null, TestControl(radio11, radio21, true), "Tab20");
  298. Assert.AreEqual(text1.Text, TestControl(group1, radio21, true), "Tab21");
  299. Assert.AreEqual(radio14.Text, TestControl(form, label2, false), "Tab22");
  300. Assert.AreEqual(radio21.Text, TestControl(group2, group3, false), "Tab23");
  301. Assert.AreEqual(4, radio21.TabIndex, "Tab24");
  302. Assert.AreEqual(1, radio11.TabIndex, "Tab25");
  303. Assert.AreEqual(3, radio13.TabIndex, "Tab26");
  304. Assert.AreEqual(35, group3.TabIndex, "Tab27");
  305. Assert.AreEqual(1, group2.TabIndex, "Tab28");
  306. Assert.AreEqual(label1.Text, TestControl(form, form, false), "Tab29");
  307. Assert.AreEqual(radio14.Text, TestControl(group1, group1, false), "Tab30");
  308. Assert.AreEqual(radio34.Text, TestControl(group3, group3, false), "Tab31");
  309. Assert.AreEqual(null, TestControl(label1, label1, false), "Tab31");
  310. Assert.AreEqual(null, TestControl(radio11, radio21, false), "Tab32");
  311. }
  312. [Test]
  313. public void ScaleTest()
  314. {
  315. Control r1 = new Control();
  316. r1.Width = 40;
  317. r1.Height = 20;
  318. r1.Scale(2);
  319. Assert.AreEqual(80, r1.Width, "Scale1");
  320. Assert.AreEqual(40, r1.Height, "Scale2");
  321. }
  322. [Test]
  323. public void TextTest()
  324. {
  325. Control r1 = new Control();
  326. r1.Text = "Hi" ;
  327. Assert.AreEqual("Hi" , r1.Text , "Text1");
  328. r1.ResetText();
  329. Assert.AreEqual("" , r1.Text , "Text2");
  330. }
  331. [Test]
  332. public void PubMethodTest7()
  333. {
  334. Control r1 = new Control();
  335. r1.RightToLeft = RightToLeft.Yes ;
  336. r1.ResetRightToLeft() ;
  337. Assert.AreEqual(RightToLeft.No , r1.RightToLeft , "#81");
  338. r1.ImeMode = ImeMode.Off ;
  339. r1.ResetImeMode () ;
  340. Assert.AreEqual(ImeMode.NoControl , r1.ImeMode , "#82");
  341. r1.ForeColor= SystemColors.GrayText ;
  342. r1.ResetForeColor() ;
  343. Assert.AreEqual(SystemColors.ControlText , r1.ForeColor , "#83");
  344. //r1.Font = Font.FromHdc();
  345. r1.ResetFont () ;
  346. //Assert.AreEqual(FontFamily.GenericSansSerif , r1.Font , "#83");
  347. r1.Cursor = Cursors.Hand ;
  348. r1.ResetCursor () ;
  349. Assert.AreEqual(Cursors.Default , r1.Cursor , "#83");
  350. //r1.DataBindings = System.Windows.Forms.Binding ;
  351. //r1.ResetBindings() ;
  352. //Assert.AreEqual(ControlBindingsCollection , r1.DataBindings , "#83");
  353. r1.BackColor = System.Drawing.Color.Black ;
  354. r1.ResetBackColor() ;
  355. Assert.AreEqual( SystemColors.Control , r1.BackColor , "#84");
  356. r1.BackColor = System.Drawing.Color.Black ;
  357. r1.Refresh() ;
  358. Assert.AreEqual( null , r1.Region , "#85");
  359. Rectangle M = new Rectangle(10, 20, 30 ,40);
  360. r1.RectangleToScreen(M) ;
  361. Assert.AreEqual( null , r1.Region , "#86");
  362. }
  363. [Test]
  364. public void ScreenClientCoords()
  365. {
  366. Label l;
  367. Point p1;
  368. Point p2;
  369. Point p3;
  370. l = new Label();
  371. l.Left = 10;
  372. l.Top = 12;
  373. l.Visible = true;
  374. p1 = new Point (10,10);
  375. p2 = l.PointToScreen(p1);
  376. p3 = l.PointToClient(p2);
  377. Assert.AreEqual (p1, p3, "SC1");
  378. }
  379. [Test]
  380. public void ContainsTest ()
  381. {
  382. Control t = new Control ();
  383. Control s = new Control ();
  384. t.Controls.Add (s);
  385. Assert.AreEqual (true, t.Contains (s), "Con1");
  386. Assert.AreEqual (false, s.Contains (t), "Con2");
  387. Assert.AreEqual (false, s.Contains (null), "Con3");
  388. Assert.AreEqual (false, t.Contains (new Control ()), "Con4");
  389. }
  390. [Test]
  391. public void CreateHandleTest ()
  392. {
  393. Control parent;
  394. Control child;
  395. parent = null;
  396. child = null;
  397. try {
  398. parent = new Control ();
  399. child = new Control ();
  400. parent.Visible = true;
  401. parent.Controls.Add (child);
  402. Assert.IsFalse (parent.IsHandleCreated, "CH1");
  403. Assert.IsFalse (child.IsHandleCreated, "CH2");
  404. parent.CreateControl ();
  405. Assert.IsNotNull (parent.Handle, "CH3");
  406. Assert.IsNotNull (child.Handle, "CH4");
  407. Assert.IsTrue (parent.IsHandleCreated, "CH5");
  408. Assert.IsTrue (child.IsHandleCreated, "CH6");
  409. } finally {
  410. if (parent != null)
  411. parent.Dispose ();
  412. if (child != null)
  413. child.Dispose ();
  414. }
  415. // Accessing Handle Property creates the handle
  416. try {
  417. parent = new Control ();
  418. parent.Visible = true;
  419. child = new Control ();
  420. parent.Controls.Add (child);
  421. Assert.IsFalse (parent.IsHandleCreated, "CH7");
  422. Assert.IsFalse (child.IsHandleCreated, "CH8");
  423. Assert.IsNotNull (parent.Handle, "CH9");
  424. Assert.IsTrue (parent.IsHandleCreated, "CH10");
  425. Assert.IsTrue (child.IsHandleCreated, "CH11");
  426. } finally {
  427. if (parent != null)
  428. parent.Dispose ();
  429. if (child != null)
  430. child.Dispose ();
  431. }
  432. }
  433. [Test]
  434. public void CreateGraphicsTest ()
  435. {
  436. Graphics g = null;
  437. Pen p = null;
  438. try {
  439. Control c = new Control ();
  440. c.SetBounds (0,0, 20, 20);
  441. g = c.CreateGraphics ();
  442. Assert.IsNotNull (g, "Graph1");
  443. } finally {
  444. if (p != null)
  445. p.Dispose ();
  446. if (g != null)
  447. g.Dispose ();
  448. }
  449. }
  450. bool delegateCalled = false;
  451. public delegate void TestDelegate ();
  452. [Test]
  453. [ExpectedException(typeof(System.InvalidOperationException))]
  454. public void InvokeException1 () {
  455. Control c = new Control ();
  456. IAsyncResult result;
  457. result = c.BeginInvoke (new TestDelegate (delegate_call));
  458. c.EndInvoke (result);
  459. }
  460. [Test]
  461. public void InvokeTest () {
  462. Control c = null;
  463. try {
  464. c = new Control ();
  465. IAsyncResult result;
  466. c.CreateControl ();
  467. result = c.BeginInvoke (new TestDelegate (delegate_call));
  468. c.EndInvoke (result);
  469. Assert.AreEqual (true, delegateCalled, "Invoke1");
  470. } finally {
  471. if (c != null)
  472. c.Dispose ();
  473. }
  474. }
  475. public void delegate_call () {
  476. delegateCalled = true;
  477. }
  478. [Test]
  479. public void FindFormTest () {
  480. Form f = new Form ();
  481. f.Name = "form";
  482. Control c = null;
  483. try {
  484. f.Controls.Add (c = new Control ());
  485. Assert.AreEqual (f.Name, c.FindForm ().Name, "Find1");
  486. f.Controls.Remove (c);
  487. GroupBox g = new GroupBox ();
  488. g.Name = "box";
  489. f.Controls.Add (g);
  490. g.Controls.Add (c);
  491. Assert.AreEqual (f.Name, f.FindForm ().Name, "Find2");
  492. g.Controls.Remove (c);
  493. Assert.IsNull(c.FindForm (), "Find3");
  494. } finally {
  495. if (c != null)
  496. c.Dispose ();
  497. if (f != null)
  498. f.Dispose ();
  499. }
  500. }
  501. [Test]
  502. public void FocusTest ()
  503. {
  504. Form f = null;
  505. Button c = null, d = null;
  506. try {
  507. f = new Form ();
  508. f.Visible = true;
  509. c = new Button ();
  510. c.Visible = true;
  511. f.Controls.Add (c);
  512. d = new Button ();
  513. d.Visible = false;
  514. f.Controls.Add (d);
  515. Assert.IsTrue (c.CanFocus, "Focus1");
  516. Assert.IsFalse (c.Focused, "Focus2");
  517. c.Focus ();
  518. Assert.IsTrue (c.Focused, "Focus3");
  519. d.Focus ();
  520. Assert.IsFalse (d.Focused, "Focus4");
  521. d.Visible = true;
  522. d.Focus ();
  523. Assert.IsTrue (d.Focused, "Focus5");
  524. Assert.IsFalse (c.Focused, "Focus6");
  525. c.Enabled = false;
  526. Assert.IsFalse (c.Focused, "Focus7");
  527. } finally {
  528. if (f != null)
  529. f.Dispose ();
  530. if (c != null)
  531. c.Dispose ();
  532. if (d != null)
  533. d.Dispose ();
  534. }
  535. }
  536. [Test]
  537. public void FromHandleTest ()
  538. {
  539. Control c1 = null;
  540. Control c2 = null;
  541. try {
  542. c1 = new Control ();
  543. c2 = new Control ();
  544. c1.Name = "parent";
  545. c2.Name = "child";
  546. c1.Controls.Add(c2);
  547. // Handle
  548. Assert.AreEqual (c1.Name, Control.FromHandle (c1.Handle).Name, "Handle1");
  549. Assert.IsNull (Control.FromHandle (IntPtr.Zero), "Handle2");
  550. // ChildHandle
  551. Assert.AreEqual (c1.Name, Control.FromChildHandle (c1.Handle).Name, "Handle3");
  552. Assert.IsNull (Control.FromChildHandle (IntPtr.Zero), "Handle4");
  553. } finally {
  554. if (c1 != null)
  555. c1.Dispose ();
  556. if (c2 != null)
  557. c2.Dispose ();
  558. }
  559. }
  560. [Test]
  561. public void GetChildAtPointTest ()
  562. {
  563. Control c = null, d = null, e = null;
  564. try {
  565. c = new Control ();
  566. c.Name = "c1";
  567. c.SetBounds (0, 0, 100, 100);
  568. d = new Control ();
  569. d.Name = "d1";
  570. d.SetBounds (10, 10, 40, 40);
  571. c.Controls.Add (d);
  572. e = new Control ();
  573. e.Name = "e1";
  574. e.SetBounds (55, 55, 10, 10);
  575. Control l = c.GetChildAtPoint (new Point (15, 15));
  576. Assert.AreEqual (d.Name, l.Name, "Child1");
  577. Assert.IsFalse (e.Name == l.Name, "Child2");
  578. l = c.GetChildAtPoint (new Point (57, 57));
  579. Assert.IsNull (l, "Child3");
  580. l = c.GetChildAtPoint (new Point (10, 10));
  581. Assert.AreEqual (d.Name, l.Name, "Child4");
  582. // GetChildAtPointSkip is not implemented and the following test is breaking for Net_2_0 profile
  583. // #if NET_2_0
  584. // c.Controls.Add (e);
  585. // e.Visible = false;
  586. // l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
  587. // Assert.IsNull (l, "Child5");
  588. // e.Visible = true;
  589. // l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
  590. // Assert.AreSame (e.Name, l.Name, "Child6");
  591. // #endif // NET_2_0
  592. } finally {
  593. if (c != null)
  594. c.Dispose ();
  595. if (d != null)
  596. d.Dispose ();
  597. }
  598. }
  599. public class LayoutTestControl : Control {
  600. public int LayoutCount;
  601. public LayoutTestControl () : base() {
  602. LayoutCount = 0;
  603. }
  604. protected override void OnLayout(LayoutEventArgs levent) {
  605. LayoutCount++;
  606. base.OnLayout (levent);
  607. }
  608. }
  609. [Test]
  610. public void LayoutTest() {
  611. LayoutTestControl c;
  612. c = new LayoutTestControl();
  613. c.SuspendLayout();
  614. c.SuspendLayout();
  615. c.SuspendLayout();
  616. c.SuspendLayout();
  617. c.ResumeLayout(true);
  618. c.PerformLayout();
  619. c.ResumeLayout(true);
  620. c.PerformLayout();
  621. c.ResumeLayout(true);
  622. c.PerformLayout();
  623. c.ResumeLayout(true);
  624. c.PerformLayout();
  625. c.ResumeLayout(true);
  626. c.PerformLayout();
  627. c.ResumeLayout(true);
  628. c.PerformLayout();
  629. c.ResumeLayout(true);
  630. c.PerformLayout();
  631. c.SuspendLayout();
  632. c.PerformLayout();
  633. Assert.AreEqual(5, c.LayoutCount, "Layout Suspend/Resume locking does not bottom out at 0");
  634. }
  635. }
  636. }