<29/05/07 by:yoni yonik@mainsoft.com>
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / SiteMapDataSourceTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.View.cs
3 //
4 // Author:
5 //      Yoni Klein (yonik@mainsoft.com)
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
29
30 #if NET_2_0
31
32 using NUnit.Framework;
33 using System;
34 using System.IO;
35 using System.Globalization;
36 using System.Configuration;
37 using System.Collections;
38 using System.Collections.Specialized;
39 using System.Web;
40 using System.Web.UI;
41 using System.Web.UI.WebControls;
42 using System.Security.Permissions;
43
44
45 namespace MonoTests.System.Web.UI.WebControls
46 {
47         class PokerSiteMapDataSource : SiteMapDataSource
48         {
49                 public PokerSiteMapDataSource ()
50                 {
51                         TrackViewState ();
52                 }
53                 public object SaveState ()
54                 {
55                         return SaveViewState ();
56                 }
57                 public void LoadState (object o)
58                 {
59                         LoadViewState (o);
60                 }
61                 public StateBag StateBag
62                 {
63                         get { return base.ViewState; }
64                 }
65                 public HierarchicalDataSourceView DoHierarchicalDataSourceView (string str)
66                 {
67                         return GetHierarchicalView (str);
68                 }
69         }
70
71         [TestFixture]
72         public class SiteMapDataSourceTest
73         {
74
75                 [Test]
76                 public void SiteMapDataSource_DefaultProperties ()
77                 {
78
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");
85                         
86                 }
87
88                 [Test]
89                 public void SiteMapDataSource_ContainsListCollection ()
90                 {       
91                         PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
92                         Assert.AreEqual (true, p.ContainsListCollection, "ContainsListCollection");
93                 }
94                 
95                 [Test]
96                 public void SiteMapDataSource_ChangeProperties ()
97                 {
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");
102
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");
109
110                         p.StartFromCurrentNode = true;
111                         Assert.AreEqual (true, p.StartFromCurrentNode, "StartFromCurrentNode");
112                         Assert.AreEqual (3, p.StateBag.Count, "StartFromCurrentNode#1");
113
114                         p.StartingNodeOffset = 1;
115                         Assert.AreEqual (1, p.StartingNodeOffset, "StartingNodeOffset");
116                         Assert.AreEqual (4, p.StateBag.Count, "StartingNodeOffset#2");
117
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");
124                 }\r
125 \r
126                 \r
127                 [Test]\r
128                 [Category("NotWorking")]\r
129                 public void SiteMapDataSource_DataSourceChanged ()\r
130                 {\r
131                         PokerSiteMapDataSource p = new PokerSiteMapDataSource ();\r
132                         ((IDataSource) p).DataSourceChanged += new EventHandler (SiteMapDataSourceTest_DataSourceChanged);\r
133                         \r
134                         eventChecker = false;\r
135                         p.ShowStartingNode = false;\r
136                         Assert.IsTrue (eventChecker, "DataSourceChanged#1");\r
137 \r
138                         eventChecker = false;\r
139                         p.SiteMapProvider = "test";\r
140                         Assert.IsTrue (eventChecker, "DataSourceChanged#2");\r
141 \r
142                         eventChecker = false;\r
143                         p.StartFromCurrentNode = true;\r
144                         Assert.IsTrue (eventChecker, "DataSourceChanged#3");\r
145 \r
146                         eventChecker = false;\r
147                         p.StartingNodeOffset = 1;\r
148                         Assert.IsTrue (eventChecker, "DataSourceChanged#4");\r
149 \r
150                         eventChecker = false;\r
151                         p.StartingNodeUrl = "default.aspx";\r
152                         Assert.IsTrue (eventChecker, "DataSourceChanged#5");\r
153                 }\r
154 \r
155                 bool eventChecker;\r
156                 void SiteMapDataSourceTest_DataSourceChanged (object sender, EventArgs e)\r
157                 {\r
158                         eventChecker = true;\r
159                 }
160
161
162                 [Test]
163                 public void SiteMapDataSource_GetList ()
164                 {
165                         PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
166                         Assert.IsNotNull (p.GetList (), "GetList");
167                         Assert.IsTrue (p.ContainsListCollection, "ContainsListCollection");
168                 }
169
170                 [Test]
171                 public void SiteMapDataSource_GetViewNames () {
172                         PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
173                         Assert.AreEqual (1, p.GetViewNames ().Count, "GetViewNames().Count");
174                         Assert.AreEqual ("DefaultView", ((string []) p.GetViewNames ()) [0], "GetViewNames () [0]");
175                 }
176                 
177                 [Test]
178                 public void SiteMapDataSource_GetView ()
179                 {
180                         PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
181                         p.Provider = new mySiteMapProvider ();
182                         DataSourceView V  = p.GetView("");
183                         Assert.IsNotNull (V, "GetView");
184                 }
185
186                 [Test]
187                 public void SiteMapDataSource_ViewState ()
188                 {
189                         PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
190                         p.SiteMapProvider = "test";
191
192                         p.StartFromCurrentNode = false;
193                         p.StartingNodeOffset = 1;
194                         p.StartingNodeUrl = "default.aspx";
195
196                         object state = p.SaveState ();
197                         PokerSiteMapDataSource copy = new PokerSiteMapDataSource ();
198                         copy.LoadState (state);
199
200                         Assert.AreEqual ("test", copy.SiteMapProvider, "SiteMapProvider");
201                         Assert.AreEqual (false, copy.StartFromCurrentNode, "StartFromCurrentNode");
202                         Assert.AreEqual (1, copy.StartingNodeOffset, "StartingNodeOffset");
203                         Assert.AreEqual ("default.aspx", copy.StartingNodeUrl, "StartingNodeUrl");
204                 }
205
206                 [Test]
207                 public void SiteMapDataSource_HierarchicalDataSourceView ()
208                 {
209                         PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
210                         p.Provider = new mySiteMapProvider ();
211                         HierarchicalDataSourceView h = p.DoHierarchicalDataSourceView ("1");
212                         Assert.IsNotNull (h, "HierarchicalDataSourceView");
213                 }
214
215         }
216
217    
218         // Helper Class
219         public class mySiteMapProvider : StaticSiteMapProvider
220         {
221                 private SiteMapNode rootNode = null;
222                 // Implement a default constructor.
223                 public mySiteMapProvider ()
224                 {
225                 }
226                 // Some basic state to help track the initialization state of the provider.
227                 private bool initialized = false;
228                 public virtual bool IsInitialized
229                 {
230                         get { return initialized; }
231                 }
232
233                 // Return the root node of the current site map.
234                 public override SiteMapNode RootNode
235                 {
236                         get
237                         {
238                                 SiteMapNode temp = null;
239                                 temp = BuildSiteMap ();
240                                 return temp;
241                         }
242                 }
243                 protected override SiteMapNode GetRootNodeCore ()
244                 {
245                         return RootNode;
246                 }
247                 // Initialize is used to initialize the properties and any state that the
248                 // AccessProvider holds, but is not used to build the site map.
249                 // The site map is built when the BuildSiteMap method is called.
250                 public override void Initialize (string name, NameValueCollection attributes)
251                 {
252                         if (IsInitialized)
253                                 return;
254
255                         base.Initialize (name, attributes);
256                         initialized = true;
257                 }
258
259                 ///
260                 /// SiteMapProvider and StaticSiteMapProvider methods that this derived class must override.
261                 ///
262                 // Clean up any collections or other state that an instance of this may hold.
263                 protected override void Clear ()
264                 {
265                         lock (this) {
266                                 rootNode = null;
267                                 base.Clear ();
268                         }
269                 }
270
271                 // Build an in-memory representation from persistent
272                 // storage, and return the root node of the site map.
273                 public override SiteMapNode BuildSiteMap ()
274                 {
275                         // Since the SiteMap class is static, make sure that it is
276                         // not modified while the site map is built.
277                         lock (this) {
278
279                                 // If there is no root node, then there is no site map.
280                                 if (null == rootNode) {
281                                         // Start with a clean slate
282                                         Clear ();
283
284                                         // Select the root node of the site map .
285                                         rootNode = new SiteMapNode (this, "1", "default.aspx", "Default");
286
287                                 }
288
289                                 else return null;
290
291                                 SiteMapNode childNode = null;
292                                 childNode = new SiteMapNode (this, "2", "catalog.aspx", "catalog");
293                                 AddNode (childNode, rootNode);
294                                 childNode = new SiteMapNode (this, "3", "aboutus.aspx", "about us");
295                                 AddNode (childNode, rootNode);
296
297                                 return rootNode;
298                         }
299                 }
300         }
301 }
302 #endif