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 ==
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."""
102 uinput = evdev.UInput(name=
'unity8-autopilot-power-button',
103 devnode=
'/dev/autopilot-uinput')
105 uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 1)
106 uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 0)
110 uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 1)
111 uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 0)
115 def restart_unity_with_testability(*args):
116 """Restarts (or starts) unity with testability enabled.
118 Passes *args arguments to the launched process.
121 args += (
"QT_LOAD_TESTABILITY=1",)
122 return restart_unity(*args)
125 def restart_unity(*args):
126 """Restarts (or starts) unity8 using the provided arguments.
128 Passes *args arguments to the launched process.
130 :raises subprocess.CalledProcessError: if unable to stop or start the
134 status = _get_unity_status()
135 if "start/" in status:
137 output = subprocess.check_output(
138 [
'/sbin/initctl',
'stop',
'unity8'])
140 except subprocess.CalledProcessError
as e:
142 "Failed to stop running instance of unity8: %s" % e.output,
147 command = [
'/sbin/initctl',
'start',
'unity8'] + list(args)
148 output = subprocess.check_output(
150 stderr=subprocess.STDOUT,
151 universal_newlines=
True,
154 pid = _get_unity_pid()
155 except subprocess.CalledProcessError
as e:
156 e.args += (
"Failed to start unity8: %s" % e.output,)
159 return _get_unity_proxy_object(pid)
162 def _get_unity_status():
164 return subprocess.check_output([
168 ], universal_newlines=
True)
169 except subprocess.CalledProcessError
as e:
170 raise CannotAccessUnity(
"Unable to get unity's status: %s" % str(e))
173 def _get_unity_pid():
174 status = _get_unity_status()
175 if not "start/" in status:
176 raise CannotAccessUnity(
"Unity is not in the running state.")
177 return int(status.split()[-1])
180 def _get_unity_proxy_object(pid):
181 return get_proxy_object_for_existing_process(
183 emulator_base=emulators.UnityEmulatorBase,