ControlTest.cs 71 KB

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