示例#1
0
 def test_callback_id_property(self):
     mock_event_client = mock.Mock()
     handler = callback_handler.CallbackHandler(
         callback_id=MOCK_CALLBACK_ID,
         event_client=mock_event_client,
         ret_value=None,
         method_name=None)
     self.assertEqual(handler.callback_id, MOCK_CALLBACK_ID)
     with self.assertRaisesRegex(AttributeError, "can't set attribute"):
         handler.callback_id = 'ha'
示例#2
0
    def test_wait_for_event(self):
        mock_event_client = mock.Mock()
        mock_event_client.eventWaitAndGet = mock.Mock(
            return_value=MOCK_RAW_EVENT)
        handler = callback_handler.CallbackHandler(
            callback_id=MOCK_CALLBACK_ID,
            event_client=mock_event_client,
            ret_value=None,
            method_name=None)

        def some_condition(event):
            return event.data['successful']

        event = handler.waitForEvent('AsyncTaskResult', some_condition, 0.01)
示例#3
0
 def test_event_dict_to_snippet_event(self):
     mock_event_client = mock.Mock()
     mock_event_client.eventWaitAndGet = mock.Mock(
         return_value=MOCK_RAW_EVENT)
     handler = callback_handler.CallbackHandler(
         callback_id=MOCK_CALLBACK_ID,
         event_client=mock_event_client,
         ret_value=None,
         method_name=None)
     event = handler.waitAndGet('ha')
     self.assertEqual(event.name, MOCK_RAW_EVENT['name'])
     self.assertEqual(event.creation_time, MOCK_RAW_EVENT['time'])
     self.assertEqual(event.data, MOCK_RAW_EVENT['data'])
     self.assertEqual(event.callback_id, MOCK_RAW_EVENT['callbackId'])
示例#4
0
 def test_wait_and_get_timeout(self):
     mock_event_client = mock.Mock()
     java_timeout_msg = ('com.google.android.mobly.snippet.event.'
                         'EventSnippet$EventSnippetException: timeout.')
     mock_event_client.eventWaitAndGet = mock.Mock(
         side_effect=jsonrpc_client_base.ApiError(java_timeout_msg))
     handler = callback_handler.CallbackHandler(
         callback_id=MOCK_CALLBACK_ID,
         event_client=mock_event_client,
         ret_value=None,
         method_name=None)
     expected_msg = 'Timed out after waiting .*s for event "ha" .*'
     with self.assertRaisesRegex(callback_handler.TimeoutError,
                                 expected_msg):
         handler.waitAndGet('ha')
示例#5
0
    def test_wait_for_event_negative(self):
        mock_event_client = mock.Mock()
        mock_event_client.eventWaitAndGet = mock.Mock(
            return_value=MOCK_RAW_EVENT)
        handler = callback_handler.CallbackHandler(
            callback_id=MOCK_CALLBACK_ID,
            event_client=mock_event_client,
            ret_value=None,
            method_name=None)
        expected_msg = (
            'Timed out after 0.01s waiting for an "AsyncTaskResult" event that'
            ' satisfies the predicate "some_condition".')

        def some_condition(event):
            return False

        with self.assertRaisesRegex(callback_handler.TimeoutError,
                                    expected_msg):
            handler.waitForEvent('AsyncTaskResult', some_condition, 0.01)
示例#6
0
    def test_wait_for_event_max_timeout(self):
        """waitForEvent should not raise the timeout exceed threshold error.
        """
        mock_event_client = mock.Mock()
        mock_event_client.eventWaitAndGet = mock.Mock(
            return_value=MOCK_RAW_EVENT)
        handler = callback_handler.CallbackHandler(
            callback_id=MOCK_CALLBACK_ID,
            event_client=mock_event_client,
            ret_value=None,
            method_name=None,
            ad=mock.Mock())

        def some_condition(event):
            return event.data['successful']

        big_timeout = callback_handler.MAX_TIMEOUT * 2
        # This line should not raise.
        event = handler.waitForEvent('AsyncTaskResult',
                                     some_condition,
                                     timeout=big_timeout)
示例#7
0
    def _rpc(self, method, *args):
        """Sends an rpc to the app.

        Args:
            method: str, The name of the method to execute.
            args: any, The args of the method.

        Returns:
            The result of the rpc.

        Raises:
            ProtocolError: Something went wrong with the protocol.
            ApiError: The rpc went through, however executed with errors.
        """
        with self._lock:
            apiid = next(self._counter)
            data = {'id': apiid, 'method': method, 'params': args}
            request = json.dumps(data)
            self._client_send(request)
            response = self._client_receive()
        if not response:
            raise ProtocolError(self._ad,
                                ProtocolError.NO_RESPONSE_FROM_SERVER)
        result = json.loads(str(response, encoding='utf8'))
        if result['error']:
            raise ApiError(self._ad, result['error'])
        if result['id'] != apiid:
            raise ProtocolError(self._ad, ProtocolError.MISMATCHED_API_ID)
        if result.get('callback') is not None:
            if self._event_client is None:
                self._event_client = self._start_event_client()
            return callback_handler.CallbackHandler(
                callback_id=result['callback'],
                event_client=self._event_client,
                ret_value=result['result'],
                method_name=method,
                ad=self._ad)
        return result['result']