DropDownListTest.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. //
  2. // Tests for System.Web.UI.WebControls.Style.cs
  3. //
  4. // Author:
  5. // Peter Dennis Bartok ([email protected])
  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. using NUnit.Framework;
  30. using System;
  31. using System.Collections;
  32. using System.Drawing;
  33. using System.IO;
  34. using System.Data;
  35. using System.Globalization;
  36. using System.Web;
  37. using System.Web.UI;
  38. using System.Web.UI.WebControls;
  39. namespace MonoTests.System.Web.UI.WebControls
  40. {
  41. [TestFixture]
  42. public class DropDownListTest {
  43. public class NamingContainer : WebControl, INamingContainer {
  44. }
  45. public class DropDownListTestClass : DropDownList {
  46. public DropDownListTestClass ()
  47. : base () {
  48. }
  49. public StateBag StateBag {
  50. get { return base.ViewState; }
  51. }
  52. public string Render () {
  53. HtmlTextWriter writer;
  54. writer = DropDownListTest.GetWriter();
  55. base.Render (writer);
  56. return writer.InnerWriter.ToString ();
  57. }
  58. public bool IsTrackingVS () {
  59. return IsTrackingViewState;
  60. }
  61. public void SetTrackingVS () {
  62. TrackViewState ();
  63. }
  64. public object Save() {
  65. return base.SaveViewState();
  66. }
  67. public void Load(object o) {
  68. base.LoadViewState(o);
  69. }
  70. public new void RenderContents(HtmlTextWriter writer) {
  71. base.RenderContents(writer);
  72. }
  73. public new void CreateControlCollection() {
  74. base.CreateControlCollection();
  75. }
  76. public new void AddAttributesToRender(HtmlTextWriter writer) {
  77. base.AddAttributesToRender(writer);
  78. }
  79. public string[] KeyValuePairs() {
  80. IEnumerator e;
  81. string[] result;
  82. int item;
  83. e = ViewState.GetEnumerator();
  84. result = new string[ViewState.Keys.Count];
  85. item = 0;
  86. while (e.MoveNext()) {
  87. DictionaryEntry d;
  88. StateItem si;
  89. d = (DictionaryEntry)e.Current;
  90. si = (StateItem)d.Value;
  91. if (si.Value is String[]) {
  92. string[] values;
  93. values = (string[]) si.Value;
  94. result[item] = d.Key.ToString() + "=";
  95. if (values.Length > 0) {
  96. result[item] += values[0];
  97. for (int i = 1; i < values.Length; i++) {
  98. result[item] += ", " + values[i];
  99. }
  100. }
  101. } else {
  102. result[item] = d.Key.ToString() + "=" + si.Value;
  103. }
  104. item++;
  105. }
  106. return result;
  107. }
  108. }
  109. private static HtmlTextWriter GetWriter () {
  110. StringWriter sw = new StringWriter ();
  111. sw.NewLine = "\n";
  112. return new HtmlTextWriter (sw);
  113. }
  114. private bool IsEqual(object[] a1, object[] a2, string assertion) {
  115. int matches;
  116. bool[] notfound;
  117. if (a1.Length != a2.Length) {
  118. if (assertion != null) {
  119. Assert.Fail(assertion + "( different length )");
  120. }
  121. return false;
  122. }
  123. matches = 0;
  124. notfound = new bool[a1.Length];
  125. for (int i = 0; i < a1.Length; i++) {
  126. for (int j = 0; j < a2.Length; j++) {
  127. if (a1[i].Equals(a2[j])) {
  128. matches++;
  129. break;
  130. }
  131. }
  132. if ((assertion != null) && (matches != i+1)) {
  133. Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
  134. }
  135. }
  136. return matches == a1.Length;
  137. }
  138. [Test]
  139. public void DropDownList_Defaults ()
  140. {
  141. DropDownListTestClass d = new DropDownListTestClass ();
  142. Assert.AreEqual (Color.Empty, d.BackColor, "D1");
  143. Assert.AreEqual (Color.Empty, d.BorderColor, "D2");
  144. Assert.AreEqual (BorderStyle.NotSet, d.BorderStyle, "D3");
  145. Assert.AreEqual (Unit.Empty, d.BorderWidth, "D4");
  146. Assert.AreEqual (-1, d.SelectedIndex, "D5");
  147. Assert.AreEqual (string.Empty, d.ToolTip, "D6");
  148. Assert.AreEqual (0, d.Items.Count, "D7");
  149. }
  150. [Test]
  151. public void DropDownListBasic () {
  152. DropDownListTestClass d = new DropDownListTestClass ();
  153. Assert.AreEqual("<select name>\n\n</select>", d.Render(), "B1");
  154. d.ID = "blah";
  155. Assert.AreEqual("<select name=\"blah\" id=\"blah\">\n\n</select>", d.Render(), "B2");
  156. Assert.AreEqual(false, d.IsTrackingVS(), "B3");
  157. d.SetTrackingVS();
  158. Assert.AreEqual(true, d.IsTrackingVS(), "B4");
  159. d.Items.Add(new ListItem("text1", "value1"));
  160. Assert.AreEqual(1, d.Items.Count, "B5");
  161. d.Items.Add(new ListItem("text2", "value2"));
  162. Assert.AreEqual(2, d.Items.Count, "B6");
  163. d.SelectedIndex = 1;
  164. Assert.AreEqual("<select name=\"blah\" id=\"blah\">\n\t<option value=\"value1\">text1</option>\n\t<option selected=\"selected\" value=\"value2\">text2</option>\n\n</select>", d.Render(), "B7");
  165. }
  166. [Test]
  167. public void DropDownListProperties () {
  168. DropDownListTestClass d;
  169. d = new DropDownListTestClass ();
  170. Assert.AreEqual(Color.Empty, d.BorderColor, "P1");
  171. d.BorderColor = Color.Red;
  172. Assert.AreEqual(Color.Red, d.BorderColor, "P2");
  173. Assert.AreEqual(BorderStyle.NotSet, d.BorderStyle, "P3");
  174. d.BorderStyle = BorderStyle.Dotted;
  175. Assert.AreEqual(BorderStyle.Dotted, d.BorderStyle, "P4");
  176. Assert.AreEqual(Unit.Empty, d.BorderWidth, "P5");
  177. d.BorderWidth = new Unit(1);
  178. Assert.AreEqual("1px", d.BorderWidth.ToString(), "P6");
  179. Assert.AreEqual(-1, d.SelectedIndex, "P7");
  180. d.Items.Add(new ListItem("text1", "value1"));
  181. d.Items.Add(new ListItem("text2", "value2"));
  182. d.SelectedIndex = 1;
  183. Assert.AreEqual(1, d.SelectedIndex, "P8");
  184. Assert.AreEqual(string.Empty, d.ToolTip, "P9");
  185. d.ToolTip = "blah";
  186. #if NET_2_0
  187. Assert.AreEqual ("blah", d.ToolTip, "P10");
  188. #else
  189. Assert.AreEqual(string.Empty, d.ToolTip, "P10");
  190. #endif
  191. }
  192. [Test]
  193. [ExpectedException(typeof(HttpException))]
  194. public void DropDownListDoubleSelectCheck () {
  195. DropDownListTestClass d;
  196. d = new DropDownListTestClass ();
  197. d.Items.Add(new ListItem("text1", "value1"));
  198. d.Items.Add(new ListItem("text2", "value2"));
  199. d.SelectedIndex = 1;
  200. Assert.AreEqual(1, d.SelectedIndex, "DS1");
  201. d.Items[0].Selected = true;
  202. d.Items[1].Selected = true;
  203. Assert.AreEqual("<select name>\n\t<option selected=\"selected\" value=\"value1\">text1</option>\n\t<option selected=\"selected\" value=\"value2\">text2</option>\n\n</select>", d.Render(), "DS1");
  204. }
  205. [Test]
  206. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  207. public void DropDownListBorderStyleCheck () {
  208. DropDownListTestClass d;
  209. d = new DropDownListTestClass ();
  210. d.BorderStyle = (BorderStyle)Int32.MinValue;
  211. }
  212. [Test]
  213. public void DropDownListSelectedCheck () {
  214. DropDownListTestClass d;
  215. d = new DropDownListTestClass ();
  216. d.SelectedIndex = 2; // No exception thrown!!!
  217. Assert.AreEqual(-1, d.SelectedIndex, "S1");
  218. }
  219. [Test]
  220. [ExpectedException(typeof(NullReferenceException))]
  221. public void DropDownNullWriterTest () {
  222. DropDownListTestClass d;
  223. d = new DropDownListTestClass ();
  224. d.AddAttributesToRender(null);
  225. }
  226. [Test]
  227. public void DropDownListNull () {
  228. DropDownListTestClass d;
  229. d = new DropDownListTestClass ();
  230. // We want to surve the next two calls
  231. d.RenderContents(null);
  232. }
  233. #if not
  234. [Test]
  235. public void HtmlWriter () {
  236. HtmlTextWriter writer;
  237. writer = DropDownListTest.GetWriter();
  238. writer.RenderBeginTag(HtmlTextWriterTag.Select);
  239. writer.AddAttribute(HtmlTextWriterAttribute.Value, "MeValue", true);
  240. writer.RenderBeginTag(HtmlTextWriterTag.Option);
  241. writer.Write("MeText");
  242. writer.RenderEndTag();
  243. writer.RenderEndTag();
  244. Assert.AreEqual("<select>\n\t<option value=\"MeValue\">\n\t\tMeText\n\t</option>\n</select>", writer.InnerWriter.ToString(), "H1");
  245. }
  246. #endif
  247. [Test]
  248. public void DropDownNamingTest () {
  249. NamingContainer container = new NamingContainer ();
  250. DropDownListTestClass child = new DropDownListTestClass ();
  251. Assert.AreEqual ("<select name>\n\n</select>", child.Render (), "N1");
  252. container.Controls.Add (child);
  253. Assert.AreEqual ("<select name=\"_ctl0\">\n\n</select>", child.Render (), "N2");
  254. container.ID = "naming";
  255. Assert.AreEqual ("<select name=\"naming:_ctl0\">\n\n</select>", child.Render (), "N3");
  256. child.ID = "fooid";
  257. Assert.AreEqual ("<select name=\"naming:fooid\" id=\"naming_fooid\">\n\n</select>", child.Render (), "N4");
  258. }
  259. [Test]
  260. public void InitialSelectionMade ()
  261. {
  262. DropDownList ddl = new DropDownList ();
  263. ddl.Items.Add ("a");
  264. ddl.Items.Add ("b");
  265. Assert.IsNotNull (ddl.SelectedItem, "need a selected item");
  266. Assert.AreEqual ("a", ddl.SelectedItem.Text);
  267. }
  268. DataSet GetExampleData ()
  269. {
  270. DataSet ds = new DataSet ();
  271. ds.ReadXml (new StringReader (@"
  272. <DataSet>
  273. <Stocks Company='Novell Inc.' Symbol='NOVL' Price='6.14' />
  274. <Stocks Company='Microsoft Corp.' Symbol='MSFT' Price='25.92' />
  275. <Stocks Company='Google' Symbol='GOOG' Price='291.60' />
  276. </DataSet>
  277. "));
  278. return ds;
  279. }
  280. [Test]
  281. public void TestValueFieldAndTextFormat ()
  282. {
  283. DropDownListTestClass ddl = new DropDownListTestClass ();
  284. ddl.DataSource = GetExampleData ();
  285. ddl.DataValueField = "Company";
  286. ddl.DataTextFormatString = "This shouldn't show up = {0}";
  287. ddl.DataBind ();
  288. string exp = @"<select name>
  289. <option value=""Novell Inc."">Novell Inc.</option>
  290. <option value=""Microsoft Corp."">Microsoft Corp.</option>
  291. <option value=""Google"">Google</option>
  292. </select>";
  293. Assert.AreEqual (exp, ddl.Render ());
  294. }
  295. }
  296. }