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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2006 Novell, Inc.
23 // Olivier Dufour olivier.duff@free.fr
24 // Alan McGovern alan.mcgovern@gmail.com
28 using System.Collections.Generic;
30 using NUnit.Framework;
31 using System.Windows.Forms;
32 using System.ComponentModel;
34 namespace MonoTests.System.Windows.Forms
38 public class BindingNavigatorTest : TestHelper
40 private bool flag = false;
41 private BindingNavigator navigator;
43 private void SetFlag(object a, EventArgs e)
49 protected override void SetUp ()
51 IntThing test = new IntThing(50);
52 BindingSource s = new BindingSource();
54 navigator = new BindingNavigator(s);
60 public void AddNewItemTest()
62 navigator.ItemAdded += new ToolStripItemEventHandler(SetFlag);
63 navigator.Items.Add("Test Item");
64 Assert.IsTrue(flag, "#1");
68 public void AddStandardItems()
70 BindingNavigator navigator = new BindingNavigator();
71 navigator.AddStandardItems();
72 CheckStandardItems(navigator);
76 public void BeginInitTest()
78 navigator.RefreshItems += new EventHandler(SetFlag);
79 navigator.ItemAdded += new ToolStripItemEventHandler(SetFlag);
80 navigator.Paint += new PaintEventHandler(SetFlag);
82 navigator.BeginInit();
84 navigator.Invalidate();
85 navigator.AddNewItem = new ToolStripButton();
88 Assert.IsFalse(flag, "#1");
90 Assert.IsTrue(flag, "#2");
94 public void BindingSourceTest()
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");
103 [Ignore("Bug in setting the textbox width breaks this test")]
104 public void Constructor()
106 BindingNavigator navigator = new BindingNavigator(true);
107 Assert.AreEqual(11, navigator.Items.Count, "count");
108 CheckStandardItems(navigator);
110 navigator = new BindingNavigator(false);
111 Assert.IsTrue(navigator.Items.Count == 0, "#01");
113 IntThing test = new IntThing(50);
114 BindingSource s = new BindingSource();
117 navigator = new BindingNavigator((BindingSource)null);
118 Assert.AreEqual(11, navigator.Items.Count, "#02");
120 Assert.AreEqual(50, ((ToolStripTextBox)navigator.PositionItem).TextBox.Width, "#03");
124 public void ControlDisposedTest()
126 ToolStripItem existing = navigator.AddNewItem;
127 navigator.AddNewItem = new ToolStripButton();
128 Assert.IsFalse(existing.IsDisposed, "#1");
132 private void CheckStandardItems(BindingNavigator navigator)
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");
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");
161 public void ManuallyReplaceItemsTest()
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");
172 public void OnRefreshItems()
174 navigator.RefreshItems += new EventHandler(SetFlag);
175 navigator.AddNewItem = new ToolStripButton();
176 Assert.IsTrue(flag, "#1");
180 [Ignore("Not working yet")]
181 public void PositionItemTest()
183 navigator.BindingSource.PositionChanged += new EventHandler(SetFlag);
185 int position = int.Parse(navigator.PositionItem.Text);
186 navigator.PositionItem.Text = "aaa";
187 Assert.IsFalse(flag, "#1");
189 Assert.AreEqual(position.ToString(), navigator.PositionItem.Text, "#2");
191 navigator.PositionItem.Text = "-1";
193 Assert.IsFalse(flag, "#3");
195 Assert.AreEqual("1", navigator.PositionItem.Text, "#4");
197 navigator.PositionItem.Text = "7";
199 Assert.IsFalse(flag, "#5");
200 Assert.AreEqual("1", navigator.PositionItem.Text, "#6");
201 Assert.AreEqual(0, navigator.BindingSource.Position, "#7");
205 public void RefreshItemsCore()
207 navigator.RefreshItems += new EventHandler(SetFlag);
208 navigator.AddNewItem = new ToolStripButton();
209 Assert.IsTrue(flag, "#1");
214 private void RefreshNav()
216 navigator.BeginInit();
221 [Ignore("Not working")]
222 public void RemoveItemTest()
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");
233 public void SetControlNullTest()
235 navigator.AddNewItem = null;
236 Assert.IsTrue(navigator.AddNewItem == null, "#1");
237 Assert.AreEqual(11, navigator.Items.Count, "#2");
241 private class IntThing : BindingList<int>
244 public IntThing(int number)
247 for (int i = 0; i < number; i++)
253 protected override bool SupportsSearchingCore
258 protected override int FindCore(PropertyDescriptor prop, object key)
260 return this.Items.IndexOf((int)key);