1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.Framework
.Tests
.Helpers
18 using System
.Globalization
;
20 using System
.Threading
;
22 using Castle
.MonoRail
.Framework
.Helpers
;
23 using Castle
.MonoRail
.Framework
.Tests
.Controllers
;
25 using NUnit
.Framework
;
28 public class FormHelperCheckboxFieldListTestCase
36 get { return values; }
37 set { values = value; }
41 private FormHelper helper
;
42 private Product product
;
43 private SimpleUser user
;
44 private SimpleUser
[] users
;
45 private Subscription subscription
;
46 private Month
[] months
;
47 private MockClass mock
;
52 CultureInfo en
= CultureInfo
.CreateSpecificCulture("en");
54 Thread
.CurrentThread
.CurrentCulture
= en
;
55 Thread
.CurrentThread
.CurrentUICulture
= en
;
57 helper
= new FormHelper();
59 subscription
= new Subscription();
60 mock
= new MockClass();
61 months
= new Month
[] {new Month(1, "January"), new Month(1, "February")}
;
62 product
= new Product("memory card", 10, (decimal) 12.30);
63 user
= new SimpleUser();
64 users
= new SimpleUser
[] { new SimpleUser(1, false), new SimpleUser(2, true), new SimpleUser(3, false), new SimpleUser(4, true) }
;
65 mock
.Values
= new int[] { 2, 3 }
;
67 HomeController controller
= new HomeController();
69 controller
.PropertyBag
.Add("product", product
);
70 controller
.PropertyBag
.Add("user", user
);
71 controller
.PropertyBag
.Add("users", users
);
72 controller
.PropertyBag
.Add("roles", new Role
[] { new Role(1, "a"), new Role(2, "b"), new Role(3, "c") }
);
73 controller
.PropertyBag
.Add("sendemail", true);
74 controller
.PropertyBag
.Add("confirmation", "abc");
75 controller
.PropertyBag
.Add("fileaccess", FileAccess
.Read
);
76 controller
.PropertyBag
.Add("subscription", subscription
);
77 controller
.PropertyBag
.Add("months", months
);
78 controller
.PropertyBag
.Add("mock", mock
);
80 helper
.SetController(controller
);
84 /// Code posted on the mailing list
87 public void BugReport1()
89 FormHelper
.CheckboxList list
=
90 helper
.CreateCheckboxList("mock.Values", new int[] {1,2,3,4}
);
92 Assert
.IsNotNull(list
);
96 foreach(Object item
in list
)
98 String content
= list
.Item();
99 if (index
== 1 || index
== 2)
101 Assert
.AreEqual("<input type=\"checkbox\" id=\"mock_Values_" + index
+
102 "_\" name=\"mock.Values[" + index
+ "]\" value=\"" + item
+ "\" checked=\"checked\" />", content
);
106 Assert
.AreEqual("<input type=\"checkbox\" id=\"mock_Values_" + index
+
107 "_\" name=\"mock.Values[" + index
+ "]\" value=\"" + item
+ "\" />", content
);
115 /// Tests the subscription.Months as null and an int array as source
118 public void CheckboxFieldList1()
120 subscription
.Months
= null;
122 FormHelper
.CheckboxList list
=
123 helper
.CreateCheckboxList("subscription.Months", new int[] {1,2,3,4,5,6}
);
125 Assert
.IsNotNull(list
);
129 foreach(Object item
in list
)
131 String content
= list
.Item();
132 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months_" + index
+ "_\" name=\"subscription.Months[" + index
+ "]\" value=\"" + item
+ "\" />", content
);
138 /// Tests the subscription.Months as non null and an int array as source
141 public void CheckboxFieldList2()
143 subscription
.Months
= new int[] { 1, 2, 3, 4, 5 }
;
145 FormHelper
.CheckboxList list
=
146 helper
.CreateCheckboxList("subscription.Months", new int[] {1,2,3,4,5}
);
148 Assert
.IsNotNull(list
);
152 foreach(Object item
in list
)
154 String content
= list
.Item();
155 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months_" + index
+ "_\" " +
156 "name=\"subscription.Months[" + index
+ "]\" value=\"" + item
+ "\" checked=\"checked\" />", content
);
162 /// Tests the subscription.Months2 as null and using a <c>Month</c> array as data source
165 public void CheckboxFieldList3()
167 FormHelper
.CheckboxList list
=
168 helper
.CreateCheckboxList("subscription.Months2", months
, DictHelper
.Create("value=id"));
170 Assert
.IsNotNull(list
);
174 foreach(Month item
in list
)
176 String content
= list
.Item();
177 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months2_" + index
+
178 "_\" name=\"subscription.Months2[" + index
+ "].id\" value=\"" + item
.Id
+ "\" />", content
);
184 /// Tests the subscription.Months2 as null and using a <c>Month</c> array as data source
187 public void CheckboxFieldList3a()
189 subscription
.Months3
= null;
191 FormHelper
.CheckboxList list
=
192 helper
.CreateCheckboxList("subscription.Months3", months
, DictHelper
.Create("value=id"));
194 Assert
.IsNotNull(list
);
198 foreach(Month item
in list
)
200 String content
= list
.Item();
201 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months3_" + index
+
202 "_\" name=\"subscription.Months3[" + index
+ "].id\" value=\"" + item
.Id
+ "\" />", content
);
208 /// Tests the subscription.Months2 as non null and using a <c>Month</c> array as data source
211 public void CheckboxFieldList4()
213 subscription
.Months2
= new Month
[] {new Month(3, "March")}
;
215 FormHelper
.CheckboxList list
=
216 helper
.CreateCheckboxList("subscription.Months2", months
, DictHelper
.Create("value=id"));
218 Assert
.IsNotNull(list
);
222 foreach(Month item
in list
)
224 String content
= list
.Item();
225 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months2_" + index
+
226 "_\" name=\"subscription.Months2[" + index
+ "].Id\" value=\"" + item
.Id
+ "\" />", content
);
232 /// Tests the subscription.Months2 as non null and using a <c>Month</c> array as data source
233 /// but with selection
236 public void CheckboxFieldList5()
238 subscription
.Months2
= new Month
[] {new Month(1, "January"), new Month(2, "Feb") }
;
240 FormHelper
.CheckboxList list
=
241 helper
.CreateCheckboxList("subscription.Months2", months
, DictHelper
.Create("value=id"));
243 Assert
.IsNotNull(list
);
247 foreach(Month item
in list
)
249 String content
= list
.Item();
250 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months2_" + index
+
251 "_\" name=\"subscription.Months2[" + index
+ "].Id\" value=\"" + item
.Id
+ "\" checked=\"checked\" />", content
);
257 public void UsingIdPerElement()
259 subscription
.Months
= new int[] { 1, 2, 3, 4, 5 }
;
261 FormHelper
.CheckboxList list
=
262 helper
.CreateCheckboxList("subscription.Months", new int[] { 1, 2 }
);
264 Assert
.IsNotNull(list
);
268 foreach(Object item
in list
)
270 string content
= list
.Item("menu" + index
);
274 Assert
.AreEqual("<input type=\"checkbox\" id=\"menu" + index
+ "\" name=\"subscription.Months[" + index
+ "]\" value=\"" + item
+ "\" checked=\"checked\" />", content
);
278 Assert
.AreEqual("<input type=\"checkbox\" name=\"subscription.Months[" + index
+ "]\" value=\"" + item
+ "\" id=\"menu" + index
+ "\" />", content
);
285 public void CheckboxFieldListInDotNet2()
287 FormHelper
.CheckboxList list
=
288 helper
.CreateCheckboxList("subscription.Months4", months
, DictHelper
.Create("value=id"));
290 Assert
.IsNotNull(list
);
294 foreach(Month item
in list
)
296 String content
= list
.Item();
297 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months4_" + index
+ "_\" name=\"subscription.Months4[" + index
+ "].id\" value=\"" + item
.Id
+ "\" />", content
);
304 public void CheckboxFieldListInDotNet2_WithSelection()
306 subscription
.Months4
.Add(new Month(1, "January"));
307 subscription
.Months4
.Add(new Month(2, "Feb"));
309 FormHelper
.CheckboxList list
=
310 helper
.CreateCheckboxList("subscription.Months4", months
, DictHelper
.Create("value=id"));
312 Assert
.IsNotNull(list
);
316 foreach(Month item
in list
)
318 String content
= list
.Item();
319 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months4_" + index
+ "_\" name=\"subscription.Months4[" + index
+ "].Id\" value=\"" + item
.Id
+ "\" checked=\"checked\" />", content
);