Ejemplo n.º 1
0
class TestADBPythonWithAuthentication(unittest.TestCase):
    """Test the `ADBPython` class."""

    PATCH_KEY = 'python'

    def setUp(self):
        """Create an `ADBPython` instance.

        """
        with patchers.patch_adb_device, patchers.patch_connect(True)[self.PATCH_KEY]:
            self.adb = ADBPython('IP:5555', 'adbkey')

    def test_connect_success_with_priv_key(self):
        """Test when the connect attempt is successful when using a private key.

        """
        with patchers.patch_connect(True)[self.PATCH_KEY], patch('androidtv.adb_manager.open', open_priv), patch('androidtv.adb_manager.PythonRSASigner', return_value=None):
            self.assertTrue(self.adb.connect())
            self.assertTrue(self.adb.available)
            self.assertTrue(self.adb._available)

    def test_connect_success_with_priv_pub_key(self):
        """Test when the connect attempt is successful when using private and public keys.

        """
        with patchers.patch_connect(True)[self.PATCH_KEY], patch('androidtv.adb_manager.open', open_priv_pub), patch('androidtv.adb_manager.PythonRSASigner', return_value=None):
            self.assertTrue(self.adb.connect())
            self.assertTrue(self.adb.available)
            self.assertTrue(self.adb._available)
Ejemplo n.º 2
0
class TestADBPython(unittest.TestCase):
    """Test the `ADBPython` class."""

    PATCH_KEY = 'python'

    def setUp(self):
        """Create an `ADBPython` instance.

        """
        with patchers.patch_adb_device, patchers.patch_connect(True)[self.PATCH_KEY]:
            self.adb = ADBPython('IP:5555')

    def test_connect_success(self):
        """Test when the connect attempt is successful.

        """
        with patchers.patch_connect(True)[self.PATCH_KEY]:
            self.assertTrue(self.adb.connect())
            self.assertTrue(self.adb.available)
            self.assertTrue(self.adb._available)

    def test_connect_fail(self):
        """Test when the connect attempt fails.

        """
        with patchers.patch_connect(False)[self.PATCH_KEY]:
            self.assertFalse(self.adb.connect())
            self.assertFalse(self.adb.available)
            self.assertFalse(self.adb._available)

    def test_adb_shell_fail(self):
        """Test when an ADB command is not sent because the device is unavailable.

        """
        self.assertFalse(self.adb.available)
        with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell(None)[self.PATCH_KEY]:
            self.assertIsNone(self.adb.shell("TEST"))

        with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell("TEST")[self.PATCH_KEY]:
            self.assertTrue(self.adb.connect())
            with patch.object(self.adb, '_adb_lock', LockedLock):
                self.assertIsNone(self.adb.shell("TEST"))

    def test_adb_shell_success(self):
        """Test when an ADB command is successfully sent.

        """
        with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell("TEST")[self.PATCH_KEY]:
            self.assertTrue(self.adb.connect())
            self.assertEqual(self.adb.shell("TEST"), "TEST")
Ejemplo n.º 3
0
class TestADBPythonClose(unittest.TestCase):
    """Test the `ADBPython.close` method."""

    PATCH_KEY = 'python'

    def test_close(self):
        """Test the `ADBPython.close` method.
        """
        with patchers.patch_adb_device, patchers.patch_connect(True)[self.PATCH_KEY]:
            self.adb = ADBPython('IP:5555')

        with patchers.patch_connect(True)[self.PATCH_KEY]:
            self.assertTrue(self.adb.connect())
            self.assertTrue(self.adb.available)
            self.assertTrue(self.adb._available)

            self.adb.close()
            self.assertFalse(self.adb.available)
class TestADBPythonClose(unittest.TestCase):
    """Test the `ADBPython.close` method."""

    PATCH_KEY = 'python'

    def test_close(self):
        """Test the `ADBPython.close` method.

        """
        with patchers.PATCH_ADB_DEVICE_TCP, patchers.patch_connect(True)[
                self.PATCH_KEY]:
            self.adb = ADBPython('HOST', 5555)

        with patchers.patch_connect(True)[self.PATCH_KEY]:
            self.assertTrue(self.adb.connect())
            self.assertTrue(self.adb.available)
            self.assertTrue(self.adb._available)

            self.adb.close()
            self.assertFalse(self.adb.available)
