# ============================================================================
# mupdf-fields.test --
#
#	Tests about updating form fields.
#

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: set_field.neg.1
Description: {set field - wrong # args}
Setup: { set pdf [mupdf::open [file join $samplesDir Form-Fax.pdf]] }
Test: {
    $pdf field "From.Name" value1 value2
}
Cleanup: {$pdf quit}
Expected: error glob "wrong # args: *"


Name: set_field.neg.2
Description: {set field - wrong field name}
Setup: { set pdf [mupdf::open [file join $samplesDir Form-Fax.pdf]] }
Test: {
    $pdf field "From.NameXXX" bbb
}
Cleanup: {$pdf quit}
Expected: error glob {unknown field *}


 # =================================================================
 # WARNING: You also need a visual testing !!!
 
 # still limited to text fields ...  checkboxes and other widgets still have problems ...
 # =================================================================

Name: set_field.accented_letters
Description: { set field - value with accented letters and other utf8 chars }
Setup: { set pdf [mupdf::open [file join $samplesDir Form-Fax.pdf]] }
Test: {
    set newValue "first line: àèìòù\n...\nlast line: price is € 1.20"
    $pdf field "Message.Comments" $newValue
    expr {$newValue eq [$pdf field "Message.Comments"]}
}
Cleanup: {$pdf quit}
Expected: 1


# WARNING: currently (Nov2018) MuPDF only supports Latin Alphabet.
#  See MuPDF BUG 696478 .
#  .. "We can currently fill out the form fields in any language, 
#      but the display code still only handles the Latin alphabet.
# This means that you can enter text in a form field in any language
# (tested with Russian, Chinee, Greek, ...) but the rendered text (the appearance is wrong )
# See the following test ...
Name: set_field.other.alphabets

Description: { set field - words from other alphabets }
Setup: { set pdf [mupdf::open [file join $samplesDir Form-coffee.pdf]] }
Test: {
    set newValue1 "Russian coffee: \u0420\u0443\u0441\u0441\u043A\u0438\u0439 \u043A\u043E\u0444\u0435"
      # the above string should read "Russian coffee: Русский кофе" .
      # I entered the \uXXXX notation just for safety; some editors will corrupt these strings.
    $pdf field box1 $newValue1
    set newValue2 "Chinese coffe:  \u4E2D\u570B\u5496\u5561"   
     # the above string should read "Chinese coffe: 中國咖啡"     
    $pdf field box2 $newValue2

## WARNING: the above command produces some mupdf internal ..  "warnings" like these:
#  error: cannot find builtin CJK font
#  warning: cannot create appearance stream

    $pdf field box1
    
## WARNING: if you export results, rendering of the non-latin alphabet is wrong !
    # $pdf export $TmpResultDir/coffee.pdf   
}
Cleanup: {$pdf quit}
Expected: "Russian coffee: \u0420\u0443\u0441\u0441\u043A\u0438\u0439 \u043A\u043E\u0444\u0435"


Name: set_field.other.alphabets.save.and.relead
Description: { set field - words from other alphabets - save and reload }
Test: {
    set pdf [mupdf::open [file join $samplesDir Form-coffee.pdf]]
    set newValue1 "Greek coffee: \u0395\u03BB\u03BB\u03B7\u03BD\u03B9\u03BA\u03CC\u03C2 \u03BA\u03B1\u03C6\u03AD\u03C2"
      # the above string should read "Greek coffee:  Ελληνικός καφές" .
      # I entered the \uXXXX notation just for safety; some editors will corrupt these strings.
    $pdf field box1 $newValue1
    $pdf export [file join $TmpResultDir newCoffee.pdf]
    $pdf quit
    
    set pdf [mupdf::open [file join $TmpResultDir newCoffee.pdf]]
    $pdf field box1
    
## WARNING: rendering of the non-latin alphabet is wrong !
  
}
Cleanup: {$pdf quit}
Expected: "Greek coffee: \u0395\u03BB\u03BB\u03B7\u03BD\u03B9\u03BA\u03CC\u03C2 \u03BA\u03B1\u03C6\u03AD\u03C2"



Name: set_field.multiline
Description: { set field - value with accented letters and other utf8 chars }
Setup: { set pdf [mupdf::open [file join $samplesDir Form-Fax.pdf]] }
Test: {
    set newValue "first line: àèìòù\n...\nlast line: price is € 1.20"
    $pdf field "Message.Comments" $newValue
    expr {$newValue eq [$pdf field "Message.Comments"]}
}
Cleanup: {$pdf quit}
Expected: 1

 
Name: set_field.update_duplicated_fields
Description: {set field - update duplicated fields}
Setup: { set pdf [mupdf::open [file join $samplesDir Form-DuplicatedFields1.pdf]] }
Test: {
    set v1 "new value for field1"
    set v2 "new value for field2"
    
    $pdf field "FIELD2" $v2
    $pdf field "FIELD1" $v1
    set  pdfOut $TmpResultDir/NewForm.pdf
    $pdf export $pdfOut
    $pdf quit
     # reopen and check
    set pdf [mupdf::open $pdfOut]
    set vv1 [$pdf field FIELD1]
    set vv2 [$pdf field FIELD2]
    $pdf quit
    lmap oldValue [list $v1 $v2] newValue [list $vv1 $vv2] { expr {$oldValue eq $newValue} }
}
Cleanup: { 
    file delete $pdfOut
}
Expected: {1 1}


Name: get_field_attrib
Description: { get field attributes }
Setup: { set pdf [mupdf::open [file join $samplesDir Form-Fax.pdf]] }
Test: {
    $pdf fieldattrib "From.Name"
} 
Cleanup: { 
    $pdf quit
}
Expected: {-readonly 0}


Name: set_field_attrib
Description: { set field attributes }
Test: {
    set pdf [mupdf::open [file join $samplesDir Form-Fax.pdf]]
    $pdf fieldattrib "From.Name" -readonly true
    set oldVal [$pdf field "From.Name"]
    set haschanges [$pdf haschanges]
     # even if field is readonly, changes by-program are allowed.
    $pdf field "From.Name" "ABCDEFG"
    $pdf export $TmpResultDir/tmp.pdf
    $pdf quit
    
    # reopen and check changes
    set pdf [mupdf::open $TmpResultDir/tmp.pdf]
    set attribs [$pdf fieldattrib "From.Name"]
    set res2 [$pdf field "From.Name"]
    $pdf quit
    
    return [list $oldVal $haschanges $attribs $res2]    
} 
Cleanup: { 
    file delete $TmpResultDir/tmp.pdf
}
Expected: {BBBB 1 {-readonly 1} ABCDEFG}


Name: flatten_field
Description: { flatten a field }
Test: {
    set pdf [mupdf::open [file join $samplesDir Form-Fax.pdf]]
    set n0 [llength [$pdf fields]]
    $pdf flatten "From.Name" "To.Name"
    set n1 [llength [$pdf fields]]
    list $n0 $n1
} 
Cleanup: { 
    $pdf quit
}
Expected: {15 13}


Name: flatten_field_and_save
Description: { flatten a field and save}
Test: {
    set pdf [mupdf::open [file join $samplesDir Form-Fax.pdf]]
    $pdf flatten "From.Name"
    $pdf export $TmpResultDir/tmp.pdf
    $pdf quit
    
    # reopen and check changes ("From.Name" should be an unknown field")
    set pdf [mupdf::open $TmpResultDir/tmp.pdf]
    $pdf field "From.Name"
     #.. should raise an error
} 
Cleanup: { 
    $pdf quit
    file delete $TmpResultDir/tmp.pdf
}
Expected: error glob "unknown field *"


Name: flatten_all_fields_and_save2
Description: { flatten all fields and save}
Test: {
    set pdf [mupdf::open [file join $samplesDir Form-DuplicatedFields2.pdf]]
    $pdf field FIELD1 "NEW VALUE 1"
    $pdf field FIELD2 "NEW VALUE 2"
    $pdf flatten FIELD2 FIELD1 FIELD1 FIELD1 FIELD1   ;# note duplicated fieldnames
    $pdf export $TmpResultDir/tmp.pdf
    $pdf quit
    
    # reopen and check changes (no fields)
    set pdf [mupdf::open $TmpResultDir/tmp.pdf]
    $pdf fields
} 
Cleanup: { 
    $pdf quit
    file delete $TmpResultDir/tmp.pdf
}
Expected: {}

Name: flatten.composite.fields
Description: { flatten composite fields and save }
Test: {
    set pdf [mupdf::open [file join $samplesDir Form-Fax.pdf]]

    set fields [$pdf fields names]
    set To_fields [lsearch -inline -all -glob $fields "To.*"]
    set Msg_fields [lsearch -inline -all -glob $fields "Message.*"]
     # flatten all To.* fields and all Message.* fields but the first        
    $pdf flatten {*}$To_fields {*}[lrange $Msg_fields 1 end]
    $pdf export $TmpResultDir/tmp.pdf
    $pdf quit
    
    # reopen and check changes
    set pdf [mupdf::open $TmpResultDir/tmp.pdf]
    set fields [$pdf fields]
    set To_fields [lsearch -inline -all -glob $fields "To.*"]
    set Msg_fields [lsearch -inline -all -glob $fields "Message.*"]
    set res {}
    lappend res [llength $To_fields]
    lappend res [llength $Msg_fields]
}
Cleanup: { 
    $pdf quit
    file delete $TmpResultDir/tmp.pdf
}
Expected: {0 1}


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



