ControlTest.cs 24 KB

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