ControlTest.cs 54 KB

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