ControlTest.cs 26 KB

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