DropDownListTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. //
  2. // Tests for System.Web.UI.WebControls.DropDownList.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. #if NET_2_0
  154. Assert.AreEqual ("<select>\n\n</select>", d.Render (), "B1");
  155. #else
  156. Assert.AreEqual("<select name>\n\n</select>", d.Render(), "B1");
  157. #endif
  158. d.ID = "blah";
  159. Assert.AreEqual("<select name=\"blah\" id=\"blah\">\n\n</select>", d.Render(), "B2");
  160. Assert.AreEqual(false, d.IsTrackingVS(), "B3");
  161. d.SetTrackingVS();
  162. Assert.AreEqual(true, d.IsTrackingVS(), "B4");
  163. d.Items.Add(new ListItem("text1", "value1"));
  164. Assert.AreEqual(1, d.Items.Count, "B5");
  165. d.Items.Add(new ListItem("text2", "value2"));
  166. Assert.AreEqual(2, d.Items.Count, "B6");
  167. d.SelectedIndex = 1;
  168. 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");
  169. }
  170. [Test]
  171. public void DropDownListProperties () {
  172. DropDownListTestClass d;
  173. d = new DropDownListTestClass ();
  174. Assert.AreEqual(Color.Empty, d.BorderColor, "P1");
  175. d.BorderColor = Color.Red;
  176. Assert.AreEqual(Color.Red, d.BorderColor, "P2");
  177. Assert.AreEqual(BorderStyle.NotSet, d.BorderStyle, "P3");
  178. d.BorderStyle = BorderStyle.Dotted;
  179. Assert.AreEqual(BorderStyle.Dotted, d.BorderStyle, "P4");
  180. Assert.AreEqual(Unit.Empty, d.BorderWidth, "P5");
  181. d.BorderWidth = new Unit(1);
  182. Assert.AreEqual("1px", d.BorderWidth.ToString(), "P6");
  183. Assert.AreEqual(-1, d.SelectedIndex, "P7");
  184. d.Items.Add(new ListItem("text1", "value1"));
  185. d.Items.Add(new ListItem("text2", "value2"));
  186. d.SelectedIndex = 1;
  187. Assert.AreEqual(1, d.SelectedIndex, "P8");
  188. Assert.AreEqual(string.Empty, d.ToolTip, "P9");
  189. d.ToolTip = "blah";
  190. #if NET_2_0
  191. Assert.AreEqual ("blah", d.ToolTip, "P10");
  192. #else
  193. Assert.AreEqual(string.Empty, d.ToolTip, "P10");
  194. #endif
  195. }
  196. [Test]
  197. [ExpectedException(typeof(HttpException))]
  198. public void DropDownListDoubleSelectCheck () {
  199. DropDownListTestClass d;
  200. d = new DropDownListTestClass ();
  201. d.Items.Add(new ListItem("text1", "value1"));
  202. d.Items.Add(new ListItem("text2", "value2"));
  203. d.SelectedIndex = 1;
  204. Assert.AreEqual(1, d.SelectedIndex, "DS1");
  205. d.Items[0].Selected = true;
  206. d.Items[1].Selected = true;
  207. 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");
  208. }
  209. [Test]
  210. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  211. public void DropDownListBorderStyleCheck () {
  212. DropDownListTestClass d;
  213. d = new DropDownListTestClass ();
  214. d.BorderStyle = (BorderStyle)Int32.MinValue;
  215. }
  216. [Test]
  217. public void DropDownListSelectedCheck () {
  218. DropDownListTestClass d;
  219. d = new DropDownListTestClass ();
  220. d.SelectedIndex = 2; // No exception thrown!!!
  221. Assert.AreEqual(-1, d.SelectedIndex, "S1");
  222. }
  223. [Test]
  224. #if ONLY_1_1
  225. [ExpectedException(typeof(NullReferenceException))]
  226. #endif
  227. public void DropDownNullWriterTest () {
  228. DropDownListTestClass d;
  229. d = new DropDownListTestClass ();
  230. d.AddAttributesToRender(null);
  231. }
  232. [Test]
  233. public void DropDownListNull () {
  234. DropDownListTestClass d;
  235. d = new DropDownListTestClass ();
  236. // We want to surve the next two calls
  237. d.RenderContents(null);
  238. }
  239. #if not
  240. [Test]
  241. public void HtmlWriter () {
  242. HtmlTextWriter writer;
  243. writer = DropDownListTest.GetWriter();
  244. writer.RenderBeginTag(HtmlTextWriterTag.Select);
  245. writer.AddAttribute(HtmlTextWriterAttribute.Value, "MeValue", true);
  246. writer.RenderBeginTag(HtmlTextWriterTag.Option);
  247. writer.Write("MeText");
  248. writer.RenderEndTag();
  249. writer.RenderEndTag();
  250. Assert.AreEqual("<select>\n\t<option value=\"MeValue\">\n\t\tMeText\n\t</option>\n</select>", writer.InnerWriter.ToString(), "H1");
  251. }
  252. #endif
  253. [Test]
  254. public void DropDownNamingTest () {
  255. NamingContainer container = new NamingContainer ();
  256. DropDownListTestClass child = new DropDownListTestClass ();
  257. #if NET_2_0
  258. Assert.AreEqual ("<select>\n\n</select>", child.Render (), "N1");
  259. #else
  260. Assert.AreEqual ("<select name>\n\n</select>", child.Render (), "N1");
  261. #endif
  262. container.Controls.Add (child);
  263. // don't assume the generated id
  264. string s = child.Render ();
  265. Assert.IsTrue (s.StartsWith ("<select name=\""), "N2a");
  266. Assert.IsTrue (s.EndsWith ("\">\n\n</select>"), "N2b");
  267. container.ID = "naming";
  268. s = child.Render ();
  269. Assert.IsTrue (s.StartsWith ("<select name=\"naming"), "N3a");
  270. Assert.IsTrue (s.EndsWith ("\">\n\n</select>"), "N3b");
  271. child.ID = "fooid";
  272. s = child.Render ();
  273. Assert.IsTrue (s.StartsWith ("<select name=\"naming"), "N4a");
  274. Assert.IsTrue (s.EndsWith ("fooid\" id=\"naming_fooid\">\n\n</select>"), "N4b");
  275. }
  276. [Test]
  277. public void InitialSelectionMade ()
  278. {
  279. DropDownList ddl = new DropDownList ();
  280. ddl.Items.Add ("a");
  281. ddl.Items.Add ("b");
  282. Assert.IsNotNull (ddl.SelectedItem, "need a selected item");
  283. Assert.AreEqual ("a", ddl.SelectedItem.Text);
  284. }
  285. DataSet GetExampleData ()
  286. {
  287. DataSet ds = new DataSet ();
  288. ds.ReadXml (new StringReader (@"
  289. <DataSet>
  290. <Stocks Company='Novell Inc.' Symbol='NOVL' Price='6.14' />
  291. <Stocks Company='Microsoft Corp.' Symbol='MSFT' Price='25.92' />
  292. <Stocks Company='Google' Symbol='GOOG' Price='291.60' />
  293. </DataSet>
  294. "));
  295. return ds;
  296. }
  297. [Test]
  298. public void TestValueFieldAndTextFormat ()
  299. {
  300. DropDownListTestClass ddl = new DropDownListTestClass ();
  301. ddl.DataSource = GetExampleData ();
  302. ddl.DataValueField = "Company";
  303. ddl.DataTextFormatString = "This shouldn't show up = {0}";
  304. ddl.DataBind ();
  305. #if NET_2_0
  306. string exp = @"<select>
  307. <option value=""Novell Inc."">Novell Inc.</option>
  308. <option value=""Microsoft Corp."">Microsoft Corp.</option>
  309. <option value=""Google"">Google</option>
  310. </select>";
  311. #else
  312. string exp = @"<select name>
  313. <option value=""Novell Inc."">Novell Inc.</option>
  314. <option value=""Microsoft Corp."">Microsoft Corp.</option>
  315. <option value=""Google"">Google</option>
  316. </select>";
  317. #endif
  318. Assert.AreEqual (exp, ddl.Render ());
  319. }
  320. }
  321. }