ControlTest.cs 52 KB

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