import unittest
from unittest.mock import MagicMock, patch
import sys
import os
# Add the project root to sys.path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from main import (
connect_device,
device_info,
click_element,
click_point,
input_text,
press_key,
open_app,
stop_app,
get_device,
swipe,
shell_command,
install_app,
uninstall_app,
wait_for_element
)
class TestAndroidMCP(unittest.TestCase):
@patch('uiautomator2.connect')
def test_connect_device(self, mock_connect):
# Setup mock
mock_device = MagicMock()
mock_device.info = {"serial": "123456", "model": "Pixel 4"}
mock_connect.return_value = mock_device
# Test connection
result = connect_device("123456")
# Verify
mock_connect.assert_called_with("123456")
self.assertIn("Connected to device", result)
self.assertIn("123456", result)
@patch('main.get_device')
def test_click_element(self, mock_get_device):
mock_device = MagicMock()
mock_get_device.return_value = mock_device
# Test click by text
result = click_element(text="Settings")
mock_device.assert_called() # The d(text="Settings") call
# The mock usage for d(text=...) involves calling the device object
# d(text="Settings").click() -> mock_device(text="Settings").click()
mock_device.assert_called_with(text="Settings")
mock_device.return_value.click.assert_called_once()
self.assertIn("Clicked element with text='Settings'", result)
@patch('main.get_device')
def test_input_text(self, mock_get_device):
mock_device = MagicMock()
mock_get_device.return_value = mock_device
result = input_text("Hello World", clear=True)
mock_device.clear_text.assert_called_once()
mock_device.send_keys.assert_called_with("Hello World")
self.assertIn("Typed: Hello World", result)
@patch('main.get_device')
def test_open_stop_app(self, mock_get_device):
mock_device = MagicMock()
mock_get_device.return_value = mock_device
# Test Open
open_app("com.android.settings")
mock_device.app_start.assert_called_with("com.android.settings")
# Test Stop
stop_app("com.android.settings")
mock_device.app_stop.assert_called_with("com.android.settings")
@patch('main.get_device')
def test_swipe(self, mock_get_device):
mock_device = MagicMock()
mock_get_device.return_value = mock_device
swipe(100, 100, 200, 200)
mock_device.swipe.assert_called_with(100, 100, 200, 200, steps=10)
@patch('main.get_device')
def test_shell(self, mock_get_device):
mock_device = MagicMock()
mock_get_device.return_value = mock_device
mock_device.shell.return_value = ("output", 0)
result = shell_command("echo hello")
self.assertIn("Exit code: 0", result)
self.assertIn("output", result)
@patch('main.get_device')
def test_wait_for_element(self, mock_get_device):
mock_device = MagicMock()
mock_get_device.return_value = mock_device
# Test finding
mock_device.return_value.wait.return_value = True
result = wait_for_element(text="Settings")
self.assertIn("Found", result)
# Verify call structure: d(text="Settings").wait(timeout=10)
mock_device.assert_called_with(text="Settings")
mock_device.return_value.wait.assert_called_with(timeout=10)
if __name__ == '__main__':
unittest.main()