File: RadioButtonListTest.cs

package info (click to toggle)
mono 2.6.7-5.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 327,344 kB
  • ctags: 413,649
  • sloc: cs: 2,471,883; xml: 1,768,594; ansic: 350,665; sh: 13,644; makefile: 8,640; perl: 1,784; asm: 717; cpp: 209; python: 146; sql: 81; sed: 16
file content (529 lines) | stat: -rw-r--r-- 15,679 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//
// Tests for System.Web.UI.WebControls.RadioButtonList.cs
//
// Authors:
//	Jordi Mas i Hernandez (jordi@ximian.com)
//	Gonzalo Paniagua Javier (gonzalo@novell.com)
//
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System.Web.UI.WebControls;
using NUnit.Framework;
using System;
using System.Collections.Specialized;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Globalization;
using MonoTests.SystemWeb.Framework;
using MonoTests.stand_alone.WebHarness;
using System.Drawing;
using System.Collections;


namespace MonoTests.System.Web.UI.WebControls {

	[TestFixture]
	public class RadioButtonListTest
	{
		#region help_classes
		public class TestRadioButtonList : RadioButtonList {
			public StateBag StateBag {
				get { return base.ViewState; }
			}

			public string Render ()
			{
				HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
				base.Render (writer);
				return writer.InnerWriter.ToString ();
			}
		}

#if NET_2_0
		class PokerRadioButtonList : RadioButtonList
		{
			public StateBag StateBag
			{
				get { return base.ViewState; }
			}

			public string Render ()
			{
				HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
				base.Render (writer);
				return writer.InnerWriter.ToString ();
			}

			protected override Style GetItemStyle (ListItemType itemType, int repeatIndex)
			{
				Style s = new Style ();
				s.BackColor = Color.Red;
				s.BorderStyle = BorderStyle.Solid;
				WebTest.CurrentTest.UserData = "GetItemStyle";
				return s;
			}

			public new bool HasFooter
			{
				get
				{
					return base.HasFooter;
				}
			}

			public new bool HasHeader
			{
				get
				{
					return base.HasHeader;
				}
			}

			public new bool HasSeparators
			{
				get
				{
					return base.HasSeparators;
				}
			}

			public new int RepeatedItemCount
			{
				get
				{
					return base.RepeatedItemCount;
				}
			}

			protected override void RaisePostDataChangedEvent ()
			{
				base.RaisePostDataChangedEvent ();
			}

			public void DoRaisePostDataChangedEvent ()
			{
				base.RaisePostDataChangedEvent ();
			}

			public new virtual void VerifyMultiSelect()
			{
				base.VerifyMultiSelect();
			}
		}
#endif
		#endregion

		[Test]
		public void RadioButtonList_Constructor ()
		{
			TestRadioButtonList r = new TestRadioButtonList ();
			Assert.AreEqual (-1, r.CellPadding, "A1");
			Assert.AreEqual (-1, r.CellSpacing, "A2");
			Assert.AreEqual (0, r.RepeatColumns, "A3");
			Assert.AreEqual (RepeatDirection.Vertical, r.RepeatDirection, "A4");
			Assert.AreEqual (RepeatLayout.Table, r.RepeatLayout, "A5");
			Assert.AreEqual (TextAlign.Right, r.TextAlign, "A6");
			Assert.AreEqual (false, ((IRepeatInfoUser)r).HasFooter, "A7");
			Assert.AreEqual (false, ((IRepeatInfoUser)r).HasHeader, "A8");
			Assert.AreEqual (false, ((IRepeatInfoUser)r).HasSeparators, "A9");
			Assert.AreEqual (0, ((IRepeatInfoUser)r).RepeatedItemCount, "A10");
		}

		[Test]
		public void CellPaddingProperties ()
		{
			TestRadioButtonList r = new TestRadioButtonList ();
			r.CellPadding = 5;
			Assert.AreEqual (5, r.CellPadding, "setting");

			string s = r.Render ();	
#if NET_2_0
			// FIXME: missing some info to start rendering ?
			Assert.AreEqual (String.Empty, s, "htmloutput");
#else
			Assert.IsTrue (s.ToLower ().IndexOf ("cellpadding=\"5\"") !=  -1, "htmloutput");
#endif
		}	

