1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.event;
16
17 import org.htmlunit.WebDriverTestCase;
18 import org.htmlunit.junit.BrowserRunner;
19 import org.htmlunit.junit.annotation.Alerts;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.openqa.selenium.WebDriver;
23
24
25
26
27
28
29
30 @RunWith(BrowserRunner.class)
31 public class EventHandlerTest extends WebDriverTestCase {
32
33
34
35
36 @Test
37 public void caller() throws Exception {
38 final String html = DOCTYPE_HTML
39 + "<html><head>\n"
40 + "<script>\n"
41 + LOG_TITLE_FUNCTION
42 + " function test() {\n"
43 + " log(test.caller);\n"
44 + " }\n"
45 + "</script></head>\n"
46 + "<body onload='test()'>\n"
47 + "</body></html>";
48
49 final WebDriver driver = loadPage2(html);
50 assertTrue(driver.getTitle().contains("function onload(event)"));
51 }
52
53
54
55
56 @Test
57 @Alerts({"function onload(event) { test() }",
58 "function onload(event) { test() }",
59 "function onload(event) { test() }"})
60 public void testToString() throws Exception {
61 final String html = DOCTYPE_HTML
62 + "<html><head>\n"
63 + "<script>\n"
64 + LOG_TITLE_FUNCTION
65 + " function test() {\n"
66 + " var e = test.caller;\n"
67 + " log(e);\n"
68 + " log('' + e);\n"
69 + " log(e.toString());\n"
70 + " }\n"
71 + "</script></head>\n"
72 + "<body onload='test()'>\n"
73 + "</body></html>";
74
75 loadPageVerifyTitle2(html);
76 }
77
78
79
80
81 @Test
82 @Alerts({"function onload(event) { test() }",
83 "function onload(event) { test() }",
84 "function onload(event) { test() }"})
85 public void testToStringWhitespace() throws Exception {
86 final String html = DOCTYPE_HTML
87 + "<html><head>\n"
88 + "<script>\n"
89 + LOG_TITLE_FUNCTION
90 + " function test() {\n"
91 + " var e = test.caller;\n"
92 + " log(e);\n"
93 + " log('' + e);\n"
94 + " log(e.toString());\n"
95 + " }\n"
96 + "</script></head>\n"
97 + "<body onload=' test() \t \n'>\n"
98 + "</body></html>";
99
100 loadPageVerifyTitle2(html);
101 }
102
103
104
105
106 @Test
107 @Alerts({"function onload(event) { test() // comment }",
108 "function onload(event) { test() // comment }",
109 "function onload(event) { test() // comment }"})
110 public void testToStringCommentAtEnd() throws Exception {
111 final String html = DOCTYPE_HTML
112 + "<html><head>\n"
113 + "<script>\n"
114 + LOG_TITLE_FUNCTION
115 + " function test() {\n"
116 + " var e = test.caller;\n"
117 + " log(e);\n"
118 + " log('' + e);\n"
119 + " log(e.toString());\n"
120 + " }\n"
121 + "</script></head>\n"
122 + "<body onload='test() // comment'>\n"
123 + "</body></html>";
124
125 loadPageVerifyTitle2(html);
126 }
127 }