BindingTest.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2006 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jackson Harper [email protected]
  24. using System;
  25. using System.Data;
  26. using System.Collections;
  27. using System.Windows.Forms;
  28. using NUnit.Framework;
  29. namespace MonoTests.System.Windows.Forms.DataBinding {
  30. [TestFixture]
  31. public class BindingTest {
  32. [SetUp]
  33. protected virtual void SetUp ()
  34. {
  35. }
  36. [TearDown]
  37. protected virtual void TearDown ()
  38. {
  39. }
  40. [Test]
  41. public void CtorTest ()
  42. {
  43. string prop = "PROPERTY NAME";
  44. object data_source = new object ();
  45. string data_member = "DATA MEMBER";
  46. Binding b = new Binding (prop, data_source, data_member);
  47. Assert.IsNull (b.BindingManagerBase, "ctor1");
  48. Console.WriteLine ("MEMBER INFO: " + b.BindingMemberInfo);
  49. Assert.IsNotNull (b.BindingMemberInfo, "ctor2");
  50. Assert.IsNull (b.Control, "ctor3");
  51. Assert.IsFalse (b.IsBinding, "ctor4");
  52. Assert.AreSame (b.PropertyName, prop, "ctor5");
  53. Assert.AreSame (b.DataSource, data_source, "ctor6");
  54. }
  55. [Test]
  56. public void CtorNullTest ()
  57. {
  58. Binding b = new Binding (null, null, null);
  59. Assert.IsNull (b.PropertyName, "ctornull1");
  60. Assert.IsNull (b.DataSource, "ctornull2");
  61. }
  62. // XXX this belongs in a ControlBindingsCollectionTest
  63. // file.
  64. [Test]
  65. [ExpectedException (typeof (ArgumentException))] // MS: "This would cause two bindings in the collection to bind to the same property."
  66. public void DuplicateBindingAdd ()
  67. {
  68. Control c1 = new Control ();
  69. Control c2 = new Control ();
  70. c2.DataBindings.Add ("Text", c1, "Text");
  71. c2.DataBindings.Add ("Text", c1, "Text");
  72. }
  73. [Test]
  74. [Category ("NotWorking")]
  75. public void BindingManagerBaseTest ()
  76. {
  77. Control c1 = new Control ();
  78. Control c2 = new Control ();
  79. Binding binding;
  80. c1.BindingContext = new BindingContext ();
  81. c2.BindingContext = c1.BindingContext;
  82. binding = c2.DataBindings.Add ("Text", c1, "Text");
  83. Assert.IsNull (binding.BindingManagerBase, "1");
  84. c1.CreateControl ();
  85. c2.CreateControl ();
  86. Assert.IsNull (binding.BindingManagerBase, "2");
  87. c2.DataBindings.Remove (binding);
  88. binding = c2.DataBindings.Add ("Text", c1, "Text");
  89. Assert.IsTrue (binding.BindingManagerBase != null, "3");
  90. }
  91. [Test]
  92. /* create control and set binding context */
  93. public void BindingContextChangedTest ()
  94. {
  95. Control c = new Control ();
  96. // Test BindingContextChanged Event
  97. c.BindingContextChanged += new EventHandler (Event_Handler1);
  98. BindingContext bcG1 = new BindingContext ();
  99. eventcount = 0;
  100. c.BindingContext = bcG1;
  101. Assert.AreEqual (1, eventcount, "A1");
  102. }
  103. [Test]
  104. /* create control and show control */
  105. public void BindingContextChangedTest2 ()
  106. {
  107. Form f = new Form ();
  108. f.ShowInTaskbar = false;
  109. Control c = new Control ();
  110. f.Controls.Add (c);
  111. c.BindingContextChanged += new EventHandler (Event_Handler1);
  112. eventcount = 0;
  113. f.Show ();
  114. #if NET_2_0
  115. Assert.AreEqual (1, eventcount, "A1");
  116. #else
  117. Assert.AreEqual (2, eventcount, "A1");
  118. #endif
  119. f.Dispose();
  120. }
  121. [Test]
  122. /* create control, set binding context, and show control */
  123. public void BindingContextChangedTest3 ()
  124. {
  125. Form f = new Form ();
  126. f.ShowInTaskbar = false;
  127. Control c = new Control ();
  128. f.Controls.Add (c);
  129. c.BindingContextChanged += new EventHandler (Event_Handler1);
  130. eventcount = 0;
  131. c.BindingContext = new BindingContext ();;
  132. f.Show ();
  133. Assert.AreEqual (1, eventcount, "A1");
  134. f.Dispose ();
  135. }
  136. [Test]
  137. public void BindingContextChangedTest4 ()
  138. {
  139. Form f = new Form ();
  140. f.ShowInTaskbar = false;
  141. ContainerControl cc = new ContainerControl ();
  142. Control c = new Control ();
  143. f.Controls.Add (cc);
  144. cc.Controls.Add (c);
  145. c.BindingContextChanged += new EventHandler (Event_Handler1);
  146. cc.BindingContextChanged += new EventHandler (Event_Handler1);
  147. f.BindingContextChanged += new EventHandler (Event_Handler1);
  148. eventcount = 0;
  149. Console.WriteLine (">>>");
  150. f.Show ();
  151. Console.WriteLine ("<<<");
  152. #if NET_2_0
  153. Assert.AreEqual (5, eventcount, "A1");
  154. #else
  155. Assert.AreEqual (8, eventcount, "A1");
  156. #endif
  157. f.Dispose ();
  158. }
  159. int eventcount;
  160. public void Event_Handler1 (object sender, EventArgs e)
  161. {
  162. //Console.WriteLine (sender.GetType());
  163. //Console.WriteLine (Environment.StackTrace);
  164. eventcount++;
  165. }
  166. [Test]
  167. public void DataBindingCountTest1 ()
  168. {
  169. Control c = new Control ();
  170. Assert.AreEqual (0, c.DataBindings.Count, "1");
  171. c.DataBindings.Add (new Binding ("Text", c, "Name"));
  172. Assert.AreEqual (1, c.DataBindings.Count, "2");
  173. Binding b = c.DataBindings[0];
  174. Assert.AreEqual (c, b.Control, "3");
  175. Assert.AreEqual (c, b.DataSource, "4");
  176. Assert.AreEqual ("Text", b.PropertyName, "5");
  177. Assert.AreEqual ("Name", b.BindingMemberInfo.BindingField, "6");
  178. }
  179. [Test]
  180. public void DataBindingCountTest2 ()
  181. {
  182. Control c = new Control ();
  183. Control c2 = new Control ();
  184. Assert.AreEqual (0, c.DataBindings.Count, "1");
  185. c.DataBindings.Add (new Binding ("Text", c2, "Name"));
  186. Assert.AreEqual (1, c.DataBindings.Count, "2");
  187. Assert.AreEqual (0, c2.DataBindings.Count, "3");
  188. Binding b = c.DataBindings[0];
  189. Assert.AreEqual (c, b.Control, "4");
  190. Assert.AreEqual (c2, b.DataSource, "5");
  191. Assert.AreEqual ("Text", b.PropertyName, "6");
  192. Assert.AreEqual ("Name", b.BindingMemberInfo.BindingField, "7");
  193. }
  194. }
  195. }