2 // Tests for System.Web.UI.WebControls.ImageFieldTest.cs
5 // Yoni Klein (yonik@mainsoft.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.
32 using System.Collections.Generic;
36 using System.Web.UI.WebControls;
38 using System.Collections;
39 using System.Collections.Specialized;
40 using NUnit.Framework;
42 using MonoTests.stand_alone.WebHarness;
43 using MonoTests.SystemWeb.Framework;
44 using System.Threading;
47 namespace MonoTests.System.Web.UI.WebControls
49 class PokerImageField : ImageField
53 public PokerImageField ()
59 public object SaveState ()
61 return SaveViewState ();
64 public void LoadState (object o)
69 public StateBag StateBag
71 get { return base.ViewState; }
74 public Control GetControl
76 get { return base.Control; }
79 public void DoInitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
81 this.InitializeDataCell (cell, rowState);
84 public void DoCopyProperties (DataControlField newField)
86 base.CopyProperties (newField);
89 public DataControlField DoCreateField ()
91 return base.CreateField ();
94 public string DoFormatImageUrlValue (object dataValue)
96 return base.FormatImageUrlValue (dataValue);
99 public string DoGetDesignTimeValue ()
101 return base.GetDesignTimeValue ();
104 public string DoGetFormattedAlternateText (Control controlContainer)
106 return base.GetFormattedAlternateText (controlContainer);
111 public class ImageFieldTest
113 public const string BOOLFIELD = "bool";
114 public const string STRINGFIELD = "str";
115 enum DatatableType { nullDS, stringDS , emptyDS };
118 public void SetupTestCase ()
124 public void ImageField_DefaultProperty ()
126 ImageField field = new ImageField ();
127 Assert.AreEqual ("!", ImageField.ThisExpression, "ThisExpression");
128 Assert.AreEqual ("", field.AlternateText, "AlternateText ");
129 Assert.AreEqual (true, field.ConvertEmptyStringToNull, "ConvertEmptyStringToNull");
130 Assert.AreEqual ("", field.DataAlternateTextField, "DataAlternateTextField");
131 Assert.AreEqual ("", field.DataImageUrlField, "DataImageUrlField");
132 Assert.AreEqual ("", field.DataImageUrlFormatString, "DataImageUrlFormatString");
133 Assert.AreEqual ("", field.NullDisplayText, "NullDisplayText");
134 Assert.AreEqual ("", field.NullImageUrl, "NullImageUrl");
135 Assert.AreEqual (false, field.ReadOnly, "ReadOnly");
139 public void ImageField_AssignProperty ()
141 ImageField field = new ImageField ();
142 field.AlternateText = "test";
143 Assert.AreEqual ("test", field.AlternateText, "AlternateText ");
144 field.ConvertEmptyStringToNull = false;
145 Assert.AreEqual (false, field.ConvertEmptyStringToNull, "ConvertEmptyStringToNull");
146 field.DataAlternateTextField = "test";
147 Assert.AreEqual ("test", field.DataAlternateTextField, "DataAlternateTextField");
148 field.DataImageUrlField = "test";
149 Assert.AreEqual ("test", field.DataImageUrlField, "DataImageUrlField");
150 field.DataImageUrlFormatString = "test";
151 Assert.AreEqual ("test", field.DataImageUrlFormatString, "DataImageUrlFormatString");
152 field.NullDisplayText = "test";
153 Assert.AreEqual ("test", field.NullDisplayText, "NullDisplayText");
154 field.NullImageUrl = "test";
155 Assert.AreEqual ("test", field.NullImageUrl, "NullImageUrl");
156 field.ReadOnly = true;
157 Assert.AreEqual (true, field.ReadOnly, "ReadOnly");
161 public void ImageField_ExtractValuesFromCell ()
163 PokerImageField field = new PokerImageField ();
164 OrderedDictionary dictionary = new OrderedDictionary ();
165 Image image = new Image ();
166 image.ImageUrl = "test";
167 DataControlFieldCell cell = new DataControlFieldCell (null);
168 cell.Controls.Add (image);
169 field.ExtractValuesFromCell (dictionary, cell, DataControlRowState.Normal, true);
170 Assert.AreEqual (1, dictionary.Count, "ExtractValuesFromCellCount");
171 Assert.AreEqual ("test", dictionary[0].ToString (), "ExtractValuesFromCellValue");
172 cell.Controls.Clear ();
173 TextBox box = new TextBox ();
175 cell.Controls.Add (box);
176 field.ExtractValuesFromCell (dictionary, cell, DataControlRowState.Normal, true);
177 Assert.AreEqual (1, dictionary.Count, "ExtractValuesFromCellCount");
178 Assert.AreEqual ("test", dictionary[0].ToString (), "ExtractValuesFromCellValue");
182 public void ImageField_Initialize ()
184 Control control = new Control ();
186 PokerBoundField field = new PokerBoundField ();
187 bool result = field.Initialize (true, control);
188 Assert.AreEqual (false, result, "Initialize");
189 Assert.AreEqual ("test", field.GetControl.ID, "InitializeControl");
193 public void ImageField_InitializeDataCell()
195 PokerImageField field = new PokerImageField ();
196 DataControlFieldCell cell = new DataControlFieldCell(null);
197 field.DoInitializeDataCell(cell ,DataControlRowState.Normal);
198 Assert.AreEqual (0, cell.Controls.Count, "InitializeDataCellNormalNoData");
199 field.DoInitializeDataCell (cell, DataControlRowState.Alternate);
200 Assert.AreEqual (0, cell.Controls.Count, "InitializeDataCellAlternateNoData");
201 field.DoInitializeDataCell (cell, DataControlRowState.Edit);
202 Assert.AreEqual (1, cell.Controls.Count, "InitializeDataCellEditNoData");
203 TextBox box = cell.Controls[0] as TextBox;
205 Assert.Fail ("TextBox does not created on cell initilize");
207 cell.Controls.Clear ();
208 field.DataImageUrlField = "test";
209 field.DoInitializeDataCell (cell, DataControlRowState.Normal);
210 Assert.IsTrue (cell.Controls.Count > 0, "InitializeDataCellEditWithData");
212 Image image = cell.Controls[0] as Image;
214 Assert.Fail ("Image does not created on cell initilize");
216 cell.Controls.Clear ();
217 field.DoInitializeDataCell (cell, DataControlRowState.Insert);
218 Assert.AreEqual (1, cell.Controls.Count, "InitializeDataCellInsertWithData");
219 box = cell.Controls[0] as TextBox;
221 Assert.Fail ("Text does not created on cell initilize Insert RowState");
223 cell.Controls.Clear ();
224 field.DoInitializeDataCell (cell, DataControlRowState.Selected);
225 Assert.IsTrue (cell.Controls.Count > 0, "InitializeDataCellSelectedWithData");
229 public void ImageField_ValidateSupportsCallback ()
231 //This method has been implemented as an empty method
235 public void ImageField_CopyProperties ()
237 PokerImageField field = new PokerImageField ();
238 ImageField copy = new ImageField ();
239 field.AlternateText = "test";
240 field.ConvertEmptyStringToNull = true;
241 field.DataAlternateTextField = "test";
242 field.DataAlternateTextFormatString = "test";
243 field.DataImageUrlField = "test";
244 field.DataImageUrlFormatString = "test";
245 field.NullDisplayText = "test";
246 field.NullImageUrl = "test";
247 field.ReadOnly = true;
249 field.DoCopyProperties (copy);
250 Assert.AreEqual ("test", copy.AlternateText, "AlternateText");
251 Assert.AreEqual (true, copy.ConvertEmptyStringToNull, "ConvertEmptyStringToNull");
252 Assert.AreEqual ("test", copy.DataAlternateTextField, "DataAlternateTextField");
253 Assert.AreEqual ("test", copy.DataImageUrlField, "DataImageUrlField");
254 Assert.AreEqual ("test", copy.DataAlternateTextFormatString, "DataAlternateTextFormatString");
255 Assert.AreEqual ("test", copy.DataImageUrlField, "DataImageUrlField");
256 Assert.AreEqual ("test", copy.DataImageUrlFormatString, "DataImageUrlFormatString");
257 Assert.AreEqual ("test", copy.NullDisplayText, "NullDisplayText");
258 Assert.AreEqual ("test", copy.NullImageUrl, "NullImageUrl");
259 Assert.AreEqual (true, copy.ReadOnly, "ReadOnly");
263 public void ImageField_CreateField ()
265 PokerImageField field = new PokerImageField ();
266 DataControlField newfield = field.DoCreateField ();
267 if (!(newfield is ImageField)) {
268 Assert.Fail ("New ImageField was not created");
273 public void ImageField_FormatDataNavigateUrlValue ()
275 PokerImageField field = new PokerImageField ();
276 string result = field.DoFormatImageUrlValue (null);
277 Assert.AreEqual (null, result, "DoFormatImageUrlValueEmpty");
278 field.DataImageUrlFormatString = "-{0,8:G}-";
279 result = field.DoFormatImageUrlValue (10);
280 Assert.AreEqual ("- 10-", result, "FormatImageUrlValueWithData");
284 public void ImageField_GetDesignTimeValue ()
286 PokerImageField field = new PokerImageField ();
287 string result = field.DoGetDesignTimeValue ();
288 Assert.AreEqual ("Databound", result, "GetDesignTimeValue");
293 [Category("NunitWeb")]
294 public void ImageField_GetFormattedAlternateText ()
296 WebTest t = new WebTest ();
297 PageDelegates pd = new PageDelegates ();
298 pd.PreRender = _ImageFieldInit;
299 t.Invoker = new PageInvoker (pd);
301 string htmlPage = t.Run ();
302 string htmlOrigin = "<div>\r\n\t<table cellspacing=\"0\" rules=\"all\" border=\"1\" id=\"Grid\" style=\"border-collapse:collapse;\">\r\n\t\t<tr>\r\n\t\t\t<th scope=\"col\">Data</th>\r\n\t\t</tr><tr>\r\n\t\t\t<td><img src=\"Item%200\" alt=\"Item: Item 0\" /></td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><img src=\"Item%201\" alt=\"Item: Item 1\" /></td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><img src=\"Item%202\" alt=\"Item: Item 2\" /></td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><img src=\"Item%203\" alt=\"Item: Item 3\" /></td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><img src=\"Item%204\" alt=\"Item: Item 4\" /></td>\r\n\t\t</tr>\r\n\t</table>\r\n</div>";
303 string htmlControl = HtmlDiff.GetControlFromPageHtml (htmlPage);
304 HtmlDiff.AssertAreEqual (htmlOrigin, htmlControl, "GetFormattedAlternateText");
307 public static void _ImageFieldInit (Page p)
309 // This also tested DataAlternateTextField
310 LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
311 LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
312 GridView grid = new GridView();
313 grid.AutoGenerateColumns = false;
315 grid.DataSource = CreateDataSource (DatatableType.stringDS);
316 ImageField field = new ImageField();
317 field.DataImageUrlField = "Field";
318 field.DataAlternateTextField = "Field";
319 field.DataAlternateTextFormatString = "Item: {0}";
320 field.ReadOnly = true;
321 field.HeaderText = "Data" ;
322 grid.Columns.Add (field);
323 p.Form.Controls.Add (lcb);
324 p.Form.Controls.Add (grid);
325 p.Form.Controls.Add (lce);
330 [Category ("NunitWeb")]
331 public void ImageField_NullDisplayText ()
333 WebTest t = new WebTest ();
334 PageDelegates pd = new PageDelegates ();
335 pd.PreRender = _ImageFieldNullText;
336 t.Invoker = new PageInvoker (pd);
338 string htmlPage = t.Run ();
339 string htmlOrigin = "<div>\r\n\t<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">\r\n\t\t<tr>\r\n\t\t\t<th scope=\"col\">Data</th><th scope=\"col\">Field</th>\r\n\t\t</tr><tr>\r\n\t\t\t<td><span>NullDisplayText</span></td><td> </td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><span>NullDisplayText</span></td><td> </td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><span>NullDisplayText</span></td><td> </td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><span>NullDisplayText</span></td><td> </td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><span>NullDisplayText</span></td><td> </td>\r\n\t\t</tr>\r\n\t</table>\r\n</div>";
340 string htmlControl = HtmlDiff.GetControlFromPageHtml (htmlPage);
341 HtmlDiff.AssertAreEqual (htmlOrigin, htmlControl, "ImageFieldNullText");
344 public static void _ImageFieldNullText (Page p)
346 LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
347 LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
348 GridView grid = new GridView ();
349 grid.DataSource = CreateDataSource (DatatableType.nullDS);
350 ImageField field = new ImageField ();
351 field.NullDisplayText = "NullDisplayText";
352 field.DataImageUrlField = "Field";
353 field.ReadOnly = true;
354 field.HeaderText = "Data";
355 grid.Columns.Add (field);
356 p.Form.Controls.Add (lcb);
357 p.Form.Controls.Add (grid);
358 p.Form.Controls.Add (lce);
363 [Category ("NunitWeb")]
364 public void ImageField_ConvertEmptyStringToNull ()
366 WebTest t = new WebTest ();
367 PageDelegates pd = new PageDelegates ();
368 pd.PreRender = _ConvertEmptyStringToNull;
369 t.Invoker = new PageInvoker (pd);
370 string htmlPage = t.Run ();
371 string htmlOrigin = "<div>\r\n\t<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">\r\n\t\t<tr>\r\n\t\t\t<th scope=\"col\">Data</th><th scope=\"col\">Field</th>\r\n\t\t</tr><tr>\r\n\t\t\t<td><img src=\"\" /></td><td> </td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><img src=\"\" /></td><td> </td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><img src=\"\" /></td><td> </td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><img src=\"\" /></td><td> </td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><img src=\"\" /></td><td> </td>\r\n\t\t</tr>\r\n\t</table>\r\n</div>";
372 string htmlControl = HtmlDiff.GetControlFromPageHtml (htmlPage);
373 HtmlDiff.AssertAreEqual (htmlOrigin, htmlControl, "ConvertEmptyStringToNull");
376 public static void _ConvertEmptyStringToNull (Page p)
378 LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
379 LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
380 GridView grid = new GridView ();
381 grid.DataSource = CreateDataSource (DatatableType.emptyDS);
382 ImageField field = new ImageField ();
383 field.NullDisplayText = "NullDisplayText";
384 field.DataImageUrlField = "Field";
385 field.ConvertEmptyStringToNull = false;
386 field.ReadOnly = true;
387 field.HeaderText = "Data";
388 grid.Columns.Add (field);
389 p.Form.Controls.Add (lcb);
390 p.Form.Controls.Add (grid);
391 p.Form.Controls.Add (lce);
395 [TestFixtureTearDown]
396 public void TearDown ()
402 static ICollection CreateDataSource (DatatableType datatype)
404 DataTable dt = new DataTable ();
409 case DatatableType.stringDS:
410 dt.Columns.Add (new DataColumn ("Field", typeof (string)));
411 for (int i = 0; i < 5; i++) {
413 dr[0] = "Item " + i.ToString ();
417 case DatatableType.nullDS:
418 dt.Columns.Add (new DataColumn ("Field", typeof (string)));
419 for (int i = 0; i < 5; i++) {
420 dt.Rows.Add (dt.NewRow ());
423 case DatatableType.emptyDS:
424 dt.Columns.Add (new DataColumn ("Field", typeof (string)));
425 for (int i = 0; i < 5; i++) {
427 dr[0] = string.Empty ;
432 throw new ArgumentException ("Wrong data source type");
436 DataView dv = new DataView (dt);