ControlTest.cs 54 KB

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