ControlTest.cs 53 KB

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