~ubuntu-clock-dev/ubuntu-clock-app/reboot-packaging

« back to all changes in this revision

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

Setup autopilot infrastructure and added alarm test

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2014 Canonical Ltd
 
2
#
 
3
# This file is part of Ubuntu Clock App
 
4
#
 
5
# Ubuntu Clock App 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
# Ubuntu Clock App 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
"""clock-app autopilot tests."""
 
18
 
 
19
import os.path
 
20
import os
 
21
import shutil
 
22
import logging
 
23
import fixtures
 
24
 
 
25
from autopilot import input
 
26
from autopilot.platform import model
 
27
from ubuntuuitoolkit import (
 
28
    base,
 
29
    emulators as toolkit_emulators
 
30
)
 
31
 
 
32
from ubuntu_clock_app import emulators
 
33
 
 
34
logger = logging.getLogger(__name__)
 
35
 
 
36
 
 
37
class ClockAppTestCase(base.UbuntuUIToolkitAppTestCase):
 
38
 
 
39
    """A common test case class that provides several useful methods for
 
40
    clock-app tests.
 
41
 
 
42
    """
 
43
    local_location = "../../app/ubuntu-clock-app.qml"
 
44
    local_backend_dir = "../../builddir/backend/"
 
45
    installed_location = "/usr/share/ubuntu-clock-app/app/ubuntu-clock-app.qml"
 
46
    installed_backend_dir = "/usr/share/ubuntu-clock-app/builddir/backend/"
 
47
    sqlite_dir = os.path.expanduser(
 
48
        "~/.local/share/com.ubuntu.clock")
 
49
    backup_dir = sqlite_dir + ".backup"
 
50
 
 
51
    def setUp(self):
 
52
        super(ClockAppTestCase, self).setUp()
 
53
        self.pointing_device = input.Pointer(self.input_device_class.create())
 
54
 
 
55
        self.useFixture(fixtures.EnvironmentVariable('LC_ALL', newvalue='C'))
 
56
 
 
57
        # backup and wipe db's before testing
 
58
        self.temp_move_sqlite_db()
 
59
        self.addCleanup(self.restore_sqlite_db)
 
60
 
 
61
        # turn off the OSK so it doesn't block screen elements
 
62
        if model() != 'Desktop':
 
63
            os.system("stop maliit-server")
 
64
            self.addCleanup(os.system, "start maliit-server")
 
65
 
 
66
        if os.path.exists(self.local_location):
 
67
            self.launch_test_local()
 
68
        elif os.path.exists(self.installed_location):
 
69
            self.launch_test_installed()
 
70
        else:
 
71
            self.launch_test_click()
 
72
 
 
73
    def launch_test_local(self):
 
74
        self.app = self.launch_test_application(
 
75
            base.get_qmlscene_launch_command(),
 
76
            self.local_location,
 
77
            "-I", self.local_backend_dir,
 
78
            app_type='qt',
 
79
            emulator_base=toolkit_emulators.UbuntuUIToolkitEmulatorBase)
 
80
 
 
81
    def launch_test_installed(self):
 
82
        self.app = self.launch_test_application(
 
83
            base.get_qmlscene_launch_command(),
 
84
            self.installed_location,
 
85
            "-I", self.installed_backend_dir,
 
86
            app_type='qt',
 
87
            emulator_base=toolkit_emulators.UbuntuUIToolkitEmulatorBase)
 
88
 
 
89
    def launch_test_click(self):
 
90
        self.app = self.launch_click_package(
 
91
            "com.ubuntu.clock.devel",
 
92
            emulator_base=toolkit_emulators.UbuntuUIToolkitEmulatorBase)
 
93
 
 
94
    def temp_move_sqlite_db(self):
 
95
        try:
 
96
            shutil.rmtree(self.backup_dir)
 
97
        except:
 
98
            pass
 
99
        else:
 
100
            logger.warning("Prexisting backup database found and removed")
 
101
 
 
102
        try:
 
103
            shutil.move(self.sqlite_dir, self.backup_dir)
 
104
        except:
 
105
            logger.warning("No current database found")
 
106
        else:
 
107
            logger.debug("Backed up database")
 
108
 
 
109
    def restore_sqlite_db(self):
 
110
        if os.path.exists(self.backup_dir):
 
111
            if os.path.exists(self.sqlite_dir):
 
112
                try:
 
113
                    shutil.rmtree(self.sqlite_dir)
 
114
                except:
 
115
                    logger.error("Failed to remove test database and restore" /
 
116
                                 "database")
 
117
                    return
 
118
            try:
 
119
                shutil.move(self.backup_dir, self.sqlite_dir)
 
120
            except:
 
121
                logger.error("Failed to restore database")
 
122
 
 
123
    @property
 
124
    def main_view(self):
 
125
        return self.app.wait_select_single(emulators.MainView)