Example #1
0
 def get_xy(x, y, view):
     if x and y:
         return x, y
     if view:
         from device_state import DeviceState
         return DeviceState.get_view_center(view_dict=view)
     return x, y
Example #2
0
    def generate_event_based_on_utg(self):
        """
        generate an event based on current device state
        note: ensure these fields are properly maintained in each transaction:
          last_event_flag, last_touched_view, last_state, exploited_views, state_transitions
        @return: AppEvent
        """
        self.save_state_transition(self.last_event_str, self.last_state,
                                   self.current_state)

        if self.device.is_foreground(self.app):
            # the app is in foreground, clear last_event_flag
            self.last_event_flag = EVENT_FLAG_STARTED
        else:
            number_of_starts = self.last_event_flag.count(EVENT_FLAG_START_APP)
            # If we have tried too many times but the app is still not started, stop DroidBot
            if number_of_starts > START_RETRY_THRESHOLD:
                raise InputInterruptedException("The app cannot be started.")

            # if app is not started, try start it
            if self.last_event_flag.endswith(EVENT_FLAG_START_APP):
                # It seems the app stuck at some state, and cannot be started
                # just pass to let viewclient deal with this case
                pass
            else:
                start_app_intent = self.app.get_start_intent()

                self.last_event_flag += EVENT_FLAG_START_APP
                self.last_event_str = EVENT_FLAG_START_APP
                return IntentEvent(start_app_intent)

        # select a view to click
        view_to_touch = self.select_a_view(self.current_state)

        # if no view can be selected, restart the app
        if view_to_touch is None:
            stop_app_intent = self.app.get_stop_intent()
            self.last_event_flag += EVENT_FLAG_STOP_APP
            self.last_event_str = EVENT_FLAG_STOP_APP
            return IntentEvent(stop_app_intent)

        view_to_touch_str = view_to_touch['view_str']
        if view_to_touch_str.startswith('BACK'):
            result = KeyEvent('BACK')
        else:
            x, y = DeviceState.get_view_center(view_to_touch)
            result = TouchEvent(x, y)

        self.last_event_flag += EVENT_FLAG_TOUCH
        self.last_event_str = view_to_touch_str
        self.save_explored_view(self.last_state, self.last_event_str)
        return result
 def get_transformed_event(self, device):
     event_dict = self.event_dict.copy()
     if 'target_view' in event_dict:
         target_view = event_dict.pop('target_view')
         target_view_selector = event_dict.pop('target_view_selector')
         state = device.get_current_state()
         matched_view = None
         for view_dict in state.views:
             if target_view_selector.match(view_dict):
                 matched_view = view_dict
                 break
         if matched_view is None:
             device.logger.warning("target_view no match: %s" % target_view)
         else:
             from device_state import DeviceState
             (event_dict['x'],
              event_dict['y']) = DeviceState.get_view_center(matched_view)
     return AppEvent.get_event(event_dict)