Merge pull request #82 from Unity-Technologies/master-gc-race
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / BindingNavigatorTest.cs
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 //      Olivier Dufour  olivier.duff@free.fr
24 //  Alan McGovern alan.mcgovern@gmail.com
25 //
26 #if NET_2_0
27
28 using System;
29 using System.Collections.Generic;
30 using System.Text;
31 using NUnit.Framework;
32 using System.Windows.Forms;
33 using System.ComponentModel;
34
35 namespace MonoTests.System.Windows.Forms
36 {
37
38         [TestFixture]
39         public class BindingNavigatorTest : TestHelper
40         {
41                 private bool flag = false;
42                 private BindingNavigator navigator;
43
44                 private void SetFlag(object a, EventArgs e)
45                 {
46                         flag = true;
47                 }
48
49                 [SetUp]
50                 protected override void SetUp ()
51                 {
52                         IntThing test = new IntThing(50);
53                         BindingSource s = new BindingSource();
54                         s.DataSource = test;
55                         navigator = new BindingNavigator(s);
56                         flag = false;
57                         base.SetUp ();
58                 }
59
60                 [Test]
61                 public void AddNewItemTest()
62                 {
63                         navigator.ItemAdded += new ToolStripItemEventHandler(SetFlag);
64                         navigator.Items.Add("Test Item");
65                         Assert.IsTrue(flag, "#1");
66                 }
67
68                 [Test]
69                 public void AddStandardItems()
70                 {
71                         BindingNavigator navigator = new BindingNavigator();
72                         navigator.AddStandardItems();
73                         CheckStandardItems(navigator);
74                 }
75
76                 [Test]
77                 public void BeginInitTest()
78                 {
79                         navigator.RefreshItems += new EventHandler(SetFlag);
80                         navigator.ItemAdded += new ToolStripItemEventHandler(SetFlag);
81                         navigator.Paint += new PaintEventHandler(SetFlag);
82
83                         navigator.BeginInit();
84
85                         navigator.Invalidate();
86                         navigator.AddNewItem = new ToolStripButton();
87                         navigator.Refresh();
88
89                         Assert.IsFalse(flag, "#1");
90                         navigator.EndInit();
91                         Assert.IsTrue(flag, "#2");
92                 }
93
94                 [Test]
95                 public void BindingSourceTest()
96                 {
97                         navigator.BindingSource.PositionChanged += new EventHandler(SetFlag);
98                         navigator.BindingSource.Position = 5;
99                         Assert.IsTrue(flag, "#1");
100                         Assert.AreEqual("6", navigator.PositionItem.Text, "#2");
101                 }
102
103                 [Test]
104                 [Ignore("Bug in setting the textbox width breaks this test")]
105                 public void Constructor()
106                 {
107                         BindingNavigator navigator = new BindingNavigator(true);
108                         Assert.AreEqual(11, navigator.Items.Count, "count");
109                         CheckStandardItems(navigator);
110
111                         navigator = new BindingNavigator(false);
112                         Assert.IsTrue(navigator.Items.Count == 0, "#01");
113
114                         IntThing test = new IntThing(50);
115                         BindingSource s = new BindingSource();
116                         s.DataSource = test;
117
118                         navigator = new BindingNavigator((BindingSource)null);
119                         Assert.AreEqual(11, navigator.Items.Count, "#02");
120
121                         Assert.AreEqual(50, ((ToolStripTextBox)navigator.PositionItem).TextBox.Width, "#03");
122                 }
123
124                 [Test]
125                 public void ControlDisposedTest()
126                 {
127                         ToolStripItem existing = navigator.AddNewItem;
128                         navigator.AddNewItem = new ToolStripButton();
129                         Assert.IsFalse(existing.IsDisposed, "#1");
130                 }
131
132
133                 private void CheckStandardItems(BindingNavigator navigator)
134                 {
135                         Assert.IsNotNull(navigator.AddNewItem, "*1");
136                         Assert.IsNotNull(navigator.MoveFirstItem, "*2");
137                         Assert.IsNotNull(navigator.MoveLastItem, "*3");
138                         Assert.IsNotNull(navigator.MoveNextItem, "*4");
139                         Assert.IsNotNull(navigator.MovePreviousItem, "*5");
140                         Assert.IsNotNull(navigator.DeleteItem, "*6");
141                         Assert.IsNotNull(navigator.CountItem, "*7");
142                         Assert.IsNotNull(navigator.PositionItem, "*8");
143                         Assert.IsNotNull(navigator.AddNewItem, "*9");
144                         Assert.IsNotNull(navigator.AddNewItem, "*10");
145                         Assert.IsNull(navigator.BindingSource, "*11");
146
147                         Assert.IsTrue(navigator.AddNewItem is ToolStripButton, "#1");
148                         Assert.IsTrue(navigator.MoveFirstItem is ToolStripButton, "#2");
149                         Assert.IsTrue(navigator.MoveLastItem is ToolStripButton, "#3");
150                         Assert.IsTrue(navigator.MoveNextItem is ToolStripButton, "#4");
151                         Assert.IsTrue(navigator.MovePreviousItem is ToolStripButton, "#5");
152                         Assert.IsTrue(navigator.DeleteItem is ToolStripButton, "#6");
153                         Assert.IsTrue(navigator.CountItem is ToolStripLabel, "#7");
154                         Assert.IsTrue(navigator.PositionItem is ToolStripTextBox, "#8");
155                         Assert.IsTrue(navigator.AddNewItem is ToolStripButton, "#9");
156                         Assert.IsTrue(navigator.AddNewItem is ToolStripButton, "#10");
157                         Assert.AreEqual("of {0}", navigator.CountItemFormat, "#11");
158                         Assert.AreEqual(11, navigator.Items.Count, "#12");
159                 }
160
161                 [Test]
162                 public void ManuallyReplaceItemsTest()
163                 {
164                         ToolStripButton newButton = new ToolStripButton();
165                         ToolStripItem oldItem = navigator.AddNewItem;
166                         navigator.AddNewItem = newButton;
167                         Assert.AreEqual(11, navigator.Items.Count, "#1");
168                         Assert.IsFalse(navigator.Items.Contains(newButton), "#2");
169                         Assert.IsTrue(navigator.Items.Contains(oldItem), "#3");
170                 }
171
172                 [Test]
173                 public void OnRefreshItems()
174                 {
175                         navigator.RefreshItems += new EventHandler(SetFlag);
176                         navigator.AddNewItem = new ToolStripButton();
177                         Assert.IsTrue(flag, "#1");
178                 }
179
180                 [Test]
181                 [Ignore("Not working yet")]
182                 public void PositionItemTest()
183                 {
184                         navigator.BindingSource.PositionChanged += new EventHandler(SetFlag);
185
186                         int position = int.Parse(navigator.PositionItem.Text);
187                         navigator.PositionItem.Text = "aaa";
188                         Assert.IsFalse(flag, "#1");
189                         RefreshNav();
190                         Assert.AreEqual(position.ToString(), navigator.PositionItem.Text, "#2");
191
192                         navigator.PositionItem.Text = "-1";
193                         RefreshNav();
194                         Assert.IsFalse(flag, "#3");
195                         flag = false;
196                         Assert.AreEqual("1", navigator.PositionItem.Text, "#4");
197
198                         navigator.PositionItem.Text = "7";
199                         RefreshNav();
200                         Assert.IsFalse(flag, "#5");
201                         Assert.AreEqual("1", navigator.PositionItem.Text, "#6");
202                         Assert.AreEqual(0, navigator.BindingSource.Position, "#7");
203                 }
204
205                 [Test]
206                 public void RefreshItemsCore()
207                 {
208                         navigator.RefreshItems += new EventHandler(SetFlag);
209                         navigator.AddNewItem = new ToolStripButton();
210                         Assert.IsTrue(flag, "#1");
211
212                 }
213
214
215                 private void RefreshNav()
216                 {
217                         navigator.BeginInit();
218                         navigator.EndInit();
219                 }
220
221                 [Test]
222                 [Ignore("Not working")]
223                 public void RemoveItemTest()
224                 {
225                         navigator.BindingSource.Position = 5;
226                         navigator.BindingSource.ListChanged += new ListChangedEventHandler(SetFlag);
227                         navigator.BindingSource.Remove(5);
228                         Assert.IsFalse(navigator.BindingSource.Contains(5), "#1");
229                         Assert.AreEqual("6", navigator.PositionItem.Text, "#2");
230                         Assert.AreEqual(6, (navigator.BindingSource.Current), "#3");
231                 }
232
233                 [Test]
234                 public void SetControlNullTest()
235                 {
236                         navigator.AddNewItem = null;
237                         Assert.IsTrue(navigator.AddNewItem == null, "#1");
238                         Assert.AreEqual(11, navigator.Items.Count, "#2");
239                 }
240
241
242                 private class IntThing : BindingList<int>
243                 {
244                         int Number;
245                         public IntThing(int number)
246                                 : base()
247                         {
248                                 for (int i = 0; i < number; i++)
249                                         this.Add(i);
250
251                                 number = 6;
252                         }
253
254                         protected override bool SupportsSearchingCore
255                         {
256                                 get { return true; }
257                         }
258                         
259                         protected override int FindCore(PropertyDescriptor prop, object key)
260                         {
261                                 return this.Items.IndexOf((int)key);
262                                 return -1;
263                         }
264                 }
265         }
266 }
267 #endif