ControlTest.cs 25 KB

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