svn path=/trunk/mcs/; revision=60908
[mono.git] / mcs / class / System.Web / Test / mainsoft / MainsoftWebTest / NunitWebTest.cs
1 //
2 // Authors:
3 //   Rafael Mizrahi   <rafim@mainsoft.com>
4 //   Erez Lotan       <erezl@mainsoft.com>
5 //   Vladimir Krasnov <vladimirk@mainsoft.com>
6 //   
7 // 
8 // Copyright (c) 2002-2005 Mainsoft Corporation.
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.IO;
31 using System.Xml;
32 using System.Net;
33 using System.Text;
34 using System.Collections;
35 using System.Reflection;
36 using NUnit.Framework;
37
38 namespace MonoTests.stand_alone.WebHarness
39 {
40         public abstract class XmlComparableTest
41         {
42                 public abstract bool XmlCompare(XmlDocument d1, XmlDocument d2, bool ignoreAlmost);
43         }
44
45         public class WebTest : XmlComparableTest
46         {
47                 public const string BEGIN_TAG = "begint";
48                 public const string END_TAG = "endt";
49
50                 private XmlDocument _xmlIgnoreList = null;
51                 private string _compareStatus = "";
52                 private static string _compareActual = "";
53                 private static string _compareExpect = "";
54                 private string _ignoreListFile = "";
55
56                 
57
58                 public WebTest()
59                 {
60                 }
61
62                 public string IgnoreListFile
63                 {
64                         get {return _ignoreListFile;}
65                         set {_ignoreListFile = value;}
66                 }
67
68                 public string CompareStatus
69                 {
70                         get {return _compareStatus.ToString();}
71                 }
72
73                 public static string GetControlFromPageHtml (string str)
74                 {
75                         StringBuilder sb = new StringBuilder ();
76                         sb.Append (str.Substring (str.IndexOf (BEGIN_TAG) + BEGIN_TAG.Length, str.IndexOf (END_TAG) - str.IndexOf (BEGIN_TAG) - BEGIN_TAG.Length));
77                         return sb.ToString ();
78                 }
79
80                 public static void AssertAreEqual (string origin, string derived, string msg)
81                 {
82                         bool test = HtmlComparer (origin, derived);
83                         if (!test) {
84                                 Assert.AreEqual (_compareActual, _compareExpect, msg);
85                                        
86                         }
87                 }
88
89                 public static bool HtmlComparer (string origin, string derived)
90                 {
91                         XmlDocument or = new XmlDocument ();
92                         MonoTests.stand_alone.WebHarness.WebTest helper = new MonoTests.stand_alone.WebHarness.WebTest ();
93                         or.LoadXml (helper.HtmltoXml (origin));
94                         XmlDocument dr = new XmlDocument ();
95                         dr.LoadXml (helper.HtmltoXml (derived));
96                         return helper.XmlCompare (or, dr, false);
97                 }
98
99                 public override bool XmlCompare(XmlDocument d1, XmlDocument d2, bool ignoreAlmost)
100                 {
101                         XmlComparer comparer = new XmlComparer();
102                         if (ignoreAlmost == false)
103                         {
104                                 DoAlmost(d1);
105                                 DoAlmost(d2);
106                         }
107                         bool c = comparer.AreEqual(d1, d2);
108                         _compareStatus = comparer.LastCompare;
109                         _compareActual = comparer.Actual;
110                         _compareExpect = comparer.Expected;
111                         return c;
112                 }
113
114                 public string HtmltoXml(string html)
115                 {
116                         HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
117                         doc.LoadHtml(html);
118
119                         StringBuilder fixedxml = new StringBuilder();
120                         StringWriter sw = new StringWriter(fixedxml);
121
122                         try
123                         {
124                                 StringBuilder tempxml = new StringBuilder();
125                                 StringWriter tsw = new StringWriter(tempxml);
126
127                                 doc.OptionOutputAsXml = true;
128                                 doc.Save(tsw);
129
130                                 // fix style attribute
131                                 // the reason is that style attribute name-value pairs come in different order
132                                 // in .NET and GH
133                                 // Here I will sort the values of style attribute
134                                 XmlDocument tempDoc = new XmlDocument();
135                                 tempDoc.LoadXml(tempxml.ToString());
136
137                                 XmlNodeList allNodes = tempDoc.SelectNodes("//*");
138                                 foreach (XmlNode n in allNodes)
139                                 {
140                                         if (n.Attributes["style"] != null)
141                                         {
142                                                 string att = n.Attributes["style"].Value;
143                                                 string [] style = att.Trim(new char[]{' ', ';'}).Split(';');
144
145                                                 for (int styleIndex=0; styleIndex<style.Length; styleIndex++)
146                                                 {
147                                                         style[styleIndex] = FixStyleNameValue(style[styleIndex]);
148                                                 }
149                                                 Array.Sort(style);
150                                                 n.Attributes["style"].Value = string.Join(";", style);
151                                         }
152                                 }
153                                 tempDoc.Save(sw);
154                         }
155                         catch (Exception)
156                         {
157                                 Console.WriteLine("Error parsing html response...");
158                                 Console.WriteLine("Test case aborted");
159                                 return "<TestCaseAborted></TestCaseAborted>";
160                         }
161                         return fixedxml.ToString();
162                 }
163
164                 private string FixStyleNameValue(string nameValue)
165                 {
166                         string [] nv = nameValue.Split(':');
167                         // value may contain spaces in case of
168                         // multiple values for one key
169                         string [] nvalue = nv[1].Trim().Split(' ');
170                         Array.Sort(nvalue);
171                         nv[1] = string.Join(" ", nvalue);
172                         return nv[0].Trim().ToLower() + ":" + nv[1].Trim().ToLower();
173                 }
174
175                 private void DoAlmost(XmlDocument xmlDocument)
176                 {
177                         XmlNode XmlIgnoreNode;
178                         IEnumerator xmlIgnoreEnum;
179
180                         if (_xmlIgnoreList == null)
181                         {
182                                 _xmlIgnoreList = new XmlDocument();
183                                 string xml;
184                                 using (Stream source = Assembly.GetExecutingAssembly()
185                                         .GetManifestResourceStream ("HTMLComparer.nunitweb_config.xml")) {
186                                         using (StreamReader sr = new StreamReader (source))
187                                                 xml = sr.ReadToEnd ();
188                                 }
189                                 _xmlIgnoreList.LoadXml (xml);
190                         }
191
192                         // Remove by Id or Name
193                         // search by tag and if id or name match, remove all attributes
194                         // must be the first almost since the following almost delete the id and name
195                         xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/RemoveById").GetEnumerator();
196                         while (xmlIgnoreEnum.MoveNext())
197                         {
198                                 XmlNodeList DocNodeList;
199                                 XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
200                                 DocNodeList = xmlDocument.GetElementsByTagName("*");
201                                 if (DocNodeList != null)
202                                 {
203                                         foreach (XmlElement tmpXmlElement in DocNodeList)
204                                         {
205                                                 foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
206                                                 {
207                                                         if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower()) 
208                                                         {
209                                                                 if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
210                                                                 {
211                                                                         if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower() == tmpIgnoreAttr.Value.ToLower())
212                                                                         {
213                                                                                 tmpXmlElement.RemoveAllAttributes();
214                                                                         }
215                                                                 }
216                                                         }
217                                                 }
218                                         }
219                                 }       
220                         }
221                         // remove ignored attributes
222                         // search for tag and remove it's attributes
223                         xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/IgnoreList").GetEnumerator(); //FirstChild.GetEnumerator
224                         while (xmlIgnoreEnum.MoveNext())
225                         {
226                                 XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
227                                 XmlNodeList DocNodeList;
228                                 //clean specific element
229
230                                 DocNodeList = xmlDocument.GetElementsByTagName("*");
231                                 if (DocNodeList != null)
232                                 {
233                                         foreach (XmlElement tmpXmlElement in DocNodeList)
234                                         {
235                                                 if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower()) 
236                                                 {
237                                                         foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
238                                                         {
239                                                                 tmpXmlElement.RemoveAttribute(tmpIgnoreAttr.Name);
240                                                         }
241                                                 }
242                                         }
243                                 }
244                         }
245
246                         // clean javascript attribute value
247                         xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/CleanJavaScriptValueList").GetEnumerator(); //FirstChild.GetEnumerator
248                         while (xmlIgnoreEnum.MoveNext())
249                         {
250                                 XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
251                                 XmlNodeList DocNodeList;
252                                 //clean Java Script attribute values
253                                 DocNodeList = xmlDocument.GetElementsByTagName("*");
254                                 if (DocNodeList != null)
255                                 {
256                                         foreach (XmlElement tmpXmlElement in DocNodeList)
257                                         {
258                                                 if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower()) 
259                                                 {
260                                                         foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
261                                                         {
262                                                                 if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
263                                                                 {
264                                                                         if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower().IndexOf("javascript") >= 0 )
265                                                                         {
266                                                                                 tmpXmlElement.SetAttribute(tmpIgnoreAttr.Name, "");
267                                                                         }
268                                                                 }
269                                                         }
270                                                 }
271                                         }
272                                 }
273                         }
274                 }
275         }
276 }