|
16 | 16 | from pathlib import Path |
17 | 17 | from subprocess import CalledProcessError |
18 | 18 |
|
19 | | -from openandroidinstaller.tooling import search_device |
| 19 | +from openandroidinstaller.tooling import adb_reboot, search_device, check_ab_partition |
| 20 | + |
| 21 | + |
| 22 | +def test_adb_reboot_success(fp): |
| 23 | + """Test if rebooting with adb works fine.""" |
| 24 | + |
| 25 | + with fp.context() as nested_process: |
| 26 | + nested_process.register( |
| 27 | + ["test/path/to/tools/adb", "reboot"], stdout=bytes.fromhex("00") |
| 28 | + ) |
| 29 | + for line in adb_reboot(bin_path=Path("test/path/to/tools")): |
| 30 | + print(line) |
| 31 | + for_later = "error: no devices/emulators found" |
| 32 | + assert line |
| 33 | + |
| 34 | + |
| 35 | +def test_adb_reboot_failure(fp): |
| 36 | + """Test if a fail in rebooting with adb is handled properly.""" |
| 37 | + |
| 38 | + def callback_function_with_kwargs(process, return_code): |
| 39 | + process.returncode = return_code |
| 40 | + |
| 41 | + return_code = 1 |
| 42 | + |
| 43 | + with fp.context() as nested_process: |
| 44 | + nested_process.register( |
| 45 | + ["test/path/to/tools/adb", "reboot"], |
| 46 | + stdout=[ |
| 47 | + bytes("error: no devices/emulators found", encoding="utf-8"), |
| 48 | + ], |
| 49 | + callback=callback_function_with_kwargs, |
| 50 | + callback_kwargs={"return_code": return_code}, |
| 51 | + ) |
| 52 | + for line in adb_reboot(bin_path=Path("test/path/to/tools")): |
| 53 | + print(line) |
| 54 | + assert not line |
20 | 55 |
|
21 | 56 |
|
22 | 57 | def test_search_device_success(mocker): |
@@ -57,3 +92,43 @@ def patched_check_output(*args, **kwargs): |
57 | 92 | ) |
58 | 93 |
|
59 | 94 | assert device_code == None |
| 95 | + |
| 96 | + |
| 97 | +def test_check_ab_device_is_ab(mocker): |
| 98 | + """Test if checking for ab device works fine.""" |
| 99 | + mocker.patch( |
| 100 | + "openandroidinstaller.tooling.check_output", |
| 101 | + return_value=b"[ro.boot.slot_suffix]: [_b]", |
| 102 | + ) |
| 103 | + |
| 104 | + # test linux |
| 105 | + is_ab = check_ab_partition( |
| 106 | + platform="linux", bin_path=Path("openandroidinstaller/bin/") |
| 107 | + ) |
| 108 | + |
| 109 | + assert is_ab |
| 110 | + |
| 111 | + # test windows |
| 112 | + is_ab = check_ab_partition( |
| 113 | + platform="windows", bin_path=Path("openandroidinstaller/bin/") |
| 114 | + ) |
| 115 | + |
| 116 | + assert is_ab |
| 117 | + |
| 118 | + |
| 119 | +def test_check_ab_device_not_ab(mocker): |
| 120 | + """Test if checking for ab device returns False if it fails.""" |
| 121 | + |
| 122 | + def patched_check_output(*args, **kwargs): |
| 123 | + raise CalledProcessError(returncode=1, cmd="output is None") |
| 124 | + |
| 125 | + mocker.patch( |
| 126 | + "openandroidinstaller.tooling.check_output", |
| 127 | + patched_check_output, |
| 128 | + ) |
| 129 | + |
| 130 | + is_ab = check_ab_partition( |
| 131 | + platform="linux", bin_path=Path("openandroidinstaller/bin/") |
| 132 | + ) |
| 133 | + |
| 134 | + assert not is_ab |
0 commit comments