[bcl] Remove NET_4_0 defines from class libs
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / MenuItemBindingTest.cs
1 //
2 // Author:
3 //      Marek Habersack <mhabersack@novell.com>
4 //
5 // Copyright (C) 2010 Novell, Inc (http://novell.com)
6 //
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:
14 //
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 //
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.
25 using System;
26 using System.Collections.Generic;
27 using System.ComponentModel;
28 using System.Reflection;
29 using System.Web;
30 using System.Web.UI;
31 using System.Web.UI.WebControls;
32
33 using NUnit.Framework;
34 namespace MonoTests.System.Web.UI.WebControls
35 {
36         [TestFixture]
37         public class MenuItemBindingTest
38         {
39                 const string TO_STRING_EMPTY_VALUE = "(Empty)";
40
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}
64                 };
65
66                 [Test]
67                 public void Test_ToString ()
68                 {
69                         var mib = new MenuItemBinding ();
70
71                         Assert.AreEqual ("(Empty)", mib.ToString (), "#A1");
72                         foreach (var entry in toStringValues)
73                                 ToStringTestProperty (entry.Key, entry.Value);
74                 }
75
76                 void ToStringTestProperty (string propertyName, string expectedValue)
77                 {
78                         PropertyInfo pi = typeof (MenuItemBinding).GetProperty (propertyName, BindingFlags.Instance | BindingFlags.Public);
79                         if (pi == null)
80                                 Assert.Fail ("Property '{0}' not found.", propertyName);
81
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;
88                         } else {
89                                 if (t == typeof (string))
90                                         defaultValue = String.Empty;
91                                 else if (t == typeof (bool))
92                                         defaultValue = false;
93                                 else if (t == typeof (int))
94                                         defaultValue = Int32.MaxValue;
95                                 else
96                                         Assert.Fail ("Unsupported return type '{0}' for property '{1}'", t.FullName, propertyName);
97                         }
98
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";
104                                 else
105                                         setToValue = "value123";
106                         } else if (t == typeof (bool)) {
107                                 bool v = (bool) defaultValue;
108                                 if (v)
109                                         setToValue = false;
110                                 else
111                                         setToValue = true;
112                         } else if (t == typeof (int)) {
113                                 int v = (int) defaultValue;
114                                 if (v == Int32.MaxValue)
115                                         v = Int32.MinValue;
116                                 else
117                                         v = Int32.MaxValue;
118                         } else
119                                 Assert.Fail ("Unsupported return type '{0}' for property '{1}'", t.FullName, propertyName);
120
121                         var mib = new MenuItemBinding ();
122                         pi.SetValue (mib, setToValue, null);
123
124                         Assert.AreEqual (expectedValue, mib.ToString (), propertyName);
125                 }
126         }
127 }