ControlTest.cs 58 KB

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