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