1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
|
import subprocess
from unittest.mock import MagicMock, call
import pytest
from briefcase.exceptions import BriefcaseCommandError, InvalidDeviceError
from briefcase.integrations.android_sdk import ADB, AndroidSDK
@pytest.fixture
def android_sdk(android_sdk, mock_tools, tmp_path) -> AndroidSDK:
android_sdk.sleep = MagicMock()
android_sdk.mock_run = MagicMock(spec_set=ADB.run)
def mock_adb(device):
adb = ADB(mock_tools, device)
adb.run = android_sdk.mock_run
return adb
android_sdk.adb = mock_adb
# Mock some existing emulators
android_sdk.emulators = MagicMock(
return_value=[
"runningEmulator",
"idleEmulator",
]
)
return android_sdk
def test_invalid_emulator(android_sdk):
"""Attempting to start an invalid emulator raises an error."""
with pytest.raises(InvalidDeviceError):
android_sdk.start_emulator("no-such-avd")
def test_start_emulator(mock_tools, android_sdk):
"""An emulator can be started."""
# Mock 4 calls to devices.
# First call returns 3 devices, but not the new emulator.
# Second call returns the same thing.
# Third call returns the new device in an offline state.
# Last call returns the new device in an online state.
devices = {
"041234567892009a": {
"name": "Unknown device (not authorized for development)",
"authorized": False,
},
"KABCDABCDA1513": {
"name": "Kogan Agora 9",
"authorized": True,
},
"emulator-5554": {
"name": "Android SDK built for x86",
"authorized": True,
},
}
devices_3 = devices.copy()
devices_3["emulator-5556"] = {
"name": "Unknown device (offline)",
"authorized": False,
}
devices_4 = devices.copy()
devices_4["emulator-5556"] = {
"name": "generic_x86",
"authorized": True,
}
# This will result in 4 calls to get devices
android_sdk.devices = MagicMock(
side_effect=[
devices,
devices,
devices_3,
devices_4,
]
)
# There will be 5 calls on adb.run (3 calls to avd_name, then
# 2 calls to getprop)
android_sdk.mock_run.side_effect = [
# emu avd_name
subprocess.CalledProcessError(returncode=1, cmd="emu avd name"),
"runningEmulator\nOK",
"idleEmulator\nOK",
# shell getprop sys.boot_completed
"\n",
"1\n",
]
# poll() on the process continues to return None, indicating no problem.
emu_popen = MagicMock(spec=subprocess.Popen)
emu_popen.args = [android_sdk.emulator_path, "@idleEmulator"]
emu_popen.poll.return_value = None
mock_tools.subprocess.Popen.return_value = emu_popen
# Start the emulator
device, name = android_sdk.start_emulator("idleEmulator")
# The device details are as expected
assert device == "emulator-5556"
assert name == "@idleEmulator (running emulator)"
# The process was started.
mock_tools.subprocess.Popen.assert_called_with(
[
android_sdk.emulator_path,
"@idleEmulator",
"-dns-server",
"8.8.8.8",
],
env=android_sdk.env,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
bufsize=1,
start_new_session=True,
)
mock_tools.subprocess.stream_output_non_blocking.assert_called_with(
label="Android emulator",
popen_process=emu_popen,
capture_output=True,
)
# There were 5 calls to run
android_sdk.mock_run.assert_has_calls(
[
# Three calls to get avd name
call("emu", "avd", "name", quiet=1),
call("emu", "avd", "name", quiet=1),
call("emu", "avd", "name", quiet=1),
# 2 calls to get boot property
call("shell", "getprop", "sys.boot_completed"),
call("shell", "getprop", "sys.boot_completed"),
]
)
# Took a total of 3 naps.
assert android_sdk.sleep.call_count == 3
def test_start_emulator_fast_start(mock_tools, android_sdk):
"""If the emulator starts quickly, that's OK."""
# If the emulator starts *really* fast, there will only be 1 call to devices.
devices = {
"041234567892009a": {
"name": "Unknown device (not authorized for development)",
"authorized": False,
},
"KABCDABCDA1513": {
"name": "Kogan Agora 9",
"authorized": True,
},
"emulator-5554": {
"name": "generic_x86",
"authorized": True,
},
}
android_sdk.devices = MagicMock(
side_effect=[
devices,
]
)
# There will be 3 calls on adb.run (2 calls to avd_name, then
# 1 call to getprop)
android_sdk.mock_run.side_effect = [
# emu avd_name
subprocess.CalledProcessError(
returncode=1, cmd="emu avd name"
), # physical device
"idleEmulator\nOK", # running emulator
# shell getprop sys.boot_completed
"1\n",
]
# poll() on the process continues to return None, indicating no problem.
emu_popen = MagicMock(spec=subprocess.Popen)
emu_popen.args = [android_sdk.emulator_path, "@idleEmulator"]
emu_popen.poll.return_value = None
mock_tools.subprocess.Popen.return_value = emu_popen
# Start the emulator
device, name = android_sdk.start_emulator("idleEmulator")
# The device details are as expected
assert device == "emulator-5554"
assert name == "@idleEmulator (running emulator)"
# The process was started.
mock_tools.subprocess.Popen.assert_called_with(
[
android_sdk.emulator_path,
"@idleEmulator",
"-dns-server",
"8.8.8.8",
],
env=android_sdk.env,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
bufsize=1,
start_new_session=True,
)
mock_tools.subprocess.stream_output_non_blocking.assert_called_with(
label="Android emulator",
popen_process=emu_popen,
capture_output=True,
)
# There were 3 calls to run
android_sdk.mock_run.assert_has_calls(
[
# 2 calls to get avd name
call("emu", "avd", "name", quiet=1),
call("emu", "avd", "name", quiet=1),
# 1 calls to get boot property
call("shell", "getprop", "sys.boot_completed"),
]
)
# Took no naps, as everything was ready when we found it.
assert android_sdk.sleep.call_count == 0
def test_emulator_fail_to_start(mock_tools, android_sdk):
"""If the emulator fails to start, and error is displayed."""
# Mock 4 calls to devices.
# First call returns 3 devices, but not the new emulator.
# Second call returns the same thing.
# Third call returns the new device in an offline state.
# Last call returns the new device in an online state.
devices = {
"041234567892009a": {
"name": "Unknown device (not authorized for development)",
"authorized": False,
},
"KABCDABCDA1513": {
"name": "Kogan Agora 9",
"authorized": True,
},
"emulator-5554": {
"name": "Android SDK built for x86",
"authorized": True,
},
}
devices_3 = devices.copy()
devices_3["emulator-5556"] = {
"name": "Unknown device (offline)",
"authorized": False,
}
devices_4 = devices.copy()
devices_4["emulator-5556"] = {
"name": "generic_x86",
"authorized": True,
}
# This will result in 4 calls to get devices
android_sdk.devices = MagicMock(
side_effect=[
devices,
devices,
devices_3,
devices_4,
]
)
# This will result in 5 calls on adb.run (3 calls to avd_name, then
# 2 calls to getprop)
android_sdk.mock_run.side_effect = [
# emu avd_name
subprocess.CalledProcessError(returncode=1, cmd="emu avd name"),
"runningEmulator\nOK",
"idleEmulator\nOK",
# shell getprop sys.boot_completed
"\n",
"1\n",
]
# poll() on the process returns None for the first two attempts, but then
# returns 1 indicating failure.
emu_popen = MagicMock(spec=subprocess.Popen)
emu_popen.args = [android_sdk.emulator_path, "@idleEmulator"]
emu_popen.poll.side_effect = [None, None, 1]
mock_tools.subprocess.Popen.return_value = emu_popen
# Start the emulator
with pytest.raises(BriefcaseCommandError) as exc_info:
android_sdk.start_emulator("idleEmulator")
# The process was started.
mock_tools.subprocess.Popen.assert_called_with(
[
android_sdk.emulator_path,
"@idleEmulator",
"-dns-server",
"8.8.8.8",
],
env=android_sdk.env,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
bufsize=1,
start_new_session=True,
)
mock_tools.subprocess.stream_output_non_blocking.assert_called_with(
label="Android emulator",
popen_process=emu_popen,
capture_output=True,
)
# There were 2 calls to run, both to get AVD name
android_sdk.mock_run.assert_has_calls(
[
call("emu", "avd", "name", quiet=1),
call("emu", "avd", "name", quiet=1),
]
)
# Took a total of 2 naps before failing.
assert android_sdk.sleep.call_count == 2
# Expected error message was printed
assert "Android emulator was unable to start!" in exc_info.value.msg
def test_emulator_fail_to_boot(mock_tools, android_sdk):
"""If the emulator fails to boot, and error is displayed."""
# Mock 4 calls to devices.
# First call returns 3 devices, but not the new emulator.
# Second call returns the same thing.
# Third call returns the new device in an offline state.
# Last call returns the new device in an online state.
devices = {
"041234567892009a": {
"name": "Unknown device (not authorized for development)",
"authorized": False,
},
"KABCDABCDA1513": {
"name": "Kogan Agora 9",
"authorized": True,
},
"emulator-5554": {
"name": "Android SDK built for x86",
"authorized": True,
},
}
devices_3 = devices.copy()
devices_3["emulator-5556"] = {
"name": "Unknown device (offline)",
"authorized": False,
}
devices_4 = devices.copy()
devices_4["emulator-5556"] = {
"name": "Android SDK built for x86",
"authorized": True,
}
# This will result in 4 calls to get devices
android_sdk.devices = MagicMock(
side_effect=[
devices,
devices,
devices_3,
devices_4,
]
)
# This will result in 6 calls on adb.run
# 3 calls to avd_name and then 3 calls to getprop
android_sdk.mock_run.side_effect = [
# emu avd_name
subprocess.CalledProcessError(returncode=1, cmd="emu avd name"),
"runningEmulator\nOK",
"idleEmulator\nOK",
# shell getprop sys.boot_completed
"\n", # gets in to emulator has_booted() if block
"\n", # enters has_booted() while loop
"\n", # one loop waiting for simulator to finish booting
"1\n", # successful boot...except poll() will return non-None first raising failure
]
# poll() on the process returns failure during simulator boot
emu_popen = MagicMock(spec=subprocess.Popen)
emu_popen.args = [android_sdk.emulator_path, "@idleEmulator"]
emu_popen.poll.side_effect = [
None, # in start loop without emulator in device list
None, # in start loop without emulator in device list
None, # in start loop with emulator in offline mode
None, # in start loop with emulator in online mode
None, # in boot loop waiting for emulator to finish booting
1, # invoke failure mode for simulator boot
]
emu_popen.args = [android_sdk.emulator_path, "@idleEmulator"]
mock_tools.subprocess.Popen.return_value = emu_popen
# Start the emulator
with pytest.raises(BriefcaseCommandError) as exc_info:
android_sdk.start_emulator("idleEmulator")
# The process was started.
mock_tools.subprocess.Popen.assert_called_with(
[
android_sdk.emulator_path,
"@idleEmulator",
"-dns-server",
"8.8.8.8",
],
env=android_sdk.env,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
bufsize=1,
start_new_session=True,
)
mock_tools.subprocess.stream_output_non_blocking.assert_called_with(
label="Android emulator",
popen_process=emu_popen,
capture_output=True,
)
# There were 6 calls to run before failure
android_sdk.mock_run.assert_has_calls(
[
# 3 calls to get avd name
call("emu", "avd", "name", quiet=1),
call("emu", "avd", "name", quiet=1),
call("emu", "avd", "name", quiet=1),
# 3 calls to get boot property (since boot fails
# before last/fourth one would return success)
call("shell", "getprop", "sys.boot_completed"),
call("shell", "getprop", "sys.boot_completed"),
call("shell", "getprop", "sys.boot_completed"),
]
)
# Took a total of 4 naps.
assert android_sdk.sleep.call_count == 4
# Expected error message was printed.
assert "Android emulator was unable to boot!" in exc_info.value.msg
def test_emulator_ctrl_c(mock_tools, android_sdk, capsys):
"""If emulator startup is interrupted by the user, an error is displayed."""
# Short circuit device loop by returning no devices
android_sdk.devices = MagicMock(side_effect=[{}, {}])
# poll() on the process returns None for the first two attempts, but then
# raises KeyboardInterrupt to simulate the user sending CTRL-C
emu_popen = MagicMock(spec=subprocess.Popen)
emu_popen.args = [android_sdk.emulator_path, "@idleEmulator"]
emu_popen.poll.side_effect = [None, None, KeyboardInterrupt]
mock_tools.subprocess.Popen.return_value = emu_popen
# Mock emulator output
emu_streamer = MagicMock()
mock_tools.subprocess.stream_output_non_blocking = MagicMock(
return_value=emu_streamer
)
emu_streamer.captured_output = "This is stdout"
# Start the emulator
with pytest.raises(KeyboardInterrupt):
android_sdk.start_emulator("idleEmulator")
# The process was started.
mock_tools.subprocess.Popen.assert_called_with(
[
android_sdk.emulator_path,
"@idleEmulator",
"-dns-server",
"8.8.8.8",
],
env=android_sdk.env,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
bufsize=1,
start_new_session=True,
)
mock_tools.subprocess.stream_output_non_blocking.assert_called_with(
label="Android emulator",
popen_process=emu_popen,
capture_output=True,
)
# Took a total of 2 naps before KeyboardInterrupt.
assert android_sdk.sleep.call_count == 2
output = capsys.readouterr().out
# Emulator's log was printed.
assert "Emulator output log for startup failure\nThis is stdout" in output
# Expected error message was printed.
assert "Is the Android emulator not starting up properly?" in output
def test_start_emulator_extra_args(mock_tools, android_sdk):
"""The emulator can be started with extra arguments."""
devices = {
"emulator-5554": {
"name": "generic_x86",
"authorized": True,
},
}
android_sdk.devices = MagicMock(
side_effect=[
devices,
]
)
# There will be 2 calls on adb.run (1 call to avd_name, then
# 1 call to getprop)
android_sdk.mock_run.side_effect = [
# emu avd_name
"idleEmulator\nOK", # running emulator
# shell getprop sys.boot_completed
"1\n",
]
# poll() on the process continues to return None, indicating no problem.
emu_popen = MagicMock(spec=subprocess.Popen)
emu_popen.args = [android_sdk.emulator_path, "@idleEmulator"]
emu_popen.poll.return_value = None
mock_tools.subprocess.Popen.return_value = emu_popen
# Start the emulator with extra arguments
device, name = android_sdk.start_emulator(
"idleEmulator", ["-no-window", "-no-audio"]
)
# The device details are as expected
assert device == "emulator-5554"
assert name == "@idleEmulator (running emulator)"
# The process was started with the headless test options.
mock_tools.subprocess.Popen.assert_called_with(
[
android_sdk.emulator_path,
"@idleEmulator",
"-dns-server",
"8.8.8.8",
"-no-window",
"-no-audio",
],
env=android_sdk.env,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
bufsize=1,
start_new_session=True,
)
mock_tools.subprocess.stream_output_non_blocking.assert_called_with(
label="Android emulator",
popen_process=emu_popen,
capture_output=True,
)
# There were 2 calls to run
android_sdk.mock_run.assert_has_calls(
[
# 1 call to get avd name
call("emu", "avd", "name", quiet=1),
# 1 calls to get boot property
call("shell", "getprop", "sys.boot_completed"),
]
)
# Took no naps, as everything was ready when we found it.
assert android_sdk.sleep.call_count == 0
|