		[Test]
		public void CellSpacingProperties ()
		{
			TestRadioButtonList r = new TestRadioButtonList ();
			r.CellSpacing = 5;
			Assert.AreEqual (5, r.CellSpacing, "setting");

			string s = r.Render ();	
#if NET_2_0
			// FIXME: missing some info to start rendering ?
			Assert.AreEqual (String.Empty, s, "htmloutput");
#else
			Assert.IsTrue (s.ToLower ().IndexOf ("cellspacing=\"5\"") !=  -1, "htmloutput");
#endif
		}	

#if NET_2_0
		[Test]
		[Category ("NunitWeb")]
		public void Render ()
		{
			string RenderedPageHtml = new WebTest (PageInvoker.CreateOnLoad (Render_Load)).Run ();
			string RenderedControlHtml = HtmlDiff.GetControlFromPageHtml (RenderedPageHtml);
			string OriginControlHtml = @"<table id=""ctl01"" border=""0"">
											<tr>
												<td><input id=""ctl01_0"" type=""radio"" name=""ctl01"" value=""value1"" /><label for=""ctl01_0"">text2</label></td>
											</tr>
										</table>";
			HtmlDiff.AssertAreEqual (OriginControlHtml, RenderedControlHtml, "Render");
		}

		public static void Render_Load (Page p)
		{
			LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
			LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
			TestRadioButtonList c = new TestRadioButtonList ();
			p.Form.Controls.Add (lcb);
			p.Form.Controls.Add (c);
			p.Form.Controls.Add (lce);
			c.Items.Add (new ListItem ("text2", "value1"));
		}
#else
		[Test]
		public void Render ()
		{
			TestRadioButtonList c = new TestRadioButtonList ();

			c.Items.Add (new ListItem ("text2", "value1"));

			string s = c.Render ();

			Assert.IsTrue (s.ToLower ().IndexOf (" type=\"radio\"") !=  -1, "type");
			Assert.IsTrue (s.ToLower ().IndexOf ("value1") !=  -1, "value");
			Assert.IsTrue (s.ToLower ().IndexOf ("text2") !=  -1, "text");
		}
#endif

		// Exceptions
		[Test]
		[ExpectedException (typeof (ArgumentOutOfRangeException))]
		public void RepeatColumnsException ()
		{
			TestRadioButtonList r = new TestRadioButtonList ();
			r.RepeatColumns = -1;
		}

		[Test]
		[ExpectedException (typeof (ArgumentOutOfRangeException))]
		public void RepeatDirectionException ()
		{
			TestRadioButtonList r = new TestRadioButtonList ();
			r.RepeatDirection = (RepeatDirection) 4;
		}


		[Test]
		[ExpectedException (typeof (ArgumentOutOfRangeException))]
		public void RepeatLayoutException ()
		{
			TestRadioButtonList r = new TestRadioButtonList ();
			r.RepeatLayout = (RepeatLayout) 3;
		}

		bool event_called;
		void OnSelected (object sender, EventArgs args)
		{
			event_called = true;
		}

		[Test]
		public void LoadAndRaise1 ()
		{
			RadioButtonList rbl = new RadioButtonList ();
			rbl.Items.Add (new ListItem ("Uno", "1"));
			rbl.Items.Add (new ListItem ("Dos", "2"));
			rbl.Items.Add (new ListItem ("Tres", "3"));
			rbl.SelectedIndex = 2;
			NameValueCollection nvc = new NameValueCollection ();
			nvc ["XXX"] = "3";

			IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
#if NET_2_0
			Assert.IsFalse (handler.LoadPostData ("XXX", nvc), "#01");
#else
			Assert.IsTrue (handler.LoadPostData ("XXX", nvc), "#01");
#endif
			rbl.SelectedIndexChanged += new EventHandler (OnSelected);
			event_called = false;
			handler.RaisePostDataChangedEvent ();
#if NET_2_0
			Assert.IsTrue (event_called, "#02");
#else
			// Not called. Value is the same as the selected previously
			Assert.IsFalse (event_called, "#02");
#endif
			Assert.AreEqual ("3", rbl.SelectedValue, "#03");
		}

		[Test]
		public void LoadAndRaise2 ()
		{
			RadioButtonList rbl = new RadioButtonList ();
			rbl.Items.Add (new ListItem ("Uno", "1"));
			rbl.Items.Add (new ListItem ("Dos", "2"));
			rbl.Items.Add (new ListItem ("Tres", "3"));
			rbl.SelectedIndex = 2;
			NameValueCollection nvc = new NameValueCollection ();
			nvc ["XXX"] = "2";

			IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
			Assert.AreEqual (true, handler.LoadPostData ("XXX", nvc), "#01");
			rbl.SelectedIndexChanged += new EventHandler (OnSelected);
			event_called = false;
			handler.RaisePostDataChangedEvent ();
			Assert.AreEqual (true, event_called, "#02");
			Assert.AreEqual ("2", rbl.SelectedValue, "#03");
		}

