ControlTest.cs 21 KB

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