SiteMapPathTest.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. //
  2. // Tests for System.Web.UI.WebControls.SiteMapPath.cs
  3. //
  4. // Author:
  5. // Yoni Klein ([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. #if NET_2_0
  29. using NUnit.Framework;
  30. using System;
  31. using System.Drawing;
  32. using System.IO;
  33. using System.Globalization;
  34. using System.Collections;
  35. using System.Configuration;
  36. using System.Web;
  37. using System.Web.UI;
  38. using System.Web.UI.WebControls;
  39. using System.Threading;
  40. using MyWebControl = System.Web.UI.WebControls;
  41. using System.Text;
  42. using System.Collections.Specialized;
  43. using System.Diagnostics;
  44. using System.Web.Hosting;
  45. using NunitWeb;
  46. using System.Reflection;
  47. using System.Xml;
  48. using MonoTests.stand_alone.WebHarness;
  49. namespace MonoTests.System.Web.UI.WebControls
  50. {
  51. class PokerSiteMapPath : SiteMapPath
  52. {
  53. public PokerSiteMapPath ()
  54. {
  55. TrackViewState ();
  56. }
  57. public StateBag StateBag
  58. {
  59. get { return base.ViewState; }
  60. }
  61. public object SaveState ()
  62. {
  63. return SaveViewState ();
  64. }
  65. public void LoadState (object o)
  66. {
  67. LoadViewState (o);
  68. }
  69. public void InitilizeItems (SiteMapNodeItem I)
  70. {
  71. InitializeItem (I);
  72. }
  73. public void DoCreateControlHierarchy ()
  74. {
  75. CreateControlHierarchy ();
  76. }
  77. public void DoOnDataBinding (EventArgs e)
  78. {
  79. base.OnDataBinding (e);
  80. }
  81. public void DoOnItemDataBound (SiteMapNodeItemEventArgs e)
  82. {
  83. base.OnItemDataBound (e);
  84. }
  85. public void DoOnItemCteated (SiteMapNodeItemEventArgs e)
  86. {
  87. base.OnItemCreated (e);
  88. }
  89. }
  90. [Serializable]
  91. [TestFixture]
  92. public class SiteMapPathTest
  93. {
  94. [TestFixtureSetUp]
  95. public void Set_Up ()
  96. {
  97. #if DOT_NET
  98. Helper.Instance.CopyResource(Assembly.GetExecutingAssembly (), "MonoTests.System.Web.UI.WebControls.Resources.Web.sitemap","Web.sitemap");
  99. #else
  100. Helper.Instance.CopyResource (Assembly.GetExecutingAssembly (), "Web.sitemap", "Web.sitemap");
  101. #endif
  102. }
  103. [SetUp]
  104. public void SetupTestCase ()
  105. {
  106. Thread.Sleep (100);
  107. }
  108. [Test]
  109. public void SiteMapPath_DefaultProperties ()
  110. {
  111. PokerSiteMapPath p = new PokerSiteMapPath ();
  112. Assert.IsTrue (p.CurrentNodeStyle.IsEmpty, "CurrentNodeStyle");
  113. Assert.IsNull (p.CurrentNodeTemplate, "CurrentNodeTemplate");
  114. Assert.IsTrue (p.NodeStyle.IsEmpty, "NodeStyle");
  115. Assert.IsNull (p.NodeTemplate, "NodeTemplate");
  116. Assert.AreEqual (-1, p.ParentLevelsDisplayed, "ParentLevelsDisplayed");
  117. Assert.AreEqual (PathDirection.RootToCurrent, p.PathDirection, "PathDirection");
  118. Assert.IsNull (p.PathSeparatorTemplate, "PathSeparatorTemplate");
  119. Assert.IsFalse (p.RenderCurrentNodeAsLink, "RenderCurrentNodeAsLink");
  120. Assert.IsTrue (p.RootNodeStyle.IsEmpty, "RootNodeStyle");
  121. Assert.IsNull (p.RootNodeTemplate, "RootNodeTemplate");
  122. Assert.IsTrue (p.ShowToolTips, "ShowToolTips");
  123. Assert.AreEqual ("", p.SiteMapProvider, "SiteMapProvider");
  124. Assert.AreEqual ("Skip Navigation Links", p.SkipLinkText, "Skip Navigation Links");
  125. }
  126. [Test]
  127. [Category ("NotWorking")]
  128. public void SiteMapPath_DefaultNotWorkingProperties()
  129. {
  130. PokerSiteMapPath p = new PokerSiteMapPath ();
  131. Assert.AreEqual (" > ", p.PathSeparator, "PathSeparator");
  132. }
  133. [Test]
  134. public void SiteMapPath_ChangeProperties ()
  135. {
  136. PokerSiteMapPath p = new PokerSiteMapPath ();
  137. p.ShowToolTips = false;
  138. Assert.IsFalse (p.ShowToolTips, "ShowToolTips");
  139. Style currentNodeStyle = new Style ();
  140. p.CurrentNodeStyle.ForeColor = Color.AliceBlue;
  141. Assert.AreEqual (Color.AliceBlue, p.CurrentNodeStyle.ForeColor, "CurrentNodeStyle");
  142. Style NodeStyle = new Style ();
  143. p.NodeStyle.BackColor = Color.Aqua;
  144. Assert.AreEqual (Color.Aqua, p.NodeStyle.BackColor, "NodeStyle");
  145. p.PathDirection = PathDirection.CurrentToRoot;
  146. Assert.AreEqual (PathDirection.CurrentToRoot, p.PathDirection, "PathDirection");
  147. p.PathSeparator = " - ";
  148. Assert.AreEqual (" - ", p.PathSeparator, "PathSeparator");
  149. Style RootNodeStyle = new Style ();
  150. p.RootNodeStyle.BackColor = Color.Red;
  151. Assert.IsFalse (p.RootNodeStyle.IsEmpty, "RootNodeStyle#1");
  152. Assert.AreEqual (Color.Red, p.RootNodeStyle.BackColor, "RootNodeStyle#2");
  153. p.ParentLevelsDisplayed = 2;
  154. Assert.AreEqual (2, p.ParentLevelsDisplayed, "ParentLevelsDisplayed");
  155. p.RenderCurrentNodeAsLink = true;
  156. Assert.IsTrue (p.RenderCurrentNodeAsLink, "RenderCurrentNodeAsLink");
  157. p.SiteMapProvider = "test";
  158. Assert.AreEqual ("test", p.SiteMapProvider, "SiteMapProvider");
  159. p.SkipLinkText = "test";
  160. Assert.AreEqual ("test", p.SkipLinkText, "Skip Navigation Links");
  161. //programmatically create template
  162. MyWebControl.Image myImage = new MyWebControl.Image ();
  163. myImage.ImageUrl = "myimage.jpg";
  164. ImageTemplate rootNodeImageTemplate = new ImageTemplate ();
  165. rootNodeImageTemplate.MyImage = myImage;
  166. // end create template image
  167. p.RootNodeTemplate = rootNodeImageTemplate;
  168. Assert.IsNotNull (p.RootNodeTemplate, "RootNodeTemplate");
  169. Assert.AreEqual (rootNodeImageTemplate, p.RootNodeTemplate, "RootNodeTemplate");
  170. p.NodeTemplate = rootNodeImageTemplate;
  171. Assert.IsNotNull (p.NodeTemplate, "NodeTemplate");
  172. Assert.AreEqual (rootNodeImageTemplate, p.NodeTemplate, "NodeTemplate");
  173. p.CurrentNodeTemplate = rootNodeImageTemplate;
  174. Assert.IsNotNull (p.CurrentNodeTemplate, "RootNodeTemplate");
  175. Assert.AreEqual (rootNodeImageTemplate, p.CurrentNodeTemplate, "RootNodeTemplate");
  176. }
  177. [Test]
  178. public void SiteMapPath_NullProperties ()
  179. {
  180. PokerSiteMapPath p = new PokerSiteMapPath ();
  181. p.ShowToolTips = false;
  182. Assert.IsFalse (p.ShowToolTips, "ShowToolTips");
  183. Assert.AreEqual (1, p.StateBag.Count, "NullProperties#1");
  184. p.PathDirection = PathDirection.CurrentToRoot;
  185. Assert.AreEqual (PathDirection.CurrentToRoot, p.PathDirection, "PathDirection");
  186. Assert.AreEqual (2, p.StateBag.Count, "NullProperties#2");
  187. p.PathSeparator = " - ";
  188. Assert.AreEqual (3, p.StateBag.Count, "NullProperties#3");
  189. p.SiteMapProvider = "test";
  190. Assert.AreEqual (4, p.StateBag.Count, "NullProperties#4");
  191. p.SkipLinkText = "test";
  192. Assert.AreEqual (5, p.StateBag.Count, "NullProperties#5");
  193. p.SkipLinkText = null;
  194. }
  195. // Rendering tests
  196. [Test]
  197. [Category ("NunitWeb")]
  198. [Category ("NotWorking")] //Must be running after hosting bug resolve
  199. public void SiteMapPath_RenderProperty ()
  200. {
  201. string RenderedPageHtml = Helper.Instance.RunInPage (DoTestPropertyRender, null);
  202. string RenderedControlHtml = WebTest.GetControlFromPageHtml (RenderedPageHtml);
  203. string OriginControlHtml = @"<span style=""display:inline-block;""><font color=""Red"">
  204. <a href=""#ctl01_SkipLink"">
  205. <img alt=""Skip Navigation Links""
  206. height=""0"" width=""0"" src=""/NunitWeb/WebResource.axd?d=Zrz8lvSQfolS1pG07HX9g2&amp;t=632784640484505569""
  207. border=""0"" />
  208. </a><span>node1</span><span>-</span><span>
  209. <a title=""test"" href=""/NunitWeb/MyPageWithMaster.aspx"">root</a></span>
  210. <a id=""ctl01_SkipLink""></a></font></span>";
  211. WebTest.AssertAreEqual(OriginControlHtml,RenderedControlHtml,"RenderProperty");
  212. }
  213. [Test]
  214. [Category ("NunitWeb")]
  215. [Category ("NotWorking")] //Must be running after hosting bug resolve
  216. public void SiteMapPath_RenderStyles ()
  217. {
  218. string RenderedPageHtml = Helper.Instance.RunInPage (DoTestStylesRender, null);
  219. string RenderedControlHtml = WebTest.GetControlFromPageHtml (RenderedPageHtml);
  220. string OriginControlHtml = @"<span><a href=""#ctl01_SkipLink"">
  221. <img alt=""Skip Navigation Links"" height=""0"" width=""0"" src=""/NunitWeb/WebResource.axd?d=gZrz8lvSQfolS1pG07HX9g2&amp;t=632784640484505569"" border=""0"" />
  222. </a><span><a title=""test"" href=""/NunitWeb/MyPageWithMaster.aspx"">root</a></span>
  223. <span> &gt; </span><span>node1</span>
  224. <a id=""ctl01_SkipLink""></a></span>";
  225. WebTest.AssertAreEqual (OriginControlHtml, RenderedControlHtml,"RenderStyles");
  226. }
  227. [Test]
  228. [Category ("NunitWeb")]
  229. [Category ("NotWorking")] //Must be running after hosting bug resolve
  230. public void SiteMapPath_DefaultRender()
  231. {
  232. string RenderedPageHtml = Helper.Instance.RunInPage (DoTestDefaultRender, null);
  233. string RenderedControlHtml = WebTest.GetControlFromPageHtml (RenderedPageHtml);
  234. string OriginControlHtml = @"<span><a href=""#ctl01_SkipLink"">
  235. <img alt=""Skip Navigation Links"" height=""0"" width=""0"" src=""/NunitWeb/WebResource.axd?d=gZrz8lvSQfolS1pG07HX9g2&amp;t=632784640484505569"" border=""0"" /></a>
  236. <span><a title=""test"" href=""/NunitWeb/MyPageWithMaster.aspx"">root</a></span><span> &gt; </span><span>node1</span>
  237. <a id=""ctl01_SkipLink""></a></span>";
  238. WebTest.AssertAreEqual (OriginControlHtml, RenderedControlHtml,"RenderDefault");
  239. }
  240. /// <summary>
  241. /// All this methods are delegates for running tests in host assembly.
  242. /// </summary>
  243. public static void DoTestDefaultRender (HttpContext c, Page p, object param)
  244. {
  245. LiteralControl lcb = new LiteralControl (WebTest.BEGIN_TAG);
  246. LiteralControl lce = new LiteralControl (WebTest.END_TAG);
  247. SiteMapPath smp = new SiteMapPath ();
  248. p.Form.Controls.Add (lcb);
  249. p.Form.Controls.Add (smp);
  250. p.Form.Controls.Add (lce);
  251. }
  252. public static void DoTestPropertyRender (HttpContext c, Page p, object param)
  253. {
  254. SiteMapPath smp = new SiteMapPath ();
  255. smp.BackColor = Color.Red;
  256. smp.BorderColor = Color.Red;
  257. smp.BorderStyle = BorderStyle.Dashed;
  258. smp.BorderWidth = 3;
  259. smp.ForeColor = Color.Red;
  260. smp.PathDirection = PathDirection.CurrentToRoot;
  261. smp.PathSeparator = "-";
  262. LiteralControl lcb = new LiteralControl (WebTest.BEGIN_TAG);
  263. LiteralControl lce = new LiteralControl (WebTest.END_TAG);
  264. p.Form.Controls.Add (lcb);
  265. p.Form.Controls.Add (smp);
  266. p.Form.Controls.Add (lce);
  267. }
  268. public static void DoTestStylesRender (HttpContext c, Page p, object param)
  269. {
  270. PokerSiteMapPath smp = new PokerSiteMapPath ();
  271. smp.ControlStyle.BackColor = Color.Red;
  272. smp.CurrentNodeStyle.BackColor = Color.Pink;
  273. smp.NodeStyle.BorderColor = Color.Purple;
  274. smp.PathSeparatorStyle.BackColor = Color.RoyalBlue;
  275. smp.RootNodeStyle.BackColor = Color.Beige;
  276. LiteralControl lcb = new LiteralControl (WebTest.BEGIN_TAG);
  277. LiteralControl lce = new LiteralControl (WebTest.END_TAG);
  278. p.Form.Controls.Add (lcb);
  279. p.Form.Controls.Add (smp);
  280. p.Form.Controls.Add (lce);
  281. }
  282. [Test]
  283. public void SiteMapPath_ViewState ()
  284. {
  285. PokerSiteMapPath p = new PokerSiteMapPath ();
  286. p.ShowToolTips = false;
  287. Style currentNodeStyle = new Style ();
  288. p.CurrentNodeStyle.ForeColor = Color.AliceBlue;
  289. Style NodeStyle = new Style ();
  290. p.NodeStyle.BackColor = Color.Aqua;
  291. p.PathDirection = PathDirection.CurrentToRoot;
  292. Assert.AreEqual (PathDirection.CurrentToRoot, p.PathDirection, "PathDirection");
  293. p.PathSeparator = " - ";
  294. Style RootNodeStyle = new Style ();
  295. p.RootNodeStyle.BackColor = Color.Red;
  296. p.ParentLevelsDisplayed = 2;
  297. p.RenderCurrentNodeAsLink = true;
  298. p.SiteMapProvider = "test";
  299. p.SkipLinkText = "test";
  300. object state = p.SaveState ();
  301. PokerSiteMapPath copy = new PokerSiteMapPath ();
  302. copy.LoadState (state);
  303. Assert.IsFalse (copy.ShowToolTips, "ShowToolTips");
  304. Assert.AreEqual (Color.AliceBlue, copy.CurrentNodeStyle.ForeColor, "CurrentNodeStyle");
  305. Assert.AreEqual (Color.Aqua, p.NodeStyle.BackColor, "NodeStyle");
  306. Assert.AreEqual (" - ", p.PathSeparator, "PathSeparator");
  307. Assert.IsFalse (p.RootNodeStyle.IsEmpty, "RootNodeStyle#1");
  308. Assert.AreEqual (Color.Red, p.RootNodeStyle.BackColor, "RootNodeStyle#2");
  309. Assert.AreEqual (2, p.ParentLevelsDisplayed, "ParentLevelsDisplayed");
  310. Assert.IsTrue (p.RenderCurrentNodeAsLink, "RenderCurrentNodeAsLink");
  311. Assert.AreEqual ("test", p.SiteMapProvider, "SiteMapProvider");
  312. Assert.AreEqual ("test", p.SkipLinkText, "Skip Navigation Links");
  313. }
  314. [Test]
  315. [Category ("NunitWeb")]
  316. public void SiteMapPath_SiteMapRootNode ()
  317. {
  318. NunitWeb.Helper.Instance.RunInPage (SiteMapRootNode,null);
  319. }
  320. [Test]
  321. [Category ("NunitWeb")]
  322. [Category ("NotWorking")] //Must be running after hosting bug resolve
  323. public void SiteMapPath_InitializeItem ()
  324. {
  325. NunitWeb.Helper.Instance.RunInPage (InitializeItem, null);
  326. }
  327. [Test]
  328. [Category ("NunitWeb")]
  329. [Category ("NotWorking")] //Must be running after hosting bug resolve
  330. public void SiteMapPath_SiteMapChildNode ()
  331. {
  332. NunitWeb.Helper.Instance.RunInPage (InitializeItem, null);
  333. }
  334. public static void SiteMapRootNode (HttpContext c, Page p, object param)
  335. {
  336. PokerSiteMapPath smp = new PokerSiteMapPath ();
  337. Assert.AreEqual ("root", smp.Provider.RootNode.Title, "RootNode");
  338. }
  339. public static void InitializeItem (HttpContext c, Page p, object param)
  340. {
  341. PokerSiteMapPath smp = new PokerSiteMapPath ();
  342. SiteMapNodeItem I = new SiteMapNodeItem (0, SiteMapNodeItemType.PathSeparator);
  343. smp.PathSeparatorStyle.BorderColor = Color.Red;
  344. smp.InitilizeItems (I);
  345. Assert.AreEqual (Color.Red, I.BorderColor, "InitializeItem");
  346. }
  347. public static void SiteMapChildNode (HttpContext c, Page p, object param)
  348. {
  349. PokerSiteMapPath smp = new PokerSiteMapPath ();
  350. SiteMapNodeCollection myCol = smp.Provider.GetChildNodes (smp.Provider.RootNode);
  351. Assert.AreEqual (1, myCol.Count, "SiteMapChildNode#1");
  352. }
  353. // Events Stuff
  354. private bool DataBinding;
  355. private bool ItemDataBounding;
  356. private bool ItemCreated;
  357. private void DataBindingHandler (object sender, EventArgs e)
  358. {
  359. DataBinding = true;
  360. }
  361. private void ItemDataBoundHandler (object sender, SiteMapNodeItemEventArgs e)
  362. {
  363. ItemDataBounding = true;
  364. }
  365. private void ItemCreatedHandler (object sender, SiteMapNodeItemEventArgs e)
  366. {
  367. ItemCreated = true;
  368. }
  369. private void ResetEvents ()
  370. {
  371. DataBinding = false;
  372. ItemDataBounding = false;
  373. ItemCreated = false;
  374. }
  375. [Test]
  376. [Category ("NunitWeb")]
  377. public void SiteMapPath_Events ()
  378. {
  379. Helper.Instance.RunInPage (Events, null);
  380. }
  381. public void Events (HttpContext c, Page p, object param)
  382. {
  383. PokerSiteMapPath smp = new PokerSiteMapPath ();
  384. ResetEvents ();
  385. smp.DataBinding += new EventHandler (DataBindingHandler);
  386. smp.ItemDataBound += new SiteMapNodeItemEventHandler (ItemDataBoundHandler);
  387. smp.ItemCreated += new SiteMapNodeItemEventHandler (ItemCreatedHandler);
  388. Assert.AreEqual (false, DataBinding, "BeforeDataBinding");
  389. smp.DoOnDataBinding (new EventArgs ());
  390. Assert.AreEqual (true, DataBinding, "AfterDataBinding");
  391. ResetEvents ();
  392. Assert.AreEqual (false, ItemDataBounding, "BeforeItemDataBound");
  393. SiteMapNodeItem i = new SiteMapNodeItem (0, SiteMapNodeItemType.Root);
  394. smp.DoOnItemDataBound (new SiteMapNodeItemEventArgs (i));
  395. Assert.AreEqual (true, ItemDataBounding, "AfterItemDataBound");
  396. ResetEvents ();
  397. SiteMapNodeItemEventArgs MyArgs = new SiteMapNodeItemEventArgs (new SiteMapNodeItem(0,SiteMapNodeItemType.Parent));
  398. Assert.AreEqual (false, ItemCreated, "BeforeItemCreated");
  399. smp.DoOnItemCteated (MyArgs);
  400. Assert.AreEqual (true, ItemCreated, "AfterItemCreated");
  401. }
  402. [Test]
  403. [Category ("NotWorking")] //throws System.IndexOutOfRangeException : Array index is out of range
  404. [ExpectedException (typeof (ConfigurationErrorsException))]
  405. public void SiteMapPath_CreateControlHierarchy ()
  406. {
  407. PokerSiteMapPath p = new PokerSiteMapPath ();
  408. p.DoCreateControlHierarchy ();
  409. }
  410. [Test]
  411. [Category ("NotWorking")] //throws System.IndexOutOfRangeException : Array index is out of range
  412. [ExpectedException (typeof (ConfigurationErrorsException))]
  413. public void SiteMapPath_DataBindExeption ()
  414. {
  415. PokerSiteMapPath p = new PokerSiteMapPath ();
  416. p.DataBind ();
  417. }
  418. [TestFixtureTearDown]
  419. public void TearDown ()
  420. {
  421. Helper.Unload ();
  422. }
  423. // A simple Template class to wrap an image.
  424. public class ImageTemplate : ITemplate
  425. {
  426. private MyWebControl.Image myImage;
  427. public MyWebControl.Image MyImage
  428. {
  429. get
  430. {
  431. return myImage;
  432. }
  433. set
  434. {
  435. myImage = value;
  436. }
  437. }
  438. public void InstantiateIn (Control container)
  439. {
  440. container.Controls.Add (MyImage);
  441. }
  442. }
  443. }
  444. }
  445. #endif