summaryrefslogtreecommitdiff
path: root/testautomation/framework/tools/includes/template_tools.inc
blob: 20881f3fb85e762ff6fc2962adfde0bcabd62704 (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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
'encoding UTF-8  Do not remove or change this line!
'**************************************************************************
' DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
'
' Copyright 2000, 2010 Oracle and/or its affiliates.
'
' OpenOffice.org - a multi-platform office productivity suite
'
' This file is part of OpenOffice.org.
'
' OpenOffice.org is free software: you can redistribute it and/or modify
' it under the terms of the GNU Lesser General Public License version 3
' only, as published by the Free Software Foundation.
'
' OpenOffice.org is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU Lesser General Public License version 3 for more details
' (a copy is included in the LICENSE file that accompanied this code).
'
' You should have received a copy of the GNU Lesser General Public License
' version 3 along with OpenOffice.org.  If not, see
' <http://www.openoffice.org/license.html>
' for a copy of the LGPLv3 License.
'
'/******************************************************************************
'*
'*  owner : gregor.hartmann@oracle.com
'*
'*  short description : Helper functions to ease usage of templates
'*
'\******************************************************************************

function hFindTemplate( sTemplateName as string ) as integer

    '///<H3>Find a template by name in FileNewFromTemplate</H3>
    '///<i>Starting point: Templates and Documents dialog</i><br>
    '///<u>Input</u>:
    '///<ol>
    '///+<li> Name of the template to search for (string)</li>
    '///</ol>
    '///<u>Returns</u>:
    '///<ol>
    '///+<li> Index of the Template in the containing folder (integer)</li>
    '///<ul>
    '///+<li>1 ... n : Index of the template (Position in folder)</li>
    '///+<li>0 : No template found by given name</li>
    '///</ul>
    '///</ol>
    '///<u>Description</u>:
    '///<ul>
    const CFN = "hFindTemplate::"
    
    dim brc as boolean
        brc = false
    dim irc as integer
        irc = 0
        
    dim iObjectFolder as integer
    dim iObjectFolders as integer
    
    dim iItemCount as integer
    dim iCurrentItem as integer
    dim cCurrentItem as string
    
    '///+<li>select the templates from the category list</li>
    hSelectCategory( "TEMPLATES" )
    
    '///+<li>run through every item in the list to find the template.</li>
    ' NOTE: If the name of the template is not unique, the function will find
    '       the first occurrence
    ' NOTE: As we do not know the name of "My Templates" (it is localized) we 
    '       need to search all folders.. 
    iObjectFolders = FileList.getItemCount()

    '///<ul>
    for iObjectFolder = 1 to iObjectFolders
    
        '///+<li>Select the (next) folder</li>
        hSelectFileFolder( iObjectFolder , true )

        '///+<li>Retrieve the number of items within the folder</li>
        iItemCount = FileList.getItemCount()
        
        '///+<li>For each item in the folder do:</li>
        '///<ul>
        for iCurrentItem = 1 to iItemCount
        
            '///+<li>Select the (next) item</li>
            FileList.select( iCurrentItem )

            '///+<li>Get the name of the item</li>
            cCurrentItem = FileList.getSelText()
        
            '///+<li>If this is the item we are searching for, exit</li>
            if ( cCurrentItem = sTemplateName ) then
                irc = iCurrentItem : if ( irc = 0 ) then irc = 1 ' strange hack
                brc = true
                exit for
            endif
            
        next iCurrentItem
        '///</ul>
        
        '///+<li>Exit the outer loop</li>
        if ( brc ) then
            exit for
        endif
        
        '///+<li>Click &quot;Up one level&quot;</li>
        UpOneLevel.click()
        
    next iObjectFolder
    '///</ul>
    
    if ( brc ) then
        printlog( CFN & "Template found: " & cCurrentItem )
    else
        printlog( CFN & "Template could not be found." )
    endif
    
    '///+<li>Return the index of the requested template</li>
    hFindTemplate() = irc
    '///</ul>
    
end function

'*******************************************************************************

function hSelectCategory( cCategory as string ) as boolean

    '///<h3>Select a category from the left pane of the templates dialog</h3>
    '///<i>Requires: Templates and Documets dialog must be open</i><br>
    '///<u>Input</u>: Category (string):
    '///<ul>
    '///+<li>NEWDOCUMENTS to select New Documents</li>
    '///+<li>TEMPLATES to select Templates</li>
    '///+<li>MYDOCUMENTS to select My Documents</li>
    '///+<li>SAMPLES to select Samples</li>
    '///</ul>
    '///<u>Returns</u>: Alwas TRUE, no errorhandling

    Kontext "TemplateAndDocuments"
    if ( TemplateAndDocuments.exists( 2 ) ) then
        Wait( 500 )
        Category.typeKeys( "<HOME>" )
        wait( 500 )
        
        if ( UCASE( cCategory ) = "NEWDOCUMENTS" ) then
        ' do nothing, the selection should be on this item
            
        elseif ( UCASE( cCategory ) = "TEMPLATES" ) then
            Category.typeKeys( "<DOWN>" )
            
        elseif ( UCASE( cCategory ) = "MYDOCUMENTS" ) then
            Category.typeKeys( "<DOWN>" )
            Category.typeKeys( "<DOWN>" )
            
        elseif ( UCASE( cCategory ) = "SAMPLES" ) then
            Category.typeKeys( "<DOWN>" )
            Category.typeKeys( "<DOWN>" )
            Category.typeKeys( "<DOWN>" )
        endif

        hSelectCategory() = true
    else
        warnlog( "TemplateAndDocuments dialog did not open" )
    endif

end function

'***************************************************************************

function hSelectFileFolder( iFolder as integer , bVerbose as boolean  ) as integer

    '///<h3>Select a folder on the templates dialog's right pane by index</h3>
    '///<i>Requires: Templates and Documents dialog must be open</i><br>
    '///<u>Input</u>: 
    '///<ol>
    '///+<li>Index of the folder to be selected on the categories-pane (integer)</li>
    '///<ul>
    '///+<li>Must be > 0</li>
    '///</ul>
    '///+<li>Turn printlog on for debugging purpose (boolean)</li>
    '///<ul>
    '///+<li>TRUE : Be verbose</li>
    '///+<li>FALSE : Run silent</li>
    '///</ul>
    '///</ol>
    '///<u>Returns</u>: 
    '///<ol>
    '///+<li>Number of items in the selected folder (integer)</li>
    '///<ul>
    '///+<li>Must be > 0</li>
    '///</ul>
    '///</ol>

    '///<u>Description</u>:
    '///<ul>
    dim iItems as integer
    dim cFolder as string

    Kontext "TemplateAndDocuments"
    
    '///+<li>Select the entry with the given index</li>
    FileList.select( iFolder )
    
    '///+<li>Retrieve the name of the selected object</li>
    cFolder = FileList.getText()
    
    '///+<li>Press return</li>
    FileList.typeKeys( "<return>" )
    wait( 500 )
    
    '///+<li>Get the number of items in the current folder</li>
    iItems = FileList.getItemCount()
    
    '///+<li>Print a comment to the log if specified</li>
    if ( bVerbose ) then
        printlog( " * " & cFolder & " contains " & iItems & " items." )
    endif

    '///+<li>Return the number of items</li>
    hSelectFileFolder() = iItems
    '///</ul>

end function

'*******************************************************************************

function hGetFileFolderName( iFolder as integer ) as string

    '///<h3>Get the name of the currently selected folder on templates dialog</h3>
    '///<i>Requires: Templates and Documents dialog must be open</i><br>
    '///<u>Input</u>: Index of the desired folder<br>
    '///<u>Returns</u>: Name of the selected folder

    Kontext "TemplateAndDocuments"
    FileList.select( iFolder )
    WaitSlot()
    hGetFileFolderName() = FileList.getText()
    WaitSlot()

end function

'*******************************************************************************

function hSelectDocumentObject( iTitle as integer , iMode as integer ) as string

    const CFN = "hSelectDocumentObject::"
    
    '///<h3>Open or edit sample/template from the templates dialog</h3>
    '///<i>Requires: Templates and Documents dialog must be open</i><br>
    '///<u>Input</u>: 
    '///<ol>
    '///+<li>Index of the folder to be selected on the categories-pane (integer)</li>
    '///<ul>
    '///+<li>Valid positive index</li>
    '///</ul>
    '///+<li>Mode in which to open the template (integer)</li>
    '///<ul>
    '///+<li>0 = Do not open the object, just return its name</li>
    '///+<li>1 = Open a new document based on the selected Template</li>
    '///+<li>2 = edit the template (unsupported)</li>
    '///</ul>
    '///</ol>
    '///<u>Returns</u>:
    '///<ul>
    '///+<li>The name of the selected item (string)</li>
    '///</ul>
    '///<u>Description</u>:
    '///<ul>

    dim cTitle as string
    dim brc as boolean
    dim iObjectCount as integer

    Kontext "TemplateAndDocuments"

    '///+<li>Get the title of the selected object</li>
    cTitle = hGetFileFolderName( iTitle )
    
    '///+<li>Count the number of objects in the list</li>
    iObjectCount = FileList.getItemCount()
    printlog( CFN & "Title: " & cTitle )

    select case iMode
    case 0 ' do not load the document, return the title and exit the function
        hSelectDocumentObject() = cTitle
        exit function
    case 1 ' open new document based on the template
        WaitSlot()
        kontext "TemplateAndDocuments"
        FileList.typeKeys( "<return>" )
        WaitSlot( 5000 )

        try
            kontext "TemplateAndDocuments"
            if ( TemplateAndDocuments.exists() ) then
                '///+<li>If yes: Try to determine if it is a new folder</li>
                if ( hIsObjectAFolder( iObjectCount ) ) then
                    hSelectDocumentObject() = "Folder"
                    exit function
                endif

            endif
        catch
        endcatch
        
    case 2 : warnlog( "Unsupported option: Edit template" )
        
    end select

    hFileWait()
    hHandleActivesOnLoad( 2, false )
    brc = hHandleInitialDialogs()

    '///+<li>If all initial dialogs were handled correctly, return the title</li>
    if ( brc ) then
        hSelectDocumentObject() = cTitle
    else
        hSelectDocumentObject() = ""
    endif
    '///</ul>

end function

'*******************************************************************************

function hIsTemplateDialogClosed() as boolean

    const CFN = "hIsTemplateDialogClosed::"
    '///<h3>Test whether the Templates and Documents dialog is closed after executing an object</h3>
    '///<i>Requires: Templates and Documents dialog must be open</i><br>
    '///<u>Returns</u>: 
    '///<ul>
    '///+<li>TRUE if the Templates and Documents dialog cannot be found</li>
    '///+<li>FALSE if the selected object was a foder (Templates and Documents still open</li>
    '///</ul>
    
    dim iTry as integer : iTry = 0

    
    if ( WaitSlot( 2000 ) = WSFinished ) then
        kontext "TemplateAndDocuments"
        try
            if ( TemplateAndDocuments.exists() ) then
                printlog( CFN & "Dialog still open. Maybe we opened a folder" )
                hIsTemplateDialogClosed() = false
                exit function
            else
                printlog( CFN & "Regular object. Dialog is closed" )
                hIsTemplateDialogClosed() = true
                exit function
            endif
        catch
            warnlog( "Failure to query Templates and Documents dialog" )
            hIsTemplateDialogClosed() = true
        endcatch
    else
        warnlog( "Slot not finished within 2000 msec" )
        hIsTemplateDialogClosed() = true
    endif

end function

'*******************************************************************************

function hIsObjectAFolder( iObjects as integer ) as boolean

    '///<h3>Test whether the &quot;Chapters&quot; folder has been selected</h3>
    '///<i>E.g. the Chapters-folder belongs to a master document and must be skipped.
    '///+ To didentify this folder, the number of items is checked (here: 4) which 
    '///+ should be unique (all other folders have more items)</i><br>
    '///<i>Requires: Templates and Documents dialog must be open</i><br>
    '///<u>Input</u>. Number of objects in the folder (integer)<br>
    '///<u>Returns</u>: TRUE if number of items matches iObjects (boolean)<br>
    '///<u>Description</u>:
    '///<ul>
    
    ' NOTE: This function should only handle one folder called "Chapters"
    '       below "Text Documents". We do not want to load the chapters
    '       separately, they are a part of a Master-Document and will be
    '       loaded at another time.
    '       To find out whether we are in a new folder or not, the number
    '       of objects in the parent folder is compared to the number in the
    '       current. This is a hack with a good probability to work.
    
    const CFN = "hIsObjectAFolder::"
    dim iCurrentObjects as integer

    if ( iObjects < 1 ) then
        warnlog( CFN & "Invalid Objectcount passed to function: " & iObjects )
        warnlog( CFN & "Defaulting to 1 -> outcome is undefined" )
        iObjects = 1
    endif

    kontext "TemplateAndDocuments"
    '///+<li>Compare the objectcount. If different, this is another folder</li>
    '///+<li>If the number is unchanged, we have an unknown error -> crash</li>
    iCurrentObjects = Filelist.getItemCount()

    if ( iCurrentObjects <> iObjects ) then
        printlog( CFN & "Object appears to be a folder with " & iCurrentObjects & " items" )
        hIsObjectAFolder() = true
    else
        warnlog( CFN & "Unknown error: Objectcount is unchanged" )
        hIsObjectAFolder() = false
    endif

    '///</ul>

end function

'*******************************************************************************

function hDeleteUserTemplates() as integer

    ' Recommendation: Use outside of testcase

    '///<h3>Delete all files located in the user templates directory</h3>
    '///<i>Uses hDeleteFile tzo remove all files below gOfficePath\user\template</i><br>
    '///<u>Input</u>: Nothing<br>
    '///<u>Returns</u>: Number of deleted objects<br>
    '///<u>Description</u>:
    '///<ul>
    
    const CFN = "hDeleteUserTemplates::"
    
    dim iFileCount as integer
    dim aFileList( 200 ) as string
    dim sPath as string
    dim iCounter as integer
    
    '///+<li>Set the path to the user-templates (default location)</li>
    sPath = convertpath( gOfficePath & "user\template" )
    
    '///+<li>Load the list of files into an array</li>
    iFileCount = GetFileList( sPath, "*.*", aFileList() )
    printlog( CFN & "Found " & iFileCount & " file(s)." )
    
    '///+<li>Delete each file and print the result to the log</li>
    for iCounter = 1 to iFileCount
    
        hDeleteFile( aFileList( iCounter ) )
        
    next iCounter
    
    '///+<li>Return the number of files that was found in the templates directory</li>
    hDeleteUserTemplates() = iFileCount
    
    '///</ul>
    
end function