BindingTest.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 {
  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. [Test]
  63. /* create control and set binding context */
  64. public void BindingContextChangedTest ()
  65. {
  66. Control c = new Control ();
  67. // Test BindingContextChanged Event
  68. c.BindingContextChanged += new EventHandler (Event_Handler1);
  69. BindingContext bcG1 = new BindingContext ();
  70. eventcount = 0;
  71. c.BindingContext = bcG1;
  72. Assert.AreEqual (1, eventcount, "A1");
  73. }
  74. [Test]
  75. [Category ("NotWorking")]
  76. /* create control and show control */
  77. public void BindingContextChangedTest2 ()
  78. {
  79. Form f = new Form ();
  80. f.ShowInTaskbar = false;
  81. Control c = new Control ();
  82. f.Controls.Add (c);
  83. c.BindingContextChanged += new EventHandler (Event_Handler1);
  84. eventcount = 0;
  85. f.Show ();
  86. #if NET_2_0
  87. Assert.AreEqual (1, eventcount, "A1");
  88. #else
  89. Assert.AreEqual (2, eventcount, "A1");
  90. #endif
  91. f.Dispose();
  92. }
  93. [Test]
  94. /* create control, set binding context, and show control */
  95. public void BindingContextChangedTest3 ()
  96. {
  97. Form f = new Form ();
  98. f.ShowInTaskbar = false;
  99. Control c = new Control ();
  100. f.Controls.Add (c);
  101. c.BindingContextChanged += new EventHandler (Event_Handler1);
  102. eventcount = 0;
  103. c.BindingContext = new BindingContext ();;
  104. f.Show ();
  105. Assert.AreEqual (1, eventcount, "A1");
  106. f.Dispose ();
  107. }
  108. int eventcount;
  109. public void Event_Handler1 (object sender, EventArgs e)
  110. {
  111. Console.WriteLine (Environment.StackTrace);
  112. eventcount++;
  113. }
  114. [Test]
  115. public void DataBindingCountTest1 ()
  116. {
  117. Control c = new Control ();
  118. Assert.AreEqual (0, c.DataBindings.Count, "1");
  119. c.DataBindings.Add (new Binding ("Text", c, "Name"));
  120. Assert.AreEqual (1, c.DataBindings.Count, "2");
  121. Binding b = c.DataBindings[0];
  122. Assert.AreEqual (c, b.Control, "3");
  123. Assert.AreEqual (c, b.DataSource, "4");
  124. Assert.AreEqual ("Text", b.PropertyName, "5");
  125. Assert.AreEqual ("Name", b.BindingMemberInfo.BindingField, "6");
  126. }
  127. [Test]
  128. public void DataBindingCountTest2 ()
  129. {
  130. Control c = new Control ();
  131. Control c2 = new Control ();
  132. Assert.AreEqual (0, c.DataBindings.Count, "1");
  133. c.DataBindings.Add (new Binding ("Text", c2, "Name"));
  134. Assert.AreEqual (1, c.DataBindings.Count, "2");
  135. Assert.AreEqual (0, c2.DataBindings.Count, "3");
  136. Binding b = c.DataBindings[0];
  137. Assert.AreEqual (c, b.Control, "4");
  138. Assert.AreEqual (c2, b.DataSource, "5");
  139. Assert.AreEqual ("Text", b.PropertyName, "6");
  140. Assert.AreEqual ("Name", b.BindingMemberInfo.BindingField, "7");
  141. }
  142. }
  143. }