2005-09-13 Sureshkumar T <tsureshkumar@novell.com>
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / ListControlTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.ListBoxTest.cs
3 //
4 // Author:
5 //      Jackson Harper (jackson@ximian.com)
6 //
7
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using NUnit.Framework;
32 using System;
33 using System.IO;
34 using System.Drawing;
35 using System.Collections;
36 using System.Globalization;
37 using System.Web;
38 using System.Web.UI;
39 using System.Web.UI.WebControls;
40
41 namespace MonoTests.System.Web.UI.WebControls {
42
43         public class ListControlPoker : ListControl {
44
45                 public ListControlPoker ()
46                 {
47                 }
48
49                 public void TrackState ()
50                 {
51                         TrackViewState ();
52                 }
53
54                 public object SaveState ()
55                 {
56                         return SaveViewState ();
57                 }
58
59                 public void LoadState (object state)
60                 {
61                         LoadViewState (state);
62                 }
63
64                 public object ViewStateValue (string name)
65                 {
66                         return ViewState [name];
67                 }
68
69 #if NET_2_0
70                 public void LoadControl (object state)
71                 {
72                         LoadControlState (state);
73                 }
74
75                 public object SaveControl ()
76                 {
77                         return SaveControlState ();
78                 }
79 #endif
80                 public string Render ()
81                 {
82                         StringWriter sw = new StringWriter ();
83                         sw.NewLine = "\n";
84                         HtmlTextWriter writer = new HtmlTextWriter (sw);
85                         base.Render (writer);
86                         return writer.InnerWriter.ToString ();
87                 }
88
89 #if NET_2_0
90                 public HtmlTextWriterTag GetTagKey ()
91                 {
92                         return TagKey;
93                 }
94 #endif
95         }
96
97         [TestFixture]
98         public class ListControlTest {
99
100                 private Hashtable changed = new Hashtable ();
101
102                 [Test]
103                 public void DefaultProperties ()
104                 {
105                         ListControlPoker p = new ListControlPoker ();
106
107                         Assert.AreEqual (p.AutoPostBack, false, "A1");
108                         Assert.AreEqual (p.DataMember, String.Empty, "A2");
109                         Assert.AreEqual (p.DataSource, null, "A3");
110                         Assert.AreEqual (p.DataTextField, String.Empty, "A4");
111                         Assert.AreEqual (p.DataTextFormatString, String.Empty, "A5");
112                         Assert.AreEqual (p.DataValueField, String.Empty, "A6");
113                         Assert.AreEqual (p.Items.Count, 0, "A7");
114                         Assert.AreEqual (p.SelectedIndex, -1,"A8");
115                         Assert.AreEqual (p.SelectedItem, null, "A9");
116                         Assert.AreEqual (p.SelectedValue, String.Empty, "A10");
117 #if NET_2_0
118                         Assert.IsFalse (p.AppendDataBoundItems, "A11");
119                         Assert.AreEqual (p.Text, "", "A12");
120                         Assert.AreEqual (p.GetTagKey(), HtmlTextWriterTag.Select, "A13");
121 #endif
122                 }
123
124                 [Test]
125                 public void CleanProperties ()
126                 {
127                         ListControlPoker p = new ListControlPoker ();
128
129                         p.AutoPostBack = true;
130                         Assert.AreEqual (p.AutoPostBack, true, "A2");
131
132                         p.DataMember = "DataMember";
133                         Assert.AreEqual (p.DataMember, "DataMember", "A3");
134
135                         p.DataSource = "DataSource";
136                         Assert.AreEqual (p.DataSource, "DataSource", "A4");
137
138                         p.DataTextField = "DataTextField";
139                         Assert.AreEqual (p.DataTextField, "DataTextField", "A5");
140
141                         p.DataTextFormatString = "DataTextFormatString";
142                         Assert.AreEqual (p.DataTextFormatString, "DataTextFormatString", "A6");
143
144                         p.DataValueField = "DataValueField";
145                         Assert.AreEqual (p.DataValueField, "DataValueField", "A7");
146
147                         p.SelectedIndex = 10;
148                         Assert.AreEqual (p.SelectedIndex, -1, "A8");
149
150                         p.SelectedValue = "SelectedValue";
151                         Assert.AreEqual (p.SelectedValue, String.Empty, "A9");
152                 }
153
154                 [Test]
155                 public void NullProperties ()
156                 {
157                         ListControlPoker p = new ListControlPoker ();
158
159                         p.DataMember = null;
160                         Assert.AreEqual (p.DataMember, String.Empty, "A1");
161
162                         p.DataSource = null;
163                         Assert.AreEqual (p.DataSource, null, "A2");
164
165                         p.DataTextField = null;
166                         Assert.AreEqual (p.DataTextField, String.Empty, "A3");
167
168                         p.DataTextFormatString = null;
169                         Assert.AreEqual (p.DataTextFormatString, String.Empty, "A4");
170
171                         p.DataValueField = null;
172                         Assert.AreEqual (p.DataValueField, String.Empty, "A5");
173
174                         p.SelectedValue = null;
175                         Assert.AreEqual (p.SelectedValue, String.Empty, "A6");
176                 }
177
178                 [Test]
179                 public void ClearSelection ()
180                 {
181                         ListControlPoker p = new ListControlPoker ();
182
183                         ListItem foo = new ListItem ("foo");
184                         ListItem bar = new ListItem ("bar");
185
186                         BeginIndexChanged (p);
187
188                         p.Items.Add (foo);
189                         p.Items.Add (bar);
190                         p.SelectedIndex = 1;
191
192                         // sanity for the real test
193                         Assert.AreEqual (p.Items.Count, 2, "A1");
194                         Assert.AreEqual (p.SelectedIndex, 1, "A2");
195                         Assert.AreEqual (p.SelectedItem, bar, "A3");
196                         Assert.AreEqual (p.SelectedValue, bar.Value, "A4");
197                         
198                         p.ClearSelection ();
199
200                         Assert.AreEqual (p.SelectedIndex, -1, "A5");
201                         Assert.AreEqual (p.SelectedItem, null, "A6");
202                         Assert.AreEqual (p.SelectedValue, String.Empty, "A7");
203                         Assert.IsFalse (EndIndexChanged (p), "A8");
204
205                         // make sure we are still sane
206                         Assert.AreEqual (p.Items.Count, 2, "A9");
207                 }
208
209 #if NET_2_0
210                 [Test]
211                 // Tests Save/Load ControlState
212                 public void ControlState ()
213                 {
214                         ListControlPoker a = new ListControlPoker ();
215                         ListControlPoker b = new ListControlPoker ();
216
217                         a.TrackState ();
218
219                         a.Items.Add ("a");
220                         a.Items.Add ("b");
221                         a.Items.Add ("c");
222                         a.SelectedIndex = 2;
223
224                         b.Items.Add ("a");
225                         b.Items.Add ("b");
226                         b.Items.Add ("c");
227
228                         object state = a.SaveControl();
229                         b.LoadControl (state);
230
231                         Assert.AreEqual (b.SelectedIndex, 2, "A1");
232                 }
233 #endif
234
235                 [Test]
236                 // Tests Save/Load/Track ViewState
237                 public void ViewState ()
238                 {
239                         ListControlPoker a = new ListControlPoker ();
240                         ListControlPoker b = new ListControlPoker ();
241
242                         a.TrackState ();
243
244                         BeginIndexChanged (a);
245                         BeginIndexChanged (b);
246
247                         a.Items.Add ("a");
248                         a.Items.Add ("b");
249                         a.Items.Add ("c");
250                         a.SelectedIndex = 2;
251
252                         object state = a.SaveState ();
253                         b.LoadState (state);
254
255 #if NET_2_0
256                         Assert.AreEqual (b.SelectedIndex, -1, "A1");
257 #else
258                         Assert.AreEqual (b.SelectedIndex, 2, "A1");
259 #endif
260                         Assert.AreEqual (b.Items.Count, 3, "A2");
261
262                         Assert.AreEqual (b.Items [0].Value, "a", "A3");
263                         Assert.AreEqual (b.Items [1].Value, "b", "A4");
264                         Assert.AreEqual (b.Items [2].Value, "c", "A5");
265
266                         Assert.IsFalse (EndIndexChanged (a), "A6");
267                         Assert.IsFalse (EndIndexChanged (b), "A7");
268                 }
269
270                 [Test]
271                 public void ViewStateContents ()
272                 {
273                         ListControlPoker p = new ListControlPoker ();
274
275                         p.TrackState ();
276
277                         // So the selected index can be set
278                         p.Items.Add ("one");
279                         p.Items.Add ("two");
280
281                         p.AutoPostBack = false;
282                         p.DataMember = "DataMember";
283                         p.DataSource = "DataSource";
284                         p.DataTextField = "DataTextField";
285                         p.DataTextFormatString = "DataTextFormatString";
286                         p.DataValueField = "DataValueField";
287                         p.SelectedIndex = 1;
288 #if NET_2_0
289                         p.AppendDataBoundItems = true;
290                         p.Text = "Text";
291 #endif
292
293                         Assert.AreEqual (p.ViewStateValue ("AutoPostBack"), false, "A1");
294                         Assert.AreEqual (p.ViewStateValue ("DataMember"), "DataMember", "A2");
295
296                         Assert.AreEqual (p.ViewStateValue ("DataSource"), null, "A3");
297                         Assert.AreEqual (p.ViewStateValue ("DataTextField"), "DataTextField", "A4");
298                         Assert.AreEqual (p.ViewStateValue ("DataTextFormatString"),
299                                         "DataTextFormatString", "A5");
300                         Assert.AreEqual (p.ViewStateValue ("DataValueField"), "DataValueField", "A6");
301
302 #if NET_2_0
303                         Assert.AreEqual (p.ViewStateValue ("AppendDataBoundItems"), true, "A7");
304 #endif
305
306                         // None of these are saved
307                         Assert.AreEqual (p.ViewStateValue ("SelectedIndex"), null, "A8");
308                         Assert.AreEqual (p.ViewStateValue ("SelectedItem"), null, "A9");
309                         Assert.AreEqual (p.ViewStateValue ("SelectedValue"), null, "A10");
310 #if NET_2_0
311                         Assert.AreEqual (p.ViewStateValue ("Text"), null, "A11");
312 #endif
313
314                 }
315
316                 [Test]
317                 public void SelectedIndex ()
318                 {
319                         ListControlPoker p = new ListControlPoker ();
320
321                         p.Items.Add ("one");
322                         p.Items.Add ("two");
323                         p.Items.Add ("three");
324                         p.Items.Add ("four");
325
326                         p.Items [2].Selected = true;
327                         p.Items [1].Selected = true;
328
329                         Assert.AreEqual (p.SelectedIndex, 1, "A1");
330
331                         p.ClearSelection ();
332                         p.Items [3].Selected = true;
333
334                         Assert.AreEqual (p.SelectedIndex, 3, "A2");
335
336                         p.SelectedIndex = 1;
337                         Assert.AreEqual (p.SelectedIndex, 1, "A3");
338                         Assert.IsFalse (p.Items [3].Selected, "A4");
339                 }
340
341                 [Test]
342                 public void Render ()
343                 {
344                         ListControlPoker p = new ListControlPoker ();
345
346                         string s = p.Render ();
347                         string expected = "<select>\n\n</select>";
348                         Assert.AreEqual (s, expected, "A1");
349                 }
350
351                 [Test]
352 #if !NET_2_0
353                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
354 #endif
355                 public void ItemsTooHigh ()
356                 {
357                         ListControlPoker l = new ListControlPoker ();
358
359                         l.Items.Add ("foo");
360                         l.SelectedIndex = 1;
361                 }
362
363                 [Test]
364                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
365                 public void ItemsTooLow ()
366                 {
367                         ListControlPoker l = new ListControlPoker ();
368
369                         l.Items.Add ("foo");
370                         l.SelectedIndex = -2;
371                 }
372
373                 [Test]
374                 public void ItemsOk ()
375                 {
376                         ListControlPoker l = new ListControlPoker ();
377
378                         l.Items.Add ("foo");
379                         l.SelectedIndex = 0;
380                         l.SelectedIndex = -1;
381                 }
382
383                 private void BeginIndexChanged (ListControl l)
384                 {
385                         l.SelectedIndexChanged += new EventHandler (IndexChangedHandler);
386                 }
387
388                 private bool EndIndexChanged (ListControl l)
389                 {
390                         bool res = changed [l] != null;
391                         changed [l] = null;
392                         return res;
393                 }
394
395                 private void IndexChangedHandler (object sender, EventArgs e)
396                 {
397                         changed [sender] = new object ();
398                 }
399         }
400 }
401