ControlTest.cs 80 KB

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