1 #!/usr/bin/env python 2 3 # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. 4 # Use of this source code is governed by a BSD-style license that can be 5 # found in the LICENSE file. 6 7 import common 8 import request_response 9 10 import logging 11 import mox 12 import unittest 13 14 import at_transceiver 15 import global_state 16 import modem_configuration 17 import task_loop 18 19 20 class RequestResponseTestCase(unittest.TestCase): 21 """ Test fixture for RequestResponse class. """ 22 23 def setUp(self): 24 self._mox = mox.Mox() 25 26 # Prepare modem configuration to suit our needs. 27 self._modem_conf = modem_configuration.ModemConfiguration() 28 self._modem_conf.base_wm_request_response_map = { 29 'AT1': 'AT1RESPONSE', 30 'AT2': ('AT2OK', 'AT2ERROR'), 31 'AT3': 'AT3RESPONSE' 32 } 33 self._modem_conf.plugin_wm_request_response_map = { 34 'AT3': 'AT3RESPONSE_OVERRIDEN', 35 'AT4': 'AT4RESPONSE', 36 'AT5': ['AT5RESPONSE1', 'AT5RESPONSE2'] 37 } 38 39 self._task_loop = self._mox.CreateMock(task_loop.TaskLoop) 40 self._transceiver = self._mox.CreateMock(at_transceiver.ATTransceiver) 41 self._machine = request_response.RequestResponse( 42 global_state.GlobalState(), 43 self._transceiver, 44 self._modem_conf) 45 46 # Mock out the task_loop 47 self._machine._task_loop = self._task_loop 48 49 def test_enabled_responses(self): 50 """ 51 Responses for different configurations when machine is enabled. 52 53 """ 54 self._machine.enable_machine() 55 self._task_loop.post_task_after_delay(mox.IgnoreArg(), 56 0, 57 'wm_response_text_only', 58 'AT1RESPONSE') 59 self._task_loop.post_task_after_delay(mox.IgnoreArg(), 60 0, 61 'wm_response_text_only', 62 'AT2OK') 63 self._task_loop.post_task_after_delay(mox.IgnoreArg(), 64 0, 65 'wm_response_text_only', 66 'AT3RESPONSE_OVERRIDEN') 67 self._task_loop.post_task_after_delay(mox.IgnoreArg(), 68 0, 69 'wm_response_text_only', 70 'AT4RESPONSE') 71 self._task_loop.post_task_after_delay(mox.IgnoreArg(), 72 0, 73 'wm_response_text_only', 74 'AT5RESPONSE1') 75 self._task_loop.post_task_after_delay(mox.IgnoreArg(), 76 0, 77 'wm_response_text_only', 78 'AT5RESPONSE2') 79 self._task_loop.post_task_after_delay(mox.IgnoreArg(), 80 0, 81 'wm_response_ok') 82 self._task_loop.post_task_after_delay(mox.IgnoreArg(), 83 0, 84 'wm_response_error') 85 86 self._mox.ReplayAll() 87 self._machine.act_on('AT1') 88 self._machine.act_on('AT2') 89 self._machine.act_on('AT3') 90 self._machine.act_on('AT4') 91 self._machine.act_on('AT5') 92 self._machine.act_on('AT6') 93 self._mox.VerifyAll() 94 95 96 def test_disabled_responses(self): 97 """ 98 Responses for different configurations when machine is enabled. 99 100 """ 101 self._machine.disable_machine() 102 103 self._task_loop.post_task_after_delay(mox.IgnoreArg(), 104 0, 105 'wm_response_error') 106 self._task_loop.post_task_after_delay(mox.IgnoreArg(), 107 0, 108 'wm_response_text_only', 109 'AT2ERROR') 110 self._task_loop.post_task_after_delay(mox.IgnoreArg(), 111 0, 112 'wm_response_error') 113 self._task_loop.post_task_after_delay(mox.IgnoreArg(), 114 0, 115 'wm_response_error') 116 self._task_loop.post_task_after_delay(mox.IgnoreArg(), 117 0, 118 'wm_response_error') 119 self._task_loop.post_task_after_delay(mox.IgnoreArg(), 120 0, 121 'wm_response_error') 122 123 self._mox.ReplayAll() 124 self._machine.act_on('AT1') 125 self._machine.act_on('AT2') 126 self._machine.act_on('AT3') 127 self._machine.act_on('AT4') 128 self._machine.act_on('AT5') 129 self._machine.act_on('AT6') 130 self._mox.VerifyAll() 131 132 133 if __name__ == '__main__': 134 logging.basicConfig(level=logging.DEBUG) 135 unittest.main() 136