ControlTest.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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. [Category ("NotWorking")]
  487. public void CreateHandleTest2 ()
  488. {
  489. // This should eventually test all operations
  490. // that can be performed on a control (within
  491. // reason)
  492. Control c = new Control ();
  493. Assert.IsFalse (c.IsHandleCreated, "0");
  494. c.Width = 100;
  495. Assert.IsFalse (c.IsHandleCreated, "1");
  496. c.Height = 100;
  497. Assert.IsFalse (c.IsHandleCreated, "2");
  498. c.Name = "hi";
  499. Assert.IsFalse (c.IsHandleCreated, "3");
  500. c.Left = 5;
  501. Assert.IsFalse (c.IsHandleCreated, "5");
  502. c.Top = 5;
  503. Assert.IsFalse (c.IsHandleCreated, "6");
  504. c.Location = new Point (1,1);
  505. Assert.IsFalse (c.IsHandleCreated, "7");
  506. c.Region = new Region ();
  507. Assert.IsFalse (c.IsHandleCreated, "8");
  508. c.Size = new Size (100, 100);
  509. Assert.IsFalse (c.IsHandleCreated, "9");
  510. c.Text = "bye";
  511. Assert.IsFalse (c.IsHandleCreated, "10");
  512. c.Visible = !c.Visible;
  513. Assert.IsFalse (c.IsHandleCreated, "11");
  514. }
  515. [Test]
  516. public void CreateGraphicsTest ()
  517. {
  518. Graphics g = null;
  519. Pen p = null;
  520. try {
  521. Control c = new Control ();
  522. c.SetBounds (0,0, 20, 20);
  523. g = c.CreateGraphics ();
  524. Assert.IsNotNull (g, "Graph1");
  525. } finally {
  526. if (p != null)
  527. p.Dispose ();
  528. if (g != null)
  529. g.Dispose ();
  530. }
  531. }
  532. bool delegateCalled = false;
  533. public delegate void TestDelegate ();
  534. public void delegate_call () {
  535. delegateCalled = true;
  536. }
  537. [Test]
  538. [ExpectedException(typeof(InvalidOperationException))]
  539. public void InvokeException1 () {
  540. Control c = new Control ();
  541. IAsyncResult result;
  542. result = c.BeginInvoke (new TestDelegate (delegate_call));
  543. c.EndInvoke (result);
  544. }
  545. [Test]
  546. public void FindFormTest () {
  547. Form f = new Form ();
  548. f.ShowInTaskbar = false;
  549. f.Name = "form";
  550. Control c = null;
  551. try {
  552. f.Controls.Add (c = new Control ());
  553. Assert.AreEqual (f.Name, c.FindForm ().Name, "Find1");
  554. f.Controls.Remove (c);
  555. GroupBox g = new GroupBox ();
  556. g.Name = "box";
  557. f.Controls.Add (g);
  558. g.Controls.Add (c);
  559. Assert.AreEqual (f.Name, f.FindForm ().Name, "Find2");
  560. g.Controls.Remove (c);
  561. Assert.IsNull(c.FindForm (), "Find3");
  562. } finally {
  563. if (c != null)
  564. c.Dispose ();
  565. if (f != null)
  566. f.Dispose ();
  567. }
  568. }
  569. [Test]
  570. public void FocusTest ()
  571. {
  572. Form f = null;
  573. Button c = null, d = null;
  574. try {
  575. f = new Form ();
  576. f.ShowInTaskbar = false;
  577. f.Visible = true;
  578. c = new Button ();
  579. c.Visible = true;
  580. f.Controls.Add (c);
  581. d = new Button ();
  582. d.Visible = false;
  583. f.Controls.Add (d);
  584. Assert.IsTrue (c.CanFocus, "Focus1");
  585. Assert.IsFalse (c.Focused, "Focus2");
  586. c.Focus ();
  587. Assert.IsTrue (c.Focused, "Focus3");
  588. d.Focus ();
  589. Assert.IsFalse (d.Focused, "Focus4");
  590. d.Visible = true;
  591. d.Focus ();
  592. Assert.IsTrue (d.Focused, "Focus5");
  593. Assert.IsFalse (c.Focused, "Focus6");
  594. c.Enabled = false;
  595. Assert.IsFalse (c.Focused, "Focus7");
  596. } finally {
  597. if (f != null)
  598. f.Dispose ();
  599. if (c != null)
  600. c.Dispose ();
  601. if (d != null)
  602. d.Dispose ();
  603. }
  604. }
  605. [Test]
  606. public void FromHandleTest ()
  607. {
  608. Control c1 = null;
  609. Control c2 = null;
  610. try {
  611. c1 = new Control ();
  612. c2 = new Control ();
  613. c1.Name = "parent";
  614. c2.Name = "child";
  615. c1.Controls.Add(c2);
  616. // Handle
  617. Assert.AreEqual (c1.Name, Control.FromHandle (c1.Handle).Name, "Handle1");
  618. Assert.IsNull (Control.FromHandle (IntPtr.Zero), "Handle2");
  619. // ChildHandle
  620. Assert.AreEqual (c1.Name, Control.FromChildHandle (c1.Handle).Name, "Handle3");
  621. Assert.IsNull (Control.FromChildHandle (IntPtr.Zero), "Handle4");
  622. } finally {
  623. if (c1 != null)
  624. c1.Dispose ();
  625. if (c2 != null)
  626. c2.Dispose ();
  627. }
  628. }
  629. [Test]
  630. public void GetChildAtPointTest ()
  631. {
  632. Control c = null, d = null, e = null;
  633. try {
  634. c = new Control ();
  635. c.Name = "c1";
  636. c.SetBounds (0, 0, 100, 100);
  637. d = new Control ();
  638. d.Name = "d1";
  639. d.SetBounds (10, 10, 40, 40);
  640. c.Controls.Add (d);
  641. e = new Control ();
  642. e.Name = "e1";
  643. e.SetBounds (55, 55, 10, 10);
  644. Control l = c.GetChildAtPoint (new Point (15, 15));
  645. Assert.AreEqual (d.Name, l.Name, "Child1");
  646. Assert.IsFalse (e.Name == l.Name, "Child2");
  647. l = c.GetChildAtPoint (new Point (57, 57));
  648. Assert.IsNull (l, "Child3");
  649. l = c.GetChildAtPoint (new Point (10, 10));
  650. Assert.AreEqual (d.Name, l.Name, "Child4");
  651. // GetChildAtPointSkip is not implemented and the following test is breaking for Net_2_0 profile
  652. // #if NET_2_0
  653. // c.Controls.Add (e);
  654. // e.Visible = false;
  655. // l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
  656. // Assert.IsNull (l, "Child5");
  657. // e.Visible = true;
  658. // l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
  659. // Assert.AreSame (e.Name, l.Name, "Child6");
  660. // #endif // NET_2_0
  661. } finally {
  662. if (c != null)
  663. c.Dispose ();
  664. if (d != null)
  665. d.Dispose ();
  666. }
  667. }
  668. public class LayoutTestControl : Control {
  669. public int LayoutCount;
  670. public LayoutTestControl () : base() {
  671. LayoutCount = 0;
  672. }
  673. protected override void OnLayout(LayoutEventArgs levent) {
  674. LayoutCount++;
  675. base.OnLayout (levent);
  676. }
  677. }
  678. [Test]
  679. public void LayoutTest() {
  680. LayoutTestControl c;
  681. c = new LayoutTestControl();
  682. c.SuspendLayout();
  683. c.SuspendLayout();
  684. c.SuspendLayout();
  685. c.SuspendLayout();
  686. c.ResumeLayout(true);
  687. c.PerformLayout();
  688. c.ResumeLayout(true);
  689. c.PerformLayout();
  690. c.ResumeLayout(true);
  691. c.PerformLayout();
  692. c.ResumeLayout(true);
  693. c.PerformLayout();
  694. c.ResumeLayout(true);
  695. c.PerformLayout();
  696. c.ResumeLayout(true);
  697. c.PerformLayout();
  698. c.ResumeLayout(true);
  699. c.PerformLayout();
  700. c.SuspendLayout();
  701. c.PerformLayout();
  702. Assert.AreEqual(5, c.LayoutCount, "Layout Suspend/Resume locking does not bottom out at 0");
  703. }
  704. [Test]
  705. [ExpectedException(typeof(ArgumentException))]
  706. public void TransparentBackgroundTest1() {
  707. Control c;
  708. c = new Control();
  709. c.BackColor = Color.Transparent;
  710. }
  711. [Test]
  712. public void TransparentBackgroundTest2() {
  713. Panel c;
  714. c = new Panel();
  715. c.BackColor = Color.Transparent;
  716. Assert.AreEqual(Color.Transparent, c.BackColor, "Transparent background not set");
  717. }
  718. [Test]
  719. public void TransparentBackgroundTest3() {
  720. Control c;
  721. c = new Control();
  722. c.BackColor = Color.Empty;
  723. Assert.AreEqual(Control.DefaultBackColor, c.BackColor, "Setting empty color failed");
  724. }
  725. [Test]
  726. public void EnabledTest1() {
  727. Control child;
  728. Control parent;
  729. Control grandma;
  730. grandma = new Control();
  731. parent = new Control();
  732. child = new Control();
  733. grandma.Controls.Add(parent);
  734. parent.Controls.Add(child);
  735. grandma.Enabled = false;
  736. Assert.AreEqual(grandma.Enabled, child.Enabled, "Child did not inherit disabled state");
  737. }
  738. int EnabledCalledCount = 0;
  739. private void EnabledTest2EnabledChanged(object sender, EventArgs e) {
  740. EnabledCalledCount++;
  741. }
  742. [Test]
  743. public void EnabledTest2() {
  744. // Check nesting of enabled calls
  745. // OnEnabled is not called for disabled child controls
  746. Control child;
  747. Control parent;
  748. Control grandma;
  749. EnabledCalledCount = 0;
  750. grandma = new Control();
  751. parent = new Control();
  752. child = new Control();
  753. child.EnabledChanged += new EventHandler(EnabledTest2EnabledChanged);
  754. grandma.Controls.Add(parent);
  755. parent.Controls.Add(child);
  756. grandma.Enabled = false;
  757. Assert.AreEqual(1, EnabledCalledCount, "Child Enabled Event not properly fired");
  758. grandma.Enabled = true;
  759. Assert.AreEqual(2, EnabledCalledCount, "Child Enabled Event not properly fired");
  760. child.Enabled = false;
  761. grandma.Enabled = false;
  762. Assert.AreEqual(3, EnabledCalledCount, "Child Enabled Event not properly fired");
  763. }
  764. }
  765. [TestFixture]
  766. public class ControlInvokeTest {
  767. public delegate void TestDelegate ();
  768. Form f;
  769. Control c;
  770. Thread control_t;
  771. ApplicationContext control_context;
  772. bool delegateCalled = false;
  773. object m;
  774. void CreateControl ()
  775. {
  776. f = new Form ();
  777. f.ShowInTaskbar = false;
  778. c = new Control ();
  779. f.Controls.Add (c);
  780. Console.WriteLine ("f.Handle = {0}", f.Handle);
  781. Console.WriteLine ("c.Handle = {0}", c.Handle);
  782. control_context = new ApplicationContext (f);
  783. Monitor.Enter (m);
  784. Console.WriteLine ("pulsing");
  785. Monitor.Pulse (m);
  786. Monitor.Exit (m);
  787. Console.WriteLine ("control thread running");
  788. Application.Run (control_context);
  789. c.Dispose ();
  790. }
  791. [Test]
  792. public void InvokeTest ()
  793. {
  794. m = new object ();
  795. control_t = new Thread(new ThreadStart(CreateControl));
  796. Monitor.Enter (m);
  797. control_t.Start ();
  798. Console.WriteLine ("waiting on monitor");
  799. Monitor.Wait (m);
  800. Console.WriteLine ("making async call");
  801. IAsyncResult result;
  802. result = c.BeginInvoke (new TestDelegate (delegate_call));
  803. c.EndInvoke (result);
  804. Assert.AreEqual (true, delegateCalled, "Invoke1");
  805. }
  806. public void delegate_call () {
  807. /* invoked on control_context's thread */
  808. delegateCalled = true;
  809. f.Dispose ();
  810. Application.Exit ();
  811. }
  812. }
  813. }