DropDownListTest.cs 14 KB

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