GHTBaseWeb.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. //
  2. // Authors:
  3. // Rafael Mizrahi <[email protected]>
  4. // Erez Lotan <[email protected]>
  5. // Vladimir Krasnov <[email protected]>
  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.Collections;
  31. using System.ComponentModel;
  32. using System.Drawing;
  33. using System.Web;
  34. using System.Web.SessionState;
  35. using System.Web.UI;
  36. using System.Web.UI.WebControls;
  37. using System.Web.UI.HtmlControls;
  38. using GHTWebControls;
  39. namespace GHTTests
  40. {
  41. /// <summary>
  42. /// Summary description for GHTBaseWeb.
  43. /// </summary>
  44. public class GHTBaseWeb : System.Web.UI.Page
  45. {
  46. public Control GHTActiveForm;
  47. public Control GHTActiveSubTest;
  48. public int GHTActiveSubTestId = 0;
  49. protected override void OnInit(System.EventArgs e)
  50. {
  51. base.OnInit(e);
  52. }
  53. /// <summary>
  54. /// Save a reference to the Form.
  55. /// </summary>
  56. public virtual void GHTTestBegin(Control theForm)
  57. {
  58. GHTActiveForm = theForm;
  59. }
  60. /// <summary>
  61. /// not implemented yet. use it as a stub
  62. /// </summary>
  63. public void GHTTestEnd()
  64. {
  65. }
  66. /// <summary>
  67. ///
  68. /// </summary>
  69. public void GHTSubTestBegin()
  70. {
  71. GHTSubTestBegin("");
  72. }
  73. public void GHTSubTestBegin(string Description)
  74. {
  75. GHTSubTestCreateNew(Description);
  76. }
  77. /// <summary>
  78. /// not implemented yet. use it as a stub
  79. /// </summary>
  80. public void GHTSubTestEnd()
  81. {
  82. }
  83. /// <summary>
  84. /// Add a result to Sub Test.
  85. /// </summary>
  86. /// <param name="SubTestControl">The SubTestControl to add the result to.</param>
  87. /// <param name="TraceText">The text to add.</param>
  88. /// <param name="asText">Whether to include as plain text, or to interpret special HTML charachters as HTML.</param>
  89. public void GHTSubTestAddResult(Control SubTestControl, string TraceText, bool asText)
  90. {
  91. if (asText)
  92. {
  93. TraceText = GHTHtmlToText(TraceText);
  94. }
  95. Label lbl = new Label();
  96. lbl.Text = "<br>" + TraceText;
  97. SubTestControl.Controls.Add(lbl);
  98. // TableCell tempCell = new TableCell();
  99. // TableRow tempRow = new TableRow();
  100. // tempCell.Controls.Add( new LiteralControl(TraceText));
  101. // tempRow.Controls.Add(tempCell);
  102. // SubTestControl.Controls.Add(tempRow);
  103. }
  104. public void GHTSubTestAddResult(string TraceText)
  105. {
  106. GHTSubTestAddResult(TraceText, false);
  107. }
  108. public void GHTSubTestAddResult(string TraceText, bool asText)
  109. {
  110. GHTSubTestAddResult(GHTActiveSubTest, TraceText, asText);
  111. }
  112. public void GHTSubTestAddResult(Control SubTestControl, string TraceText)
  113. {
  114. GHTSubTestAddResult(SubTestControl, TraceText,false);
  115. }
  116. // Replaces Html special charachters to escape charachters in the returned string:
  117. // Orig --> Result
  118. //----- ------
  119. // < &lt;
  120. // > &gt;
  121. // " &quot;
  122. // & &amp;
  123. public string GHTHtmlToText(string a_text)
  124. {
  125. string res = string.Empty;
  126. res = a_text.Replace("<", "&lt;");
  127. res = res.Replace(">", "&gt;");
  128. res = res.Replace("\"", "&quot;");
  129. return res;
  130. }
  131. protected void GHTSubTestExpectedExceptionCaught(System.Exception ex)
  132. {
  133. GHTSubTestAddResult("Test passed. Expected exception was caught.");
  134. }
  135. protected void GHTSubTestExpectedExceptionNotCaught(string ExceptionName)
  136. {
  137. GHTSubTestAddResult("Test failed. Expected " + ExceptionName + " exception was not caught.");
  138. }
  139. protected void GHTSubTestUnexpectedExceptionCaught(System.Exception ex)
  140. {
  141. string traceText = string.Empty;
  142. traceText += "Test Failed. Unxpected ";
  143. traceText += ex.GetType().Name;
  144. traceText += " exception was caught";
  145. traceText += "<br>Stack Trace: ";
  146. traceText += ex.ToString();
  147. GHTSubTestAddResult(traceText);
  148. }
  149. protected void Compare(Object Result, Object ExpectedResult)
  150. {
  151. if (Result.Equals(ExpectedResult))
  152. GHTSubTestAddResult("Test Passed.");
  153. else
  154. GHTSubTestAddResult("Test Failed. Result:" + GHTNormalizeToString(Result.ToString()) + ". Expected Result:" + GHTNormalizeToString(ExpectedResult.ToString()));
  155. }
  156. /// <summary>
  157. /// Create a new SubTest conteiner.
  158. /// </summary>
  159. protected GHTWebControls.GHTSubTest GHTSubTestCreateNew()
  160. {
  161. return GHTSubTestCreateNew("");
  162. }
  163. protected GHTWebControls.GHTSubTest GHTSubTestCreateNew(string Description)
  164. {
  165. return GHTSubTestCreateNew(GHTActiveForm, Description);
  166. }
  167. protected GHTWebControls.GHTSubTest GHTSubTestCreateNew(Control theForm, string Description)
  168. {
  169. if (GHTActiveSubTestId == 0)
  170. {
  171. GHTActiveSubTestId++;
  172. }
  173. while (theForm.FindControl("GHTSubTest" + GHTActiveSubTestId) != null)
  174. {
  175. GHTActiveSubTestId++;
  176. }
  177. GHTActiveForm = theForm;
  178. GHTWebControls.GHTSubTest subtest = new GHTWebControls.GHTSubTest();
  179. subtest.ID = "GHTSubTest" + GHTActiveSubTestId;
  180. subtest.Description = Description;
  181. subtest.Attributes.Add("TestName",Description);
  182. theForm.Controls.Add(subtest);
  183. GHTActiveSubTest = subtest;
  184. return subtest;
  185. }
  186. /// <summary>
  187. /// Create a new Element (e.g. Control).
  188. /// </summary>
  189. protected Object GHTElementClone(Type type)
  190. {
  191. return type.GetConstructor(Type.EmptyTypes).Invoke(null);
  192. }
  193. /// <summary>
  194. /// Common Tests of WebControl.
  195. /// </summary>
  196. protected void GHTTestCommon_WebControl(WebControl obj, string MemberName)
  197. {
  198. WebControl objNew;
  199. bool bExceptionCaught = false;
  200. MemberName = MemberName.ToLower();
  201. //AccessKey
  202. if (MemberName.CompareTo("AccessKey".ToLower())== 0)
  203. {
  204. //Press Alt-Y to get focus here
  205. obj.AccessKey = ""; //empty is ok
  206. obj.AccessKey = "Y";
  207. objNew = (WebControl)GHTElementClone(obj.GetType());
  208. // test Exceptions
  209. try
  210. {
  211. objNew.AccessKey = "XX";
  212. GHTActiveSubTest.Controls.Add(objNew);
  213. }
  214. catch (ArgumentException)
  215. {
  216. bExceptionCaught = true;
  217. }
  218. if (bExceptionCaught == true)
  219. {
  220. GHTSubTestAddResult(GHTActiveSubTest, MemberName + " ArgumentException OK");
  221. }
  222. bExceptionCaught = false;
  223. }
  224. //Attributes tested at System.Web.UI.AttributeCollection
  225. //BackColor
  226. if (MemberName.CompareTo("BackColor".ToLower())== 0)
  227. {
  228. obj.BackColor = System.Drawing.Color.AliceBlue;
  229. objNew = (WebControl)GHTElementClone(obj.GetType());
  230. objNew.BackColor = System.Drawing.Color.Magenta;
  231. GHTActiveSubTest.Controls.Add(objNew);
  232. }
  233. //BorderColor
  234. if (MemberName.CompareTo("BorderColor".ToLower())== 0)
  235. {
  236. objNew = (WebControl)GHTElementClone(obj.GetType());
  237. objNew.BorderColor = System.Drawing.Color.AliceBlue;
  238. GHTActiveSubTest.Controls.Add(objNew);
  239. }
  240. //BorderStyle
  241. if (MemberName.CompareTo("BorderStyle".ToLower())== 0)
  242. {
  243. objNew = (WebControl)GHTElementClone(obj.GetType());
  244. objNew.BorderStyle = System.Web.UI.WebControls.BorderStyle.Dotted;
  245. GHTActiveSubTest.Controls.Add(objNew);
  246. }
  247. //BorderWidth
  248. if (MemberName.CompareTo("BorderWidth".ToLower())== 0)
  249. {
  250. objNew = (WebControl)GHTElementClone(obj.GetType());
  251. objNew.BorderWidth = 10;
  252. GHTActiveSubTest.Controls.Add(objNew);
  253. }
  254. // ControlStyle
  255. // ControlStyleCreated
  256. //CssClass
  257. if (MemberName.CompareTo("CssClass".ToLower())== 0)
  258. {
  259. objNew = (WebControl)GHTElementClone(obj.GetType());
  260. objNew.CssClass = "zoobie";
  261. GHTActiveSubTest.Controls.Add(objNew);
  262. }
  263. //Enabled
  264. if (MemberName.CompareTo("Enabled".ToLower())== 0)
  265. {
  266. objNew = (WebControl)GHTElementClone(obj.GetType());
  267. objNew.Enabled = true;
  268. GHTActiveSubTest.Controls.Add(objNew);
  269. }
  270. //Font
  271. if (MemberName.CompareTo("Font".ToLower())== 0)
  272. {
  273. GHTSubTestAddResult(GHTActiveSubTest, MemberName + " " + obj.Font.ToString());
  274. }
  275. //ForeColor
  276. if (MemberName.CompareTo("ForeColor".ToLower())== 0)
  277. {
  278. objNew = (WebControl)GHTElementClone(obj.GetType());
  279. objNew.ForeColor = System.Drawing.Color.Cornsilk;
  280. GHTActiveSubTest.Controls.Add(objNew);
  281. }
  282. //Height
  283. if (MemberName.CompareTo("Height".ToLower())== 0)
  284. {
  285. objNew = (WebControl)GHTElementClone(obj.GetType());
  286. objNew.Height = 30;
  287. GHTActiveSubTest.Controls.Add(objNew);
  288. }
  289. //Style
  290. if (MemberName.CompareTo("Style".ToLower())== 0)
  291. {
  292. GHTSubTestAddResult(GHTActiveSubTest, MemberName + " " + obj.Style.ToString());
  293. }
  294. // if (MemberName.CompareTo("TagKey".ToLower())== 0)
  295. // {
  296. // obj.TagKey = "TagKey";
  297. // }
  298. // if (MemberName.CompareTo("TagName".ToLower())== 0)
  299. // {
  300. // obj.TagName = "TagName";
  301. // }
  302. //TabIndex
  303. if (MemberName.CompareTo("TabIndex".ToLower())== 0)
  304. {
  305. objNew = (WebControl)GHTElementClone(obj.GetType());
  306. objNew.TabIndex = 2;
  307. GHTActiveSubTest.Controls.Add(objNew);
  308. }
  309. //ToolTip
  310. if (MemberName.CompareTo("ToolTip".ToLower())== 0)
  311. {
  312. objNew = (WebControl)GHTElementClone(obj.GetType());
  313. objNew.ToolTip = "ToolTip";
  314. GHTActiveSubTest.Controls.Add(objNew);
  315. }
  316. //Width
  317. if (MemberName.CompareTo("Width".ToLower())== 0)
  318. {
  319. objNew = (WebControl)GHTElementClone(obj.GetType());
  320. objNew.Width = 60;
  321. GHTActiveSubTest.Controls.Add(objNew);
  322. }
  323. }
  324. protected void GHTHeader(string text)
  325. {
  326. Label header = new Label();
  327. header.Text = "<br>" + text + "<br>";
  328. header.Font.Bold = true;
  329. header.Font.Underline = true;
  330. header.Font.Size = new FontUnit(FontSize.Larger);
  331. GHTActiveForm.Controls.Add(header);
  332. }
  333. /// <summary>
  334. /// Removes tarailing @... and preceding '_' from strings if they are found
  335. /// </summary>
  336. /// <param name="orig">The original ToString result.</param>
  337. /// <returns>Normalized string.</returns>
  338. public string GHTNormalizeToString(string a_toNormalize)
  339. {
  340. //Remove the @ at the end of the tostring.
  341. int atIndex = a_toNormalize.LastIndexOf('@');
  342. if (atIndex > -1)
  343. {
  344. a_toNormalize = a_toNormalize.Remove(atIndex, a_toNormalize.Length - atIndex);
  345. }
  346. //remove the preceding '_'
  347. if (a_toNormalize.StartsWith("_") )
  348. {
  349. a_toNormalize = a_toNormalize.Substring(1);
  350. }
  351. return a_toNormalize;
  352. }
  353. /// <summary>
  354. /// Returns the normalized to string of this page.
  355. /// </summary>
  356. /// <returns></returns>
  357. public override string ToString()
  358. {
  359. return GHTNormalizeToString(base.ToString());
  360. }
  361. }
  362. }