def _make_client(self, adb_proxy=None): adb_proxy = adb_proxy or mock_android_device.MockAdbProxy( installed_packages=['com.googlecode.android_scripting']) ad = mock.Mock() ad.adb = adb_proxy ad.build_info = { 'build_version_codename': ad.adb.getprop('ro.build.version.codename'), 'build_version_sdk': ad.adb.getprop('ro.build.version.sdk'), } return sl4a_client.Sl4aClient(ad=ad)
def load_sl4a(self): """Start sl4a service on the Android device. Launch sl4a server if not already running, spin up a session on the server, and two connections to this session. Creates an sl4a client (self.sl4a) with one connection, and one EventDispatcher obj (self.ed) with the other connection. """ host_port = utils.get_available_host_port() self.sl4a = sl4a_client.Sl4aClient(host_port=host_port, adb_proxy=self.adb) self._start_jsonrpc_client(self.sl4a) # Start an EventDispatcher for the current sl4a session event_client = sl4a_client.Sl4aClient(host_port=host_port, adb_proxy=self.adb) event_client.connect(uid=self.sl4a.uid, cmd=jsonrpc_client_base.JsonRpcCommand.CONTINUE) self.ed = event_dispatcher.EventDispatcher(event_client) self.ed.start()
def load_sl4a(self): """Start sl4a service on the Android device. Launch sl4a server if not already running, spin up a session on the server, and two connections to this session. Creates an sl4a client (self.sl4a) with one connection, and one EventDispatcher obj (self.ed) with the other connection. """ self.sl4a = sl4a_client.Sl4aClient(ad=self) self.sl4a.start_app_and_connect() # Unpack the 'ed' attribute for compatibility. self.ed = self.sl4a.ed
def test_handshake_error(self, mock_create_connection): """Test error in sl4a handshake Test that if there is an error in the sl4a handshake then a protocol error will be raised. """ fake_conn = mock.MagicMock() fake_conn.makefile.return_value = MockSocketFile(None) mock_create_connection.return_value = fake_conn with self.assertRaises(sl4a_client.Sl4aProtocolError): client = sl4a_client.Sl4aClient() client.open()
def test_open_handshake(self, mock_create_connection): """Test sl4a client handshake Test that at the end of a handshake with no errors the client object has the correct parameters. """ fake_conn = mock.MagicMock() fake_conn.makefile.return_value = MockSocketFile(MOCK_RESP) mock_create_connection.return_value = fake_conn client = sl4a_client.Sl4aClient() client.open() self.assertEqual(client.uid, 1)
def test_rpc_call_increment_counter(self, mock_create_connection): """Test rpc counter Test that with each rpc call the counter is incremented by 1. """ fake_file = self.setup_mock_socket_file(mock_create_connection) client = sl4a_client.Sl4aClient() client.open() for i in range(0, 10): fake_file.resp = (MOCK_RESP_TEMPLATE % i).encode('utf-8') client.some_rpc() self.assertEquals(next(client._counter), 10)
def test_rpc_error_response(self, mock_create_connection): """Test rpc that is given an error response Test that when an rpc recieves a reponse with an error will raised an api error. """ fake_file = self.setup_mock_socket_file(mock_create_connection) client = sl4a_client.Sl4aClient() client.open() fake_file.resp = MOCK_RESP_WITH_ERROR with self.assertRaises(sl4a_client.Sl4aApiError, msg=1): client.some_rpc(1, 2, 3)
def test_open_handshake_unknown_status(self, mock_create_connection): """Test handshake with unknown status response Test that when the handshake is given an unknown status then the client will not be given a uid. """ fake_conn = mock.MagicMock() fake_conn.makefile.return_value = MockSocketFile( MOCK_RESP_UNKWN_STATUS) mock_create_connection.return_value = fake_conn client = sl4a_client.Sl4aClient() client.open() self.assertEqual(client.uid, sl4a_client.UNKNOWN_UID)
def test_rpc_no_response(self, mock_create_connection): """Test rpc that does not get a reponse Test that when an rpc does not get a response it throws a protocol error. """ fake_file = self.setup_mock_socket_file(mock_create_connection) client = sl4a_client.Sl4aClient() client.open() fake_file.resp = None with self.assertRaises( sl4a_client.Sl4aProtocolError, msg=sl4a_client.Sl4aProtocolError.NO_RESPONSE_FROM_SERVER): client.some_rpc(1, 2, 3)
def test_rpc_id_mismatch(self, mock_create_connection): """Test rpc that returns a different id than expected Test that if an rpc returns with an id that is different than what is expected will give a protocl error. """ fake_file = self.setup_mock_socket_file(mock_create_connection) client = sl4a_client.Sl4aClient() client.open() fake_file.resp = (MOCK_RESP_TEMPLATE % 52).encode('utf8') with self.assertRaises( sl4a_client.Sl4aProtocolError, msg=sl4a_client.Sl4aProtocolError.MISMATCHED_API_ID): client.some_rpc(1, 2, 3)
def test_open_no_response(self, mock_create_connection): """Test handshake no response Test that if a handshake recieves no response then it will give a protocol error. """ fake_file = self.setup_mock_socket_file(mock_create_connection) client = sl4a_client.Sl4aClient() client.open() fake_file.resp = None with self.assertRaises( sl4a_client.Sl4aProtocolError, msg=sl4a_client.Sl4aProtocolError.NO_RESPONSE_FROM_HANDSHAKE): client.some_rpc(1, 2, 3)
def test_rpc_send_to_socket(self, mock_create_connection): """Test rpc sending and recieving Tests that when an rpc is sent and received the corrent data is used. """ fake_file = self.setup_mock_socket_file(mock_create_connection) client = sl4a_client.Sl4aClient() client.open() result = client.some_rpc(1, 2, 3) self.assertEquals(result, 123) expected = {'id': 0, 'method': 'some_rpc', 'params': [1, 2, 3]} actual = json.loads(fake_file.last_write.decode('utf-8')) self.assertEqual(expected, actual)
def add_new_connection_to_session(self, session_id): """Create a new connection to an existing sl4a session. Args: session_id: UID of the sl4a session to add connection to. Returns: An Android object used to communicate with sl4a on the android device. Raises: DoesNotExistError: Raised if the session it's trying to connect to does not exist. """ if session_id not in self._droid_sessions: raise DoesNotExistError("Session %d doesn't exist." % session_id) droid = sl4a_client.Sl4aClient(port=self.h_port, uid=session_id) droid.open(cmd=sl4a_client.Sl4aCommand.CONTINUE) return droid
def start_new_session(self): """Start a new session in sl4a. Also caches the droid in a dict with its uid being the key. Returns: An Android object used to communicate with sl4a on the android device. Raises: Sl4aException: Something is wrong with sl4a and it returned an existing uid to a new session. """ droid = sl4a_client.Sl4aClient(port=self.h_port) droid.open() if droid.uid in self._droid_sessions: raise sl4a_client.Sl4aException( "SL4A returned an existing uid for a new session. Abort.") self._droid_sessions[droid.uid] = [droid] return droid
def _make_client(self, adb_proxy=None): adb_proxy = adb_proxy or MockAdbProxy() ad = mock.Mock() ad.adb = adb_proxy return sl4a_client.Sl4aClient(ad=ad)
def start(self): self._sl4a_client = sl4a_client.Sl4aClient(ad=self._ad) self._sl4a_client.start_app_and_connect()