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 Subscription subscription
;
45 private Month
[] months
;
46 private MockClass mock
;
51 CultureInfo en
= CultureInfo
.CreateSpecificCulture("en");
53 Thread
.CurrentThread
.CurrentCulture
= en
;
54 Thread
.CurrentThread
.CurrentUICulture
= en
;
56 helper
= new FormHelper();
58 subscription
= new Subscription();
59 mock
= new MockClass();
60 months
= new Month
[] {new Month(1, "January"), new Month(1, "February")}
;
61 product
= new Product("memory card", 10, (decimal) 12.30);
62 user
= new SimpleUser();
63 mock
.Values
= new int[] { 2, 3 }
;
65 HomeController controller
= new HomeController();
67 controller
.PropertyBag
.Add("product", product
);
68 controller
.PropertyBag
.Add("user", user
);
69 controller
.PropertyBag
.Add("roles", new Role
[] { new Role(1, "a"), new Role(2, "b"), new Role(3, "c") }
);
70 controller
.PropertyBag
.Add("sendemail", true);
71 controller
.PropertyBag
.Add("confirmation", "abc");
72 controller
.PropertyBag
.Add("fileaccess", FileAccess
.Read
);
73 controller
.PropertyBag
.Add("subscription", subscription
);
74 controller
.PropertyBag
.Add("months", months
);
75 controller
.PropertyBag
.Add("mock", mock
);
77 helper
.SetController(controller
);
81 /// Code posted on the mailing list
84 public void BugReport1()
86 FormHelper
.CheckboxList list
=
87 helper
.CreateCheckboxList("mock.Values", new int[] {1,2,3,4}
);
89 Assert
.IsNotNull(list
);
93 foreach(Object item
in list
)
95 String content
= list
.Item();
96 if (index
== 1 || index
== 2)
98 Assert
.AreEqual("<input type=\"checkbox\" id=\"mock_Values_" + index
+
99 "_\" name=\"mock.Values[" + index
+ "]\" value=\"" + item
+ "\" checked=\"checked\" />", content
);
103 Assert
.AreEqual("<input type=\"checkbox\" id=\"mock_Values_" + index
+
104 "_\" name=\"mock.Values[" + index
+ "]\" value=\"" + item
+ "\" />", content
);
112 /// Tests the subscription.Months as null and an int array as source
115 public void CheckboxFieldList1()
117 subscription
.Months
= null;
119 FormHelper
.CheckboxList list
=
120 helper
.CreateCheckboxList("subscription.Months", new int[] {1,2,3,4,5,6}
);
122 Assert
.IsNotNull(list
);
126 foreach(Object item
in list
)
128 String content
= list
.Item();
129 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months_" + index
+
130 "_\" name=\"subscription.Months[" + index
+ "]\" value=\"" + item
+ "\" />", content
);
136 /// Tests the subscription.Months as non null and an int array as source
139 public void CheckboxFieldList2()
141 subscription
.Months
= new int[] { 1, 2, 3, 4, 5 }
;
143 FormHelper
.CheckboxList list
=
144 helper
.CreateCheckboxList("subscription.Months", new int[] {1,2,3,4,5}
);
146 Assert
.IsNotNull(list
);
150 foreach(Object item
in list
)
152 String content
= list
.Item();
153 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months_" + index
+
154 "_\" name=\"subscription.Months[" + index
+ "]\" value=\"" + item
+ "\" checked=\"checked\" />", content
);
160 /// Tests the subscription.Months2 as null and using a <c>Month</c> array as data source
163 public void CheckboxFieldList3()
165 FormHelper
.CheckboxList list
=
166 helper
.CreateCheckboxList("subscription.Months2", months
, DictHelper
.Create("value=id"));
168 Assert
.IsNotNull(list
);
172 foreach(Month item
in list
)
174 String content
= list
.Item();
175 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months2_" + index
+
176 "_\" name=\"subscription.Months2[" + index
+ "].id\" value=\"" + item
.Id
+ "\" />", content
);
182 /// Tests the subscription.Months2 as null and using a <c>Month</c> array as data source
185 public void CheckboxFieldList3a()
187 subscription
.Months3
= null;
189 FormHelper
.CheckboxList list
=
190 helper
.CreateCheckboxList("subscription.Months3", months
, DictHelper
.Create("value=id"));
192 Assert
.IsNotNull(list
);
196 foreach(Month item
in list
)
198 String content
= list
.Item();
199 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months3_" + index
+
200 "_\" name=\"subscription.Months3[" + index
+ "].id\" value=\"" + item
.Id
+ "\" />", content
);
206 /// Tests the subscription.Months2 as non null and using a <c>Month</c> array as data source
209 public void CheckboxFieldList4()
211 subscription
.Months2
= new Month
[] {new Month(3, "March")}
;
213 FormHelper
.CheckboxList list
=
214 helper
.CreateCheckboxList("subscription.Months2", months
, DictHelper
.Create("value=id"));
216 Assert
.IsNotNull(list
);
220 foreach(Month item
in list
)
222 String content
= list
.Item();
223 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months2_" + index
+
224 "_\" name=\"subscription.Months2[" + index
+ "].Id\" value=\"" + item
.Id
+ "\" />", content
);
230 /// Tests the subscription.Months2 as non null and using a <c>Month</c> array as data source
231 /// but with selection
234 public void CheckboxFieldList5()
236 subscription
.Months2
= new Month
[] {new Month(1, "January"), new Month(2, "Feb") }
;
238 FormHelper
.CheckboxList list
=
239 helper
.CreateCheckboxList("subscription.Months2", months
, DictHelper
.Create("value=id"));
241 Assert
.IsNotNull(list
);
245 foreach(Month item
in list
)
247 String content
= list
.Item();
248 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months2_" + index
+
249 "_\" name=\"subscription.Months2[" + index
+ "].Id\" value=\"" + item
.Id
+ "\" checked=\"checked\" />", content
);
257 public void CheckboxFieldListInDotNet2()
259 FormHelper
.CheckboxList list
=
260 helper
.CreateCheckboxList("subscription.Months4", months
, DictHelper
.Create("value=id"));
262 Assert
.IsNotNull(list
);
266 foreach(Month item
in list
)
268 String content
= list
.Item();
269 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months4_" + index
+
270 "_\" name=\"subscription.Months4[" + index
+ "].id\" value=\"" + item
.Id
+ "\" />", content
);
276 public void CheckboxFieldListInDotNet2_WithSelection()
278 subscription
.Months4
.Add(new Month(1, "January"));
279 subscription
.Months4
.Add(new Month(2, "Feb"));
281 FormHelper
.CheckboxList list
=
282 helper
.CreateCheckboxList("subscription.Months4", months
, DictHelper
.Create("value=id"));
284 Assert
.IsNotNull(list
);
288 foreach(Month item
in list
)
290 String content
= list
.Item();
291 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months4_" + index
+
292 "_\" name=\"subscription.Months4[" + index
+ "].Id\" value=\"" + item
.Id
+ "\" checked=\"checked\" />", content
);