ControlTest.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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 TopLevelControlTest () {
  136. Control c = new Control ();
  137. Assert.AreEqual(null, c.TopLevelControl, "T1");
  138. Panel p = new Panel ();
  139. p.Controls.Add (c);
  140. Assert.AreEqual(null, c.TopLevelControl, "T2");
  141. Form f = new Form ();
  142. f.ShowInTaskbar = false;
  143. f.Controls.Add (p);
  144. Assert.AreEqual (f, c.TopLevelControl, "T3");
  145. Assert.AreEqual (f, f.TopLevelControl, "T4");
  146. }
  147. [Test]
  148. public void RelationTest() {
  149. Control c1;
  150. Control c2;
  151. c1 = new Control();
  152. c2 = new Control();
  153. Assert.AreEqual(true , c1.Visible , "Rel1");
  154. Assert.AreEqual(false, c1.Contains(c2) , "Rel2");
  155. Assert.AreEqual("System.Windows.Forms.Control", c1.ToString() , "Rel3");
  156. c1.Controls.Add(c2);
  157. Assert.AreEqual(true , c2.Visible , "Rel4");
  158. Assert.AreEqual(true, c1.Contains(c2) , "Rel5");
  159. c1.Anchor = AnchorStyles.Top;
  160. c1.SuspendLayout ();
  161. c1.Anchor = AnchorStyles.Left ;
  162. c1.ResumeLayout ();
  163. Assert.AreEqual(AnchorStyles.Left , c1.Anchor, "Rel6");
  164. c1.SetBounds(10, 20, 30, 40) ;
  165. Assert.AreEqual(new Rectangle(10, 20, 30, 40), c1.Bounds, "Rel7");
  166. Assert.AreEqual(c1, c2.Parent, "Rel8");
  167. }
  168. [Test]
  169. [Category ("NotWorking")]
  170. public void TabOrder() {
  171. Form form;
  172. Control active;
  173. Label label1 = new Label(); // To test non-tabstop items as well
  174. Label label2 = new Label();
  175. GroupBox group1 = new GroupBox();
  176. GroupBox group2 = new GroupBox();
  177. GroupBox group3 = new GroupBox();
  178. TextBox text1 = new TextBox();
  179. RadioButton radio11 = new RadioButton();
  180. RadioButton radio12 = new RadioButton();
  181. RadioButton radio13 = new RadioButton();
  182. RadioButton radio14 = new RadioButton();
  183. RadioButton radio21 = new RadioButton();
  184. RadioButton radio22 = new RadioButton();
  185. RadioButton radio23 = new RadioButton();
  186. RadioButton radio24 = new RadioButton();
  187. RadioButton radio31 = new RadioButton();
  188. RadioButton radio32 = new RadioButton();
  189. RadioButton radio33 = new RadioButton();
  190. RadioButton radio34 = new RadioButton();
  191. form = new Form();
  192. form.ShowInTaskbar = false;
  193. form.ClientSize = new Size (520, 520);
  194. Assert.AreEqual(new Size(520, 520), form.ClientSize, "Tab1");
  195. form.Text = "SWF Taborder Test App Form";
  196. Assert.AreEqual("SWF Taborder Test App Form", form.Text, "Tab2");
  197. label1.Location = new Point(10, 10);
  198. Assert.AreEqual(new Point(10, 10), label1.Location, "Tab3");
  199. label1.Text = "Label1";
  200. form.Controls.Add(label1);
  201. label2.Location = new Point(200, 10);
  202. label2.Text = "Label2";
  203. form.Controls.Add(label2);
  204. group1.Text = "Group1";
  205. group2.Text = "Group2";
  206. group3.Text = "Group3";
  207. group1.Size = new Size(200, 400);
  208. group2.Size = new Size(200, 400);
  209. group3.Size = new Size(180, 180);
  210. Assert.AreEqual(new Size(180, 180), group3.Size, "Tab4");
  211. group1.Location = new Point(10, 40);
  212. group2.Location = new Point(220, 40);
  213. group3.Location = new Point(10, 210);
  214. group1.TabIndex = 30;
  215. Assert.AreEqual(30, group1.TabIndex, "Tab5");
  216. group1.TabStop = true;
  217. // Don't assign, test automatic assignment
  218. //group2.TabIndex = 0;
  219. group2.TabStop = true;
  220. Assert.AreEqual(0, group2.TabIndex, "Tab6");
  221. group3.TabIndex = 35;
  222. group3.TabStop = true;
  223. // Test default tab index
  224. Assert.AreEqual(0, radio11.TabIndex, "Tab7");
  225. text1.Text = "Edit Control";
  226. radio11.Text = "Radio 1-1 [Tab1]";
  227. radio12.Text = "Radio 1-2 [Tab2]";
  228. radio13.Text = "Radio 1-3 [Tab3]";
  229. radio14.Text = "Radio 1-4 [Tab4]";
  230. radio21.Text = "Radio 2-1 [Tab4]";
  231. radio22.Text = "Radio 2-2 [Tab3]";
  232. radio23.Text = "Radio 2-3 [Tab2]";
  233. radio24.Text = "Radio 2-4 [Tab1]";
  234. radio31.Text = "Radio 3-1 [Tab1]";
  235. radio32.Text = "Radio 3-2 [Tab3]";
  236. radio33.Text = "Radio 3-3 [Tab2]";
  237. radio34.Text = "Radio 3-4 [Tab4]";
  238. // We don't assign TabIndex for radio1X; test automatic assignment
  239. text1.TabStop = true;
  240. radio11.TabStop = true;
  241. radio21.TabIndex = 4;
  242. radio22.TabIndex = 3;
  243. radio23.TabIndex = 2;
  244. radio24.TabIndex = 1;
  245. radio24.TabStop = true;
  246. radio31.TabIndex = 11;
  247. radio31.TabStop = true;
  248. radio32.TabIndex = 13;
  249. radio33.TabIndex = 12;
  250. radio34.TabIndex = 14;
  251. text1.Location = new Point(10, 100);
  252. radio11.Location = new Point(10, 20);
  253. radio12.Location = new Point(10, 40);
  254. radio13.Location = new Point(10, 60);
  255. radio14.Location = new Point(10, 80);
  256. radio21.Location = new Point(10, 20);
  257. radio22.Location = new Point(10, 40);
  258. radio23.Location = new Point(10, 60);
  259. radio24.Location = new Point(10, 80);
  260. radio31.Location = new Point(10, 20);
  261. radio32.Location = new Point(10, 40);
  262. radio33.Location = new Point(10, 60);
  263. radio34.Location = new Point(10, 80);
  264. text1.Size = new Size(150, text1.PreferredHeight);
  265. radio11.Size = new Size(150, 20);
  266. radio12.Size = new Size(150, 20);
  267. radio13.Size = new Size(150, 20);
  268. radio14.Size = new Size(150, 20);
  269. radio21.Size = new Size(150, 20);
  270. radio22.Size = new Size(150, 20);
  271. radio23.Size = new Size(150, 20);
  272. radio24.Size = new Size(150, 20);
  273. radio31.Size = new Size(150, 20);
  274. radio32.Size = new Size(150, 20);
  275. radio33.Size = new Size(150, 20);
  276. radio34.Size = new Size(150, 20);
  277. group1.Controls.Add(text1);
  278. group1.Controls.Add(radio11);
  279. group1.Controls.Add(radio12);
  280. group1.Controls.Add(radio13);
  281. group1.Controls.Add(radio14);
  282. group2.Controls.Add(radio21);
  283. group2.Controls.Add(radio22);
  284. group2.Controls.Add(radio23);
  285. group2.Controls.Add(radio24);
  286. group3.Controls.Add(radio31);
  287. group3.Controls.Add(radio32);
  288. group3.Controls.Add(radio33);
  289. group3.Controls.Add(radio34);
  290. form.Controls.Add(group1);
  291. form.Controls.Add(group2);
  292. group2.Controls.Add(group3);
  293. // Perform some tests, the TabIndex stuff below will alter the outcome
  294. Assert.AreEqual(null, Helper.TestControl(group2, radio34, true), "Tab8");
  295. Assert.AreEqual(31, group2.TabIndex, "Tab9");
  296. // Does the taborder of containers and non-selectable things change behaviour?
  297. label1.TabIndex = 5;
  298. label2.TabIndex = 4;
  299. group1.TabIndex = 3;
  300. group2.TabIndex = 1;
  301. // Start verification
  302. Assert.AreEqual(null, Helper.TestControl(group2, radio34, true), "Tab10");
  303. Assert.AreEqual(radio24.Text, Helper.TestControl(group2, group2, true), "Tab11");
  304. Assert.AreEqual(radio31.Text, Helper.TestControl(group2, group3, true), "Tab12");
  305. Assert.AreEqual(null, Helper.TestControl(group1, radio14, true), "Tab13");
  306. Assert.AreEqual(radio23.Text, Helper.TestControl(group2, radio24, true), "Tab14");
  307. Assert.AreEqual(group3.Text, Helper.TestControl(group2, radio21, true), "Tab15");
  308. Assert.AreEqual(radio13.Text, Helper.TestControl(form, radio12, true), "Tab16");
  309. Assert.AreEqual(label2.Text, Helper.TestControl(form, radio14, true), "Tab17");
  310. Assert.AreEqual(group1.Text, Helper.TestControl(form, radio34, true), "Tab18");
  311. Assert.AreEqual(radio23.Text, Helper.TestControl(group2, radio24, true), "Tab19");
  312. // Sanity checks
  313. Assert.AreEqual(null, Helper.TestControl(radio11, radio21, true), "Tab20");
  314. Assert.AreEqual(text1.Text, Helper.TestControl(group1, radio21, true), "Tab21");
  315. Assert.AreEqual(radio14.Text, Helper.TestControl(form, label2, false), "Tab22");
  316. Assert.AreEqual(radio21.Text, Helper.TestControl(group2, group3, false), "Tab23");
  317. Assert.AreEqual(4, radio21.TabIndex, "Tab24");
  318. Assert.AreEqual(1, radio11.TabIndex, "Tab25");
  319. Assert.AreEqual(3, radio13.TabIndex, "Tab26");
  320. Assert.AreEqual(35, group3.TabIndex, "Tab27");
  321. Assert.AreEqual(1, group2.TabIndex, "Tab28");
  322. Assert.AreEqual(label1.Text, Helper.TestControl(form, form, false), "Tab29");
  323. Assert.AreEqual(radio14.Text, Helper.TestControl(group1, group1, false), "Tab30");
  324. Assert.AreEqual(radio34.Text, Helper.TestControl(group3, group3, false), "Tab31");
  325. Assert.AreEqual(null, Helper.TestControl(label1, label1, false), "Tab31");
  326. Assert.AreEqual(null, Helper.TestControl(radio11, radio21, false), "Tab32");
  327. form.Dispose ();
  328. }
  329. [Test]
  330. public void ScaleTest()
  331. {
  332. Control r1 = new Control();
  333. r1.Width = 40;
  334. r1.Height = 20;
  335. r1.Scale(2);
  336. Assert.AreEqual(80, r1.Width, "Scale1");
  337. Assert.AreEqual(40, r1.Height, "Scale2");
  338. }
  339. [Test]
  340. public void TextTest()
  341. {
  342. Control r1 = new Control();
  343. r1.Text = "Hi" ;
  344. Assert.AreEqual("Hi" , r1.Text , "Text1");
  345. r1.ResetText();
  346. Assert.AreEqual("" , r1.Text , "Text2");
  347. }
  348. [Test]
  349. public void PubMethodTest7()
  350. {
  351. Control r1 = new Control();
  352. r1.RightToLeft = RightToLeft.Yes ;
  353. r1.ResetRightToLeft() ;
  354. Assert.AreEqual(RightToLeft.No , r1.RightToLeft , "#81");
  355. r1.ImeMode = ImeMode.Off ;
  356. r1.ResetImeMode () ;
  357. Assert.AreEqual(ImeMode.NoControl , r1.ImeMode , "#82");
  358. r1.ForeColor= SystemColors.GrayText ;
  359. r1.ResetForeColor() ;
  360. Assert.AreEqual(SystemColors.ControlText , r1.ForeColor , "#83");
  361. //r1.Font = Font.FromHdc();
  362. r1.ResetFont () ;
  363. //Assert.AreEqual(FontFamily.GenericSansSerif , r1.Font , "#83");
  364. r1.Cursor = Cursors.Hand ;
  365. r1.ResetCursor () ;
  366. Assert.AreEqual(Cursors.Default , r1.Cursor , "#83");
  367. //r1.DataBindings = System.Windows.Forms.Binding ;
  368. //r1.ResetBindings() ;
  369. //Assert.AreEqual(ControlBindingsCollection , r1.DataBindings , "#83");
  370. r1.BackColor = Color.Black ;
  371. r1.ResetBackColor() ;
  372. Assert.AreEqual( SystemColors.Control , r1.BackColor , "#84");
  373. r1.BackColor = Color.Black ;
  374. r1.Refresh() ;
  375. Assert.AreEqual( null , r1.Region , "#85");
  376. Rectangle M = new Rectangle(10, 20, 30 ,40);
  377. r1.RectangleToScreen(M) ;
  378. Assert.AreEqual( null , r1.Region , "#86");
  379. }
  380. [Test]
  381. public void ScreenClientCoords()
  382. {
  383. Label l;
  384. Point p1;
  385. Point p2;
  386. Point p3;
  387. l = new Label();
  388. l.Left = 10;
  389. l.Top = 12;
  390. l.Visible = true;
  391. p1 = new Point (10,10);
  392. p2 = l.PointToScreen(p1);
  393. p3 = l.PointToClient(p2);
  394. Assert.AreEqual (p1, p3, "SC1");
  395. }
  396. [Test]
  397. public void ContainsTest ()
  398. {
  399. Control t = new Control ();
  400. Control s = new Control ();
  401. t.Controls.Add (s);
  402. Assert.AreEqual (true, t.Contains (s), "Con1");
  403. Assert.AreEqual (false, s.Contains (t), "Con2");
  404. Assert.AreEqual (false, s.Contains (null), "Con3");
  405. Assert.AreEqual (false, t.Contains (new Control ()), "Con4");
  406. }
  407. [Test]
  408. public void CreateHandleTest ()
  409. {
  410. Control parent;
  411. Control child;
  412. parent = null;
  413. child = null;
  414. try {
  415. parent = new Control ();
  416. child = new Control ();
  417. parent.Visible = true;
  418. parent.Controls.Add (child);
  419. Assert.IsFalse (parent.IsHandleCreated, "CH1");
  420. Assert.IsFalse (child.IsHandleCreated, "CH2");
  421. parent.CreateControl ();
  422. Assert.IsNotNull (parent.Handle, "CH3");
  423. Assert.IsNotNull (child.Handle, "CH4");
  424. Assert.IsTrue (parent.IsHandleCreated, "CH5");
  425. Assert.IsTrue (child.IsHandleCreated, "CH6");
  426. } finally {
  427. if (parent != null)
  428. parent.Dispose ();
  429. if (child != null)
  430. child.Dispose ();
  431. }
  432. // Accessing Handle Property creates the handle
  433. try {
  434. parent = new Control ();
  435. parent.Visible = true;
  436. child = new Control ();
  437. parent.Controls.Add (child);
  438. Assert.IsFalse (parent.IsHandleCreated, "CH7");
  439. Assert.IsFalse (child.IsHandleCreated, "CH8");
  440. Assert.IsNotNull (parent.Handle, "CH9");
  441. Assert.IsTrue (parent.IsHandleCreated, "CH10");
  442. Assert.IsTrue (child.IsHandleCreated, "CH11");
  443. } finally {
  444. if (parent != null)
  445. parent.Dispose ();
  446. if (child != null)
  447. child.Dispose ();
  448. }
  449. }
  450. [Test]
  451. public void CreateGraphicsTest ()
  452. {
  453. Graphics g = null;
  454. Pen p = null;
  455. try {
  456. Control c = new Control ();
  457. c.SetBounds (0,0, 20, 20);
  458. g = c.CreateGraphics ();
  459. Assert.IsNotNull (g, "Graph1");
  460. } finally {
  461. if (p != null)
  462. p.Dispose ();
  463. if (g != null)
  464. g.Dispose ();
  465. }
  466. }
  467. bool delegateCalled = false;
  468. public delegate void TestDelegate ();
  469. public void delegate_call () {
  470. delegateCalled = true;
  471. }
  472. [Test]
  473. [ExpectedException(typeof(InvalidOperationException))]
  474. public void InvokeException1 () {
  475. Control c = new Control ();
  476. IAsyncResult result;
  477. result = c.BeginInvoke (new TestDelegate (delegate_call));
  478. c.EndInvoke (result);
  479. }
  480. [Test]
  481. public void FindFormTest () {
  482. Form f = new Form ();
  483. f.ShowInTaskbar = false;
  484. f.Name = "form";
  485. Control c = null;
  486. try {
  487. f.Controls.Add (c = new Control ());
  488. Assert.AreEqual (f.Name, c.FindForm ().Name, "Find1");
  489. f.Controls.Remove (c);
  490. GroupBox g = new GroupBox ();
  491. g.Name = "box";
  492. f.Controls.Add (g);
  493. g.Controls.Add (c);
  494. Assert.AreEqual (f.Name, f.FindForm ().Name, "Find2");
  495. g.Controls.Remove (c);
  496. Assert.IsNull(c.FindForm (), "Find3");
  497. } finally {
  498. if (c != null)
  499. c.Dispose ();
  500. if (f != null)
  501. f.Dispose ();
  502. }
  503. }
  504. [Test]
  505. public void FocusTest ()
  506. {
  507. Form f = null;
  508. Button c = null, d = null;
  509. try {
  510. f = new Form ();
  511. f.ShowInTaskbar = false;
  512. f.Visible = true;
  513. c = new Button ();
  514. c.Visible = true;
  515. f.Controls.Add (c);
  516. d = new Button ();
  517. d.Visible = false;
  518. f.Controls.Add (d);
  519. Assert.IsTrue (c.CanFocus, "Focus1");
  520. Assert.IsFalse (c.Focused, "Focus2");
  521. c.Focus ();
  522. Assert.IsTrue (c.Focused, "Focus3");
  523. d.Focus ();
  524. Assert.IsFalse (d.Focused, "Focus4");
  525. d.Visible = true;
  526. d.Focus ();
  527. Assert.IsTrue (d.Focused, "Focus5");
  528. Assert.IsFalse (c.Focused, "Focus6");
  529. c.Enabled = false;
  530. Assert.IsFalse (c.Focused, "Focus7");
  531. } finally {
  532. if (f != null)
  533. f.Dispose ();
  534. if (c != null)
  535. c.Dispose ();
  536. if (d != null)
  537. d.Dispose ();
  538. }
  539. }
  540. [Test]
  541. public void FromHandleTest ()
  542. {
  543. Control c1 = null;
  544. Control c2 = null;
  545. try {
  546. c1 = new Control ();
  547. c2 = new Control ();
  548. c1.Name = "parent";
  549. c2.Name = "child";
  550. c1.Controls.Add(c2);
  551. // Handle
  552. Assert.AreEqual (c1.Name, Control.FromHandle (c1.Handle).Name, "Handle1");
  553. Assert.IsNull (Control.FromHandle (IntPtr.Zero), "Handle2");
  554. // ChildHandle
  555. Assert.AreEqual (c1.Name, Control.FromChildHandle (c1.Handle).Name, "Handle3");
  556. Assert.IsNull (Control.FromChildHandle (IntPtr.Zero), "Handle4");
  557. } finally {
  558. if (c1 != null)
  559. c1.Dispose ();
  560. if (c2 != null)
  561. c2.Dispose ();
  562. }
  563. }
  564. [Test]
  565. public void GetChildAtPointTest ()
  566. {
  567. Control c = null, d = null, e = null;
  568. try {
  569. c = new Control ();
  570. c.Name = "c1";
  571. c.SetBounds (0, 0, 100, 100);
  572. d = new Control ();
  573. d.Name = "d1";
  574. d.SetBounds (10, 10, 40, 40);
  575. c.Controls.Add (d);
  576. e = new Control ();
  577. e.Name = "e1";
  578. e.SetBounds (55, 55, 10, 10);
  579. Control l = c.GetChildAtPoint (new Point (15, 15));
  580. Assert.AreEqual (d.Name, l.Name, "Child1");
  581. Assert.IsFalse (e.Name == l.Name, "Child2");
  582. l = c.GetChildAtPoint (new Point (57, 57));
  583. Assert.IsNull (l, "Child3");
  584. l = c.GetChildAtPoint (new Point (10, 10));
  585. Assert.AreEqual (d.Name, l.Name, "Child4");
  586. // GetChildAtPointSkip is not implemented and the following test is breaking for Net_2_0 profile
  587. // #if NET_2_0
  588. // c.Controls.Add (e);
  589. // e.Visible = false;
  590. // l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
  591. // Assert.IsNull (l, "Child5");
  592. // e.Visible = true;
  593. // l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
  594. // Assert.AreSame (e.Name, l.Name, "Child6");
  595. // #endif // NET_2_0
  596. } finally {
  597. if (c != null)
  598. c.Dispose ();
  599. if (d != null)
  600. d.Dispose ();
  601. }
  602. }
  603. public class LayoutTestControl : Control {
  604. public int LayoutCount;
  605. public LayoutTestControl () : base() {
  606. LayoutCount = 0;
  607. }
  608. protected override void OnLayout(LayoutEventArgs levent) {
  609. LayoutCount++;
  610. base.OnLayout (levent);
  611. }
  612. }
  613. [Test]
  614. public void LayoutTest() {
  615. LayoutTestControl c;
  616. c = new LayoutTestControl();
  617. c.SuspendLayout();
  618. c.SuspendLayout();
  619. c.SuspendLayout();
  620. c.SuspendLayout();
  621. c.ResumeLayout(true);
  622. c.PerformLayout();
  623. c.ResumeLayout(true);
  624. c.PerformLayout();
  625. c.ResumeLayout(true);
  626. c.PerformLayout();
  627. c.ResumeLayout(true);
  628. c.PerformLayout();
  629. c.ResumeLayout(true);
  630. c.PerformLayout();
  631. c.ResumeLayout(true);
  632. c.PerformLayout();
  633. c.ResumeLayout(true);
  634. c.PerformLayout();
  635. c.SuspendLayout();
  636. c.PerformLayout();
  637. Assert.AreEqual(5, c.LayoutCount, "Layout Suspend/Resume locking does not bottom out at 0");
  638. }
  639. [Test]
  640. [ExpectedException(typeof(ArgumentException))]
  641. public void TransparentBackgroundTest1() {
  642. Control c;
  643. c = new Control();
  644. c.BackColor = Color.Transparent;
  645. }
  646. [Test]
  647. public void TransparentBackgroundTest2() {
  648. Panel c;
  649. c = new Panel();
  650. c.BackColor = Color.Transparent;
  651. Assert.AreEqual(Color.Transparent, c.BackColor, "Transparent background not set");
  652. }
  653. [Test]
  654. public void TransparentBackgroundTest3() {
  655. Control c;
  656. c = new Control();
  657. c.BackColor = Color.Empty;
  658. Assert.AreEqual(Control.DefaultBackColor, c.BackColor, "Setting empty color failed");
  659. }
  660. [Test]
  661. public void EnabledTest1() {
  662. Control child;
  663. Control parent;
  664. Control grandma;
  665. grandma = new Control();
  666. parent = new Control();
  667. child = new Control();
  668. grandma.Controls.Add(parent);
  669. parent.Controls.Add(child);
  670. grandma.Enabled = false;
  671. Assert.AreEqual(grandma.Enabled, child.Enabled, "Child did not inherit disabled state");
  672. }
  673. int EnabledCalledCount = 0;
  674. private void EnabledTest2EnabledChanged(object sender, EventArgs e) {
  675. EnabledCalledCount++;
  676. }
  677. [Test]
  678. public void EnabledTest2() {
  679. // Check nesting of enabled calls
  680. // OnEnabled is not called for disabled child controls
  681. Control child;
  682. Control parent;
  683. Control grandma;
  684. EnabledCalledCount = 0;
  685. grandma = new Control();
  686. parent = new Control();
  687. child = new Control();
  688. child.EnabledChanged += new EventHandler(EnabledTest2EnabledChanged);
  689. grandma.Controls.Add(parent);
  690. parent.Controls.Add(child);
  691. grandma.Enabled = false;
  692. Assert.AreEqual(1, EnabledCalledCount, "Child Enabled Event not properly fired");
  693. grandma.Enabled = true;
  694. Assert.AreEqual(2, EnabledCalledCount, "Child Enabled Event not properly fired");
  695. child.Enabled = false;
  696. grandma.Enabled = false;
  697. Assert.AreEqual(3, EnabledCalledCount, "Child Enabled Event not properly fired");
  698. }
  699. }
  700. [TestFixture]
  701. public class ControlInvokeTest {
  702. public delegate void TestDelegate ();
  703. Form f;
  704. Control c;
  705. Thread control_t;
  706. ApplicationContext control_context;
  707. bool delegateCalled = false;
  708. object m;
  709. void CreateControl ()
  710. {
  711. f = new Form ();
  712. f.ShowInTaskbar = false;
  713. c = new Control ();
  714. f.Controls.Add (c);
  715. Console.WriteLine ("f.Handle = {0}", f.Handle);
  716. Console.WriteLine ("c.Handle = {0}", c.Handle);
  717. control_context = new ApplicationContext (f);
  718. Monitor.Enter (m);
  719. Console.WriteLine ("pulsing");
  720. Monitor.Pulse (m);
  721. Monitor.Exit (m);
  722. Console.WriteLine ("control thread running");
  723. Application.Run (control_context);
  724. c.Dispose ();
  725. }
  726. [Test]
  727. public void InvokeTest ()
  728. {
  729. m = new object ();
  730. control_t = new Thread(new ThreadStart(CreateControl));
  731. Monitor.Enter (m);
  732. control_t.Start ();
  733. Console.WriteLine ("waiting on monitor");
  734. Monitor.Wait (m);
  735. Console.WriteLine ("making async call");
  736. IAsyncResult result;
  737. result = c.BeginInvoke (new TestDelegate (delegate_call));
  738. c.EndInvoke (result);
  739. Assert.AreEqual (true, delegateCalled, "Invoke1");
  740. }
  741. public void delegate_call () {
  742. /* invoked on control_context's thread */
  743. delegateCalled = true;
  744. f.Dispose ();
  745. Application.Exit ();
  746. }
  747. }
  748. }