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.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 HTMLMenuElement}.
30   *
31   * @author Daniel Gredler
32   * @author Ronald Brill
33   * @author Frank Danek
34   */
35  @RunWith(BrowserRunner.class)
36  public class HTMLMenuElementTest extends WebDriverTestCase {
37  
38      /**
39       * @throws Exception if the test fails
40       */
41      @Test
42      @Alerts("[object HTMLMenuElement]")
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              + "  <menu 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 HtmlMenu);
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('menu1').compact);\n"
77                  + "        log(document.getElementById('menu2').compact);\n"
78                  + "        log(document.getElementById('menu3').compact);\n"
79                  + "        log(document.getElementById('menu4').compact);\n"
80                  + "        log(document.getElementById('menu1').getAttribute('compact'));\n"
81                  + "        log(document.getElementById('menu2').getAttribute('compact'));\n"
82                  + "        log(document.getElementById('menu3').getAttribute('compact'));\n"
83                  + "        log(document.getElementById('menu4').getAttribute('compact'));\n"
84  
85                  + "        document.getElementById('menu1').compact = true;\n"
86                  + "        document.getElementById('menu2').compact = false;\n"
87                  + "        document.getElementById('menu3').compact = 'xyz';\n"
88                  + "        document.getElementById('menu4').compact = null;\n"
89                  + "        log(document.getElementById('menu1').compact);\n"
90                  + "        log(document.getElementById('menu2').compact);\n"
91                  + "        log(document.getElementById('menu3').compact);\n"
92                  + "        log(document.getElementById('menu4').compact);\n"
93                  + "        log(document.getElementById('menu1').getAttribute('compact'));\n"
94                  + "        log(document.getElementById('menu2').getAttribute('compact'));\n"
95                  + "        log(document.getElementById('menu3').getAttribute('compact'));\n"
96                  + "        log(document.getElementById('menu4').getAttribute('compact'));\n"
97                  + "      }\n"
98                  + "    </script>\n"
99                  + "  </head>\n"
100                 + "  <body onload='test()'>\n"
101                 + "    <menu id='menu1'><li>a</li><li>b</li></menu>\n"
102                 + "    <menu compact='' id='menu2'><li>a</li><li>b</li></menu>\n"
103                 + "    <menu compact='blah' id='menu3'><li>a</li><li>b</li></menu>\n"
104                 + "    <menu compact='2' id='menu4'><li>a</li><li>b</li></menu>\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", "undefined",
116              "null", "", "blah", "context", "ToolBar", "list",
117              "context", "toolbar", "ConText", "", "unknown"})
118     public void type() throws Exception {
119         final String html = DOCTYPE_HTML
120                 + "<html>\n"
121                 + "  <head>\n"
122                 + "    <script>\n"
123                 + LOG_TITLE_FUNCTION
124                 + "      function test() {\n"
125                 + "        log(document.getElementById('menu1').type);\n"
126                 + "        log(document.getElementById('menu2').type);\n"
127                 + "        log(document.getElementById('menu3').type);\n"
128                 + "        log(document.getElementById('menu4').type);\n"
129                 + "        log(document.getElementById('menu5').type);\n"
130                 + "        log(document.getElementById('menu1').getAttribute('type'));\n"
131                 + "        log(document.getElementById('menu2').getAttribute('type'));\n"
132                 + "        log(document.getElementById('menu3').getAttribute('type'));\n"
133                 + "        log(document.getElementById('menu4').getAttribute('type'));\n"
134                 + "        log(document.getElementById('menu5').getAttribute('type'));\n"
135 
136                 + "        try { document.getElementById('menu1').type = 'list' } catch(e) { logEx(e); }\n"
137                 + "        log(document.getElementById('menu1').type);\n"
138 
139                 + "        try { document.getElementById('menu1').type = 'context' } catch(e) { logEx(e); }\n"
140                 + "        log(document.getElementById('menu1').type);\n"
141 
142                 + "        try { document.getElementById('menu1').type = 'toolbar' } catch(e) { logEx(e); }\n"
143                 + "        log(document.getElementById('menu1').type);\n"
144 
145                 + "        try { document.getElementById('menu1').type = 'ConText' } catch(e) { logEx(e); }\n"
146                 + "        log(document.getElementById('menu1').type);\n"
147 
148                 + "        try { document.getElementById('menu1').type = '' } catch(e) { logEx(e); }\n"
149                 + "        log(document.getElementById('menu1').type);\n"
150 
151                 + "        try { document.getElementById('menu1').type = 'unknown' } catch(e) { logEx(e); }\n"
152                 + "        log(document.getElementById('menu1').type);\n"
153                 + "      }\n"
154                 + "    </script>\n"
155                 + "  </head>\n"
156                 + "  <body onload='test()'>\n"
157                 + "    <menu id='menu1'><li>a</li><li>b</li></menu>\n"
158                 + "    <menu type='' id='menu2'><li>a</li><li>b</li></menu>\n"
159                 + "    <menu type='blah' id='menu3'><li>a</li><li>b</li></menu>\n"
160                 + "    <menu type='context' id='menu4'><li>a</li><li>b</li></menu>\n"
161                 + "    <menu type='ToolBar' id='menu5'><li>a</li><li>b</li></menu>\n"
162                 + "  </body>\n"
163                 + "</html>";
164 
165         loadPageVerifyTitle2(html);
166     }
167 
168     /**
169      * @throws Exception if an error occurs
170      */
171     @Test
172     @Alerts({"undefined", "undefined", "new", ""})
173     public void label() throws Exception {
174         final String html = DOCTYPE_HTML
175                 + "<html>\n"
176                 + "  <head>\n"
177                 + "    <script>\n"
178                 + LOG_TITLE_FUNCTION
179                 + "      function test() {\n"
180                 + "        var menu1 = document.getElementById('menu1');\n"
181                 + "        log(menu1.label);\n"
182 
183                 + "        var menu2 = document.getElementById('menu1');\n"
184                 + "        log(menu2.label);\n"
185 
186                 + "        menu1.label = 'new';\n"
187                 + "        log(menu1.label);\n"
188 
189                 + "        menu1.label = '';\n"
190                 + "        log(menu1.label);\n"
191                 + "      }\n"
192                 + "    </script>\n"
193                 + "  </head>\n"
194                 + "  <body onload='test()'>\n"
195                 + "    <menu id='menu1' ><li>a</li><li>b</li></menu>\n"
196                 + "    <menu id='menu2' label='Menu1'><li>a</li><li>b</li></menu>\n"
197                 + "  </body>\n"
198                 + "</html>";
199 
200         loadPageVerifyTitle2(html);
201     }
202 }