Unity 8
fixture_setup.py
1 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2 #
3 # Unity Autopilot Test Suite
4 # Copyright (C) 2014, 2015 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 import fixtures
21 import subprocess
22 import logging
23 
24 from autopilot import introspection
25 
26 from unity8 import (
27  get_binary_path,
28  process_helpers
29 )
30 from unity8.shell import emulators
31 
32 logger = logging.getLogger(__name__)
33 
34 class LaunchDashApp(fixtures.Fixture):
35 
36  """Fixture to launch the Dash app."""
37 
38  def __init__(self, binary_path, variables):
39  """Initialize an instance.
40 
41  :param str binary_path: The path to the Dash app binary.
42  :param variables: The variables to use when launching the app.
43  :type variables: A dictionary.
44 
45  """
46  super().__init__()
47  self.binary_path = binary_path
48  self.variables = variables
49 
50  def setUp(self):
51  """Launch the dash app when the fixture is used."""
52  super().setUp()
53  self.addCleanup(self.stop_application)
55 
56  def launch_application(self):
57  binary_arg = 'BINARY={}'.format(self.binary_path)
58  testability_arg = 'QT_LOAD_TESTABILITY={}'.format(1)
59  env_args = [
60  '{}={}'.format(key, value) for key, value in self.variables.items()
61  ]
62  all_args = [binary_arg, testability_arg] + env_args
63 
64  pid = process_helpers.start_job('unity8-dash', *all_args)
65  return introspection.get_proxy_object_for_existing_process(
66  pid=pid,
67  emulator_base=emulators.UnityEmulatorBase,
68  )
69 
70  def stop_application(self):
71  process_helpers.stop_job('unity8-dash')
72 
73 
74 class DisplayRotationLock(fixtures.Fixture):
75 
76  def __init__(self, enable):
77  super().__init__()
78  self.enable = enable
79 
80  def setUp(self):
81  super().setUp()
82  original_state = self._is_rotation_lock_enabled()
83  if self.enable != original_state:
84  self.addCleanup(self._set_rotation_lock, original_state)
85  self._set_rotation_lock(self.enable)
86 
87  def _is_rotation_lock_enabled(self):
88  command = [
89  'gsettings', 'get',
90  'com.ubuntu.touch.system',
91  'rotation-lock'
92  ]
93  output = subprocess.check_output(command, universal_newlines=True)
94  return True if output.count('true') else False
95 
96  def _set_rotation_lock(self, value):
97  value_string = 'true' if value else 'false'
98  command = [
99  'gsettings', 'set',
100  'com.ubuntu.touch.system',
101  'rotation-lock', value_string
102  ]
103  subprocess.check_output(command)
104 
105 class LaunchMockIndicatorService(fixtures.Fixture):
106 
107  """Fixture to launch the indicator test service."""
108 
109  def __init__(self, action_delay, ensure_not_running=True):
110  """Initialize an instance.
111 
112  :param action_delay: The delay to use when activating actions.
113  Measured in milliseconds. Value of -1 will result in infinite delay.
114  :type action_delay: An integer.
115  :param boolean ensure_not_running: Make sure service is not running
116 
117  """
118  super(LaunchMockIndicatorService, self).__init__()
119  self.action_delay = action_delay
120  self.ensure_not_running = ensure_not_running
121 
122  def setUp(self):
123  super(LaunchMockIndicatorService, self).setUp()
124  if self.ensure_not_running:
126  self.addCleanup(self.stop_service)
127  self.application_proxy = self.launch_service()
128 
129  def launch_service(self):
130  logger.info("Starting unity-mock-indicator-service")
131  binary_path = get_binary_path('unity-mock-indicator-service')
132  binary_arg = 'BINARY={}'.format(binary_path)
133  env_args = 'ARGS=-t {}'.format(self.action_delay)
134  all_args = [binary_arg, env_args]
135  process_helpers.start_job('unity-mock-indicator-service', *all_args)
136 
137  def stop_service(self):
138  logger.info("Stopping unity-mock-indicator-service")
139  process_helpers.stop_job('unity-mock-indicator-service')
140 
141  def ensure_service_not_running(self):
142  if process_helpers.is_job_running('unity-mock-indicator-service'):
143  self.stop_service()
def __init__(self, binary_path, variables)