~ubuntu-branches/ubuntu/natty/mago/natty

« back to all changes in this revision

Viewing changes to mago/application/gedit.py

  • Committer: Bazaar Package Importer
  • Author(s): Ara Pulido
  • Date: 2010-10-18 16:08:42 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20101018160842-6euef3m6mqxjlcai
Tags: 0.3-0ubuntu1
* New upstream release:
  + Added i18n support
  + Tests working on Maverick 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
PACKAGE = "mago"
 
2
 
 
3
#-*- coding:utf-8 -*-
 
4
"""
 
5
This is the "gedit" module.
 
6
 
 
7
This module provides a wrapper for LDTP to make writing GEdit tests easier.
 
8
"""
 
9
import ooldtp
 
10
import ldtp
 
11
import os
 
12
from .main import Application
 
13
from ..gconfwrapper import GConf
 
14
from ..cmd import globals
 
15
import time
 
16
import gettext
 
17
 
 
18
gettext.install (True)
 
19
gettext.bindtextdomain (PACKAGE, globals.LOCALE_SHARE)
 
20
gettext.textdomain (PACKAGE)
 
21
t = gettext.translation(PACKAGE, globals.LOCALE_SHARE, fallback = True)
 
22
_ = t.gettext
 
23
 
 
24
 
 
25
class GEdit(Application):
 
26
    """
 
27
    GEdit manages the Gedit application.
 
28
    """
 
29
    WINDOW     = _("frm*gedit")
 
30
    TXT_FIELD  = "txt*"
 
31
    LAUNCHER   = "gedit"
 
32
    SAVE_DLG   = _("dlgSave*")
 
33
    SAVE_DLG_TXT_NAME = _("txtName")
 
34
    SAVE_DLG_BTN_SAVE = _("btnSave")
 
35
    QUESTION_DLG = _("dlgQuestion")
 
36
    QUESTION_DLG_BTN_SAVE = _("btnSave")
 
37
    QUESTION_DLG_BTN_SAVE_AS = _("btnSaveAs")
 
38
    QUESTION_DLG_BTN_CLOSE = _("btnClosewithoutSaving")
 
39
    MNU_QUIT = _("mnuQuit")
 
40
    MNU_CLOSE = _("mnuClose")
 
41
    MNU_NEW = _("mnuNew")
 
42
    MNU_SAVE = _("mnuSave")
 
43
 
 
44
    def __init__(self):
 
45
        Application.__init__(self)
 
46
 
 
47
 
 
48
    def write_text(self, text):
 
49
        """
 
50
        It writes text to the current buffer of the Gedit window.
 
51
 
 
52
        @type text: string
 
53
        @param text: The text string to be written to the current buffer.
 
54
        """
 
55
        Application.write_text(self, text, self.TXT_FIELD)
 
56
 
 
57
    def save(self, filename):
 
58
        """
 
59
        It tries to save the current opened buffer to the filename passed as parameter.
 
60
 
 
61
        TODO: It does not manage the overwrite dialog yet.
 
62
 
 
63
        @type filename: string
 
64
        @param filename: The name of the file to save the buffer to.
 
65
        """
 
66
        Application.save(self, self.MNU_SAVE)
 
67
        ooldtp.context(self.name)
 
68
 
 
69
        try:
 
70
            ldtp.waittillguiexist(self.SAVE_DLG)
 
71
            save_dialog = ooldtp.context(self.SAVE_DLG)
 
72
        except ldtp.LdtpExecutionError:
 
73
            raise ldtp.LdtpExecutionError, "The Gedit save dialog was not found."
 
74
        try:
 
75
            save_dlg_txt_filename = save_dialog.getchild(self.SAVE_DLG_TXT_NAME)
 
76
        except ldtp.LdtpExecutionError:
 
77
            raise ldtp.LdtpExecutionError, "The filename txt field in Gedit save dialog was not found."
 
78
        try:
 
79
            ldtp.wait(2)
 
80
            save_dlg_txt_filename.settextvalue(filename)
 
81
        except ldtp.LdtpExecutionError:
 
82
           raise ldtp.LdtpExecutionError, "We couldn't write text."
 
83
 
 
84
        try:
 
85
            save_dlg_btn_save = save_dialog.getchild(self.SAVE_DLG_BTN_SAVE)
 
86
        except ldtp.LdtpExecutionError:
 
87
            raise ldtp.LdtpExecutionError, "The button Save in Gedit save dialog was not found."
 
88
        
 
89
        try:
 
90
            save_dlg_btn_save.click()
 
91
        except ldtp.LdtpExecutionError:
 
