ControlTest.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  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. [Category ("NotWorking")]
  564. public void TabOrder() {
  565. Form form;
  566. Control active;
  567. Label label1 = new Label(); // To test non-tabstop items as well
  568. Label label2 = new Label();
  569. GroupBox group1 = new GroupBox();
  570. GroupBox group2 = new GroupBox();
  571. GroupBox group3 = new GroupBox();
  572. TextBox text1 = new TextBox();
  573. RadioButton radio11 = new RadioButton();
  574. RadioButton radio12 = new RadioButton();
  575. RadioButton radio13 = new RadioButton();
  576. RadioButton radio14 = new RadioButton();
  577. RadioButton radio21 = new RadioButton();
  578. RadioButton radio22 = new RadioButton();
  579. RadioButton radio23 = new RadioButton();
  580. RadioButton radio24 = new RadioButton();
  581. RadioButton radio31 = new RadioButton();
  582. RadioButton radio32 = new RadioButton();
  583. RadioButton radio33 = new RadioButton();
  584. RadioButton radio34 = new RadioButton();
  585. form = new Form();
  586. form.ShowInTaskbar = false;
  587. form.ClientSize = new Size (520, 520);
  588. Assert.AreEqual(new Size(520, 520), form.ClientSize, "Tab1");
  589. form.Text = "SWF Taborder Test App Form";
  590. Assert.AreEqual("SWF Taborder Test App Form", form.Text, "Tab2");
  591. label1.Location = new Point(10, 10);
  592. Assert.AreEqual(new Point(10, 10), label1.Location, "Tab3");
  593. label1.Text = "Label1";
  594. form.Controls.Add(label1);
  595. label2.Location = new Point(200, 10);
  596. label2.Text = "Label2";
  597. form.Controls.Add(label2);
  598. group1.Text = "Group1";
  599. group2.Text = "Group2";
  600. group3.Text = "Group3";
  601. group1.Size = new Size(200, 400);
  602. group2.Size = new Size(200, 400);
  603. group3.Size = new Size(180, 180);
  604. Assert.AreEqual(new Size(180, 180), group3.Size, "Tab4");
  605. group1.Location = new Point(10, 40);
  606. group2.Location = new Point(220, 40);
  607. group3.Location = new Point(10, 210);
  608. group1.TabIndex = 30;
  609. Assert.AreEqual(30, group1.TabIndex, "Tab5");
  610. group1.TabStop = true;
  611. // Don't assign, test automatic assignment
  612. //group2.TabIndex = 0;
  613. group2.TabStop = true;
  614. Assert.AreEqual(0, group2.TabIndex, "Tab6");
  615. group3.TabIndex = 35;
  616. group3.TabStop = true;
  617. // Test default tab index
  618. Assert.AreEqual(0, radio11.TabIndex, "Tab7");
  619. text1.Text = "Edit Control";
  620. radio11.Text = "Radio 1-1 [Tab1]";
  621. radio12.Text = "Radio 1-2 [Tab2]";
  622. radio13.Text = "Radio 1-3 [Tab3]";
  623. radio14.Text = "Radio 1-4 [Tab4]";
  624. radio21.Text = "Radio 2-1 [Tab4]";
  625. radio22.Text = "Radio 2-2 [Tab3]";
  626. radio23.Text = "Radio 2-3 [Tab2]";
  627. radio24.Text = "Radio 2-4 [Tab1]";
  628. radio31.Text = "Radio 3-1 [Tab1]";
  629. radio32.Text = "Radio 3-2 [Tab3]";
  630. radio33.Text = "Radio 3-3 [Tab2]";
  631. radio34.Text = "Radio 3-4 [Tab4]";
  632. // We don't assign TabIndex for radio1X; test automatic assignment
  633. text1.TabStop = true;
  634. radio11.TabStop = true;
  635. radio21.TabIndex = 4;
  636. radio22.TabIndex = 3;
  637. radio23.TabIndex = 2;
  638. radio24.TabIndex = 1;
  639. radio24.TabStop = true;
  640. radio31.TabIndex = 11;
  641. radio31.TabStop = true;
  642. radio32.TabIndex = 13;
  643. radio33.TabIndex = 12;
  644. radio34.TabIndex = 14;
  645. text1.Location = new Point(10, 100);
  646. radio11.Location = new Point(10, 20);
  647. radio12.Location = new Point(10, 40);
  648. radio13.Location = new Point(10, 60);
  649. radio14.Location = new Point(10, 80);
  650. radio21.Location = new Point(10, 20);
  651. radio22.Location = new Point(10, 40);
  652. radio23.Location = new Point(10, 60);
  653. radio24.Location = new Point(10, 80);
  654. radio31.Location = new Point(10, 20);
  655. radio32.Location = new Point(10, 40);
  656. radio33.Location = new Point(10, 60);
  657. radio34.Location = new Point(10, 80);
  658. text1.Size = new Size(150, text1.PreferredHeight);
  659. radio11.Size = new Size(150, 20);
  660. radio12.Size = new Size(150, 20);
  661. radio13.Size = new Size(150, 20);
  662. radio14.Size = new Size(150, 20);
  663. radio21.Size = new Size(150, 20);
  664. radio22.Size = new Size(150, 20);
  665. radio23.Size = new Size(150, 20);
  666. radio24.Size = new Size(150, 20);
  667. radio31.Size = new Size(150, 20);
  668. radio32.Size = new Size(150, 20);
  669. radio33.Size = new Size(150, 20);
  670. radio34.Size = new Size(150, 20);
  671. group1.Controls.Add(text1);
  672. group1.Controls.Add(radio11);
  673. group1.Controls.Add(radio12);
  674. group1.Controls.Add(radio13);
  675. group1.Controls.Add(radio14);
  676. group2.Controls.Add(radio21);
  677. group2.Controls.Add(radio22);
  678. group2.Controls.Add(radio23);
  679. group2.Controls.Add(radio24);
  680. group3.Controls.Add(radio31);
  681. group3.Controls.Add(radio32);
  682. group3.Controls.Add(radio33);
  683. group3.Controls.Add(radio34);
  684. form.Controls.Add(group1);
  685. form.Controls.Add(group2);
  686. group2.Controls.Add(group3);
  687. // Perform some tests, the TabIndex stuff below will alter the outcome
  688. Assert.AreEqual(null, Helper.TestControl(group2, radio34, true), "Tab8");
  689. Assert.AreEqual(31, group2.TabIndex, "Tab9");
  690. // Does the taborder of containers and non-selectable things change behaviour?
  691. label1.TabIndex = 5;
  692. label2.TabIndex = 4;
  693. group1.TabIndex = 3;
  694. group2.TabIndex = 1;
  695. // Start verification
  696. Assert.AreEqual(null, Helper.TestControl(group2, radio34, true), "Tab10");
  697. Assert.AreEqual(radio24.Text, Helper.TestControl(group2, group2, true), "Tab11");
  698. Assert.AreEqual(radio31.Text, Helper.TestControl(group2, group3, true), "Tab12");
  699. Assert.AreEqual(null, Helper.TestControl(group1, radio14, true), "Tab13");
  700. Assert.AreEqual(radio23.Text, Helper.TestControl(group2, radio24, true), "Tab14");
  701. Assert.AreEqual(group3.Text, Helper.TestControl(group2, radio21, true), "Tab15");
  702. Assert.AreEqual(radio13.Text, Helper.TestControl(form, radio12, true), "Tab16");
  703. Assert.AreEqual(label2.Text, Helper.TestControl(form, radio14, true), "Tab17");
  704. Assert.AreEqual(group1.Text, Helper.TestControl(form, radio34, true), "Tab18");
  705. Assert.AreEqual(radio23.Text, Helper.TestControl(group2, radio24, true), "Tab19");
  706. // Sanity checks
  707. Assert.AreEqual(null, Helper.TestControl(radio11, radio21, true), "Tab20");
  708. Assert.AreEqual(text1.Text, Helper.TestControl(group1, radio21, true), "Tab21");
  709. Assert.AreEqual(radio14.Text, Helper.TestControl(form, label2, false), "Tab22");
  710. Assert.AreEqual(radio21.Text, Helper.TestControl(group2, group3, false), "Tab23");
  711. Assert.AreEqual(4, radio21.TabIndex, "Tab24");
  712. Assert.AreEqual(1, radio11.TabIndex, "Tab25");
  713. Assert.AreEqual(3, radio13.TabIndex, "Tab26");
  714. Assert.AreEqual(35, group3.TabIndex, "Tab27");
  715. Assert.AreEqual(1, group2.TabIndex, "Tab28");
  716. Assert.AreEqual(label1.Text, Helper.TestControl(form, form, false), "Tab29");
  717. Assert.AreEqual(radio14.Text, Helper.TestControl(group1, group1, false), "Tab30");
  718. Assert.AreEqual(radio34.Text, Helper.TestControl(group3, group3, false), "Tab31");
  719. Assert.AreEqual(null, Helper.TestControl(label1, label1, false), "Tab31");
  720. Assert.AreEqual(null, Helper.TestControl(radio11, radio21, false), "Tab32");
  721. form.Dispose ();
  722. }
  723. [Test]
  724. public void ScaleTest()
  725. {
  726. Control r1 = new Control();
  727. r1.Width = 40;
  728. r1.Height = 20;
  729. r1.Scale(2);
  730. Assert.AreEqual(80, r1.Width, "Scale1");
  731. Assert.AreEqual(40, r1.Height, "Scale2");
  732. }
  733. [Test]
  734. public void TextTest()
  735. {
  736. Control r1 = new Control();
  737. r1.Text = "Hi" ;
  738. Assert.AreEqual("Hi" , r1.Text , "Text1");
  739. r1.ResetText();
  740. Assert.AreEqual("" , r1.Text , "Text2");
  741. }
  742. [Test]
  743. public void PubMethodTest7()
  744. {
  745. Control r1 = new Control();
  746. r1.RightToLeft = RightToLeft.Yes ;
  747. r1.ResetRightToLeft() ;
  748. Assert.AreEqual(RightToLeft.No , r1.RightToLeft , "#81");
  749. r1.ImeMode = ImeMode.Off ;
  750. r1.ResetImeMode () ;
  751. Assert.AreEqual(ImeMode.NoControl , r1.ImeMode , "#82");
  752. r1.ForeColor= SystemColors.GrayText ;
  753. r1.ResetForeColor() ;
  754. Assert.AreEqual(SystemColors.ControlText , r1.ForeColor , "#83");
  755. //r1.Font = Font.FromHdc();
  756. r1.ResetFont () ;
  757. //Assert.AreEqual(FontFamily.GenericSansSerif , r1.Font , "#83");
  758. r1.Cursor = Cursors.Hand ;
  759. r1.ResetCursor () ;
  760. Assert.AreEqual(Cursors.Default , r1.Cursor , "#83");
  761. //r1.DataBindings = System.Windows.Forms.Binding ;
  762. //r1.ResetBindings() ;
  763. //Assert.AreEqual(ControlBindingsCollection , r1.DataBindings , "#83");
  764. r1.BackColor = Color.Black ;
  765. r1.ResetBackColor() ;
  766. Assert.AreEqual( SystemColors.Control , r1.BackColor , "#84");
  767. r1.BackColor = Color.Black ;
  768. r1.Refresh() ;
  769. Assert.AreEqual( null , r1.Region , "#85");
  770. Rectangle M = new Rectangle(10, 20, 30 ,40);
  771. r1.RectangleToScreen(M) ;
  772. Assert.AreEqual( null , r1.Region , "#86");
  773. }
  774. [Test]
  775. public void ScreenClientCoords()
  776. {
  777. Label l;
  778. Point p1;
  779. Point p2;
  780. Point p3;
  781. l = new Label();
  782. l.Left = 10;
  783. l.Top = 12;
  784. l.Visible = true;
  785. p1 = new Point (10,10);
  786. p2 = l.PointToScreen(p1);
  787. p3 = l.PointToClient(p2);
  788. Assert.AreEqual (p1, p3, "SC1");
  789. }
  790. [Test]
  791. public void ContainsTest ()
  792. {
  793. Control t = new Control ();
  794. Control s = new Control ();
  795. t.Controls.Add (s);
  796. Assert.AreEqual (true, t.Contains (s), "Con1");
  797. Assert.AreEqual (false, s.Contains (t), "Con2");
  798. Assert.AreEqual (false, s.Contains (null), "Con3");
  799. Assert.AreEqual (false, t.Contains (new Control ()), "Con4");
  800. }
  801. [Test]
  802. public void CreateHandleTest ()
  803. {
  804. Control parent;
  805. Control child;
  806. parent = null;
  807. child = null;
  808. try {
  809. parent = new Control ();
  810. child = new Control ();
  811. parent.Visible = true;
  812. parent.Controls.Add (child);
  813. Assert.IsFalse (parent.IsHandleCreated, "CH1");
  814. Assert.IsFalse (child.IsHandleCreated, "CH2");
  815. parent.CreateControl ();
  816. Assert.IsNotNull (parent.Handle, "CH3");
  817. Assert.IsNotNull (child.Handle, "CH4");
  818. Assert.IsTrue (parent.IsHandleCreated, "CH5");
  819. Assert.IsTrue (child.IsHandleCreated, "CH6");
  820. } finally {
  821. if (parent != null)
  822. parent.Dispose ();
  823. if (child != null)
  824. child.Dispose ();
  825. }
  826. // Accessing Handle Property creates the handle
  827. try {
  828. parent = new Control ();
  829. parent.Visible = true;
  830. child = new Control ();
  831. parent.Controls.Add (child);
  832. Assert.IsFalse (parent.IsHandleCreated, "CH7");
  833. Assert.IsFalse (child.IsHandleCreated, "CH8");
  834. Assert.IsNotNull (parent.Handle, "CH9");
  835. Assert.IsTrue (parent.IsHandleCreated, "CH10");
  836. Assert.IsTrue (child.IsHandleCreated, "CH11");
  837. } finally {
  838. if (parent != null)
  839. parent.Dispose ();
  840. if (child != null)
  841. child.Dispose ();
  842. }
  843. }
  844. [Test]
  845. [Category ("NotWorking")]
  846. public void CreateHandleTest2 ()
  847. {
  848. // This should eventually test all operations
  849. // that can be performed on a control (within
  850. // reason)
  851. Control c = new Control ();
  852. Assert.IsFalse (c.IsHandleCreated, "0");
  853. c.Width = 100;
  854. Assert.IsFalse (c.IsHandleCreated, "1");
  855. c.Height = 100;
  856. Assert.IsFalse (c.IsHandleCreated, "2");
  857. c.Name = "hi";
  858. Assert.IsFalse (c.IsHandleCreated, "3");
  859. c.Left = 5;
  860. Assert.IsFalse (c.IsHandleCreated, "5");
  861. c.Top = 5;
  862. Assert.IsFalse (c.IsHandleCreated, "6");
  863. c.Location = new Point (1, 1);
  864. Assert.IsFalse (c.IsHandleCreated, "7");
  865. c.Region = new Region ();
  866. Assert.IsFalse (c.IsHandleCreated, "8");
  867. c.Size = new Size (100, 100);
  868. Assert.IsFalse (c.IsHandleCreated, "9");
  869. c.Text = "bye";
  870. Assert.IsFalse (c.IsHandleCreated, "10");
  871. c.Visible = !c.Visible;
  872. Assert.IsFalse (c.IsHandleCreated, "11");
  873. }
  874. [Test]
  875. [Category ("NotWorking")]
  876. public void IsHandleCreated_NotVisible ()
  877. {
  878. Control c = new Control ();
  879. c.Visible = false;
  880. Form form = new Form ();
  881. form.ShowInTaskbar = false;
  882. form.Controls.Add (c);
  883. form.Show ();
  884. Assert.IsFalse (c.IsHandleCreated, "#1");
  885. c.Visible = true;
  886. Assert.IsTrue (c.IsHandleCreated, "#2");
  887. c.Visible = false;
  888. Assert.IsTrue (c.IsHandleCreated, "#3");
  889. }
  890. [Test]
  891. public void CreateGraphicsTest ()
  892. {
  893. Graphics g = null;
  894. Pen p = null;
  895. try {
  896. Control c = new Control ();
  897. c.SetBounds (0,0, 20, 20);
  898. g = c.CreateGraphics ();
  899. Assert.IsNotNull (g, "Graph1");
  900. } finally {
  901. if (p != null)
  902. p.Dispose ();
  903. if (g != null)
  904. g.Dispose ();
  905. }
  906. }
  907. bool delegateCalled = false;
  908. public delegate void TestDelegate ();
  909. public void delegate_call () {
  910. delegateCalled = true;
  911. }
  912. [Test]
  913. [ExpectedException(typeof(InvalidOperationException))]
  914. public void InvokeException1 () {
  915. Control c = new Control ();
  916. IAsyncResult result;
  917. result = c.BeginInvoke (new TestDelegate (delegate_call));
  918. c.EndInvoke (result);
  919. }
  920. [Test]
  921. public void FindFormTest () {
  922. Form f = new Form ();
  923. f.ShowInTaskbar = false;
  924. f.Name = "form";
  925. Control c = null;
  926. try {
  927. f.Controls.Add (c = new Control ());
  928. Assert.AreEqual (f.Name, c.FindForm ().Name, "Find1");
  929. f.Controls.Remove (c);
  930. GroupBox g = new GroupBox ();
  931. g.Name = "box";
  932. f.Controls.Add (g);
  933. g.Controls.Add (c);
  934. Assert.AreEqual (f.Name, f.FindForm ().Name, "Find2");
  935. g.Controls.Remove (c);
  936. Assert.IsNull(c.FindForm (), "Find3");
  937. } finally {
  938. if (c != null)
  939. c.Dispose ();
  940. if (f != null)
  941. f.Dispose ();
  942. }
  943. }
  944. [Test]
  945. public void FocusTest ()
  946. {
  947. Form f = null;
  948. Button c = null, d = null;
  949. try {
  950. f = new Form ();
  951. f.ShowInTaskbar = false;
  952. f.Visible = true;
  953. c = new Button ();
  954. c.Visible = true;
  955. f.Controls.Add (c);
  956. d = new Button ();
  957. d.Visible = false;
  958. f.Controls.Add (d);
  959. Assert.IsTrue (c.CanFocus, "Focus1");
  960. Assert.IsFalse (c.Focused, "Focus2");
  961. c.Focus ();
  962. Assert.IsTrue (c.Focused, "Focus3");
  963. d.Focus ();
  964. Assert.IsFalse (d.Focused, "Focus4");
  965. d.Visible = true;
  966. d.Focus ();
  967. Assert.IsTrue (d.Focused, "Focus5");
  968. Assert.IsFalse (c.Focused, "Focus6");
  969. c.Enabled = false;
  970. Assert.IsFalse (c.Focused, "Focus7");
  971. } finally {
  972. if (f != null)
  973. f.Dispose ();
  974. if (c != null)
  975. c.Dispose ();
  976. if (d != null)
  977. d.Dispose ();
  978. }
  979. }
  980. [Test]
  981. public void FromHandleTest ()
  982. {
  983. Control c1 = null;
  984. Control c2 = null;
  985. try {
  986. c1 = new Control ();
  987. c2 = new Control ();
  988. c1.Name = "parent";
  989. c2.Name = "child";
  990. c1.Controls.Add(c2);
  991. // Handle
  992. Assert.AreEqual (c1.Name, Control.FromHandle (c1.Handle).Name, "Handle1");
  993. Assert.IsNull (Control.FromHandle (IntPtr.Zero), "Handle2");
  994. // ChildHandle
  995. Assert.AreEqual (c1.Name, Control.FromChildHandle (c1.Handle).Name, "Handle3");
  996. Assert.IsNull (Control.FromChildHandle (IntPtr.Zero), "Handle4");
  997. } finally {
  998. if (c1 != null)
  999. c1.Dispose ();
  1000. if (c2 != null)
  1001. c2.Dispose ();
  1002. }
  1003. }
  1004. [Test]
  1005. public void GetChildAtPointTest ()
  1006. {
  1007. Control c = null, d = null, e = null;
  1008. try {
  1009. c = new Control ();
  1010. c.Name = "c1";
  1011. c.SetBounds (0, 0, 100, 100);
  1012. d = new Control ();
  1013. d.Name = "d1";
  1014. d.SetBounds (10, 10, 40, 40);
  1015. c.Controls.Add (d);
  1016. e = new Control ();
  1017. e.Name = "e1";
  1018. e.SetBounds (55, 55, 10, 10);
  1019. Control l = c.GetChildAtPoint (new Point (15, 15));
  1020. Assert.AreEqual (d.Name, l.Name, "Child1");
  1021. Assert.IsFalse (e.Name == l.Name, "Child2");
  1022. l = c.GetChildAtPoint (new Point (57, 57));
  1023. Assert.IsNull (l, "Child3");
  1024. l = c.GetChildAtPoint (new Point (10, 10));
  1025. Assert.AreEqual (d.Name, l.Name, "Child4");
  1026. // GetChildAtPointSkip is not implemented and the following test is breaking for Net_2_0 profile
  1027. // #if NET_2_0
  1028. // c.Controls.Add (e);
  1029. // e.Visible = false;
  1030. // l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
  1031. // Assert.IsNull (l, "Child5");
  1032. // e.Visible = true;
  1033. // l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
  1034. // Assert.AreSame (e.Name, l.Name, "Child6");
  1035. // #endif // NET_2_0
  1036. } finally {
  1037. if (c != null)
  1038. c.Dispose ();
  1039. if (d != null)
  1040. d.Dispose ();
  1041. }
  1042. }
  1043. [Test]
  1044. [Category ("NotWorking")] // Theme.DefaultFont change must be approved first
  1045. public void ResetFontTest ()
  1046. {
  1047. Control c = new Control ();
  1048. c.Font = new Font (c.Font.FontFamily, 3, FontStyle.Italic);
  1049. c.ResetFont ();
  1050. Assert.IsFalse (c.Font.Bold, "#1");
  1051. //Assert.AreEqual ("Microsoft Sans Serif", c.Font.FontFamily.Name, "#2");
  1052. Assert.IsFalse (c.Font.Italic, "#3");
  1053. //Assert.AreEqual ("Microsoft Sans Serif", c.Font.Name, "#4");
  1054. Assert.AreEqual (8.25, c.Font.Size, "#5");
  1055. Assert.AreEqual (8.25, c.Font.SizeInPoints, "#6");
  1056. Assert.IsFalse (c.Font.Strikeout, "#7");
  1057. Assert.IsFalse (c.Font.Underline, "#8");
  1058. Assert.AreEqual (GraphicsUnit.Point, c.Font.Unit, "#9");
  1059. #if NET_2_0
  1060. Assert.AreEqual (true, c.Font.IsSystemFont, "#10");
  1061. #endif
  1062. }
  1063. public class LayoutTestControl : Control {
  1064. public int LayoutCount;
  1065. public LayoutTestControl () : base() {
  1066. LayoutCount = 0;
  1067. }
  1068. protected override void OnLayout(LayoutEventArgs levent) {
  1069. LayoutCount++;
  1070. base.OnLayout (levent);
  1071. }
  1072. }
  1073. [Test]
  1074. public void LayoutTest() {
  1075. LayoutTestControl c;
  1076. c = new LayoutTestControl();
  1077. c.SuspendLayout();
  1078. c.SuspendLayout();
  1079. c.SuspendLayout();
  1080. c.SuspendLayout();
  1081. c.ResumeLayout(true);
  1082. c.PerformLayout();
  1083. c.ResumeLayout(true);
  1084. c.PerformLayout();
  1085. c.ResumeLayout(true);
  1086. c.PerformLayout();
  1087. c.ResumeLayout(true);
  1088. c.PerformLayout();
  1089. c.ResumeLayout(true);
  1090. c.PerformLayout();
  1091. c.ResumeLayout(true);
  1092. c.PerformLayout();
  1093. c.ResumeLayout(true);
  1094. c.PerformLayout();
  1095. c.SuspendLayout();
  1096. c.PerformLayout();
  1097. Assert.AreEqual(5, c.LayoutCount, "Layout Suspend/Resume locking does not bottom out at 0");
  1098. }
  1099. [Test]
  1100. [ExpectedException(typeof(ArgumentException))]
  1101. public void TransparentBackgroundTest1() {
  1102. Control c;
  1103. c = new Control();
  1104. c.BackColor = Color.Transparent;
  1105. }
  1106. [Test]
  1107. public void TransparentBackgroundTest2() {
  1108. Panel c;
  1109. c = new Panel();
  1110. c.BackColor = Color.Transparent;
  1111. Assert.AreEqual(Color.Transparent, c.BackColor, "Transparent background not set");
  1112. }
  1113. [Test]
  1114. public void TransparentBackgroundTest3() {
  1115. Control c;
  1116. c = new Control();
  1117. c.BackColor = Color.Empty;
  1118. Assert.AreEqual(Control.DefaultBackColor, c.BackColor, "Setting empty color failed");
  1119. }
  1120. [Test]
  1121. public void Dock_Value_Invalid ()
  1122. {
  1123. Control c = new Control ();
  1124. try {
  1125. c.Dock = (DockStyle) 666;
  1126. Assert.Fail ("#1");
  1127. } catch (InvalidEnumArgumentException ex) {
  1128. Assert.AreEqual (typeof (InvalidEnumArgumentException), ex.GetType (), "#2");
  1129. Assert.IsNotNull (ex.Message, "#3");
  1130. Assert.IsNotNull (ex.ParamName, "#4");
  1131. Assert.AreEqual ("value", ex.ParamName, "#5");
  1132. Assert.IsNull (ex.InnerException, "#6");
  1133. }
  1134. }
  1135. [Test]
  1136. public void EnabledTest1() {
  1137. Control child;
  1138. Control parent;
  1139. Control grandma;
  1140. grandma = new Control();
  1141. parent = new Control();
  1142. child = new Control();
  1143. grandma.Controls.Add(parent);
  1144. parent.Controls.Add(child);
  1145. grandma.Enabled = false;
  1146. Assert.AreEqual(grandma.Enabled, child.Enabled, "Child did not inherit disabled state");
  1147. }
  1148. int EnabledCalledCount = 0;
  1149. private void EnabledTest2EnabledChanged(object sender, EventArgs e) {
  1150. EnabledCalledCount++;
  1151. }
  1152. [Test]
  1153. public void EnabledTest2() {
  1154. // Check nesting of enabled calls
  1155. // OnEnabled is not called for disabled child controls
  1156. Control child;
  1157. Control parent;
  1158. Control grandma;
  1159. EnabledCalledCount = 0;
  1160. grandma = new Control();
  1161. parent = new Control();
  1162. child = new Control();
  1163. child.EnabledChanged += new EventHandler(EnabledTest2EnabledChanged);
  1164. grandma.Controls.Add(parent);
  1165. parent.Controls.Add(child);
  1166. grandma.Enabled = false;
  1167. Assert.AreEqual(1, EnabledCalledCount, "Child Enabled Event not properly fired");
  1168. grandma.Enabled = true;
  1169. Assert.AreEqual(2, EnabledCalledCount, "Child Enabled Event not properly fired");
  1170. child.Enabled = false;
  1171. grandma.Enabled = false;
  1172. Assert.AreEqual(3, EnabledCalledCount, "Child Enabled Event not properly fired");
  1173. }
  1174. [Test]
  1175. public void ControlsRemoveNullTest ()
  1176. {
  1177. Control c = new Control ();
  1178. c.Controls.Remove (null);
  1179. }
  1180. [Test]
  1181. public void ControlsAddNullTest ()
  1182. {
  1183. Control c = new Control ();
  1184. c.Controls.Add (null);
  1185. }
  1186. [Test]
  1187. [ExpectedException (typeof (ArgumentNullException))]
  1188. public void ControlsSetChildIndexNullTest ()
  1189. {
  1190. Control c = new Control ();
  1191. c.Controls.SetChildIndex (null, 1);
  1192. }
  1193. [Test]
  1194. [ExpectedException (typeof (ArgumentNullException))]
  1195. public void ControlsAddRangeNullTest ()
  1196. {
  1197. Control c = new Control ();
  1198. c.Controls.AddRange (null);
  1199. }
  1200. [Test]
  1201. public void ControlsAddRangeNullElementTest ()
  1202. {
  1203. Control c = new Control ();
  1204. Control[] subcontrols = new Control[2];
  1205. subcontrols[0] = new Control ();
  1206. subcontrols[1] = null;
  1207. c.Controls.AddRange (subcontrols);
  1208. }
  1209. [Test]
  1210. public void RegionTest () {
  1211. Form f = new Form ();
  1212. f.ShowInTaskbar = false;
  1213. Control c = new Control ();
  1214. f.Controls.Add (c);
  1215. Assert.IsNull (c.Region, "#A1");
  1216. f.Show ();
  1217. Assert.IsNull (c.Region, "#A2");
  1218. c.Region = null;
  1219. Assert.IsNull (c.Region, "#A3");
  1220. f.Dispose ();
  1221. Region region = new Region ();
  1222. f = new Form ();
  1223. f.ShowInTaskbar = false;
  1224. c = new Control ();
  1225. f.Controls.Add (c);
  1226. c.Region = region;
  1227. Assert.IsNotNull (c.Region, "#B1");
  1228. Assert.AreSame (region, c.Region, "#B2");
  1229. f.Show ();
  1230. c.Region = null;
  1231. Assert.IsNull (c.Region, "#B3");
  1232. f.Dispose ();
  1233. }
  1234. [Test] // bug #80280
  1235. public void Validated_Multiple_Containers ()
  1236. {
  1237. Form form = new Form ();
  1238. form.ShowInTaskbar = false;
  1239. UserControl control1 = new UserControl();
  1240. UserControl container1 = new UserControl();
  1241. control1.Tag = true;
  1242. control1.Validated += new EventHandler (Control_ValidatedHandler);
  1243. container1.Controls.Add(control1);
  1244. form.Controls.Add (container1);
  1245. UserControl container2 = new UserControl();
  1246. UserControl control2 = new UserControl();
  1247. container2.Controls.Add(control2);
  1248. form.Controls.Add (container2);
  1249. Assert.IsTrue ((bool) control1.Tag, "#1");
  1250. control1.Select();
  1251. Assert.IsTrue ((bool) control1.Tag, "#2");
  1252. control2.Select();
  1253. Assert.IsFalse ((bool) control1.Tag, "#3");
  1254. form.Dispose ();
  1255. }
  1256. private void Control_ValidatedHandler (object sender, EventArgs e)
  1257. {
  1258. ((Control) sender).Tag = false;
  1259. }
  1260. }
  1261. [TestFixture]
  1262. public class ControlResizeLayoutTest
  1263. {
  1264. class ControlPoker : Control {
  1265. public void DoOnResize ()
  1266. {
  1267. OnResize (EventArgs.Empty);
  1268. }
  1269. }
  1270. int child_event;
  1271. string child_affected_property;
  1272. void ChildLayoutEvent (object sender, LayoutEventArgs e)
  1273. {
  1274. child_event ++;
  1275. child_affected_property = e.AffectedProperty;
  1276. }
  1277. int parent_event;
  1278. string parent_affected_property;
  1279. void ParentLayoutEvent (object sender, LayoutEventArgs e)
  1280. {
  1281. parent_event ++;
  1282. parent_affected_property = e.AffectedProperty;
  1283. }
  1284. [Test]
  1285. public void Test ()
  1286. {
  1287. Panel p = new Panel ();
  1288. ControlPoker c = new ControlPoker();
  1289. p.Controls.Add (c);
  1290. p.Layout += new LayoutEventHandler (ParentLayoutEvent);
  1291. c.Layout += new LayoutEventHandler (ChildLayoutEvent);
  1292. c.DoOnResize ();
  1293. Assert.AreEqual (1, child_event, "1");
  1294. Assert.AreEqual ("Bounds", child_affected_property, "2");
  1295. Assert.AreEqual (0, parent_event, "3");
  1296. }
  1297. }
  1298. [TestFixture]
  1299. public class ControlInvokeTest {
  1300. public delegate void TestDelegate ();
  1301. Form f;
  1302. Control c;
  1303. Thread control_t;
  1304. ApplicationContext control_context;
  1305. bool delegateCalled = false;
  1306. object m;
  1307. void CreateControl ()
  1308. {
  1309. f = new Form ();
  1310. f.ShowInTaskbar = false;
  1311. c = new Control ();
  1312. f.Controls.Add (c);
  1313. Console.WriteLine ("f.Handle = {0}", f.Handle);
  1314. Console.WriteLine ("c.Handle = {0}", c.Handle);
  1315. control_context = new ApplicationContext (f);
  1316. Monitor.Enter (m);
  1317. Console.WriteLine ("pulsing");
  1318. Monitor.Pulse (m);
  1319. Monitor.Exit (m);
  1320. Console.WriteLine ("control thread running");
  1321. Application.Run (control_context);
  1322. c.Dispose ();
  1323. }
  1324. [Test]
  1325. public void InvokeTest ()
  1326. {
  1327. m = new object ();
  1328. control_t = new Thread(new ThreadStart(CreateControl));
  1329. Monitor.Enter (m);
  1330. control_t.Start ();
  1331. Console.WriteLine ("waiting on monitor");
  1332. Monitor.Wait (m);
  1333. Console.WriteLine ("making async call");
  1334. IAsyncResult result;
  1335. result = c.BeginInvoke (new TestDelegate (delegate_call));
  1336. c.EndInvoke (result);
  1337. Assert.AreEqual (true, delegateCalled, "Invoke1");
  1338. }
  1339. public void delegate_call () {
  1340. /* invoked on control_context's thread */
  1341. delegateCalled = true;
  1342. f.Dispose ();
  1343. Application.Exit ();
  1344. }
  1345. }
  1346. [TestFixture]
  1347. public class ControlWMTest
  1348. {
  1349. [Test]
  1350. public void WM_PARENTNOTIFY_Test ()
  1351. {
  1352. WMTester tester;
  1353. Control child;
  1354. int child_handle;
  1355. tester = new WMTester ();
  1356. child = new Control ();
  1357. tester.Controls.Add (child);
  1358. tester.Visible = true;
  1359. child.Visible = true;
  1360. child_handle = child.Handle.ToInt32 ();
  1361. ArrayList msgs;
  1362. Message m1;
  1363. msgs = tester.Find (WndMsg.WM_PARENTNOTIFY);
  1364. Assert.AreEqual (1, msgs.Count, "#1");
  1365. m1 = (Message) msgs [0];
  1366. Assert.AreEqual (WndMsg.WM_CREATE, ((WndMsg) LowOrder (m1.WParam)), "#2");
  1367. //Assert.AreEqual (child.Identifier??, HighOrder (m1.WParam), "#3");
  1368. Assert.AreEqual (child_handle, m1.LParam.ToInt32 (), "#4");
  1369. child.Dispose ();
  1370. msgs = tester.Find (WndMsg.WM_PARENTNOTIFY);
  1371. Assert.AreEqual (2, msgs.Count, "#5");
  1372. m1 = (Message) msgs [1];
  1373. Assert.AreEqual (WndMsg.WM_DESTROY, ((WndMsg) LowOrder (m1.WParam)), "#6");
  1374. //Assert.AreEqual (child.Identifier??, HighOrder (m1.WParam), "#7");
  1375. Assert.AreEqual (child_handle, m1.LParam.ToInt32 (), "#8");
  1376. tester.Dispose ();
  1377. }
  1378. internal static int LowOrder (int param)
  1379. {
  1380. return ((int)(short)(param & 0xffff));
  1381. }
  1382. internal static int HighOrder (int param)
  1383. {
  1384. return ((int)(short)(param >> 16));
  1385. }
  1386. internal static int LowOrder (IntPtr param)
  1387. {
  1388. return ((int)(short)(param.ToInt32 () & 0xffff));
  1389. }
  1390. internal static int HighOrder (IntPtr param)
  1391. {
  1392. return ((int)(short)(param.ToInt32 () >> 16));
  1393. }
  1394. internal class WMTester : Form
  1395. {
  1396. internal ArrayList Messages = new ArrayList ();
  1397. internal bool Contains (WndMsg msg)
  1398. {
  1399. return Contains (msg, Messages);
  1400. }
  1401. internal bool Contains (WndMsg msg, ArrayList list)
  1402. {
  1403. foreach (Message m in Messages)
  1404. {
  1405. if (m.Msg == (int) msg)
  1406. return true;
  1407. }
  1408. return false;
  1409. }
  1410. internal ArrayList Find (WndMsg msg)
  1411. {
  1412. ArrayList result = new ArrayList ();
  1413. foreach (Message m in Messages)
  1414. {
  1415. if (m.Msg == (int) msg)
  1416. result.Add (m);
  1417. }
  1418. return result;
  1419. }
  1420. protected override void WndProc(ref Message m)
  1421. {
  1422. Console.WriteLine ("WndProc: " + m.ToString ());
  1423. Messages.Add (m);
  1424. base.WndProc (ref m);
  1425. }
  1426. }
  1427. }
  1428. }