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.HtmlMenu;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.junit.jupiter.api.Test;
21  import org.openqa.selenium.By;
22  import org.openqa.selenium.WebDriver;
23  import org.openqa.selenium.WebElement;
24  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
25  
26  /**
27   * Unit tests for {@link HTMLMenuElement}.
28   *
29   * @author Daniel Gredler
30   * @author Ronald Brill
31   * @author Frank Danek
32   */
33  public class HTMLMenuElementTest extends WebDriverTestCase {
34  
35      /**
36       * @throws Exception if the test fails
37       */
38      @Test
39      @Alerts("[object HTMLMenuElement]")
40      public void simpleScriptable() throws Exception {
41          final String html = DOCTYPE_HTML
42              + "<html><head>\n"
43              + "<script>\n"
44              + LOG_TITLE_FUNCTION
45              + "  function test() {\n"
46              + "    log(document.getElementById('myId'));\n"
47              + "  }\n"
48              + "</script>\n"
49              + "</head><body onload='test()'>\n"
50              + "  <menu id='myId'/>\n"
51              + "</body></html>";
52  
53          final WebDriver driver = loadPageVerifyTitle2(html);
54          if (driver instanceof HtmlUnitDriver) {
55              final WebElement element = driver.findElement(By.id("myId"));
56              assertTrue(toHtmlElement(element) instanceof HtmlMenu);
57          }
58      }
59  
60      /**
61       * @throws Exception if an error occurs
62       */
63      @Test
64      @Alerts({"false", "true", "true", "true", "null", "", "blah", "2",
65               "true", "false", "true", "false", "", "null", "", "null"})
66      public void compact() throws Exception {
67          final String html = DOCTYPE_HTML
68                  + "<html>\n"
69                  + "  <head>\n"
70                  + "    <script>\n"
71                  + LOG_TITLE_FUNCTION
72                  + "      function test() {\n"
73                  + "        log(document.getElementById('menu1').compact);\n"
74                  + "        log(document.getElementById('menu2').compact);\n"
75                  + "        log(document.getElementById('menu3').compact);\n"
76                  + "        log(document.getElementById('menu4').compact);\n"
77                  + "        log(document.getElementById('menu1').getAttribute('compact'));\n"
78                  + "        log(document.getElementById('menu2').getAttribute('compact'));\n"
79                  + "        log(document.getElementById('menu3').getAttribute('compact'));\n"
80                  + "        log(document.getElementById('menu4').getAttribute('compact'));\n"
81  
82                  + "        document.getElementById('menu1').compact = true;\n"
83                  + "        document.getElementById('menu2').compact = false;\n"
84                  + "        document.getElementById('menu3').compact = 'xyz';\n"
85                  + "        document.getElementById('menu4').compact = null;\n"
86                  + "        log(document.getElementById('menu1').compact);\n"
87                  + "        log(document.getElementById('menu2').compact);\n"
88                  + "        log(document.getElementById('menu3').compact);\n"
89                  + "        log(document.getElementById('menu4').compact);\n"
90                  + "        log(document.getElementById('menu1').getAttribute('compact'));\n"
91                  + "        log(document.getElementById('menu2').getAttribute('compact'));\n"
92                  + "        log(document.getElementById('menu3').getAttribute('compact'));\n"
93                  + "        log(document.getElementById('menu4').getAttribute('compact'));\n"
94                  + "      }\n"
95                  + "    </script>\n"
96                  + "  </head>\n"
97                  + "  <body onload='test()'>\n"
98                  + "    <menu id='menu1'><li>a</li><li>b</li></menu>\n"
99                  + "    <menu compact='' id='menu2'><li>a</li><li>b</li></menu>\n"
100                 + "    <menu compact='blah' id='menu3'><li>a</li><li>b</li></menu>\n"
101                 + "    <menu compact='2' id='menu4'><li>a</li><li>b</li></menu>\n"
102                 + "  </body>\n"
103                 + "</html>";
104 
105         loadPageVerifyTitle2(html);
106     }
107 
108     /**
109      * @throws Exception if an error occurs
110      */
111     @Test
112     @Alerts({"undefined", "undefined", "undefined", "undefined", "undefined",
113              "null", "", "blah", "context", "ToolBar", "list",
114              "context", "toolbar", "ConText", "", "unknown"})
115     public void type() throws Exception {
116         final String html = DOCTYPE_HTML
117                 + "<html>\n"
118                 + "  <head>\n"
119                 + "    <script>\n"
120                 + LOG_TITLE_FUNCTION
121                 + "      function test() {\n"
122                 + "        log(document.getElementById('menu1').type);\n"
123                 + "        log(document.getElementById('menu2').type);\n"
124                 + "        log(document.getElementById('menu3').type);\n"
125                 + "        log(document.getElementById('menu4').type);\n"
126                 + "        log(document.getElementById('menu5').type);\n"
127                 + "        log(document.getElementById('menu1').getAttribute('type'));\n"
128                 + "        log(document.getElementById('menu2').getAttribute('type'));\n"
129                 + "        log(document.getElementById('menu3').getAttribute('type'));\n"
130                 + "        log(document.getElementById('menu4').getAttribute('type'));\n"
131                 + "        log(document.getElementById('menu5').getAttribute('type'));\n"
132 
133                 + "        try { document.getElementById('menu1').type = 'list' } catch(e) { logEx(e); }\n"
134                 + "        log(document.getElementById('menu1').type);\n"
135 
136                 + "        try { document.getElementById('menu1').type = 'context' } catch(e) { logEx(e); }\n"
137                 + "        log(document.getElementById('menu1').type);\n"
138 
139                 + "        try { document.getElementById('menu1').type = 'toolbar' } catch(e) { logEx(e); }\n"
140                 + "        log(document.getElementById('menu1').type);\n"
141 
142                 + "        try { document.getElementById('menu1').type = 'ConText' } catch(e) { logEx(e); }\n"
143                 + "        log(document.getElementById('menu1').type);\n"
144 
145                 + "        try { document.getElementById('menu1').type = '' } catch(e) { logEx(e); }\n"
146                 + "        log(document.getElementById('menu1').type);\n"
147 
148                 + "        try { document.getElementById('menu1').type = 'unknown' } catch(e) { logEx(e); }\n"
149                 + "        log(document.getElementById('menu1').type);\n"
150                 + "      }\n"
151                 + "    </script>\n"
152                 + "  </head>\n"
153                 + "  <body onload='test()'>\n"
154                 + "    <menu id='menu1'><li>a</li><li>b</li></menu>\n"
155                 + "    <menu type='' id='menu2'><li>a</li><li>b</li></menu>\n"
156                 + "    <menu type='blah' id='menu3'><li>a</li><li>b</li></menu>\n"
157                 + "    <menu type='context' id='menu4'><li>a</li><li>b</li></menu>\n"
158                 + "    <menu type='ToolBar' id='menu5'><li>a</li><li>b</li></menu>\n"
159                 + "  </body>\n"
160                 + "</html>";
161 
162         loadPageVerifyTitle2(html);
163     }
164 
165     /**
166      * @throws Exception if an error occurs
167      */
168     @Test
169     @Alerts({"undefined", "undefined", "new", ""})
170     public void label() throws Exception {
171         final String html = DOCTYPE_HTML
172                 + "<html>\n"
173                 + "  <head>\n"
174                 + "    <script>\n"
175                 + LOG_TITLE_FUNCTION
176                 + "      function test() {\n"
177                 + "        var menu1 = document.getElementById('menu1');\n"
178                 + "        log(menu1.label);\n"
179 
180                 + "        var menu2 = document.getElementById('menu1');\n"
181                 + "        log(menu2.label);\n"
182 
183                 + "        menu1.label = 'new';\n"
184                 + "        log(menu1.label);\n"
185 
186                 + "        menu1.label = '';\n"
187                 + "        log(menu1.label);\n"
188                 + "      }\n"
189                 + "    </script>\n"
190                 + "  </head>\n"
191                 + "  <body onload='test()'>\n"
192                 + "    <menu id='menu1' ><li>a</li><li>b</li></menu>\n"
193                 + "    <menu id='menu2' label='Menu1'><li>a</li><li>b</li></menu>\n"
194                 + "  </body>\n"
195                 + "</html>";
196 
197         loadPageVerifyTitle2(html);
198     }
199 }