summaryrefslogtreecommitdiffstats
path: root/dom/system/gonk/tests/marionette/test_all_network_info.js
blob: 5225ab6d6c48c3a46e2a5b54dd546e13edc1a1a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

MARIONETTE_TIMEOUT = 60000;
MARIONETTE_HEAD_JS = "head.js";

var networkManager =
  Cc["@mozilla.org/network/manager;1"].getService(Ci.nsINetworkManager);
ok(networkManager,
   "networkManager.constructor is " + networkManager.constructor);

var wifiManager = window.navigator.mozWifiManager;
ok(wifiManager, "wifiManager.constructor is " + wifiManager.constructor);

function setEmulatorAPN() {
  let apn = [
    [{"carrier":"T-Mobile US",
      "apn":"epc.tmobile.com",
      "mmsc":"http://mms.msg.eng.t-mobile.com/mms/wapenc",
      "types":["default","supl","mms","ims","dun", "fota"]}]
  ];

  return setSettings(SETTINGS_KEY_DATA_APN_SETTINGS, apn);
}

function ensureWifiEnabled(aEnabled) {
  if (wifiManager.enabled === aEnabled) {
    log('Already ' + (aEnabled ? 'enabled' : 'disabled'));
    return Promise.resolve();
  }
  return requestWifiEnabled(aEnabled);
}

function requestWifiEnabled(aEnabled) {
  let promises = [];

  promises.push(waitForTargetEvent(wifiManager, aEnabled ? 'enabled' : 'disabled',
    function() {
      return wifiManager.enabled === aEnabled ? true : false;
  }));
  promises.push(setSettings(SETTINGS_KEY_WIFI_ENABLED, aEnabled));

  return Promise.all(promises);
}

// Test initial State
function verifyInitialState() {
  log("= verifyInitialState =");

  // Data and wifi should be off before starting any test.
  return getSettings(SETTINGS_KEY_DATA_ENABLED)
    .then(value => {
      is(value, false, "Data must be off");
    })
    .then(() => ensureWifiEnabled(false));
}

function testAllNetworkInfo(aAnyConnected) {
  log("= testAllNetworkInfo = " + aAnyConnected);

  let allNetworkInfo = networkManager.allNetworkInfo;
  ok(allNetworkInfo, "NetworkManager.allNetworkInfo");

  let count = Object.keys(allNetworkInfo).length;
  ok(count > 0, "NetworkManager.allNetworkInfo count");

  let connected = false;
  for (let networkId in allNetworkInfo) {
    if (allNetworkInfo.hasOwnProperty(networkId)) {
      let networkInfo = allNetworkInfo[networkId];
      if (networkInfo.state == Ci.nsINetworkInfo.NETWORK_STATE_CONNECTED) {
        connected = true;
        break;
      }
    }
  }

  is(aAnyConnected, connected, "NetworkManager.allNetworkInfo any connected");
}

// Start test
startTestBase(function() {

  let origApnSettings, origWifiEnabled;
  return Promise.resolve()
    .then(() => {
      origWifiEnabled = wifiManager.enabled;
    })
    .then(() => verifyInitialState())
    .then(() => getSettings(SETTINGS_KEY_DATA_APN_SETTINGS))
    .then(value => {
      origApnSettings = value;
    })
    .then(() => setEmulatorAPN())
    .then(() => setDataEnabledAndWait(true))
    .then(() => testAllNetworkInfo(true))
    .then(() => setDataEnabledAndWait(false))
    .then(() => testAllNetworkInfo(false))
    // Restore original apn settings and wifi state.
    .then(() => {
      if (origApnSettings) {
        return setSettings(SETTINGS_KEY_DATA_APN_SETTINGS, origApnSettings);
      }
    })
    .then(() => ensureWifiEnabled(origWifiEnabled));
});