@@ -87,6 +87,55 @@ def unlock_unity(unity_proxy_obj=None):
8787 greeter.created.wait_for(False)
8888 logger.info("Greeter unlocked, continuing.")
8989
90+def lock_unity(unity_proxy_obj=None):
91+ """Helper function that attempts to lock unity greeter.
92+
93+ If unity_proxy_object is None create a proxy object by querying for the
94+ running unity process.
95+ Otherwise re-use the passed proxy object.
96+
97+ :raises RuntimeError: if the greeter attempts and fails to be locked.
98+
99+ :raises RuntimeWarning: when the greeter is found because it is
100+ already locked.
101+ :raises CannotAccessUnity: if unity is not introspectable or cannot be
102+ found on dbus.
103+ :raises CannotAccessUnity: if unity's upstart status is not "start" or the
104+ upstart job cannot be found at all.
105+
106+ """
107+ if unity_proxy_obj is None:
108+ try:
109+ pid = _get_unity_pid()
110+ unity = _get_unity_proxy_object(pid)
111+ main_window = unity.select_single(main_window_emulator.QQuickView)
112+ except ProcessSearchError as e:
113+ raise CannotAccessUnity(
114+ "Cannot introspect unity, make sure that it has been started "
115+ "with testability. Perhaps use the function "
116+ "'restart_unity_with_testability' this module provides."
117+ "(%s)"
118+ % str(e)
119+ )
120+ else:
121+ main_window = unity_proxy_obj.select_single(
122+ main_window_emulator.QQuickView)
123+
124+ greeter = main_window.get_greeter()
125+ if greeter.created is True:
126+ raise RuntimeWarning("Greeter appears to be already locked.")
127+
128+ bus = dbus.SessionBus()
129+ dbus_proxy = bus.get_object("com.canonical.UnityGreeter", "/")
130+ try:
131+ dbus_proxy.ShowGreeter()
132+ except dbus.DBusException:
133+ logger.info("Failed to lock greeter")
134+ raise
135+ else:
136+ greeter.created.wait_for(True)
137+ logger.info("Greeter locked, continuing.")
138+
90139def restart_unity_with_testability(*args):
91140 """Restarts (or starts) unity with testability enabled.
92141