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.network;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link NetworkInformation}.
23   *
24   * @author Ronald Brill
25   */
26  public class NetworkInformationTest extends WebDriverTestCase {
27  
28      /**
29       * @throws Exception if the test fails
30       */
31      @Test
32      @Alerts(DEFAULT = {"undefined", "undefined", "undefined"},
33              CHROME = {"[object NetworkInformation]", "undefined", "undefined"},
34              EDGE = {"[object NetworkInformation]", "undefined", "undefined"})
35      public void navigatorConnection() throws Exception {
36          final String html = DOCTYPE_HTML
37              + "<html><head>\n"
38              + "<script>\n"
39              + LOG_TITLE_FUNCTION
40              + "  function test() {\n"
41              + "    log(navigator.connection);\n"
42              + "    log(navigator.mozConnection);\n"
43              + "    log(navigator.webkitConnection);\n"
44              + "  }\n"
45              + "</script>\n"
46              + "</head><body onload='test()'>\n"
47              + "</body></html>";
48  
49          loadPageVerifyTitle2(html);
50      }
51  
52      /**
53       * @throws Exception if the test fails
54       */
55      @Test
56      @Alerts(DEFAULT = "no connection",
57              CHROME = "undefined",
58              EDGE = "undefined")
59      public void type() throws Exception {
60          final String html = DOCTYPE_HTML
61              + "<html><head>\n"
62              + "<script>\n"
63              + LOG_TITLE_FUNCTION
64              + "  function test() {\n"
65              + "    var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;\n"
66              + "    if (connection) {"
67              + "      log(connection.type);\n"
68              + "    } else {\n"
69              + "      log('no connection');\n"
70              + "    }\n"
71              + "  }\n"
72              + "</script>\n"
73              + "</head><body onload='test()'>\n"
74              + "</body></html>";
75  
76          loadPageVerifyTitle2(html);
77      }
78  
79      /**
80       * @throws Exception if the test fails
81       */
82      @Test
83      @Alerts(DEFAULT = "no connection",
84              CHROME = "undefined",
85              EDGE = "undefined")
86      public void downlinkMax() throws Exception {
87          final String html = DOCTYPE_HTML
88              + "<html><head>\n"
89              + "<script>\n"
90              + LOG_TITLE_FUNCTION
91              + "  function test() {\n"
92              + "    var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;\n"
93              + "    if (connection) {"
94              + "      log(connection.downlinkMax);\n"
95              + "    } else {\n"
96              + "      log('no connection');\n"
97              + "    }\n"
98              + "  }\n"
99              + "</script>\n"
100             + "</head><body onload='test()'>\n"
101             + "</body></html>";
102 
103         loadPageVerifyTitle2(html);
104     }
105 
106     /**
107      * @throws Exception if the test fails
108      */
109     @Test
110     @Alerts(DEFAULT = "no connection",
111             CHROME = "4g",
112             EDGE = "4g")
113     public void effectiveType() throws Exception {
114         final String html = DOCTYPE_HTML
115             + "<html><head>\n"
116             + "<script>\n"
117             + LOG_TITLE_FUNCTION
118             + "  function test() {\n"
119             + "    var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;\n"
120             + "    if (connection) {"
121             + "      log(connection.effectiveType);\n"
122             + "    } else {\n"
123             + "      log('no connection');\n"
124             + "    }\n"
125             + "  }\n"
126             + "</script>\n"
127             + "</head><body onload='test()'>\n"
128             + "</body></html>";
129 
130         loadPageVerifyTitle2(html);
131     }
132 
133     /**
134      * @throws Exception if the test fails
135      */
136     @Test
137     @Alerts(DEFAULT = "no connection",
138             CHROME = "10",
139             EDGE = "10")
140     public void downlink() throws Exception {
141         final String html = DOCTYPE_HTML
142             + "<html><head>\n"
143             + "<script>\n"
144             + LOG_TITLE_FUNCTION
145             + "  function test() {\n"
146             + "    var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;\n"
147             + "    if (connection) {"
148             + "      log(connection.downlink);\n"
149             + "    } else {\n"
150             + "      log('no connection');\n"
151             + "    }\n"
152             + "  }\n"
153             + "</script>\n"
154             + "</head><body onload='test()'>\n"
155             + "</body></html>";
156 
157         loadPageVerifyTitle2(html);
158     }
159 
160     /**
161      * @throws Exception if the test fails
162      */
163     @Test
164     @Alerts(DEFAULT = "no connection",
165             CHROME = "50",
166             EDGE = "50")
167     public void rtt() throws Exception {
168         final String html = DOCTYPE_HTML
169             + "<html><head>\n"
170             + "<script>\n"
171             + LOG_TITLE_FUNCTION
172             + "  function test() {\n"
173             + "    var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;\n"
174             + "    if (connection) {"
175             + "      log(connection.rtt);\n"
176             + "    } else {\n"
177             + "      log('no connection');\n"
178             + "    }\n"
179             + "  }\n"
180             + "</script>\n"
181             + "</head><body onload='test()'>\n"
182             + "</body></html>";
183 
184         loadPageVerifyTitle2(html);
185     }
186 
187 }