blob: dd32166db568c703b19a1872442661dcca6462c0 [file] [log] [blame]
Karsten Tausche81ffea22018-06-19 15:34:01 +02001#!/usr/bin/env python3
2
3from android_adb_wrapper import *
4import argparse
5import sys
6from uiautomator import Device
7
8
9def set_wifi_state(dut, turn_on):
10 """Turn WiFi on or off.
11
12 This checks the current WiFi settings and turns it on or off. It does
13 nothing if the settings are already in the desired state.
14
15 Parameters:
16 dut (Device): The device object.
17 enabled: Boolean, true for on, false for off
18 Raises:
19 DeviceCommandError: If the UI automation fails.
20 """
21 # Open the Wi-Fi settings
22 adb(
23 "shell",
24 ("am start -a android.settings.WIFI_SETTINGS " "--activity-clear-task"),
25 serial=dut.serial,
26 )
27
28 # Check if there is an option to turn WiFi on or off
29 wifi_enabler = dut(
30 text="OFF", resourceId="com.android.settings:id/switch_widget"
31 )
32 wifi_disabler = dut(
33 text="ON", resourceId="com.android.settings:id/switch_widget"
34 )
35
36 if not wifi_enabler.exists and not wifi_disabler.exists:
37 raise DeviceCommandError(
38 dut,
39 "UI: set Wi-Fi state",
40 "Neither switch for turning Wi-Fi on nor for turning it off are present.",
41 )
42 if wifi_enabler.exists and wifi_disabler.exists:
43 raise DeviceCommandError(
44 dut,
45 "UI: set Wi-Fi state",
46 "Unexpected UI: Both, a switch for turning Wi-Fi on and for turning it off are present.",
47 )
48
49 if turn_on:
50 if wifi_enabler.exists:
51 wifi_enabler.click()
52 else:
53 print("Wi-Fi is already enabled.")
54 else:
55 if wifi_disabler.exists:
56 wifi_disabler.click()
57 else:
58 print("Wi-Fi is already disabled.")
59
60 # Leave the settings
61 dut.press.back()
62
63
64def main():
65 parser = argparse.ArgumentParser()
66 parser.add_argument(
67 "-a",
68 dest="ACTION",
69 required=True,
70 nargs="+",
71 help="Action to perform. Following action is currently implemented: \
72 set_wifi_state <on|off>",
73 )
74 parser.add_argument(
75 "-s",
76 dest="SERIALS",
77 nargs="+",
78 help="Serial numbers of devices to configure. \
79 If not present, all available devices will be configured.",
80 )
81 args = parser.parse_args()
82
83 if args.ACTION[0] != "set_wifi_state" or args.ACTION[1] not in (
84 "on",
85 "off",
86 ):
87 print(
88 "ERROR: Specified ACTION is not supported: {}".format(args.ACTION),
89 file=sys.stderr,
90 )
91 sys.exit(1)
92
93 serials = args.SERIALS if args.SERIALS is not None else list_devices()
94
95 for serial in serials:
96 print("Configuring device {}…".format(serial))
97
98 dut = Device(serial)
99 # Work around the not-so-easy Device class
100 dut.serial = serial
101
102 try:
103 unlock(dut)
104
105 set_wifi_state(dut, args.ACTION[1] == "on")
106
107 except DeviceCommandError as e:
108 print("ERROR {}".format(e), file=sys.stderr)
109
110
111if __name__ == "__main__":
112 main()