Fix problems with overlong directory names: phase #1
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / BoundFieldTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.BoundFieldTest.cs
3 //
4 // Author:
5 //      Yoni Klein (yonik@mainsoft.com)
6 //
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 #if NET_2_0
31
32 using System;
33 using System.Collections.Generic;
34 using System.Text;
35 using System.Web;
36 using System.Web.UI;
37 using System.Web.UI.WebControls;
38 using System.IO;
39 using System.Drawing;
40 using System.Collections;
41 using System.Collections.Specialized;
42 using Image = System.Web.UI.WebControls.Image;
43 using NUnit.Framework;
44 using System.Globalization;\r
45 using MonoTests.SystemWeb.Framework;\r
46 using MonoTests.stand_alone.WebHarness;
47
48
49
50 namespace MonoTests.System.Web.UI.WebControls
51 {
52         class PokerBoundField : BoundField
53         {
54                 public Button bindbutoon;
55
56                 public PokerBoundField () {
57                         TrackViewState ();
58                         bindbutoon = new Button ();
59                         bindbutoon.DataBinding += new EventHandler (OnDataBindField);
60                 }
61
62
63                 public StateBag StateBag {
64                         get { return base.ViewState; }
65                 }
66
67                 public bool DoSupportsHtmlEncode {
68                         get {
69                                 return base.SupportsHtmlEncode;
70                         }
71                 }
72
73                 public void DoCopyProperties (DataControlField newField) {
74                         base.CopyProperties (newField);
75                 }
76
77                 public DataControlField DoCreateField () {
78                         return base.CreateField ();
79                 }
80
81                 public string DoFormatDataValue (object dataValue, bool encode) {
82                         return this.FormatDataValue (dataValue, encode);
83                 }
84
85                 public object DoGetDesignTimeValue () {
86                         return base.GetDesignTimeValue ();
87                 }
88
89                 public object DoGetValue (Control controlContainer) {
90                         return base.GetValue (controlContainer);
91                 }
92
93                 public void DoInitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState) {
94                         base.InitializeDataCell (cell, rowState);
95                 }
96
97                 public void DoOnDataBindField (object sender, EventArgs e) {
98                         base.OnDataBindField (sender, e);
99                 }
100
101                 public Control GetControl {
102                         get { return base.Control; }
103                 }
104         }\r
105 \r
106 \r
107         [Serializable]
108         [TestFixture]
109         public class BoundFieldTest
110         {
111                 [Test]
112                 public void BoundField_DefaultProperty () {
113                         PokerBoundField bf = new PokerBoundField ();
114                         Assert.AreEqual ("!", PokerBoundField.ThisExpression, "StaticThisExpression");
115                         Assert.AreEqual ("", bf.DataField, "DataField");
116                         Assert.AreEqual ("", bf.DataFormatString, "DataFormatString");
117                         Assert.AreEqual ("", bf.HeaderText, "HeaderText");
118                         Assert.AreEqual (true, bf.HtmlEncode, "HtmlEncode");
119                         Assert.AreEqual ("", bf.NullDisplayText, "NullDisplayText");
120                         Assert.AreEqual (false, bf.ReadOnly, "ReadOnly");
121
122                         //Protected 
123                         Assert.AreEqual (true, bf.DoSupportsHtmlEncode, "SupportsHtmlEncode");
124                 }
125
126                 [Test]
127                 public void BoundField_DefaultPropertyNotWorking () {
128                         PokerBoundField bf = new PokerBoundField ();
129                         Assert.AreEqual (false, bf.ApplyFormatInEditMode, "ApplyFormatInEditMode");
130                         Assert.AreEqual (true, bf.ConvertEmptyStringToNull, "ConvertEmptyStringToNull");
131                 }
132
133                 [Test]
134                 public void BoundField_AssignProperty () {
135                         PokerBoundField bf = new PokerBoundField ();
136                         bf.ConvertEmptyStringToNull = false;
137                         Assert.AreEqual (false, bf.ConvertEmptyStringToNull, "ConvertEmptyStringToNull");
138                         bf.DataField = "test";
139                         Assert.AreEqual ("test", bf.DataField, "DataField");
140                         bf.DataFormatString = "test";
141                         Assert.AreEqual ("test", bf.DataFormatString, "DataFormatString");
142                         bf.HeaderText = "test";
143                         Assert.AreEqual ("test", bf.HeaderText, "HeaderText");
144                         bf.HtmlEncode = false;
145                         Assert.AreEqual (false, bf.HtmlEncode, "HtmlEncode");
146                         bf.NullDisplayText = "test";
147                         Assert.AreEqual ("test", bf.NullDisplayText, "NullDisplayText");
148                         bf.ReadOnly = true;
149                         Assert.AreEqual (true, bf.ReadOnly, "ReadOnly");
150                 }
151
152                 [Test]
153                 public void BoundField_AssignPropertyNotWorking () {
154                         PokerBoundField bf = new PokerBoundField ();
155                         bf.ApplyFormatInEditMode = true;
156                         Assert.AreEqual (true, bf.ApplyFormatInEditMode, "ApplyFormatInEditMode");
157                 }
158
159                 [Test]
160                 public void BoundField_ExtractValuesFromCell () {
161                         PokerBoundField bf = new PokerBoundField ();
162                         OrderedDictionary dictionary = new OrderedDictionary ();
163                         DataControlFieldCell cell = new DataControlFieldCell (null);
164                         cell.Text = "test";
165                         bf.ExtractValuesFromCell (dictionary, cell, DataControlRowState.Normal, true);
166                         Assert.AreEqual (1, dictionary.Count, "ExtractValuesFromCellCount");
167                         Assert.AreEqual ("test", dictionary [0].ToString (), "ExtractValuesFromCellValue");
168                 }
169
170                 [Test]
171                 public void BoundField_Initialize () {
172                         // This method initilize to private fields in a base class DataControlField 
173                         // Always return false
174                         PokerBoundField bf = new PokerBoundField ();
175                         Control control = new Control ();
176                         control.ID = "test";
177                         bool res = bf.Initialize (true, control);
178                         // Assert.AreEqual (false, res, "InitializeResult");
179                         Assert.AreEqual ("test", bf.GetControl.ID, "InitializeControl");
180                 }
181
182                 [Test]
183                 public void BoundField_InitializeCell () {
184                         PokerBoundField bf = new PokerBoundField ();
185                         // Header text
186                         DataControlFieldCell cell = new DataControlFieldCell (null);
187                         bf.HeaderText = "headertext";
188
189                         bf.InitializeCell (cell, DataControlCellType.Header, DataControlRowState.Edit, 1);
190                         Assert.AreEqual ("headertext", cell.Text, "InitializeCellHeaderText");
191                         // Empty header text
192                         bf.HeaderText = "";
193                         bf.InitializeCell (cell, DataControlCellType.Header, DataControlRowState.Edit, 1);
194                         Assert.AreEqual (" ", cell.Text, "InitializeCellEmpty");
195
196                         bf.HeaderText = "headertext";
197                         // Header image url not empty
198                         bf.HeaderImageUrl = "headerurl";
199                         bf.InitializeCell (cell, DataControlCellType.Header, DataControlRowState.Edit, 1);
200                         if (cell.Controls [0] is Image) {
201                                 Image image = (Image) cell.Controls [0];
202                                 Assert.AreEqual ("headerurl", image.ImageUrl, "InitializeCellHeaderImageUrl");
203                         }
204                         else {
205                                 Assert.Fail ("Header Image dos not created");
206                         }
207
208                         // Footer empty
209                         bf.FooterText = "footertext";
210                         bf.InitializeCell (cell, DataControlCellType.Footer, DataControlRowState.Edit, 1);
211                         Assert.AreEqual ("footertext", cell.Text, "InitializeCellFooterText");
212                         bf.FooterText = "";
213                         bf.InitializeCell (cell, DataControlCellType.Footer, DataControlRowState.Edit, 1);
214                         Assert.AreEqual (" ", cell.Text, "InitializeCellFooterEmpty");
215                 }
216
217                 [Test]
218                 public void BoundField_ValidateSupportsCallback () {
219                         //This method has been implemented as an empty method           
220                 }
221
222                 [Test]
223                 public void BoundField_SortExpression () {
224                         // Sorting enable = true , link button must be created
225                         PokerBoundField bf = new PokerBoundField ();
226                         // Header text
227                         DataControlFieldCell cell = new DataControlFieldCell (null);
228
229                         bf.HeaderImageUrl = "";
230                         bf.SortExpression = "a";
231                         bf.HeaderText = "sortbutton";
232                         bf.Initialize (true, new Control ());    // _base._sortingenable set to true
233                         bf.InitializeCell (cell, DataControlCellType.Header, DataControlRowState.Edit, 1);
234                         if (cell.Controls [0] is Button) { // mono
235                                 Button lb = (Button) cell.Controls [0];
236                                 Assert.AreEqual ("Sort", lb.CommandName, "InitializeCellHeaderSortButtonCommand");
237                                 Assert.AreEqual ("a", lb.CommandArgument, "InitializeCellHeaderSortButtonArgument");
238                                 Assert.AreEqual ("sortbutton", lb.Text, "InitializeCellHeaderSortButtonText");
239
240                         }
241                         else if (cell.Controls [0] is LinkButton) { // .net
242                                 LinkButton lb = (LinkButton) cell.Controls [0];
243                                 Assert.AreEqual ("Sort", lb.CommandName, "InitializeCellHeaderSortButtonCommand");
244                                 Assert.AreEqual ("a", lb.CommandArgument, "InitializeCellHeaderSortButtonArgument");
245                                 Assert.AreEqual ("sortbutton", lb.Text, "InitializeCellHeaderSortButtonText");
246
247                         }
248                         else {
249                                 Assert.Fail ("Sort button does not created");
250                         }
251                 }
252
253
254                 [Test]
255                 public void BoundField_CopyProperties () {
256                         PokerBoundField bf = new PokerBoundField ();
257                         BoundField copy = new BoundField ();
258                         // Look not working property
259                         // bf.ApplyFormatInEditMode = true;
260                         bf.ConvertEmptyStringToNull = true;
261                         bf.DataField = "test";
262                         bf.DataFormatString = "test";
263                         bf.HtmlEncode = true;
264                         bf.NullDisplayText = "test";
265                         bf.ReadOnly = true;
266                         bf.DoCopyProperties (copy);
267                         // Look not working property
268                         // Assert.AreEqual (true, copy.ApplyFormatInEditMode, "ApplyFormatInEditMode");
269                         Assert.AreEqual (true, copy.ConvertEmptyStringToNull, "ConvertEmptyStringToNull");
270                         Assert.AreEqual ("test", copy.DataField, "DataField");
271                         Assert.AreEqual ("test", copy.DataFormatString, "DataFormatString");
272                         Assert.AreEqual (true, copy.HtmlEncode, "HtmlEncode");
273                         Assert.AreEqual ("test", copy.NullDisplayText, "NullDisplayText");
274                         Assert.AreEqual (true, copy.ReadOnly, "ReadOnly");
275                 }
276
277                 [Test]
278                 public void BoundField_CreateField () {
279                         PokerBoundField bf = new PokerBoundField ();
280                         BoundField newfield = (BoundField) bf.DoCreateField ();
281                         Assert.IsNotNull (newfield, "CreateField");
282                 }
283
284                 [Test]
285                 public void BoundField_FormatDataValue () {
286                         string result;
287                         PokerBoundField bf = new PokerBoundField ();
288
289                         bf.NullDisplayText = "NullDisplayText";
290                         result = bf.DoFormatDataValue (null, false);
291                         Assert.AreEqual ("NullDisplayText", result, "FormatDataValueNullDataValue");
292
293                         result = bf.DoFormatDataValue ("test", true);
294                         Assert.AreEqual ("test", result, "FormatDataValueTextDataValue");
295
296                         result = bf.DoFormatDataValue ("", true);
297                         Assert.AreEqual ("NullDisplayText", result, "FormatEmptyDataValue");
298
299                         bf.DataFormatString = "-{0,8:G}-";
300                         result = bf.DoFormatDataValue (10, false);
301                         Assert.AreEqual ("-      10-", result, "FormatDataValueWithFormat");
302                 }
303
304                 [Test]
305                 [Category ("NotWorking")]
306                 public void BoundField_GetDesignTimeValue () {
307                         string result;
308                         PokerBoundField bf = new PokerBoundField ();
309                         result = (string) bf.DoGetDesignTimeValue ();
310                         Assert.AreEqual ("Databound", result, "GetDesignTimeValue");
311                 }
312
313                 [Test]
314                 [ExpectedException(typeof(HttpException), "A data item was not found in the container. The container must either implement IDataItemContainer, or have a property named DataItem.")]
315                 public void BoundField_GetValueNull () {
316                         PokerBoundField bf = new PokerBoundField ();
317                         SimpleSpreadsheetRow ds = new SimpleSpreadsheetRow (0, null);
318                         bf.DataField = PokerBoundField.ThisExpression;
319                         string result = (string) bf.DoGetValue (ds);
320                 }
321
322                 [Test]
323                 public void BoundField_GetValue () {
324                         PokerBoundField bf = new PokerBoundField ();
325                         SimpleSpreadsheetRow ds = new SimpleSpreadsheetRow (0, "test");
326                         bf.DataField = PokerBoundField.ThisExpression;
327                         string result = (string) bf.DoGetValue (ds);
328                         Assert.AreEqual ("test", result, "GetValueFromIDataItemContainer");
329                 }
330
331                 [Test]
332                 public void BoundField_GetValueDataItem () {
333                         PokerBoundField bf = new PokerBoundField ();
334                         ControlWithDataItem ds = new ControlWithDataItem ("test");
335                         bf.DataField = PokerBoundField.ThisExpression;
336                         string result = (string) bf.DoGetValue (ds);
337                         Assert.AreEqual ("test", result, "GetValueFromIDataItemContainer");
338                 }
339
340                 [Test]
341                 public void BoundField_InitializeDataCell () {
342                         PokerBoundField bf = new PokerBoundField ();
343                         bf.HeaderText = "headertest";
344                         DataControlFieldCell cell = new DataControlFieldCell (null);
345                         DataControlRowState state = DataControlRowState.Edit;
346                         Assert.AreEqual (0, cell.Controls.Count, "InitializeDataCellControlsBeforeInit");
347                         bf.DoInitializeDataCell (cell, state);
348                         Assert.AreEqual (1, cell.Controls.Count, "InitializeDataCellControlsAfterInit");
349                 }
350
351                 [Test]
352                 [ExpectedException (typeof (HttpException))]
353                 public void BoundField_OnDataBindFieldExeption () {
354                         PokerBoundField bf = new PokerBoundField ();
355                         bf.bindbutoon.DataBind ();
356
357                 }
358
359                 [Test]
360                 [ExpectedException (typeof (HttpException))]
361                 public void BoundField_GetValueExeption () {
362                         PokerBoundField bf = new PokerBoundField ();
363                         bf.DoGetValue (null);
364                 }\r
365 \r
366                 [Test]\r
367                 [Category ("NunitWeb")]\r
368                 public void BoundField_NullValueRender ()\r
369                 {\r
370                         string html = new WebTest (PageInvoker.CreateOnLoad (new PageDelegate (BasicRenderTestInit))).Run ();\r
371                         string orightml = @"<div>\r
372                                 <table cellspacing=""0"" rules=""all"" border=""1"" id=""GridView1"" style=""border-collapse:collapse;"">\r
373                                         <tr>\r
374                                                 <th scope=""col"">&nbsp;</th><th scope=""col"">&nbsp;</th>\r
375                                         </tr><tr>\r
376                                                 <td>Norway</td><td>Norway</td>\r
377                                         </tr><tr>\r
378                                                 <td>Sweden</td><td>Sweden</td>\r
379                                         </tr><tr>\r
380                                                 <td>EMPTY</td><td>&nbsp;</td>\r
381                                         </tr><tr>\r
382                                                 <td>Italy</td><td>Italy</td>\r
383                                         </tr>\r
384                                 </table>\r
385                                 </div>";\r
386                         html = HtmlDiff.GetControlFromPageHtml (html);\r
387                         HtmlDiff.AssertAreEqual (orightml, html, "NullValueRender");\r
388                 }\r
389 \r
390                 public static void BasicRenderTestInit (Page p)\r
391                 {\r
392                         ArrayList myds = new ArrayList ();\r
393                         myds.Add (new myds_data ("Norway"));\r
394                         myds.Add (new myds_data ("Sweden"));\r
395                         myds.Add (new myds_data (""));\r
396                         myds.Add (new myds_data ("Italy"));\r
397 \r
398                         BoundField bf = new BoundField ();\r
399                         bf.DataField = "Field1";\r
400                         bf.NullDisplayText = "EMPTY";\r
401 \r
402                         BoundField bf2 = new BoundField ();\r
403                         bf2.DataField = "Field1";\r
404 \r
405                         GridView GridView1 = new GridView();\r
406                         GridView1.AutoGenerateColumns = false;\r
407                         GridView1.Columns.Add (bf);\r
408                         GridView1.Columns.Add (bf2);\r
409                         GridView1.DataSource = myds;\r
410                         GridView1.DataBind ();\r
411 \r
412                         LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);\r
413                         LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);\r
414 \r
415                         p.Form.Controls.Add (lcb);\r
416                         p.Form.Controls.Add (GridView1);\r
417                         p.Form.Controls.Add (lce);\r
418                 }\r
419 \r
420                 class myds_data\r
421                 {\r
422                         string _s = "";\r
423                         public myds_data (string s)\r
424                         {\r
425                                 _s = s;\r
426                         }\r
427 \r
428                         public string Field1\r
429                         {\r
430                                 get { return _s; }\r
431                         }\r
432                 }\r
433 \r
434                 
435                 class ControlWithDataItem : Control
436                 {
437                         readonly object _data;
438                         public ControlWithDataItem (object data) {
439                                 _data = data;
440                         }
441
442                         public object DataItem {
443                                 get {
444                                         return _data;
445                                 }
446                         }
447                 }
448
449                 public class SimpleSpreadsheetRow : TableRow, IDataItemContainer
450                 {
451                         private object data;
452                         private int _itemIndex;
453
454                         public SimpleSpreadsheetRow (int itemIndex, object o) {
455                                 data = o;
456                                 _itemIndex = itemIndex;
457                         }
458
459                         public virtual object Data {
460                                 get {
461                                         return data;
462                                 }
463                         }
464
465                         object IDataItemContainer.DataItem {
466                                 get {
467                                         return Data;
468                                 }
469                         }
470
471                         int IDataItemContainer.DataItemIndex {
472                                 get {
473                                         return _itemIndex;
474                                 }
475                         }
476
477                         int IDataItemContainer.DisplayIndex {
478                                 get {
479                                         return _itemIndex;
480                                 }
481                         }
482                 }
483         }
484 }
485 #endif