# mupdf-changepdf.test --
#
#	Tests about commands for munipulating (changing) PDFs.
#

package require TTXN
  # package under test is tclMuPDF 2.0
package require tclMuPDF 2.0

set thisDir [file dirname [file normalize [info script]]]
set samplesDir [file join $thisDir samples]
set TmpResultDir [file join $thisDir tmpResults]
file mkdir $TmpResultDir
file delete {*}[glob -nocomplain $TmpResultDir/*]


Name: quit
Description: {quit: open and quit}
Test: {
    set pdf [mupdf::open [file join $samplesDir Sample-01.pdf]]
    $pdf quit

    mupdf::isobject $pdf
}
Expected: 0


Name: addsignature.neg.1
Description: {open, change, addsignature (wrong)  and quit}
Setup: {
    # make a working copy of 'Hello.pdf'
    set origFilename [file join $samplesDir Hello.pdf]
    set filename     [file join $TmpResultDir Hello-tmp.pdf]

    file copy -force $origFilename $filename

    set pdf [mupdf::open $filename]
}
Test: {
    $pdf addsigfield "testSignature" 0 100.0 100.00 ;# missing params    
}
Cleanup: {
    $pdf quit
    file delete $filename
}
Expected: error glob {wrong # args: *}


Name: addsignature.neg.2
Description: {open, change, addsignature (duplicated)  and quit}
Setup: {
    # get a pdf with some fields ... make a working copy of it
    set origFilename [file join $samplesDir Form-Fax.pdf]
    set filename     [file join $TmpResultDir tmptmp.pdf]

    file copy -force $origFilename $filename

    set pdf [mupdf::open $filename]
}
Test: {
     # get the name of the 1st field
    set fieldname [lindex [$pdf fields] 0 0]    
    $pdf addsigfield $fieldname 0 100.0 100.00 300.0 200.0    
}
Cleanup: {
    $pdf quit
    file delete $filename
}
Expected: error {Duplicated field name}


Name: addsignature
Description: {open, change, addsignature and save}
Setup: {
    # get a pdf with some fields ... make a working copy of it
    set origFilename [file join $samplesDir Form-Fax.pdf]
    set filename     [file join $TmpResultDir tmptmp.pdf]

    file copy -force $origFilename $filename
}
Test: {
    set pdf [mupdf::open $filename]
    $pdf addsigfield "testSignature" 0 100.0 100.00 300.0 200.0
    $pdf close   
     # now reopen the changed file and check the new field is present     
    set pdf [mupdf::open $filename]
    set L [$pdf fields]
    set fieldInfo [lsearch -inline -index 0 $L "testSignature"]      
    $pdf quit
    
    return $fieldInfo
}
Cleanup: {
    file delete $filename
}
Expected: {testSignature signature {}}


Name: change-export-change-close
Description: {open, change, export, change, close}
Setup: {
    # get a pdf with some fields ... make a working copy of it
    set origFilename [file join $samplesDir Sample-01.pdf]
    set filename     [file join $TmpResultDir tmptmp.pdf]
    set filename2    [file join $TmpResultDir tmptmp2.pdf]
    
    file copy -force $origFilename $filename
}
Test: {
    set pdf [mupdf::open $filename]
    $pdf addsigfield "testSignature1" 0 100.0 100.00 300.0 200.0
    $pdf export $filename2 
    $pdf addsigfield "testSignature2" 0 100.0 400.00 300.0 500.0
    $pdf close    
     # now reopen the changed file and check the new field is present     
    set pdf [mupdf::open $filename]
    set L [$pdf fields]
    set fields {}
    lappend fields [lsearch -inline -index 0 $L "testSignature1"]      
    lappend fields [lsearch -inline -index 0 $L "testSignature2"]      
    $pdf quit
    
    return $fields
}
Cleanup: {
    file delete $filename
    file delete $filename2
}
Expected: {{testSignature1 signature {}} {testSignature2 signature {}}}

Name: BUGFIX-change-change-export-close
    #  NOTE: before bugfixing, result was {} {}  (.e.g no changes saved on closing (since changes have been saved with export))
Description: {open, change, export, DON'T change, close}
Setup: {
    # get a pdf with some fields ... make a working copy of it
    set origFilename [file join $samplesDir Sample-01.pdf]
    set filename     [file join $TmpResultDir tmptmp.pdf]
    set filename2    [file join $TmpResultDir tmptmp2.pdf]
    
    file copy -force $origFilename $filename
}
Test: {
    set pdf [mupdf::open $filename]
    $pdf addsigfield "testSignature1" 0 100.0 100.00 300.0 200.0
    $pdf addsigfield "testSignature2" 0 100.0 400.00 300.0 500.0
    $pdf export $filename2 
    $pdf close    
     # now reopen the changed file and check the new field is present     
    set pdf [mupdf::open $filename]
    set L [$pdf fields]
    set fields {}
    lappend fields [lsearch -inline -index 0 $L "testSignature1"]      
    lappend fields [lsearch -inline -index 0 $L "testSignature2"]      
    $pdf quit
    
    return $fields
}
Cleanup: {
    file delete $filename
    file delete $filename2
}
Expected: {{testSignature1 signature {}} {testSignature2 signature {}}}




Name: BUGFIX-error_on_closing
    #  NOTE: before bugfixing, due to some external error (e.g. file locked) document was closed without saving !
Description: {open (a locked file), change and close/save}
Only: IF_YOU_CAN_LOCK_A_FILE
  #----------------------------------------------------------------
  #... In order to reproduce this case, an external application would be required ...
  #... Missing this ability, this testcase cannot be run ....
  #
  #... NOTE: tested 'manually'  ok
#----------------------------------------------------------------
Setup: {
    # get a pdf with some fields ... make a working copy of it
    set origFilename [file join $samplesDir Sample-01.pdf]
    set filename     [file join $TmpResultDir tmptmp.pdf]
    
    file copy -force $origFilename $filename

     # simulate a locking app on the same file (e.g Acrobat )
    set f [open $filename a+]
###  !!  NOO  this won't  lock the file  !!!
### AFAIK there is no way to lock a file in tclsh (neither launching tclsh in another process)
### The ability to set an exclusive lock is a feature of some apps (Acrobat, Excel , ...)
### that cannot be replicated in an automated test environment
### e.g. even if I presume Acrobat is installed in every test-environment, and 
###    Acrobat is *strongly recommended* for testing this project's results, 
###    how can I start/stop Acrobat from a script  (...start is easy.., stop is difficult ..) ?
}
Test: {
    set pdf [mupdf::open $filename]
    $pdf addsigfield "testSignature1" 0 100.0 100.00 300.0 200.0
     # since the file is locked by another app, changes cannot be saved,
     # then 'close' should fail (without 'quitting $pdf')
    set err [catch { $pdf close }]
     # check $pdf is still alive
    set isAlive [mupdf::isobject $pdf]
    return [list $err $isAlive]
}
Cleanup: {
    if { [mupdf::isobject $pdf] } { $pdf quit }
    close $f ;#  release the app lock
    file delete $filename
}
Expected: {1 1}


Name: change.and.quit
Description: {quit: open change and quit}
Test: {
    set filename [file join $samplesDir Hello.pdf]
    set preModTime [file mtime $filename]
    set pdf [mupdf::open $filename]
    $pdf addsigfield "testSignature" 0 100.0 100.00 300.0 200.0    
    $pdf quit
    set postModTime [file mtime $filename]
    
    expr {$preModTime == $postModTime}
}
Expected: 1

Name: haschanges
Description: {open, change, check and quit}
Setup: {
    # make a working copy of 'Hello.pdf'
    set origFilename [file join $samplesDir Hello.pdf]
    set filename     [file join $TmpResultDir Hello-tmp.pdf]
    set filename2    [file join $TmpResultDir Hello-tmp2.pdf]

    file copy -force $origFilename $filename

    set pdf [mupdf::open $filename]
}
Test: {
    set L {}
    lappend L [$pdf haschanges]
    $pdf addsigfield "testSignature" 0 100.0 100.00 300.0 200.0    
    lappend L [$pdf haschanges]
    $pdf export $filename2
    lappend L [$pdf haschanges] 
    return $L
}
Cleanup: {
    $pdf quit
    file delete $filename
    file delete $filename2
}
Expected: {0 1 1}


# === cleanup ========================================
::tcltest::cleanupTests