ControlTest.cs 51 KB

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