OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """PyAuto: Python Interface to Chromium's Automation Proxy. | 6 """PyAuto: Python Interface to Chromium's Automation Proxy. |
7 | 7 |
8 PyAuto uses swig to expose Automation Proxy interfaces to Python. | 8 PyAuto uses swig to expose Automation Proxy interfaces to Python. |
9 For complete documentation on the functionality available, | 9 For complete documentation on the functionality available, |
10 run pydoc on this file. | 10 run pydoc on this file. |
(...skipping 4215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4226 | 4226 |
4227 # Remembered networks do not have /service/ prepended to the service path | 4227 # Remembered networks do not have /service/ prepended to the service path |
4228 # even though wifi_networks does. We want this prepended to allow for | 4228 # even though wifi_networks does. We want this prepended to allow for |
4229 # consistency and easy string comparison with wifi_networks. | 4229 # consistency and easy string comparison with wifi_networks. |
4230 remembered_wifi = {} | 4230 remembered_wifi = {} |
4231 network_info['remembered_wifi'] = dict([('/service/' + k, v) for k, v in | 4231 network_info['remembered_wifi'] = dict([('/service/' + k, v) for k, v in |
4232 network_info['remembered_wifi'].iteritems()]) | 4232 network_info['remembered_wifi'].iteritems()]) |
4233 | 4233 |
4234 return network_info | 4234 return network_info |
4235 | 4235 |
| 4236 def GetServicePath(self, ssid): |
| 4237 """Returns the service path associated with an SSID. |
| 4238 |
| 4239 Args: |
| 4240 ssid: String defining the SSID we are searching for. |
| 4241 |
| 4242 Returns: |
| 4243 The service path or None if SSID does not exist. |
| 4244 """ |
| 4245 service_list = self.GetNetworkInfo() |
| 4246 service_list = service_list.get('wifi_networks', []) |
| 4247 for service_path, service_obj in service_list.iteritems(): |
| 4248 if service_obj['name'] == ssid: |
| 4249 return service_path |
| 4250 return None |
| 4251 |
4236 def NetworkScan(self): | 4252 def NetworkScan(self): |
4237 """Causes ChromeOS to scan for available wifi networks. | 4253 """Causes ChromeOS to scan for available wifi networks. |
4238 | 4254 |
4239 Blocks until scanning is complete. | 4255 Blocks until scanning is complete. |
4240 | 4256 |
4241 Returns: | 4257 Returns: |
4242 The new list of networks obtained from GetNetworkInfo(). | 4258 The new list of networks obtained from GetNetworkInfo(). |
4243 | 4259 |
4244 Raises: | 4260 Raises: |
4245 pyauto_errors.JSONInterfaceError if the automation call returns an error. | 4261 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
(...skipping 14 matching lines...) Expand all Loading... |
4260 'command': 'ToggleNetworkDevice', | 4276 'command': 'ToggleNetworkDevice', |
4261 'device': device, | 4277 'device': device, |
4262 'enable': enable, | 4278 'enable': enable, |
4263 } | 4279 } |
4264 return self._GetResultFromJSONRequest(cmd_dict, windex=None) | 4280 return self._GetResultFromJSONRequest(cmd_dict, windex=None) |
4265 | 4281 |
4266 PROXY_TYPE_DIRECT = 1 | 4282 PROXY_TYPE_DIRECT = 1 |
4267 PROXY_TYPE_MANUAL = 2 | 4283 PROXY_TYPE_MANUAL = 2 |
4268 PROXY_TYPE_PAC = 3 | 4284 PROXY_TYPE_PAC = 3 |
4269 | 4285 |
| 4286 def WaitUntilWifiNetworkAvailable(self, ssid, timeout=60, is_hidden=False): |
| 4287 """Waits until the given network is available. |
| 4288 |
| 4289 Routers that are just turned on may take up to 1 minute upon turning them |
| 4290 on to broadcast their SSID. |
| 4291 |
| 4292 Args: |
| 4293 ssid: SSID of the service we want to connect to. |
| 4294 timeout: timeout (in seconds) |
| 4295 |
| 4296 Raises: |
| 4297 Exception if timeout duration has been hit before wifi router is seen. |
| 4298 |
| 4299 Returns: |
| 4300 True, when the wifi network is seen within the timout period. |
| 4301 False, otherwise. |
| 4302 """ |
| 4303 def _GotWifiNetwork(): |
| 4304 # Returns non-empty array if desired SSID is available. |
| 4305 try: |
| 4306 return [wifi for wifi in |
| 4307 self.NetworkScan().get('wifi_networks', {}).values() |
| 4308 if wifi.get('name') == ssid] |
| 4309 except pyauto_errors.JSONInterfaceError: |
| 4310 # Temporary fix until crosbug.com/14174 is fixed. |
| 4311 # NetworkScan is only used in updating the list of networks so errors |
| 4312 # thrown by it are not critical to the results of wifi tests that use |
| 4313 # this method. |
| 4314 return False |
| 4315 |
| 4316 # The hidden AP's will always be on, thus we will assume it is ready to |
| 4317 # connect to. |
| 4318 if is_hidden: |
| 4319 return bool(_GotWifiNetwork()) |
| 4320 |
| 4321 return self.WaitUntil(_GotWifiNetwork, timeout=timeout, retry_sleep=1) |
| 4322 |
4270 def GetProxyTypeName(self, proxy_type): | 4323 def GetProxyTypeName(self, proxy_type): |
4271 values = { self.PROXY_TYPE_DIRECT: 'Direct Internet connection', | 4324 values = { self.PROXY_TYPE_DIRECT: 'Direct Internet connection', |
4272 self.PROXY_TYPE_MANUAL: 'Manual proxy configuration', | 4325 self.PROXY_TYPE_MANUAL: 'Manual proxy configuration', |
4273 self.PROXY_TYPE_PAC: 'Automatic proxy configuration' } | 4326 self.PROXY_TYPE_PAC: 'Automatic proxy configuration' } |
4274 return values[proxy_type] | 4327 return values[proxy_type] |
4275 | 4328 |
4276 def GetProxySettingsOnChromeOS(self, windex=0): | 4329 def GetProxySettingsOnChromeOS(self, windex=0): |
4277 """Get current proxy settings on Chrome OS. | 4330 """Get current proxy settings on Chrome OS. |
4278 | 4331 |
4279 Returns: | 4332 Returns: |
(...skipping 1151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5431 successful = result.wasSuccessful() | 5484 successful = result.wasSuccessful() |
5432 if not successful: | 5485 if not successful: |
5433 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) | 5486 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) |
5434 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ | 5487 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ |
5435 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) | 5488 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) |
5436 sys.exit(not successful) | 5489 sys.exit(not successful) |
5437 | 5490 |
5438 | 5491 |
5439 if __name__ == '__main__': | 5492 if __name__ == '__main__': |
5440 Main() | 5493 Main() |
OLD | NEW |