~verzegnassi-stefano/+junk/ubuntu-terminal-app-uitk13

« back to all changes in this revision

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

  • Committer: Filippo Scognamiglio
  • Date: 2014-10-25 04:42:31 UTC
  • Revision ID: flscogna@gmail.com-20141025044231-javjhusbqa171127
Initial reboot commit.

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 Canonical Ltd.
 
4
#
 
5
# This program is free software; you can redistribute it and/or modify
 
6
# it under the terms of the GNU Lesser General Public License as published by
 
7
# the Free Software Foundation; version 3.
 
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 Lesser General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU Lesser General Public License
 
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
"""Terminal app autopilot helpers."""
 
18
 
 
19
from time import sleep
 
20
import sqlite3
 
21
import os.path
 
22
import ubuntuuitoolkit
 
23
 
 
24
 
 
25
class TerminalApp(object):
 
26
 
 
27
    """Autopilot helper object for the terminal application."""
 
28
 
 
29
    def __init__(self, app_proxy):
 
30
        self.app = app_proxy
 
31
        self.main_view = self.app.select_single(MainView)
 
32
 
 
33
    @property
 
34
    def pointing_device(self):
 
35
        return self.app.pointing_device
 
36
 
 
37
 
 
38
class MainView(ubuntuuitoolkit.MainView):
 
39
 
 
40
    """Autopilot custom proxy object for the MainView."""
 
41
 
 
42
    def __init__(self, *args):
 
43
        super(MainView, self).__init__(*args)
 
44
        self.visible.wait_for(True)
 
45
 
 
46
    def get_slider_item(self, slider):
 
47
        return self.wait_select_single("Slider", objectName=slider)
 
48
 
 
49
    def get_control_panel(self):
 
50
        return self.wait_select_single("CtrlKeys", objectName="kbCtrl")
 
51
 
 
52
    def get_function_panel(self):
 
53
        return self.wait_select_single("FnKeys", objectName="kbFn")
 
54
 
 
55
    def get_text_panel(self):
 
56
        return self.wait_select_single("ScrlKeys", objectName="kbScrl")
 
57
 
 
58
    def get_terminal_page(self):
 
59
        return self.wait_select_single("Terminal", objectName="pgTerm")
 
60
 
 
61
    def get_circle_menu(self):
 
62
        return self.wait_select_single("CircleMenu", objectName="cmenu")
 
63
 
 
64
    def long_tap_terminal_center(self):
 
65
        x, y, w, h = self.globalRect
 
66
        tap_x = (x + w) / 2
 
67
        tap_y = (y + h) / 3
 
68
 
 
69
        # tap in the top third of the screen, to avoid OSK
 
70
        self.pointing_device.move(tap_x, tap_y)
 
71
        self.pointing_device.press()
 
72
        # we can hold the press for a long time without issue
 
73
        # so we'll ensure the app recieves our signal when under load
 
74
        sleep(4)
 
75
        self.pointing_device.release()
 
76
 
 
77
    def drag_horizontal_slider(self, slider, pos):
 
78
        """Drag slider until value is set"""
 
79
        slItem = self.get_slider_item(slider)
 
80
 
 
81
        slRect = slItem.select_single("SliderStyle")
 
82
 
 
83
        slideMin = int(slItem.minimumValue)
 
84
        slideMax = int(slItem.maximumValue)
 
85
 
 
86
        if pos > slideMax:
 
87
            raise ValueError("Pos cannot be greater than" + str(slideMax))
 
88
 
 
89
        if pos < slideMin:
 
90
            raise ValueError("Pos cannot be less than" + str(slideMin))
 
91
 
 
92
        x, y, w, h = slRect.globalRect
 
93
        # calculate the approximate slide step width
 
94
        # we take half of the theoretical step value just to make
 
95
        # sure we don't miss any values
 
96
        step = w / ((slideMax - slideMin) * 2)
 
97
        sx = x + step
 
98
        sy = y + h / 2
 
99
        loop = 1
 
100
 
 
101
        # set the slider to minimum value and loop
 
102
        self.pointing_device.move(sx, sy)
 
103
        self.pointing_device.click()
 
104
 
 
105
        # drag through the slider until the pos matches our desired value
 
106
        # in case of bad sliding, loop will also timeout
 
107
        while round(slItem.value) != pos and slItem.value < slideMax \
 
108
                and loop <= (slideMax * 2):
 
109
                valuePos = int(sx + step)
 
110
                self.pointing_device.drag(sx, sy, valuePos, sy)
 
111
                sx = valuePos
 
112
                sleep(1)
 
113
                loop = loop + 1
 
114
 
 
115
 
 
116
class DbMan(object):
 
117
 
 
118
    """
 
119
    Helper functions for dealing with sqlite databases
 
120
    """
 
121
 
 
122
    def get_db(self):
 
123
        dbs_path = os.path.expanduser(
 
124
            "~/.local/share/com.ubuntu.terminal/Databases")
 
125
        if not os.path.exists(dbs_path):
 
126
            return None
 
127
 
 
128
        files = [f for f in os.listdir(dbs_path)
 
129
                 if os.path.splitext(f)[1] == ".ini"]
 
130
        for f in files:
 
131
            ini_path = os.path.join(dbs_path, f)
 
132
            with open(ini_path) as ini:
 
133
                for line in ini:
 
134
                    if "=" in line:
 
135
                        key, val = line.strip().split("=")
 
136
                        if key == "Name" and val == "UbuntuTerminalDB":
 
137
                            try:
 
138
                                return ini_path.replace(".ini", ".sqlite")
 
139
                            except OSError:
 
140
                                pass
 
141
        return None
 
142
 
 
143
    def clean_db(self):
 
144
        path = self.get_db()
 
145
        if path is None:
 
146
            self.assertNotEquals(path, None)
 
147
        try:
 
148
            os.remove(path)
 
149
        except OSError:
 
150
            pass
 
151
 
 
152
    def get_font_size_from_storage(self):
 
153
        db = self.get_db()
 
154
        conn = sqlite3.connect(db)
 
155
 
 
156
        with conn:
 
157
            cur = conn.cursor()
 
158
            cur.execute("SELECT value FROM config WHERE item='fontSize'")
 
159
            row = cur.fetchone()
 
160
            if row is None:
 
161
                self.assertNotEquals(row, None)
 
162
            return int(row[0])
 
163
 
 
164
    def get_color_scheme_from_storage(self):
 
165
        db = self.get_db()
 
166
        conn = sqlite3.connect(db)
 
167
 
 
168
        with conn:
 
169
            cur = conn.cursor()
 
170
            cur.execute("SELECT value FROM config WHERE item='colorScheme'")
 
171
            row = cur.fetchone()
 
172
            if row is None:
 
173
                self.assertNotEquals(row, None)
 
174
            return row[0]