This file is indexed.

/usr/lib/python3/dist-packages/autopilot/tests/functional/test_process_emulator.py is in python3-autopilot-tests 1.4+14.04.20140416-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#
# Autopilot Functional Test Tool
# Copyright (C) 2012-2014 Canonical
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import os
import sys
from subprocess import Popen, call
from textwrap import dedent
from threading import Thread
from time import sleep, time

from testtools import skipIf
from testtools.matchers import Equals, NotEquals, LessThan

from autopilot.exceptions import BackendException
from autopilot.matchers import Eventually
from autopilot.platform import model
from autopilot.process import ProcessManager
from autopilot.testcase import AutopilotTestCase
from autopilot.tests.functional.fixtures import ExecutableScript


@skipIf(model() != "Desktop", "Not suitable for device (ProcManager)")
class ProcessEmulatorTests(AutopilotTestCase):

    def ensure_gedit_not_running(self):
        """Close any open gedit applications."""
        apps = self.process_manager.get_running_applications_by_desktop_file(
            'gedit.desktop')
        if apps:
            # this is a bit brutal, but easier in this context than the
            # alternative.
            call(['killall', 'gedit'])

    def test_wait_for_app_running_works(self):
        """Make sure we can wait for an application to start."""
        def start_gedit():
            sleep(5)
            Popen(['gedit'])

        self.addCleanup(self.ensure_gedit_not_running)
        start = time()
        t = Thread(target=start_gedit())
        t.start()
        ret = self.process_manager.wait_until_application_is_running(
            'gedit.desktop', 10)
        end = time()
        t.join()

        self.assertThat(ret, Equals(True))
        self.assertThat(abs(end - start - 5.0), LessThan(1))

    def test_wait_for_app_running_times_out_correctly(self):
        """Make sure the bamf emulator times out correctly if no app is
        started."""
        self.ensure_gedit_not_running()

        start = time()
        ret = self.process_manager.wait_until_application_is_running(
            'gedit.desktop', 5)
        end = time()

        self.assertThat(abs(end - start - 5.0), LessThan(1))
        self.assertThat(ret, Equals(False))

    def test_start_app(self):
        """Ensure we can start an Application."""
        app = self.process_manager.start_app('Calculator')

        self.assertThat(app, NotEquals(None))
        # locale='C' does not work here as this goes through bamf, so we can't
        # assert the precise name
        self.assertThat(app.name, NotEquals(''))
        self.assertThat(app.desktop_file, Equals('gcalctool.desktop'))

    def test_start_app_window(self):
        """Ensure we can start an Application Window."""
        app = self.process_manager.start_app_window('Calculator', locale='C')

        self.assertThat(app, NotEquals(None))
        self.assertThat(app.name, Equals('Calculator'))

    @skipIf(model() != 'Desktop', "Bamf only available on desktop (Qt4)")
    def test_bamf_geometry_gives_reliable_results(self):
        script = dedent("""\
            #!%s
            from PyQt4.QtGui import QMainWindow, QApplication
            from sys import argv

            app = QApplication(argv)
            win = QMainWindow()
            win.show()
            app.exec_()
            """ % sys.executable)
        path = self.useFixture(ExecutableScript(script)).path
        app_proxy = self.launch_test_application(path, app_type='qt')
        proxy_window = app_proxy.select_single('QMainWindow')
        pm = ProcessManager.create()
        window = [
            w for w in pm.get_open_windows()
            if w.name == os.path.basename(path)
        ][0]
        self.assertThat(list(window.geometry), Equals(proxy_window.geometry))


class ProcessManagerApplicationNoCleanupTests(AutopilotTestCase):
    """Testing the process manager without the automated cleanup that running
    within as an AutopilotTestCase provides.

    """

    def test_can_close_all_app(self):
        """Ensure that closing an app actually closes all app instances."""
        try:
            process_manager = ProcessManager.create(preferred_backend="BAMF")
        except BackendException as e:
            self.skip("Test is only for BAMF backend ({}).".format(str(e)))

        process_manager.start_app('Calculator')

        process_manager.close_all_app('Calculator')

        # ps/pgrep lists gnome-calculator as gnome-calculato (see lp
        # bug:1174911)
        ret_code = call(["pgrep", "-c", "gnome-calculato"])
        self.assertThat(ret_code, Equals(1), "Application is still running")


class BAMFResizeWindowTestCase(AutopilotTestCase):
    """Tests for the BAMF window helpers."""

    scenarios = [
        ('increase size', dict(delta_width=40, delta_height=40)),
        ('decrease size', dict(delta_width=-40, delta_height=-40))
    ]

    def start_mock_window(self):
        self.launch_test_application(
            'window-mocker',
            app_type='qt'
        )

        try:
            process_manager = ProcessManager.create(preferred_backend='BAMF')
        except BackendException as e:
            self.skip('Test is only for BAMF backend ({}).'.format(str(e)))

        window = [
            w for w in process_manager.get_open_windows()
            if w.name == 'Default Window Title'
        ][0]

        return window

    def test_resize_window_must_update_width_and_height_geometry(self):
        window = self.start_mock_window()

        def get_size():
            _, _, width, height = window.geometry
            return width, height

        initial_width, initial_height = get_size()

        expected_width = initial_width + self.delta_width
        expected_height = initial_height + self.delta_height
        window.resize(width=expected_width, height=expected_height)

        self.assertThat(
            get_size, Eventually(Equals((expected_width, expected_height))))