aboutsummaryrefslogtreecommitdiff
path: root/automated/lib/android_ui_wifi.py
blob: dd32166db568c703b19a1872442661dcca6462c0 (plain)
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
#!/usr/bin/env python3

from android_adb_wrapper import *
import argparse
import sys
from uiautomator import Device


def set_wifi_state(dut, turn_on):
    """Turn WiFi on or off.

        This checks the current WiFi settings and turns it on or off. It does
        nothing if the settings are already in the desired state.

        Parameters:
            dut (Device): The device object.
            enabled: Boolean, true for on, false for off
        Raises:
            DeviceCommandError: If the UI automation fails.
    """
    # Open the Wi-Fi settings
    adb(
        "shell",
        ("am start -a android.settings.WIFI_SETTINGS " "--activity-clear-task"),
        serial=dut.serial,
    )

    # Check if there is an option to turn WiFi on or off
    wifi_enabler = dut(
        text="OFF", resourceId="com.android.settings:id/switch_widget"
    )
    wifi_disabler = dut(
        text="ON", resourceId="com.android.settings:id/switch_widget"
    )

    if not wifi_enabler.exists and not wifi_disabler.exists:
        raise DeviceCommandError(
            dut,
            "UI: set Wi-Fi state",
            "Neither switch for turning Wi-Fi on nor for turning it off are present.",
        )
    if wifi_enabler.exists and wifi_disabler.exists:
        raise DeviceCommandError(
            dut,
            "UI: set Wi-Fi state",
            "Unexpected UI: Both, a switch for turning Wi-Fi on and for turning it off are present.",
        )

    if turn_on:
        if wifi_enabler.exists:
            wifi_enabler.click()
        else:
            print("Wi-Fi is already enabled.")
    else:
        if wifi_disabler.exists:
            wifi_disabler.click()
        else:
            print("Wi-Fi is already disabled.")

    # Leave the settings
    dut.press.back()


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-a",
        dest="ACTION",
        required=True,
        nargs="+",
        help="Action to perform. Following action is currently implemented: \
                        set_wifi_state <on|off>",
    )
    parser.add_argument(
        "-s",
        dest="SERIALS",
        nargs="+",
        help="Serial numbers of devices to configure. \
                        If not present, all available devices will be configured.",
    )
    args = parser.parse_args()

    if args.ACTION[0] != "set_wifi_state" or args.ACTION[1] not in (
        "on",
        "off",
    ):
        print(
            "ERROR: Specified ACTION is not supported: {}".format(args.ACTION),
            file=sys.stderr,
        )
        sys.exit(1)

    serials = args.SERIALS if args.SERIALS is not None else list_devices()

    for serial in serials:
        print("Configuring device {}…".format(serial))

        dut = Device(serial)
        # Work around the not-so-easy Device class
        dut.serial = serial

        try:
            unlock(dut)

            set_wifi_state(dut, args.ACTION[1] == "on")

        except DeviceCommandError as e:
            print("ERROR {}".format(e), file=sys.stderr)


if __name__ == "__main__":
    main()