ControlTest.cs 97 KB

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