~ubuntu-branches/ubuntu/vivid/mago/vivid

« back to all changes in this revision

Viewing changes to mago/application/gcalc.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2011-02-08 13:32:13 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110208133213-m1og7ey0m990chg6
Tags: 0.3+bzr20-0ubuntu1
* debian/rules:
  - updated to debhelper 7
  - use dh_python2 instead of python-central
* debian/pycompat:
  - removed, no longer needed
* debian/control:
  - dropped cdbs and python-central dependencies
* bzr snapshot of the current trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#-*- coding:utf-8 -*-
2
 
"""
3
 
This is the "gcalc" module.
4
 
 
5
 
This module provides the LDTP wrapper to make gcalc testing easier.
6
 
"""
7
 
import ooldtp
8
 
import ldtp
9
 
from .main import Application
10
 
from ..gconfwrapper import GConf
11
 
import time
12
 
 
13
 
class Calculator(Application):
14
 
    """
15
 
    The Calculator class contains methods for exercising the gcalctool application
16
 
    """
17
 
    LAUNCHER            = "gcalctool"
18
 
    WINDOW              = "frmCalculator*"
19
 
    WINDOW_BASIC        = "frmCalculator"
20
 
    WINDOW_ADVANCED     = "frmCalculator"
21
 
    WINDOW_FINANCIAL    = "frmCalculator"
22
 
    WINDOW_SCIENTIFIC   = "frmCalculator"
23
 
    WINDOW_PROGRAMMING  = "frmCalculator"
24
 
    DLG_PREFERENCES     = "dlgPreferences"
25
 
    MNU_BASIC           = "mnuBasic"
26
 
    MNU_ADVANCED        = "mnuAdvanced"
27
 
    MNU_FINANCIAL       = "mnuFinancial"
28
 
    MNU_SCIENTIFIC      = "mnuScientific"
29
 
    MNU_PROGRAMMING     = "mnuProgramming"
30
 
    MNU_PREFERENCES     = "mnuPreferences"
31
 
    DLG_PREFERENCES     = "dlgPreferences"
32
 
    CHK_THOUSANDS       = "chkShowthousandsseparators"
33
 
    BTN_CLOSE           = "btnClose"
34
 
 
35
 
    VIEWS = {"BASIC"         : {"WINDOW" : WINDOW_BASIC,
36
 
                                "MENU" : MNU_BASIC},
37
 
             "ADVANCED"      : {"WINDOW" : WINDOW_ADVANCED,
38
 
                                "MENU" : MNU_ADVANCED},
39
 
             "FINANCIAL"     : {"WINDOW" : WINDOW_FINANCIAL,
40
 
                                "MENU" : MNU_FINANCIAL},
41
 
             "SCIENTIFIC"    : {"WINDOW" : WINDOW_SCIENTIFIC,
42
 
                                "MENU" : MNU_SCIENTIFIC},
43
 
             "PROGRAMMING"   : {"WINDOW" : WINDOW_PROGRAMMING,
44
 
                                "MENU" : MNU_PROGRAMMING}}
45
 
    BUTTONS = {"0" : "btn0",
46
 
               "1" : "btn1",
47
 
               "2" : "btn2",
48
 
               "3" : "btn3",
49
 
               "4" : "btn4",
50
 
               "5" : "btn5",
51
 
               "6" : "btn6",
52
 
               "7" : "btn7",
53
 
               "8" : "btn8",
54
 
               "9" : "btn9",
55
 
               "=" : "btnresult",
56
 
               "+" : "btnadd",
57
 
               "-" : "btnsubtract",
58
 
               "*" : "btnmultiply",
59
 
               "/" : "btndivide",
60
 
               "C" : "btnclear",
61
 
               "." : "btnnumeric_point",
62
 
               "s" : "btnsquare_root",
63
 
               "m" : "btnmodulusdivide"}
64
 
    EDITBAR_ROLE        = "edit bar"
65
 
    EDITBAR_INDEX       = 0
66
 
    SLEEP_DELAY         = 2
67
 
    GCONF_MODE_KEY      = "/apps/gcalctool/button_layout"
68
 
    GCONF_MODE_VAL      = "BASIC"
69
 
    
70
 
    def __init__(self):
71
 
        Application.__init__(self)
72
 
    
73
 
    def open(self):
74
 
        """
75
 
        Opens the application
76
 
        """
77
 
        try:
78
 
            GConf.set_item(self.GCONF_MODE_KEY, self.GCONF_MODE_VAL)
79
 
        except GConf.GConfError:
80
 
            raise ldtp.LdtpExecutionError, "Could not set default view in gconf"
81
 
        Application.open(self)
82
 
        
83
 
    def showing(self, button):
