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;
35 namespace MonoTests.System.Web.UI.WebControls
38 public class MenuItemBindingTest
40 const string TO_STRING_EMPTY_VALUE = "(Empty)";
42 static readonly SortedDictionary <string, string> toStringValues = new SortedDictionary<string, string> (StringComparer.OrdinalIgnoreCase) {
43 {"DataMember", "value"},
44 {"Depth", TO_STRING_EMPTY_VALUE},
45 {"Enabled", TO_STRING_EMPTY_VALUE},
46 {"EnabledField", TO_STRING_EMPTY_VALUE},
47 {"FormatString", TO_STRING_EMPTY_VALUE},
48 {"ImageUrl", TO_STRING_EMPTY_VALUE},
49 {"ImageUrlField", TO_STRING_EMPTY_VALUE},
50 {"NavigateUrl", TO_STRING_EMPTY_VALUE},
51 {"Selectable", TO_STRING_EMPTY_VALUE},
52 {"SelectableField", TO_STRING_EMPTY_VALUE},
53 {"Target", TO_STRING_EMPTY_VALUE},
54 {"TargetField", TO_STRING_EMPTY_VALUE},
55 {"Text", TO_STRING_EMPTY_VALUE},
56 {"TextField", TO_STRING_EMPTY_VALUE},
57 {"ToolTip", TO_STRING_EMPTY_VALUE},
58 {"ToolTipField", TO_STRING_EMPTY_VALUE},
59 {"Value", TO_STRING_EMPTY_VALUE},
60 {"ValueField", TO_STRING_EMPTY_VALUE},
61 {"PopOutImageUrl", TO_STRING_EMPTY_VALUE},
62 {"PopOutImageUrlField", TO_STRING_EMPTY_VALUE},
63 {"SeparatorImageUrl", TO_STRING_EMPTY_VALUE},
64 {"SeparatorImageUrlField", TO_STRING_EMPTY_VALUE}
68 public void Test_ToString ()
70 var mib = new MenuItemBinding ();
72 Assert.AreEqual ("(Empty)", mib.ToString (), "#A1");
73 foreach (var entry in toStringValues)
74 ToStringTestProperty (entry.Key, entry.Value);
77 void ToStringTestProperty (string propertyName, string expectedValue)
79 PropertyInfo pi = typeof (MenuItemBinding).GetProperty (propertyName, BindingFlags.Instance | BindingFlags.Public);
81 Assert.Fail ("Property '{0}' not found.", propertyName);
83 object defaultValue = null;
84 object[] attrs = pi.GetCustomAttributes (typeof (DefaultValueAttribute), false);
85 Type t = pi.PropertyType;
86 if (attrs != null && attrs.Length > 0) {
87 var dva = attrs [0] as DefaultValueAttribute;
88 defaultValue = dva.Value;
90 if (t == typeof (string))
91 defaultValue = String.Empty;
92 else if (t == typeof (bool))
94 else if (t == typeof (int))
95 defaultValue = Int32.MaxValue;
97 Assert.Fail ("Unsupported return type '{0}' for property '{1}'", t.FullName, propertyName);
100 object setToValue = null;
101 if (t == typeof (string)) {
102 string v = defaultValue as String;
103 if (v == String.Empty || v != "value")
104 setToValue = "value";
106 setToValue = "value123";
107 } else if (t == typeof (bool)) {
108 bool v = (bool) defaultValue;
113 } else if (t == typeof (int)) {
114 int v = (int) defaultValue;
115 if (v == Int32.MaxValue)
120 Assert.Fail ("Unsupported return type '{0}' for property '{1}'", t.FullName, propertyName);
122 var mib = new MenuItemBinding ();
123 pi.SetValue (mib, setToValue, null);
125 Assert.AreEqual (expectedValue, mib.ToString (), propertyName);