Home | History | Annotate | Download | only in network_ChromeCellularSmokeTest
      1 # Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 import logging
      6 
      7 from autotest_lib.client.bin import test
      8 from autotest_lib.client.common_lib import error
      9 from autotest_lib.client.cros.cellular import mm1_constants
     10 from autotest_lib.client.cros.cellular import test_environment
     11 from autotest_lib.client.cros.cellular.pseudomodem import pm_constants
     12 from autotest_lib.client.cros.networking import pm_proxy
     13 from autotest_lib.client.cros.networking.chrome_testing \
     14         import chrome_networking_test_context as cntc
     15 from autotest_lib.client.cros.networking.chrome_testing import test_utils
     16 
     17 class network_ChromeCellularSmokeTest(test.test):
     18     """
     19     Tests that Chrome can bring the network to a connected state and effectively
     20     access the internet through the cellular network. The test repeats a
     21     connect/disconnect sequence several times and makes sure that Chrome can
     22     always connect to the network via chrome.networkingPrivate.
     23 
     24     """
     25     version = 1
     26 
     27     CONNECT_COUNT = 5
     28 
     29     def _setup_modem_proxy(self):
     30         pseudomm = pm_proxy.PseudoMMProxy.get_proxy()
     31         self._modem = pseudomm.get_modem()
     32 
     33 
     34     def _get_modem_state(self):
     35         props = self._modem.properties(mm1_constants.I_MODEM)
     36         return props[mm1_constants.MM_MODEM_PROPERTY_NAME_STATE]
     37 
     38 
     39     def _get_cellular_network(self):
     40         networks = self._chrome_testing.find_cellular_networks()
     41         if len(networks) != 1:
     42             raise error.TestFail(
     43                     'Expected 1 cellular network, found ' + str(len(networks)))
     44         network = networks[0]
     45         test_utils.simple_network_sanity_check(
     46                 network, pm_constants.DEFAULT_TEST_NETWORK_PREFIX,
     47                 self._chrome_testing.CHROME_NETWORK_TYPE_CELLULAR)
     48         return network
     49 
     50 
     51     def _assert_modem_state(self, expected_state):
     52         modem_state = self._get_modem_state()
     53         if modem_state != expected_state:
     54             raise error.TestFail(
     55                     'Expected modem state to be "' +
     56                     mm1_constants.ModemStateToString(expected_state) +
     57                     '", found: ' +
     58                     mm1_constants.ModemStateToString(modem_state))
     59 
     60 
     61     def _ensure_network_status(self, network_id, status, timeout):
     62         test_utils.check_ui_property(
     63                 self._chrome_testing, network_id, 'ConnectionState', status)
     64 
     65 
     66     def _disconnect_cellular_network(self):
     67         # Make sure that the network becomes disconnected.
     68         network_id = self._network['GUID']
     69         logging.info('Disconnecting from network: ' + network_id)
     70         call_status = self._chrome_testing.call_test_function(
     71                 test_utils.LONG_TIMEOUT,
     72                 'disconnectFromNetwork',
     73                 '"' + network_id + '"')
     74         logging.info('Checking that the network is disconnected.')
     75         self._ensure_network_status(
     76                 network_id, 'NotConnected', test_utils.LONG_TIMEOUT)
     77         logging.info('The network is disconnected. Checking that the modem is '
     78                      'in the REGISTERED state.')
     79         self._assert_modem_state(mm1_constants.MM_MODEM_STATE_REGISTERED)
     80         logging.info('Modem is disconnected. Disconnect was successful.')
     81 
     82 
     83     def _connect_cellular_network(self):
     84         # Make sure that the network becomes connected.
     85         network_id = self._network['GUID']
     86         logging.info('Connecting to network: ' + network_id)
     87         call_status = self._chrome_testing.call_test_function(
     88                 test_utils.LONG_TIMEOUT,
     89                 'connectToNetwork',
     90                 '"' + network_id + '"')
     91         logging.info('Checking that the network is connected.')
     92         self._ensure_network_status(
     93                 network_id, 'Connected', test_utils.LONG_TIMEOUT)
     94         logging.info('The network is connected. Checking that the modem is in '
     95                      'the CONNECTED state.')
     96         self._assert_modem_state(mm1_constants.MM_MODEM_STATE_CONNECTED)
     97         logging.info('Modem is connected. Connect was successful.')
     98 
     99 
    100     def _run_once_internal(self):
    101         # Set up a ModemManager proxy to use to verify the modem state.
    102         self._setup_modem_proxy()
    103 
    104         # Make sure that there is a single cellular network and it matches
    105         # the data from pseudomm.
    106         self._network = self._get_cellular_network()
    107 
    108         # Disconnect from the network before doing any operations.
    109         self._disconnect_cellular_network()
    110 
    111         logging.info('Starting connect/disconnect sequence.')
    112         for _ in xrange(self.CONNECT_COUNT):
    113             self._connect_cellular_network()
    114             self._disconnect_cellular_network()
    115 
    116 
    117     def run_once(self, family):
    118         test_env = test_environment.CellularPseudoMMTestEnvironment(
    119                 pseudomm_args=({'family': family},))
    120         testing_context = cntc.ChromeNetworkingTestContext()
    121         with test_env, testing_context:
    122             self._chrome_testing = testing_context
    123             self._run_once_internal()
    124