~vthompson/ubuntu-weather-app/reboot-manage-current-location

« back to all changes in this revision

Viewing changes to tests/autopilot/ubuntu_weather_app/tests/__init__.py

  • Committer: Victor Thompson
  • Date: 2015-06-05 23:10:37 UTC
  • mfrom: (45.1.1 reboot)
  • Revision ID: victor.thompson@gmail.com-20150605231037-s8o7xm2j053nevv7
Merge of trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
#
 
3
# Copyright (C) 2013, 2014, 2015 Canonical Ltd
 
4
#
 
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.
 
8
#
 
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.
 
13
#
 
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/>.
 
16
 
 
17
"""Weather app autopilot tests."""
 
18
 
 
19
import os
 
20
import os.path
 
21
import shutil
 
22
import logging
 
23
 
 
24
import fixtures
 
25
from ubuntu_weather_app import UbuntuWeatherApp
 
26
 
 
27
from autopilot import logging as autopilot_logging
 
28
from autopilot.testcase import AutopilotTestCase
 
29
 
 
30
import ubuntuuitoolkit
 
31
from ubuntuuitoolkit import base
 
32
 
 
33
logger = logging.getLogger(__name__)
 
34
 
 
35
 
 
36
class BaseTestCaseWithPatchedHome(AutopilotTestCase):
 
37
 
 
38
    """A common test case class that provides several useful methods for
 
39
    ubuntu-weather-app tests.
 
40
 
 
41
    """
 
42
 
 
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"
 
48
 
 
49
    def get_launcher_method_and_type(self):
 
50
        if os.path.exists(self.local_location):
 
51
            launch = self.launch_test_local
 
52
            test_type = 'local'
 
53
        elif os.path.exists(self.installed_location):
 
54
            launch = self.launch_test_installed
 
55
            test_type = 'deb'
 
56
        else:
 
57
            launch = self.launch_test_click
 
58
            test_type = 'click'
 
59
        return launch, test_type
 
60
 
 
61
    def setUp(self):
 
62
        super(BaseTestCaseWithPatchedHome, self).setUp()
 
63
        self.launcher, self.test_type = self.get_launcher_method_and_type()
 
64
        self.home_dir = self._patch_home()
 
65
 
 
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(),
 
70
            self.local_location,
 
71
            "debug",
 
72
            app_type='qt',
 
73
            emulator_base=ubuntuuitoolkit.UbuntuUIToolkitCustomProxyObjectBase)
 
74
 
 
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,
 
80
            "debug",
 
81
            app_type='qt',
 
82
            emulator_base=ubuntuuitoolkit.UbuntuUIToolkitCustomProxyObjectBase)
 
83
 
 
84
    @autopilot_logging.log_action(logger.info)
 
85
    def launch_test_click(self):
 
86
        return self.launch_click_package(
 
87
            "com.ubuntu.weather",
 
88
            emulator_base=ubuntuuitoolkit.UbuntuUIToolkitCustomProxyObjectBase)
 
89
 
 
90
    def _copy_xauthority_file(self, directory):
 
91
        """ Copy .Xauthority file to directory, if it exists in /home
 
92
        """
 
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
 
97
 
 
98
        xauth = os.path.expanduser(os.path.join(os.environ.get('HOME'),
 
99
                                   '.Xauthority'))
 
100
        if os.path.isfile(xauth):
 
101
            logger.debug("Copying .Xauthority to %s" % directory)
 
102
            shutil.copyfile(
 
103
                os.path.expanduser(os.path.join(os.environ.get('HOME'),
 
104
                                   '.Xauthority')),
 
105
                os.path.join(directory, '.Xauthority'))
 
106
 
 
107
    def _patch_home(self):
 
108
        """ mock /home for testing purposes to preserve user data
 
109
        """
 
110
 
 
111
        # if running on non-phablet device,
 
112
        # run in temp folder to avoid mucking up home
 
113
        # bug 1316746
 
114
        # bug 1376423
 
115
        if self.test_type is 'click':
 
116
            # just use home for now on devices
 
117
            temp_dir = os.environ.get('HOME')
 
118
 
 
119
            # before each test, remove the app's databases
 
120
            local_dir = os.path.join(temp_dir,
 
121
                                     '.local/share/com.ubuntu.weather')
 
122
 
 
123
            if (os.path.exists(local_dir)):
 
124
                shutil.rmtree(local_dir)
 
125
 
 
126
            local_dir = os.path.join(temp_dir, '.config/com.ubuntu.weather')
 
127
 
 
128
            if (os.path.exists(local_dir)):
 
129
                shutil.rmtree(local_dir)
 
130
        else:
 
131
            temp_dir_fixture = fixtures.TempDir()
 
132
            self.useFixture(temp_dir_fixture)
 
133
            temp_dir = temp_dir_fixture.path
 
134
 
 
135
            # before we set fixture, copy xauthority if needed
 
136
            self._copy_xauthority_file(temp_dir)
 
137
            self.useFixture(fixtures.EnvironmentVariable('HOME',
 
138
                                                         newvalue=temp_dir))
 
139
 
 
140
            logger.debug("Patched home to fake home directory %s" % temp_dir)
 
141
        return temp_dir
 
142
 
 
143
 
 
144
class UbuntuWeatherAppTestCase(BaseTestCaseWithPatchedHome):
 
145
 
 
146
    """Base test case that launches the ubuntu-weather-app."""
 
147
 
 
148
    def setUp(self):
 
149
        super(UbuntuWeatherAppTestCase, self).setUp()
 
150
        self.app = UbuntuWeatherApp(self.launcher())