UNPKG

4.05 kBJavaScriptView Raw
1/*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20*/
21
22/* jshint jasmine: true */
23/* global Connection */
24
25exports.defineAutoTests = function () {
26 describe('Network (navigator.connection)', function () {
27 it("network.spec.1 should exist", function () {
28 expect(navigator.network && navigator.network.connection).toBeDefined();
29 expect(navigator.connection).toBeDefined();
30 });
31
32 it("network.spec.2 should be set to a valid value", function () {
33 var validValues = {
34 'unknown': 1,
35 'ethernet': 1,
36 'wifi': 1,
37 '2g': 1,
38 'cellular': 1,
39 '3g': 1,
40 '4g': 1,
41 'none': 1
42 };
43 expect(validValues[navigator.connection.type]).toBe(1);
44 });
45
46 it("network.spec.3 should have the same value in deprecated and non-deprecated apis", function () {
47 expect(navigator.network.connection.type).toBe(navigator.connection.type);
48 });
49
50 it("network.spec.4 should define constants for connection status", function () {
51 expect(Connection.UNKNOWN).toBe("unknown");
52 expect(Connection.ETHERNET).toBe("ethernet");
53 expect(Connection.WIFI).toBe("wifi");
54 expect(Connection.CELL_2G).toBe("2g");
55 expect(Connection.CELL_3G).toBe("3g");
56 expect(Connection.CELL_4G).toBe("4g");
57 expect(Connection.NONE).toBe("none");
58 expect(Connection.CELL).toBe("cellular");
59 });
60 });
61};
62
63/******************************************************************************/
64/******************************************************************************/
65/******************************************************************************/
66
67exports.defineManualTests = function (contentEl, createActionButton) {
68 function eventOutput(s) {
69 var el = document.getElementById("results");
70 el.innerHTML = el.innerHTML + s + "<br>";
71 }
72
73 function printNetwork() {
74 eventOutput("navigator.connection.type=" + navigator.connection.type);
75 eventOutput("navigator.network.connection.type=" + navigator.network.connection.type);
76 }
77
78 function onEvent(e) {
79 eventOutput('Event of type: ' + e.type);
80 printNetwork();
81 }
82
83 /******************************************************************************/
84
85 var html = '<div id="info">' +
86 '<b>Results:</b><br>' +
87 '<span id="results"></span>' +
88 '</div><div id="connection"></div>' +
89 'Expected result: Status box will update with type of connection using two different methods. Both values must match.' +
90 ' The result will be unknown, ethernet, wifi, 2g, 3g, 4g, none, or cellular. Make sure it matches what the device is connected to.' +
91 '</p> <div id="actions"></div>';
92
93 document.addEventListener("online", onEvent, false);
94 document.addEventListener("offline", onEvent, false);
95 contentEl.innerHTML = html;
96
97 createActionButton('Show Network Connection', function () {
98 printNetwork();
99 }, 'connection');
100
101 createActionButton('Clear Log', function () {
102 document.getElementById('results').innerHTML = '';
103 }, 'actions');
104};