RepeatInfoTest.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // RepeatInfoTest.cs - Unit tests for System.Web.UI.WebControls.RepeatInfo
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.ComponentModel;
  31. using System.Drawing;
  32. using System.IO;
  33. using System.Web;
  34. using System.Web.UI;
  35. using System.Web.UI.WebControls;
  36. using NUnit.Framework;
  37. namespace MonoTests.System.Web.UI.WebControls {
  38. public class RepeatInfoUser : IRepeatInfoUser {
  39. private bool footer;
  40. private bool header;
  41. private bool separators;
  42. private int count;
  43. private int counter;
  44. public RepeatInfoUser (bool footer, bool header, bool separators, int count)
  45. {
  46. counter = 0;
  47. this.footer = footer;
  48. this.header = header;
  49. this.separators = separators;
  50. this.count = count;
  51. }
  52. public bool HasFooter {
  53. get { return footer; }
  54. }
  55. public bool HasHeader {
  56. get { return header; }
  57. }
  58. public bool HasSeparators {
  59. get { return separators; }
  60. }
  61. public int RepeatedItemCount {
  62. get { return count; }
  63. }
  64. public Style GetItemStyle (ListItemType itemType, int repeatIndex)
  65. {
  66. return null;
  67. }
  68. public void RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
  69. {
  70. writer.Write ((counter++).ToString ());
  71. }
  72. }
  73. [TestFixture]
  74. public class RepeatInfoTest {
  75. private HtmlTextWriter GetWriter ()
  76. {
  77. StringWriter sw = new StringWriter ();
  78. sw.NewLine = "\n";
  79. return new HtmlTextWriter (sw);
  80. }
  81. string DoTest (int cols, int cnt, RepeatDirection d, RepeatLayout l, bool OuterTableImplied, bool ftr, bool hdr, bool sep)
  82. {
  83. HtmlTextWriter htw = GetWriter ();
  84. RepeatInfo ri = new RepeatInfo ();
  85. ri.RepeatColumns = cols;
  86. ri.RepeatDirection = d;
  87. ri.RepeatLayout = l;
  88. ri.OuterTableImplied = OuterTableImplied;
  89. ri.RenderRepeater (htw, new RepeatInfoUser (ftr, hdr, sep, cnt), new TableStyle (), new DataList ());
  90. return htw.InnerWriter.ToString ();
  91. }
  92. public void DefaultValues ()
  93. {
  94. RepeatInfo ri = new RepeatInfo ();
  95. Assert.AreEqual (0, ri.RepeatColumns, "RepeatColumns");
  96. Assert.AreEqual (RepeatDirection.Vertical, ri.RepeatDirection, "RepeatDirection");
  97. Assert.AreEqual (RepeatLayout.Table, ri.RepeatLayout, "RepeatLayout");
  98. Assert.IsFalse (ri.OuterTableImplied, "OuterTableImplied");
  99. }
  100. [Test]
  101. public void RepeatColumns_Negative ()
  102. {
  103. RepeatInfo ri = new RepeatInfo ();
  104. ri.RepeatColumns = -1;
  105. Assert.AreEqual (-1, ri.RepeatColumns, "-1");
  106. ri.RepeatColumns = Int32.MinValue;
  107. Assert.AreEqual (Int32.MinValue, ri.RepeatColumns, "Int32.MinValue");
  108. }
  109. [Test]
  110. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  111. public void RepeatDirection_Invalid ()
  112. {
  113. RepeatInfo ri = new RepeatInfo ();
  114. ri.RepeatDirection = (RepeatDirection) Int32.MinValue;
  115. }
  116. [Test]
  117. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  118. public void RepeatLayout_Invalid ()
  119. {
  120. RepeatInfo ri = new RepeatInfo ();
  121. ri.RepeatLayout = (RepeatLayout) Int32.MinValue;
  122. }
  123. private void RenderRepeater_BaseControl (string s, string msg, WebControl wc)
  124. {
  125. RepeatInfo ri = new RepeatInfo ();
  126. ri.RepeatColumns = 3;
  127. ri.RepeatDirection = RepeatDirection.Vertical;
  128. ri.RepeatLayout = RepeatLayout.Table;
  129. HtmlTextWriter writer = GetWriter ();
  130. ri.RenderRepeater (writer, new RepeatInfoUser (false, false, false, 1), new TableStyle (), wc);
  131. Assert.AreEqual (s, writer.InnerWriter.ToString (), msg);
  132. }
  133. [Test]
  134. [ExpectedException (typeof (NullReferenceException))]
  135. public void RenderRepeater_BaseControl_Null ()
  136. {
  137. RenderRepeater_BaseControl ("shouldn't get here", "null", null);
  138. }
  139. [Test]
  140. public void RenderRepeater_BaseControl ()
  141. {
  142. #if NET_2_0
  143. string noid = "<table border=\"0\">\n\t<tr>\n\t\t<td>0</td><td></td><td></td>\n\t</tr>\n</table>";
  144. string id_enabled = "<table id=\"foo\" disabled=\"disabled\" border=\"0\">\n\t<tr>\n\t\t<td>0</td><td></td><td></td>\n\t</tr>\n</table>";
  145. #else
  146. string noid = "<table border=\"0\">\n\t<tr>\n\t\t<td>0</td>\n\t</tr>\n</table>";
  147. string id_enabled = "<table id=\"foo\" disabled=\"disabled\" border=\"0\">\n\t<tr>\n\t\t<td>0</td>\n\t</tr>\n</table>";
  148. #endif
  149. RenderRepeater_BaseControl (noid, "Table", new Table ());
  150. RenderRepeater_BaseControl (noid, "DataList", new DataList ());
  151. RenderRepeater_BaseControl (noid, "DataListItem", new DataListItem (0, ListItemType.Item));
  152. Label l = new Label ();
  153. l.Enabled = false;
  154. l.ID = "foo";
  155. RenderRepeater_BaseControl (id_enabled, "id and disabled", l);
  156. }
  157. }
  158. }