File: test_select_target_device.py

package info (click to toggle)
python-briefcase 0.3.22-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,300 kB
  • sloc: python: 59,405; makefile: 57
file content (387 lines) | stat: -rw-r--r-- 12,776 bytes parent folder | download | duplicates (2)
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
from unittest.mock import MagicMock

import pytest

from briefcase.exceptions import BriefcaseCommandError, InvalidDeviceError
from briefcase.integrations.android_sdk import (
    ADB,
    AndroidDeviceNotAuthorized,
    AndroidSDK,
)


@pytest.fixture
def android_sdk(android_sdk, tmp_path) -> AndroidSDK:
    android_sdk.devices = MagicMock(
        return_value={
            "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,
            },
        }
    )

    android_sdk.emulators = MagicMock(
        return_value=[
            "runningEmulator",
            "idleEmulator",
        ]
    )

    # Set up an ADB for each device.
    def mock_adb(device_id):
        adb = MagicMock(spec_set=ADB)
        if device_id == "emulator-5554":
            adb.avd_name.return_value = "runningEmulator"
        else:
            adb.avd_name.return_value = None
        return adb

    android_sdk.adb = mock_adb

    return android_sdk


def test_explicit_device(mock_tools, android_sdk):
    """If the user explicitly names a physical device, it is returned."""

    # Select device with an explicit device ID
    device, name, avd = android_sdk.select_target_device("KABCDABCDA1513")

    # Physical running device, so no AVD
    assert device == "KABCDABCDA1513"
    assert name == "Kogan Agora 9 (KABCDABCDA1513)"
    assert avd is None

    # No input was requested
    assert mock_tools.console.prompts == []


def test_explicit_unauthorized_device(mock_tools, android_sdk):
    """If the user explicitly names an unauthorized physical device, an error is
    raised."""

    # Select unauthorized device with an explicit device ID
    with pytest.raises(AndroidDeviceNotAuthorized):
        android_sdk.select_target_device("041234567892009a")

    # No input was requested
    assert mock_tools.console.prompts == []


def test_explicit_running_emulator_by_id(mock_tools, android_sdk):
    """If the user explicitly names a running emulator by device ID, it is selected."""

    # Select emulator with an explicit device ID
    device, name, avd = android_sdk.select_target_device("emulator-5554")

    # Emulator is running, so there is a device ID
    assert device == "emulator-5554"
    assert name == "@runningEmulator (running emulator)"
    assert avd == "runningEmulator"

    # No input was requested
    assert mock_tools.console.prompts == []


def test_explicit_running_emulator_by_avd(mock_tools, android_sdk):
    """If the user explicitly names a running emulator by AVD, it is selected."""

    # Select emulator with an explicit device ID
    device, name, avd = android_sdk.select_target_device("@runningEmulator")

    # Emulator is running, so there is a device ID
    assert device == "emulator-5554"
    assert name == "@runningEmulator (running emulator)"
    assert avd == "runningEmulator"

    # No input was requested
    assert mock_tools.console.prompts == []


def test_explicit_idle_emulator(mock_tools, android_sdk):
    """If the user explicitly names an idle emulator by AVD, it is selected."""

    # Select emulator with an explicit device ID
    device, name, avd = android_sdk.select_target_device("@idleEmulator")

    # Emulator is not running, so no device ID
    assert device is None
    assert name == "@idleEmulator (emulator)"
    assert avd == "idleEmulator"

    # No input was requested
    assert mock_tools.console.prompts == []


def test_explicit_invalid_device(mock_tools, android_sdk):
    """If the user explicitly names a non-existet device, an error is raised."""

    # Select emulator with an invalid device ID
    with pytest.raises(InvalidDeviceError):
        device, name, avd = android_sdk.select_target_device("deadbeefcafe")

    # No input was requested
    assert mock_tools.console.prompts == []


