Unity 8
 All Classes Functions Properties
test_upstart.py
1 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2 #
3 # Unity Autopilot Test Suite
4 # Copyright (C) 2013, 2014 Canonical
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19 
20 """Tests for upstart integration."""
21 
22 import os
23 import signal
24 import subprocess
25 import time
26 
27 from testtools.matchers._basic import Equals
28 from autopilot.matchers import Eventually
29 from autopilot.introspection import get_proxy_object_for_existing_process
30 
31 from unity8 import get_binary_path
32 from unity8.shell.emulators import UnityEmulatorBase
33 from unity8.shell.tests import UnityTestCase, _get_device_emulation_scenarios
34 
35 import logging
36 
37 logger = logging.getLogger(__name__)
38 
39 # from __future__ import range
40 # (python3's range, is same as python2's xrange)
41 import sys
42 if sys.version_info < (3,):
43  range = xrange
44 
45 
46 class UpstartIntegrationTests(UnityTestCase):
47 
48  scenarios = _get_device_emulation_scenarios()
49 
50  def _get_status(self):
51  pid, status = os.waitpid(
52  self.process.pid, os.WUNTRACED | os.WCONTINUED | os.WNOHANG)
53  logger.debug(
54  "Got status: {0}; stopped: {1}; continued: {2}".format(
55  status, os.WIFSTOPPED(status), os.WIFCONTINUED(status)))
56  return status
57 
58  def _launch_unity(self):
59  self.patch_environment("QT_LOAD_TESTABILITY", "1")
60  self.process = subprocess.Popen(
61  [get_binary_path()] + self.unity_geometry_args)
62 
63  def ensure_stopped():
64  self.process.terminate()
65  for i in range(10):
66  try:
67  self._get_status()
68  except OSError:
69  break
70  else:
71  time.sleep(1)
72  try:
73  self._get_status()
74  except OSError:
75  pass
76  else:
77  self.process.kill()
78 
79  self.process.wait()
80 
81  self.addCleanup(ensure_stopped)
82 
83  def _set_proxy(self):
84  super(UpstartIntegrationTests, self)._set_proxy(
85  get_proxy_object_for_existing_process(
86  pid=self.process.pid,
87  emulator_base=UnityEmulatorBase,))
88 
89  def test_no_sigstop(self):
90  self.patch_environment("UNITY_MIR_EMITS_SIGSTOP", "")
91  self._launch_unity()
92  self._set_proxy()
93 
94  logger.debug("Unity started, waiting for it to be ready.")
95  self.assertUnityReady()
96  logger.debug("Unity loaded and ready.")
97 
98  def test_expect_sigstop(self):
99  self.patch_environment("UNITY_MIR_EMITS_SIGSTOP", "1")
100  self._launch_unity()
101  self.assertThat(
102  lambda: os.WIFSTOPPED(self._get_status()),
103  Eventually(Equals(True)), "Unity8 should raise SIGSTOP when ready")
104 
105  self.process.send_signal(signal.SIGCONT)
106  self.assertThat(
107  lambda: os.WIFCONTINUED(self._get_status()),
108  Eventually(Equals(True)), "Unity8 should have resumed")
109 
110  logger.debug("Unity started, waiting for it to be ready.")
111  self._set_proxy()
112  self.assertUnityReady()
113  logger.debug("Unity loaded and ready.")