		[Test]
		public void LoadAndRaise3 ()
		{
			RadioButtonList rbl = new RadioButtonList ();
			rbl.Items.Add (new ListItem ("Uno", "1"));
			rbl.Items.Add (new ListItem ("Dos", "2"));
			rbl.Items.Add (new ListItem ("Tres", "3"));
			rbl.SelectedIndex = 2;
			NameValueCollection nvc = new NameValueCollection ();
			nvc ["XXX"] = "blah";

			IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
			Assert.AreEqual (false, handler.LoadPostData ("XXX", nvc), "#01");
		}

		
#if NET_2_0
		[Test]
		[ExpectedException(typeof(HttpException))]
		public void VerifyMultiSelectTest()
	        {
	            PokerRadioButtonList list = new PokerRadioButtonList();
	            list.VerifyMultiSelect();
	        }

		[Test]
		public void Defaults ()
		{
			PokerRadioButtonList r = new PokerRadioButtonList ();
			Assert.AreEqual (0, r.RepeatedItemCount, "RepeatedItemCount");
			Assert.AreEqual (false, r.HasFooter, "HasFooter");
			Assert.AreEqual (false, r.HasHeader, "HasHeader");
			Assert.AreEqual (false, r.HasSeparators, "HasSeparators");
		}
		
		[Test]
		[Category ("NunitWeb")]
		public void GetItemStyle ()
		{
			WebTest t = new WebTest (PageInvoker.CreateOnLoad (GetItemStyle_Load));
			string html = t.Run ();
			string ctrl = HtmlDiff.GetControlFromPageHtml (html);
			if (ctrl == string.Empty)
				Assert.Fail ("RadioButtonList not created fail");
			Assert.AreEqual ("GetItemStyle", (string) t.UserData, "GetItemStyle not done");
			if (ctrl.IndexOf ("<td style=\"background-color:Red;border-style:Solid;\">") == -1)
				Assert.Fail ("RadioButtonList style not rendered");
		}

		public static void GetItemStyle_Load (Page p)
		{
			PokerRadioButtonList rbl = new PokerRadioButtonList ();
			rbl.Items.Add (new ListItem ("Uno", "1"));
			rbl.Items.Add (new ListItem ("Dos", "2"));
			rbl.Items.Add (new ListItem ("Tres", "3"));
			p.Form.Controls.Add (new LiteralControl (HtmlDiff.BEGIN_TAG));
			p.Form.Controls.Add (rbl);
			p.Form.Controls.Add (new LiteralControl (HtmlDiff.END_TAG));
		}

		[Test]
		public void  RaisePostDataChangedEvent ()
		{
			PokerRadioButtonList r = new PokerRadioButtonList ();
			r.SelectedIndexChanged += new EventHandler (r_SelectedIndexChanged);
			Assert.AreEqual (false, eventSelectedIndexChanged, "Before");
			r.DoRaisePostDataChangedEvent ();
			Assert.AreEqual (true, eventSelectedIndexChanged, "After");
		}

		bool eventSelectedIndexChanged;
		void r_SelectedIndexChanged (object sender, EventArgs e)
		{
			eventSelectedIndexChanged = true;
		}

		[Test]
		[Category ("NunitWeb")]
		public void RaisePostDataChangedEvent_PostBack ()
		{
			WebTest t = new WebTest (PageInvoker.CreateOnInit (RaisePostDataChangedEvent_Init));
			string html = t.Run ();
			FormRequest fr = new FormRequest (t.Response, "form1");
			fr.Controls.Add ("__EVENTTARGET");
			fr.Controls.Add ("__EVENTARGUMENT");
			fr.Controls.Add ("RadioButtonList1");

			fr.Controls["__EVENTTARGET"].Value = "RadioButtonList1";
			fr.Controls["__EVENTARGUMENT"].Value = "";
			fr.Controls["RadioButtonList1"].Value = "test";
			t.Request = fr;
			t.Run ();
			if (t.UserData == null)
				Assert.Fail ("RaisePostDataChangedEvent Failed#1");
			Assert.AreEqual ("SelectedIndexChanged", (string) t.UserData, "RaisePostDataChangedEvent Failed#2");
		}

