1 'encoding UTF-8 Do not remove or change this line!
2 '**************************************************************************
3 '* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 '* Copyright 2008 by Sun Microsystems, Inc.
7 '* OpenOffice.org - a multi-platform office productivity suite
9 '* $RCSfile: fileoperations.inc,v $
13 '* last change: $Author: jsk $ $Date: 2008-06-20 08:03:54 $
15 '* This file is part of OpenOffice.org.
17 '* OpenOffice.org is free software: you can redistribute it and/or modify
18 '* it under the terms of the GNU Lesser General Public License version 3
19 '* only, as published by the Free Software Foundation.
21 '* OpenOffice.org is distributed in the hope that it will be useful,
22 '* but WITHOUT ANY WARRANTY; without even the implied warranty of
23 '* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 '* GNU Lesser General Public License version 3 for more details
25 '* (a copy is included in the LICENSE file that accompanied this code).
27 '* You should have received a copy of the GNU Lesser General Public License
28 '* version 3 along with OpenOffice.org. If not, see
29 '* <http://www.openoffice.org/license.html>
30 '* for a copy of the LGPLv3 License.
32 '/************************************************************************
34 '* owner : joerg.skottke@sun.com
36 '* short description : check the internal file dialog ( extended tests )
38 '\******************************************************************************
40 function hSaveLoadDelSuccess( cFile as string ) as integer
42 '///<h3>Successfully save, close, load, close and delete a file</h3>
43 '///<i>Uses</i>: framework\tools\t_stringtools.inc<br><br>
46 '///+<li>Filename incl. extension (string)</li>
50 '///+<li>Errorcode (integer)</li>
52 '///+<li>0 = all ok</li>
53 '///+<li>1 = Saving failed</li>
54 '///+<li>2 = Closing the file failed</li>
55 '///+<li>3 = Reloading failed</li>
56 '///+<li>4 = Closing the file failed</li>
57 '///+<li>5 = Deleting failed</li>
58 '///+<li>-1 = Post operation error</li>
61 '///<u>Description</u>:
65 dim cFileExt as string
66 cFileExt = cFile & hGetSuffix( "current" )
68 const CFN = "hSaveLoadDelSuccess::"
71 printlog( CFN & "Enter with option: " & cFile & "/" & cFileExt )
73 '///+<li>Close the navigator if it exists</li>
76 '///+<li>Save the current file, overwriting existing</li>
77 brc = hSaveFileExpectSuccess( cFile , TRUE ) ' save and overwrite
80 '///+<li>Close the file</li>
81 brc = hDestroyDocument()
84 '///+<li>Reload the file</li>
85 brc = hLoadFileExpectSuccess( cFileExt )
88 '///+<li>Close the document</li>
89 brc = hDestroyDocument()
92 '///+<li>Delete the file via FileOpen</li>
93 brc = hDeleteFileViaFileOpen( cFileExt )
95 printlog( CFN & "Save, close, load, close, delete ok" )
96 hSaveLoadDelSuccess() = 0
98 warnlog( CFN & "Failed to delete file" )
99 hSaveLoadDelSuccess() = 5
103 warnlog( CFN & "Failed to close file" )
104 hSaveLoadDelSuccess() = 4
108 warnlog( CFN & "Failed to load file" )
109 hSaveLoadDelSuccess() = 3
114 warnlog( CFN & "Closing file failed" )
115 hSaveLoadDelSuccess() = 2
119 warnlog( CFN & "Saving failed" )
120 hSaveLoadDelSuccess() = 1
123 '///+<li>Close possible Messagebox (#i33946#)</li>
125 if ( active.exists( 1 ) ) then
126 printlog( CFN & "Unexpected message: " & active.getText() )
127 qaerrorlog( "#i33946# - message when deleting last document in folder" )
129 hSaveLoadDelSuccess() = 6
132 '///+<li>Close document</li>
133 brc = hDestroyDocument()
139 '*******************************************************************************
141 function hLoadFileExpectSuccess( fpath as string ) as boolean
143 '///<h3>Load a file where failure is expected</h3>
144 '///<i>Uses</i>: framework\tools\t_stringtools.inc<br><br>
145 '///<i>This function is quite similar to hLoadFile but does much less
146 '///+ errorhandling so the information of the type of failure is a little
147 '///+ more exact</i><br><br>
150 '///+<li>Filename incl. extension (string)</li>
154 '///+<li>Errorcondition (boolean)</li>
156 '///+<li>TRUE = File was loaded without problems</li>
157 '///+<li>FALSE = Any error</li>
160 '///<u>Description</u>:
166 const CFN = "hLoadFileExpectSuccess::"
167 printlog( CFN & "Enter with option: " & fpath )
169 '///+<li>Click FileOpen (or use the menu)</li>
172 '///+<li>Enter the filename (with extension)</li>
173 'printlog( " - Type the filepath/name into the entryfield" )
175 if ( OeffnenDlg.exists( 1 ) ) then
176 DateiName.setText( fpath )
178 '///+<li>Click "Open"</li>
179 'printlog( " - Click 'Open'" )
182 '///+<li>Watch out for an unexpected messagebox<br>
184 if ( Active.Exists( 1 ) ) then
185 printlog( "Unexpected active: " & active.getText() )
193 '///+recover in case of error so the test has a chance to continue</li>
194 ' try to recover in case of failure so the test can continue.
196 if ( Oeffnen.exists( 1 ) ) then
197 warnlog( "The file was not opened, it doesn't appear to exist" )
202 warnlog( CFN & "File Open dialog did not open" )
205 '///+<li>Return the errorcode</li>
206 printlog( CFN & "Exit with result: " & brc )
207 hLoadFileExpectSuccess() = brc
212 '*******************************************************************************
214 function hLoadFileExpectFailure( fpath as string ) as boolean
216 '///<h3>Load a file where failure is expected</h3>
217 '///<i>Uses</i>: framework\tools\t_stringtools.inc<br><br>
220 '///+<li>Filename incl. extension (string)</li>
224 '///+<li>Errorcondition (boolean)</li>
226 '///+<li>TRUE = There was an error loading the file (as expected)</li>
227 '///+<li>FALSE = The file was loaded without problems/any other error</li>
230 '///<u>Description</u>:
233 const CFN = "hLoadFileExpectFailure::"
238 printlog( CFN & "Enter with option: " & fpath )
240 '///+<li>Click FileOpen</li>
244 '///+<li>Enter filepath</li>
245 DateiName.setText( convertpath( fpath ) )
247 '///+<li>Click "Open"</li>
250 '///+<li>Close the expected warning dialog, return to FileOpen</li>
252 if ( Active.Exists( 1 ) ) then
253 printlog( CFN & "Expected active: " & active.getText() )
257 if ( OeffnenDlg.exists( 1 ) ) then
261 warnlog( CFN & "File Open dialog is not open" )
266 '///+<li>Verify that we are on FileOpen dialog, set errorcondition true</li>
269 if ( Oeffnen.exists( 1 ) ) then
270 printlog( CFN & "File is not loaded, still in File/Open. Good, cancelling" )
278 '///+<li>If the document is loaded, set errorcondition to false</li>
281 warnlog( CFN & "The file appears to have loaded correctly. Not good" )
283 '///+<li>Click away possible messagebox to recover</li>
285 if ( active.exists( 1 ) ) then
286 printlog( CFN & "Unexpected active: " & active.getText() )
291 '///+<li>Return errorcondition</li>
292 hLoadFileExpectFailure() = brc
297 '*******************************************************************************
299 function hSaveFileExpectSuccess( fpath as string , bReplace as boolean ) as boolean
301 '///<h3>Save a file with optional replace where success is expected</h3>
302 '///<i>Uses</i>: framework\tools\t_stringtools.inc<br><br>
305 '///+<li>Filename incl. extension (string)</li>
306 '///+<li>Replace file (boolean)</li>
308 '///+<li>TRUE = Replace the file</li>
309 '///+<li>FALSE = Do not replace the file</li>
314 '///+<li>Errorcondition (boolean)</li>
316 '///+<li>TRUE = There was an error loading the file (as expected)</li>
317 '///+<li>FALSE = The file was loaded without problems/any other error</li>
320 '///<u>Description</u>:
323 const CFN = "hSaveFileExpectSuccess::"
327 dim iDocumentCount as integer
329 printlog( CFN & "Enter with options: " & fpath & ", " & bReplace )
331 '///+<li>Make sure we start from the backing window</li>
332 do while ( getDocumentCount > 0 )
337 '///+<li>Open a new document</li>
340 '///+<li>Click "Save As..."</li>
343 Kontext "SpeichernDlg"
344 if ( SpeichernDlg.exists( 1 ) ) then
346 '///+<li>Enter a filename (with extension)</li>
347 Dateiname.setText( hGetWorkPath() & fpath )
349 '///+<li>Click "Save"</li>
352 '///+<li>If the file exists, say "yes" to replace it</li>
355 if ( active.exists( 2 ) ) then
356 printlog( CFN & "Overwriting file" )
357 printlog( "Message: " & active.getText() )
361 warnlog( CFN & "Unexpected active - no YES button available." )
362 printlog( CFN & "if any this should have been overwrite warning." )
367 '///+<li>Handle any unexpected errormessage with "OK"</li>
369 if ( active.exists( 2 ) ) then
370 printlog( CFN & "Unexpected active: " & active.getText() )
375 '///+<li>The FileSave dialog should be closed at this point</li>
376 kontext "SpeichernDlg"
377 if ( SpeichernDlg.exists( 1 ) ) then
378 warnlog( CFN & "File Save dialog is still open, it should be closed" )
379 SpeichernDlg.cancel()
382 warnlog( CFN & "Failed to open File Open dialog" )
386 '///+<li>Verify that exactly one document is open</li>
387 if( getDocumentCount <> 1 ) then
388 warnlog( CFN & "Incorrect number of open documents" )
392 printlog( CFN & "Exit with result: " & brc )
393 hSaveFileExpectSuccess() = brc
398 '*******************************************************************************
400 function hSaveFileExpectFailure( fpath as string , errortype as integer ) as boolean
402 '///<h3>Save a file where failure is expected</h3>
403 '///<i>Uses</i>: framework\tools\t_stringtools.inc<br><br>
406 '///+<li>Filename incl. extension (string)</li>
407 '///+<li>Errortype (Integer). Valid options are:</li>
409 '///+<li>0 = Invalid characters in string</li>
410 '///+<li>1 = Filename is interpreted as device</li>
415 '///+<li>Errorcondition (boolean)</li>
417 '///+<li>TRUE = <b><i>There was an error saving the file (as expected)</i></b></li>
418 '///+<li>FALSE = The file was saved without problems/any other error</li>
421 '///<u>Description</u>:
424 ' currently we have two different kinds of failure
425 ' 1. File cannot be saved due to invalid character(s)
426 ' This is errortype = 0
427 ' 2. File cannot be saved because the given name is interpreted as device
428 ' This is errortype = 1
429 ' This sequence tries to save a document with an invalid name. The errormsg
430 ' is closed, the filedialog cancelled and the file closed.
437 const CFN = "hSaveFileExpectFailure::"
439 '///+<li>Open an new document</li>
442 '///+<li>Click "Save As;"</li>
444 printlog( CFN & "Enter with options: " & fpath & ", " & errortype )
447 '///+<li>Enter the filename</li>
448 kontext "SpeichernDlg"
449 if ( SpeichernDlg.exists( 1 ) ) then
451 Kontext "SpeichernDlg"
452 Dateiname.setText( fpath )
454 '///+<li>Click "Save"</li>
458 '///+<li>Handle invalid characters (Errormessages)</li>
459 select case( errortype )
465 if ( Active.exists( 1 ) ) then
467 printlog( CFN & "Check for possible overwrite warning..." )
470 printlog( CFN & "Closed Messagebox with <YES>" )
471 printlog( CFN & "This was the overwrite warning" )
477 if ( Active.exists( 1 ) ) then
478 cMsg = active.getText()
481 printlog( CFN & "Closed Save-Failure warning with <OK>" )
484 qaerrorlog( CFN & "Unknown dialog encountered, <OK> failed:" )
493 if ( Active.exists( 1 ) ) then
494 printlog( CFN & "Expected active: " & active.getText() )
497 Kontext "SpeichernDLG"
498 if ( SpeichernDlg.exists( 1 ) ) then
499 SpeichernDLG.cancel()
502 warnlog( CFN & "File Save dialog is missing. Bad!" )
506 warnlog( CFN & "Errormessage for '/', '\' or ':' is missing" )
513 warnlog( CFN & "Failed to open File Save dialog" )
517 '///+<li>Close the document</li>
520 '///+<li>Return errorcondition</li>
521 printlog( CFN & "Exit with status: " & brc )
522 hSaveFileExpectFailure() = brc
528 '*******************************************************************************
530 function hCreateDirectoryExpectSuccess( dirname as string ) as boolean
532 '///<h3>Create a directory where success is expected</h3>
533 '///<i>Uses</i>: framework\tools\t_stringtools.inc<br><br>
536 '///+<li>Name of the directory (string)</li>
538 '///+<li>Relative or absolute, absolute is recommended</li>
543 '///+<li>Errorcondition (boolean)</li>
545 '///+<li>TRUE = The directory wass created</li>
546 '///+<li>FALSE = On any error (Unable to create or file already exists)</li>
549 '///<u>Description</u>:
555 const CFN = "hCreateDirectoryExpectSuccess::"
556 printlog( CFN & "Enter with option: " & dirname )
558 '///+<li>Click "File Open"</li>
561 '///+<li>Click on "New folder"</li>
563 if ( OeffnenDlg.exists( 2 ) ) then
567 '///+<li>Enter a name for the folder into the dialog</li>
568 Kontext "NeuerOrdner"
569 if ( NeuerOrdner.exists( 1 ) ) then
570 OrdnerName.setText( dirname )
572 '///+<li>Click "OK"</li>
575 warnlog( CFN & "New folder dialog did not open." )
579 '///+<li>Handle possible messagebox (unexpected)</li>
581 if Active.Exists( 1 ) then
582 printlog( CFN & "Unexpected active: " & active.getText() )
591 warnlog( CFN & "File Open dialog is not open." )
595 '///+<li>Return errorcondition</li>
596 printlog( CFN & "Exit with result: " & brc )
597 hCreateDirectoryExpectSuccess() = brc
603 '*******************************************************************************
605 function hDeleteFileViaFileOpen( cFile as String ) as boolean
607 '///<h3>Delete a file using the File Open dialog</h3>
608 '///<i>Uses</i>: framework\tools\t_stringtools.inc<br><br>
611 '///+<li>Filename incl. extension (string)</li>
615 '///+<li>Errorcondition (boolean)</li>
617 '///+<li>TRUE = File was deleted</li>
618 '///+<li>FALSE = Any other condition</li>
621 '///<u>Description</u>:
624 ' This sub deletes a file by name. No errors or warnings are expected.
625 ' if everything goes well, we're back to the originating document after
628 dim iSelectedFilePosition as integer
631 const CFN = "hDeleteFileViaFileOpen::"
632 printlog( CFN & "Enter with option: " & cFile )
634 ' Test for wildcards - the dialog cannot handle those
635 if ( instr( cFile , "?" ) <> 0 ) then
636 warnlog( CFN & "Incorrect call to function, wildcards are not allowed" )
637 hDeleteFileViaFileOpen() = false
641 ' Test for wildcards - the dialog cannot handle those
642 if ( instr( cFile , "*" ) <> 0 ) then
643 warnlog( CFN & "Incorrect call to function, wildcards are not allowed" )
644 hDeleteFileViaFileOpen() = false
648 '///+<li>Click "File Open"</li>
651 '///+<li>Look for the requested file, get the position from the filelist</li>
653 iSelectedFilePosition = hFindFileObjectViaFileOpen( cFile )
655 '///+<li>If the file exists, delete it + verify, if not: Warn and exit</li>
656 if ( iSelectedFilePosition > 0 ) then
659 'printlog( CFN & "Requested file: " & cFile )
660 'printlog( CFN & "Object is at..: " & iSelectedFilePosition )
661 'printlog( CFN & "Object name is: " & DateiAuswahl.getSelText() )
663 DateiAuswahl.TypeKeys( "<DELETE>" )
665 ' Confirm deletion. This dialog should always pop up when deleting
666 Kontext "ConfirmDelete"
667 if ( ConfirmDelete.exists( 1 ) ) then
669 printlog( CFN & "Deleted file: " & cFile )
671 warnlog( CFN & "Messagebox to confirm deletion of file is missing" )
674 ' Handle possible Warnings/Errormessages. If no errors happen,
675 ' verify that the file has been deleted. Note that this dialog will not
676 ' change the returnvalue.
678 if( Active.exists( 1 ) ) then
679 warnlog( CFN & "Unexpected active: " & active.getText() )
683 ' Verify that the file does no longer exist in the filelist.
684 iSelectedFilePosition = hFindFileObjectViaFileOpen( cFile )
685 if ( iSelectedFilePosition = 0 ) then
694 warnlog( CFN & "File not found in workdir: " & cFile )
698 '///+<li>Cancel the FileOpen dialog</li>
702 '///+<li>Return errorcondition</li>
703 printlog( CFN & "Exit with result: " & brc )
704 hDeleteFileViaFileOpen() = brc
710 '******************************************************************************
712 function hGetFileCountViaFileOpen() as integer
714 '///<h3>Get the number of items listed in the FilePicker (File Open)</h3>
715 '///<i>Note</i>: This function works on the current workdirectory<br><br>
718 '///+<li>Nothing</li>
722 '///+<li>Number of items listed in filepicker</li>
724 '///<u>Description</u>:
727 '///+<li>Click "File open"</li>
730 '///+<li>Retrieve the number of items in the filepicker window</li>
732 hGetFileCountViaFileOpen() = DateiAuswahl.getItemCount()
734 '///+<li>Close "File Open"</li>
740 '*******************************************************************************
742 function hNameGen_append( iDecChar as long ) as string
744 '///<h3>Create a filename with specified character at the end</h3>
747 '///+<li>Character as number (integer)</li>
749 '///+<li>Only positive numbers are allowed</li>
750 '///+<li>Respect integer boundaries</li>
755 '///+<li>A filename containing a special character at the end (before suffix)</li>
757 '///<u>Description</u>:
761 const CFN = "hNameGen_append::"
763 '///+<li>Create a string "test", append ascii char</li>
764 cFile = "test" & CHR$( iDecChar )
765 printlog( CFN & "ASCII " & iDecChar & " appended, len = " & len( cFile ) )
766 if ( len( cFile ) <> 5 ) then
767 warnlog( CFN & "Character not appended" )
770 '///+<li>Return the new filename</li>
771 hNameGen_append() = cFile
777 '*******************************************************************************
779 function hNameGen_lead( iDecChar as long ) as string
781 '///<h3>Create a filename with specified character at the beginning</h3>
784 '///+<li>Character as number (integer)</li>
786 '///+<li>Only positive numbers are allowed</li>
787 '///+<li>Respect integer boundaries</li>
792 '///+<li>A filename containing a special character at the beginning</li>
794 '///<u>Description</u>:
798 const CFN = "hNameGen_lead::"
800 '///+<li>Create a string beginning with ascii char and append "test"</li>
801 cFile = CHR$( iDecChar ) & "test"
802 printlog( CFN & "ASCII " & iDecChar & " prepended, len = " & len( cFile ) )
803 if ( len( cFile ) <> 5 ) then
804 warnlog( CFN & "Character not prepended" )
807 '///+<li>Return the new filename</li>
808 hNamegen_lead() = cFile
814 '*******************************************************************************
816 function hGetRelPath( cStartDir, cFileName as String ) as string
818 '///<h3>Remove the absolute component from a path specification</h3>
819 '///<i>Usually you do not need this function, avoid using it</i><br><br>
822 '///+<li>Absolute part of a filespec (startdirectory) (string)</li>
824 '///+<li>e.g. "/home/oscar/mydir"</li>
826 '///+<li>Fully qualified path to the file/directory (string)</li>
828 '///+<li>e.g. "/home/oscar/mydir/somedir/myfile.txt"</li>
833 '///+<li>Remaining part of the fully qualified path minus startdirectory (string)</li>
835 '///+<li>e.g. "/somedir/myfile.txt"</li>
838 '///<u>Description</u>:
841 '///+<li>Find the end of startdir within the FQ filename</li>
842 hGetRelPath() = mid( cFileName , len( cStartDir + 1 ) )
847 '*******************************************************************************
849 function hFindFileObjectViaFileOpen( cName as string ) as integer
851 '///<h3>Get the position of a filesystem object in filelist of fileopen dialog</h3>
852 '///<i>Note that the filepicker must be open in order to use this function.<br>
853 '///The object with the given name is selected when leaving the function.</i><br>
856 '///+<li>Filename incl. extension (string) but without any pathseparator</li>
860 '///+<li>Position of the object in the filepicker list</li>
862 '///+<li>0: Object not found</li>
863 '///+<li>> 0: Position</li>
866 '///<u>Description</u>:
869 const CFN = "hFindFileObjectViaFileOpen::"
871 dim iCurrentObject as integer
872 dim cCurrentName as string
873 dim iFileOpenItemCount as integer
879 dim iPathItems as integer
880 dim asPathList( 100 ) as string
882 printlog( CFN & "Enter with option (File): " & cName )
884 '///+<li>If a fully qualified path has been provided we have to split it up</li>
885 iPathItems = DirNameList( cPath, asPathList() )
886 'printlog( CFN & "iPathItems (new): " & iPathItems )
888 if ( iPathItems > 3 ) then
889 cName = asPathList( iPathItems )
890 printlog( CFN & "CNAME from plist: " & cName )
892 '///+<li>And walk to the directory</li>
893 cPath = left( cPath , ( len ( cPath ) - len( cName ) ) - 1 )
894 printlog( CFN & "CPATH from plist: " & cPath )
897 if ( OeffnenDlg.exists( 1 ) ) then
898 Dateiname.setText( cPath )
901 warnlog( CFN & "File Open dialog is not open" )
902 hFindFileObjectViaFileOpen() = 0
907 '///+<li>get the number of items in the filepicker</li>
909 iFileOpenItemCount = DateiAuswahl.getItemCount()
911 '///+<li>Jump to the first item in the filelist and select it with SPACE</li>
912 DateiAuswahl.typeKeys( "<HOME>" )
913 DateiAuswahl.typeKeys( "<SPACE>" )
915 '///+<li>Navigate through the list until we find the requested object</li>
916 for iCurrentObject = 1 to iFileOpenItemCount
918 cCurrentName = DateiAuswahl.getSelText()
920 if ( cCurrentName = cName ) then
921 iPos = iCurrentObject
925 DateiAuswahl.typeKeys( "<DOWN>" )
929 '///+<li>Print some info to the log</li>
931 printlog( CFN & "Exit: Found item [" & cName & "] at pos: " & iPos )
933 printlog( CFN & "Exit: Requested item [" & cName & "] was not found." )
936 '///+<li>return the position or 0 on failure</li>
937 hFindFileObjectViaFileOpen() = iPos