2 // Tests for System.Web.UI.WebControls.View.cs
5 // Yoni Klein (yonik@mainsoft.com)
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
32 using NUnit.Framework;
35 using System.Globalization;
36 using System.Configuration;
37 using System.Collections;
38 using System.Collections.Specialized;
41 using System.Web.UI.WebControls;
42 using System.Security.Permissions;
45 namespace MonoTests.System.Web.UI.WebControls
47 class PokerSiteMapDataSource : SiteMapDataSource
49 public PokerSiteMapDataSource ()
53 public object SaveState ()
55 return SaveViewState ();
57 public void LoadState (object o)
61 public StateBag StateBag
63 get { return base.ViewState; }
65 public HierarchicalDataSourceView DoHierarchicalDataSourceView (string str)
67 return GetHierarchicalView (str);
72 public class SiteMapDataSourceTest
76 public void SiteMapDataSource_DefaultProperties ()
79 PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
80 Assert.AreEqual (true, p.ShowStartingNode, "ShowStartingNode");
81 Assert.AreEqual (string.Empty, p.SiteMapProvider, "SiteMapProvider");
82 Assert.AreEqual (false, p.StartFromCurrentNode, "StartFromCurrentNode");
83 Assert.AreEqual (0, p.StartingNodeOffset, "StartingNodeOffset");
84 Assert.AreEqual (string.Empty, p.StartingNodeUrl, "StartingNodeUrl");
89 public void SiteMapDataSource_ContainsListCollection ()
91 PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
92 Assert.AreEqual (true, p.ContainsListCollection, "ContainsListCollection");
96 public void SiteMapDataSource_ChangeProperties ()
98 PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
99 p.ShowStartingNode = false;
100 Assert.AreEqual (false, p.ShowStartingNode, "ShowStartingNode");
101 Assert.AreEqual (1, p.StateBag.Count, "ShowStartingNode#1");
103 p.SiteMapProvider = "test";
104 Assert.AreEqual ("test", p.SiteMapProvider, "SiteMapProvider");
105 Assert.AreEqual (2, p.StateBag.Count, "SiteMapProvider#1");
106 // null properties doe's not affects on state bag count
107 p.SiteMapProvider = null;
108 Assert.AreEqual (2, p.StateBag.Count, "SiteMapProvider#2");
110 p.StartFromCurrentNode = true;
111 Assert.AreEqual (true, p.StartFromCurrentNode, "StartFromCurrentNode");
112 Assert.AreEqual (3, p.StateBag.Count, "StartFromCurrentNode#1");
114 p.StartingNodeOffset = 1;
115 Assert.AreEqual (1, p.StartingNodeOffset, "StartingNodeOffset");
116 Assert.AreEqual (4, p.StateBag.Count, "StartingNodeOffset#2");
118 p.StartingNodeUrl = "default.aspx";
119 Assert.AreEqual ("default.aspx", p.StartingNodeUrl, "StartingNodeUrl");
120 Assert.AreEqual (5, p.StateBag.Count, "StartingNodeUrl#1");
121 // null properties doe's not affects on state bag count
122 p.StartingNodeUrl = null;
123 Assert.AreEqual (5, p.StateBag.Count, "StartingNodeUrl#2");
127 public void SiteMapDataSource_GetList ()
129 PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
130 Assert.IsNotNull (p.GetList (), "GetList");
131 Assert.IsTrue (p.ContainsListCollection, "ContainsListCollection");
135 public void SiteMapDataSource_GetViewNames () {
136 PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
137 Assert.AreEqual (1, p.GetViewNames ().Count, "GetViewNames().Count");
138 Assert.AreEqual ("DefaultView", ((string []) p.GetViewNames ()) [0], "GetViewNames () [0]");
142 public void SiteMapDataSource_GetView ()
144 PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
145 p.Provider = new mySiteMapProvider ();
146 DataSourceView V = p.GetView("");
147 Assert.IsNotNull (V, "GetView");
151 public void SiteMapDataSource_ViewState ()
153 PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
154 p.SiteMapProvider = "test";
156 p.StartFromCurrentNode = false;
157 p.StartingNodeOffset = 1;
158 p.StartingNodeUrl = "default.aspx";
160 object state = p.SaveState ();
161 PokerSiteMapDataSource copy = new PokerSiteMapDataSource ();
162 copy.LoadState (state);
164 Assert.AreEqual ("test", copy.SiteMapProvider, "SiteMapProvider");
165 Assert.AreEqual (false, copy.StartFromCurrentNode, "StartFromCurrentNode");
166 Assert.AreEqual (1, copy.StartingNodeOffset, "StartingNodeOffset");
167 Assert.AreEqual ("default.aspx", copy.StartingNodeUrl, "StartingNodeUrl");
171 public void SiteMapDataSource_HierarchicalDataSourceView ()
173 PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
174 p.Provider = new mySiteMapProvider ();
175 HierarchicalDataSourceView h = p.DoHierarchicalDataSourceView ("1");
176 Assert.IsNotNull (h, "HierarchicalDataSourceView");
183 public class mySiteMapProvider : StaticSiteMapProvider
185 private SiteMapNode rootNode = null;
186 // Implement a default constructor.
187 public mySiteMapProvider ()
190 // Some basic state to help track the initialization state of the provider.
191 private bool initialized = false;
192 public virtual bool IsInitialized
194 get { return initialized; }
197 // Return the root node of the current site map.
198 public override SiteMapNode RootNode
202 SiteMapNode temp = null;
203 temp = BuildSiteMap ();
207 protected override SiteMapNode GetRootNodeCore ()
211 // Initialize is used to initialize the properties and any state that the
212 // AccessProvider holds, but is not used to build the site map.
213 // The site map is built when the BuildSiteMap method is called.
214 public override void Initialize (string name, NameValueCollection attributes)
219 base.Initialize (name, attributes);
224 /// SiteMapProvider and StaticSiteMapProvider methods that this derived class must override.
226 // Clean up any collections or other state that an instance of this may hold.
227 protected override void Clear ()
235 // Build an in-memory representation from persistent
236 // storage, and return the root node of the site map.
237 public override SiteMapNode BuildSiteMap ()
239 // Since the SiteMap class is static, make sure that it is
240 // not modified while the site map is built.
243 // If there is no root node, then there is no site map.
244 if (null == rootNode) {
245 // Start with a clean slate
248 // Select the root node of the site map .
249 rootNode = new SiteMapNode (this, "1", "default.aspx", "Default");
255 SiteMapNode childNode = null;
256 childNode = new SiteMapNode (this, "2", "catalog.aspx", "catalog");
257 AddNode (childNode, rootNode);
258 childNode = new SiteMapNode (this, "3", "aboutus.aspx", "about us");
259 AddNode (childNode, rootNode);