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;
16  
17  import org.htmlunit.SimpleWebTestCase;
18  import org.junit.jupiter.api.Test;
19  
20  /**
21   * Tests for {@link Navigator}.
22   *
23   * @author Ahmed Ashour
24   * @author Ronald Brill
25   */
26  public class Navigator2Test extends SimpleWebTestCase {
27  
28      /**
29       * Tests the "cookieEnabled" property.
30       * @throws Exception on test failure
31       */
32      @Test
33      public void cookieEnabled() throws Exception {
34          cookieEnabled(true);
35          cookieEnabled(false);
36      }
37  
38      private void cookieEnabled(final boolean cookieEnabled) throws Exception {
39          final String html = DOCTYPE_HTML
40              + "<html><head>\n"
41              + "<script>\n"
42              + "function test() {\n"
43              + "  alert(navigator.cookieEnabled);\n"
44              + "}\n"
45              + "</script>\n"
46              + "</head>\n"
47              + "<body onload='test()'></body>\n"
48              + "</html>";
49  
50          setExpectedAlerts(Boolean.toString(cookieEnabled));
51          if (!cookieEnabled) {
52              getWebClient().getCookieManager().setCookiesEnabled(cookieEnabled);
53          }
54  
55          loadPageWithAlerts(html);
56      }
57  
58      /**
59       * Tests the "javaEnabled" method.
60       * @throws Exception on test failure
61       */
62      @Test
63      public void javaEnabled() throws Exception {
64          attribute("javaEnabled()", "false");
65      }
66  
67      /**
68       * Generic method for testing the value of a specific navigator attribute.
69       * @param name the name of the attribute to test
70       * @param value the expected value for the named attribute
71       * @throws Exception on test failure
72       */
73      void attribute(final String name, final String value) throws Exception {
74          final String html = DOCTYPE_HTML
75                  + "<html>\n"
76                  + "<head>\n"
77                  + "  <title>test</title>\n"
78                  + "  <script>\n"
79                  + "  function doTest() {\n"
80                  + "    alert('" + name + " = ' + window.navigator." + name + ");\n"
81                  + "  }\n"
82                  + "  </script>\n"
83                  + "</head>\n"
84                  + "<body onload=\'doTest()\'>\n"
85                  + "</body>\n"
86                  + "</html>";
87  
88          setExpectedAlerts(name + " = " + value);
89          loadPageWithAlerts(html);
90      }
91  }