ControlTest.cs 78 KB

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