ControlTest.cs 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795
  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. public class OnPaintTester : Form
  22. {
  23. int counter;
  24. int total;
  25. ArrayList list = new ArrayList ();
  26. public bool Recursive;
  27. public bool TestRefresh;
  28. public bool TestInvalidate;
  29. public bool TestUpdate;
  30. #if NET_2_0
  31. public new bool DoubleBuffered {
  32. get {
  33. return base.DoubleBuffered;
  34. }
  35. set {
  36. base.DoubleBuffered = value;
  37. }
  38. }
  39. #endif
  40. protected override void OnPaint (PaintEventArgs pevent)
  41. {
  42. Assert.IsFalse (list.Contains (pevent.Graphics), "OnPaintTester.OnPaint: Got the same Graphics twice");
  43. list.Add (pevent.Graphics);
  44. if (total > 10)
  45. return;
  46. Recursive = counter > 0 || Recursive;
  47. counter++;
  48. if (counter < 2) {
  49. if (TestRefresh)
  50. Refresh ();
  51. else if (TestInvalidate)
  52. Invalidate ();
  53. else {
  54. Update ();
  55. }
  56. }
  57. base.OnPaint (pevent);
  58. counter--;
  59. total++;
  60. }
  61. public new void Show ()
  62. {
  63. base.Show ();
  64. Application.DoEvents ();
  65. }
  66. }
  67. [Test]
  68. public void OnPaintTest ()
  69. {
  70. using (OnPaintTester t = new OnPaintTester ()) {
  71. t.TestRefresh = true;
  72. t.Show ();
  73. Assert.IsTrue (t.Recursive, "#1");
  74. }
  75. using (OnPaintTester t = new OnPaintTester ()) {
  76. t.TestUpdate = true;
  77. t.Show ();
  78. Assert.IsFalse (t.Recursive, "#2");
  79. }
  80. using (OnPaintTester t = new OnPaintTester ()) {
  81. t.TestInvalidate = true;
  82. t.Show ();
  83. Assert.IsFalse (t.Recursive, "#3");
  84. }
  85. }
  86. #if NET_2_0
  87. [Test]
  88. public void OnPaintDoubleBufferedTest ()
  89. {
  90. using (OnPaintTester t = new OnPaintTester ()) {
  91. t.DoubleBuffered = true;
  92. t.TestRefresh = true;
  93. t.Show ();
  94. Assert.IsTrue (t.Recursive, "#1");
  95. }
  96. using (OnPaintTester t = new OnPaintTester ()) {
  97. t.DoubleBuffered = true;
  98. t.TestUpdate = true;
  99. t.Show ();
  100. Assert.IsFalse (t.Recursive, "#2");
  101. }
  102. using (OnPaintTester t = new OnPaintTester ()) {
  103. t.DoubleBuffered = true;
  104. t.TestInvalidate = true;
  105. t.Show ();
  106. Assert.IsFalse (t.Recursive, "#3");
  107. }
  108. }
  109. #endif
  110. public class PaintEventForm : Form
  111. {
  112. public ArrayList overrides = new ArrayList ();
  113. public ArrayList events = new ArrayList ();
  114. public PaintEventForm ()
  115. {
  116. Paint += new PaintEventHandler (DoubleBufferEventForm_Paint);
  117. }
  118. public bool GetControlStyle (ControlStyles style)
  119. {
  120. return base.GetStyle (style);
  121. }
  122. public void SetControlStyle (ControlStyles style, bool value)
  123. {
  124. base.SetStyle (style, value);
  125. }
  126. void DoubleBufferEventForm_Paint (object sender, PaintEventArgs e)
  127. {
  128. events.Add("Paint");
  129. }
  130. protected override void OnPaintBackground (PaintEventArgs e)
  131. {
  132. base.OnPaintBackground (e);
  133. overrides.Add("OnPaintBackground");
  134. }
  135. protected override void OnPaint (PaintEventArgs pevent)
  136. {
  137. base.OnPaint (pevent);
  138. overrides.Add("OnPaint");
  139. }
  140. }
  141. [Test]
  142. public void EventStyleTest ()
  143. {
  144. #if NET_2_0
  145. using (PaintEventForm f = new PaintEventForm ()) {
  146. f.Show ();
  147. f.SetControlStyle (ControlStyles.OptimizedDoubleBuffer, true);
  148. f.Refresh ();
  149. Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#A1");
  150. Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#A2");
  151. Assert.IsTrue (f.events.Contains ("Paint"), "#A3");
  152. }
  153. #endif
  154. using (PaintEventForm f = new PaintEventForm ()) {
  155. f.Show ();
  156. f.SetControlStyle (ControlStyles.DoubleBuffer, true);
  157. f.Refresh ();
  158. Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#B1");
  159. Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#B2");
  160. Assert.IsTrue (f.events.Contains ("Paint"), "#B3");
  161. }
  162. using (PaintEventForm f = new PaintEventForm ()) {
  163. f.Show ();
  164. f.SetControlStyle (ControlStyles.AllPaintingInWmPaint, true);
  165. f.Refresh ();
  166. Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#C1");
  167. Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#C2");
  168. Assert.IsTrue (f.events.Contains ("Paint"), "#C3");
  169. }
  170. using (PaintEventForm f = new PaintEventForm ()) {
  171. f.Show ();
  172. f.SetControlStyle (ControlStyles.UserPaint, true);
  173. f.Refresh ();
  174. Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#D1");
  175. Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#D2");
  176. Assert.IsTrue (f.events.Contains ("Paint"), "#D3");
  177. }
  178. using (PaintEventForm f = new PaintEventForm ()) {
  179. f.Show ();
  180. f.SetControlStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  181. f.Refresh ();
  182. Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#E1");
  183. Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#E2");
  184. Assert.IsTrue (f.events.Contains ("Paint"), "#E3");
  185. }
  186. using (PaintEventForm f = new PaintEventForm ()) {
  187. f.Show ();
  188. f.SetControlStyle (ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
  189. f.Refresh ();
  190. Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#F1");
  191. Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#F2");
  192. Assert.IsTrue (f.events.Contains ("Paint"), "#F3");
  193. }
  194. using (PaintEventForm f = new PaintEventForm ()) {
  195. f.Show ();
  196. f.SetControlStyle (ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
  197. f.Refresh ();
  198. Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#G1");
  199. Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#G2");
  200. Assert.IsTrue (f.events.Contains ("Paint"), "#G3");
  201. }
  202. using (PaintEventForm f = new PaintEventForm ()) {
  203. f.Show ();
  204. f.SetControlStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
  205. f.Refresh ();
  206. Assert.IsTrue (f.overrides.Contains ("OnPaintBackground"), "#H1");
  207. Assert.IsTrue (f.overrides.Contains ("OnPaint"), "#H2");
  208. Assert.IsTrue (f.events.Contains ("Paint"), "#H3");
  209. }
  210. }
  211. #if NET_2_0
  212. public class DoubleBufferedForm : Form
  213. {
  214. public bool painted;
  215. public bool failed;
  216. public DoubleBufferedForm ()
  217. {
  218. this.DoubleBuffered = true;
  219. }
  220. protected override void OnPaint (PaintEventArgs e)
  221. {
  222. if (failed || painted)
  223. return;
  224. painted = true;
  225. Height = Height + 1;
  226. try {
  227. e.Graphics.DrawString (Size.ToString (), Font, Brushes.AliceBlue, new Point (2, 2));
  228. } catch (Exception exception) {
  229. Console.WriteLine (exception.StackTrace);
  230. failed = true;
  231. }
  232. }
  233. }
  234. public class DoubleBufferControl : Control
  235. {
  236. public bool IsDoubleBuffered
  237. {
  238. get { return base.DoubleBuffered; }
  239. set { base.DoubleBuffered = value; }
  240. }
  241. public bool GetControlStyle (ControlStyles style)
  242. {
  243. return base.GetStyle (style);
  244. }
  245. public void SetControlStyle (ControlStyles style, bool value)
  246. {
  247. base.SetStyle (style, value);
  248. }
  249. }
  250. [Test]
  251. public void DoubleBufferTest ()
  252. {
  253. DoubleBufferedForm f = new DoubleBufferedForm ();
  254. f.Show ();
  255. f.Refresh ();
  256. Assert.IsFalse (f.failed, "#01");
  257. Assert.IsTrue (f.painted, "The control was never painted, so please check the test");
  258. }
  259. [Test]
  260. public void DoubleBufferedTest ()
  261. {
  262. DoubleBufferControl c = new DoubleBufferControl ();
  263. Assert.IsFalse (c.IsDoubleBuffered, "#A1");
  264. Assert.IsTrue (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#A2");
  265. Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#A3");
  266. Assert.IsFalse (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#A4");
  267. Assert.IsTrue (c.GetControlStyle (ControlStyles.UserPaint), "#A5");
  268. c.SetControlStyle (ControlStyles.OptimizedDoubleBuffer, true);
  269. Assert.IsTrue (c.IsDoubleBuffered, "#B1");
  270. Assert.IsTrue (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#B2");
  271. Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#B3");
  272. Assert.IsTrue (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#B4");
  273. Assert.IsTrue (c.GetControlStyle (ControlStyles.UserPaint), "#A5");
  274. c.SetControlStyle (ControlStyles.AllPaintingInWmPaint, false);
  275. c.SetControlStyle (ControlStyles.UserPaint, false);
  276. Assert.IsTrue (c.IsDoubleBuffered, "#C1");
  277. Assert.IsFalse (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#C2");
  278. Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#C3");
  279. Assert.IsTrue (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#C4");
  280. Assert.IsFalse (c.GetControlStyle (ControlStyles.UserPaint), "#C5");
  281. c.SetControlStyle (ControlStyles.OptimizedDoubleBuffer, false);
  282. Assert.IsFalse (c.IsDoubleBuffered, "#D1");
  283. Assert.IsFalse (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#D2");
  284. Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#D3");
  285. Assert.IsFalse (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#D4");
  286. Assert.IsFalse (c.GetControlStyle (ControlStyles.UserPaint), "#D5");
  287. c.SetControlStyle (ControlStyles.DoubleBuffer, true);
  288. Assert.IsFalse (c.IsDoubleBuffered, "#E1");
  289. Assert.IsFalse (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#E2");
  290. Assert.IsTrue (c.GetControlStyle (ControlStyles.DoubleBuffer), "#E3");
  291. Assert.IsFalse (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#E4");
  292. Assert.IsFalse (c.GetControlStyle (ControlStyles.UserPaint), "#E5");
  293. c.SetControlStyle (ControlStyles.DoubleBuffer, false);
  294. Assert.IsFalse (c.IsDoubleBuffered, "#F1");
  295. Assert.IsFalse (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#F2");
  296. Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#F3");
  297. Assert.IsFalse (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#F4");
  298. Assert.IsFalse (c.GetControlStyle (ControlStyles.UserPaint), "#F5");
  299. c.IsDoubleBuffered = true;
  300. Assert.IsTrue (c.IsDoubleBuffered, "#G1");
  301. Assert.IsTrue (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#G2");
  302. Assert.IsFalse (c.GetControlStyle (ControlStyles.DoubleBuffer), "#G3");
  303. Assert.IsTrue (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#G4");
  304. Assert.IsFalse (c.GetControlStyle (ControlStyles.UserPaint), "#G5");
  305. c.SetControlStyle (ControlStyles.AllPaintingInWmPaint, true);
  306. c.SetControlStyle (ControlStyles.OptimizedDoubleBuffer, true);
  307. c.SetControlStyle (ControlStyles.DoubleBuffer, true);
  308. c.SetControlStyle (ControlStyles.UserPaint, true);
  309. c.IsDoubleBuffered = false;
  310. Assert.IsFalse (c.IsDoubleBuffered, "#H1");
  311. Assert.IsTrue (c.GetControlStyle (ControlStyles.AllPaintingInWmPaint), "#H2");
  312. Assert.IsTrue (c.GetControlStyle (ControlStyles.DoubleBuffer), "#H3");
  313. Assert.IsFalse (c.GetControlStyle (ControlStyles.OptimizedDoubleBuffer), "#H4");
  314. Assert.IsTrue (c.GetControlStyle (ControlStyles.UserPaint), "#H5");
  315. }
  316. [Test]
  317. public void DoubleBufferedStyleTest ()
  318. {
  319. DoubleBufferControl c = new DoubleBufferControl ();
  320. TestControlStyle.CheckStyles (c, "#A1", ControlStyles.UserPaint, ControlStyles.StandardClick, ControlStyles.Selectable, ControlStyles.StandardDoubleClick, ControlStyles.AllPaintingInWmPaint, ControlStyles.UseTextForAccessibility);
  321. c.IsDoubleBuffered = true;
  322. TestControlStyle.CheckStyles (c, "#A2", ControlStyles.UserPaint, ControlStyles.StandardClick, ControlStyles.Selectable, ControlStyles.StandardDoubleClick, ControlStyles.AllPaintingInWmPaint, ControlStyles.UseTextForAccessibility, ControlStyles.OptimizedDoubleBuffer);
  323. c.IsDoubleBuffered = false;
  324. TestControlStyle.CheckStyles (c, "#A3", ControlStyles.UserPaint, ControlStyles.StandardClick, ControlStyles.Selectable, ControlStyles.StandardDoubleClick, ControlStyles.AllPaintingInWmPaint, ControlStyles.UseTextForAccessibility);
  325. c = new DoubleBufferControl ();
  326. foreach (ControlStyles style in Enum.GetValues (typeof(ControlStyles))) {
  327. c.SetControlStyle (style, false);
  328. }
  329. TestControlStyle.CheckStyles (c, "#B1");
  330. c.IsDoubleBuffered = true;
  331. TestControlStyle.CheckStyles (c, "#B2", ControlStyles.OptimizedDoubleBuffer, ControlStyles.AllPaintingInWmPaint);
  332. c.IsDoubleBuffered = false;
  333. TestControlStyle.CheckStyles (c, "#B3", ControlStyles.AllPaintingInWmPaint);
  334. }
  335. #endif
  336. class Helper {
  337. public static void TestAccessibility(Control c, string Default, string Description, string Name, AccessibleRole Role)
  338. {
  339. Assert.IsNotNull (c.AccessibilityObject, "Acc1");
  340. Assert.AreEqual (Default, c.AccessibleDefaultActionDescription, "Acc2");
  341. Assert.AreEqual (Description, c.AccessibleDescription, "Acc3");
  342. Assert.AreEqual (Name, c.AccessibleName, "Acc4");
  343. Assert.AreEqual (Role, c.AccessibleRole, "Acc5");
  344. }
  345. public static string TestControl(Control container, Control start, bool forward) {
  346. Control ctl;
  347. ctl = container.GetNextControl(start, forward);
  348. if (ctl == null) {
  349. return null;
  350. }
  351. return ctl.Text;
  352. }
  353. }
  354. [Test]
  355. public void CreatedTest ()
  356. {
  357. Control c = new Control ();
  358. Assert.IsFalse (c.Created, "A1");
  359. }
  360. [Test]
  361. [Category ("NotWorking")]
  362. public void CreatedAccessibilityTest ()
  363. {
  364. Control c = new Control ();
  365. Assert.IsFalse (c.Created, "A1");
  366. Helper.TestAccessibility(c, null, null, null, AccessibleRole.Default);
  367. Assert.IsTrue (c.Created, "A2");
  368. c.Dispose ();
  369. Assert.IsFalse (c.Created, "A3");
  370. }
  371. [Test]
  372. [Category ("NotWorking")]
  373. public void BoundsTest ()
  374. {
  375. Control c = new Control ();
  376. Assert.IsTrue (c.Bounds.IsEmpty, "A1");
  377. Assert.IsTrue (c.Size.IsEmpty, "A2");
  378. Assert.IsTrue (c.ClientSize.IsEmpty, "A3");
  379. Assert.IsTrue (c.ClientRectangle.IsEmpty, "A4");
  380. Assert.AreEqual (((IWin32Window)c).Handle, c.Handle, "A5");
  381. /* this part fails on linux because we can't allocate X windows which are 0x0,
  382. and the Control bounds directly reflect the size of the X window */
  383. Assert.IsTrue (c.Bounds.IsEmpty, "A6");
  384. Assert.IsTrue (c.Size.IsEmpty, "A7");
  385. Assert.IsTrue (c.ClientSize.IsEmpty, "A8");
  386. Assert.IsTrue (c.ClientRectangle.IsEmpty, "A9");
  387. }
  388. [Test]
  389. [Category ("NotWorking")] // Theme.DefaultFont change must be approved first
  390. public void FontTest ()
  391. {
  392. Control c = new Control ();
  393. Assert.IsFalse (c.Font.Bold, "#A1");
  394. //Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#A2");
  395. Assert.IsFalse (c.Font.Italic, "#A3");
  396. //Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#A4");
  397. Assert.AreEqual (8.25, c.Font.Size, "#A5");
  398. Assert.AreEqual (8.25, c.Font.SizeInPoints, "#A6");
  399. Assert.IsFalse (c.Font.Strikeout, "#A7");
  400. Assert.IsFalse (c.Font.Underline, "#A8");
  401. Assert.AreEqual (GraphicsUnit.Point, c.Font.Unit, "#A9");
  402. #if NET_2_0
  403. Assert.IsTrue (c.Font.IsSystemFont, "#A10");
  404. #endif
  405. c.Font = new Font (c.Font.FontFamily, 3, FontStyle.Italic);
  406. Assert.IsFalse (c.Font.Bold, "#B1");
  407. //Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#B2");
  408. Assert.IsTrue (c.Font.Italic, "#B3");
  409. //Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#B4");
  410. Assert.AreEqual (3, c.Font.Size, "#B5");
  411. Assert.AreEqual (3, c.Font.SizeInPoints, "#B6");
  412. Assert.IsFalse (c.Font.Strikeout, "#B7");
  413. Assert.IsFalse (c.Font.Underline, "#B8");
  414. Assert.AreEqual (GraphicsUnit.Point, c.Font.Unit, "#B9");
  415. #if NET_2_0
  416. Assert.AreEqual (false, c.Font.IsSystemFont, "#B10");
  417. #endif
  418. }
  419. [Test]
  420. [Category ("NotWorking")] // on Unix mapping is done to Bitstream Vera Sans
  421. public void FontTest_Names ()
  422. {
  423. Control c = new Control ();
  424. Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#1");
  425. Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#2");
  426. }
  427. [Test]
  428. public void PubPropTest()
  429. {
  430. Control c = new Control();
  431. Assert.IsFalse (c.AllowDrop , "A1");
  432. Assert.AreEqual(AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "A2");
  433. Assert.AreEqual ("Control", c.BackColor.Name , "B1");
  434. Assert.IsNull (c.BackgroundImage, "B2");
  435. Assert.IsNull (c.BindingContext, "B3");
  436. #if NET_2_0
  437. Assert.AreEqual (ImageLayout.Tile, c.BackgroundImageLayout, "B4");
  438. #endif
  439. Assert.IsFalse (c.CanFocus, "C1");
  440. Assert.IsTrue (c.CanSelect, "C2");
  441. Assert.IsFalse (c.Capture, "C3");
  442. Assert.IsTrue (c.CausesValidation, "C4");
  443. Assert.IsNotNull (c.CompanyName, "C7");
  444. Assert.IsNull (c.Container, "C8");
  445. Assert.IsFalse (c.ContainsFocus, "C9");
  446. Assert.IsNull (c.ContextMenu, "C10");
  447. Assert.AreEqual (0, c.Controls.Count, "C11");
  448. Assert.IsFalse (c.Created, "C12");
  449. Assert.AreEqual (Cursors.Default, c.Cursor, "C13");
  450. Assert.IsNotNull(c.DataBindings, "D1");
  451. Assert.AreEqual("Control", Control.DefaultBackColor.Name, "D2");
  452. Assert.AreEqual("ControlText", Control.DefaultForeColor.Name, "D3");
  453. Assert.AreEqual(FontStyle.Regular, Control.DefaultFont.Style, "D4");
  454. Assert.AreEqual (new Rectangle(0, 0, 0, 0), c.DisplayRectangle , "D5");
  455. Assert.IsFalse (c.Disposing, "D6");
  456. Assert.AreEqual(DockStyle.None, c.Dock, "D7");
  457. Assert.IsTrue (c.Enabled, "E1");
  458. Assert.IsFalse (c.Focused, "F1");
  459. Assert.AreEqual (FontStyle.Regular, c.Font.Style, "F2");
  460. Assert.AreEqual (SystemColors.ControlText, c.ForeColor, "F3");
  461. Assert.IsFalse (c.HasChildren, "H2");
  462. Assert.AreEqual (ImeMode.NoControl, c.ImeMode, "I1");
  463. Assert.IsFalse (c.InvokeRequired, "I2");
  464. Assert.IsFalse (c.IsAccessible, "I3");
  465. Assert.IsFalse (c.IsDisposed, "I4");
  466. Assert.IsFalse (c.IsHandleCreated, "I5");
  467. Assert.AreEqual(Point.Empty, c.Location, "L2");
  468. #if NET_2_0
  469. Assert.IsTrue(c.MaximumSize.IsEmpty);
  470. Assert.IsTrue(c.MinimumSize.IsEmpty);
  471. #endif
  472. Assert.AreEqual (Keys.None, Control.ModifierKeys, "M1");
  473. Assert.IsFalse (Control.MousePosition.IsEmpty, "M2");
  474. Assert.AreEqual (MouseButtons.None, Control.MouseButtons, "M3");
  475. Assert.AreEqual("", c.Name, "N1");
  476. c.Name = "Control Name";
  477. Assert.AreEqual("Control Name", c.Name, "N2");
  478. Assert.IsNull (c.Parent, "P1");
  479. Assert.IsNotNull (c.ProductName, "P2");
  480. Assert.IsTrue (c.ProductName != "", "P3");
  481. Assert.IsNotNull (c.ProductVersion, "P4");
  482. Assert.IsTrue (c.ProductVersion != "", "P5");
  483. Assert.IsFalse (c.RecreatingHandle, "R1");
  484. Assert.IsNull (c.Region, "R2");
  485. Assert.AreEqual (RightToLeft.No, c.RightToLeft, "R4");
  486. Assert.IsNull (c.Site, "S1");
  487. Assert.AreEqual (0, c.TabIndex , "T1");
  488. Assert.IsTrue (c.TabStop, "T2");
  489. Assert.IsNull (c.Tag, "T3");
  490. Assert.AreEqual ("", c.Text, "T4");
  491. Assert.IsTrue (c.Visible, "V1");
  492. }
  493. [Test]
  494. public void SizeChangeTest ()
  495. {
  496. Form f = new Form ();
  497. Control c = new Control ();
  498. f.Controls.Add(c);
  499. f.Show();
  500. c.Resize += new EventHandler(SizeChangedTest_ResizeHandler);
  501. c.Tag = true;
  502. c.Size = c.Size;
  503. Assert.AreEqual (true, (bool) c.Tag, "#1");
  504. f.Close ();
  505. }
  506. private void SizeChangedTest_ResizeHandler (object sender, EventArgs e)
  507. {
  508. ((Control) sender).Tag = false;
  509. }
  510. [Test]
  511. public void NegativeHeightTest ()
  512. {
  513. Control c = new Control ();
  514. IntPtr handle = c.Handle;
  515. c.Resize += new EventHandler(NegativeHeightTest_ResizeHandler);
  516. c.Tag = -2;
  517. c.Height = 2;
  518. c.Height = -2;
  519. Assert.AreEqual (0, (int) c.Tag, "#1");
  520. c.Dispose ();
  521. Assert.AreEqual (handle, handle, "Removes warning.");
  522. }
  523. private void NegativeHeightTest_ResizeHandler (object sender, EventArgs e)
  524. {
  525. Control c = (Control) sender;
  526. c.Tag = c.Height;
  527. }
  528. [Test]
  529. public void TopLevelControlTest () {
  530. Control c = new Control ();
  531. Assert.AreEqual(null, c.TopLevelControl, "T1");
  532. Panel p = new Panel ();
  533. p.Controls.Add (c);
  534. Assert.AreEqual(null, c.TopLevelControl, "T2");
  535. Form f = new Form ();
  536. f.ShowInTaskbar = false;
  537. f.Controls.Add (p);
  538. Assert.AreEqual (f, c.TopLevelControl, "T3");
  539. Assert.AreEqual (f, f.TopLevelControl, "T4");
  540. }
  541. [Test]
  542. public void RelationTest() {
  543. Control c1;
  544. Control c2;
  545. c1 = new Control();
  546. c2 = new Control();
  547. Assert.AreEqual(true , c1.Visible , "Rel1");
  548. Assert.AreEqual(false, c1.Contains(c2) , "Rel2");
  549. Assert.AreEqual("System.Windows.Forms.Control", c1.ToString() , "Rel3");
  550. c1.Controls.Add(c2);
  551. Assert.AreEqual(true , c2.Visible , "Rel4");
  552. Assert.AreEqual(true, c1.Contains(c2) , "Rel5");
  553. c1.Anchor = AnchorStyles.Top;
  554. c1.SuspendLayout ();
  555. c1.Anchor = AnchorStyles.Left ;
  556. c1.ResumeLayout ();
  557. Assert.AreEqual(AnchorStyles.Left , c1.Anchor, "Rel6");
  558. c1.SetBounds(10, 20, 30, 40) ;
  559. Assert.AreEqual(new Rectangle(10, 20, 30, 40), c1.Bounds, "Rel7");
  560. Assert.AreEqual(c1, c2.Parent, "Rel8");
  561. }
  562. [Test]
  563. public void AnchorDockTest ()
  564. {
  565. Control c = new Control ();
  566. Assert.AreEqual (DockStyle.None, c.Dock, "1");
  567. Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "2");
  568. c.Dock = DockStyle.Top;
  569. Assert.AreEqual (DockStyle.Top, c.Dock, "3");
  570. Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "4");
  571. c.Anchor = AnchorStyles.Top;
  572. Assert.AreEqual (DockStyle.Top, c.Dock, "5");
  573. Assert.AreEqual (AnchorStyles.Top, c.Anchor, "6");
  574. }
  575. [Test]
  576. [Category ("NotWorking")]
  577. public void TabOrder()
  578. {
  579. Form form;
  580. Control active;
  581. Label label1 = new Label(); // To test non-tabstop items as well
  582. Label label2 = new Label();
  583. GroupBox group1 = new GroupBox();
  584. GroupBox group2 = new GroupBox();
  585. GroupBox group3 = new GroupBox();
  586. TextBox text1 = new TextBox();
  587. RadioButton radio11 = new RadioButton();
  588. RadioButton radio12 = new RadioButton();
  589. RadioButton radio13 = new RadioButton();
  590. RadioButton radio14 = new RadioButton();
  591. RadioButton radio21 = new RadioButton();
  592. RadioButton radio22 = new RadioButton();
  593. RadioButton radio23 = new RadioButton();
  594. RadioButton radio24 = new RadioButton();
  595. RadioButton radio31 = new RadioButton();
  596. RadioButton radio32 = new RadioButton();
  597. RadioButton radio33 = new RadioButton();
  598. RadioButton radio34 = new RadioButton();
  599. form = new Form();
  600. form.ShowInTaskbar = false;
  601. form.ClientSize = new Size (520, 520);
  602. Assert.AreEqual(new Size(520, 520), form.ClientSize, "Tab1");
  603. form.Text = "SWF Taborder Test App Form";
  604. Assert.AreEqual("SWF Taborder Test App Form", form.Text, "Tab2");
  605. label1.Location = new Point(10, 10);
  606. Assert.AreEqual(new Point(10, 10), label1.Location, "Tab3");
  607. label1.Text = "Label1";
  608. form.Controls.Add(label1);
  609. label2.Location = new Point(200, 10);
  610. label2.Text = "Label2";
  611. form.Controls.Add(label2);
  612. group1.Text = "Group1";
  613. group2.Text = "Group2";
  614. group3.Text = "Group3";
  615. group1.Size = new Size(200, 400);
  616. group2.Size = new Size(200, 400);
  617. group3.Size = new Size(180, 180);
  618. Assert.AreEqual(new Size(180, 180), group3.Size, "Tab4");
  619. group1.Location = new Point(10, 40);
  620. group2.Location = new Point(220, 40);
  621. group3.Location = new Point(10, 210);
  622. group1.TabIndex = 30;
  623. Assert.AreEqual(30, group1.TabIndex, "Tab5");
  624. group1.TabStop = true;
  625. // Don't assign, test automatic assignment
  626. //group2.TabIndex = 0;
  627. group2.TabStop = true;
  628. Assert.AreEqual(0, group2.TabIndex, "Tab6");
  629. group3.TabIndex = 35;
  630. group3.TabStop = true;
  631. // Test default tab index
  632. Assert.AreEqual(0, radio11.TabIndex, "Tab7");
  633. text1.Text = "Edit Control";
  634. radio11.Text = "Radio 1-1 [Tab1]";
  635. radio12.Text = "Radio 1-2 [Tab2]";
  636. radio13.Text = "Radio 1-3 [Tab3]";
  637. radio14.Text = "Radio 1-4 [Tab4]";
  638. radio21.Text = "Radio 2-1 [Tab4]";
  639. radio22.Text = "Radio 2-2 [Tab3]";
  640. radio23.Text = "Radio 2-3 [Tab2]";
  641. radio24.Text = "Radio 2-4 [Tab1]";
  642. radio31.Text = "Radio 3-1 [Tab1]";
  643. radio32.Text = "Radio 3-2 [Tab3]";
  644. radio33.Text = "Radio 3-3 [Tab2]";
  645. radio34.Text = "Radio 3-4 [Tab4]";
  646. // We don't assign TabIndex for radio1X; test automatic assignment
  647. text1.TabStop = true;
  648. radio11.TabStop = true;
  649. radio21.TabIndex = 4;
  650. radio22.TabIndex = 3;
  651. radio23.TabIndex = 2;
  652. radio24.TabIndex = 1;
  653. radio24.TabStop = true;
  654. radio31.TabIndex = 11;
  655. radio31.TabStop = true;
  656. radio32.TabIndex = 13;
  657. radio33.TabIndex = 12;
  658. radio34.TabIndex = 14;
  659. text1.Location = new Point(10, 100);
  660. radio11.Location = new Point(10, 20);
  661. radio12.Location = new Point(10, 40);
  662. radio13.Location = new Point(10, 60);
  663. radio14.Location = new Point(10, 80);
  664. radio21.Location = new Point(10, 20);
  665. radio22.Location = new Point(10, 40);
  666. radio23.Location = new Point(10, 60);
  667. radio24.Location = new Point(10, 80);
  668. radio31.Location = new Point(10, 20);
  669. radio32.Location = new Point(10, 40);
  670. radio33.Location = new Point(10, 60);
  671. radio34.Location = new Point(10, 80);
  672. text1.Size = new Size(150, text1.PreferredHeight);
  673. radio11.Size = new Size(150, 20);
  674. radio12.Size = new Size(150, 20);
  675. radio13.Size = new Size(150, 20);
  676. radio14.Size = new Size(150, 20);
  677. radio21.Size = new Size(150, 20);
  678. radio22.Size = new Size(150, 20);
  679. radio23.Size = new Size(150, 20);
  680. radio24.Size = new Size(150, 20);
  681. radio31.Size = new Size(150, 20);
  682. radio32.Size = new Size(150, 20);
  683. radio33.Size = new Size(150, 20);
  684. radio34.Size = new Size(150, 20);
  685. group1.Controls.Add(text1);
  686. group1.Controls.Add(radio11);
  687. group1.Controls.Add(radio12);
  688. group1.Controls.Add(radio13);
  689. group1.Controls.Add(radio14);
  690. group2.Controls.Add(radio21);
  691. group2.Controls.Add(radio22);
  692. group2.Controls.Add(radio23);
  693. group2.Controls.Add(radio24);
  694. group3.Controls.Add(radio31);
  695. group3.Controls.Add(radio32);
  696. group3.Controls.Add(radio33);
  697. group3.Controls.Add(radio34);
  698. form.Controls.Add(group1);
  699. form.Controls.Add(group2);
  700. group2.Controls.Add(group3);
  701. // Perform some tests, the TabIndex stuff below will alter the outcome
  702. Assert.AreEqual(null, Helper.TestControl(group2, radio34, true), "Tab8");
  703. Assert.AreEqual(31, group2.TabIndex, "Tab9");
  704. // Does the taborder of containers and non-selectable things change behaviour?
  705. label1.TabIndex = 5;
  706. label2.TabIndex = 4;
  707. group1.TabIndex = 3;
  708. group2.TabIndex = 1;
  709. // Start verification
  710. Assert.AreEqual(null, Helper.TestControl(group2, radio34, true), "Tab10");
  711. Assert.AreEqual(radio24.Text, Helper.TestControl(group2, group2, true), "Tab11");
  712. Assert.AreEqual(radio31.Text, Helper.TestControl(group2, group3, true), "Tab12");
  713. Assert.AreEqual(null, Helper.TestControl(group1, radio14, true), "Tab13");
  714. Assert.AreEqual(radio23.Text, Helper.TestControl(group2, radio24, true), "Tab14");
  715. Assert.AreEqual(group3.Text, Helper.TestControl(group2, radio21, true), "Tab15");
  716. Assert.AreEqual(radio13.Text, Helper.TestControl(form, radio12, true), "Tab16");
  717. Assert.AreEqual(label2.Text, Helper.TestControl(form, radio14, true), "Tab17");
  718. Assert.AreEqual(group1.Text, Helper.TestControl(form, radio34, true), "Tab18");
  719. Assert.AreEqual(radio23.Text, Helper.TestControl(group2, radio24, true), "Tab19");
  720. // Sanity checks
  721. Assert.AreEqual(null, Helper.TestControl(radio11, radio21, true), "Tab20");
  722. Assert.AreEqual(text1.Text, Helper.TestControl(group1, radio21, true), "Tab21");
  723. Assert.AreEqual(radio14.Text, Helper.TestControl(form, label2, false), "Tab22");
  724. Assert.AreEqual(radio21.Text, Helper.TestControl(group2, group3, false), "Tab23");
  725. Assert.AreEqual(4, radio21.TabIndex, "Tab24");
  726. Assert.AreEqual(1, radio11.TabIndex, "Tab25");
  727. Assert.AreEqual(3, radio13.TabIndex, "Tab26");
  728. Assert.AreEqual(35, group3.TabIndex, "Tab27");
  729. Assert.AreEqual(1, group2.TabIndex, "Tab28");
  730. Assert.AreEqual(label1.Text, Helper.TestControl(form, form, false), "Tab29");
  731. Assert.AreEqual(radio14.Text, Helper.TestControl(group1, group1, false), "Tab30");
  732. Assert.AreEqual(radio34.Text, Helper.TestControl(group3, group3, false), "Tab31");
  733. Assert.AreEqual(null, Helper.TestControl(label1, label1, false), "Tab31");
  734. Assert.AreEqual(null, Helper.TestControl(radio11, radio21, false), "Tab32");
  735. form.Dispose ();
  736. }
  737. [Test]
  738. public void ScaleTest()
  739. {
  740. Control r1 = new Control();
  741. r1.Width = 40;
  742. r1.Height = 20;
  743. r1.Scale(2);
  744. Assert.AreEqual(80, r1.Width, "Scale1");
  745. Assert.AreEqual(40, r1.Height, "Scale2");
  746. }
  747. [Test]
  748. public void TextTest()
  749. {
  750. Control r1 = new Control();
  751. r1.Text = "Hi" ;
  752. Assert.AreEqual("Hi" , r1.Text , "Text1");
  753. r1.ResetText();
  754. Assert.AreEqual("" , r1.Text , "Text2");
  755. }
  756. [Test]
  757. public void PubMethodTest7()
  758. {
  759. Control r1 = new Control();
  760. r1.RightToLeft = RightToLeft.Yes ;
  761. r1.ResetRightToLeft() ;
  762. Assert.AreEqual(RightToLeft.No , r1.RightToLeft , "#81");
  763. r1.ImeMode = ImeMode.Off ;
  764. r1.ResetImeMode () ;
  765. Assert.AreEqual(ImeMode.NoControl , r1.ImeMode , "#82");
  766. r1.ForeColor= SystemColors.GrayText ;
  767. r1.ResetForeColor() ;
  768. Assert.AreEqual(SystemColors.ControlText , r1.ForeColor , "#83");
  769. //r1.Font = Font.FromHdc();
  770. r1.ResetFont () ;
  771. //Assert.AreEqual(FontFamily.GenericSansSerif , r1.Font , "#83");
  772. r1.Cursor = Cursors.Hand ;
  773. r1.ResetCursor () ;
  774. Assert.AreEqual(Cursors.Default , r1.Cursor , "#83");
  775. //r1.DataBindings = System.Windows.Forms.Binding ;
  776. //r1.ResetBindings() ;
  777. //Assert.AreEqual(ControlBindingsCollection , r1.DataBindings , "#83");
  778. r1.BackColor = Color.Black ;
  779. r1.ResetBackColor() ;
  780. Assert.AreEqual( SystemColors.Control , r1.BackColor , "#84");
  781. r1.BackColor = Color.Black ;
  782. r1.Refresh() ;
  783. Assert.AreEqual( null , r1.Region , "#85");
  784. Rectangle M = new Rectangle(10, 20, 30 ,40);
  785. r1.RectangleToScreen(M) ;
  786. Assert.AreEqual( null , r1.Region , "#86");
  787. }
  788. [Test]
  789. public void ScreenClientCoords()
  790. {
  791. Label l;
  792. Point p1;
  793. Point p2;
  794. Point p3;
  795. l = new Label();
  796. l.Left = 10;
  797. l.Top = 12;
  798. l.Visible = true;
  799. p1 = new Point (10,10);
  800. p2 = l.PointToScreen(p1);
  801. p3 = l.PointToClient(p2);
  802. Assert.AreEqual (p1, p3, "SC1");
  803. }
  804. [Test]
  805. public void ContainsTest ()
  806. {
  807. Control t = new Control ();
  808. Control s = new Control ();
  809. t.Controls.Add (s);
  810. Assert.AreEqual (true, t.Contains (s), "Con1");
  811. Assert.AreEqual (false, s.Contains (t), "Con2");
  812. Assert.AreEqual (false, s.Contains (null), "Con3");
  813. Assert.AreEqual (false, t.Contains (new Control ()), "Con4");
  814. }
  815. [Test]
  816. public void CreateHandleTest ()
  817. {
  818. Control parent;
  819. Control child;
  820. parent = null;
  821. child = null;
  822. try {
  823. parent = new Control ();
  824. child = new Control ();
  825. parent.Visible = true;
  826. parent.Controls.Add (child);
  827. Assert.IsFalse (parent.IsHandleCreated, "CH1");
  828. Assert.IsFalse (child.IsHandleCreated, "CH2");
  829. parent.CreateControl ();
  830. Assert.IsNotNull (parent.Handle, "CH3");
  831. Assert.IsNotNull (child.Handle, "CH4");
  832. Assert.IsTrue (parent.IsHandleCreated, "CH5");
  833. Assert.IsTrue (child.IsHandleCreated, "CH6");
  834. } finally {
  835. if (parent != null)
  836. parent.Dispose ();
  837. if (child != null)
  838. child.Dispose ();
  839. }
  840. // Accessing Handle Property creates the handle
  841. try {
  842. parent = new Control ();
  843. parent.Visible = true;
  844. child = new Control ();
  845. parent.Controls.Add (child);
  846. Assert.IsFalse (parent.IsHandleCreated, "CH7");
  847. Assert.IsFalse (child.IsHandleCreated, "CH8");
  848. Assert.IsNotNull (parent.Handle, "CH9");
  849. Assert.IsTrue (parent.IsHandleCreated, "CH10");
  850. Assert.IsTrue (child.IsHandleCreated, "CH11");
  851. } finally {
  852. if (parent != null)
  853. parent.Dispose ();
  854. if (child != null)
  855. child.Dispose ();
  856. }
  857. }
  858. [Test]
  859. [Category ("NotWorking")]
  860. public void CreateHandleTest2 ()
  861. {
  862. // This should eventually test all operations
  863. // that can be performed on a control (within
  864. // reason)
  865. Control c = new Control ();
  866. Assert.IsFalse (c.IsHandleCreated, "0");
  867. c.Width = 100;
  868. Assert.IsFalse (c.IsHandleCreated, "1");
  869. c.Height = 100;
  870. Assert.IsFalse (c.IsHandleCreated, "2");
  871. c.Name = "hi";
  872. Assert.IsFalse (c.IsHandleCreated, "3");
  873. c.Left = 5;
  874. Assert.IsFalse (c.IsHandleCreated, "5");
  875. c.Top = 5;
  876. Assert.IsFalse (c.IsHandleCreated, "6");
  877. c.Location = new Point (1, 1);
  878. Assert.IsFalse (c.IsHandleCreated, "7");
  879. c.Region = new Region ();
  880. Assert.IsFalse (c.IsHandleCreated, "8");
  881. c.Size = new Size (100, 100);
  882. Assert.IsFalse (c.IsHandleCreated, "9");
  883. c.Text = "bye";
  884. Assert.IsFalse (c.IsHandleCreated, "10");
  885. c.Visible = !c.Visible;
  886. Assert.IsFalse (c.IsHandleCreated, "11");
  887. }
  888. [Test]
  889. [Category ("NotWorking")]
  890. public void IsHandleCreated_NotVisible ()
  891. {
  892. Control c = new Control ();
  893. c.Visible = false;
  894. Form form = new Form ();
  895. form.ShowInTaskbar = false;
  896. form.Controls.Add (c);
  897. form.Show ();
  898. Assert.IsFalse (c.IsHandleCreated, "#1");
  899. c.Visible = true;
  900. Assert.IsTrue (c.IsHandleCreated, "#2");
  901. c.Visible = false;
  902. Assert.IsTrue (c.IsHandleCreated, "#3");
  903. }
  904. [Test]
  905. public void CreateGraphicsTest ()
  906. {
  907. Graphics g = null;
  908. Pen p = null;
  909. try {
  910. Control c = new Control ();
  911. c.SetBounds (0,0, 20, 20);
  912. g = c.CreateGraphics ();
  913. Assert.IsNotNull (g, "Graph1");
  914. } finally {
  915. if (p != null)
  916. p.Dispose ();
  917. if (g != null)
  918. g.Dispose ();
  919. }
  920. }
  921. bool delegateCalled = false;
  922. public delegate void TestDelegate ();
  923. public void delegate_call () {
  924. delegateCalled = true;
  925. }
  926. [Test]
  927. [ExpectedException(typeof(InvalidOperationException))]
  928. public void InvokeException1 () {
  929. Control c = new Control ();
  930. IAsyncResult result;
  931. result = c.BeginInvoke (new TestDelegate (delegate_call));
  932. c.EndInvoke (result);
  933. }
  934. [Test]
  935. public void FindFormTest () {
  936. Form f = new Form ();
  937. f.ShowInTaskbar = false;
  938. f.Name = "form";
  939. Control c = null;
  940. try {
  941. f.Controls.Add (c = new Control ());
  942. Assert.AreEqual (f.Name, c.FindForm ().Name, "Find1");
  943. f.Controls.Remove (c);
  944. GroupBox g = new GroupBox ();
  945. g.Name = "box";
  946. f.Controls.Add (g);
  947. g.Controls.Add (c);
  948. Assert.AreEqual (f.Name, f.FindForm ().Name, "Find2");
  949. g.Controls.Remove (c);
  950. Assert.IsNull(c.FindForm (), "Find3");
  951. } finally {
  952. if (c != null)
  953. c.Dispose ();
  954. if (f != null)
  955. f.Dispose ();
  956. }
  957. }
  958. [Test]
  959. public void FocusTest ()
  960. {
  961. Form f = null;
  962. Button c = null, d = null;
  963. try {
  964. f = new Form ();
  965. f.ShowInTaskbar = false;
  966. f.Visible = true;
  967. c = new Button ();
  968. c.Visible = true;
  969. f.Controls.Add (c);
  970. d = new Button ();
  971. d.Visible = false;
  972. f.Controls.Add (d);
  973. Assert.IsTrue (c.CanFocus, "Focus1");
  974. Assert.IsFalse (c.Focused, "Focus2");
  975. c.Focus ();
  976. Assert.IsTrue (c.Focused, "Focus3");
  977. d.Focus ();
  978. Assert.IsFalse (d.Focused, "Focus4");
  979. d.Visible = true;
  980. d.Focus ();
  981. Assert.IsTrue (d.Focused, "Focus5");
  982. Assert.IsFalse (c.Focused, "Focus6");
  983. c.Enabled = false;
  984. Assert.IsFalse (c.Focused, "Focus7");
  985. } finally {
  986. if (f != null)
  987. f.Dispose ();
  988. if (c != null)
  989. c.Dispose ();
  990. if (d != null)
  991. d.Dispose ();
  992. }
  993. }
  994. [Test]
  995. public void FromHandleTest ()
  996. {
  997. Control c1 = null;
  998. Control c2 = null;
  999. try {
  1000. c1 = new Control ();
  1001. c2 = new Control ();
  1002. c1.Name = "parent";
  1003. c2.Name = "child";
  1004. c1.Controls.Add(c2);
  1005. // Handle
  1006. Assert.AreEqual (c1.Name, Control.FromHandle (c1.Handle).Name, "Handle1");
  1007. Assert.IsNull (Control.FromHandle (IntPtr.Zero), "Handle2");
  1008. // ChildHandle
  1009. Assert.AreEqual (c1.Name, Control.FromChildHandle (c1.Handle).Name, "Handle3");
  1010. Assert.IsNull (Control.FromChildHandle (IntPtr.Zero), "Handle4");
  1011. } finally {
  1012. if (c1 != null)
  1013. c1.Dispose ();
  1014. if (c2 != null)
  1015. c2.Dispose ();
  1016. }
  1017. }
  1018. [Test]
  1019. public void GetChildAtPointTest ()
  1020. {
  1021. Control c = null, d = null, e = null;
  1022. try {
  1023. c = new Control ();
  1024. c.Name = "c1";
  1025. c.SetBounds (0, 0, 100, 100);
  1026. d = new Control ();
  1027. d.Name = "d1";
  1028. d.SetBounds (10, 10, 40, 40);
  1029. c.Controls.Add (d);
  1030. e = new Control ();
  1031. e.Name = "e1";
  1032. e.SetBounds (55, 55, 10, 10);
  1033. Control l = c.GetChildAtPoint (new Point (15, 15));
  1034. Assert.AreEqual (d.Name, l.Name, "Child1");
  1035. Assert.IsFalse (e.Name == l.Name, "Child2");
  1036. l = c.GetChildAtPoint (new Point (57, 57));
  1037. Assert.IsNull (l, "Child3");
  1038. l = c.GetChildAtPoint (new Point (10, 10));
  1039. Assert.AreEqual (d.Name, l.Name, "Child4");
  1040. // GetChildAtPointSkip is not implemented and the following test is breaking for Net_2_0 profile
  1041. // #if NET_2_0
  1042. // c.Controls.Add (e);
  1043. // e.Visible = false;
  1044. // l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
  1045. // Assert.IsNull (l, "Child5");
  1046. // e.Visible = true;
  1047. // l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
  1048. // Assert.AreSame (e.Name, l.Name, "Child6");
  1049. // #endif // NET_2_0
  1050. } finally {
  1051. if (c != null)
  1052. c.Dispose ();
  1053. if (d != null)
  1054. d.Dispose ();
  1055. }
  1056. }
  1057. [Test]
  1058. [Category ("NotWorking")] // Theme.DefaultFont change must be approved first
  1059. public void ResetFontTest ()
  1060. {
  1061. Control c = new Control ();
  1062. c.Font = new Font (c.Font.FontFamily, 3, FontStyle.Italic);
  1063. c.ResetFont ();
  1064. Assert.IsFalse (c.Font.Bold, "#1");
  1065. //Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#2");
  1066. Assert.IsFalse (c.Font.Italic, "#3");
  1067. //Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#4");
  1068. Assert.AreEqual (8.25, c.Font.Size, "#5");
  1069. Assert.AreEqual (8.25, c.Font.SizeInPoints, "#6");
  1070. Assert.IsFalse (c.Font.Strikeout, "#7");
  1071. Assert.IsFalse (c.Font.Underline, "#8");
  1072. Assert.AreEqual (GraphicsUnit.Point, c.Font.Unit, "#9");
  1073. #if NET_2_0
  1074. Assert.AreEqual (true, c.Font.IsSystemFont, "#10");
  1075. #endif
  1076. }
  1077. public class LayoutTestControl : Control {
  1078. public int LayoutCount;
  1079. public LayoutTestControl () : base() {
  1080. LayoutCount = 0;
  1081. }
  1082. protected override void OnLayout(LayoutEventArgs levent) {
  1083. LayoutCount++;
  1084. base.OnLayout (levent);
  1085. }
  1086. }
  1087. [Test]
  1088. public void LayoutTest() {
  1089. LayoutTestControl c;
  1090. c = new LayoutTestControl();
  1091. c.SuspendLayout();
  1092. c.SuspendLayout();
  1093. c.SuspendLayout();
  1094. c.SuspendLayout();
  1095. c.ResumeLayout(true);
  1096. c.PerformLayout();
  1097. c.ResumeLayout(true);
  1098. c.PerformLayout();
  1099. c.ResumeLayout(true);
  1100. c.PerformLayout();
  1101. c.ResumeLayout(true);
  1102. c.PerformLayout();
  1103. c.ResumeLayout(true);
  1104. c.PerformLayout();
  1105. c.ResumeLayout(true);
  1106. c.PerformLayout();
  1107. c.ResumeLayout(true);
  1108. c.PerformLayout();
  1109. c.SuspendLayout();
  1110. c.PerformLayout();
  1111. Assert.AreEqual(5, c.LayoutCount, "Layout Suspend/Resume locking does not bottom out at 0");
  1112. }
  1113. [Test]
  1114. [ExpectedException(typeof(ArgumentException))]
  1115. public void TransparentBackgroundTest1() {
  1116. Control c;
  1117. c = new Control();
  1118. c.BackColor = Color.Transparent;
  1119. }
  1120. [Test]
  1121. public void TransparentBackgroundTest2() {
  1122. Panel c;
  1123. c = new Panel();
  1124. c.BackColor = Color.Transparent;
  1125. Assert.AreEqual(Color.Transparent, c.BackColor, "Transparent background not set");
  1126. }
  1127. [Test]
  1128. public void TransparentBackgroundTest3() {
  1129. Control c;
  1130. c = new Control();
  1131. c.BackColor = Color.Empty;
  1132. Assert.AreEqual(Control.DefaultBackColor, c.BackColor, "Setting empty color failed");
  1133. }
  1134. [Test]
  1135. public void Dock_Value_Invalid ()
  1136. {
  1137. Control c = new Control ();
  1138. try {
  1139. c.Dock = (DockStyle) 666;
  1140. Assert.Fail ("#1");
  1141. } catch (InvalidEnumArgumentException ex) {
  1142. Assert.AreEqual (typeof (InvalidEnumArgumentException), ex.GetType (), "#2");
  1143. Assert.IsNotNull (ex.Message, "#3");
  1144. Assert.IsNotNull (ex.ParamName, "#4");
  1145. Assert.AreEqual ("value", ex.ParamName, "#5");
  1146. Assert.IsNull (ex.InnerException, "#6");
  1147. }
  1148. }
  1149. [Test]
  1150. public void EnabledTest1() {
  1151. Control child;
  1152. Control parent;
  1153. Control grandma;
  1154. grandma = new Control();
  1155. parent = new Control();
  1156. child = new Control();
  1157. grandma.Controls.Add(parent);
  1158. parent.Controls.Add(child);
  1159. grandma.Enabled = false;
  1160. Assert.AreEqual(grandma.Enabled, child.Enabled, "Child did not inherit disabled state");
  1161. }
  1162. int EnabledCalledCount = 0;
  1163. private void EnabledTest2EnabledChanged(object sender, EventArgs e) {
  1164. EnabledCalledCount++;
  1165. }
  1166. [Test]
  1167. public void EnabledTest2() {
  1168. // Check nesting of enabled calls
  1169. // OnEnabled is not called for disabled child controls
  1170. Control child;
  1171. Control parent;
  1172. Control grandma;
  1173. EnabledCalledCount = 0;
  1174. grandma = new Control();
  1175. parent = new Control();
  1176. child = new Control();
  1177. child.EnabledChanged += new EventHandler(EnabledTest2EnabledChanged);
  1178. grandma.Controls.Add(parent);
  1179. parent.Controls.Add(child);
  1180. grandma.Enabled = false;
  1181. Assert.AreEqual(1, EnabledCalledCount, "Child Enabled Event not properly fired");
  1182. grandma.Enabled = true;
  1183. Assert.AreEqual(2, EnabledCalledCount, "Child Enabled Event not properly fired");
  1184. child.Enabled = false;
  1185. grandma.Enabled = false;
  1186. Assert.AreEqual(3, EnabledCalledCount, "Child Enabled Event not properly fired");
  1187. }
  1188. [Test]
  1189. public void ControlsRemoveNullTest ()
  1190. {
  1191. Control c = new Control ();
  1192. c.Controls.Remove (null);
  1193. }
  1194. [Test]
  1195. public void ControlsAddNullTest ()
  1196. {
  1197. Control c = new Control ();
  1198. c.Controls.Add (null);
  1199. }
  1200. [Test]
  1201. [ExpectedException (typeof (ArgumentNullException))]
  1202. public void ControlsSetChildIndexNullTest ()
  1203. {
  1204. Control c = new Control ();
  1205. c.Controls.SetChildIndex (null, 1);
  1206. }
  1207. [Test]
  1208. [ExpectedException (typeof (ArgumentNullException))]
  1209. public void ControlsAddRangeNullTest ()
  1210. {
  1211. Control c = new Control ();
  1212. c.Controls.AddRange (null);
  1213. }
  1214. [Test]
  1215. public void ControlsAddRangeNullElementTest ()
  1216. {
  1217. Control c = new Control ();
  1218. Control[] subcontrols = new Control[2];
  1219. subcontrols[0] = new Control ();
  1220. subcontrols[1] = null;
  1221. c.Controls.AddRange (subcontrols);
  1222. }
  1223. [Test]
  1224. public void RegionTest () {
  1225. Form f = new Form ();
  1226. f.ShowInTaskbar = false;
  1227. Control c = new Control ();
  1228. f.Controls.Add (c);
  1229. Assert.IsNull (c.Region, "#A1");
  1230. f.Show ();
  1231. Assert.IsNull (c.Region, "#A2");
  1232. c.Region = null;
  1233. Assert.IsNull (c.Region, "#A3");
  1234. f.Dispose ();
  1235. Region region = new Region ();
  1236. f = new Form ();
  1237. f.ShowInTaskbar = false;
  1238. c = new Control ();
  1239. f.Controls.Add (c);
  1240. c.Region = region;
  1241. Assert.IsNotNull (c.Region, "#B1");
  1242. Assert.AreSame (region, c.Region, "#B2");
  1243. f.Show ();
  1244. c.Region = null;
  1245. Assert.IsNull (c.Region, "#B3");
  1246. f.Dispose ();
  1247. }
  1248. [Test] // bug #80280
  1249. public void Validated_Multiple_Containers ()
  1250. {
  1251. Form form = new Form ();
  1252. form.ShowInTaskbar = false;
  1253. UserControl control1 = new UserControl();
  1254. UserControl container1 = new UserControl();
  1255. control1.Tag = true;
  1256. control1.Validated += new EventHandler (Control_ValidatedHandler);
  1257. container1.Controls.Add(control1);
  1258. form.Controls.Add (container1);
  1259. UserControl container2 = new UserControl();
  1260. UserControl control2 = new UserControl();
  1261. container2.Controls.Add(control2);
  1262. form.Controls.Add (container2);
  1263. Assert.IsTrue ((bool) control1.Tag, "#1");
  1264. control1.Select();
  1265. Assert.IsTrue ((bool) control1.Tag, "#2");
  1266. control2.Select();
  1267. Assert.IsFalse ((bool) control1.Tag, "#3");
  1268. form.Dispose ();
  1269. }
  1270. private void Control_ValidatedHandler (object sender, EventArgs e)
  1271. {
  1272. ((Control) sender).Tag = false;
  1273. }
  1274. }
  1275. [TestFixture]
  1276. public class ControlSetTopLevelTest
  1277. {
  1278. class ControlPoker : Control {
  1279. public void DoSetTopLevel ()
  1280. {
  1281. SetTopLevel (true);
  1282. }
  1283. public bool DoGetTopLevel ()
  1284. {
  1285. return GetTopLevel ();
  1286. }
  1287. }
  1288. [Test]
  1289. public void TestControl ()
  1290. {
  1291. ControlPoker c = new ControlPoker ();
  1292. c.Visible = false;
  1293. c.DoSetTopLevel ();
  1294. Assert.IsTrue (c.DoGetTopLevel (), "1");
  1295. Assert.IsFalse (c.Visible, "2");
  1296. }
  1297. [Test]
  1298. [ExpectedException (typeof (ArgumentException))]
  1299. public void TestChildControl ()
  1300. {
  1301. Control c1 = new Control();
  1302. ControlPoker c2 = new ControlPoker ();
  1303. c1.Controls.Add (c2);
  1304. c2.DoSetTopLevel ();
  1305. }
  1306. [Test]
  1307. public void TestForm ()
  1308. {
  1309. Form f = new Form ();
  1310. Assert.IsFalse (f.Visible, "3");
  1311. f.TopLevel = true;
  1312. Assert.IsTrue (f.Visible, "4");
  1313. }
  1314. }
  1315. [TestFixture]
  1316. public class ControlResizeLayoutTest
  1317. {
  1318. class ControlPoker : Control {
  1319. public void DoOnResize ()
  1320. {
  1321. OnResize (EventArgs.Empty);
  1322. }
  1323. }
  1324. int child_event;
  1325. string child_affected_property;
  1326. void ChildLayoutEvent (object sender, LayoutEventArgs e)
  1327. {
  1328. child_event ++;
  1329. child_affected_property = e.AffectedProperty;
  1330. }
  1331. int parent_event;
  1332. string parent_affected_property;
  1333. void ParentLayoutEvent (object sender, LayoutEventArgs e)
  1334. {
  1335. parent_event ++;
  1336. parent_affected_property = e.AffectedProperty;
  1337. }
  1338. [Test]
  1339. public void Test ()
  1340. {
  1341. Panel p = new Panel ();
  1342. ControlPoker c = new ControlPoker();
  1343. p.Controls.Add (c);
  1344. p.Layout += new LayoutEventHandler (ParentLayoutEvent);
  1345. c.Layout += new LayoutEventHandler (ChildLayoutEvent);
  1346. c.DoOnResize ();
  1347. Assert.AreEqual (1, child_event, "1");
  1348. Assert.AreEqual ("Bounds", child_affected_property, "2");
  1349. Assert.AreEqual (0, parent_event, "3");
  1350. }
  1351. }
  1352. [TestFixture]
  1353. public class ControlInvokeTest {
  1354. public delegate void TestDelegate ();
  1355. Form f;
  1356. Control c;
  1357. Thread control_t;
  1358. ApplicationContext control_context;
  1359. bool delegateCalled = false;
  1360. object m;
  1361. void CreateControl ()
  1362. {
  1363. f = new Form ();
  1364. f.ShowInTaskbar = false;
  1365. c = new Control ();
  1366. f.Controls.Add (c);
  1367. Console.WriteLine ("f.Handle = {0}", f.Handle);
  1368. Console.WriteLine ("c.Handle = {0}", c.Handle);
  1369. control_context = new ApplicationContext (f);
  1370. Monitor.Enter (m);
  1371. Console.WriteLine ("pulsing");
  1372. Monitor.Pulse (m);
  1373. Monitor.Exit (m);
  1374. Console.WriteLine ("control thread running");
  1375. Application.Run (control_context);
  1376. c.Dispose ();
  1377. }
  1378. [Test]
  1379. public void InvokeTest ()
  1380. {
  1381. m = new object ();
  1382. control_t = new Thread(new ThreadStart(CreateControl));
  1383. Monitor.Enter (m);
  1384. control_t.Start ();
  1385. Console.WriteLine ("waiting on monitor");
  1386. Monitor.Wait (m);
  1387. Console.WriteLine ("making async call");
  1388. IAsyncResult result;
  1389. result = c.BeginInvoke (new TestDelegate (delegate_call));
  1390. c.EndInvoke (result);
  1391. Assert.AreEqual (true, delegateCalled, "Invoke1");
  1392. }
  1393. public void delegate_call () {
  1394. /* invoked on control_context's thread */
  1395. delegateCalled = true;
  1396. f.Dispose ();
  1397. Application.Exit ();
  1398. }
  1399. }
  1400. [TestFixture]
  1401. public class ControlWMTest
  1402. {
  1403. [Test]
  1404. public void WM_PARENTNOTIFY_Test ()
  1405. {
  1406. WMTester tester;
  1407. Control child;
  1408. int child_handle;
  1409. tester = new WMTester ();
  1410. child = new Control ();
  1411. tester.Controls.Add (child);
  1412. tester.Visible = true;
  1413. child.Visible = true;
  1414. child_handle = child.Handle.ToInt32 ();
  1415. ArrayList msgs;
  1416. Message m1;
  1417. msgs = tester.Find (WndMsg.WM_PARENTNOTIFY);
  1418. Assert.AreEqual (1, msgs.Count, "#1");
  1419. m1 = (Message) msgs [0];
  1420. Assert.AreEqual (WndMsg.WM_CREATE, ((WndMsg) LowOrder (m1.WParam)), "#2");
  1421. //Assert.AreEqual (child.Identifier??, HighOrder (m1.WParam), "#3");
  1422. Assert.AreEqual (child_handle, m1.LParam.ToInt32 (), "#4");
  1423. child.Dispose ();
  1424. msgs = tester.Find (WndMsg.WM_PARENTNOTIFY);
  1425. Assert.AreEqual (2, msgs.Count, "#5");
  1426. m1 = (Message) msgs [1];
  1427. Assert.AreEqual (WndMsg.WM_DESTROY, ((WndMsg) LowOrder (m1.WParam)), "#6");
  1428. //Assert.AreEqual (child.Identifier??, HighOrder (m1.WParam), "#7");
  1429. Assert.AreEqual (child_handle, m1.LParam.ToInt32 (), "#8");
  1430. tester.Dispose ();
  1431. }
  1432. internal static int LowOrder (int param)
  1433. {
  1434. return ((int)(short)(param & 0xffff));
  1435. }
  1436. internal static int HighOrder (int param)
  1437. {
  1438. return ((int)(short)(param >> 16));
  1439. }
  1440. internal static int LowOrder (IntPtr param)
  1441. {
  1442. return ((int)(short)(param.ToInt32 () & 0xffff));
  1443. }
  1444. internal static int HighOrder (IntPtr param)
  1445. {
  1446. return ((int)(short)(param.ToInt32 () >> 16));
  1447. }
  1448. internal class WMTester : Form
  1449. {
  1450. internal ArrayList Messages = new ArrayList ();
  1451. internal bool Contains (WndMsg msg)
  1452. {
  1453. return Contains (msg, Messages);
  1454. }
  1455. internal bool Contains (WndMsg msg, ArrayList list)
  1456. {
  1457. foreach (Message m in Messages)
  1458. {
  1459. if (m.Msg == (int) msg)
  1460. return true;
  1461. }
  1462. return false;
  1463. }
  1464. internal ArrayList Find (WndMsg msg)
  1465. {
  1466. ArrayList result = new ArrayList ();
  1467. foreach (Message m in Messages)
  1468. {
  1469. if (m.Msg == (int) msg)
  1470. result.Add (m);
  1471. }
  1472. return result;
  1473. }
  1474. protected override void WndProc(ref Message m)
  1475. {
  1476. Console.WriteLine ("WndProc: " + m.ToString ());
  1477. Messages.Add (m);
  1478. base.WndProc (ref m);
  1479. }
  1480. }
  1481. }
  1482. }