|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 // getNetworks() can take some time.. |
|
5 MARIONETTE_TIMEOUT = 60000; |
|
6 MARIONETTE_HEAD_JS = "head.js"; |
|
7 |
|
8 function isHomeNetwork(network) { |
|
9 is(network.longName, "Android"); |
|
10 is(network.shortName, "Android"); |
|
11 is(network.mcc, "310"); |
|
12 is(network.mnc, "260"); |
|
13 } |
|
14 |
|
15 function isRoamingNetwork(network) { |
|
16 is(network.longName, "TelKila"); |
|
17 is(network.shortName, "TelKila"); |
|
18 is(network.mcc, "310"); |
|
19 is(network.mnc, "295"); |
|
20 } |
|
21 |
|
22 function testConnectionInfo() { |
|
23 log("Validate initial states"); |
|
24 let voice = mobileConnection.voice; |
|
25 is(voice.connected, true); |
|
26 is(voice.state, "registered"); |
|
27 is(voice.emergencyCallsOnly, false); |
|
28 is(voice.roaming, false); |
|
29 isHomeNetwork(voice.network); |
|
30 |
|
31 let data = mobileConnection.data; |
|
32 // data.connected = true means there's an active data call which we |
|
33 // can't predict here. |
|
34 is(data.state, "registered"); |
|
35 is(data.emergencyCallsOnly, false); |
|
36 is(data.roaming, false); |
|
37 isHomeNetwork(data.network); |
|
38 } |
|
39 |
|
40 function testGetNetworks() { |
|
41 log("Enumerating available networks"); |
|
42 return getNetworks() |
|
43 .then(function resolve(aNetworks) { |
|
44 // The emulator RIL server should always return 2 networks: |
|
45 // {"longName":"Android","shortName":"Android","mcc":310,"mnc":260,"state":"available"} |
|
46 // {"longName":"TelKila","shortName":"TelKila","mcc":310,"mnc":295,"state":"available"} |
|
47 is(aNetworks.length, 2); |
|
48 |
|
49 let network1 = aNetworks[0]; |
|
50 isHomeNetwork(network1); |
|
51 is(network1.state, "available"); |
|
52 |
|
53 let network2 = aNetworks[1]; |
|
54 isRoamingNetwork(network2); |
|
55 is(network2.state, "available"); |
|
56 |
|
57 return aNetworks; |
|
58 }); |
|
59 } |
|
60 |
|
61 function testSelectNetwork(aNetwork, aValidator) { |
|
62 log("Selecting network '" + aNetwork.longName + "' manually"); |
|
63 isnot(aNetwork.longName, mobileConnection.voice.network.longName, |
|
64 "aNetwork.longName"); |
|
65 |
|
66 return selectNetworkAndWait(aNetwork) |
|
67 .then(function() { |
|
68 is(mobileConnection.networkSelectionMode, "manual", |
|
69 "mobileConnection.networkSelectionMode"); |
|
70 is(mobileConnection.voice.network.longName, aNetwork.longName, |
|
71 "mobileConnection.voice.network.longName"); |
|
72 |
|
73 aValidator(mobileConnection.voice.network); |
|
74 }); |
|
75 } |
|
76 |
|
77 function testSelectNetworkAutomatically(aHomeNetwork, aValidator) { |
|
78 log("Selecting network '" + aHomeNetwork.longName + "' automatically"); |
|
79 isnot(aHomeNetwork.longName, mobileConnection.voice.network.longName, |
|
80 "aHomeNetwork.longName"); |
|
81 |
|
82 return selectNetworkAutomaticallyAndWait() |
|
83 .then(function() { |
|
84 is(mobileConnection.networkSelectionMode, "automatic", |
|
85 "mobileConnection.networkSelectionMode"); |
|
86 is(mobileConnection.voice.network.longName, aHomeNetwork.longName, |
|
87 "mobileConnection.voice.network.longName"); |
|
88 |
|
89 aValidator(mobileConnection.voice.network); |
|
90 }); |
|
91 } |
|
92 |
|
93 function throwsException(fn) { |
|
94 try { |
|
95 fn(); |
|
96 ok(false, "function did not throw an exception: " + fn); |
|
97 } catch (e) { |
|
98 ok(true, "function successfully caught exception: " + e); |
|
99 } |
|
100 } |
|
101 |
|
102 function testSelectNetworkErrors(aNetworkToSelect, aAnotherNetwork) { |
|
103 throwsException(() => mobileConnection.selectNetwork(null)); |
|
104 throwsException(() => mobileConnection.selectNetwork({})); |
|
105 |
|
106 isnot(aNetworkToSelect.longName, mobileConnection.voice.network.longName, |
|
107 "aNetworkToSelect.longName"); |
|
108 let promise = selectNetworkAndWait(aNetworkToSelect); |
|
109 |
|
110 // attempt to selectNetwork while one request has already been sent |
|
111 throwsException(() => mobileConnection.selectNetwork(aAnotherNetwork)); |
|
112 |
|
113 return promise; |
|
114 } |
|
115 |
|
116 function testSelectExistingNetworkManual(aNetwork) { |
|
117 // When the current network is selected again, the DOMRequest's onsuccess |
|
118 // should be called, but the network shouldn't actually change |
|
119 |
|
120 log("Selecting '" + aNetwork.longName + "' manually (should already be selected)"); |
|
121 is(aNetwork.longName, mobileConnection.voice.network.longName, |
|
122 "aNetwork.longName"); |
|
123 |
|
124 function voiceChange() { |
|
125 let network = mobileConnection.voice.network; |
|
126 if (network.longName !== aNetwork.longName) { |
|
127 ok(false, "voicechange event emitted while selecting existing '" + |
|
128 aNetwork.longName + "' manually"); |
|
129 } |
|
130 } |
|
131 |
|
132 mobileConnection.addEventListener("voicechange", voiceChange); |
|
133 |
|
134 return selectNetwork(aNetwork) |
|
135 .then(function resolve() { |
|
136 let deferred = Promise.defer(); |
|
137 |
|
138 // Give the voicechange event another opportunity to fire |
|
139 setTimeout(function() { |
|
140 mobileConnection.removeEventListener("voicechange", voiceChange); |
|
141 deferred.resolve(); |
|
142 }, 3000); |
|
143 |
|
144 return deferred.promise; |
|
145 }, function reject() { |
|
146 mobileConnection.removeEventListener("voicechange", voiceChange); |
|
147 ok(false, "selectNetwork fails"); |
|
148 }); |
|
149 } |
|
150 |
|
151 function testSelectExistingNetworkAuto(aHomeNetwork) { |
|
152 // Now try the same thing but using automatic selection |
|
153 log("Selecting '" + aHomeNetwork.longName + "' automatically (should already be selected)"); |
|
154 is(aHomeNetwork.longName, mobileConnection.voice.network.longName, |
|
155 "aHomeNetwork.longName"); |
|
156 |
|
157 function voiceChange() { |
|
158 let network = mobileConnection.voice.network; |
|
159 if (network.longName !== aHomeNetwork.longName) { |
|
160 ok(false, "voicechange event emitted while selecting existing '" + |
|
161 aHomeNetwork.longName + "' automatically"); |
|
162 } |
|
163 } |
|
164 |
|
165 mobileConnection.addEventListener("voicechange", voiceChange); |
|
166 |
|
167 return selectNetworkAutomatically() |
|
168 .then(function resolve() { |
|
169 let deferred = Promise.defer(); |
|
170 |
|
171 // Give the voicechange event another opportunity to fire |
|
172 setTimeout(function() { |
|
173 mobileConnection.removeEventListener("voicechange", voiceChange); |
|
174 deferred.resolve(); |
|
175 }, 3000); |
|
176 |
|
177 return deferred.promise; |
|
178 }, function reject() { |
|
179 mobileConnection.removeEventListener("voicechange", voiceChange); |
|
180 ok(false, "selectNetwork fails"); |
|
181 }); |
|
182 } |
|
183 |
|
184 startTestCommon(function() { |
|
185 let promise = Promise.resolve(); |
|
186 if (mobileConnection.networkSelectionMode != "automatic") { |
|
187 promise = promise.then(selectNetworkAutomatically); |
|
188 } |
|
189 |
|
190 return promise |
|
191 .then(() => testConnectionInfo()) |
|
192 .then(() => testGetNetworks()) |
|
193 .then(function(aNetworks) { |
|
194 let homeNetwork = aNetworks[0], |
|
195 roamingNetwork = aNetworks[1]; |
|
196 |
|
197 // We're initially connected to home network, so let's connect to roaming |
|
198 // network first. |
|
199 return testSelectNetwork(roamingNetwork, isRoamingNetwork) |
|
200 |
|
201 // Then connect back to home network automatically. |
|
202 .then(() => testSelectNetworkAutomatically(homeNetwork, isHomeNetwork)) |
|
203 |
|
204 // Then try connect to roaming network again. |
|
205 .then(() => testSelectNetworkErrors(roamingNetwork, homeNetwork)) |
|
206 |
|
207 // Roaming network should has been selected, try select it again. |
|
208 .then(() => testSelectExistingNetworkManual(roamingNetwork)) |
|
209 |
|
210 // Switch back to home network and try selecte it again. |
|
211 .then(() => selectNetworkAutomaticallyAndWait()) |
|
212 .then(() => testSelectExistingNetworkAuto(homeNetwork)); |
|
213 }); |
|
214 }); |