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