2 // Tests for System.Web.UI.WebControls.Style.cs
5 // Peter Dennis Bartok (pbartok@novell.com)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using NUnit.Framework;
33 using System.Collections;
37 using System.Globalization;
40 using System.Web.UI.WebControls;
42 namespace MonoTests.System.Web.UI.WebControls
45 public class DropDownListTest {
46 public class NamingContainer : WebControl, INamingContainer {
50 public class DropDownListTestClass : DropDownList {
52 public DropDownListTestClass ()
56 public StateBag StateBag {
57 get { return base.ViewState; }
60 public string Render () {
61 HtmlTextWriter writer;
63 writer = DropDownListTest.GetWriter();
65 return writer.InnerWriter.ToString ();
68 public bool IsTrackingVS () {
69 return IsTrackingViewState;
72 public void SetTrackingVS () {
76 public object Save() {
77 return base.SaveViewState();
80 public void Load(object o) {
81 base.LoadViewState(o);
84 public new void RenderContents(HtmlTextWriter writer) {
85 base.RenderContents(writer);
88 public new void CreateControlCollection() {
89 base.CreateControlCollection();
92 public new void AddAttributesToRender(HtmlTextWriter writer) {
93 base.AddAttributesToRender(writer);
96 public string[] KeyValuePairs() {
101 e = ViewState.GetEnumerator();
102 result = new string[ViewState.Keys.Count];
105 while (e.MoveNext()) {
109 d = (DictionaryEntry)e.Current;
110 si = (StateItem)d.Value;
112 if (si.Value is String[]) {
115 values = (string[]) si.Value;
116 result[item] = d.Key.ToString() + "=";
117 if (values.Length > 0) {
118 result[item] += values[0];
120 for (int i = 1; i < values.Length; i++) {
121 result[item] += ", " + values[i];
125 result[item] = d.Key.ToString() + "=" + si.Value;
134 private static HtmlTextWriter GetWriter () {
135 StringWriter sw = new StringWriter ();
137 return new HtmlTextWriter (sw);
140 private bool IsEqual(object[] a1, object[] a2, string assertion) {
144 if (a1.Length != a2.Length) {
145 if (assertion != null) {
146 Assert.Fail(assertion + "( different length )");
152 notfound = new bool[a1.Length];
154 for (int i = 0; i < a1.Length; i++) {
155 for (int j = 0; j < a2.Length; j++) {
156 if (a1[i].Equals(a2[j])) {
161 if ((assertion != null) && (matches != i+1)) {
162 Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
166 return matches == a1.Length;
170 public void DropDownList_Defaults ()
172 DropDownListTestClass d = new DropDownListTestClass ();
174 Assert.AreEqual (Color.Empty, d.BackColor, "D1");
175 Assert.AreEqual (Color.Empty, d.BorderColor, "D2");
176 Assert.AreEqual (BorderStyle.NotSet, d.BorderStyle, "D3");
177 Assert.AreEqual (Unit.Empty, d.BorderWidth, "D4");
178 Assert.AreEqual (-1, d.SelectedIndex, "D5");
179 Assert.AreEqual (string.Empty, d.ToolTip, "D6");
180 Assert.AreEqual (0, d.Items.Count, "D7");
184 public void DropDownListBasic () {
185 DropDownListTestClass d = new DropDownListTestClass ();
187 Assert.AreEqual ("<select>\n\n</select>", d.Render (), "B1");
189 Assert.AreEqual("<select name>\n\n</select>", d.Render(), "B1");
192 Assert.AreEqual("<select name=\"blah\" id=\"blah\">\n\n</select>", d.Render(), "B2");
194 Assert.AreEqual(false, d.IsTrackingVS(), "B3");
196 Assert.AreEqual(true, d.IsTrackingVS(), "B4");
198 d.Items.Add(new ListItem("text1", "value1"));
199 Assert.AreEqual(1, d.Items.Count, "B5");
200 d.Items.Add(new ListItem("text2", "value2"));
201 Assert.AreEqual(2, d.Items.Count, "B6");
204 Assert.AreEqual("<select name=\"blah\" id=\"blah\">\n\t<option value=\"value1\">text1</option>\n\t<option selected=\"selected\" value=\"value2\">text2</option>\n\n</select>", d.Render(), "B7");
208 public void DropDownListProperties () {
209 DropDownListTestClass d;
211 d = new DropDownListTestClass ();
213 Assert.AreEqual(Color.Empty, d.BorderColor, "P1");
214 d.BorderColor = Color.Red;
215 Assert.AreEqual(Color.Red, d.BorderColor, "P2");
217 Assert.AreEqual(BorderStyle.NotSet, d.BorderStyle, "P3");
218 d.BorderStyle = BorderStyle.Dotted;
219 Assert.AreEqual(BorderStyle.Dotted, d.BorderStyle, "P4");
221 Assert.AreEqual(Unit.Empty, d.BorderWidth, "P5");
222 d.BorderWidth = new Unit(1);
223 Assert.AreEqual("1px", d.BorderWidth.ToString(), "P6");
225 Assert.AreEqual(-1, d.SelectedIndex, "P7");
226 d.Items.Add(new ListItem("text1", "value1"));
227 d.Items.Add(new ListItem("text2", "value2"));
229 Assert.AreEqual(1, d.SelectedIndex, "P8");
231 Assert.AreEqual(string.Empty, d.ToolTip, "P9");
234 Assert.AreEqual ("blah", d.ToolTip, "P10");
236 Assert.AreEqual(string.Empty, d.ToolTip, "P10");
241 [ExpectedException(typeof(HttpException))]
242 public void DropDownListDoubleSelectCheck () {
243 DropDownListTestClass d;
245 d = new DropDownListTestClass ();
246 d.Items.Add(new ListItem("text1", "value1"));
247 d.Items.Add(new ListItem("text2", "value2"));
249 Assert.AreEqual(1, d.SelectedIndex, "DS1");
250 d.Items[0].Selected = true;
251 d.Items[1].Selected = true;
252 Assert.AreEqual("<select name>\n\t<option selected=\"selected\" value=\"value1\">text1</option>\n\t<option selected=\"selected\" value=\"value2\">text2</option>\n\n</select>", d.Render(), "DS1");
256 [ExpectedException(typeof(ArgumentOutOfRangeException))]
257 public void DropDownListBorderStyleCheck () {
258 DropDownListTestClass d;
260 d = new DropDownListTestClass ();
261 d.BorderStyle = (BorderStyle)Int32.MinValue;
265 public void DropDownListSelectedCheck () {
266 DropDownListTestClass d;
268 d = new DropDownListTestClass ();
269 d.SelectedIndex = 2; // No exception thrown!!!
270 Assert.AreEqual(-1, d.SelectedIndex, "S1");
275 [ExpectedException(typeof(NullReferenceException))]
277 public void DropDownNullWriterTest () {
278 DropDownListTestClass d;
280 d = new DropDownListTestClass ();
281 d.AddAttributesToRender(null);
285 public void DropDownListNull () {
286 DropDownListTestClass d;
288 d = new DropDownListTestClass ();
290 // We want to surve the next two calls
291 d.RenderContents(null);
296 public void HtmlWriter () {
297 HtmlTextWriter writer;
299 writer = DropDownListTest.GetWriter();
300 writer.RenderBeginTag(HtmlTextWriterTag.Select);
301 writer.AddAttribute(HtmlTextWriterAttribute.Value, "MeValue", true);
302 writer.RenderBeginTag(HtmlTextWriterTag.Option);
303 writer.Write("MeText");
304 writer.RenderEndTag();
305 writer.RenderEndTag();
306 Assert.AreEqual("<select>\n\t<option value=\"MeValue\">\n\t\tMeText\n\t</option>\n</select>", writer.InnerWriter.ToString(), "H1");
311 public void DropDownNamingTest () {
312 NamingContainer container = new NamingContainer ();
313 DropDownListTestClass child = new DropDownListTestClass ();
315 Assert.AreEqual ("<select>\n\n</select>", child.Render (), "N1");
317 Assert.AreEqual ("<select name>\n\n</select>", child.Render (), "N1");
319 container.Controls.Add (child);
320 // don't assume the generated id
321 string s = child.Render ();
322 Assert.IsTrue (s.StartsWith ("<select name=\""), "N2a");
323 Assert.IsTrue (s.EndsWith ("\">\n\n</select>"), "N2b");
325 container.ID = "naming";
327 Assert.IsTrue (s.StartsWith ("<select name=\"naming"), "N3a");
328 Assert.IsTrue (s.EndsWith ("\">\n\n</select>"), "N3b");
332 Assert.IsTrue (s.StartsWith ("<select name=\"naming"), "N4a");
333 Assert.IsTrue (s.EndsWith ("fooid\" id=\"naming_fooid\">\n\n</select>"), "N4b");
337 public void InitialSelectionMade ()
339 DropDownList ddl = new DropDownList ();
343 Assert.IsNotNull (ddl.SelectedItem, "need a selected item");
344 Assert.AreEqual ("a", ddl.SelectedItem.Text);
347 DataSet GetExampleData ()
349 DataSet ds = new DataSet ();
350 ds.ReadXml (new StringReader (@"
352 <Stocks Company='Novell Inc.' Symbol='NOVL' Price='6.14' />
353 <Stocks Company='Microsoft Corp.' Symbol='MSFT' Price='25.92' />
354 <Stocks Company='Google' Symbol='GOOG' Price='291.60' />
362 public void TestValueFieldAndTextFormat ()
364 DropDownListTestClass ddl = new DropDownListTestClass ();
365 ddl.DataSource = GetExampleData ();
366 ddl.DataValueField = "Company";
367 ddl.DataTextFormatString = "This shouldn't show up = {0}";
370 string exp = @"<select>
371 <option value=""Novell Inc."">Novell Inc.</option>
372 <option value=""Microsoft Corp."">Microsoft Corp.</option>
373 <option value=""Google"">Google</option>
377 string exp = @"<select name>
378 <option value=""Novell Inc."">Novell Inc.</option>
379 <option value=""Microsoft Corp."">Microsoft Corp.</option>
380 <option value=""Google"">Google</option>
384 Assert.AreEqual (exp, ddl.Render ());