# mupdf-export.test --
#
#	Tests about commands for saving/exporting PDFs.
#

package require TTXN
  # package under test is tclMuPdf 2.1.1
package require tclMuPDF 2.1.1

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: change.and.close-save
Description: {open, change and close/save}
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
}
Test: {
     # .. working on $filename ..
    set preModTime [file mtime $filename]
    set pdf [mupdf::open $filename]
    $pdf addsigfield "testSignature" 0 100.0 100.00 300.0 200.0    
    $pdf close
    set postModTime [file mtime $filename]
    
    expr {$preModTime < $postModTime}
}
Cleanup: {
    file delete $filename
}
Expected: 1


Name: export.neg.1
Description: {export: wrong args}
Setup: {
    set filename [file join $samplesDir Hello.pdf]
    set pdf [mupdf::open $filename]
}
Test: {
    $pdf export
}
Cleanup: {
    $pdf quit
}
Expected: error glob {wrong # args: *}

Name: export.plain
Description: {export: simply export the original file}
Setup: {
    set filename [file join $samplesDir Hello.pdf]
    set exportedFilename [file join $TmpResultDir copyOfHello.pdf]
    set pdf [mupdf::open $filename]
}
Test: {
    $pdf export $exportedFilename
    expr {[file size $filename] == [file size $exportedFilename]} 
}
Cleanup: {
    $pdf quit
    file delete $exportedFilename
}
Expected: 1

Name: export.changes
Description: {export: export the original file plus changes}
Setup: {
    set filename [file join $samplesDir Hello.pdf]
    set exportedFilename [file join $TmpResultDir copyOfHello.pdf]
    set pdf [mupdf::open $filename]
}
Test: {
	$pdf addsigfield "testSignature" 0 10 20 30 40
    $pdf export $exportedFilename

     # now reopen the exportedfile and check the new field is present     
    set pdf2 [mupdf::open $exportedFilename]
    set L [$pdf2 fields]
    set fieldInfo [lsearch -inline -index 0 $L "testSignature"]      
    $pdf2 quit
    
    return $fieldInfo
}
Cleanup: {
    $pdf quit
    file delete $exportedFilename
}
Expected: {testSignature signature {}}


Name: export.changes_twice
Description: {export: export the original file plus changes, twice}
Setup: {
    set filename [file join $samplesDir Hello.pdf]
    set exportedFilename1 [file join $TmpResultDir copyOfHello1.pdf]
    set exportedFilename2 [file join $TmpResultDir copyOfHello2.pdf]
    set pdf [mupdf::open $filename]
}
Test: {
    $pdf addsigfield "testSignature1" 0 10 20 30 40
    $pdf export $exportedFilename1
    
    $pdf addsigfield "testSignature2" 0 100 200 300 400
    $pdf export $exportedFilename2

     # now reopen the exportedfile2 and check both fields are present
    set pdf2 [mupdf::open $exportedFilename2]
    set L [$pdf2 fields]
    set fieldInfo [lsearch -inline -all -index 0 $L "testSignature*"]      
    $pdf2 quit
    
    return $fieldInfo
}
Cleanup: {
    $pdf quit
    file delete $exportedFilename1
    file delete $exportedFilename1
}
Expected: {{testSignature1 signature {}} {testSignature2 signature {}}}


 # BUGFIX: several (incremental) export may corrupt the file ..
 # - Fixed using pdf_save_snapshot() for incremental save
 #   Fixed in tclMuPDF 2.1.1
Name: BUG.export.changes_twice
Description: {export: export the original file plus changes, twice}
Setup: {
    set filename [file join $samplesDir Form-coffee.pdf]
    set exportedFilename1 [file join $TmpResultDir copyOfFormCoffe1.pdf]
    set exportedFilename2 [file join $TmpResultDir copyOfFormCoffee2.pdf]
    set pdf [mupdf::open $filename]
}
Test: {
	$pdf field "box1"  "HELLO"
	$pdf field "box2"  "WORLD!"
    $pdf export $exportedFilename1

	$pdf flatten "box1" "box2"
    $pdf export $exportedFilename2

     # now reopen the exportedfile2
    set pdf2 [mupdf::open $exportedFilename2]
    set wasrepaired [$pdf2 wasrepaired]
    set warnings [$pdf2 warnings]
    $pdf2 quit
    
    return [list $wasrepaired $warnings]
}
Cleanup: {
    $pdf quit
    file delete $exportedFilename1
    file delete $exportedFilename1
}
Expected: {0 {}}


Name: export.badfile
Description: {export: export to a bad filename}
Setup: {
    set filename [file join $samplesDir Hello.pdf]
    set pdf [mupdf::open $filename]
}
Test: {
     # save to a non existing directory !
    $pdf export "x/x/x/x/impossible.pdf"
}
Cleanup: {
    $pdf quit
}
Expected: error glob {cannot open *}


Name: export.badfile.2
Description: {export: export to itself (even with a different path-spec ..}
Setup: {
    set filename [file join $samplesDir Hello.pdf]
    set pdf [mupdf::open $filename]
    set currDir [pwd]   
}
Test: {
    set fullPath [file normalize $filename]
    set fullDir [file dirname $fullPath]
    set tailDir [file tail $fullDir]
    set parentFullDir [file dirname $fullDir]
    
    cd $parentFullDir
       # the following [file join ..] refers to the same file,
       # with a different relative path after the currDir has changed ..
    $pdf export [file join $tailDir .. $tailDir Hello.pdf]
	return ok 
}
Cleanup: {
    $pdf quit
    cd $currDir
}
Expected: ok

Name: export.badfile.3
Description: {export: exporting to an already opened pdf ...}
Setup: {
    set filename [file join $samplesDir Hello.pdf]
    set pdf [mupdf::open $filename]
    set filename2 [file join $samplesDir Book.pdf]
    set pdf2 [mupdf::open $filename2]
}
Test: {
    $pdf export $filename2
}
Cleanup: {
    $pdf quit
    $pdf2 quit
}
Expected: error {cannot overwrite an opened PDF-file}


Name: export.badfile.4
Description: {export: exporting to the same file (with case letters changed) ...}
Setup: {
    set filename [file join $samplesDir Hello.pdf]
    set pdf [mupdf::open $filename]
}
Test: {
    $pdf addsigfield "testSignature" 0 10 20 30 40  ; # this forces a real pdf_save
    $pdf export [file join $TmpResultDir Hello-tmp.pdf]
    $pdf export [file join $TmpResultDir HELLO-tMp.pdf]
    #  NOTE: before bugfixing, the above operation reported an error like the following:
    #   error copying "..../Hello-tmp.pdf" to "..../HELLO-tMp.pdf"
    return OK
}
Cleanup: {
    $pdf quit
    file delete [file join $TmpResultDir Hello-tmp.pdf]
}
Expected: OK

 # this tests checks for a fix of the pdf_save_document occorring in mupdf 1.11
 # on Windows-32bit
 
 # NOO !useless test.
 # mupdf won't raise an error if file is damaged (just 'prints' some errors')
 #
Name: export_and_reload
Description: {export: export and then reload the result ...}
Test: {
    set filename [file join $samplesDir Book.pdf]
    set exportfilename [file join $TmpResultDir Book-signed.pdf]
    file delete $exportfilename
    set pdf [mupdf::open $filename]
    $pdf addsigfield "testSignature" 0 10 20 30 40   
    $pdf export $exportfilename
    $pdf quit

    set pdf [mupdf::open $exportfilename]
    return "ok"
}
Cleanup: {
    $pdf quit
}
Expected: ok


  ### TO DO ...   exporting over locked files ....



# ============================================================================


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