Merge pull request #217 from QuickJack/master
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / WebBrowserTest.cs
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 (avidigal@novell.com)
24 //
25
26 #if NET_2_0
27
28 using System;
29 using System.Drawing;
30 using System.Windows.Forms;
31 using NUnit.Framework;
32 using io=System.IO;
33
34 namespace MonoTests.System.Windows.Forms
35 {
36         [TestFixture]
37         public class WebBrowserTest
38         {
39                 WebBrowser wb = null;
40                 
41                 [SetUp]
42                 public void SetUp()
43                 {
44                         wb = new WebBrowser ();
45                         bool start = false;
46                         wb.DocumentCompleted += delegate (object sender, WebBrowserDocumentCompletedEventArgs e) {
47                                 if (e.Url.Equals ("about:blank")) {
48                                         start = true;
49                                 }
50                         };
51                         
52                         while (!start){}
53                 }
54                 
55                 public string CreateTestPage (string html)
56                 {
57                         io.FileStream f = null;
58                         string path;
59                         Random rnd;
60                         int num = 0;
61
62                         rnd = new Random ();
63                         do {
64                                 num = rnd.Next ();
65                                 num++;
66                                 path = io.Path.Combine (io.Path.GetTempPath(), "html" + num.ToString("x") + ".html");
67
68                                 try {
69                                         f = new io.FileStream (path, io.FileMode.CreateNew, io.FileAccess.ReadWrite, io.FileShare.Read,
70                                                             8192, (io.FileOptions) 1);
71                                 }
72                                 catch (global::System.Security.SecurityException) {
73                                         // avoid an endless loop
74                                         throw;
75                                 }
76                                 catch {
77                                 }
78                         } while (f == null);
79                         io.StreamWriter s = new io.StreamWriter (f);
80                         s.Write (html);                 
81                         f.Close();
82                         return path;
83                 }
84                 
85                 
86                 [Test]
87                 public void LoadingEventsTest()
88                 {
89                         string html = "<html><head></head><body></body></html>";
90                         string url = "file://" + CreateTestPage (html);
91                         string events = String.Empty;
92                         bool stop = false;
93                         wb.DocumentCompleted += delegate (object sender, WebBrowserDocumentCompletedEventArgs e) {
94                                 if (e.Url.Equals (url)) {
95                                         stop = true;
96                                         Assert.AreEqual("navigatingnavigated", events, "#A1");                                  
97                                 }
98                         };
99                         wb.Navigating += delegate (object sender1, WebBrowserNavigatingEventArgs e1) {
100                                 if (e1.Url.Equals (url))
101                                         events += "navigating";
102                         };
103                         wb.Navigated += delegate (object sender1, WebBrowserNavigatedEventArgs e1) {
104                                 if (e1.Url.Equals (url))
105                                         events += "navigated";
106                         };
107                         
108                         wb.Navigate (url);
109                         while (!stop){}
110                 }               
111
112                 [Test]
113                 public void InnerHtmlTest()
114                 {
115                         string html = "<html><head></head><body><div id=\"testid\"></div></body></html>";
116                         string url = "file://" + CreateTestPage (html);
117                         string test = "testing inner html";
118                         bool stop = false;
119                         wb.DocumentCompleted += delegate (object sender, WebBrowserDocumentCompletedEventArgs e) {
120                                 Console.Error.WriteLine (wb.Document.Body.InnerHtml);
121                                 if (e.Url.Equals (url)) {
122                                         stop = true;
123                                         Assert.IsNotNull (wb.Document, "#A1");
124                                         HtmlElement elem = wb.Document.GetElementById ("testid");
125                                         Assert.IsNotNull (elem, "#A2");
126                                         elem.InnerHtml = test;
127                                         string ret = elem.InnerHtml;
128                                         Assert.AreEqual (ret, test, "#A3");
129                                 }
130                         };                      
131                         wb.Navigate (url);
132                         while (!stop){}
133                 }               
134         
135         }
136 }
137 #endif