		public static void RaisePostDataChangedEvent_Init (Page p)
		{
			TestRadioButtonList r = new TestRadioButtonList ();
			r.ID = "RadioButtonList1";
			r.Items.Add (new ListItem ("test", "test"));
			r.SelectedIndexChanged += new EventHandler (event_SelectedIndexChanged);
			p.Form.Controls.Add (r);
		}

		public static void event_SelectedIndexChanged (object sender, EventArgs e)
		{
			WebTest.CurrentTest.UserData = "SelectedIndexChanged";	
		}

		#region help_class
		class Poker : RadioButtonList
		{
			protected override bool LoadPostData (string postDataKey, global::System.Collections.Specialized.NameValueCollection postCollection)
			{
				if (WebTest.CurrentTest.UserData == null) {
					ArrayList list = new ArrayList ();
					list.Add ("LoadPostData");
					WebTest.CurrentTest.UserData = list;
				}
				else {
					ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
					if (list == null)
						throw new NullReferenceException ();
					list.Add ("LoadPostData");
					WebTest.CurrentTest.UserData = list;
				}
				return base.LoadPostData (postDataKey, postCollection);
			}
			
			protected override void OnLoad (EventArgs e)
			{
				if (WebTest.CurrentTest.UserData == null) {
					ArrayList list = new ArrayList ();
					list.Add ("ControlLoad");
					WebTest.CurrentTest.UserData = list;
				}
				else {
					ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
					if (list == null)
						throw new NullReferenceException ();
					list.Add ("ControlLoad");
					WebTest.CurrentTest.UserData = list;
				}
				base.OnLoad (e);
			}
		}
		#endregion

		[Test]
		[Category ("NunitWeb")]
		public void LoadPostData ()  //Just flow and not implementation detail
		{
			WebTest t = new WebTest (PageInvoker.CreateOnLoad (LoadPostData_Load));
			string html = t.Run ();
			FormRequest fr = new FormRequest (t.Response, "form1");
			fr.Controls.Add ("__EVENTTARGET");
			fr.Controls.Add ("__EVENTARGUMENT");
			fr.Controls.Add ("RadioButtonList1");

			fr.Controls["__EVENTTARGET"].Value = "RadioButtonList1";
			fr.Controls["__EVENTARGUMENT"].Value = "";
			fr.Controls["RadioButtonList1"].Value = "test";
			t.Request = fr;
			t.Run ();

			ArrayList eventlist = t.UserData as ArrayList;
			if (eventlist == null)
				Assert.Fail ("User data does not been created fail");
			Assert.AreEqual ("ControlLoad", eventlist[0], "Live Cycle Flow #1");
			Assert.AreEqual ("PageLoad", eventlist[1], "Live Cycle Flow #2");
			Assert.AreEqual ("ControlLoad", eventlist[2], "Live Cycle Flow #3");
			Assert.AreEqual ("LoadPostData", eventlist[3], "Live Cycle Flow #4");

		}

		public static void LoadPostData_Load (Page p)
		{
			Poker b = new Poker ();
			b.ID = "RadioButtonList1";
			b.Items.Add (new ListItem ("test", "test"));
			p.Form.Controls.Add (b);
			if (p.IsPostBack) {
				if (WebTest.CurrentTest.UserData == null) {
					ArrayList list = new ArrayList ();
					list.Add ("PageLoad");
					WebTest.CurrentTest.UserData = list;
				}
				else {
					ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
					if (list == null)
						throw new NullReferenceException ();
					list.Add ("PageLoad");
					WebTest.CurrentTest.UserData = list;
				}
			}
		}

		[Test]
		public void RepeatedItemCount ()
		{
			PokerRadioButtonList r = new PokerRadioButtonList ();
			Assert.AreEqual (0, r.RepeatedItemCount, "RepeatedItemCount#1");
			r.Items.Add (new ListItem ("Uno", "1"));
			Assert.AreEqual (1, r.RepeatedItemCount, "RepeatedItemCount#2");
			r.Items.Add (new ListItem ("Dos", "2"));
			Assert.AreEqual (2, r.RepeatedItemCount, "RepeatedItemCount#3");
			r.Items.Remove (r.Items[1]);
			Assert.AreEqual (1, r.RepeatedItemCount, "RepeatedItemCount#4");
		}

		[TestFixtureTearDown]
		public void TearDown ()
		{
			WebTest.Unload ();
		}
#endif
	}
}