ControlTest.cs 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. //
  2. // Copyright (c) 2005 Novell, Inc.
  3. //
  4. // Authors:
  5. // Ritvik Mayank ([email protected])
  6. //
  7. using System;
  8. using System.Collections;
  9. using InvalidEnumArgumentException = System.ComponentModel.InvalidEnumArgumentException;
  10. using System.Drawing;
  11. using System.Reflection;
  12. using System.Runtime.Remoting;
  13. using System.Threading;
  14. using System.Windows.Forms;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Windows.Forms
  17. {
  18. [TestFixture]
  19. public class ControlTest
  20. {
  21. class Helper {
  22. public static void TestAccessibility(Control c, string Default, string Description, string Name, AccessibleRole Role)
  23. {
  24. Assert.IsNotNull (c.AccessibilityObject, "Acc1");
  25. Assert.AreEqual (Default, c.AccessibleDefaultActionDescription, "Acc2");
  26. Assert.AreEqual (Description, c.AccessibleDescription, "Acc3");
  27. Assert.AreEqual (Name, c.AccessibleName, "Acc4");
  28. Assert.AreEqual (Role, c.AccessibleRole, "Acc5");
  29. }
  30. public static string TestControl(Control container, Control start, bool forward) {
  31. Control ctl;
  32. ctl = container.GetNextControl(start, forward);
  33. if (ctl == null) {
  34. return null;
  35. }
  36. return ctl.Text;
  37. }
  38. }
  39. [Test]
  40. public void CreatedTest ()
  41. {
  42. Control c = new Control ();
  43. Assert.IsFalse (c.Created, "A1");
  44. }
  45. [Test]
  46. [Category ("NotWorking")]
  47. public void CreatedAccessibilityTest ()
  48. {
  49. Control c = new Control ();
  50. Assert.IsFalse (c.Created, "A1");
  51. Helper.TestAccessibility(c, null, null, null, AccessibleRole.Default);
  52. Assert.IsTrue (c.Created, "A2");
  53. c.Dispose ();
  54. Assert.IsFalse (c.Created, "A3");
  55. }
  56. [Test]
  57. [Category ("NotWorking")]
  58. public void BoundsTest ()
  59. {
  60. Control c = new Control ();
  61. Assert.IsTrue (c.Bounds.IsEmpty, "A1");
  62. Assert.IsTrue (c.Size.IsEmpty, "A2");
  63. Assert.IsTrue (c.ClientSize.IsEmpty, "A3");
  64. Assert.IsTrue (c.ClientRectangle.IsEmpty, "A4");
  65. Assert.AreEqual (((IWin32Window)c).Handle, c.Handle, "A5");
  66. /* this part fails on linux because we can't allocate X windows which are 0x0,
  67. and the Control bounds directly reflect the size of the X window */
  68. Assert.IsTrue (c.Bounds.IsEmpty, "A6");
  69. Assert.IsTrue (c.Size.IsEmpty, "A7");
  70. Assert.IsTrue (c.ClientSize.IsEmpty, "A8");
  71. Assert.IsTrue (c.ClientRectangle.IsEmpty, "A9");
  72. }
  73. [Test]
  74. public void PubPropTest()
  75. {
  76. Control c = new Control();
  77. Assert.IsFalse (c.AllowDrop , "A1");
  78. Assert.AreEqual(AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "A2");
  79. Assert.AreEqual ("Control", c.BackColor.Name , "B1");
  80. Assert.IsNull (c.BackgroundImage, "B2");
  81. Assert.IsNull (c.BindingContext, "B3");
  82. Assert.IsFalse (c.CanFocus, "C1");
  83. Assert.IsTrue (c.CanSelect, "C2");
  84. Assert.IsFalse (c.Capture, "C3");
  85. Assert.IsTrue (c.CausesValidation, "C4");
  86. Assert.IsNotNull (c.CompanyName, "C7");
  87. Assert.IsNull (c.Container, "C8");
  88. Assert.IsFalse (c.ContainsFocus, "C9");
  89. Assert.IsNull (c.ContextMenu, "C10");
  90. Assert.AreEqual (0, c.Controls.Count, "C11");
  91. Assert.IsFalse (c.Created, "C12");
  92. Assert.AreEqual (Cursors.Default, c.Cursor, "C13");
  93. Assert.IsNotNull(c.DataBindings, "D1");
  94. Assert.AreEqual("Control", Control.DefaultBackColor.Name, "D2");
  95. Assert.AreEqual("ControlText", Control.DefaultForeColor.Name, "D3");
  96. Assert.AreEqual(FontStyle.Regular, Control.DefaultFont.Style, "D4");
  97. Assert.AreEqual (new Rectangle(0, 0, 0, 0), c.DisplayRectangle , "D5");
  98. Assert.IsFalse (c.Disposing, "D6");
  99. Assert.AreEqual(DockStyle.None, c.Dock, "D7");
  100. Assert.IsTrue (c.Enabled, "E1");
  101. Assert.IsFalse (c.Focused, "F1");
  102. Assert.AreEqual (FontStyle.Regular, c.Font.Style, "F2");
  103. Assert.AreEqual (SystemColors.ControlText, c.ForeColor, "F3");
  104. Assert.IsFalse (c.HasChildren, "H2");
  105. Assert.AreEqual (ImeMode.NoControl, c.ImeMode, "I1");
  106. Assert.IsFalse (c.InvokeRequired, "I2");
  107. Assert.IsFalse (c.IsAccessible, "I3");
  108. Assert.IsFalse (c.IsDisposed, "I4");
  109. Assert.IsFalse (c.IsHandleCreated, "I5");
  110. Assert.AreEqual(Point.Empty, c.Location, "L2");
  111. #if NET_2_0
  112. Assert.IsTrue(c.MaximumSize.IsEmpty);
  113. Assert.IsTrue(c.MinimumSize.IsEmpty);
  114. #endif
  115. Assert.AreEqual (Keys.None, Control.ModifierKeys, "M1");
  116. Assert.IsFalse (Control.MousePosition.IsEmpty, "M2");
  117. Assert.AreEqual (MouseButtons.None, Control.MouseButtons, "M3");
  118. Assert.AreEqual("", c.Name, "N1");
  119. c.Name = "Control Name";
  120. Assert.AreEqual("Control Name", c.Name, "N2");
  121. Assert.IsNull (c.Parent, "P1");
  122. Assert.IsNotNull (c.ProductName, "P2");
  123. Assert.IsTrue (c.ProductName != "", "P3");
  124. Assert.IsNotNull (c.ProductVersion, "P4");
  125. Assert.IsTrue (c.ProductVersion != "", "P5");
  126. Assert.IsFalse (c.RecreatingHandle, "R1");
  127. Assert.IsNull (c.Region, "R2");
  128. Assert.AreEqual (RightToLeft.No, c.RightToLeft, "R4");
  129. Assert.IsNull (c.Site, "S1");
  130. Assert.AreEqual (0, c.TabIndex , "T1");
  131. Assert.IsTrue (c.TabStop, "T2");
  132. Assert.IsNull (c.Tag, "T3");
  133. Assert.AreEqual ("", c.Text, "T4");
  134. Assert.IsTrue (c.Visible, "V1");
  135. }
  136. [Test]
  137. public void SizeChangeTest ()
  138. {
  139. Form f = new Form ();
  140. Control c = new Control ();
  141. f.Controls.Add(c);
  142. f.Show();
  143. c.Resize += new EventHandler(SizeChangedTest_ResizeHandler);
  144. c.Tag = true;
  145. c.Size = c.Size;
  146. Assert.AreEqual (true, (bool) c.Tag, "#1");
  147. f.Close ();
  148. }
  149. private void SizeChangedTest_ResizeHandler (object sender, EventArgs e)
  150. {
  151. Console.WriteLine("Resized");
  152. ((Control) sender).Tag = false;
  153. }
  154. [Test]
  155. public void NegativeHeightTest ()
  156. {
  157. Control c = new Control ();
  158. IntPtr handle = c.Handle;
  159. c.Resize += new EventHandler(NegativeHeightTest_ResizeHandler);
  160. c.Tag = -2;
  161. c.Height = 2;
  162. c.Height = -2;
  163. Assert.AreEqual (0, (int) c.Tag, "#1");
  164. c.Dispose ();
  165. Assert.AreEqual (handle, handle, "Removes warning.");
  166. }
  167. private void NegativeHeightTest_ResizeHandler (object sender, EventArgs e)
  168. {
  169. Control c = (Control) sender;
  170. c.Tag = c.Height;
  171. }
  172. [Test]
  173. public void TopLevelControlTest () {
  174. Control c = new Control ();
  175. Assert.AreEqual(null, c.TopLevelControl, "T1");
  176. Panel p = new Panel ();
  177. p.Controls.Add (c);
  178. Assert.AreEqual(null, c.TopLevelControl, "T2");
  179. Form f = new Form ();
  180. f.ShowInTaskbar = false;
  181. f.Controls.Add (p);
  182. Assert.AreEqual (f, c.TopLevelControl, "T3");
  183. Assert.AreEqual (f, f.TopLevelControl, "T4");
  184. }
  185. [Test]
  186. public void RelationTest() {
  187. Control c1;
  188. Control c2;
  189. c1 = new Control();
  190. c2 = new Control();
  191. Assert.AreEqual(true , c1.Visible , "Rel1");
  192. Assert.AreEqual(false, c1.Contains(c2) , "Rel2");
  193. Assert.AreEqual("System.Windows.Forms.Control", c1.ToString() , "Rel3");
  194. c1.Controls.Add(c2);
  195. Assert.AreEqual(true , c2.Visible , "Rel4");
  196. Assert.AreEqual(true, c1.Contains(c2) , "Rel5");
  197. c1.Anchor = AnchorStyles.Top;
  198. c1.SuspendLayout ();
  199. c1.Anchor = AnchorStyles.Left ;
  200. c1.ResumeLayout ();
  201. Assert.AreEqual(AnchorStyles.Left , c1.Anchor, "Rel6");
  202. c1.SetBounds(10, 20, 30, 40) ;
  203. Assert.AreEqual(new Rectangle(10, 20, 30, 40), c1.Bounds, "Rel7");
  204. Assert.AreEqual(c1, c2.Parent, "Rel8");
  205. }
  206. [Test]
  207. [Category ("NotWorking")]
  208. public void TabOrder() {
  209. Form form;
  210. Control active;
  211. Label label1 = new Label(); // To test non-tabstop items as well
  212. Label label2 = new Label();
  213. GroupBox group1 = new GroupBox();
  214. GroupBox group2 = new GroupBox();
  215. GroupBox group3 = new GroupBox();
  216. TextBox text1 = new TextBox();
  217. RadioButton radio11 = new RadioButton();
  218. RadioButton radio12 = new RadioButton();
  219. RadioButton radio13 = new RadioButton();
  220. RadioButton radio14 = new RadioButton();
  221. RadioButton radio21 = new RadioButton();
  222. RadioButton radio22 = new RadioButton();
  223. RadioButton radio23 = new RadioButton();
  224. RadioButton radio24 = new RadioButton();
  225. RadioButton radio31 = new RadioButton();
  226. RadioButton radio32 = new RadioButton();
  227. RadioButton radio33 = new RadioButton();
  228. RadioButton radio34 = new RadioButton();
  229. form = new Form();
  230. form.ShowInTaskbar = false;
  231. form.ClientSize = new Size (520, 520);
  232. Assert.AreEqual(new Size(520, 520), form.ClientSize, "Tab1");
  233. form.Text = "SWF Taborder Test App Form";
  234. Assert.AreEqual("SWF Taborder Test App Form", form.Text, "Tab2");
  235. label1.Location = new Point(10, 10);
  236. Assert.AreEqual(new Point(10, 10), label1.Location, "Tab3");
  237. label1.Text = "Label1";
  238. form.Controls.Add(label1);
  239. label2.Location = new Point(200, 10);
  240. label2.Text = "Label2";
  241. form.Controls.Add(label2);
  242. group1.Text = "Group1";
  243. group2.Text = "Group2";
  244. group3.Text = "Group3";
  245. group1.Size = new Size(200, 400);
  246. group2.Size = new Size(200, 400);
  247. group3.Size = new Size(180, 180);
  248. Assert.AreEqual(new Size(180, 180), group3.Size, "Tab4");
  249. group1.Location = new Point(10, 40);
  250. group2.Location = new Point(220, 40);
  251. group3.Location = new Point(10, 210);
  252. group1.TabIndex = 30;
  253. Assert.AreEqual(30, group1.TabIndex, "Tab5");
  254. group1.TabStop = true;
  255. // Don't assign, test automatic assignment
  256. //group2.TabIndex = 0;
  257. group2.TabStop = true;
  258. Assert.AreEqual(0, group2.TabIndex, "Tab6");
  259. group3.TabIndex = 35;
  260. group3.TabStop = true;
  261. // Test default tab index
  262. Assert.AreEqual(0, radio11.TabIndex, "Tab7");
  263. text1.Text = "Edit Control";
  264. radio11.Text = "Radio 1-1 [Tab1]";
  265. radio12.Text = "Radio 1-2 [Tab2]";
  266. radio13.Text = "Radio 1-3 [Tab3]";
  267. radio14.Text = "Radio 1-4 [Tab4]";
  268. radio21.Text = "Radio 2-1 [Tab4]";
  269. radio22.Text = "Radio 2-2 [Tab3]";
  270. radio23.Text = "Radio 2-3 [Tab2]";
  271. radio24.Text = "Radio 2-4 [Tab1]";
  272. radio31.Text = "Radio 3-1 [Tab1]";
  273. radio32.Text = "Radio 3-2 [Tab3]";
  274. radio33.Text = "Radio 3-3 [Tab2]";
  275. radio34.Text = "Radio 3-4 [Tab4]";
  276. // We don't assign TabIndex for radio1X; test automatic assignment
  277. text1.TabStop = true;
  278. radio11.TabStop = true;
  279. radio21.TabIndex = 4;
  280. radio22.TabIndex = 3;
  281. radio23.TabIndex = 2;
  282. radio24.TabIndex = 1;
  283. radio24.TabStop = true;
  284. radio31.TabIndex = 11;
  285. radio31.TabStop = true;
  286. radio32.TabIndex = 13;
  287. radio33.TabIndex = 12;
  288. radio34.TabIndex = 14;
  289. text1.Location = new Point(10, 100);
  290. radio11.Location = new Point(10, 20);
  291. radio12.Location = new Point(10, 40);
  292. radio13.Location = new Point(10, 60);
  293. radio14.Location = new Point(10, 80);
  294. radio21.Location = new Point(10, 20);
  295. radio22.Location = new Point(10, 40);
  296. radio23.Location = new Point(10, 60);
  297. radio24.Location = new Point(10, 80);
  298. radio31.Location = new Point(10, 20);
  299. radio32.Location = new Point(10, 40);
  300. radio33.Location = new Point(10, 60);
  301. radio34.Location = new Point(10, 80);
  302. text1.Size = new Size(150, text1.PreferredHeight);
  303. radio11.Size = new Size(150, 20);
  304. radio12.Size = new Size(150, 20);
  305. radio13.Size = new Size(150, 20);
  306. radio14.Size = new Size(150, 20);
  307. radio21.Size = new Size(150, 20);
  308. radio22.Size = new Size(150, 20);
  309. radio23.Size = new Size(150, 20);
  310. radio24.Size = new Size(150, 20);
  311. radio31.Size = new Size(150, 20);
  312. radio32.Size = new Size(150, 20);
  313. radio33.Size = new Size(150, 20);
  314. radio34.Size = new Size(150, 20);
  315. group1.Controls.Add(text1);
  316. group1.Controls.Add(radio11);
  317. group1.Controls.Add(radio12);
  318. group1.Controls.Add(radio13);
  319. group1.Controls.Add(radio14);
  320. group2.Controls.Add(radio21);
  321. group2.Controls.Add(radio22);
  322. group2.Controls.Add(radio23);
  323. group2.Controls.Add(radio24);
  324. group3.Controls.Add(radio31);
  325. group3.Controls.Add(radio32);
  326. group3.Controls.Add(radio33);
  327. group3.Controls.Add(radio34);
  328. form.Controls.Add(group1);
  329. form.Controls.Add(group2);
  330. group2.Controls.Add(group3);
  331. // Perform some tests, the TabIndex stuff below will alter the outcome
  332. Assert.AreEqual(null, Helper.TestControl(group2, radio34, true), "Tab8");
  333. Assert.AreEqual(31, group2.TabIndex, "Tab9");
  334. // Does the taborder of containers and non-selectable things change behaviour?
  335. label1.TabIndex = 5;
  336. label2.TabIndex = 4;
  337. group1.TabIndex = 3;
  338. group2.TabIndex = 1;
  339. // Start verification
  340. Assert.AreEqual(null, Helper.TestControl(group2, radio34, true), "Tab10");
  341. Assert.AreEqual(radio24.Text, Helper.TestControl(group2, group2, true), "Tab11");
  342. Assert.AreEqual(radio31.Text, Helper.TestControl(group2, group3, true), "Tab12");
  343. Assert.AreEqual(null, Helper.TestControl(group1, radio14, true), "Tab13");
  344. Assert.AreEqual(radio23.Text, Helper.TestControl(group2, radio24, true), "Tab14");
  345. Assert.AreEqual(group3.Text, Helper.TestControl(group2, radio21, true), "Tab15");
  346. Assert.AreEqual(radio13.Text, Helper.TestControl(form, radio12, true), "Tab16");
  347. Assert.AreEqual(label2.Text, Helper.TestControl(form, radio14, true), "Tab17");
  348. Assert.AreEqual(group1.Text, Helper.TestControl(form, radio34, true), "Tab18");
  349. Assert.AreEqual(radio23.Text, Helper.TestControl(group2, radio24, true), "Tab19");
  350. // Sanity checks
  351. Assert.AreEqual(null, Helper.TestControl(radio11, radio21, true), "Tab20");
  352. Assert.AreEqual(text1.Text, Helper.TestControl(group1, radio21, true), "Tab21");
  353. Assert.AreEqual(radio14.Text, Helper.TestControl(form, label2, false), "Tab22");
  354. Assert.AreEqual(radio21.Text, Helper.TestControl(group2, group3, false), "Tab23");
  355. Assert.AreEqual(4, radio21.TabIndex, "Tab24");
  356. Assert.AreEqual(1, radio11.TabIndex, "Tab25");
  357. Assert.AreEqual(3, radio13.TabIndex, "Tab26");
  358. Assert.AreEqual(35, group3.TabIndex, "Tab27");
  359. Assert.AreEqual(1, group2.TabIndex, "Tab28");
  360. Assert.AreEqual(label1.Text, Helper.TestControl(form, form, false), "Tab29");
  361. Assert.AreEqual(radio14.Text, Helper.TestControl(group1, group1, false), "Tab30");
  362. Assert.AreEqual(radio34.Text, Helper.TestControl(group3, group3, false), "Tab31");
  363. Assert.AreEqual(null, Helper.TestControl(label1, label1, false), "Tab31");
  364. Assert.AreEqual(null, Helper.TestControl(radio11, radio21, false), "Tab32");
  365. form.Dispose ();
  366. }
  367. [Test]
  368. public void ScaleTest()
  369. {
  370. Control r1 = new Control();
  371. r1.Width = 40;
  372. r1.Height = 20;
  373. r1.Scale(2);
  374. Assert.AreEqual(80, r1.Width, "Scale1");
  375. Assert.AreEqual(40, r1.Height, "Scale2");
  376. }
  377. [Test]
  378. public void TextTest()
  379. {
  380. Control r1 = new Control();
  381. r1.Text = "Hi" ;
  382. Assert.AreEqual("Hi" , r1.Text , "Text1");
  383. r1.ResetText();
  384. Assert.AreEqual("" , r1.Text , "Text2");
  385. }
  386. [Test]
  387. public void PubMethodTest7()
  388. {
  389. Control r1 = new Control();
  390. r1.RightToLeft = RightToLeft.Yes ;
  391. r1.ResetRightToLeft() ;
  392. Assert.AreEqual(RightToLeft.No , r1.RightToLeft , "#81");
  393. r1.ImeMode = ImeMode.Off ;
  394. r1.ResetImeMode () ;
  395. Assert.AreEqual(ImeMode.NoControl , r1.ImeMode , "#82");
  396. r1.ForeColor= SystemColors.GrayText ;
  397. r1.ResetForeColor() ;
  398. Assert.AreEqual(SystemColors.ControlText , r1.ForeColor , "#83");
  399. //r1.Font = Font.FromHdc();
  400. r1.ResetFont () ;
  401. //Assert.AreEqual(FontFamily.GenericSansSerif , r1.Font , "#83");
  402. r1.Cursor = Cursors.Hand ;
  403. r1.ResetCursor () ;
  404. Assert.AreEqual(Cursors.Default , r1.Cursor , "#83");
  405. //r1.DataBindings = System.Windows.Forms.Binding ;
  406. //r1.ResetBindings() ;
  407. //Assert.AreEqual(ControlBindingsCollection , r1.DataBindings , "#83");
  408. r1.BackColor = Color.Black ;
  409. r1.ResetBackColor() ;
  410. Assert.AreEqual( SystemColors.Control , r1.BackColor , "#84");
  411. r1.BackColor = Color.Black ;
  412. r1.Refresh() ;
  413. Assert.AreEqual( null , r1.Region , "#85");
  414. Rectangle M = new Rectangle(10, 20, 30 ,40);
  415. r1.RectangleToScreen(M) ;
  416. Assert.AreEqual( null , r1.Region , "#86");
  417. }
  418. [Test]
  419. public void ScreenClientCoords()
  420. {
  421. Label l;
  422. Point p1;
  423. Point p2;
  424. Point p3;
  425. l = new Label();
  426. l.Left = 10;
  427. l.Top = 12;
  428. l.Visible = true;
  429. p1 = new Point (10,10);
  430. p2 = l.PointToScreen(p1);
  431. p3 = l.PointToClient(p2);
  432. Assert.AreEqual (p1, p3, "SC1");
  433. }
  434. [Test]
  435. public void ContainsTest ()
  436. {
  437. Control t = new Control ();
  438. Control s = new Control ();
  439. t.Controls.Add (s);
  440. Assert.AreEqual (true, t.Contains (s), "Con1");
  441. Assert.AreEqual (false, s.Contains (t), "Con2");
  442. Assert.AreEqual (false, s.Contains (null), "Con3");
  443. Assert.AreEqual (false, t.Contains (new Control ()), "Con4");
  444. }
  445. [Test]
  446. public void CreateHandleTest ()
  447. {
  448. Control parent;
  449. Control child;
  450. parent = null;
  451. child = null;
  452. try {
  453. parent = new Control ();
  454. child = new Control ();
  455. parent.Visible = true;
  456. parent.Controls.Add (child);
  457. Assert.IsFalse (parent.IsHandleCreated, "CH1");
  458. Assert.IsFalse (child.IsHandleCreated, "CH2");
  459. parent.CreateControl ();
  460. Assert.IsNotNull (parent.Handle, "CH3");
  461. Assert.IsNotNull (child.Handle, "CH4");
  462. Assert.IsTrue (parent.IsHandleCreated, "CH5");
  463. Assert.IsTrue (child.IsHandleCreated, "CH6");
  464. } finally {
  465. if (parent != null)
  466. parent.Dispose ();
  467. if (child != null)
  468. child.Dispose ();
  469. }
  470. // Accessing Handle Property creates the handle
  471. try {
  472. parent = new Control ();
  473. parent.Visible = true;
  474. child = new Control ();
  475. parent.Controls.Add (child);
  476. Assert.IsFalse (parent.IsHandleCreated, "CH7");
  477. Assert.IsFalse (child.IsHandleCreated, "CH8");
  478. Assert.IsNotNull (parent.Handle, "CH9");
  479. Assert.IsTrue (parent.IsHandleCreated, "CH10");
  480. Assert.IsTrue (child.IsHandleCreated, "CH11");
  481. } finally {
  482. if (parent != null)
  483. parent.Dispose ();
  484. if (child != null)
  485. child.Dispose ();
  486. }
  487. }
  488. [Test]
  489. [Category ("NotWorking")]
  490. public void CreateHandleTest2 ()
  491. {
  492. // This should eventually test all operations
  493. // that can be performed on a control (within
  494. // reason)
  495. Control c = new Control ();
  496. Assert.IsFalse (c.IsHandleCreated, "0");
  497. c.Width = 100;
  498. Assert.IsFalse (c.IsHandleCreated, "1");
  499. c.Height = 100;
  500. Assert.IsFalse (c.IsHandleCreated, "2");
  501. c.Name = "hi";
  502. Assert.IsFalse (c.IsHandleCreated, "3");
  503. c.Left = 5;
  504. Assert.IsFalse (c.IsHandleCreated, "5");
  505. c.Top = 5;
  506. Assert.IsFalse (c.IsHandleCreated, "6");
  507. c.Location = new Point (1,1);
  508. Assert.IsFalse (c.IsHandleCreated, "7");
  509. c.Region = new Region ();
  510. Assert.IsFalse (c.IsHandleCreated, "8");
  511. c.Size = new Size (100, 100);
  512. Assert.IsFalse (c.IsHandleCreated, "9");
  513. c.Text = "bye";
  514. Assert.IsFalse (c.IsHandleCreated, "10");
  515. c.Visible = !c.Visible;
  516. Assert.IsFalse (c.IsHandleCreated, "11");
  517. }
  518. [Test]
  519. public void CreateGraphicsTest ()
  520. {
  521. Graphics g = null;
  522. Pen p = null;
  523. try {
  524. Control c = new Control ();
  525. c.SetBounds (0,0, 20, 20);
  526. g = c.CreateGraphics ();
  527. Assert.IsNotNull (g, "Graph1");
  528. } finally {
  529. if (p != null)
  530. p.Dispose ();
  531. if (g != null)
  532. g.Dispose ();
  533. }
  534. }
  535. bool delegateCalled = false;
  536. public delegate void TestDelegate ();
  537. public void delegate_call () {
  538. delegateCalled = true;
  539. }
  540. [Test]
  541. [ExpectedException(typeof(InvalidOperationException))]
  542. public void InvokeException1 () {
  543. Control c = new Control ();
  544. IAsyncResult result;
  545. result = c.BeginInvoke (new TestDelegate (delegate_call));
  546. c.EndInvoke (result);
  547. }
  548. [Test]
  549. public void FindFormTest () {
  550. Form f = new Form ();
  551. f.ShowInTaskbar = false;
  552. f.Name = "form";
  553. Control c = null;
  554. try {
  555. f.Controls.Add (c = new Control ());
  556. Assert.AreEqual (f.Name, c.FindForm ().Name, "Find1");
  557. f.Controls.Remove (c);
  558. GroupBox g = new GroupBox ();
  559. g.Name = "box";
  560. f.Controls.Add (g);
  561. g.Controls.Add (c);
  562. Assert.AreEqual (f.Name, f.FindForm ().Name, "Find2");
  563. g.Controls.Remove (c);
  564. Assert.IsNull(c.FindForm (), "Find3");
  565. } finally {
  566. if (c != null)
  567. c.Dispose ();
  568. if (f != null)
  569. f.Dispose ();
  570. }
  571. }
  572. [Test]
  573. public void FocusTest ()
  574. {
  575. Form f = null;
  576. Button c = null, d = null;
  577. try {
  578. f = new Form ();
  579. f.ShowInTaskbar = false;
  580. f.Visible = true;
  581. c = new Button ();
  582. c.Visible = true;
  583. f.Controls.Add (c);
  584. d = new Button ();
  585. d.Visible = false;
  586. f.Controls.Add (d);
  587. Assert.IsTrue (c.CanFocus, "Focus1");
  588. Assert.IsFalse (c.Focused, "Focus2");
  589. c.Focus ();
  590. Assert.IsTrue (c.Focused, "Focus3");
  591. d.Focus ();
  592. Assert.IsFalse (d.Focused, "Focus4");
  593. d.Visible = true;
  594. d.Focus ();
  595. Assert.IsTrue (d.Focused, "Focus5");
  596. Assert.IsFalse (c.Focused, "Focus6");
  597. c.Enabled = false;
  598. Assert.IsFalse (c.Focused, "Focus7");
  599. } finally {
  600. if (f != null)
  601. f.Dispose ();
  602. if (c != null)
  603. c.Dispose ();
  604. if (d != null)
  605. d.Dispose ();
  606. }
  607. }
  608. [Test]
  609. public void FromHandleTest ()
  610. {
  611. Control c1 = null;
  612. Control c2 = null;
  613. try {
  614. c1 = new Control ();
  615. c2 = new Control ();
  616. c1.Name = "parent";
  617. c2.Name = "child";
  618. c1.Controls.Add(c2);
  619. // Handle
  620. Assert.AreEqual (c1.Name, Control.FromHandle (c1.Handle).Name, "Handle1");
  621. Assert.IsNull (Control.FromHandle (IntPtr.Zero), "Handle2");
  622. // ChildHandle
  623. Assert.AreEqual (c1.Name, Control.FromChildHandle (c1.Handle).Name, "Handle3");
  624. Assert.IsNull (Control.FromChildHandle (IntPtr.Zero), "Handle4");
  625. } finally {
  626. if (c1 != null)
  627. c1.Dispose ();
  628. if (c2 != null)
  629. c2.Dispose ();
  630. }
  631. }
  632. [Test]
  633. public void GetChildAtPointTest ()
  634. {
  635. Control c = null, d = null, e = null;
  636. try {
  637. c = new Control ();
  638. c.Name = "c1";
  639. c.SetBounds (0, 0, 100, 100);
  640. d = new Control ();
  641. d.Name = "d1";
  642. d.SetBounds (10, 10, 40, 40);
  643. c.Controls.Add (d);
  644. e = new Control ();
  645. e.Name = "e1";
  646. e.SetBounds (55, 55, 10, 10);
  647. Control l = c.GetChildAtPoint (new Point (15, 15));
  648. Assert.AreEqual (d.Name, l.Name, "Child1");
  649. Assert.IsFalse (e.Name == l.Name, "Child2");
  650. l = c.GetChildAtPoint (new Point (57, 57));
  651. Assert.IsNull (l, "Child3");
  652. l = c.GetChildAtPoint (new Point (10, 10));
  653. Assert.AreEqual (d.Name, l.Name, "Child4");
  654. // GetChildAtPointSkip is not implemented and the following test is breaking for Net_2_0 profile
  655. // #if NET_2_0
  656. // c.Controls.Add (e);
  657. // e.Visible = false;
  658. // l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
  659. // Assert.IsNull (l, "Child5");
  660. // e.Visible = true;
  661. // l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
  662. // Assert.AreSame (e.Name, l.Name, "Child6");
  663. // #endif // NET_2_0
  664. } finally {
  665. if (c != null)
  666. c.Dispose ();
  667. if (d != null)
  668. d.Dispose ();
  669. }
  670. }
  671. public class LayoutTestControl : Control {
  672. public int LayoutCount;
  673. public LayoutTestControl () : base() {
  674. LayoutCount = 0;
  675. }
  676. protected override void OnLayout(LayoutEventArgs levent) {
  677. LayoutCount++;
  678. base.OnLayout (levent);
  679. }
  680. }
  681. [Test]
  682. public void LayoutTest() {
  683. LayoutTestControl c;
  684. c = new LayoutTestControl();
  685. c.SuspendLayout();
  686. c.SuspendLayout();
  687. c.SuspendLayout();
  688. c.SuspendLayout();
  689. c.ResumeLayout(true);
  690. c.PerformLayout();
  691. c.ResumeLayout(true);
  692. c.PerformLayout();
  693. c.ResumeLayout(true);
  694. c.PerformLayout();
  695. c.ResumeLayout(true);
  696. c.PerformLayout();
  697. c.ResumeLayout(true);
  698. c.PerformLayout();
  699. c.ResumeLayout(true);
  700. c.PerformLayout();
  701. c.ResumeLayout(true);
  702. c.PerformLayout();
  703. c.SuspendLayout();
  704. c.PerformLayout();
  705. Assert.AreEqual(5, c.LayoutCount, "Layout Suspend/Resume locking does not bottom out at 0");
  706. }
  707. [Test]
  708. [ExpectedException(typeof(ArgumentException))]
  709. public void TransparentBackgroundTest1() {
  710. Control c;
  711. c = new Control();
  712. c.BackColor = Color.Transparent;
  713. }
  714. [Test]
  715. public void TransparentBackgroundTest2() {
  716. Panel c;
  717. c = new Panel();
  718. c.BackColor = Color.Transparent;
  719. Assert.AreEqual(Color.Transparent, c.BackColor, "Transparent background not set");
  720. }
  721. [Test]
  722. public void TransparentBackgroundTest3() {
  723. Control c;
  724. c = new Control();
  725. c.BackColor = Color.Empty;
  726. Assert.AreEqual(Control.DefaultBackColor, c.BackColor, "Setting empty color failed");
  727. }
  728. [Test]
  729. public void Dock_Value_Invalid ()
  730. {
  731. Control c = new Control ();
  732. try {
  733. c.Dock = (DockStyle) 666;
  734. Assert.Fail ("#1");
  735. } catch (InvalidEnumArgumentException ex) {
  736. Assert.AreEqual (typeof (InvalidEnumArgumentException), ex.GetType (), "#2");
  737. Assert.IsNotNull (ex.Message, "#3");
  738. Assert.IsNotNull (ex.ParamName, "#4");
  739. Assert.AreEqual ("value", ex.ParamName, "#5");
  740. Assert.IsNull (ex.InnerException, "#6");
  741. }
  742. }
  743. [Test]
  744. public void EnabledTest1() {
  745. Control child;
  746. Control parent;
  747. Control grandma;
  748. grandma = new Control();
  749. parent = new Control();
  750. child = new Control();
  751. grandma.Controls.Add(parent);
  752. parent.Controls.Add(child);
  753. grandma.Enabled = false;
  754. Assert.AreEqual(grandma.Enabled, child.Enabled, "Child did not inherit disabled state");
  755. }
  756. int EnabledCalledCount = 0;
  757. private void EnabledTest2EnabledChanged(object sender, EventArgs e) {
  758. EnabledCalledCount++;
  759. }
  760. [Test]
  761. public void EnabledTest2() {
  762. // Check nesting of enabled calls
  763. // OnEnabled is not called for disabled child controls
  764. Control child;
  765. Control parent;
  766. Control grandma;
  767. EnabledCalledCount = 0;
  768. grandma = new Control();
  769. parent = new Control();
  770. child = new Control();
  771. child.EnabledChanged += new EventHandler(EnabledTest2EnabledChanged);
  772. grandma.Controls.Add(parent);
  773. parent.Controls.Add(child);
  774. grandma.Enabled = false;
  775. Assert.AreEqual(1, EnabledCalledCount, "Child Enabled Event not properly fired");
  776. grandma.Enabled = true;
  777. Assert.AreEqual(2, EnabledCalledCount, "Child Enabled Event not properly fired");
  778. child.Enabled = false;
  779. grandma.Enabled = false;
  780. Assert.AreEqual(3, EnabledCalledCount, "Child Enabled Event not properly fired");
  781. }
  782. [Test]
  783. public void ControlsRemoveNullTest ()
  784. {
  785. Control c = new Control ();
  786. c.Controls.Remove (null);
  787. }
  788. [Test]
  789. public void ControlsAddNullTest ()
  790. {
  791. Control c = new Control ();
  792. c.Controls.Add (null);
  793. }
  794. [Test]
  795. [ExpectedException (typeof (ArgumentNullException))]
  796. public void ControlsSetChildIndexNullTest ()
  797. {
  798. Control c = new Control ();
  799. c.Controls.SetChildIndex (null, 1);
  800. }
  801. [Test]
  802. [ExpectedException (typeof (ArgumentNullException))]
  803. public void ControlsAddRangeNullTest ()
  804. {
  805. Control c = new Control ();
  806. c.Controls.AddRange (null);
  807. }
  808. [Test]
  809. public void ControlsAddRangeNullElementTest ()
  810. {
  811. Control c = new Control ();
  812. Control[] subcontrols = new Control[2];
  813. subcontrols[0] = new Control ();
  814. subcontrols[1] = null;
  815. c.Controls.AddRange (subcontrols);
  816. }
  817. [Test]
  818. public void RegionTest () {
  819. Form f = new Form ();
  820. f.ShowInTaskbar = false;
  821. Control c = new Control ();
  822. f.Controls.Add (c);
  823. Assert.IsNull (c.Region, "#A1");
  824. f.Show ();
  825. Assert.IsNull (c.Region, "#A2");
  826. c.Region = null;
  827. Assert.IsNull (c.Region, "#A3");
  828. f.Dispose ();
  829. Region region = new Region ();
  830. f = new Form ();
  831. f.ShowInTaskbar = false;
  832. c = new Control ();
  833. f.Controls.Add (c);
  834. c.Region = region;
  835. Assert.IsNotNull (c.Region, "#B1");
  836. Assert.AreSame (region, c.Region, "#B2");
  837. f.Show ();
  838. c.Region = null;
  839. Assert.IsNull (c.Region, "#B3");
  840. }
  841. }
  842. [TestFixture]
  843. public class ControlInvokeTest {
  844. public delegate void TestDelegate ();
  845. Form f;
  846. Control c;
  847. Thread control_t;
  848. ApplicationContext control_context;
  849. bool delegateCalled = false;
  850. object m;
  851. void CreateControl ()
  852. {
  853. f = new Form ();
  854. f.ShowInTaskbar = false;
  855. c = new Control ();
  856. f.Controls.Add (c);
  857. Console.WriteLine ("f.Handle = {0}", f.Handle);
  858. Console.WriteLine ("c.Handle = {0}", c.Handle);
  859. control_context = new ApplicationContext (f);
  860. Monitor.Enter (m);
  861. Console.WriteLine ("pulsing");
  862. Monitor.Pulse (m);
  863. Monitor.Exit (m);
  864. Console.WriteLine ("control thread running");
  865. Application.Run (control_context);
  866. c.Dispose ();
  867. }
  868. [Test]
  869. public void InvokeTest ()
  870. {
  871. m = new object ();
  872. control_t = new Thread(new ThreadStart(CreateControl));
  873. Monitor.Enter (m);
  874. control_t.Start ();
  875. Console.WriteLine ("waiting on monitor");
  876. Monitor.Wait (m);
  877. Console.WriteLine ("making async call");
  878. IAsyncResult result;
  879. result = c.BeginInvoke (new TestDelegate (delegate_call));
  880. c.EndInvoke (result);
  881. Assert.AreEqual (true, delegateCalled, "Invoke1");
  882. }
  883. public void delegate_call () {
  884. /* invoked on control_context's thread */
  885. delegateCalled = true;
  886. f.Dispose ();
  887. Application.Exit ();
  888. }
  889. }
  890. [TestFixture]
  891. public class ControlWMTest
  892. {
  893. [Test]
  894. public void WM_PARENTNOTIFY_Test ()
  895. {
  896. WMTester tester;
  897. Control child;
  898. int child_handle;
  899. tester = new WMTester ();
  900. child = new Control ();
  901. tester.Controls.Add (child);
  902. tester.Visible = true;
  903. child.Visible = true;
  904. child_handle = child.Handle.ToInt32 ();
  905. ArrayList msgs;
  906. Message m1;
  907. msgs = tester.Find (WndMsg.WM_PARENTNOTIFY);
  908. Assert.AreEqual (1, msgs.Count, "#1");
  909. m1 = (Message) msgs [0];
  910. Assert.AreEqual (WndMsg.WM_CREATE, ((WndMsg) LowOrder (m1.WParam)), "#2");
  911. //Assert.AreEqual (child.Identifier??, HighOrder (m1.WParam), "#3");
  912. Assert.AreEqual (child_handle, m1.LParam.ToInt32 (), "#4");
  913. child.Dispose ();
  914. msgs = tester.Find (WndMsg.WM_PARENTNOTIFY);
  915. Assert.AreEqual (2, msgs.Count, "#5");
  916. m1 = (Message) msgs [1];
  917. Assert.AreEqual (WndMsg.WM_DESTROY, ((WndMsg) LowOrder (m1.WParam)), "#6");
  918. //Assert.AreEqual (child.Identifier??, HighOrder (m1.WParam), "#7");
  919. Assert.AreEqual (child_handle, m1.LParam.ToInt32 (), "#8");
  920. tester.Dispose ();
  921. }
  922. internal static int LowOrder (int param)
  923. {
  924. return ((int)(short)(param & 0xffff));
  925. }
  926. internal static int HighOrder (int param)
  927. {
  928. return ((int)(short)(param >> 16));
  929. }
  930. internal static int LowOrder (IntPtr param)
  931. {
  932. return ((int)(short)(param.ToInt32 () & 0xffff));
  933. }
  934. internal static int HighOrder (IntPtr param)
  935. {
  936. return ((int)(short)(param.ToInt32 () >> 16));
  937. }
  938. internal class WMTester : Form
  939. {
  940. internal ArrayList Messages = new ArrayList ();
  941. internal bool Contains (WndMsg msg)
  942. {
  943. return Contains (msg, Messages);
  944. }
  945. internal bool Contains (WndMsg msg, ArrayList list)
  946. {
  947. foreach (Message m in Messages)
  948. {
  949. if (m.Msg == (int) msg)
  950. return true;
  951. }
  952. return false;
  953. }
  954. internal ArrayList Find (WndMsg msg)
  955. {
  956. ArrayList result = new ArrayList ();
  957. foreach (Message m in Messages)
  958. {
  959. if (m.Msg == (int) msg)
  960. result.Add (m);
  961. }
  962. return result;
  963. }
  964. protected override void WndProc(ref Message m)
  965. {
  966. Console.WriteLine ("WndProc: " + m.ToString ());
  967. Messages.Add (m);
  968. base.WndProc (ref m);
  969. }
  970. }
  971. }
  972. }