ControlTest.cs 51 KB

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