~allanlesage/autopilot/unity-panel-service-mixin

« back to all changes in this revision

Viewing changes to autopilot/introspection/unity_panel_service.py

  • Committer: Allan LeSage
  • Date: 2013-02-08 23:08:25 UTC
  • Revision ID: allan.lesage@canonical.com-20130208230825-d0uebfnhf5ajn85e
Adds unity-panel-service mixin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
# Copyright 2013 Canonical
 
3
# Author: Allan LeSage
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify it
 
6
# under the terms of the GNU General Public License version 3, as published
 
7
# by the Free Software Foundation.
 
8
 
 
9
import psutil
 
10
import dbus
 
11
from autopilot.introspection import ApplicationIntrospectionTestMixin, get_autopilot_proxy_object_for_process
 
12
from autopilot.emulators.dbus_handler import get_session_bus
 
13
 
 
14
 
 
15
 
 
16
def find_process(process_name):
 
17
    """Returns a psutil process similar to a Popen process."""
 
18
 
 
19
    def process_name_equals(process):
 
20
        if process.name == process_name:
 
21
            return True
 
22
        else:
 
23
            return False
 
24
    processes = filter(process_name_equals, psutil.get_process_list())
 
25
    try:
 
26
        return processes[0]
 
27
    except IndexError:
 
28
        return None
 
29
 
 
30
 
 
31
class UnityPanelServiceTestMixin(ApplicationIntrospectionTestMixin):
 
32
 
 
33
    def _update_activation_environment_with_autopilot(self):
 
34
        """Add 'autopilot' to GTK_MODULES via Unity dbus-activation.
 
35
 
 
36
        When unity-panel-service automatically re-launches, it'll come back with
 
37
        'autopilot' in its GTK_MODULES.
 
38
        """
 
39
 
 
40
        session_bus = get_session_bus()
 
41
        proxy = session_bus.get_object('org.freedesktop.DBus',
 
42
                                       "/org/freedesktop/DBus")
 
43
        proxy.UpdateActivationEnvironment({'GTK_MODULES':
 
44
                                               "autopilot"})
 
45
 
 
46
 
 
47
    def _kill_unity_panel_service(self):
 
48
        """Kill the process named 'unity-panel-service'."""
 
49
 
 
50
        unity_panel_service = find_process('unity-panel-service')
 
51
        if unity_panel_service==None:
 
52
            return
 
53
        unity_panel_service.kill()
 
54
 
 
55
 
 
56
    def prepare_environment(self):
 
57
        """Set up 'unity-panel-service' as our app."""
 
58
 
 
59
        # add our GTK_MODULES to Unity's DBus launching env
 
60
        self._update_activation_environment_with_autopilot()
 
61
        # kill the indicators; they'll come back with 'autopilot' appended to GTK_MODULES
 
62
        self._kill_unity_panel_service()
 
63
        # wait until the unity-panel-service comes back
 
64
        unity_panel_service_process = None
 
65
        while unity_panel_service_process == None:
 
66
            unity_panel_service_process = find_process('unity-panel-service')
 
67
        self.app = get_autopilot_proxy_object_for_process(
 
68
            unity_panel_service_process)