UserControlTest.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // UserControlTest.cs
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. // Copyright (c) 2006 Daniel Nauck
  24. //
  25. // Authors:
  26. // Daniel Nauck (dna(at)mono-project(dot)de)
  27. using System;
  28. using System.Windows.Forms;
  29. using System.Drawing;
  30. using System.Reflection;
  31. using NUnit.Framework;
  32. using System.Collections;
  33. using System.ComponentModel;
  34. namespace MonoTests.System.Windows.Forms
  35. {
  36. [TestFixture]
  37. public class UserControlTest : TestHelper
  38. {
  39. UserControl uc = null;
  40. [SetUp]
  41. protected override void SetUp () {
  42. uc = new UserControl();
  43. base.SetUp ();
  44. }
  45. [Test]
  46. public void PropertyTest()
  47. {
  48. Assert.AreEqual(string.Empty, uc.Text, "#A1");
  49. #if NET_2_0
  50. Assert.AreEqual(BorderStyle.None, uc.BorderStyle, "#A2");
  51. uc.BorderStyle = BorderStyle.Fixed3D;
  52. Assert.AreEqual(BorderStyle.Fixed3D, uc.BorderStyle, "#A3");
  53. uc.BorderStyle = BorderStyle.FixedSingle;
  54. Assert.AreEqual(BorderStyle.FixedSingle, uc.BorderStyle, "#A4");
  55. uc.BorderStyle = BorderStyle.None;
  56. Assert.AreEqual(BorderStyle.None, uc.BorderStyle, "#A5");
  57. #endif
  58. }
  59. #if NET_2_0
  60. [Test]
  61. [ExpectedException(typeof(InvalidEnumArgumentException))]
  62. public void BorderStyleInvalidEnumArgumentException()
  63. {
  64. uc.BorderStyle = (BorderStyle) 9999;
  65. }
  66. [Test]
  67. public void MethodCreateParams ()
  68. {
  69. ExposeProtectedProperties uc = new ExposeProtectedProperties ();
  70. Assert.AreEqual (WindowStyles.WS_TILED | WindowStyles.WS_MAXIMIZEBOX | WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_VISIBLE | WindowStyles.WS_CHILD, (WindowStyles)uc.CreateParams.Style, "D1");
  71. Assert.AreEqual (WindowExStyles.WS_EX_CONTROLPARENT, (WindowExStyles)uc.CreateParams.ExStyle, "D2");
  72. }
  73. private class ExposeProtectedProperties : UserControl
  74. {
  75. public new CreateParams CreateParams { get { return base.CreateParams; } }
  76. }
  77. [Test]
  78. public void AutoSize ()
  79. {
  80. Form f = new Form ();
  81. f.ShowInTaskbar = false;
  82. Panel p = new Panel ();
  83. p.AutoSize = true;
  84. f.Controls.Add (p);
  85. Button b = new Button ();
  86. b.Size = new Size (200, 200);
  87. b.Location = new Point (200, 200);
  88. p.Controls.Add (b);
  89. f.Show ();
  90. Assert.AreEqual (new Size (403, 403), p.ClientSize, "A1");
  91. p.Controls.Remove (b);
  92. Assert.AreEqual (new Size (200, 100), p.ClientSize, "A2");
  93. p.AutoSizeMode = AutoSizeMode.GrowAndShrink;
  94. Assert.AreEqual (new Size (0, 0), p.ClientSize, "A3");
  95. f.Close ();
  96. }
  97. [Test]
  98. public void PreferredSize ()
  99. {
  100. Form f = new Form ();
  101. f.ShowInTaskbar = false;
  102. UserControl p = new UserControl ();
  103. f.Controls.Add (p);
  104. Button b1 = new Button ();
  105. b1.Size = new Size (200, 200);
  106. b1.Dock = DockStyle.Fill;
  107. p.Controls.Add (b1);
  108. Button b = new Button ();
  109. b.Size = new Size (100, 100);
  110. b.Dock = DockStyle.Top;
  111. p.Controls.Add (b);
  112. f.Show ();
  113. Assert.AreEqual (new Size (0, 100), p.PreferredSize, "A1");
  114. b1.Dock = DockStyle.Left;
  115. Assert.AreEqual (new Size (200, 100), p.PreferredSize, "A2");
  116. b1.Dock = DockStyle.None;
  117. Assert.AreEqual (new Size (203, 203), p.PreferredSize, "A3");
  118. b1.Dock = DockStyle.Fill;
  119. b.Dock = DockStyle.Fill;
  120. Assert.AreEqual (new Size (0, 0), p.PreferredSize, "A4");
  121. b1.Dock = DockStyle.Top;
  122. b.Dock = DockStyle.Left;
  123. Assert.AreEqual (new Size (100, 200), p.PreferredSize, "A5");
  124. Button b2 = new Button ();
  125. b2.Size = new Size (50, 50);
  126. p.Controls.Add (b2);
  127. Assert.AreEqual (new Size (100, 200), p.PreferredSize, "A6");
  128. b2.Left = 300;
  129. Assert.AreEqual (new Size (353, 200), p.PreferredSize, "A7");
  130. b2.Top = 300;
  131. Assert.AreEqual (new Size (353, 353), p.PreferredSize, "A8");
  132. b2.Anchor = AnchorStyles.Bottom;
  133. Assert.AreEqual (new Size (100, 200), p.PreferredSize, "A9");
  134. b2.Anchor = AnchorStyles.Left;
  135. Assert.AreEqual (new Size (353, 353), p.PreferredSize, "A10");
  136. f.Dispose ();
  137. }
  138. #endif
  139. }
  140. }