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