ControlTest.cs 94 KB

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