class TestADBPython(unittest.TestCase):
    """Test the `ADBPython` class."""

    PATCH_KEY = 'python'

    def setUp(self):
        """Create an `ADBPython` instance.

        """
        with patchers.PATCH_ADB_DEVICE_TCP, patchers.patch_connect(True)[
                self.PATCH_KEY]:
            self.adb = ADBPython('HOST', 5555)

    def test_locked_lock(self):
        """Test that the ``FakeLock`` class works as expected.

        """
        with patch.object(self.adb, '_adb_lock', FakeLock()):
            with _acquire(self.adb._adb_lock):
                with self.assertRaises(LockNotAcquiredException):
                    with _acquire(self.adb._adb_lock):
                        pass

            with _acquire(self.adb._adb_lock) as acquired:
                self.assertTrue(acquired)

    def test_connect_success(self):
        """Test when the connect attempt is successful.

        """
        with patchers.patch_connect(True)[self.PATCH_KEY]:
            self.assertTrue(self.adb.connect())
            self.assertTrue(self.adb.available)
            self.assertTrue(self.adb._available)

    def test_connect_fail(self):
        """Test when the connect attempt fails.

        """
        with patchers.patch_connect(False)[self.PATCH_KEY]:
            self.assertFalse(self.adb.connect())
            self.assertFalse(self.adb.available)
            self.assertFalse(self.adb._available)

        with patchers.patch_connect(True)[self.PATCH_KEY]:
            self.assertTrue(self.adb.connect())
            self.assertTrue(self.adb.available)
            self.assertTrue(self.adb._available)

        with patchers.PATCH_CONNECT_FAIL_CUSTOM_EXCEPTION[self.PATCH_KEY]:
            self.assertFalse(self.adb.connect())
            self.assertFalse(self.adb.available)
            self.assertFalse(self.adb._available)

    def test_connect_fail_lock(self):
        """Test when the connect attempt fails due to the lock.

        """
        with patchers.patch_connect(True)[self.PATCH_KEY]:
            with patch.object(self.adb, '_adb_lock', LockedLock()):
                self.assertFalse(self.adb.connect())
                self.assertFalse(self.adb.available)
                self.assertFalse(self.adb._available)

    def test_adb_shell_fail(self):
        """Test when an ADB shell command is not sent because the device is unavailable.

        """
        self.assertFalse(self.adb.available)
        with patchers.patch_connect(True)[
                self.PATCH_KEY], patchers.patch_shell(None)[self.PATCH_KEY]:
            self.assertIsNone(self.adb.shell("TEST"))

        with patchers.patch_connect(True)[
                self.PATCH_KEY], patchers.patch_shell("TEST")[self.PATCH_KEY]:
            self.assertTrue(self.adb.connect())
            with patch.object(self.adb, '_adb_lock', LockedLock()):
                with self.assertRaises(LockNotAcquiredException):
                    self.adb.shell("TEST")

                with self.assertRaises(LockNotAcquiredException):
                    self.adb.shell("TEST2")

    def test_adb_shell_success(self):
        """Test when an ADB shell command is successfully sent.

        """
        with patchers.patch_connect(True)[
                self.PATCH_KEY], patchers.patch_shell("TEST")[self.PATCH_KEY]:
            self.assertTrue(self.adb.connect())
            self.assertEqual(self.adb.shell("TEST"), "TEST")

    def test_adb_shell_fail_lock_released(self):
        """Test that the ADB lock gets released when an exception is raised.

        """
        with patchers.patch_connect(True)[
                self.PATCH_KEY], patchers.patch_shell("TEST")[self.PATCH_KEY]:
            self.assertTrue(self.adb.connect())

        with patchers.patch_shell("TEST",
                                  error=True)[self.PATCH_KEY], patch.object(
                                      self.adb, '_adb_lock', FakeLock()):
            with patch('{}.FakeLock.release'.format(__name__)) as release:
                with self.assertRaises(Exception):
                    self.adb.shell("TEST")
                assert release.called

    def test_adb_shell_lock_not_acquired_not_released(self):
        """Test that the lock does not get released if it is not acquired.

        """
        with patchers.patch_connect(True)[
                self.PATCH_KEY], patchers.patch_shell("TEST")[self.PATCH_KEY]:
            self.assertTrue(self.adb.connect())
            self.assertEqual(self.adb.shell("TEST"), "TEST")

        with patchers.patch_shell("TEST")[self.PATCH_KEY], patch.object(
                self.adb, '_adb_lock', LockedLock()):
            with patch('{}.LockedLock.release'.format(__name__)) as release:
                with self.assertRaises(LockNotAcquiredException):
                    self.adb.shell("TEST")

                release.assert_not_called()

    def test_adb_push_fail(self):
        """Test when an ADB push command is not executed because the device is unavailable.

        """
        self.assertFalse(self.adb.available)
        with patchers.patch_connect(True)[self.PATCH_KEY]:
            with patchers.PATCH_PUSH[self.PATCH_KEY] as patch_push:
                self.adb.push("TEST_LOCAL_PATCH", "TEST_DEVICE_PATH")
                patch_push.assert_not_called()

        with patchers.patch_connect(True)[self.PATCH_KEY]:
            with patchers.PATCH_PUSH[self.PATCH_KEY] as patch_push:
                self.assertTrue(self.adb.connect())
                with patch.object(self.adb, '_adb_lock', LockedLock()):
                    with self.assertRaises(LockNotAcquiredException):
                        self.adb.push("TEST_LOCAL_PATH", "TEST_DEVICE_PATH")

                    patch_push.assert_not_called()

    def test_adb_push_success(self):
        """Test when an ADB push command is successfully executed.

        """
        with patchers.patch_connect(True)[self.PATCH_KEY]:
            with patchers.PATCH_PUSH[self.PATCH_KEY] as patch_push:
                self.assertTrue(self.adb.connect())
                self.adb.push("TEST_LOCAL_PATH", "TEST_DEVICE_PATH")
                self.assertEqual(patch_push.call_count, 1)

    def test_adb_pull_fail(self):
        """Test when an ADB pull command is not executed because the device is unavailable.

        """
        self.assertFalse(self.adb.available)
        with patchers.patch_connect(True)[self.PATCH_KEY]:
            with patchers.PATCH_PULL[self.PATCH_KEY] as patch_pull:
                self.adb.pull("TEST_LOCAL_PATCH", "TEST_DEVICE_PATH")
                patch_pull.assert_not_called()

        with patchers.patch_connect(True)[self.PATCH_KEY]:
            with patchers.PATCH_PULL[self.PATCH_KEY] as patch_pull:
                self.assertTrue(self.adb.connect())
                with patch.object(self.adb, '_adb_lock', LockedLock()):
                    with self.assertRaises(LockNotAcquiredException):
                        self.adb.pull("TEST_LOCAL_PATH", "TEST_DEVICE_PATH")
                    patch_pull.assert_not_called()

    def test_adb_pull_success(self):
        """Test when an ADB pull command is successfully executed.

        """
        with patchers.patch_connect(True)[self.PATCH_KEY]:
            with patchers.PATCH_PULL[self.PATCH_KEY] as patch_pull:
                self.assertTrue(self.adb.connect())
                self.adb.pull("TEST_LOCAL_PATH", "TEST_DEVICE_PATH")
                self.assertEqual(patch_pull.call_count, 1)

    def test_adb_screencap_fail_unavailable(self):
        """Test when an ADB screencap command fails because the connection is unavailable.

        """
        self.assertFalse(self.adb.available)
        self.assertIsNone(self.adb.screencap())

    def test_adb_screencap_lock_not_acquired(self):
        """Test when an ADB screencap command fails because the ADB lock could not be acquired.

        """
        with patchers.patch_connect(True)[
                self.PATCH_KEY], patchers.patch_shell("TEST")[self.PATCH_KEY]:
            self.assertTrue(self.adb.connect())
            self.assertEqual(self.adb.shell("TEST"), "TEST")

        with patchers.patch_shell(PNG_IMAGE)[self.PATCH_KEY], patch.object(
                self.adb, '_adb_lock', LockedLock()):
            with patch('{}.LockedLock.release'.format(__name__)) as release:
                with self.assertRaises(LockNotAcquiredException):
                    self.adb.screencap()

                release.assert_not_called()

    def test_adb_screencap_success(self):
        """Test the `screencap` method.

        """
        with patchers.patch_connect(True)[
                self.PATCH_KEY], patchers.patch_shell(PNG_IMAGE)[
                    self.PATCH_KEY]:
            self.assertTrue(self.adb.connect())

            if isinstance(self.adb, ADBPython):
                self.assertEqual(self.adb.screencap(), PNG_IMAGE)

                with patchers.patch_shell(PNG_IMAGE_NEEDS_REPLACING)[
                        self.PATCH_KEY]:
                    self.assertEqual(self.adb.screencap(), PNG_IMAGE)

            else:
                with patch.object(self.adb._adb_device,
                                  'screencap',
                                  return_value=PNG_IMAGE):
                    self.assertEqual(self.adb.screencap(), PNG_IMAGE)