"""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