Ejemplo n.º 1
0
 def test_is_port_available_negative(self, mock_list_occupied_adb_ports):
     test_s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     test_s.bind(('localhost', 0))
     port = test_s.getsockname()[1]
     try:
         self.assertFalse(adb.is_port_available(port))
     finally:
         test_s.close()
    def get_droid(self, handle_event=True):
        """Create an sl4n connection to the device.

        Return the connection handler 'droid'. By default, another connection
        on the same session is made for EventDispatcher, and the dispatcher is
        returned to the caller as well.
        If sl4n server is not started on the device, try to start it.

        Args:
            handle_event: True if this droid session will need to handle
                events.

        Returns:
            droid: Android object useds to communicate with sl4n on the android
                device.
            ed: An optional EventDispatcher to organize events for this droid.

        Examples:
            Don't need event handling:
            >>> ad = NativeAndroidDevice()
            >>> droid = ad.get_droid(False)

            Need event handling:
            >>> ad = NativeAndroidDevice()
            >>> droid, ed = ad.get_droid()
        """
        if not self.h_port or not is_port_available(self.h_port):
            self.h_port = get_available_host_port()
        self.adb.tcp_forward(self.h_port, self.d_port)
        pid = self.adb.shell("ps | grep sl4n | awk '{print $2}'").decode(
            'ascii')
        while (pid):
            self.adb.shell("kill {}".format(pid))
            pid = self.adb.shell("ps | grep sl4n | awk '{print $2}'").decode(
                'ascii')
        call(["adb -s " + self.serial + " shell sh -c \"/system/bin/sl4n\" &"],
             shell=True)
        try:
            time.sleep(3)
            droid = self.start_new_session()
        except:
            droid = self.start_new_session()
        return droid
    def get_droid(self, handle_event=True):
        """Create an sl4a connection to the device.

        Return the connection handler 'droid'. By default, another connection
        on the same session is made for EventDispatcher, and the dispatcher is
        returned to the caller as well.
        If sl4a server is not started on the device, try to start it.

        Args:
            handle_event: True if this droid session will need to handle
                events.

        Returns:
            droid: Android object used to communicate with sl4a on the android
                device.
            ed: An optional EventDispatcher to organize events for this droid.

        Examples:
            Don't need event handling:
            >>> ad = AndroidDevice()
            >>> droid = ad.get_droid(False)

            Need event handling:
            >>> ad = AndroidDevice()
            >>> droid, ed = ad.get_droid()
        """
        if not self.h_port or not adb.is_port_available(self.h_port):
            self.h_port = adb.get_available_host_port()
        self.adb.tcp_forward(self.h_port, self.d_port)

        try:
            droid = self.start_new_session()
        except:
            sl4a_client.start_sl4a(self.adb)
            droid = self.start_new_session()

        if handle_event:
            ed = self.get_dispatcher(droid)
            return droid, ed
        return droid
Ejemplo n.º 4
0
 def test_is_port_available_positive(self):
     test_s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     test_s.bind(('localhost', 0))
     port = test_s.getsockname()[1]
     test_s.close()
     self.assertTrue(adb.is_port_available(port))