DropDownListTest.cs 11 KB

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