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
29 using System.Collections.Generic;
31 using NUnit.Framework;
32 using System.Windows.Forms;
33 using System.ComponentModel;
35 namespace MonoTests.System.Windows.Forms
39 public class BindingNavigatorTest : TestHelper
41 private bool flag = false;
42 private BindingNavigator navigator;
44 private void SetFlag(object a, EventArgs e)
50 protected override void SetUp ()
52 IntThing test = new IntThing(50);
53 BindingSource s = new BindingSource();
55 navigator = new BindingNavigator(s);
61 public void AddNewItemTest()
63 navigator.ItemAdded += new ToolStripItemEventHandler(SetFlag);
64 navigator.Items.Add("Test Item");
65 Assert.IsTrue(flag, "#1");
69 public void AddStandardItems()
71 BindingNavigator navigator = new BindingNavigator();
72 navigator.AddStandardItems();
73 CheckStandardItems(navigator);
77 public void BeginInitTest()
79 navigator.RefreshItems += new EventHandler(SetFlag);
80 navigator.ItemAdded += new ToolStripItemEventHandler(SetFlag);
81 navigator.Paint += new PaintEventHandler(SetFlag);
83 navigator.BeginInit();
85 navigator.Invalidate();
86 navigator.AddNewItem = new ToolStripButton();
89 Assert.IsFalse(flag, "#1");
91 Assert.IsTrue(flag, "#2");
95 public void BindingSourceTest()
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");
104 [Ignore("Bug in setting the textbox width breaks this test")]
105 public void Constructor()
107 BindingNavigator navigator = new BindingNavigator(true);
108 Assert.AreEqual(11, navigator.Items.Count, "count");
109 CheckStandardItems(navigator);
111 navigator = new BindingNavigator(false);
112 Assert.IsTrue(navigator.Items.Count == 0, "#01");
114 IntThing test = new IntThing(50);
115 BindingSource s = new BindingSource();
118 navigator = new BindingNavigator((BindingSource)null);
119 Assert.AreEqual(11, navigator.Items.Count, "#02");
121 Assert.AreEqual(50, ((ToolStripTextBox)navigator.PositionItem).TextBox.Width, "#03");
125 public void ControlDisposedTest()
127 ToolStripItem existing = navigator.AddNewItem;
128 navigator.AddNewItem = new ToolStripButton();
129 Assert.IsFalse(existing.IsDisposed, "#1");
133 private void CheckStandardItems(BindingNavigator navigator)
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");
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");
162 public void ManuallyReplaceItemsTest()
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");
173 public void OnRefreshItems()
175 navigator.RefreshItems += new EventHandler(SetFlag);
176 navigator.AddNewItem = new ToolStripButton();
177 Assert.IsTrue(flag, "#1");
181 [Ignore("Not working yet")]
182 public void PositionItemTest()
184 navigator.BindingSource.PositionChanged += new EventHandler(SetFlag);
186 int position = int.Parse(navigator.PositionItem.Text);
187 navigator.PositionItem.Text = "aaa";
188 Assert.IsFalse(flag, "#1");
190 Assert.AreEqual(position.ToString(), navigator.PositionItem.Text, "#2");
192 navigator.PositionItem.Text = "-1";
194 Assert.IsFalse(flag, "#3");
196 Assert.AreEqual("1", navigator.PositionItem.Text, "#4");
198 navigator.PositionItem.Text = "7";
200 Assert.IsFalse(flag, "#5");
201 Assert.AreEqual("1", navigator.PositionItem.Text, "#6");
202 Assert.AreEqual(0, navigator.BindingSource.Position, "#7");
206 public void RefreshItemsCore()
208 navigator.RefreshItems += new EventHandler(SetFlag);
209 navigator.AddNewItem = new ToolStripButton();
210 Assert.IsTrue(flag, "#1");
215 private void RefreshNav()
217 navigator.BeginInit();
222 [Ignore("Not working")]
223 public void RemoveItemTest()
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");
234 public void SetControlNullTest()
236 navigator.AddNewItem = null;
237 Assert.IsTrue(navigator.AddNewItem == null, "#1");
238 Assert.AreEqual(11, navigator.Items.Count, "#2");
242 private class IntThing : BindingList<int>
245 public IntThing(int number)
248 for (int i = 0; i < number; i++)
254 protected override bool SupportsSearchingCore
259 protected override int FindCore(PropertyDescriptor prop, object key)
261 return this.Items.IndexOf((int)key);