84
 
        """
85
 
        Test whether the specified button is showing
86
 
        
87
 
        @param button: Specify the button to test
88
 
        @type button: string
89
 
        @return: Value indicating whether the button is showing in the view
90
 
        @rtype: boolean
91
 
        
92
 
        >>> c = Calculator()
93
 
        >>> c.open() # Calculator opens in BASIC view
94
 
        >>> c.showing("Reciprocal")
95
 
        False
96
 
        >>> c.set_view("ADVANCED")
97
 
        >>> c.showing("Reciprocal")
98
 
        True
99
 
        """
100
 
        calculator = ooldtp.context(self.name)
101
 
 
102
 
        children = calculator.getchild(button,'push button')
103
 
        for child in children:
104
 
            childName=child.getName()
105
 
            if calculator.hasstate(childName, ldtp.state.SHOWING):
106
 
                return True
107
 
        
108
 
        return False
109
 
    
110
 
    def push(self, buttons):
111
 
        """
112
 
        Push the specified buttons in the calculator window.
113
 
        
114
 
        >>> c = Calculator()
115
 
        >>> c.open()
116
 
        >>> c.push('2+2=')
117
 
        
118
 
        @param buttons: Specify the buttons to push.
119
 
        @type buttons: string
120
 
        """
121
 
        calculator = ooldtp.context(self.name)
122
 
        for button in buttons:
123
 
            children = calculator.getchild(self.BUTTONS[button],'push button')
124
 
            for child in children:
125
 
                child.click()
126
 
    
127
 
    def get_view(self):
128
 
        """
129
 
        Get the current view of the calculator.
130
 
        
131
 
        >>> c = Calculator()
132
 
        >>> c.open()
133
 
        >>> c.get_view()
134
 
        'BASIC'
135
 
        
136
 
        @return: The current view: BASIC, ADVANCED, FINANCIAL, SCIENTIFIC,
137
 
                 or PROGRAMMING
138
 
        @rtype: string
139
 
        """
140
 
        windowname = self.name
141
 
        for view in self.VIEWS:
142
 
            if self.VIEWS[view]['WINDOW'] == windowname:
143
 
                return view
144
 
        raise LookupError, "Could not identify the current calculator view"
145
 
 
146
 
    def set_view(self, new_view):
147
 
        """
148
 
        Change to the specified view in the calculator
149
 
        
150
 
        >>> c = Calculator()
151
 
        >>> c.open()
152
 
        >>> c.set_view('ADVANCED')
153
 
        
154
 
        @param new_view: Specify the new view. Valid values are:
155
 
                         BASIC, ADVANCED, FINANCIAL, SCIENTIFIC, PROGRAMMING
156
 
        @type new_view: string
157
 
        """
158
 
        new_window_name = self.VIEWS[new_view]["WINDOW"]
159
 
        new_view_menu_name = self.VIEWS[new_view]["MENU"]
160
 
        calculator = ooldtp.context(self.name)
161
 
        mnu_new_view = calculator.getchild(new_view_menu_name)
162
 
 
163
 
        mnu_new_view.selectmenuitem() 
164
 
        
165
 
        self.set_name(new_window_name)
166
 
        calculator.remap()
167
 
        time.sleep(self.SLEEP_DELAY) # need to give mago some time to catch up to calc ui
168
 
    
169
 
    def get_value(self):
170
 
        """
171
 
        Read the value from the calculator screen
172
 
        
173
 
        >>> c = Calculator()
174
 
        >>> c.open()
175
 
        >>> c.push('2+2=')
176
 
        >>> c.get_value()
177
 
        u'4'
178
 
        
179
 
        @return: The contents of the calculator's screen
180
 
        @rtype: string
181
 
        """
182
 
        calculator = ooldtp.context(self.name)
183
 
        editBar = calculator.getchild(role=self.EDITBAR_ROLE)
184
 
        value = editBar[0].gettextvalue()
185
 
        return value
186
 
 
187
 
    def set_thousands_separator(self, option = True):
188
 
        """
189
 
        Sets the thousands separator to true or false
190
 
 
191
 
        @param option: set the thousands separator to true or false
192
 
                       default: true
193
 
        @type: boolean
194
 
        """
195
 
        calculator = ooldtp.context(self.name)
196
 
        
197
 
        preferences_mnu = calculator.getchild(self.MNU_PREFERENCES)
198
 
        preferences_mnu.selectmenuitem()
199
 
 
200
 
        ldtp.waittillguiexist(self.DLG_PREFERENCES)
201
 
 
202
 
        preferences_dlg = ooldtp.context(self.DLG_PREFERENCES)
203
 
 
204
 
        chk_thousands = preferences_dlg.getchild(self.CHK_THOUSANDS)
205
 
 
206
 
        if option:
207
 
            chk_thousands.check()
208
 
        else:
209
 
            chk_thousands.uncheck()
210
 
 
211
 
        preferences_dlg.getchild(self.BTN_CLOSE).click()
212