1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
3
# Copyright (C) 2013, 2014, 2015 Canonical Ltd
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License version 3 as
7
# published by the Free Software Foundation.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
"""Weather app autopilot tests."""
25
from ubuntu_weather_app import UbuntuWeatherApp
27
from autopilot import logging as autopilot_logging
28
from autopilot.testcase import AutopilotTestCase
30
import ubuntuuitoolkit
31
from ubuntuuitoolkit import base
33
logger = logging.getLogger(__name__)
36
class BaseTestCaseWithPatchedHome(AutopilotTestCase):
38
"""A common test case class that provides several useful methods for
39
ubuntu-weather-app tests.
43
working_dir = os.getcwd()
44
local_location_dir = os.path.dirname(os.path.dirname(working_dir))
45
local_location = local_location_dir + "/app/ubuntu-weather-app.qml"
46
installed_location = "/usr/share/ubuntu-weather-app/app/" \
47
"ubuntu-weather-app.qml"
49
def get_launcher_method_and_type(self):
50
if os.path.exists(self.local_location):
51
launch = self.launch_test_local
53
elif os.path.exists(self.installed_location):
54
launch = self.launch_test_installed
57
launch = self.launch_test_click
59
return launch, test_type
62
super(BaseTestCaseWithPatchedHome, self).setUp()
63
self.launcher, self.test_type = self.get_launcher_method_and_type()
64
self.home_dir = self._patch_home()
66
@autopilot_logging.log_action(logger.info)
67
def launch_test_local(self):
68
return self.launch_test_application(
69
base.get_qmlscene_launch_command(),
73
emulator_base=ubuntuuitoolkit.UbuntuUIToolkitCustomProxyObjectBase)
75
@autopilot_logging.log_action(logger.info)
76
def launch_test_installed(self):
77
return self.launch_test_application(
78
base.get_qmlscene_launch_command(),
79
self.installed_location,
82
emulator_base=ubuntuuitoolkit.UbuntuUIToolkitCustomProxyObjectBase)
84
@autopilot_logging.log_action(logger.info)
85
def launch_test_click(self):
86
return self.launch_click_package(
88
emulator_base=ubuntuuitoolkit.UbuntuUIToolkitCustomProxyObjectBase)
90
def _copy_xauthority_file(self, directory):
91
""" Copy .Xauthority file to directory, if it exists in /home
93
# If running under xvfb, as jenkins does,
94
# xsession will fail to start without xauthority file
95
# Thus if the Xauthority file is in the home directory
96
# make sure we copy it to our temp home directory
98
xauth = os.path.expanduser(os.path.join(os.environ.get('HOME'),
100
if os.path.isfile(xauth):
101
logger.debug("Copying .Xauthority to %s" % directory)
103
os.path.expanduser(os.path.join(os.environ.get('HOME'),
105
os.path.join(directory, '.Xauthority'))
107
def _patch_home(self):
108
""" mock /home for testing purposes to preserve user data
111
# if running on non-phablet device,
112
# run in temp folder to avoid mucking up home
115
if self.test_type is 'click':
116
# just use home for now on devices
117
temp_dir = os.environ.get('HOME')
119
# before each test, remove the app's databases
120
local_dir = os.path.join(temp_dir,
121
'.local/share/com.ubuntu.weather')
123
if (os.path.exists(local_dir)):
124
shutil.rmtree(local_dir)
126
local_dir = os.path.join(temp_dir, '.config/com.ubuntu.weather')
128
if (os.path.exists(local_dir)):
129
shutil.rmtree(local_dir)
131
temp_dir_fixture = fixtures.TempDir()
132
self.useFixture(temp_dir_fixture)
133
temp_dir = temp_dir_fixture.path
135
# before we set fixture, copy xauthority if needed
136
self._copy_xauthority_file(temp_dir)
137
self.useFixture(fixtures.EnvironmentVariable('HOME',
140
logger.debug("Patched home to fake home directory %s" % temp_dir)
144
class UbuntuWeatherAppTestCase(BaseTestCaseWithPatchedHome):
146
"""Base test case that launches the ubuntu-weather-app."""
149
super(UbuntuWeatherAppTestCase, self).setUp()
150
self.app = UbuntuWeatherApp(self.launcher())