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.html.HtmlDirectory;
19  import org.htmlunit.junit.BrowserRunner;
20  import org.htmlunit.junit.annotation.Alerts;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  import org.openqa.selenium.By;
24  import org.openqa.selenium.WebDriver;
25  import org.openqa.selenium.WebElement;
26  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
27  
28  /**
29   * Unit tests for {@link HTMLDirectoryElement}.
30   *
31   * @author Daniel Gredler
32   * @author Frank Danek
33   * @author Ronald Brill
34   */
35  @RunWith(BrowserRunner.class)
36  public class HTMLDirectoryElementTest extends WebDriverTestCase {
37  
38      /**
39       * @throws Exception if the test fails
40       */
41      @Test
42      @Alerts("[object HTMLDirectoryElement]")
43      public void simpleScriptable() throws Exception {
44          final String html = DOCTYPE_HTML
45              + "<html><head>\n"
46              + "<script>\n"
47              + LOG_TITLE_FUNCTION
48              + "  function test() {\n"
49              + "    log(document.getElementById('myId'));\n"
50              + "  }\n"
51              + "</script>\n"
52              + "</head><body onload='test()'>\n"
53              + "  <dir id='myId'/>\n"
54              + "</body></html>";
55  
56          final WebDriver driver = loadPageVerifyTitle2(html);
57          if (driver instanceof HtmlUnitDriver) {
58              final WebElement element = driver.findElement(By.id("myId"));
59              assertTrue(toHtmlElement(element) instanceof HtmlDirectory);
60          }
61      }
62  
63      /**
64       * @throws Exception if an error occurs
65       */
66      @Test
67      @Alerts({"false", "true", "true", "true", "null", "", "blah", "2",
68               "true", "false", "true", "false", "", "null", "", "null"})
69      public void compact() throws Exception {
70          final String html = DOCTYPE_HTML
71                  + "<html>\n"
72                  + "  <head>\n"
73                  + "    <script>\n"
74                  + LOG_TITLE_FUNCTION
75                  + "      function test() {\n"
76                  + "        log(document.getElementById('dir1').compact);\n"
77                  + "        log(document.getElementById('dir2').compact);\n"
78                  + "        log(document.getElementById('dir3').compact);\n"
79                  + "        log(document.getElementById('dir4').compact);\n"
80                  + "        log(document.getElementById('dir1').getAttribute('compact'));\n"
81                  + "        log(document.getElementById('dir2').getAttribute('compact'));\n"
82                  + "        log(document.getElementById('dir3').getAttribute('compact'));\n"
83                  + "        log(document.getElementById('dir4').getAttribute('compact'));\n"
84  
85                  + "        document.getElementById('dir1').compact = true;\n"
86                  + "        document.getElementById('dir2').compact = false;\n"
87                  + "        document.getElementById('dir3').compact = 'xyz';\n"
88                  + "        document.getElementById('dir4').compact = null;\n"
89                  + "        log(document.getElementById('dir1').compact);\n"
90                  + "        log(document.getElementById('dir2').compact);\n"
91                  + "        log(document.getElementById('dir3').compact);\n"
92                  + "        log(document.getElementById('dir4').compact);\n"
93                  + "        log(document.getElementById('dir1').getAttribute('compact'));\n"
94                  + "        log(document.getElementById('dir2').getAttribute('compact'));\n"
95                  + "        log(document.getElementById('dir3').getAttribute('compact'));\n"
96                  + "        log(document.getElementById('dir4').getAttribute('compact'));\n"
97                  + "      }\n"
98                  + "    </script>\n"
99                  + "  </head>\n"
100                 + "  <body onload='test()'>\n"
101                 + "    <dir id='dir1'><li>a</li><li>b</li></dir>\n"
102                 + "    <dir compact='' id='dir2'><li>a</li><li>b</li></dir>\n"
103                 + "    <dir compact='blah' id='dir3'><li>a</li><li>b</li></dir>\n"
104                 + "    <dir compact='2' id='dir4'><li>a</li><li>b</li></dir>\n"
105                 + "  </body>\n"
106                 + "</html>";
107 
108         loadPageVerifyTitle2(html);
109     }
110 
111     /**
112      * @throws Exception if an error occurs
113      */
114     @Test
115     @Alerts({"undefined", "undefined", "undefined", "undefined",
116              "null", "", "blah", "A", "1", "a", "A", "i", "I", "u"})
117     public void type() throws Exception {
118         final String html = DOCTYPE_HTML
119                 + "<html>\n"
120                 + "  <head>\n"
121                 + "    <script>\n"
122                 + LOG_TITLE_FUNCTION
123                 + "      function test() {\n"
124                 + "        log(document.getElementById('dir1').type);\n"
125                 + "        log(document.getElementById('dir2').type);\n"
126                 + "        log(document.getElementById('dir3').type);\n"
127                 + "        log(document.getElementById('dir4').type);\n"
128                 + "        log(document.getElementById('dir1').getAttribute('type'));\n"
129                 + "        log(document.getElementById('dir2').getAttribute('type'));\n"
130                 + "        log(document.getElementById('dir3').getAttribute('type'));\n"
131                 + "        log(document.getElementById('dir4').getAttribute('type'));\n"
132 
133                 + "        document.getElementById('dir1').type = '1';\n"
134                 + "        log(document.getElementById('dir1').type);\n"
135 
136                 + "        document.getElementById('dir1').type = 'a';\n"
137                 + "        log(document.getElementById('dir1').type);\n"
138 
139                 + "        document.getElementById('dir1').type = 'A';\n"
140                 + "        log(document.getElementById('dir1').type);\n"
141 
142                 + "        document.getElementById('dir1').type = 'i';\n"
143                 + "        log(document.getElementById('dir1').type);\n"
144 
145                 + "        document.getElementById('dir1').type = 'I';\n"
146                 + "        log(document.getElementById('dir1').type);\n"
147 
148                 + "        try { document.getElementById('dir1').type = 'u' } catch(e) {logEx(e);}\n"
149                 + "        log(document.getElementById('dir1').type);\n"
150                 + "      }\n"
151                 + "    </script>\n"
152                 + "  </head>\n"
153                 + "  <body onload='test()'>\n"
154                 + "    <dir id='dir1'><li>a</li><li>b</li></dir>\n"
155                 + "    <dir type='' id='dir2'><li>a</li><li>b</li></dir>\n"
156                 + "    <dir type='blah' id='dir3'><li>a</li><li>b</li></dir>\n"
157                 + "    <dir type='A' id='dir4'><li>a</li><li>b</li></dir>\n"
158                 + "  </body>\n"
159                 + "</html>";
160 
161         loadPageVerifyTitle2(html);
162     }
163 }