WebBrowserTest.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2008 Novell, Inc.
  21. //
  22. // Authors:
  23. // Andreia Gaita ([email protected])
  24. //
  25. #if NET_2_0
  26. using System;
  27. using System.Drawing;
  28. using System.Windows.Forms;
  29. using NUnit.Framework;
  30. using io=System.IO;
  31. namespace MonoTests.System.Windows.Forms
  32. {
  33. [TestFixture]
  34. public class WebBrowserTest
  35. {
  36. WebBrowser wb = null;
  37. [SetUp]
  38. public void SetUp()
  39. {
  40. wb = new WebBrowser ();
  41. bool start = false;
  42. wb.DocumentCompleted += delegate (object sender, WebBrowserDocumentCompletedEventArgs e) {
  43. if (e.Url.Equals ("about:blank")) {
  44. start = true;
  45. }
  46. };
  47. while (!start){}
  48. }
  49. public string CreateTestPage (string html)
  50. {
  51. io.FileStream f = null;
  52. string path;
  53. Random rnd;
  54. int num = 0;
  55. rnd = new Random ();
  56. do {
  57. num = rnd.Next ();
  58. num++;
  59. path = io.Path.Combine (io.Path.GetTempPath(), "html" + num.ToString("x") + ".html");
  60. try {
  61. f = new io.FileStream (path, io.FileMode.CreateNew, io.FileAccess.ReadWrite, io.FileShare.Read,
  62. 8192, (io.FileOptions) 1);
  63. }
  64. catch (global::System.Security.SecurityException) {
  65. // avoid an endless loop
  66. throw;
  67. }
  68. catch {
  69. }
  70. } while (f == null);
  71. io.StreamWriter s = new io.StreamWriter (f);
  72. s.Write (html);
  73. f.Close();
  74. return path;
  75. }
  76. [Test]
  77. public void LoadingEventsTest()
  78. {
  79. string html = "<html><head></head><body></body></html>";
  80. string url = "file://" + CreateTestPage (html);
  81. string events = String.Empty;
  82. bool stop = false;
  83. wb.DocumentCompleted += delegate (object sender, WebBrowserDocumentCompletedEventArgs e) {
  84. if (e.Url.Equals (url)) {
  85. stop = true;
  86. Assert.AreEqual("navigatingnavigated", events, "#A1");
  87. }
  88. };
  89. wb.Navigating += delegate (object sender1, WebBrowserNavigatingEventArgs e1) {
  90. if (e1.Url.Equals (url))
  91. events += "navigating";
  92. };
  93. wb.Navigated += delegate (object sender1, WebBrowserNavigatedEventArgs e1) {
  94. if (e1.Url.Equals (url))
  95. events += "navigated";
  96. };
  97. wb.Navigate (url);
  98. while (!stop){}
  99. }
  100. [Test]
  101. public void InnerHtmlTest()
  102. {
  103. string html = "<html><head></head><body><div id=\"testid\"></div></body></html>";
  104. string url = "file://" + CreateTestPage (html);
  105. string test = "testing inner html";
  106. bool stop = false;
  107. wb.DocumentCompleted += delegate (object sender, WebBrowserDocumentCompletedEventArgs e) {
  108. Console.Error.WriteLine (wb.Document.Body.InnerHtml);
  109. if (e.Url.Equals (url)) {
  110. stop = true;
  111. Assert.IsNotNull (wb.Document, "#A1");
  112. HtmlElement elem = wb.Document.GetElementById ("testid");
  113. Assert.IsNotNull (elem, "#A2");
  114. elem.InnerHtml = test;
  115. string ret = elem.InnerHtml;
  116. Assert.AreEqual (ret, test, "#A3");
  117. }
  118. };
  119. wb.Navigate (url);
  120. while (!stop){}
  121. }
  122. }
  123. }
  124. #endif