ControlTest.cs 31 KB

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