2006-11-22 Igor Zelmanovich <igorz@mainsoft.com>
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / TableStyleTest.cs
1 //
2 // TableStyleTest.cs
3 //      - Unit tests for System.Web.UI.WebControls.TableStyle
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.IO;
32 using System.Web;
33 using System.Web.UI;
34 using System.Web.UI.WebControls;
35
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Web.UI.WebControls {
39
40         public class TestTableStyle : TableStyle {
41
42                 public TestTableStyle ()
43                         : base ()
44                 {
45                 }
46
47                 public TestTableStyle (StateBag bag)
48                         : base (bag)
49                 {
50                 }
51
52                 public bool Empty {
53                         get { return base.IsEmpty; }
54                 }
55
56                 public StateBag StateBag {
57                         get { return base.ViewState; }
58                 }
59
60                 public void LoadVS (object o)
61                 {
62                         LoadViewState (o);
63                 }
64
65                 public void TrackVS ()
66                 {
67                         TrackViewState ();
68                 }
69
70                 public object SaveVS ()
71                 {
72                         return SaveViewState ();
73                 }
74 #if NET_2_0
75                 public void Fill (CssStyleCollection attributes, IUrlResolutionService urlResolver)
76                 {
77                         base.FillStyleAttributes (attributes, urlResolver);
78                 }
79 #endif
80
81                 public bool SetBitCalledFlag = false;
82                 public int SetBitCalledValue = 0;
83                 protected override void SetBit (int bit) {
84                         SetBitCalledFlag = true;
85                         SetBitCalledValue = bit;
86                         base.SetBit (bit);
87                 }
88         }
89
90 #if NET_2_0
91         public class TestResolutionService : IUrlResolutionService {
92
93                 public string ResolveClientUrl (string relativeUrl)
94                 {
95                         return "http://www.mono-project.com";
96                 }
97         }
98 #endif
99
100         [TestFixture]
101         public class TableStyleTest {
102
103                 private const string imageUrl = "http://www.mono-project.com/stylesheets/images.wiki.png";
104
105                 private void DefaultProperties (TestTableStyle ts)
106                 {
107                         Assert.AreEqual (0, ts.StateBag.Count, "ViewState.Count");
108
109                         Assert.AreEqual (String.Empty, ts.BackImageUrl, "BackImageUrl");
110                         Assert.AreEqual (-1, ts.CellPadding, "CellPadding");
111                         Assert.AreEqual (-1, ts.CellSpacing, "CellSpacing");
112                         // LAMESPEC: default is document to be GridLines.Both
113                         Assert.AreEqual (GridLines.None, ts.GridLines, "GridLines");
114                         Assert.AreEqual (HorizontalAlign.NotSet, ts.HorizontalAlign, "HorizontalAlign");
115
116                         Assert.AreEqual (0, ts.StateBag.Count, "ViewState.Count-2");
117                         ts.Reset ();
118                         Assert.AreEqual (0, ts.StateBag.Count, "Reset");
119                 }
120
121                 private void NullProperties (TestTableStyle ts)
122                 {
123                         Assert.IsTrue (ts.Empty, "Empty");
124                         ts.BackImageUrl = String.Empty; // doesn't accept null, see specific test
125                         Assert.AreEqual (String.Empty, ts.BackImageUrl, "BackImageUrl");
126                         Assert.IsFalse (ts.Empty, "!Empty");
127
128                         ts.CellPadding = -1;
129                         Assert.AreEqual (-1, ts.CellPadding, "CellPadding");
130                         ts.CellSpacing = -1;
131                         Assert.AreEqual (-1, ts.CellSpacing, "CellSpacing");
132                         ts.GridLines = GridLines.None;
133                         Assert.AreEqual (GridLines.None, ts.GridLines, "GridLines");
134                         ts.HorizontalAlign = HorizontalAlign.NotSet;
135                         Assert.AreEqual (HorizontalAlign.NotSet, ts.HorizontalAlign, "HorizontalAlign");
136
137                         Assert.AreEqual (5, ts.StateBag.Count, "ViewState.Count-1");
138                         ts.Reset ();
139                         Assert.AreEqual (0, ts.StateBag.Count, "Reset");
140                         Assert.IsTrue (ts.Empty, "Empty/Reset");
141                 }
142
143                 [Test]
144                 public void Constructor_Default ()
145                 {
146                         TestTableStyle ts = new TestTableStyle ();
147                         DefaultProperties (ts);
148                         NullProperties (ts);
149                 }
150
151                 [Test]
152                 public void Constructor_StateBag_Null ()
153                 {
154                         TestTableStyle ts = new TestTableStyle (null);
155                         Assert.IsNotNull (ts.StateBag, "StateBag");
156                         DefaultProperties (ts);
157                         NullProperties (ts);
158                 }
159
160                 [Test]
161                 public void Constructor_StateBag ()
162                 {
163                         TestTableStyle ts = new TestTableStyle (new StateBag ());
164                         Assert.IsNotNull (ts.StateBag, "StateBag");
165                         DefaultProperties (ts);
166                         NullProperties (ts);
167                 }
168
169                 [Test]
170                 [ExpectedException (typeof (ArgumentNullException))]
171                 public void BackImageUrl_Null ()
172                 {
173                         TableStyle ts = new TableStyle ();
174                         ts.BackImageUrl = null;
175                 }
176
177                 [Test]
178                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
179                 public void CellPadding_Invalid ()
180                 {
181                         TableStyle ts = new TableStyle ();
182                         ts.CellPadding = Int32.MinValue;
183                 }
184
185                 [Test]
186                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
187                 public void CellSpacing_Invalid ()
188                 {
189                         TableStyle ts = new TableStyle ();
190                         ts.CellSpacing = Int32.MinValue;
191                 }
192
193                 [Test]
194                 // LAMESPEC: documented as ArgumentException
195                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
196                 public void GridLines_Invalid ()
197                 {
198                         TableStyle ts = new TableStyle ();
199                         ts.GridLines = (GridLines)Int32.MinValue;
200                 }
201
202                 [Test]
203                 // LAMESPEC: documented as ArgumentException
204                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
205                 public void HorizontalAlign_Invalid ()
206                 {
207                         TableStyle ts = new TableStyle ();
208                         ts.HorizontalAlign = (HorizontalAlign)Int32.MinValue;
209                 }
210
211                 [Test]
212                 public void AddAttributesToRender_Null_WebControl ()
213                 {
214                         TableStyle ts = new TableStyle ();
215                         ts.AddAttributesToRender (null, new Table ());
216                         // no exception
217                 }
218
219                 [Test]
220                 public void AddAttributesToRender_HtmlTextWriter_Null ()
221                 {
222                         TableStyle ts = GetTableStyle ();
223                         HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
224                         ts.AddAttributesToRender (writer, null);
225                         Assert.AreEqual (String.Empty, writer.InnerWriter.ToString (), "empty");
226                 }
227
228                 [Test]
229                 public void AddAttributesToRender ()
230                 {
231                         TableStyle ts = GetTableStyle ();
232                         HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
233                         ts.AddAttributesToRender (writer, new Table ());
234                         Assert.AreEqual (String.Empty, writer.InnerWriter.ToString (), "empty");
235                 }
236
237                 private TableStyle GetTableStyle ()
238                 {
239                         TableStyle ts = new TableStyle ();
240                         ts.BackImageUrl = imageUrl;
241                         ts.CellPadding = 1;
242                         ts.CellSpacing = 2;
243                         ts.GridLines = GridLines.Both;
244                         ts.HorizontalAlign = HorizontalAlign.Justify;
245                         return ts;
246                 }
247
248                 private void CheckTableStyle (TableStyle ts)
249                 {
250                         Assert.AreEqual (imageUrl, ts.BackImageUrl, "BackImageUrl");
251                         Assert.AreEqual (1, ts.CellPadding, "CellPadding");
252                         Assert.AreEqual (2, ts.CellSpacing, "CellSpacing");
253                         Assert.AreEqual (GridLines.Both, ts.GridLines, "GridLines");
254                         Assert.AreEqual (HorizontalAlign.Justify, ts.HorizontalAlign, "HorizontalAlign");
255                 }
256
257                 [Test]
258                 public void CopyFrom_Null ()
259                 {
260                         TableStyle ts = GetTableStyle ();
261                         ts.CopyFrom (null);
262                         CheckTableStyle (ts);
263                 }
264
265                 [Test]
266                 public void CopyFrom_Self ()
267                 {
268                         TableStyle ts = GetTableStyle ();
269                         ts.CopyFrom (ts);
270                         CheckTableStyle (ts);
271                 }
272
273                 [Test]
274                 public void CopyFrom_Empty ()
275                 {
276                         TestTableStyle ts = new TestTableStyle ();
277                         ts.CopyFrom (new TableStyle ());
278                         DefaultProperties (ts);
279                 }
280
281                 [Test]
282                 public void CopyFrom_IsEmpty ()
283                 {
284                         TestTableStyle c = new TestTableStyle ();
285                         TableStyle s = new TableStyle ();
286
287                         
288                         s.BorderWidth = Unit.Empty;
289                         c.CopyFrom (s);
290                         Assert.IsTrue (c.Empty, "A1");
291                         
292                         s.GridLines = GridLines.Both;
293                         c.CopyFrom (s);
294                         Assert.IsFalse (c.Empty, "A2");
295                 }
296
297                 [Test]
298                 public void CopyFrom ()
299                 {
300                         TableStyle ts = new TableStyle ();
301                         ts.BackImageUrl = imageUrl + "1";
302                         ts.CellPadding = 2;
303                         ts.CellSpacing = 3;
304                         ts.GridLines = GridLines.Horizontal;
305                         ts.HorizontalAlign = HorizontalAlign.Left;
306
307                         ts.CopyFrom (GetTableStyle ());
308                         CheckTableStyle (ts);
309                 }
310
311                 [Test]
312                 public void MergeWith_Null ()
313                 {
314                         TableStyle ts = GetTableStyle ();
315                         ts.MergeWith (null);
316                         CheckTableStyle (ts);
317                 }
318
319                 [Test]
320                 public void MergeWith_Self ()
321                 {
322                         TableStyle ts = GetTableStyle ();
323                         ts.MergeWith (ts);
324                         CheckTableStyle (ts);
325                 }
326
327                 [Test]
328                 public void MergeWith_Empty ()
329                 {
330                         TestTableStyle ts = new TestTableStyle ();
331                         ts.MergeWith (new TableStyle ());
332                         DefaultProperties (ts);
333                 }
334
335                 [Test]
336                 public void MergeWith ()
337                 {
338                         TableStyle ts = new TableStyle ();
339                         ts.BackImageUrl = imageUrl + "1";
340                         ts.CellPadding = 2;
341                         ts.CellSpacing = 3;
342                         ts.GridLines = GridLines.Horizontal;
343                         ts.HorizontalAlign = HorizontalAlign.Left;
344
345                         ts.MergeWith (GetTableStyle ());
346
347                         Assert.AreEqual (imageUrl + "1", ts.BackImageUrl, "BackImageUrl");
348                         Assert.AreEqual (2, ts.CellPadding, "CellPadding");
349                         Assert.AreEqual (3, ts.CellSpacing, "CellSpacing");
350                         Assert.AreEqual (GridLines.Horizontal, ts.GridLines, "GridLines");
351                         Assert.AreEqual (HorizontalAlign.Left, ts.HorizontalAlign, "HorizontalAlign");
352                 }
353
354                 [Test]
355                 public void GridLines_VS ()
356                 {
357                         TestTableStyle ts = new TestTableStyle ();
358                         ts.TrackVS ();
359                         ts.GridLines = GridLines.Both;
360                         object o = ts.SaveVS ();
361                         ts = new TestTableStyle ();
362                         ts.LoadVS (o);
363                         Assert.AreEqual (GridLines.Both, ts.GridLines, "GL");
364                 }
365 #if NET_2_0
366                 private CssStyleCollection GetCssCollection ()
367                 {
368                         return new AttributeCollection (new StateBag ()).CssStyle;
369                 }
370
371                 [Test]
372                 public void FillStyleAttributes_Null_Resolver ()
373                 {
374                         TestTableStyle ts = new TestTableStyle ();
375                         ts.Fill (null, new TestResolutionService ());
376                         // no exception
377                 }
378
379                 [Test]
380                 public void FillStyleAttributes_Css_Null ()
381                 {
382                         TestTableStyle ts = new TestTableStyle ();
383                         ts.Fill (GetCssCollection (), null);
384                         // no exception
385                 }
386
387                 [Test]
388                 public void FillStyleAttributes_Empty ()
389                 {
390                         CssStyleCollection css = GetCssCollection ();
391                         TestTableStyle ts = new TestTableStyle ();
392                         ts.Fill (css, new TestResolutionService ());
393                         Assert.AreEqual (0, css.Count, "Count");
394                 }
395
396                 [Test]
397                 public void FillStyleAttributes_NotCss ()
398                 {
399                         CssStyleCollection css = GetCssCollection ();
400                         TestTableStyle ts = new TestTableStyle ();
401                         ts.CellPadding = 1;
402                         ts.CellSpacing = 1;
403                         ts.GridLines = GridLines.Both;
404                         ts.HorizontalAlign = HorizontalAlign.Justify;
405                         ts.Fill (css, new TestResolutionService ());
406                         Assert.AreEqual (0, css.Count, "Count");
407                 }
408
409                 [Test]
410                 public void FillStyleAttributes_Css_WithoutResolution ()
411                 {
412                         CssStyleCollection css = GetCssCollection ();
413                         TestTableStyle ts = new TestTableStyle ();
414                         ts.BackImageUrl = "http://www.go-mono.com";
415                         ts.Fill (css, null);
416                         Assert.AreEqual (1, css.Count, "Count");
417                         Assert.AreEqual ("http://www.go-mono.com", css["background-image"], "css[string]");
418                         Assert.AreEqual ("http://www.go-mono.com", css[HtmlTextWriterStyle.BackgroundImage], "css[HtmlTextWriterStyle]");
419                         Assert.AreEqual ("background-image:url(http://www.go-mono.com);", css.Value, "css.Value");
420                 }
421
422                 [Test]
423                 public void FillStyleAttributes_Css_WithResolution ()
424                 {
425                         CssStyleCollection css = GetCssCollection ();
426                         TestTableStyle ts = new TestTableStyle ();
427                         ts.BackImageUrl = "http://www.go-mono.com";
428                         ts.Fill (css, new TestResolutionService ());
429                         Assert.AreEqual (1, css.Count, "Count");
430                         Assert.AreEqual ("http://www.mono-project.com", css["background-image"], "css[string]");
431                         Assert.AreEqual ("http://www.mono-project.com", css[HtmlTextWriterStyle.BackgroundImage], "css[HtmlTextWriterStyle]");
432                         Assert.AreEqual ("background-image:url(http://www.mono-project.com);", css.Value, "css.Value");
433                         Assert.AreEqual ("http://www.go-mono.com", ts.BackImageUrl, "BackImageUrl");
434                 }
435 #endif
436                 [Test]
437                 [Category ("NotWorking")]
438                 public void BackImageUrl ()
439                 {
440                         TableStyle ts = new TableStyle ();
441                         ts.BackImageUrl = "test 1.jpg";
442                         StringWriter sw = new StringWriter ();
443                         sw.NewLine = "\n";
444                         HtmlTextWriter htw = new HtmlTextWriter (sw);
445                         ts.AddAttributesToRender (htw);
446                         htw.RenderBeginTag ("tagName");
447                         string res = htw.InnerWriter.ToString ();
448                         string expected = "<tagName style=\"background-image:url(";
449 #if NET_2_0
450                         expected += "test%201.jpg";
451 #else
452                         expected += "test 1.jpg";
453 #endif
454                         expected += ");\">\n";
455                         Assert.AreEqual (expected, res);
456                 }
457
458                 [Test]
459                 public void SetBitCalledWhenSetProperty () {
460                         TestTableStyle s = new TestTableStyle ();
461
462                         s.SetBitCalledFlag = false;
463                         s.BackImageUrl = "http://www.mono-project.com";
464                         Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BackImageUrl");
465                         Assert.AreEqual (0x10000, s.SetBitCalledValue, "SetBit() was called with wrong argument : BackImageUrl");
466
467                         s.SetBitCalledFlag = false;
468                         s.CellPadding = 1;
469                         Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : CellPadding");
470                         Assert.AreEqual (0x20000, s.SetBitCalledValue, "SetBit() was called with wrong argument : CellPadding");
471
472                         s.SetBitCalledFlag = false;
473                         s.CellSpacing = 1;
474                         Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : CellSpacing");
475                         Assert.AreEqual (0x40000, s.SetBitCalledValue, "SetBit() was called with wrong argument : CellSpacing");
476
477                         s.SetBitCalledFlag = false;
478                         s.GridLines = GridLines.Vertical;
479                         Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : GridLines");
480                         Assert.AreEqual (0x80000, s.SetBitCalledValue, "SetBit() was called with wrong argument : GridLines");
481
482                         s.SetBitCalledFlag = false;
483                         s.HorizontalAlign = HorizontalAlign.Right;
484                         Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : HorizontalAlign");
485                         Assert.AreEqual (0x100000, s.SetBitCalledValue, "SetBit() was called with wrong argument : HorizontalAlign");
486                 }
487         }
488 }