ControlTest.cs 31 KB

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