ControlTest.cs 50 KB

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