3 // Marek Habersack <mhabersack@novell.com>
5 // Copyright (C) 2010 Novell, Inc (http://novell.com)
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 using System.Collections.Generic;
27 using System.ComponentModel;
28 using System.Reflection;
31 using System.Web.UI.WebControls;
33 using NUnit.Framework;
34 namespace MonoTests.System.Web.UI.WebControls
37 public class MenuItemBindingTest
39 const string TO_STRING_EMPTY_VALUE = "(Empty)";
41 static readonly SortedDictionary <string, string> toStringValues = new SortedDictionary<string, string> (StringComparer.OrdinalIgnoreCase) {
42 {"DataMember", "value"},
43 {"Depth", TO_STRING_EMPTY_VALUE},
44 {"Enabled", TO_STRING_EMPTY_VALUE},
45 {"EnabledField", TO_STRING_EMPTY_VALUE},
46 {"FormatString", TO_STRING_EMPTY_VALUE},
47 {"ImageUrl", TO_STRING_EMPTY_VALUE},
48 {"ImageUrlField", TO_STRING_EMPTY_VALUE},
49 {"NavigateUrl", TO_STRING_EMPTY_VALUE},
50 {"Selectable", TO_STRING_EMPTY_VALUE},
51 {"SelectableField", TO_STRING_EMPTY_VALUE},
52 {"Target", TO_STRING_EMPTY_VALUE},
53 {"TargetField", TO_STRING_EMPTY_VALUE},
54 {"Text", TO_STRING_EMPTY_VALUE},
55 {"TextField", TO_STRING_EMPTY_VALUE},
56 {"ToolTip", TO_STRING_EMPTY_VALUE},
57 {"ToolTipField", TO_STRING_EMPTY_VALUE},
58 {"Value", TO_STRING_EMPTY_VALUE},
59 {"ValueField", TO_STRING_EMPTY_VALUE},
60 {"PopOutImageUrl", TO_STRING_EMPTY_VALUE},
61 {"PopOutImageUrlField", TO_STRING_EMPTY_VALUE},
62 {"SeparatorImageUrl", TO_STRING_EMPTY_VALUE},
63 {"SeparatorImageUrlField", TO_STRING_EMPTY_VALUE}
67 public void Test_ToString ()
69 var mib = new MenuItemBinding ();
71 Assert.AreEqual ("(Empty)", mib.ToString (), "#A1");
72 foreach (var entry in toStringValues)
73 ToStringTestProperty (entry.Key, entry.Value);
76 void ToStringTestProperty (string propertyName, string expectedValue)
78 PropertyInfo pi = typeof (MenuItemBinding).GetProperty (propertyName, BindingFlags.Instance | BindingFlags.Public);
80 Assert.Fail ("Property '{0}' not found.", propertyName);
82 object defaultValue = null;
83 object[] attrs = pi.GetCustomAttributes (typeof (DefaultValueAttribute), false);
84 Type t = pi.PropertyType;
85 if (attrs != null && attrs.Length > 0) {
86 var dva = attrs [0] as DefaultValueAttribute;
87 defaultValue = dva.Value;
89 if (t == typeof (string))
90 defaultValue = String.Empty;
91 else if (t == typeof (bool))
93 else if (t == typeof (int))
94 defaultValue = Int32.MaxValue;
96 Assert.Fail ("Unsupported return type '{0}' for property '{1}'", t.FullName, propertyName);
99 object setToValue = null;
100 if (t == typeof (string)) {
101 string v = defaultValue as String;
102 if (v == String.Empty || v != "value")
103 setToValue = "value";
105 setToValue = "value123";
106 } else if (t == typeof (bool)) {
107 bool v = (bool) defaultValue;
112 } else if (t == typeof (int)) {
113 int v = (int) defaultValue;
114 if (v == Int32.MaxValue)
119 Assert.Fail ("Unsupported return type '{0}' for property '{1}'", t.FullName, propertyName);
121 var mib = new MenuItemBinding ();
122 pi.SetValue (mib, setToValue, null);
124 Assert.AreEqual (expectedValue, mib.ToString (), propertyName);