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