~fginther/ubuntu-test-cases/mp-testing-unlock

« back to all changes in this revision

Viewing changes to utils/target/unlock_screen.py

  • Committer: Andy Doan
  • Date: 2014-03-18 22:13:08 UTC
  • mfrom: (201.2.3 touch)
  • Revision ID: andy.doan@canonical.com-20140318221308-69gs5ultuypij34u
fix unlock screen and autopilot gatekeeper bugs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
2
 
3
 
from autopilot import introspection
4
 
from autopilot.display import Display
5
 
from autopilot.input import Pointer, Touch
 
3
import logging
 
4
import sys
 
5
 
 
6
from unity8 import process_helpers as helpers
 
7
 
6
8
import dbus
7
 
import os
8
 
import sys
 
9
 
 
10
logging.basicConfig(level=logging.INFO)
 
11
 
 
12
 
 
13
def unlock_screen():
 
14
    _restart_with_testability()
 
15
    helpers.unlock_unity()
 
16
 
 
17
 
 
18
def _restart_with_testability():
 
19
    powerd = _get_powerd_interface()
 
20
    powerd_cookie = _take_powerd_screen_lock(powerd)
 
21
    helpers.restart_unity_with_testability()
 
22
    _release_powerd_screen_lock(powerd, powerd_cookie)
 
23
 
 
24
 
 
25
def _get_powerd_interface():
 
26
    bus = dbus.SystemBus()
 
27
    return bus.get_object('com.canonical.powerd', '/com/canonical/powerd')
 
28
 
 
29
 
 
30
def _take_powerd_screen_lock(powerd):
 
31
    logging.info('Taking screen lock (http://pad.lv/1236525).')
 
32
    return powerd.requestSysState(
 
33
        "autopilot-lock", 1, dbus_interface='com.canonical.powerd'
 
34
    )
 
35
 
 
36
 
 
37
def _release_powerd_screen_lock(powerd, powerd_cookie):
 
38
    logging.info('Releasing screen lock (http://pad.lv/1236525)')
 
39
    powerd.clearSysState(
 
40
        powerd_cookie, dbus_interface='com.canonical.powerd'
 
41
    )
9
42
 
10
43
 
11
44
def help():
12
45
    print "Usage:"
13
 
    print "Run the script without any argument to unlock with assertion"
14
 
    print ""
15
 
    print "option -q:"
16
 
    print "Execute with parameter -q to unlock the screen quickly without introspection"
17
 
 
18
 
 
19
 
def start_unity_in_testability():
20
 
    override_file = "~/.config/upstart/unity8.override"
21
 
 
22
 
    os.system('echo "exec unity8 -testability" > ~/.config/upstart/unity8.override')
23
 
    os.system('echo "kill timeout 30" >> ~/.config/upstart/unity8.override')    
24
 
 
25
 
    print "----------------------------------------------------------------------"
26
 
    print "Stopping Unity"
27
 
    os.system('/sbin/stop unity8')
28
 
    print "Unity stopped"
29
 
    os.system('rm -f /tmp/mir_socket')
30
 
 
31
 
    print "----------------------------------------------------------------------"
32
 
    print "Taking screen lock (#1235000)"
33
 
    bus = dbus.SystemBus()
34
 
    powerd = bus.get_object('com.canonical.powerd', '/com/canonical/powerd')
35
 
    powerd_cookie = powerd.requestSysState("autopilot-lock", 1, dbus_interface='com.canonical.powerd')
36
 
    print "----------------------------------------------------------------------"
37
 
    print "Starting Unity with testability"
38
 
    os.system('/sbin/start unity8')
39
 
    print "Unity started"
40
 
    print "----------------------------------------------------------------------"
41
 
    print "Releasing screen lock (#1235000)"
42
 
    powerd.clearSysState(powerd_cookie, dbus_interface='com.canonical.powerd')
43
 
 
44
 
    print "----------------------------------------------------------------------"
45
 
    if os.path.exists(override_file):
46
 
        os.remove(override_file)
47
 
    print "Cleaned up upstart override"
48
 
    print "----------------------------------------------------------------------"
49
 
 
50
 
    print "Turning on the screen"
51
 
    print ""
52
 
 
53
 
 
54
 
def unlock_screen():
55
 
    input_device = Touch.create()
56
 
    pointing_device = Pointer(input_device)
57
 
    conn = "com.canonical.Shell.BottomBarVisibilityCommunicator"
58
 
    unity8 = introspection.get_proxy_object_for_existing_process(connection_name=conn)
59
 
    greeter = unity8.select_single("Greeter")
60
 
    x, y, w, h = greeter.globalRect
61
 
    tx = x + w
62
 
    ty = y + (h / 2)
63
 
 
64
 
    pointing_device.drag(tx, ty, tx / 2, ty)
65
 
    try:
66
 
        greeter.shown.wait_for(False)
67
 
    except AssertionError:
68
 
        print "----------------------------------------------------------------------"
69
 
        print "THE SCREEN DIDN'T UNLOCK THE FIRST TRY, NOW ATTEMPTING A BLIND SWIPE"
70
 
        unlock_screen_blind(greeter)
71
 
 
72
 
 
73
 
def unlock_screen_blind(greeter=None):
74
 
    input_device = Touch.create()
75
 
    pointing_device = Pointer(input_device)
76
 
    x, y, w, h = Display.create(preferred_backend='UPA').get_screen_geometry(0)
77
 
    tx = x + w
78
 
    ty = y + (h / 2)
79
 
 
80
 
    pointing_device.drag(tx, ty, tx / 2, ty)
81
 
    if greeter is not None:
82
 
        try:
83
 
            greeter.shown.wait_for(False)
84
 
            if greeter.shown is False:
85
 
                print ""
86
 
                print "THE SCREEN IS NOW UNLOCKED"
87
 
        except AssertionError:
88
 
            print "----------------------------------------------------------------------"
89
 
            "THE SCREEN DIDN'T UNLOCK, RESTART THE DEVICE AND RUN THE SCRIPT AGAIN"
90
 
 
91
 
 
92
 
if len(sys.argv) >= 2 and sys.argv[1] == '-q':
93
 
    unlock_screen_blind()
94
 
elif len(sys.argv) >= 2 and sys.argv[1] == '-h':
 
46
    print "Run the script without any argument to unlock with assertion."
 
47
 
 
48
 
 
49
if len(sys.argv) >= 2 and sys.argv[1] == '-h':
95
50
    help()
96
51
else:
97
 
    start_unity_in_testability()
98
52
    unlock_screen()