SiteMapDataSourceTest.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // Tests for System.Web.UI.WebControls.View.cs
  3. //
  4. // Author:
  5. // Yoni Klein ([email protected])
  6. //
  7. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using NUnit.Framework;
  30. using System;
  31. using System.IO;
  32. using System.Globalization;
  33. using System.Configuration;
  34. using System.Collections;
  35. using System.Collections.Specialized;
  36. using System.Web;
  37. using System.Web.UI;
  38. using System.Web.UI.WebControls;
  39. using System.Security.Permissions;
  40. namespace MonoTests.System.Web.UI.WebControls
  41. {
  42. class PokerSiteMapDataSource : SiteMapDataSource
  43. {
  44. public PokerSiteMapDataSource ()
  45. {
  46. TrackViewState ();
  47. }
  48. public object SaveState ()
  49. {
  50. return SaveViewState ();
  51. }
  52. public void LoadState (object o)
  53. {
  54. LoadViewState (o);
  55. }
  56. public StateBag StateBag
  57. {
  58. get { return base.ViewState; }
  59. }
  60. public HierarchicalDataSourceView DoHierarchicalDataSourceView (string str)
  61. {
  62. return GetHierarchicalView (str);
  63. }
  64. }
  65. [TestFixture]
  66. public class SiteMapDataSourceTest
  67. {
  68. [Test]
  69. public void SiteMapDataSource_DefaultProperties ()
  70. {
  71. PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
  72. Assert.AreEqual (true, p.ShowStartingNode, "ShowStartingNode");
  73. Assert.AreEqual (string.Empty, p.SiteMapProvider, "SiteMapProvider");
  74. Assert.AreEqual (false, p.StartFromCurrentNode, "StartFromCurrentNode");
  75. Assert.AreEqual (0, p.StartingNodeOffset, "StartingNodeOffset");
  76. Assert.AreEqual (string.Empty, p.StartingNodeUrl, "StartingNodeUrl");
  77. }
  78. [Test]
  79. public void SiteMapDataSource_ContainsListCollection ()
  80. {
  81. PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
  82. Assert.AreEqual (true, p.ContainsListCollection, "ContainsListCollection");
  83. }
  84. [Test]
  85. public void SiteMapDataSource_ChangeProperties ()
  86. {
  87. PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
  88. p.ShowStartingNode = false;
  89. Assert.AreEqual (false, p.ShowStartingNode, "ShowStartingNode");
  90. Assert.AreEqual (1, p.StateBag.Count, "ShowStartingNode#1");
  91. p.SiteMapProvider = "test";
  92. Assert.AreEqual ("test", p.SiteMapProvider, "SiteMapProvider");
  93. Assert.AreEqual (2, p.StateBag.Count, "SiteMapProvider#1");
  94. // null properties doe's not affects on state bag count
  95. p.SiteMapProvider = null;
  96. Assert.AreEqual (2, p.StateBag.Count, "SiteMapProvider#2");
  97. p.StartFromCurrentNode = true;
  98. Assert.AreEqual (true, p.StartFromCurrentNode, "StartFromCurrentNode");
  99. Assert.AreEqual (3, p.StateBag.Count, "StartFromCurrentNode#1");
  100. p.StartingNodeOffset = 1;
  101. Assert.AreEqual (1, p.StartingNodeOffset, "StartingNodeOffset");
  102. Assert.AreEqual (4, p.StateBag.Count, "StartingNodeOffset#2");
  103. p.StartingNodeUrl = "default.aspx";
  104. Assert.AreEqual ("default.aspx", p.StartingNodeUrl, "StartingNodeUrl");
  105. Assert.AreEqual (5, p.StateBag.Count, "StartingNodeUrl#1");
  106. // null properties doe's not affects on state bag count
  107. p.StartingNodeUrl = null;
  108. Assert.AreEqual (5, p.StateBag.Count, "StartingNodeUrl#2");
  109. }
  110. [Test]
  111. public void SiteMapDataSource_DataSourceChanged ()
  112. {
  113. PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
  114. ((IDataSource) p).DataSourceChanged += new EventHandler (SiteMapDataSourceTest_DataSourceChanged);
  115. eventChecker = false;
  116. p.ShowStartingNode = false;
  117. Assert.IsTrue (eventChecker, "DataSourceChanged#1");
  118. eventChecker = false;
  119. p.SiteMapProvider = "test";
  120. Assert.IsTrue (eventChecker, "DataSourceChanged#2");
  121. eventChecker = false;
  122. p.StartFromCurrentNode = true;
  123. Assert.IsTrue (eventChecker, "DataSourceChanged#3");
  124. eventChecker = false;
  125. p.StartingNodeOffset = 1;
  126. Assert.IsTrue (eventChecker, "DataSourceChanged#4");
  127. eventChecker = false;
  128. p.StartingNodeUrl = "default.aspx";
  129. Assert.IsTrue (eventChecker, "DataSourceChanged#5");
  130. }
  131. bool eventChecker;
  132. void SiteMapDataSourceTest_DataSourceChanged (object sender, EventArgs e)
  133. {
  134. eventChecker = true;
  135. }
  136. [Test]
  137. public void SiteMapDataSource_GetList ()
  138. {
  139. PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
  140. Assert.IsNotNull (p.GetList (), "GetList");
  141. Assert.IsTrue (p.ContainsListCollection, "ContainsListCollection");
  142. }
  143. [Test]
  144. public void SiteMapDataSource_GetViewNames () {
  145. PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
  146. Assert.AreEqual (1, p.GetViewNames ().Count, "GetViewNames().Count");
  147. Assert.AreEqual ("DefaultView", ((string []) p.GetViewNames ()) [0], "GetViewNames () [0]");
  148. }
  149. [Test]
  150. public void SiteMapDataSource_GetView ()
  151. {
  152. PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
  153. p.Provider = new mySiteMapProvider ();
  154. DataSourceView V = p.GetView("");
  155. Assert.IsNotNull (V, "GetView");
  156. }
  157. [Test]
  158. public void SiteMapDataSource_ViewState ()
  159. {
  160. PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
  161. p.SiteMapProvider = "test";
  162. p.StartFromCurrentNode = false;
  163. p.StartingNodeOffset = 1;
  164. p.StartingNodeUrl = "default.aspx";
  165. object state = p.SaveState ();
  166. PokerSiteMapDataSource copy = new PokerSiteMapDataSource ();
  167. copy.LoadState (state);
  168. Assert.AreEqual ("test", copy.SiteMapProvider, "SiteMapProvider");
  169. Assert.AreEqual (false, copy.StartFromCurrentNode, "StartFromCurrentNode");
  170. Assert.AreEqual (1, copy.StartingNodeOffset, "StartingNodeOffset");
  171. Assert.AreEqual ("default.aspx", copy.StartingNodeUrl, "StartingNodeUrl");
  172. }
  173. [Test]
  174. public void SiteMapDataSource_HierarchicalDataSourceView ()
  175. {
  176. PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
  177. p.Provider = new mySiteMapProvider ();
  178. HierarchicalDataSourceView h = p.DoHierarchicalDataSourceView ("1");
  179. Assert.IsNotNull (h, "HierarchicalDataSourceView");
  180. }
  181. }
  182. // Helper Class
  183. public class mySiteMapProvider : StaticSiteMapProvider
  184. {
  185. private SiteMapNode rootNode = null;
  186. // Implement a default constructor.
  187. public mySiteMapProvider ()
  188. {
  189. }
  190. // Some basic state to help track the initialization state of the provider.
  191. private bool initialized = false;
  192. public virtual bool IsInitialized
  193. {
  194. get { return initialized; }
  195. }
  196. // Return the root node of the current site map.
  197. public override SiteMapNode RootNode
  198. {
  199. get
  200. {
  201. SiteMapNode temp = null;
  202. temp = BuildSiteMap ();
  203. return temp;
  204. }
  205. }
  206. protected override SiteMapNode GetRootNodeCore ()
  207. {
  208. return RootNode;
  209. }
  210. // Initialize is used to initialize the properties and any state that the
  211. // AccessProvider holds, but is not used to build the site map.
  212. // The site map is built when the BuildSiteMap method is called.
  213. public override void Initialize (string name, NameValueCollection attributes)
  214. {
  215. if (IsInitialized)
  216. return;
  217. base.Initialize (name, attributes);
  218. initialized = true;
  219. }
  220. ///
  221. /// SiteMapProvider and StaticSiteMapProvider methods that this derived class must override.
  222. ///
  223. // Clean up any collections or other state that an instance of this may hold.
  224. protected override void Clear ()
  225. {
  226. lock (this) {
  227. rootNode = null;
  228. base.Clear ();
  229. }
  230. }
  231. // Build an in-memory representation from persistent
  232. // storage, and return the root node of the site map.
  233. public override SiteMapNode BuildSiteMap ()
  234. {
  235. // Since the SiteMap class is static, make sure that it is
  236. // not modified while the site map is built.
  237. lock (this) {
  238. // If there is no root node, then there is no site map.
  239. if (null == rootNode) {
  240. // Start with a clean slate
  241. Clear ();
  242. // Select the root node of the site map .
  243. rootNode = new SiteMapNode (this, "1", "default.aspx", "Default");
  244. }
  245. else return null;
  246. SiteMapNode childNode = null;
  247. childNode = new SiteMapNode (this, "2", "catalog.aspx", "catalog");
  248. AddNode (childNode, rootNode);
  249. childNode = new SiteMapNode (this, "3", "aboutus.aspx", "about us");
  250. AddNode (childNode, rootNode);
  251. return rootNode;
  252. }
  253. }
  254. }
  255. }
  256. #endif