def test_explicit_invalid_avd(mock_tools, android_sdk):
    """If the user explicitly names a non-existent device, an error is raised."""

    # Select emulator with an invalid AVD
    with pytest.raises(InvalidDeviceError):
        device, name, avd = android_sdk.select_target_device("@invalidEmulator")

    # No input was requested
    assert mock_tools.console.prompts == []


def test_select_device(mock_tools, android_sdk, capsys):
    """If the user manually selects a physical device, details are returned."""
    # Mock the user input
    mock_tools.console.values = ["2"]

    # Run the selection with no pre-existing choice
    device, name, avd = android_sdk.select_target_device(None)

    # Physical running device, so no AVD
    assert device == "KABCDABCDA1513"
    assert name == "Kogan Agora 9 (KABCDABCDA1513)"
    assert avd is None

    # The user was asked to select a device
    assert len(mock_tools.console.prompts) == 1

    # A re-run prompt has been provided
    out = capsys.readouterr().out
    assert "briefcase run android -d KABCDABCDA1513" in out


def test_select_unauthorized_device(mock_tools, android_sdk):
    """If the user manually selects an unauthorized running device, an error is
    raised."""
    # Mock the user input
    mock_tools.console.values = ["3"]

    # Run the selection with no pre-existing choice
    with pytest.raises(AndroidDeviceNotAuthorized):
        android_sdk.select_target_device(None)


def test_select_running_emulator(mock_tools, android_sdk, capsys):
    """If the user manually selects a running emulator, details are returned."""
    # Mock the user input
    mock_tools.console.values = ["1"]

    # Run the selection with no pre-existing choice
    device, name, avd = android_sdk.select_target_device(None)

    # Emulator is running, so there is a device ID
    assert device == "emulator-5554"
    assert name == "@runningEmulator (running emulator)"
    assert avd == "runningEmulator"

    # A re-run prompt has been provided
    out = capsys.readouterr().out
    assert 'briefcase run android -d "@runningEmulator"' in out


def test_select_idle_emulator(mock_tools, android_sdk, capsys):
    """If the user manually selects a running device, details are returned."""
    # Mock the user input
    mock_tools.console.values = ["4"]

    # Run the selection with no pre-existing choice
    device, name, avd = android_sdk.select_target_device(None)

    # Emulator is not running, so no device ID
    assert device is None
    assert name == "@idleEmulator (emulator)"
    assert avd == "idleEmulator"

    # A re-run prompt has been provided
    out = capsys.readouterr().out
    assert 'briefcase run android -d "@idleEmulator"' in out


def test_select_create_emulator(mock_tools, android_sdk, capsys):
    """If the user manually selects a running device, details are returned."""
    # Mock the user input
    mock_tools.console.values = ["5"]

    # Run the selection with no pre-existing choice
    device, name, avd = android_sdk.select_target_device(None)

    # No device details
    assert device is None
    assert name is None
    assert avd is None

    # A re-run prompt has *not* been provided
    out = capsys.readouterr().out
    assert "In future, you could specify this device" not in out


def test_input_disabled(mock_tools, android_sdk):
    """If input has been disabled, and there are multiple simulators, an error is
    raised."""
    mock_tools.console.input_enabled = False

    # Run the selection with no pre-existing choice
    with pytest.raises(BriefcaseCommandError):
        android_sdk.select_target_device(None)

    # No input was requested
    assert mock_tools.console.prompts == []


def test_input_disabled_no_simulators(mock_tools, android_sdk):
    """If input has been disabled, and there are no simulators, 'create' is selected."""
    mock_tools.console.input_enabled = False

    # Remove all the devices and emulators
    android_sdk.devices = MagicMock(return_value={})
    android_sdk.emulators = MagicMock(return_value=[])

    # Run the selection with no pre-existing choice
    device, name, avd = android_sdk.select_target_device(None)

    # No device details
    assert device is None
    assert name is None
    assert avd is None

    # No input was requested
    assert mock_tools.console.prompts == []


