[SRE] Improved token fixups processing.
[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 #if NET_4_0
35 namespace MonoTests.System.Web.UI.WebControls
36 {
37         [TestFixture]
38         public class MenuItemBindingTest
39         {
40                 const string TO_STRING_EMPTY_VALUE = "(Empty)";
41
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}
65                 };
66
67                 [Test]
68                 public void Test_ToString ()
69                 {
70                         var mib = new MenuItemBinding ();
71
72                         Assert.AreEqual ("(Empty)", mib.ToString (), "#A1");
73                         foreach (var entry in toStringValues)
74                                 ToStringTestProperty (entry.Key, entry.Value);
75                 }
76
77                 void ToStringTestProperty (string propertyName, string expectedValue)
78                 {
79                         PropertyInfo pi = typeof (MenuItemBinding).GetProperty (propertyName, BindingFlags.Instance | BindingFlags.Public);
80                         if (pi == null)
81                                 Assert.Fail ("Property '{0}' not found.", propertyName);
82
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;
89                         } else {
90                                 if (t == typeof (string))
91                                         defaultValue = String.Empty;
92                                 else if (t == typeof (bool))
93                                         defaultValue = false;
94                                 else if (t == typeof (int))
95                                         defaultValue = Int32.MaxValue;
96                                 else
97                                         Assert.Fail ("Unsupported return type '{0}' for property '{1}'", t.FullName, propertyName);
98                         }
99
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";
105                                 else
106                                         setToValue = "value123";
107                         } else if (t == typeof (bool)) {
108                                 bool v = (bool) defaultValue;
109                                 if (v)
110                                         setToValue = false;
111                                 else
112                                         setToValue = true;
113                         } else if (t == typeof (int)) {
114                                 int v = (int) defaultValue;
115                                 if (v == Int32.MaxValue)
116                                         v = Int32.MinValue;
117                                 else
118                                         v = Int32.MaxValue;
119                         } else
120                                 Assert.Fail ("Unsupported return type '{0}' for property '{1}'", t.FullName, propertyName);
121
122                         var mib = new MenuItemBinding ();
123                         pi.SetValue (mib, setToValue, null);
124
125                         Assert.AreEqual (expectedValue, mib.ToString (), propertyName);
126                 }
127         }
128 }
129 #endif