summaryrefslogtreecommitdiff
path: root/uitest/ui_logger_dsl/dsl_core.py
blob: d914e2ed2dc4e29a5073b9fc00befceebc575b86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/env python3
# This file is part of the LibreOffice UI_logger project.
#
# This file contain the implementation of the Compiler
# for the new logger grammar
#
# ul stands for Ui_Logger

import os
import sys
import argparse
from textx.metamodel import metamodel_from_file

def parse_args():
    """
    This function parses the command-line arguments
    to get the input and output file details
    """
    parser = argparse.ArgumentParser(description = "Generate a UI test file from log")
    parser.add_argument("input_address", type = str, help = "The log file address")
    parser.add_argument("output_address", type = str, help = "The test file address")
    args = parser.parse_args()
    return args

class ul_Compiler:
    prev_command=""
    variables=[]
    objects = dict()
    current_app=""
    def __init__(self , input_address , output_address):
        self.ui_dsl_mm = metamodel_from_file('ui_logger_dsl_grammar.tx')
        self.output_stream=self.initiate_test_generation(output_address)
        self.input_address=input_address

    def get_log_file(self , input_address):
        try:
            # load the program
            content = self.ui_dsl_mm.model_from_file(input_address)
        except IOError as err:
            print("IO error: {0}".format(err))
            print("Use " + os.path.basename(sys.argv[0]) + " -h to get usage instructions")
            sys.exit(1)

        return content

    def initiate_test_generation(self,output_address):
        try:
            f = open(output_address,"w")
        except IOError as err:
            print("IO error: {0}".format(err))
            print("Use " + os.path.basename(sys.argv[0]) + " -h to get usage instructions")
            sys.exit(1)
        line="# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-\n\n" + \
        "from uitest.framework import UITestCase\n" + \
        "from libreoffice.uno.propertyvalue import mkPropertyValues\n" + \
        "import importlib\n\n" + \
        "class TestClass(UITestCase):\n" + \
        "\tdef test_function(self):\n"

        self.variables.append(line)

        return f

    def compile(self):
        self.ui_dsl_mm.register_obj_processors({
            'UNOCommand': self.handle_uno,
            'StarterCommand': self.handle_start,
            'CloseDialog': self.handle_Dialog,
            'OpenModelessDialog': self.handle_Dialog,
            'OpenModalDialog':self.handle_Dialog,
            'ButtonUIObject':self.handle_button,
            'CheckBoxUIObject':self.handle_check_box,
            'TabControlUIObject':self.handle_tab,
            'ComboBoxUIObject':self.handle_Combo_box,
            'RadioButtonUIObject':self.handle_Radio_button,
            'ListBoxUIObject':self.handle_List_box,
            'SpinFieldUIObject':self.handle_spin_field,
            'EditUIObject':self.handle_Edit_uiObject,
            'writer_Type_command':self.handle_writer_type,
            'writer_Select_command':self.handle_writer_select,
            'writer_GOTO_command':self.handle_wirter_goto,
            })

        self.log_lines=self.get_log_file(self.input_address)

    def handle_uno(self, UNOCommand):
        if(UNOCommand.prameters==None):
            line = "\t\tself.xUITest.executeCommand(\"" + \
                UNOCommand.uno_command_name +"\")\n"
        else:
            paramaters=""
            for p in UNOCommand.prameters.parameter_data :
                paramaters = paramaters + "\"" + p.key + "\" : " + str(p.value) + " ,"
            paramaters = paramaters[:-1]

            line = "\t\tself.xUITest.executeCommandWithParameters(\"" + \
                UNOCommand.uno_command_name +"\", mkPropertyValues({"+ paramaters +"}) )\n"

        self.variables.append(line)
        self.prev_command=UNOCommand

    def handle_start(self, StarterCommand):
        line="\t\tMainDoc = self.ui_test.create_doc_in_start_center(\""+\
            StarterCommand.program_name+"\")\n"
        self.variables.append(line)

        line="\t\tMainWindow = self.xUITest.getTopFocusWindow()\n"
        self.variables.append(line)
        app={"writer":"writer_edit","calc":"grid_window"}#to be continue
        self.current_app=app[StarterCommand.program_name]
        self.prev_command=StarterCommand

    def handle_Dialog(self, DialogCommand):

        if (DialogCommand.__class__.__name__ == "OpenModalDialog"):
            old_line = self.variables.pop()

            if (self.prev_command.__class__.__name__ == "UNOCommand"):
                key_word=self.prev_command.uno_command_name[-6:]
            else:
                key_word=old_line[-9:-3]

            if ( key_word == "Dialog"):
                old_line="\t\tself.ui_test.execute_dialog_through_command(\""+\
                    self.prev_command.uno_command_name+"\")\n"
            self.variables.append(old_line)
            line = "\t\t" + DialogCommand.dialog_name + " = self.xUITest.getTopFocusWindow()\n"

        elif (DialogCommand.__class__.__name__ == "OpenModelessDialog"):
            old_line = self.variables.pop()
            if (self.prev_command.__class__.__name__ == "UNOCommand"):
                key_word=self.prev_command.uno_command_name[-6:]
            else:
                key_word=old_line[-9:-3]

            if ( key_word == "Dialog"):
                old_line="\t\tself.ui_test.execute_modeless_dialog_through_command(\""+\
                    self.prev_command.uno_command_name+"\")\n"
            self.variables.append(old_line)
            line = "\t\t" + DialogCommand.dialog_name + "  = self.xUITest.getTopFocusWindow()\n"

        elif (DialogCommand.__class__.__name__ == "CloseDialog"):
            if (self.prev_command.__class__.__name__ == "ButtonUIObject"):
                old_line = self.variables.pop()
                line="\t\tself.ui_test.close_dialog_through_button("+\
                    self.prev_command.ui_button+")\n"

        self.variables.append(line)
        self.prev_command=DialogCommand

    def handle_button(self, ButtonUIObject):

        if ButtonUIObject.ui_button in self.objects:
            self.objects[ButtonUIObject.ui_button]+=1
        else:
            self.objects[ButtonUIObject.ui_button]=1
            line="\t\t"+ButtonUIObject.ui_button+" = "+ButtonUIObject.parent_id+\
                ".getChild(\""+ButtonUIObject.ui_button+"\")\n"
            self.variables.append(line)

        line="\t\t"+ButtonUIObject.ui_button+".executeAction(\"CLICK\",tuple())\n"
        self.variables.append(line)
        self.prev_command=ButtonUIObject

    def handle_check_box(self, CheckBoxUIObject):

        if CheckBoxUIObject.Check_box_id in self.objects:
            self.objects[CheckBoxUIObject.Check_box_id]+=1
        else:
            self.objects[CheckBoxUIObject.Check_box_id]=1
            line="\t\t"+CheckBoxUIObject.Check_box_id+" = "+CheckBoxUIObject.parent_id+\
                ".getChild(\""+CheckBoxUIObject.Check_box_id+"\")\n"
            self.variables.append(line)

        line="\t\t"+CheckBoxUIObject.Check_box_id+".executeAction(\"CLICK\",tuple())\n"
        self.variables.append(line)
        self.prev_command=CheckBoxUIObject

    def handle_tab(self, TabControlUIObject):

        if TabControlUIObject.tab_id in self.objects:
            self.objects[TabControlUIObject.tab_id]+=1
        else:
            self.objects[TabControlUIObject.tab_id]=1
            line="\t\t"+TabControlUIObject.tab_id+" = "+TabControlUIObject.parent_id+\
                ".getChild(\""+TabControlUIObject.tab_id+"\")\n"
            self.variables.append(line)

        line="\t\t"+TabControlUIObject.tab_id+\
            ".executeAction(\"SELECT\", mkPropertyValues({\"POS\": \""+\
            str(TabControlUIObject.tab_page_number)+"\"}))\n"
        self.variables.append(line)
        self.prev_command=TabControlUIObject

    def handle_Combo_box(self, ComboBoxUIObject):

        if ComboBoxUIObject.Combo_box_id in self.objects:
            self.objects[ComboBoxUIObject.Combo_box_id]+=1
        else:
            self.objects[ComboBoxUIObject.Combo_box_id]=1
            line="\t\t"+ComboBoxUIObject.Combo_box_id+" = "+ComboBoxUIObject.parent_id+\
                ".getChild(\""+ComboBoxUIObject.Combo_box_id+"\")\n"
            self.variables.append(line)

        line="\t\t"+ComboBoxUIObject.Combo_box_id+\
            ".executeAction(\"SELECT\", mkPropertyValues({\"POS\": \""+\
            str(ComboBoxUIObject.item_num)+"\"}))\n"
        self.variables.append(line)
        self.prev_command=ComboBoxUIObject

    def handle_Radio_button(self,RadioButtonUIObject):

        if RadioButtonUIObject.Radio_button_id in self.objects:
            self.objects[RadioButtonUIObject.Radio_button_id]+=1
        else:
            self.objects[RadioButtonUIObject.Radio_button_id]=1
            line="\t\t"+RadioButtonUIObject.Radio_button_id+" = "+RadioButtonUIObject.parent_id+\
                ".getChild(\""+RadioButtonUIObject.Radio_button_id+"\")\n"
            self.variables.append(line)

        line="\t\t"+RadioButtonUIObject.Radio_button_id+".executeAction(\"CLICK\",tuple())\n"
        self.variables.append(line)
        self.prev_command=RadioButtonUIObject

    def handle_List_box(self, ListBoxUIObject):

        if ListBoxUIObject.list_id in self.objects:
            self.objects[ListBoxUIObject.list_id]+=1
        else:
            self.objects[ListBoxUIObject.list_id]=1
            line="\t\t"+ListBoxUIObject.list_id+" = "+ListBoxUIObject.parent_id+\
                ".getChild(\""+ListBoxUIObject.list_id+"\")\n"
            self.variables.append(line)

        line="\t\t"+ListBoxUIObject.list_id+\
            ".executeAction(\"SELECT\", mkPropertyValues({\"POS\": \""+\
            str(ListBoxUIObject.POS)+"\"}))\n"
        self.variables.append(line)
        self.prev_command=ListBoxUIObject

    def handle_spin_field(self,SpinFieldUIObject):

        if SpinFieldUIObject.Spin_id in self.objects:
            self.objects[SpinFieldUIObject.Spin_id]+=1
        else:
            self.objects[SpinFieldUIObject.Spin_id]=1
            line="\t\t"+SpinFieldUIObject.Spin_id+" = "+SpinFieldUIObject.parent_id+\
                ".getChild(\""+SpinFieldUIObject.Spin_id+"\")\n"
            self.variables.append(line)

        if(SpinFieldUIObject.change=="Increase"):
            line="\t\t"+SpinFieldUIObject.Spin_id+".executeAction(\"UP\",tuple())\n"
        elif(SpinFieldUIObject.change=="Decrease"):
            line="\t\t"+SpinFieldUIObject.Spin_id+".executeAction(\"DOWN\",tuple())\n"
        self.variables.append(line)
        self.prev_command=SpinFieldUIObject

    def handle_Edit_uiObject(self,EditUIObject):

        if(EditUIObject.action.__class__.__name__ =="Type_action"):
            if EditUIObject.action.edit_button in self.objects:
                self.objects[EditUIObject.action.edit_button]+=1
            else:
                self.objects[EditUIObject.action.edit_button]=1
                line="\t\t"+EditUIObject.action.edit_button+" = "+EditUIObject.parent_id+\
                ".getChild(\""+EditUIObject.action.edit_button+"\")\n"
                self.variables.append(line)

            if(EditUIObject.action.what_to_type.__class__.__name__=="char"):
                line="\t\t"+EditUIObject.action.edit_button+\
                ".executeAction(\"TYPE\", mkPropertyValues({\"TEXT\": \""+\
                EditUIObject.action.what_to_type.input_char+"\"}))\n"

            elif(EditUIObject.action.what_to_type.__class__.__name__=="KeyCode"):
                line="\t\t"+EditUIObject.action.edit_button+\
                ".executeAction(\"TYPE\", mkPropertyValues({\"KEYCODE\":"+\
                EditUIObject.action.what_to_type.input_key_code+"\"}))\n"
            self.variables.append(line)

        if(EditUIObject.action.__class__.__name__ =="SELECT"):
            if EditUIObject.action.edit_button in self.objects:
                self.objects[EditUIObject.action.edit_button]+=1
            else:
                self.objects[EditUIObject.action.edit_button]=1
                line="\t\t"+EditUIObject.action.edit_button+" = "+EditUIObject.parent_id+\
                ".getChild(\""+EditUIObject.action.edit_button+"\")\n"
                self.variables.append(line)

            line="\t\t"+EditUIObject.action.edit_button+\
                ".executeAction(\"SELECT\", mkPropertyValues({\"from\": \""+\
                str(EditUIObject.action.from_pos )+"\", \"TO\": \""+\
                str(EditUIObject.action.to_pos)+"\"}))\n"
            self.variables.append(line)

        if(EditUIObject.action.__class__.__name__ =="Clear"):
            if EditUIObject.action.edit_button in self.objects:
                self.objects[EditUIObject.action.edit_button]+=1
            else:
                self.objects[EditUIObject.action.edit_button]=1
                line="\t\t"+EditUIObject.action.edit_button+" = "+EditUIObject.parent_id+\
                ".getChild(\""+EditUIObject.action.edit_button+"\")\n"
                self.variables.append(line)

            line="\t\t"+EditUIObject.action.edit_button+\
                ".executeAction(\"CLEAR\",tuple())\n"
            self.variables.append(line)
        self.prev_command=EditUIObject

    def handle_writer_type (self,writer_Type_command):

        if "writer_edit" in self.objects:
            self.objects["writer_edit"]+=1
        else:
            self.objects["writer_edit"]=1
            line="\t\twriter_edit = MainWindow.getChild(\"writer_edit\")\n"
            self.variables.append(line)

        if(writer_Type_command.what_to_type.__class__.__name__=="char"):
            line="\t\twriter_edit.executeAction(\"TYPE\", mkPropertyValues"+\
            "({\"TEXT\": \""+\
            writer_Type_command.what_to_type.input_char+"\"}))\n"
        elif(writer_Type_command.what_to_type.__class__.__name__=="KeyCode"):
            line="\t\twriter_edit.executeAction(\"TYPE\", mkPropertyValues"+\
            "({\"KEYCODE\": \""+\
            writer_Type_command.what_to_type.input_key_code+"\"}))\n"
        self.variables.append(line)
        self.prev_command=writer_Type_command

    def handle_writer_select (self,writer_Select_command):

        if "writer_edit" in self.objects:
            self.objects["writer_edit"]+=1
        else:
            self.objects["writer_edit"]=1
            line="\t\twriter_edit = MainWindow.getChild(\"writer_edit\")\n"
            self.variables.append(line)

        line="\t\twriter_edit.executeAction(\"SELECT\", mkPropertyValues({\"END_POS\": \""+\
            str(writer_Select_command.from_pos)+"\", \"START_POS\": \""+\
                str(writer_Select_command.to_pos)+"\"}))\n"
        self.variables.append(line)
        self.prev_command=writer_Select_command

    def handle_wirter_goto (self,writer_GOTO_command):

        if "writer_edit" in self.objects:
            self.objects["writer_edit"]+=1
        else:
            self.objects["writer_edit"]=1
            line="\t\twriter_edit = MainWindow.getChild(\"writer_edit\")\n"
            self.variables.append(line)

        line="\t\twriter_edit.executeAction(\"GOTO\", mkPropertyValues({\"PAGE\": \""+\
            str(writer_GOTO_command.page_num)+"\"}))\n"
        self.variables.append(line)
        self.prev_command=writer_GOTO_command

    def Generate_UI_test(self):
        line="\t\tself.ui_test.close_doc()"
        self.variables.append(line)

        for line in self.variables:
            self.output_stream.write(str(line))

    def __del__(self):
        self.output_stream.close()

def main():
    args = parse_args()
    ui_logger = ul_Compiler(args.input_address,args.output_address)
    ui_logger.compile()
    for statement in (ui_logger.log_lines.commands):
        print(statement)
    ui_logger.Generate_UI_test()
    del ui_logger

if __name__ == '__main__':
    main()