def test_input_disabled_one_device(mock_tools, android_sdk):
    """If input has been disabled, and there is a single device, it is selected."""
    mock_tools.console.input_enabled = False

    # Set up a single device.
    android_sdk.devices = MagicMock(
        return_value={
            "KABCDABCDA1513": {
                "name": "Kogan Agora 9",
                "authorized": True,
            },
        }
    )
    android_sdk.emulators = MagicMock(return_value=[])

    # Run the selection with no pre-existing choice
    device, name, avd = android_sdk.select_target_device(None)

    # The only device is returned
    assert device == "KABCDABCDA1513"
    assert name == "Kogan Agora 9 (KABCDABCDA1513)"
    assert avd is None

    # No input was requested
    assert mock_tools.console.prompts == []


def test_explicit_new_device(android_sdk):
    """If the user provides a device configuration, that configuration is created."""
    android_sdk._create_emulator = MagicMock()

    # Select a target device by providing a device config
    device, name, avd = android_sdk.select_target_device('{"avd":"myDevice"}')

    # A request was made to create the emulator using the internal method.
    android_sdk._create_emulator.assert_called_once_with(avd="myDevice")

    assert device is None
    assert name == "@myDevice (emulator)"
    assert avd == "myDevice"


def test_explicit_new_device_existing(android_sdk):
    """If the user provides a device configuration, that configuration is created."""
    android_sdk._create_emulator = MagicMock()

    # Select a target device by providing a device config
    device, name, avd = android_sdk.select_target_device('{"avd":"idleEmulator"}')

    # The emulator already exists, so it won't be created.
    android_sdk._create_emulator.assert_not_called()

    assert device is None
    assert name == "@idleEmulator (emulator)"
    assert avd == "idleEmulator"


def test_explicit_new_device_full_spec(android_sdk):
    """If the user provides a device configuration, that configuration is created."""
    android_sdk._create_emulator = MagicMock()

    # Select a target device by providing a device config
    device, name, avd = android_sdk.select_target_device(
        '{"avd":"myDevice","device_type":"pixel","skin":"pixel_6",'
        '"system_image":"system-images;android-21;default;arm64-v8a"}'
    )

    # A request was made to create the emulator using the internal method.
    android_sdk._create_emulator.assert_called_once_with(
        avd="myDevice",
        device_type="pixel",
        skin="pixel_6",
        system_image="system-images;android-21;default;arm64-v8a",
    )

    assert device is None
    assert name == "@myDevice (emulator)"
    assert avd == "myDevice"


def test_explicit_new_device_bad_spec(android_sdk):
    """If the user provides a bad device configuration, an error is raised."""
    android_sdk._create_emulator = MagicMock()

    # Select a target device by providing a device config
    with pytest.raises(
        BriefcaseCommandError,
        match=r"Unable to create emulator with definition '{NOT A JSON SNIPPET}'",
    ):
        android_sdk.select_target_device("{NOT A JSON SNIPPET}")


def test_explicit_new_device_incomplete_spec(android_sdk):
    """If the user provides a device configuration but doesn't provide an AVD, an error
    is raised."""
    android_sdk._create_emulator = MagicMock()

    # Select a target device by providing a device config
    with pytest.raises(
        BriefcaseCommandError,
        match=r"No AVD provided for new device",
    ):
        android_sdk.select_target_device('{"device_type":"pixel","skin":"pixel_3a"}')


def test_explicit_new_device_unknown_spec(android_sdk):
    """If the user provides a device configuration with an unknown argument, an error is
    raised."""

    def mock_create_emulator(avd, device_type=None, skin=None, system_image=None):
        pass

    android_sdk._create_emulator = MagicMock(side_effect=mock_create_emulator)

    # Select a target device by providing a device config
    with pytest.raises(
        BriefcaseCommandError,
        match=r"Unknown device property 'color'",
    ):
        android_sdk.select_target_device(
            '{"avd":"myPhone","device_type":"pixel","color":"blue"}'
        )