svn path=/branches/mono-1-1-9/mcs/; revision=51216
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / RepeatInfoTest.cs
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
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
77                 public Style GetItemStyle (ListItemType itemType, int repeatIndex)
78                 {
79                         return null;
80                 }
81
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
91                 private HtmlTextWriter GetWriter ()
92                 {
93                         StringWriter sw = new StringWriter ();
94                         sw.NewLine = "\n";
95                         return new HtmlTextWriter (sw);
96                 }
97
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                 public void DefaultValues ()
112                 {
113                         RepeatInfo ri = new RepeatInfo ();
114                         Assert.AreEqual (0, ri.RepeatColumns, "RepeatColumns");
115                         Assert.AreEqual (RepeatDirection.Vertical, ri.RepeatDirection, "RepeatDirection");
116                         Assert.AreEqual (RepeatLayout.Table, ri.RepeatLayout, "RepeatLayout");
117                         Assert.IsFalse (ri.OuterTableImplied, "OuterTableImplied");
118                 }
119
120                 [Test]
121                 public void RepeatColumns_Negative ()
122                 {
123                         RepeatInfo ri = new RepeatInfo ();
124                         ri.RepeatColumns = -1;
125                         Assert.AreEqual (-1, ri.RepeatColumns, "-1");
126                         ri.RepeatColumns = Int32.MinValue;
127                         Assert.AreEqual (Int32.MinValue, ri.RepeatColumns, "Int32.MinValue");
128                 }
129
130                 [Test]
131                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
132                 public void RepeatDirection_Invalid ()
133                 {
134                         RepeatInfo ri = new RepeatInfo ();
135                         ri.RepeatDirection = (RepeatDirection) Int32.MinValue;
136                 }
137
138                 [Test]
139                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
140                 public void RepeatLayout_Invalid ()
141                 {
142                         RepeatInfo ri = new RepeatInfo ();
143                         ri.RepeatLayout = (RepeatLayout) Int32.MinValue;
144                 }
145
146                 private void RenderRepeater_BaseControl (string s, string msg, WebControl wc)
147                 {
148                         RepeatInfo ri = new RepeatInfo ();
149                         ri.RepeatColumns = 3;
150                         ri.RepeatDirection = RepeatDirection.Vertical;
151                         ri.RepeatLayout = RepeatLayout.Table;
152
153                         HtmlTextWriter writer = GetWriter ();
154                         ri.RenderRepeater (writer, new RepeatInfoUser (false, false, false, 1), new TableStyle (), wc);
155                         Assert.AreEqual (s, writer.InnerWriter.ToString (), msg);
156                 }
157
158                 [Test]
159                 [ExpectedException (typeof (NullReferenceException))]
160                 public void RenderRepeater_BaseControl_Null ()
161                 {
162                         RenderRepeater_BaseControl ("shouldn't get here", "null", null);
163                 }
164
165
166                 
167                 [Test]
168                 public void RenderRepeater_BaseControl ()
169                 {
170 #if NET_2_0
171                         string noid = "<table border=\"0\">\n\t<tr>\n\t\t<td>0</td><td></td><td></td>\n\t</tr>\n</table>";
172                         string id_enabled = "<table id=\"foo\" disabled=\"disabled\" border=\"0\">\n\t<tr>\n\t\t<td>0</td><td></td><td></td>\n\t</tr>\n</table>";
173 #else
174                         string noid = "<table border=\"0\">\n\t<tr>\n\t\t<td>0</td>\n\t</tr>\n</table>";
175                         string id_enabled = "<table id=\"foo\" disabled=\"disabled\" border=\"0\">\n\t<tr>\n\t\t<td>0</td>\n\t</tr>\n</table>";
176 #endif
177                         RenderRepeater_BaseControl (noid, "Table", new Table ());
178                         RenderRepeater_BaseControl (noid, "DataList", new DataList ());
179                         RenderRepeater_BaseControl (noid, "DataListItem", new DataListItem (0, ListItemType.Item));
180
181                         Label l = new Label ();
182                         l.Enabled = false;
183                         l.ID = "foo";
184                         
185                         RenderRepeater_BaseControl (id_enabled, "id and disabled", l);                  
186                 }
187         }
188 }
189