27 if sys.version >=
'3':
28 from autopilot.exceptions
import ProcessSearchError
30 from autopilot.introspection
import ProcessSearchError
31 from autopilot.introspection
import get_proxy_object_for_existing_process
36 logger = logging.getLogger(__name__)
39 class CannotAccessUnity(Exception):
43 def unlock_unity(unity_proxy_obj=None):
44 """Helper function that attempts to unlock the unity greeter.
46 If unity_proxy_object is None create a proxy object by querying for the
47 running unity process.
48 Otherwise re-use the passed proxy object.
50 :raises RuntimeError: if the greeter attempts and fails to be unlocked.
52 :raises RuntimeWarning: when the greeter cannot be found because it is
54 :raises CannotAccessUnity: if unity is not introspectable or cannot be
56 :raises CannotAccessUnity: if unity's upstart status is not "start" or the
57 upstart job cannot be found at all.
60 if unity_proxy_obj
is None:
62 pid = _get_unity_pid()
63 unity = _get_unity_proxy_object(pid)
64 main_window = unity.select_single(main_window_emulator.QQuickView)
65 except ProcessSearchError
as e:
66 raise CannotAccessUnity(
67 "Cannot introspect unity, make sure that it has been started "
68 "with testability. Perhaps use the function "
69 "'restart_unity_with_testability' this module provides."
74 main_window = unity_proxy_obj.select_single(
75 main_window_emulator.QQuickView)
77 greeter = main_window.get_greeter()
78 if greeter.created
is False:
79 raise RuntimeWarning(
"Greeter appears to be already unlocked.")
89 except AssertionError:
93 logger.info(
"Failed to unlock greeter, trying again...")
95 logger.info(
"Greeter unlocked, continuing.")
99 def lock_unity(unity_proxy_obj=None):
100 """Helper function that attempts to lock the unity greeter."""
103 uinput = evdev.UInput(name=
'unity8-autopilot-power-button',
104 devnode=
'/dev/autopilot-uinput')
106 uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 1)
107 uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 0)
111 uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 1)
112 uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 0)
116 def restart_unity_with_testability(*args):
117 """Restarts (or starts) unity with testability enabled.
119 Passes *args arguments to the launched process.
122 args += (
"QT_LOAD_TESTABILITY=1",)
123 return restart_unity(*args)
126 def restart_unity(*args):
127 """Restarts (or starts) unity8 using the provided arguments.
129 Passes *args arguments to the launched process.
131 :raises subprocess.CalledProcessError: if unable to stop or start the
135 status = _get_unity_status()
136 if "start/" in status:
138 output = subprocess.check_output(
139 [
'/sbin/initctl',
'stop',
'unity8'])
141 except subprocess.CalledProcessError
as e:
143 "Failed to stop running instance of unity8: %s" % e.output,
148 command = [
'/sbin/initctl',
'start',
'unity8'] + list(args)
149 output = subprocess.check_output(
151 stderr=subprocess.STDOUT,
152 universal_newlines=
True,
155 pid = _get_unity_pid()
156 except subprocess.CalledProcessError
as e:
157 e.args += (
"Failed to start unity8: %s" % e.output,)
160 return _get_unity_proxy_object(pid)
163 def _get_unity_status():
165 return subprocess.check_output([
169 ], universal_newlines=
True)
170 except subprocess.CalledProcessError
as e:
171 raise CannotAccessUnity(
"Unable to get unity's status: %s" % str(e))
174 def _get_unity_pid():
175 status = _get_unity_status()
176 if "start/" not in status:
177 raise CannotAccessUnity(
"Unity is not in the running state.")
178 return int(status.split()[-1])
181 def _get_unity_proxy_object(pid):
182 return get_proxy_object_for_existing_process(
184 emulator_base=emulators.UnityEmulatorBase,