View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.html;
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  
23  /**
24   * Unit tests for {@link HTMLTitleElement}.
25   * @author Sudhan Moghe
26   * @author Ronald Brill
27   */
28  @RunWith(BrowserRunner.class)
29  public class HTMLTitleElementTest extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if the test fails
33       */
34      @Test
35      @Alerts({"Page Title", "New Title"})
36      public void text() throws Exception {
37          final String html = DOCTYPE_HTML
38              + "<html>\n"
39              + "  <head>\n"
40              + "    <title>Page Title</title>\n"
41              + "    <script>\n"
42              + LOG_TEXTAREA_FUNCTION
43              + "      function test() {\n"
44              + "        var title = document.getElementsByTagName('title')[0];\n"
45              + "        log(title.text);\n"
46              + "        title.text = 'New Title';\n"
47              + "        log(title.text);\n"
48              + "      }\n"
49              + "    </script>\n"
50              + "  </head>\n"
51              + "  <body onload='test()'>\n"
52              + LOG_TEXTAREA
53              + "  </body>\n"
54              + "</html>";
55  
56          loadPageVerifyTextArea2(html);
57      }
58  
59      /**
60       * @throws Exception if the test fails
61       */
62      @Test
63      @Alerts({"", "New Title"})
64      public void textCreateElement() throws Exception {
65          final String html = DOCTYPE_HTML
66              + "<html>\n"
67              + "  <head>\n"
68              + "    <script>\n"
69              + LOG_TEXTAREA_FUNCTION
70              + "      function test() {\n"
71              + "        var title = document.createElement('title');\n"
72              + "        log(title.text);\n"
73              + "        title.text = 'New Title';\n"
74              + "        log(title.text);\n"
75              + "      }\n"
76              + "    </script>\n"
77              + "  </head>\n"
78              + "  <body onload='test()'>\n"
79              + LOG_TEXTAREA
80              + "  </body>\n"
81              + "</html>";
82  
83          loadPageVerifyTextArea2(html);
84      }
85  
86      /**
87       * @throws Exception if the test fails
88       */
89      @Test
90      @Alerts({"Page Title", "</> htmx rocks!", "</> htmx rocks!"})
91      public void innerHtml() throws Exception {
92          final String html = DOCTYPE_HTML
93              + "<html>\n"
94              + "  <head>\n"
95              + "    <title>Page Title</title>\n"
96              + "    <script>\n"
97              + LOG_TEXTAREA_FUNCTION
98              + "      function test() {\n"
99              + "        var title = document.getElementsByTagName('title')[0];\n"
100             + "        log(title.text);\n"
101             + "        title.innerHTML = '</> htmx rocks!';\n"
102             + "        log(title.text);\n"
103             + "        log(window.document.title);\n"
104             + "      }\n"
105             + "    </script>\n"
106             + "  </head>\n"
107             + "  <body onload='test()'>\n"
108             + LOG_TEXTAREA
109             + "  </body>\n"
110             + "</html>";
111 
112         loadPageVerifyTextArea2(html);
113     }
114 
115     /**
116      * @throws Exception if the test fails
117      */
118     @Test
119     @Alerts({"Page Title", "<div>htmx rocks</div>", "<div>htmx rocks</div>"})
120     public void innerHtmlTag() throws Exception {
121         final String html = DOCTYPE_HTML
122             + "<html>\n"
123             + "  <head>\n"
124             + "    <title>Page Title</title>\n"
125             + "    <script>\n"
126             + LOG_TEXTAREA_FUNCTION
127             + "      function test() {\n"
128             + "        var title = document.getElementsByTagName('title')[0];\n"
129             + "        log(title.text);\n"
130             + "        title.innerHTML = '<div>htmx rocks</div>';\n"
131             + "        log(title.text);\n"
132             + "        log(window.document.title);\n"
133             + "      }\n"
134             + "    </script>\n"
135             + "  </head>\n"
136             + "  <body onload='test()'>\n"
137             + LOG_TEXTAREA
138             + "  </body>\n"
139             + "</html>";
140 
141         loadPageVerifyTextArea2(html);
142     }
143 
144     /**
145      * @throws Exception if the test fails
146      */
147     @Test
148     @Alerts({"", "</> htmx rocks!", "</> htmx rocks!"})
149     public void innerHtmlEscaping() throws Exception {
150         final String html = DOCTYPE_HTML
151             + "<html>\n"
152             + "  <head>\n"
153             + "    <title></title>\n"
154             + "    <script>\n"
155             + LOG_TEXTAREA_FUNCTION
156             + "      function test() {\n"
157             + "        var title = document.getElementsByTagName('title')[0];\n"
158             + "        log(title.text);\n"
159             + "        title.innerHTML = '&lt;/> htmx rocks!';\n"
160             + "        log(title.text);\n"
161             + "        log(window.document.title);\n"
162             + "      }\n"
163             + "    </script>\n"
164             + "  </head>\n"
165             + "  <body onload='test()'>\n"
166             + LOG_TEXTAREA
167             + "  </body>\n"
168             + "</html>";
169 
170         loadPageVerifyTextArea2(html);
171     }
172 }