You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
241 lines
9.0 KiB
241 lines
9.0 KiB
"""
|
|
滑动手势集成测试
|
|
"""
|
|
import pytest
|
|
import asyncio
|
|
from unittest.mock import Mock, patch, AsyncMock
|
|
from app.services.swipe_service import SwipeService
|
|
from app.schemas.swipe import SwipeRequest, SwipeDirectionRequest, SwipePointRequest
|
|
from app.core.device.manager import DeviceSource
|
|
|
|
class TestSwipeService:
|
|
"""滑动手势服务测试类"""
|
|
|
|
@pytest.fixture
|
|
def swipe_service(self):
|
|
"""创建滑动手势服务实例"""
|
|
return SwipeService()
|
|
|
|
@pytest.fixture
|
|
def mock_device(self):
|
|
"""模拟设备"""
|
|
device = Mock()
|
|
device.device_id = "test_device_123"
|
|
device.protocol_type = "adb"
|
|
return device
|
|
|
|
@pytest.fixture
|
|
def mock_device_manager(self):
|
|
"""模拟设备管理器"""
|
|
manager = Mock()
|
|
manager.get_device = AsyncMock()
|
|
return manager
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_perform_swipe_registered_device(self, swipe_service, mock_device_manager):
|
|
"""测试注册设备执行滑动手势"""
|
|
# 设置模拟
|
|
with patch.object(swipe_service, 'get_device_source') as mock_get_source, \
|
|
patch.object(swipe_service, '_execute_registered_device_swipe') as mock_execute:
|
|
|
|
# 设置模拟返回值
|
|
mock_get_source.return_value = DeviceSource.REGISTERED
|
|
mock_execute.return_value = {
|
|
"success": True,
|
|
"message": "swipe down from (540,384) to (540,1536) in 300 ms on 1080x1920",
|
|
"device_id": "test_device_123",
|
|
"direction": "down",
|
|
"distance": 0.6,
|
|
"duration": 300,
|
|
"screen_size": {"width": 1080, "height": 1920}
|
|
}
|
|
|
|
# 执行测试
|
|
result = await swipe_service.perform_swipe(
|
|
device_id="test_device_123",
|
|
x=0.5,
|
|
y=0.2,
|
|
direction="down",
|
|
distance=0.6,
|
|
duration=300
|
|
)
|
|
|
|
# 验证结果
|
|
assert result["success"] is True
|
|
assert result["device_id"] == "test_device_123"
|
|
assert result["direction"] == "down"
|
|
assert result["distance"] == 0.6
|
|
assert result["duration"] == 300
|
|
assert result["screen_size"] == {"width": 1080, "height": 1920}
|
|
|
|
# 验证调用
|
|
mock_get_source.assert_called_once_with("test_device_123")
|
|
mock_execute.assert_called_once_with("test_device_123", 0.5, 0.2, "down", 0.6, 300)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_perform_swipe_auto_discovered_device(self, swipe_service, mock_device_manager):
|
|
"""测试自动发现设备执行滑动手势"""
|
|
# 设置模拟
|
|
with patch.object(swipe_service, 'get_device_source') as mock_get_source, \
|
|
patch.object(swipe_service, '_execute_auto_discovered_device_swipe') as mock_execute:
|
|
|
|
# 设置模拟返回值
|
|
mock_get_source.return_value = DeviceSource.AUTO_DISCOVERED
|
|
mock_execute.return_value = {
|
|
"success": True,
|
|
"message": "swipe down from (540,384) to (540,1536) in 300 ms on 1080x1920",
|
|
"device_id": "test_device_123",
|
|
"direction": "down",
|
|
"distance": 0.6,
|
|
"duration": 300,
|
|
"screen_size": {"width": 1080, "height": 1920}
|
|
}
|
|
|
|
# 执行测试
|
|
result = await swipe_service.perform_swipe(
|
|
device_id="test_device_123",
|
|
x=0.5,
|
|
y=0.2,
|
|
direction="down",
|
|
distance=0.6,
|
|
duration=300
|
|
)
|
|
|
|
# 验证结果
|
|
assert result["success"] is True
|
|
assert result["device_id"] == "test_device_123"
|
|
assert result["direction"] == "down"
|
|
assert result["distance"] == 0.6
|
|
assert result["duration"] == 300
|
|
assert result["screen_size"] == {"width": 1080, "height": 1920}
|
|
|
|
# 验证调用
|
|
mock_get_source.assert_called_once_with("test_device_123")
|
|
mock_execute.assert_called_once_with("test_device_123", 0.5, 0.2, "down", 0.6, 300)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_perform_swipe_device_not_found(self, swipe_service, mock_device_manager):
|
|
"""测试设备不存在的情况"""
|
|
# 设置模拟
|
|
with patch.object(swipe_service, 'get_device_source') as mock_get_source:
|
|
# 模拟设备不存在的情况
|
|
mock_get_source.side_effect = ValueError("设备 non_existent_device 不存在")
|
|
|
|
# 执行测试
|
|
result = await swipe_service.perform_swipe(
|
|
device_id="non_existent_device",
|
|
direction="down"
|
|
)
|
|
|
|
# 验证结果
|
|
assert result["success"] is False
|
|
assert "设备 non_existent_device 不存在" in result["error"]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_perform_swipe_invalid_direction(self, swipe_service, mock_device_manager):
|
|
"""测试无效滑动方向"""
|
|
# 设置模拟
|
|
with patch.object(swipe_service, 'get_device_source') as mock_get_source, \
|
|
patch.object(swipe_service, '_execute_auto_discovered_device_swipe') as mock_execute:
|
|
|
|
# 设置模拟返回值
|
|
mock_get_source.return_value = DeviceSource.AUTO_DISCOVERED
|
|
mock_execute.return_value = {
|
|
"success": False,
|
|
"error": "无效的滑动方向: invalid_direction",
|
|
"device_id": "test_device_123"
|
|
}
|
|
|
|
# 执行测试
|
|
result = await swipe_service.perform_swipe(
|
|
device_id="test_device_123",
|
|
direction="invalid_direction"
|
|
)
|
|
|
|
# 验证结果
|
|
assert result["success"] is False
|
|
assert "无效的滑动方向" in result["error"]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_swipe_direction_methods(self, swipe_service, mock_device_manager):
|
|
"""测试方向滑动方法"""
|
|
# 设置模拟
|
|
with patch.object(swipe_service, 'perform_swipe') as mock_perform:
|
|
mock_perform.return_value = {"success": True, "message": "test"}
|
|
|
|
# 测试各个方向滑动方法
|
|
await swipe_service.swipe_up("test_device_123")
|
|
mock_perform.assert_called_with("test_device_123", direction="up", distance=0.6, duration=300)
|
|
|
|
await swipe_service.swipe_down("test_device_123")
|
|
mock_perform.assert_called_with("test_device_123", direction="down", distance=0.6, duration=300)
|
|
|
|
await swipe_service.swipe_left("test_device_123")
|
|
mock_perform.assert_called_with("test_device_123", direction="left", distance=0.6, duration=300)
|
|
|
|
await swipe_service.swipe_right("test_device_123")
|
|
mock_perform.assert_called_with("test_device_123", direction="right", distance=0.6, duration=300)
|
|
|
|
class TestSwipeSchemas:
|
|
"""滑动手势数据模型测试类"""
|
|
|
|
def test_swipe_request_valid(self):
|
|
"""测试有效的滑动手势请求"""
|
|
request = SwipeRequest(
|
|
device_id="test_device",
|
|
x=0.5,
|
|
y=0.3,
|
|
direction="up",
|
|
distance=0.5,
|
|
duration=200
|
|
)
|
|
|
|
assert request.device_id == "test_device"
|
|
assert request.x == 0.5
|
|
assert request.y == 0.3
|
|
assert request.direction == "up"
|
|
assert request.distance == 0.5
|
|
assert request.duration == 200
|
|
|
|
def test_swipe_request_defaults(self):
|
|
"""测试滑动手势请求默认值"""
|
|
request = SwipeRequest(device_id="test_device")
|
|
|
|
assert request.x == 0.5
|
|
assert request.y == 0.2
|
|
assert request.direction == "down"
|
|
assert request.distance == 0.6
|
|
assert request.duration == 300
|
|
|
|
def test_swipe_direction_request(self):
|
|
"""测试方向滑动手势请求"""
|
|
request = SwipeDirectionRequest(
|
|
device_id="test_device",
|
|
distance=0.4,
|
|
duration=250
|
|
)
|
|
|
|
assert request.device_id == "test_device"
|
|
assert request.distance == 0.4
|
|
assert request.duration == 250
|
|
|
|
def test_swipe_point_request(self):
|
|
"""测试从指定点滑动手势请求"""
|
|
request = SwipePointRequest(
|
|
device_id="test_device",
|
|
x=0.7,
|
|
y=0.8,
|
|
direction="left",
|
|
distance=0.3,
|
|
duration=150
|
|
)
|
|
|
|
assert request.device_id == "test_device"
|
|
assert request.x == 0.7
|
|
assert request.y == 0.8
|
|
assert request.direction == "left"
|
|
assert request.distance == 0.3
|
|
assert request.duration == 150
|
|
|
|
if __name__ == "__main__":
|
|
pytest.main([__file__])
|
|
|