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 
23 from autopilot import introspection
24 
25 from unity8 import process_helpers
26 from unity8.shell import emulators
27 
28 
29 class LaunchDashApp(fixtures.Fixture):
30 
31  """Fixture to launch the Dash app."""
32 
33  def __init__(self, binary_path, variables):
34  """Initialize an instance.
35 
36  :param str binary_path: The path to the Dash app binary.
37  :param variables: The variables to use when launching the app.
38  :type variables: A dictionary.
39 
40  """
41  super().__init__()
42  self.binary_path = binary_path
43  self.variables = variables
44 
45  def setUp(self):
46  """Launch the dash app when the fixture is used."""
47  super().setUp()
48  self.addCleanup(self.stop_application)
50 
51  def launch_application(self):
52  binary_arg = 'BINARY={}'.format(self.binary_path)
53  testability_arg = 'QT_LOAD_TESTABILITY={}'.format(1)
54  env_args = [
55  '{}={}'.format(key, value) for key, value in self.variables.items()
56  ]
57  all_args = [binary_arg, testability_arg] + env_args
58 
59  pid = process_helpers.start_job('unity8-dash', *all_args)
60  return introspection.get_proxy_object_for_existing_process(
61  pid=pid,
62  emulator_base=emulators.UnityEmulatorBase,
63  )
64 
65  def stop_application(self):
66  process_helpers.stop_job('unity8-dash')
67 
68 
69 class DisplayRotationLock(fixtures.Fixture):
70 
71  def __init__(self, enable):
72  super().__init__()
73  self.enable = enable
74 
75  def setUp(self):
76  super().setUp()
77  original_state = self._is_rotation_lock_enabled()
78  if self.enable != original_state:
79  self.addCleanup(self._set_rotation_lock, original_state)
80  self._set_rotation_lock(self.enable)
81 
82  def _is_rotation_lock_enabled(self):
83  command = [
84  'gsettings', 'get',
85  'com.ubuntu.touch.system',
86  'rotation-lock'
87  ]
88  output = subprocess.check_output(command, universal_newlines=True)
89  return True if output.count('true') else False
90 
91  def _set_rotation_lock(self, value):
92  value_string = 'true' if value else 'false'
93  command = [
94  'gsettings', 'set',
95  'com.ubuntu.touch.system',
96  'rotation-lock', value_string
97  ]
98  subprocess.check_output(command)
def __init__(self, binary_path, variables)