92
            raise ldtp.LdtpExecutionError, "There was an error when pushing the Save button."
 
93
 
 
94
        ldtp.waittillguinotexist(self.SAVE_DLG)
 
95
        ldtp.wait(1)
 
96
 
 
97
    def close(self, save=False, filename=''):
 
98
        """
 
99
        Given a gedit window, it tries to close the application.
 
100
        By default, it closes without saving. This behaviour can be changed to save (or save as) on close.
 
101
         
 
102
        @type save: boolean
 
103
        @param save: If True, the edited file will be saved on close.
 
104
 
 
105
        @type filename: string
 
106
        @param filename: The file name to save the buffer to 
 
107
        """
 
108
 
 
109
        # Exit using the Quit menu 
 
110
        try:
 
111
            gedit = ooldtp.context(self.name)
 
112
            try:
 
113
                quit_menu = gedit.getchild(self.MNU_QUIT)
 
114
            except ldtp.LdtpExecutionError:
 
115
                raise ldtp.LdtpExecutionError, "The quit menu was not found."
 
116
            quit_menu.selectmenuitem()
 
117
        except ldtp.LdtpExecutionError:
 
118
            raise ldtp.LdtpExecutionError, "Mmm, something went wrong when closing the application."
 
119
 
 
120
        question_dialog = None
 
121
        count = 0
 
122
        while not gedit.waittillguinotexist(guiTimeOut=1) and \
 
123
                count < 10:
 
124
            try:
 
125
                question_dialog = ooldtp.context(self.QUESTION_DLG)
 
126
            except:
 
127
                count += 1
 
128
            else:
 
129
                break
 
130
 
 
131
        # If the text has changed, the save dialog will appear
 
132
        if question_dialog:
 
133
            # Test if the file needs to be saved
 
134
            if save:
 
135
                try:
 
136
                    question_dlg_btn_save = question_dialog.getchild(self.QUESTION_DLG_BTN_SAVE)
 
137
                    question_dlg_btn_save.click()
 
138
                except ldtp.LdtpExecutionError:
 
139
                    # If the Save button was not found, we will try to find the Save As
 
140
                    try:
 
141
                        question_dlg_btn_save = question_dialog.getchild(self.QUESTION_DLG_BTN_SAVE_AS)
 
142
                        question_dlg_btn_save.click()
 
143
                    except ldtp.LdtpExecutionError:
 
144
                        raise ldtp.LdtpExecutionError, "The save or save as buttons in Gedit question dialog were not found."
 
145
 
 
146
                    try:
 
147
                        ldtp.waittillguiexist(self.SAVE_DLG)
 
148
                        save_dialog = ooldtp.context(self.SAVE_DLG)
 
149
                    except ldtp.LdtpExecutionError:
 
150
                        raise ldtp.LdtpExecutionError, "The Gedit save dialog was not found."
 
151
                    try:
 
152
                        save_dlg_txt_filename = save_dialog.getchild(self.SAVE_DLG_TXT_NAME)
 
153
                    except ldtp.LdtpExecutionError:
 
154
                        raise ldtp.LdtpExecutionError, "The filename txt field in Gedit save dialog was not found."
 
155
                    try:
 
156
                        ldtp.wait(2)
 
157
                        save_dlg_txt_filename.settextvalue(filename)
 
158
                    except ldtp.LdtpExecutionError:
 
159
                        raise ldtp.LdtpExecutionError, "There was an error when writing the text."
 
160
 
 
161
                    try:
 
162
                        save_dlg_btn_save = save_dialog.getchild(self.SAVE_DLG_BTN_SAVE)
 
163
                    except ldtp.LdtpExecutionError:
 
164
                        raise ldtp.LdtpExecutionError, "The save button in Gedit save dialog was not found."
 
165
        
 
166
                    try:
 
167
                        save_dlg_btn_save.click()
 
168
                    except ldtp.LdtpExecutionError:
 
169
                        raise ldtp.LdtpExecutionError, "There was an error when pushing the Save button."
 
170
 
 
171
                    ldtp.waittillguinotexist(self.SAVE_DLG)
 
172
            
 
173
            else:
 
174
                try:
 
175
                    question_dlg_btn_close = question_dialog.getchild(self.QUESTION_DLG_BTN_CLOSE)
 
176
                    question_dlg_btn_close.click()
 
177
                except ldtp.LdtpExecutionError:
 
178
                    raise ldtp.LdtpExecutionError, "It was not possible to click the close button."
 
179
 
 
180
            gedit.waittillguinotexist(guiTimeOut=20)
 
181