test: 添加 Windows 测试脚本
- test_network.py: 网络功能测试 (IP/DNS/WiFi/流量) - test_system.py: 系统工具测试 (设备信息/电源/进程) - test_android_api.py: Android API 综合测试 (Intent/集合/文件/Base64/数学/日历) - 所有测试通过
This commit is contained in:
1 parent
b3d75f8cc2
commit
144b5ce45e
7 files changed
+311
No files matched your search
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
@@ -0,0 +1,141 @@
|
|||||||
|
"""
|
||||||
|
Android API 综合测试脚本
|
||||||
|
测试 Android-pyApi 的核心 API 功能
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
|
|
||||||
|
from android_pyapi import (
|
||||||
|
Intent, Uri, Bundle, ComponentName,
|
||||||
|
ArrayList, HashMap, HashSet, JSONObject, JSONArray,
|
||||||
|
String, StringBuilder, Integer, Math, System, Thread,
|
||||||
|
File, FileInputStream, FileOutputStream,
|
||||||
|
Log, Base64, Scanner,
|
||||||
|
Calendar, Date, SimpleDateFormat,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_intent():
|
||||||
|
"""测试 Intent"""
|
||||||
|
print("\n[Intent 测试]")
|
||||||
|
intent = Intent("android.intent.action.VIEW")
|
||||||
|
intent.setData(Uri.parse("https://example.com"))
|
||||||
|
intent.putExtra("key1", "value1")
|
||||||
|
intent.putExtra("key2", 42)
|
||||||
|
print(f" Action: {intent.getAction()}")
|
||||||
|
print(f" Data: {intent.getData()}")
|
||||||
|
print(f" Extra String: {intent.getStringExtra('key1')}")
|
||||||
|
print(f" Extra Int: {intent.getIntExtra('key2', 0)}")
|
||||||
|
|
||||||
|
|
||||||
|
def test_collections():
|
||||||
|
"""测试集合类"""
|
||||||
|
print("\n[集合类测试]")
|
||||||
|
lst = ArrayList()
|
||||||
|
lst.add("Apple")
|
||||||
|
lst.add("Banana")
|
||||||
|
lst.add("Cherry")
|
||||||
|
print(f" ArrayList size: {lst.size()}")
|
||||||
|
print(f" First: {lst.get(0)}")
|
||||||
|
|
||||||
|
m = HashMap()
|
||||||
|
m.put("name", "Android-pyApi")
|
||||||
|
m.put("version", "1.0.0")
|
||||||
|
print(f" HashMap get: {m.get('name')}")
|
||||||
|
|
||||||
|
json = JSONObject()
|
||||||
|
json.put("platform", "Windows")
|
||||||
|
json.put("ok", True)
|
||||||
|
print(f" JSONObject: {json.toString()}")
|
||||||
|
|
||||||
|
|
||||||
|
def test_string():
|
||||||
|
"""测试字符串类"""
|
||||||
|
print("\n[字符串测试]")
|
||||||
|
s = String("Hello, Android-pyApi!")
|
||||||
|
print(f" Length: {s.length()}")
|
||||||
|
print(f" Upper: {s.toUpperCase()}")
|
||||||
|
print(f" Contains 'Android': {s.contains('Android')}")
|
||||||
|
|
||||||
|
sb = StringBuilder()
|
||||||
|
sb.append("Hello")
|
||||||
|
sb.append(", World!")
|
||||||
|
print(f" StringBuilder: {sb.toString()}")
|
||||||
|
|
||||||
|
|
||||||
|
def test_file():
|
||||||
|
"""测试文件类"""
|
||||||
|
print("\n[文件测试]")
|
||||||
|
import tempfile
|
||||||
|
temp_file = os.path.join(tempfile.gettempdir(), "android_pyapi_test.txt")
|
||||||
|
|
||||||
|
f = File(temp_file)
|
||||||
|
print(f" Path: {f.getAbsolutePath()}")
|
||||||
|
|
||||||
|
with open(temp_file, 'w') as file:
|
||||||
|
file.write("Android-pyApi test")
|
||||||
|
|
||||||
|
print(f" Exists: {f.exists()}")
|
||||||
|
print(f" Name: {f.getName()}")
|
||||||
|
print(f" Size: {f.length()} bytes")
|
||||||
|
|
||||||
|
os.remove(temp_file)
|
||||||
|
print(f" After delete: {f.exists()}")
|
||||||
|
|
||||||
|
|
||||||
|
def test_base64():
|
||||||
|
"""测试 Base64"""
|
||||||
|
print("\n[Base64 测试]")
|
||||||
|
text = "Hello, Android-pyApi!"
|
||||||
|
encoded = Base64.encodeToString(text.encode('utf-8'))
|
||||||
|
decoded = Base64.decode(encoded).decode('utf-8')
|
||||||
|
print(f" Original: {text}")
|
||||||
|
print(f" Encoded: {encoded}")
|
||||||
|
print(f" Decoded: {decoded}")
|
||||||
|
print(f" Match: {text == decoded}")
|
||||||
|
|
||||||
|
|
||||||
|
def test_math():
|
||||||
|
"""测试数学类"""
|
||||||
|
print("\n[数学测试]")
|
||||||
|
print(f" PI: {Math.PI}")
|
||||||
|
print(f" abs(-42): {Math.abs(-42)}")
|
||||||
|
print(f" max(10, 20): {Math.max(10, 20)}")
|
||||||
|
print(f" sqrt(144): {Math.sqrt(144)}")
|
||||||
|
print(f" pow(2, 10): {Math.pow(2, 10)}")
|
||||||
|
|
||||||
|
|
||||||
|
def test_calendar():
|
||||||
|
"""测试日历类"""
|
||||||
|
print("\n[日历测试]")
|
||||||
|
cal = Calendar.getInstance()
|
||||||
|
print(f" Year: {cal.get(Calendar.YEAR)}")
|
||||||
|
print(f" Month: {cal.get(Calendar.MONTH) + 1}")
|
||||||
|
print(f" Day: {cal.get(Calendar.DAY_OF_MONTH)}")
|
||||||
|
print(f" Hour: {cal.get(Calendar.HOUR_OF_DAY)}")
|
||||||
|
print(f" Minute: {cal.get(Calendar.MINUTE)}")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""主测试函数"""
|
||||||
|
print("=" * 60)
|
||||||
|
print("Android-pyApi 综合测试")
|
||||||
|
print("=" * 60)
|
||||||
|
|
||||||
|
test_intent()
|
||||||
|
test_collections()
|
||||||
|
test_string()
|
||||||
|
test_file()
|
||||||
|
test_base64()
|
||||||
|
test_math()
|
||||||
|
test_calendar()
|
||||||
|
|
||||||
|
print("\n" + "=" * 60)
|
||||||
|
print("所有测试完成!")
|
||||||
|
print("=" * 60)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
"""
|
||||||
|
网络测试脚本 - 基于 Android-pyApi
|
||||||
|
模拟 Operit 的 network_test.js 功能
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
|
|
||||||
|
from android_pyapi import (
|
||||||
|
ConnectivityManager, NetworkInfo, Intent, Uri
|
||||||
|
)
|
||||||
|
from android_pyapi._windows_impl import WindowsNetwork
|
||||||
|
|
||||||
|
|
||||||
|
def test_network():
|
||||||
|
"""测试网络功能"""
|
||||||
|
print("=" * 60)
|
||||||
|
print("Android-pyApi 网络测试")
|
||||||
|
print("=" * 60)
|
||||||
|
|
||||||
|
# 1. 获取 IP 地址
|
||||||
|
print("\n[1] IP 地址:")
|
||||||
|
ip = WindowsNetwork.get_ip_address()
|
||||||
|
print(f" 本机 IP: {ip}")
|
||||||
|
|
||||||
|
# 2. 网络连通性
|
||||||
|
print("\n[2] 网络连通性:")
|
||||||
|
connected = WindowsNetwork.get_connectivity()
|
||||||
|
print(f" 连接状态: {'已连接' if connected else '未连接'}")
|
||||||
|
|
||||||
|
# 3. WiFi 信息
|
||||||
|
print("\n[3] WiFi 信息:")
|
||||||
|
wifi = WindowsNetwork.get_wifi_info()
|
||||||
|
print(f" SSID: {wifi.get('ssid', 'N/A') or 'N/A'}")
|
||||||
|
print(f" 信号: {wifi.get('signal', 0)}%")
|
||||||
|
print(f" 状态: {wifi.get('status', 'unknown')}")
|
||||||
|
|
||||||
|
# 4. DNS 服务器
|
||||||
|
print("\n[4] DNS 服务器:")
|
||||||
|
dns = WindowsNetwork.get_dns_servers()
|
||||||
|
for d in dns[:3]:
|
||||||
|
print(f" - {d}")
|
||||||
|
|
||||||
|
# 5. 默认网关
|
||||||
|
print("\n[5] 默认网关:")
|
||||||
|
gw = WindowsNetwork.get_gateway()
|
||||||
|
print(f" 网关: {gw}")
|
||||||
|
|
||||||
|
# 6. MAC 地址
|
||||||
|
print("\n[6] MAC 地址:")
|
||||||
|
mac = WindowsNetwork.get_mac_address()
|
||||||
|
print(f" MAC: {mac}")
|
||||||
|
|
||||||
|
# 7. 网络接口
|
||||||
|
print("\n[7] 网络接口:")
|
||||||
|
interfaces = WindowsNetwork.get_interfaces()
|
||||||
|
for iface in interfaces[:5]:
|
||||||
|
print(f" - {iface.get('ifname', 'N/A')}: {iface.get('operstate', 'N/A')}")
|
||||||
|
|
||||||
|
# 8. 流量统计
|
||||||
|
print("\n[8] 流量统计:")
|
||||||
|
traffic = WindowsNetwork.get_rx_tx_bytes()
|
||||||
|
print(f" 接收: {traffic.get('rx_bytes', 0) / 1024 / 1024:.2f} MB")
|
||||||
|
print(f" 发送: {traffic.get('tx_bytes', 0) / 1024 / 1024:.2f} MB")
|
||||||
|
|
||||||
|
print("\n" + "=" * 60)
|
||||||
|
print("测试完成!")
|
||||||
|
print("=" * 60)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_network()
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
"""
|
||||||
|
系统工具测试脚本 - 基于 Android-pyApi
|
||||||
|
模拟 Operit 的 system_tools.js 功能
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
|
|
||||||
|
from android_pyapi import (
|
||||||
|
Build, PowerManager, BatteryManager, ActivityManager,
|
||||||
|
Environment, Log
|
||||||
|
)
|
||||||
|
from android_pyapi._windows_impl import (
|
||||||
|
WindowsPower, WindowsProcess, WindowsDisplay, WindowsShell
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_system():
|
||||||
|
"""测试系统工具功能"""
|
||||||
|
print("=" * 60)
|
||||||
|
print("Android-pyApi 系统工具测试")
|
||||||
|
print("=" * 60)
|
||||||
|
|
||||||
|
# 1. 设备信息 (Build)
|
||||||
|
print("\n[1] 设备信息 (Build):")
|
||||||
|
print(f" MODEL: {Build.MODEL}")
|
||||||
|
print(f" MANUFACTURER: {Build.MANUFACTURER}")
|
||||||
|
print(f" BRAND: {Build.BRAND}")
|
||||||
|
print(f" DEVICE: {Build.DEVICE}")
|
||||||
|
print(f" PRODUCT: {Build.PRODUCT}")
|
||||||
|
print(f" FINGERPRINT: {Build.FINGERPRINT}")
|
||||||
|
|
||||||
|
# 2. 系统信息
|
||||||
|
print("\n[2] 系统信息:")
|
||||||
|
sys_info = WindowsProcess.get_system_info()
|
||||||
|
print(f" 主机名: {sys_info.get('hostname', 'N/A')}")
|
||||||
|
print(f" 平台: {sys_info.get('platform', 'N/A')}")
|
||||||
|
print(f" 版本: {sys_info.get('windows_version', 'N/A')}")
|
||||||
|
print(f" 架构: {sys_info.get('arch', 'N/A')}")
|
||||||
|
print(f" Python: {sys_info.get('python', 'N/A')}")
|
||||||
|
if 'cpu_count' in sys_info:
|
||||||
|
print(f" CPU 核心: {sys_info['cpu_count']}")
|
||||||
|
if 'memory_total' in sys_info:
|
||||||
|
print(f" 内存总量: {sys_info['memory_total'] / 1024 / 1024 / 1024:.2f} GB")
|
||||||
|
|
||||||
|
# 3. 电源信息
|
||||||
|
print("\n[3] 电源信息:")
|
||||||
|
battery = WindowsPower.get_battery_info()
|
||||||
|
print(f" 电池存在: {battery['present']}")
|
||||||
|
print(f" 电量: {battery['level']}%")
|
||||||
|
print(f" 状态: {battery['status']}")
|
||||||
|
|
||||||
|
power = WindowsPower.get_power_status()
|
||||||
|
print(f" 充电中: {power['is_charging']}")
|
||||||
|
|
||||||
|
# 4. CPU 和内存
|
||||||
|
print("\n[4] CPU 和内存:")
|
||||||
|
cpu = WindowsPower.get_cpu_usage()
|
||||||
|
print(f" CPU 使用率: {cpu}%")
|
||||||
|
|
||||||
|
mem = WindowsPower.get_memory_info()
|
||||||
|
print(f" 内存使用率: {mem['percent']}%")
|
||||||
|
print(f" 可用内存: {mem['available'] / 1024 / 1024 / 1024:.2f} GB")
|
||||||
|
|
||||||
|
# 5. 显示信息
|
||||||
|
print("\n[5] 显示信息:")
|
||||||
|
w, h = WindowsDisplay.get_screen_size()
|
||||||
|
print(f" 屏幕尺寸: {w}x{h}")
|
||||||
|
|
||||||
|
x, y = WindowsDisplay.get_mouse_position()
|
||||||
|
print(f" 鼠标位置: ({x}, {y})")
|
||||||
|
|
||||||
|
# 6. 进程列表
|
||||||
|
print("\n[6] 进程列表 (前10个):")
|
||||||
|
procs = WindowsProcess.list_processes()
|
||||||
|
for p in procs[:10]:
|
||||||
|
print(f" - {p.get('name', 'N/A')} (PID: {p.get('pid', 'N/A')})")
|
||||||
|
|
||||||
|
# 7. 运行中的应用
|
||||||
|
print("\n[7] 运行中的应用 (前5个):")
|
||||||
|
apps = WindowsProcess.get_running_apps()
|
||||||
|
for app in apps[:5]:
|
||||||
|
print(f" - {app.get('name', 'N/A')}")
|
||||||
|
|
||||||
|
# 8. Shell 命令测试
|
||||||
|
print("\n[8] Shell 命令测试:")
|
||||||
|
result = WindowsShell.exec("echo Hello from Android-pyApi!")
|
||||||
|
print(f" 输出: {result.get('stdout', 'N/A')}")
|
||||||
|
|
||||||
|
print("\n" + "=" * 60)
|
||||||
|
print("测试完成!")
|
||||||
|
print("=" * 60)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_system()
|
||||||
Whitespace-only changes.
Reference in new issue
Block a user