ControlTest.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. //
  2. // Copyright (c) 2005 Novell, Inc.
  3. //
  4. // Authors:
  5. // Ritvik Mayank ([email protected])
  6. //
  7. using System;
  8. using System.Windows.Forms;
  9. using System.Drawing;
  10. using System.Threading;
  11. using System.Reflection;
  12. using System.Runtime.Remoting;
  13. using NUnit.Framework;
  14. [TestFixture]
  15. public class ControlTest
  16. {
  17. [Test]
  18. public void PubPropTest()
  19. {
  20. Control c = new Control();
  21. Assert.AreEqual(false, c.AccessibilityObject == null, "#1");
  22. Assert.AreEqual(false, c.AllowDrop , "#2");
  23. Assert.AreEqual(AnchorStyles.Top | AnchorStyles.Left, c.Anchor, "#4");
  24. Assert.AreEqual("Control", c.BackColor.Name , "#6");
  25. Assert.AreEqual(null, c.BindingContext, "#7");
  26. Assert.AreEqual(null, c.BackgroundImage, "#8");
  27. Assert.AreEqual(0, c.Bottom, "#10");
  28. //Assert.AreEqual (Rectangle (0,0, 100, 23) , c.Bounds , "#11");
  29. //Assert.AreEqual ( BoundsSpecified.X , c.Bounds , "#11");
  30. Assert.AreEqual(false, c.CanFocus, "#12");
  31. Assert.AreEqual(true, c.CanSelect, "#13");
  32. Assert.AreEqual(false, c.Capture, "#14");
  33. Assert.AreEqual(true, c.CausesValidation, "#15");
  34. //Assert.AreEqual (false , c.ClientRectangle , "#16");
  35. //Assert.AreEqual (false , c.ClientSize , "#17");
  36. // Assert.AreEqual("Mono Project, Novell, Inc.", c.CompanyName, "#18");
  37. Assert.AreEqual(null, c.Container, "#19");
  38. Assert.AreEqual(false, c.ContainsFocus, "#20");
  39. Assert.AreEqual(null, c.ContextMenu, "#21");
  40. //Assert.AreEqual (Control+ControlCollection , c.Controls, "#22");
  41. Assert.AreEqual(true, c.Created, "#23");
  42. Assert.AreEqual(Cursors.Default, c.Cursor, "#24");
  43. Assert.AreEqual(false, c.DataBindings == null, "#25");
  44. //Assert.AreEqual(false, c.DefaultBackColor, "#25a");
  45. //Assert.AreEqual(false, c.DefaultForeColor == null, "#25b");
  46. //Assert.AreEqual(false, c.DefaultFont == null, "#25c");
  47. //Assert.AreEqual (false , c.DisplayRectangle , "#26");
  48. //<{X=0,Y=0,Width=100,Height=23}>
  49. Assert.AreEqual(false, c.Disposing, "#27");
  50. Assert.AreEqual(DockStyle.None, c.Dock, "#28");
  51. Assert.AreEqual(true, c.Enabled, "#29");
  52. Assert.AreEqual(false, c.Focused, "#31");
  53. //Assert.AreEqual(FontFamily.GenericSansSerif, c.Font, "#32");
  54. Assert.AreEqual(SystemColors.ControlText, c.ForeColor, "#33");
  55. //Assert.AreEqual (IWin32Window.Handle , c.Handle, "#34");
  56. Assert.AreEqual(false, c.HasChildren, "#35");
  57. Assert.AreEqual(0, c.Height, "#36");
  58. //Assert.AreEqual (false , c.ImeMode, "#41");
  59. Assert.AreEqual(false, c.InvokeRequired, "#42");
  60. Assert.AreEqual(false, c.IsAccessible, "#43");
  61. Assert.AreEqual(false, c.IsDisposed, "#44");
  62. Assert.AreEqual(true, c.IsHandleCreated, "#45");
  63. Assert.AreEqual(0, c.Left, "#46");
  64. Assert.AreEqual(Point.Empty, c.Location, "#47");
  65. //Assert.AreEqual(Point.Empty, c.ModifierKeys, "#47a");
  66. //Assert.AreEqual(Point.Empty, c.MousePosition, "#47b");
  67. //Assert.AreEqual(Point.Empty, c.MouseButtons, "#47c");
  68. Assert.AreEqual("", c.Name, "#48");
  69. Assert.AreEqual(null, c.Parent, "#49");
  70. //Assert.AreEqual("Novell Mono MWF", c.ProductName, "#52");
  71. Assert.AreEqual("1.1.4322.573", c.ProductVersion, "#53");
  72. Assert.AreEqual(false, c.RecreatingHandle, "#54");
  73. Assert.AreEqual(null, c.Region, "#55");
  74. Assert.AreEqual(0, c.Right, "#56");
  75. Assert.AreEqual(RightToLeft.No, c.RightToLeft, "#57");
  76. Assert.AreEqual(null, c.Site, "#58");
  77. //Assert.AreEqual (false , c.Size, "#59");
  78. //Assert.AreEqual(true , c.TabIndex , "#60");
  79. //true , 0
  80. Assert.AreEqual(true, c.TabStop, "#60a");
  81. Assert.AreEqual(null, c.Tag, "#61");
  82. Assert.AreEqual("", c.Text, "#62");
  83. Assert.AreEqual(0, c.Top, "#64");
  84. Assert.AreEqual(null, c.TopLevelControl, "#65");
  85. Assert.AreEqual(true, c.Visible, "#67");
  86. Assert.AreEqual(0, c.Width, "#68");
  87. }
  88. [Test]
  89. public void PubMethodTest2()
  90. {
  91. Control C1 = new Control();
  92. Control ctl = new Control();
  93. C1.Show() ;
  94. Assert.AreEqual(true , C1.Visible , "#69");
  95. Assert.AreEqual(false, C1.Contains(ctl) , "#70");
  96. Assert.AreEqual("System.Windows.Forms.Control", C1.ToString() , "#71");
  97. //C1.Update ();
  98. //Assert.AreEqual(false, C1 , "#70");
  99. //Causes the control to redraw the invalidated regions within its client area.
  100. C1.Anchor = AnchorStyles.Top;
  101. C1.SuspendLayout ();
  102. C1.Anchor = AnchorStyles.Left ;
  103. C1.ResumeLayout ();
  104. Assert.AreEqual(AnchorStyles.Left , C1.Anchor, "#73");
  105. C1.SetBounds(10, 20, 30, 40) ;
  106. Assert.AreEqual( 20 ,C1.Bounds.Top , "#74a");
  107. Assert.AreEqual( 10 ,C1.Bounds.Left , "#74b");
  108. Assert.AreEqual( 30 ,C1.Bounds.Width , "#74c");
  109. Assert.AreEqual( 40 ,C1.Bounds.Height , "#74d");
  110. //C1.SendToBack() ;
  111. //Assert.AreEqual( false , C1.Bounds , "#75");
  112. }
  113. [Test]
  114. public void PubMethodTest3()
  115. {
  116. Control B = new Control();
  117. //Form frm = new Form.ControlNativeWindow() ;
  118. //B.SelectNextControl(frm) ;
  119. //Assert.AreEqual(true, B.TabStop , "#75");
  120. //LayoutEventArgs l = LayoutEventArgs ();
  121. //Layout L1 = B.PerformLayout(l) ;
  122. //Anchor = AnchorStyles.Bottom
  123. B.SuspendLayout() ;
  124. B.Size = Size.Empty ;
  125. B.Location = Point.Empty;
  126. B.Anchor = AnchorStyles.Bottom;
  127. B.Dock = DockStyle.Left;
  128. B.ResumeLayout() ;
  129. //Assert.AreEqual(false, B.Size , "#75a");
  130. //Assert.AreEqual(false, B.Location , "#75b");
  131. Assert.AreEqual(AnchorStyles.Top | AnchorStyles.Left , B.Anchor , "#75c");
  132. Assert.AreEqual(DockStyle.None , B.Dock , "#75d");
  133. }
  134. [Test]
  135. public void PubMethodTest4()
  136. {
  137. Control s1 = new Control();
  138. Control s2 = new Control();
  139. s1.Text = "abc";
  140. s2.Text = "abc";
  141. Assert.AreEqual(false, s1.Equals(s2), "#76");
  142. Assert.AreEqual(true, s1.Equals(s1), "#77");
  143. }
  144. [Test]
  145. public void PubMethodTest5()
  146. {
  147. Control r1 = new Control();
  148. r1.Width = 40;
  149. r1.Height = 20;
  150. r1.Scale(2);
  151. Assert.AreEqual(80, r1.Width, "#78");
  152. Assert.AreEqual(40, r1.Height, "#79");
  153. }
  154. [Test]
  155. public void PubMethodTest6()
  156. {
  157. Control r1 = new Control();
  158. r1.Text = "Hi" ;
  159. r1.ResetText();
  160. Assert.AreEqual("" , r1.Text , "#80");
  161. }
  162. [Test]
  163. public void PubMethodTest7()
  164. {
  165. Control r1 = new Control();
  166. r1.RightToLeft = RightToLeft.Yes ;
  167. r1.ResetRightToLeft() ;
  168. Assert.AreEqual(RightToLeft.No , r1.RightToLeft , "#81");
  169. r1.ImeMode = ImeMode.Off ;
  170. r1.ResetImeMode () ;
  171. Assert.AreEqual(ImeMode.NoControl , r1.ImeMode , "#82");
  172. r1.ForeColor= SystemColors.GrayText ;
  173. r1.ResetForeColor() ;
  174. Assert.AreEqual(SystemColors.ControlText , r1.ForeColor , "#83");
  175. //r1.Font = Font.FromHdc();
  176. r1.ResetFont () ;
  177. //Assert.AreEqual(FontFamily.GenericSansSerif , r1.Font , "#83");
  178. r1.Cursor = Cursors.Hand ;
  179. r1.ResetCursor () ;
  180. Assert.AreEqual(Cursors.Default , r1.Cursor , "#83");
  181. //r1.DataBindings = System.Windows.Forms.Binding ;
  182. //r1.ResetBindings() ;
  183. //Assert.AreEqual(ControlBindingsCollection , r1.DataBindings , "#83");
  184. r1.BackColor = System.Drawing.Color.Black ;
  185. r1.ResetBackColor() ;
  186. Assert.AreEqual( SystemColors.Control , r1.BackColor , "#84");
  187. r1.BackColor = System.Drawing.Color.Black ;
  188. r1.Refresh() ;
  189. Assert.AreEqual( null , r1.Region , "#85");
  190. Rectangle M = new Rectangle(10, 20, 30 ,40);
  191. r1.RectangleToScreen(M) ;
  192. Assert.AreEqual( null , r1.Region , "#86");
  193. }
  194. [Test]
  195. public void PubMethodTest8()
  196. {
  197. Label N = new Label();
  198. N.Left = 10;
  199. N.Top = 12;
  200. N.Visible = true;
  201. Point p = new Point (10,10);
  202. Point p1 = N.PointToScreen(p) ;
  203. Point p2 = N.PointToClient (p1);
  204. Assert.AreEqual (p, p2, "#1 converting client->screen->client should not loose data");
  205. }
  206. [Test]
  207. public void ContainsTest ()
  208. {
  209. Control t = new Control ();
  210. Control s = new Control ();
  211. t.Controls.Add (s);
  212. Assert.AreEqual (true, t.Contains (s), "#1 should contain");
  213. Assert.AreEqual (false, s.Contains (t), "#2 should not contain");
  214. Assert.AreEqual (false, s.Contains (null), "#3 should not contain");
  215. Assert.AreEqual (false, t.Contains (new Control ()), "#4 should not contain");
  216. }
  217. [Test]
  218. public void CreateHandleTest ()
  219. {
  220. Control parent = null, child = null;
  221. try {
  222. parent = new Control ();
  223. parent.Visible = true;
  224. child = new Control ();
  225. parent.Controls.Add (child);
  226. Assert.IsFalse (parent.IsHandleCreated, "#1 handle should not be created while ctor");
  227. Assert.IsFalse (child.IsHandleCreated, "#2 handle should not be created while ctor");
  228. parent.CreateControl ();
  229. Assert.IsNotNull (parent.Handle, "#3 should create a handle");
  230. Assert.IsNotNull (child.Handle, "#4 should create a handle");
  231. Assert.IsTrue (parent.IsHandleCreated, "#5 should have created handle");
  232. Assert.IsTrue (child.IsHandleCreated, "#6 should have created handle");
  233. } finally {
  234. if (parent != null)
  235. parent.Dispose ();
  236. if (child != null)
  237. child.Dispose ();
  238. }
  239. // Accessing Handle Property creates the handle
  240. try {
  241. parent = new Control ();
  242. parent.Visible = true;
  243. child = new Control ();
  244. parent.Controls.Add (child);
  245. Assert.IsFalse (parent.IsHandleCreated, "#7 handle is not created while ctor");
  246. Assert.IsFalse (child.IsHandleCreated, "#8 handle is not created while ctor");
  247. Assert.IsNotNull (parent.Handle, "#9 should create a handle");
  248. Assert.IsTrue (parent.IsHandleCreated, "#10 should have created handle");
  249. Assert.IsTrue (child.IsHandleCreated, "#11 should have created handle");
  250. } finally {
  251. if (parent != null)
  252. parent.Dispose ();
  253. if (child != null)
  254. child.Dispose ();
  255. }
  256. }
  257. [Test]
  258. public void CreateGraphicsTest ()
  259. {
  260. Graphics g = null;
  261. Pen p = null;
  262. try {
  263. Control c = new Control ();
  264. c.SetBounds (0,0, 20, 20);
  265. g = c.CreateGraphics ();
  266. Assert.IsNotNull (g, "#1 should create a graphics object");
  267. g.DrawLine (p = new Pen (Color.Red), 10, 10, 20, 20);
  268. } finally {
  269. if (p != null)
  270. p.Dispose ();
  271. if (g != null)
  272. g.Dispose ();
  273. }
  274. }
  275. bool delegateCalled = false;
  276. public delegate void TestDelegate ();
  277. [Test]
  278. public void InvokeTest ()
  279. {
  280. Control c = null;
  281. try {
  282. c = new Control ();
  283. IAsyncResult result;
  284. try {
  285. result = c.BeginInvoke (new TestDelegate (delegate_call));
  286. c.EndInvoke (result);
  287. Assert.Fail ("#1 should not invoke without window handle");
  288. } catch (InvalidOperationException) { }
  289. c.CreateControl ();
  290. result = c.BeginInvoke (new TestDelegate (delegate_call));
  291. c.EndInvoke (result);
  292. Assert.IsTrue (delegateCalled, "#1 value should have been set to true");
  293. } finally {
  294. if (c != null)
  295. c.Dispose ();
  296. }
  297. }
  298. public void delegate_call ()
  299. {
  300. delegateCalled = true;
  301. }
  302. [Test]
  303. public void FindFormTest ()
  304. {
  305. Form f = new Form ();
  306. f.Name = "form";
  307. Control c = null;
  308. try {
  309. f.Controls.Add (c = new Control ());
  310. Assert.AreEqual (f.Name, c.FindForm ().Name, "#1 should find the parent form");
  311. f.Controls.Remove (c);
  312. GroupBox g = new GroupBox ();
  313. g.Name = "box";
  314. f.Controls.Add (g);
  315. g.Controls.Add (c);
  316. Assert.AreEqual (f.Name, f.FindForm ().Name, "#2 still should find the form");
  317. g.Controls.Remove (c);
  318. Assert.IsNull(c.FindForm (), "#3 should be null");
  319. } finally {
  320. if (c != null)
  321. c.Dispose ();
  322. if (f != null)
  323. f.Dispose ();
  324. }
  325. }
  326. [Test]
  327. public void FocusTest ()
  328. {
  329. Form f = null;
  330. Button c = null, d = null;
  331. try {
  332. f = new Form ();
  333. f.Visible = true;
  334. c = new Button ();
  335. c.Visible = true;
  336. f.Controls.Add (c);
  337. d = new Button ();
  338. d.Visible = false;
  339. f.Controls.Add (d);
  340. Assert.IsTrue (c.CanFocus, "#1 button should be able to be focused");
  341. Assert.IsFalse (c.Focused, "#2 button should not be focussed initially");
  342. c.Focus ();
  343. Assert.IsTrue (c.Focused, "#3 button should be focussed after Focus ()");
  344. d.Focus ();
  345. Assert.IsFalse (d.Focused, "#4 invisible button should not be focussed");
  346. d.Visible = true;
  347. d.Focus ();
  348. Assert.IsTrue (d.Focused, "#5 button should be focussed after Focus () & visible");
  349. Assert.IsFalse (c.Focused, "#6 button should lost focus");
  350. c.Enabled = false;
  351. Assert.IsFalse (c.Focused, "#7 disabled button should not be focused");
  352. } finally {
  353. if (f != null)
  354. f.Dispose ();
  355. if (c != null)
  356. c.Dispose ();
  357. if (d != null)
  358. d.Dispose ();
  359. }
  360. }
  361. [Test]
  362. public void FromChildHandleTest ()
  363. {
  364. // FIXME : how to make a control to have more than one handle?
  365. Control c = null;
  366. try {
  367. c = new Control ();
  368. c.Name = "hello";
  369. IntPtr handle = c.Handle;
  370. Assert.AreEqual (c.Name, Control.FromChildHandle (handle).Name, "#1 handle should be able to relate to control");
  371. handle = IntPtr.Zero;
  372. Assert.IsNull (Control.FromChildHandle (handle), "#2 should return null");
  373. } finally {
  374. if (c != null)
  375. c.Dispose ();
  376. }
  377. }
  378. [Test]
  379. public void FromHandleTest ()
  380. {
  381. Control c = null;
  382. try {
  383. c = new Control ();
  384. c.Name = "hello";
  385. IntPtr handle = c.Handle;
  386. Assert.AreEqual (c.Name, Control.FromHandle (handle).Name, "#1 handle should be able to relate to control");
  387. handle = IntPtr.Zero;
  388. Assert.IsNull (Control.FromHandle (handle), "#2 should return null");
  389. } finally {
  390. if (c != null)
  391. c.Dispose ();
  392. }
  393. }
  394. [Test]
  395. public void GetChildAtPointTest ()
  396. {
  397. Control c = null, d = null, e = null;
  398. try {
  399. c = new Control ();
  400. c.Name = "c1";
  401. c.SetBounds (0, 0, 100, 100);
  402. d = new Control ();
  403. d.Name = "d1";
  404. d.SetBounds (10, 10, 40, 40);
  405. c.Controls.Add (d);
  406. e = new Control ();
  407. e.Name = "e1";
  408. e.SetBounds (55, 55, 10, 10);
  409. Control l = c.GetChildAtPoint (new Point (15, 15));
  410. Assert.AreEqual (d.Name, l.Name, "#1 should return the cild");
  411. Assert.IsFalse (e.Name == l.Name, "#2 e is not child of c");
  412. l = c.GetChildAtPoint (new Point (57, 57));
  413. Assert.IsNull (l, "#3 no control at 55");
  414. l = c.GetChildAtPoint (new Point (10, 10));
  415. Assert.AreEqual (d.Name, l.Name, "#4 should return the child even if it falls on border");
  416. /* net 2.0 overload
  417. #if NET_2_0
  418. c.Controls.Add (e);
  419. e.Visible = false;
  420. l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
  421. Assert.IsNull (l, "#4 should ignore of type invisible");
  422. e.Visible = true;
  423. l = c.GetChildAtPoint (new Point (57, 57), GetChildAtPointSkip.Invisible);
  424. Assert.AreSame (e.Name, l.Name, "#4 should show visible controls");
  425. #endif // NET_2_0
  426. */
  427. } finally {
  428. if (c != null)
  429. c.Dispose ();
  430. if (d != null)
  431. d.Dispose ();
  432. }
  433. }
  434. }