3 // - Unit tests for System.Web.UI.WebControls.TableStyle
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
34 using System.Web.UI.WebControls;
36 using NUnit.Framework;
38 namespace MonoTests.System.Web.UI.WebControls {
40 public class TestTableStyle : TableStyle {
42 public TestTableStyle ()
47 public TestTableStyle (StateBag bag)
53 get { return base.IsEmpty; }
56 public StateBag StateBag {
57 get { return base.ViewState; }
60 public void LoadVS (object o)
65 public void TrackVS ()
70 public object SaveVS ()
72 return SaveViewState ();
75 public void Fill (CssStyleCollection attributes, IUrlResolutionService urlResolver)
77 base.FillStyleAttributes (attributes, urlResolver);
81 public bool SetBitCalledFlag = false;
82 public int SetBitCalledValue = 0;
83 protected override void SetBit (int bit) {
84 SetBitCalledFlag = true;
85 SetBitCalledValue = bit;
91 public class TestResolutionService : IUrlResolutionService {
93 public string ResolveClientUrl (string relativeUrl)
95 return "http://www.mono-project.com";
101 public class TableStyleTest {
103 private const string imageUrl = "http://www.mono-project.com/stylesheets/images.wiki.png";
105 private void DefaultProperties (TestTableStyle ts)
107 Assert.AreEqual (0, ts.StateBag.Count, "ViewState.Count");
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");
116 Assert.AreEqual (0, ts.StateBag.Count, "ViewState.Count-2");
118 Assert.AreEqual (0, ts.StateBag.Count, "Reset");
121 private void NullProperties (TestTableStyle ts)
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");
129 Assert.AreEqual (-1, ts.CellPadding, "CellPadding");
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");
137 Assert.AreEqual (5, ts.StateBag.Count, "ViewState.Count-1");
139 Assert.AreEqual (0, ts.StateBag.Count, "Reset");
140 Assert.IsTrue (ts.Empty, "Empty/Reset");
144 public void Constructor_Default ()
146 TestTableStyle ts = new TestTableStyle ();
147 DefaultProperties (ts);
152 public void Constructor_StateBag_Null ()
154 TestTableStyle ts = new TestTableStyle (null);
155 Assert.IsNotNull (ts.StateBag, "StateBag");
156 DefaultProperties (ts);
161 public void Constructor_StateBag ()
163 TestTableStyle ts = new TestTableStyle (new StateBag ());
164 Assert.IsNotNull (ts.StateBag, "StateBag");
165 DefaultProperties (ts);
170 [ExpectedException (typeof (ArgumentNullException))]
171 public void BackImageUrl_Null ()
173 TableStyle ts = new TableStyle ();
174 ts.BackImageUrl = null;
178 [ExpectedException (typeof (ArgumentOutOfRangeException))]
179 public void CellPadding_Invalid ()
181 TableStyle ts = new TableStyle ();
182 ts.CellPadding = Int32.MinValue;
186 [ExpectedException (typeof (ArgumentOutOfRangeException))]
187 public void CellSpacing_Invalid ()
189 TableStyle ts = new TableStyle ();
190 ts.CellSpacing = Int32.MinValue;
194 // LAMESPEC: documented as ArgumentException
195 [ExpectedException (typeof (ArgumentOutOfRangeException))]
196 public void GridLines_Invalid ()
198 TableStyle ts = new TableStyle ();
199 ts.GridLines = (GridLines)Int32.MinValue;
203 // LAMESPEC: documented as ArgumentException
204 [ExpectedException (typeof (ArgumentOutOfRangeException))]
205 public void HorizontalAlign_Invalid ()
207 TableStyle ts = new TableStyle ();
208 ts.HorizontalAlign = (HorizontalAlign)Int32.MinValue;
212 public void AddAttributesToRender_Null_WebControl ()
214 TableStyle ts = new TableStyle ();
215 ts.AddAttributesToRender (null, new Table ());
220 public void AddAttributesToRender_HtmlTextWriter_Null ()
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");
229 public void AddAttributesToRender ()
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");
237 private TableStyle GetTableStyle ()
239 TableStyle ts = new TableStyle ();
240 ts.BackImageUrl = imageUrl;
243 ts.GridLines = GridLines.Both;
244 ts.HorizontalAlign = HorizontalAlign.Justify;
248 private void CheckTableStyle (TableStyle ts)
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");
258 public void CopyFrom_Null ()
260 TableStyle ts = GetTableStyle ();
262 CheckTableStyle (ts);
266 public void CopyFrom_Self ()
268 TableStyle ts = GetTableStyle ();
270 CheckTableStyle (ts);
274 public void CopyFrom_Empty ()
276 TestTableStyle ts = new TestTableStyle ();
277 ts.CopyFrom (new TableStyle ());
278 DefaultProperties (ts);
282 public void CopyFrom_IsEmpty ()
284 TestTableStyle c = new TestTableStyle ();
285 TableStyle s = new TableStyle ();
288 s.BorderWidth = Unit.Empty;
290 Assert.IsTrue (c.Empty, "A1");
292 s.GridLines = GridLines.Both;
294 Assert.IsFalse (c.Empty, "A2");
298 public void CopyFrom ()
300 TableStyle ts = new TableStyle ();
301 ts.BackImageUrl = imageUrl + "1";
304 ts.GridLines = GridLines.Horizontal;
305 ts.HorizontalAlign = HorizontalAlign.Left;
307 ts.CopyFrom (GetTableStyle ());
308 CheckTableStyle (ts);
312 public void MergeWith_Null ()
314 TableStyle ts = GetTableStyle ();
316 CheckTableStyle (ts);
320 public void MergeWith_Self ()
322 TableStyle ts = GetTableStyle ();
324 CheckTableStyle (ts);
328 public void MergeWith_Empty ()
330 TestTableStyle ts = new TestTableStyle ();
331 ts.MergeWith (new TableStyle ());
332 DefaultProperties (ts);
336 public void MergeWith ()
338 TableStyle ts = new TableStyle ();
339 ts.BackImageUrl = imageUrl + "1";
342 ts.GridLines = GridLines.Horizontal;
343 ts.HorizontalAlign = HorizontalAlign.Left;
345 ts.MergeWith (GetTableStyle ());
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");
355 public void GridLines_VS ()
357 TestTableStyle ts = new TestTableStyle ();
359 ts.GridLines = GridLines.Both;
360 object o = ts.SaveVS ();
361 ts = new TestTableStyle ();
363 Assert.AreEqual (GridLines.Both, ts.GridLines, "GL");
366 private CssStyleCollection GetCssCollection ()
368 return new AttributeCollection (new StateBag ()).CssStyle;
372 public void FillStyleAttributes_Null_Resolver ()
374 TestTableStyle ts = new TestTableStyle ();
375 ts.Fill (null, new TestResolutionService ());
380 public void FillStyleAttributes_Css_Null ()
382 TestTableStyle ts = new TestTableStyle ();
383 ts.Fill (GetCssCollection (), null);
388 public void FillStyleAttributes_Empty ()
390 CssStyleCollection css = GetCssCollection ();
391 TestTableStyle ts = new TestTableStyle ();
392 ts.Fill (css, new TestResolutionService ());
393 Assert.AreEqual (0, css.Count, "Count");
397 public void FillStyleAttributes_NotCss ()
399 CssStyleCollection css = GetCssCollection ();
400 TestTableStyle ts = new TestTableStyle ();
403 ts.GridLines = GridLines.Both;
404 ts.HorizontalAlign = HorizontalAlign.Justify;
405 ts.Fill (css, new TestResolutionService ());
406 Assert.AreEqual (0, css.Count, "Count");
410 public void FillStyleAttributes_Css_WithoutResolution ()
412 CssStyleCollection css = GetCssCollection ();
413 TestTableStyle ts = new TestTableStyle ();
414 ts.BackImageUrl = "http://www.go-mono.com";
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");
423 public void FillStyleAttributes_Css_WithResolution ()
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");
437 [Category ("NotWorking")]
438 public void BackImageUrl ()
440 TableStyle ts = new TableStyle ();
441 ts.BackImageUrl = "test 1.jpg";
442 StringWriter sw = new StringWriter ();
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(";
450 expected += "test%201.jpg";
452 expected += "test 1.jpg";
454 expected += ");\">\n";
455 Assert.AreEqual (expected, res);
459 public void SetBitCalledWhenSetProperty () {
460 TestTableStyle s = new TestTableStyle ();
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");
467 s.SetBitCalledFlag = false;
469 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : CellPadding");
470 Assert.AreEqual (0x20000, s.SetBitCalledValue, "SetBit() was called with wrong argument : CellPadding");
472 s.SetBitCalledFlag = false;
474 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : CellSpacing");
475 Assert.AreEqual (0x40000, s.SetBitCalledValue, "SetBit() was called with wrong argument : CellSpacing");
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");
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");