1 //
2 // RepeatInfoTest.cs - Unit tests for System.Web.UI.WebControls.RepeatInfo
3 //
4 // Author:
5 //	Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 
29 using System;
30 using System.Collections;
31 using System.ComponentModel;
32 using System.Drawing;
33 using System.IO;
34 using System.Web;
35 using System.Web.UI;
36 using System.Web.UI.WebControls;
37 
38 using NUnit.Framework;
39 
40 namespace MonoTests.System.Web.UI.WebControls {
41 
42 	public class RepeatInfoUser : IRepeatInfoUser {
43 
44 		private bool footer;
45 		private bool header;
46 		private bool separators;
47 		private int count;
48 		private int counter;
49 
50 
RepeatInfoUser(bool footer, bool header, bool separators, int count)51 		public RepeatInfoUser (bool footer, bool header, bool separators, int count)
52 		{
53 			counter = 0;
54 			this.footer = footer;
55 			this.header = header;
56 			this.separators = separators;
57 			this.count = count;
58 		}
59 
60 
61 		public bool HasFooter {
62 			get { return footer; }
63 		}
64 
65 		public bool HasHeader {
66 			get { return header; }
67 		}
68 
69 		public bool HasSeparators {
70 			get { return separators; }
71 		}
72 
73 		public int RepeatedItemCount {
74 			get { return count; }
75 		}
76 
GetItemStyle(ListItemType itemType, int repeatIndex)77 		public Style GetItemStyle (ListItemType itemType, int repeatIndex)
78 		{
79 			return null;
80 		}
81 
RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)82 		public void RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
83 		{
84 			writer.Write ((counter++).ToString ());
85 		}
86 	}
87 
88 	[TestFixture]
89 	public class RepeatInfoTest {
90 
GetWriter()91 		private HtmlTextWriter GetWriter ()
92 		{
93 			StringWriter sw = new StringWriter ();
94 			sw.NewLine = "\n";
95 			return new HtmlTextWriter (sw);
96 		}
97 
DoTest(int cols, int cnt, RepeatDirection d, RepeatLayout l, bool OuterTableImplied, bool ftr, bool hdr, bool sep)98 		string DoTest (int cols, int cnt, RepeatDirection d, RepeatLayout l, bool OuterTableImplied, bool ftr, bool hdr, bool sep)
99 		{
100 			HtmlTextWriter htw = GetWriter ();
101 			RepeatInfo ri = new RepeatInfo ();
102 			ri.RepeatColumns = cols;
103 			ri.RepeatDirection = d;
104 			ri.RepeatLayout = l;
105 			ri.OuterTableImplied = OuterTableImplied;
106 
107 			ri.RenderRepeater (htw, new RepeatInfoUser (ftr, hdr, sep, cnt), new TableStyle (), new DataList ());
108 			return htw.InnerWriter.ToString ();
109 		}
110 
111 		[Test]
DefaultValues()112 		public void DefaultValues ()
113 		{
114 			RepeatInfo ri = new RepeatInfo ();
115 			Assert.AreEqual (0, ri.RepeatColumns, "RepeatColumns");
116 			Assert.AreEqual (RepeatDirection.Vertical, ri.RepeatDirection, "RepeatDirection");
117 			Assert.AreEqual (RepeatLayout.Table, ri.RepeatLayout, "RepeatLayout");
118 			Assert.IsFalse (ri.OuterTableImplied, "OuterTableImplied");
119 		}
120 
121 		[Test]
RepeatColumns_Negative()122 		public void RepeatColumns_Negative ()
123 		{
124 			RepeatInfo ri = new RepeatInfo ();
125 			ri.RepeatColumns = -1;
126 			Assert.AreEqual (-1, ri.RepeatColumns, "-1");
127 			ri.RepeatColumns = Int32.MinValue;
128 			Assert.AreEqual (Int32.MinValue, ri.RepeatColumns, "Int32.MinValue");
129 		}
130 
131 		[Test]
132 		[ExpectedException (typeof (ArgumentOutOfRangeException))]
RepeatDirection_Invalid()133 		public void RepeatDirection_Invalid ()
134 		{
135 			RepeatInfo ri = new RepeatInfo ();
136 			ri.RepeatDirection = (RepeatDirection) Int32.MinValue;
137 		}
138 
139 		[Test]
140 		[ExpectedException (typeof (ArgumentOutOfRangeException))]
RepeatLayout_Invalid()141 		public void RepeatLayout_Invalid ()
142 		{
143 			RepeatInfo ri = new RepeatInfo ();
144 			ri.RepeatLayout = (RepeatLayout) Int32.MinValue;
145 		}
146 
RenderRepeater_BaseControl(string s, string msg, WebControl wc)147 		private void RenderRepeater_BaseControl (string s, string msg, WebControl wc)
148 		{
149 			RepeatInfo ri = new RepeatInfo ();
150 			ri.RepeatColumns = 3;
151 			ri.RepeatDirection = RepeatDirection.Vertical;
152 			ri.RepeatLayout = RepeatLayout.Table;
153 
154 			HtmlTextWriter writer = GetWriter ();
155 			ri.RenderRepeater (writer, new RepeatInfoUser (false, false, false, 1), new TableStyle (), wc);
156 			string rendered = writer.InnerWriter.ToString ();
157 			Assert.AreEqual (s, rendered, msg);
158 		}
159 
160 		[Test]
161 		[ExpectedException (typeof (NullReferenceException))]
RenderRepeater_BaseControl_Null()162 		public void RenderRepeater_BaseControl_Null ()
163 		{
164 			RenderRepeater_BaseControl ("shouldn't get here", "null", null);
165 		}
166 
167 		[Test]
RenderRepeater_BaseControl()168 		public void RenderRepeater_BaseControl ()
169 		{
170 			string noid = "<table>\n\t<tr>\n\t\t<td>0</td><td></td><td></td>\n\t</tr>\n</table>";
171 			string id_enabled = "<table id=\"foo\" class=\"aspNetDisabled\">\n\t<tr>\n\t\t<td>0</td><td></td><td></td>\n\t</tr>\n</table>";
172 			RenderRepeater_BaseControl (noid, "Table", new Table ());
173 			RenderRepeater_BaseControl (noid, "DataList", new DataList ());
174 			RenderRepeater_BaseControl (noid, "DataListItem", new DataListItem (0, ListItemType.Item));
175 
176 			Label l = new Label ();
177 			l.Enabled = false;
178 			l.ID = "foo";
179 
180 			RenderRepeater_BaseControl (id_enabled, "id and disabled", l);
181 		}
182 	}
183 }
184 
185