Files
douyin_guaji-main/tests/test_launch_package.py
T
CodexandClaude Opus 4.7 4c1d95d4b5 feat(recovery): 桌面自救改用包名启动抖音,点击坐标降为兜底
当检测到不在抖音前台时,原先点击桌面图标 + 直播 + 进入直播间 3 步坐标的方式
依赖图标位置和分辨率,不同设备/桌面常常失败。改为优先调用 ADB 包名启动:

- underlying_operations 新增 launch_package(device_id, pkg):
  monkey -p <pkg> -c android.intent.category.LAUNCHER 1(主路径)
  失败回退到 am start -n <pkg>/<MainActivity>(多个常见 activity 兜底)
  默认按 com.ss.android.ugc.aweme / .lite / TikTok 顺序尝试

- 新增 is_douyin_foreground 便捷方法

- 改造 3 处自救点:
  fudai_modules/run_once_basic_helpers.py:handle_desktop_recovery
  douyin/other/douyin_yanghao.py(养号循环 + _yanghao_check_and_recover_from_desktop ×2)
  包名启动失败时仍走旧的点击逻辑作为兜底,向后兼容

新增 12 个单元测试覆盖 launch_package 主路径/兜底/失败和自救新行为。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 19:08:09 +08:00

109 lines
4.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""launch_package / is_douyin_foreground 单元测试。
覆盖:
- launch_package:monkey 成功 / monkey 失败 am 兜底成功 / 全部失败
- launch_package:指定包名 vs 默认列表
- is_douyin_foreground:抖音 / 抖音极速版 / 桌面 / 空
"""
from __future__ import annotations
from unittest.mock import patch
import pytest
@pytest.fixture
def op():
"""构造一个 underlying_operations 实例(避免每个测试重复导入)。"""
from douyin.core.underlying_operations import underlying_operations
return underlying_operations()
# ============================================================
# launch_package
# ============================================================
class TestLaunchPackage:
def test_monkey_success_returns_true_and_pkg(self, op):
"""monkey 命令成功(returncode 0 + 输出正常)→ 立即返回。"""
with patch.object(op, "_run_adb_shell_capture", return_value=(True, "Events injected: 1")) as mock_capture, \
patch.object(op, "_run_adb_shell", return_value=False) as mock_shell:
ok, pkg = op.launch_package("MOCK_DEVICE", "com.ss.android.ugc.aweme")
assert ok is True
assert pkg == "com.ss.android.ugc.aweme"
# 应该只调用了 monkey 一次(不需要 am 兜底)
mock_capture.assert_called_once()
mock_shell.assert_not_called()
def test_monkey_no_activities_falls_back_to_am_start(self, op):
"""monkey 输出 'No activities found' 触发 am start 兜底。"""
with patch.object(op, "_run_adb_shell_capture", return_value=(True, "** No activities found to run, monkey aborted.")), \
patch.object(op, "_run_adb_shell", return_value=True) as mock_shell:
ok, pkg = op.launch_package("MOCK_DEVICE", "com.ss.android.ugc.aweme")
assert ok is True
assert pkg == "com.ss.android.ugc.aweme"
# 应该用了 am start 兜底
mock_shell.assert_called()
def test_monkey_aborted_falls_back_to_am_start(self, op):
"""monkey 'aborted' 也触发 am start 兜底。"""
with patch.object(op, "_run_adb_shell_capture", return_value=(True, "Monkey aborted due to error.")), \
patch.object(op, "_run_adb_shell", return_value=True):
ok, pkg = op.launch_package("MOCK_DEVICE", "com.ss.android.ugc.aweme")
assert ok is True
def test_all_methods_fail_returns_false(self, op):
"""monkey 失败 + am start 全部 activity 失败 → 返回 (False, '')。"""
with patch.object(op, "_run_adb_shell_capture", return_value=(False, "")), \
patch.object(op, "_run_adb_shell", return_value=False):
ok, pkg = op.launch_package("MOCK_DEVICE", "com.ss.android.ugc.aweme")
assert ok is False
assert pkg == ""
def test_default_tries_all_packages_in_order(self, op):
"""不指定包名时按 DOUYIN_PACKAGES 顺序尝试,第二个包成功也算成功。"""
call_count = {"n": 0}
def fake_capture(device_id, cmd, **kwargs):
call_count["n"] += 1
# 第一个包失败、第二个包成功
if "com.ss.android.ugc.aweme.lite" in cmd:
return (True, "Events injected: 1")
return (False, "")
with patch.object(op, "_run_adb_shell_capture", side_effect=fake_capture), \
patch.object(op, "_run_adb_shell", return_value=False):
ok, pkg = op.launch_package("MOCK_DEVICE")
assert ok is True
assert pkg == "com.ss.android.ugc.aweme.lite"
def test_empty_package_list_falls_through(self, op):
"""显式传入空字符串使用默认列表。"""
with patch.object(op, "_run_adb_shell_capture", return_value=(True, "Events injected: 1")):
ok, pkg = op.launch_package("MOCK_DEVICE", "")
assert ok is True
# 默认应该尝试主版抖音
assert pkg == "com.ss.android.ugc.aweme"
# ============================================================
# is_douyin_foreground
# ============================================================
class TestIsDouyinForeground:
def test_aweme_main(self, op):
with patch.object(op, "get_top_package", return_value="com.ss.android.ugc.aweme"):
assert op.is_douyin_foreground("MOCK") is True
def test_aweme_lite(self, op):
with patch.object(op, "get_top_package", return_value="com.ss.android.ugc.aweme.lite"):
assert op.is_douyin_foreground("MOCK") is True
def test_launcher_returns_false(self, op):
with patch.object(op, "get_top_package", return_value="com.android.launcher"):
assert op.is_douyin_foreground("MOCK") is False
def test_empty_returns_false(self, op):
with patch.object(op, "get_top_package", return_value=""):
assert op.is_douyin_foreground("MOCK") is False