2022-01-15 21:17:38 +00:00

12 lines
4.5 KiB
Go

//
// Copyright 2020 FoxyUtils ehf. All rights reserved.
//
// This is a commercial product and requires a license to operate.
// A trial license can be obtained at https://unidoc.io
//
// DO NOT EDIT: generated by unitwist Go source code obfuscator.
//
// Use of this source code is governed by the UniDoc End User License Agreement
// terms that can be accessed at https://unidoc.io/eula/
package testutils ;import (_fg "crypto/md5";_bd "encoding/hex";_cf "errors";_aa "fmt";_ad "image";_cb "image/png";_a "io";_bc "os";_c "os/exec";_d "path/filepath";_g "strings";_b "testing";);func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_fgc ,_aff :=HashFile (file1 );if _aff !=nil {return false ,_aff ;};_ed ,_aff :=HashFile (file2 );if _aff !=nil {return false ,_aff ;};if _fgc ==_ed {return true ,nil ;};_fb ,_aff :=ReadPNG (file1 );if _aff !=nil {return false ,_aff ;};_de ,_aff :=ReadPNG (file2 );if _aff !=nil {return false ,_aff ;};if _fb .Bounds ()!=_de .Bounds (){return false ,nil ;};return CompareImages (_fb ,_de );};func CopyFile (src ,dst string )error {_ce ,_ab :=_bc .Open (src );if _ab !=nil {return _ab ;};defer _ce .Close ();_cea ,_ab :=_bc .Create (dst );if _ab !=nil {return _ab ;};defer _cea .Close ();_ ,_ab =_a .Copy (_cea ,_ce );return _ab ;};func ReadPNG (file string )(_ad .Image ,error ){_bdc ,_adb :=_bc .Open (file );if _adb !=nil {return nil ,_adb ;};defer _bdc .Close ();return _cb .Decode (_bdc );};var (ErrRenderNotSupported =_cf .New ("\u0072\u0065\u006e\u0064\u0065r\u0069\u006e\u0067\u0020\u0050\u0044\u0046\u0020\u0066\u0069\u006c\u0065\u0073 \u0069\u0073\u0020\u006e\u006f\u0074\u0020\u0073\u0075\u0070\u0070\u006f\u0072\u0074\u0065\u0064\u0020\u006f\u006e\u0020\u0074\u0068\u0069\u0073\u0020\u0073\u0079\u0073\u0074\u0065m"););func CompareImages (img1 ,img2 _ad .Image )(bool ,error ){_be :=img1 .Bounds ();_ff :=0;for _gb :=0;_gb < _be .Size ().X ;_gb ++{for _cfa :=0;_cfa < _be .Size ().Y ;_cfa ++{_af ,_ge ,_gbg ,_ :=img1 .At (_gb ,_cfa ).RGBA ();_e ,_eb ,_fa ,_ :=img2 .At (_gb ,_cfa ).RGBA ();if _af !=_e ||_ge !=_eb ||_gbg !=_fa {_ff ++;};};};_bfb :=float64 (_ff )/float64 (_be .Dx ()*_be .Dy ());if _bfb > 0.0001{_aa .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_bfb ,_ff );return false ,nil ;};return true ,nil ;};func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_ga :=_c .LookPath ("\u0067\u0073");_ga !=nil {return ErrRenderNotSupported ;};return _c .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_aa .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();};func RunRenderTest (t *_b .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_db :=_g .TrimSuffix (_d .Base (pdfPath ),_d .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_edb *_b .T ){_cg :=_d .Join (outputDir ,_db );_cfc :=_cg +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _ef :=RenderPDFToPNGs (pdfPath ,0,_cfc );_ef !=nil {_edb .Skip (_ef );};for _cd :=1;true ;_cd ++{_dg :=_aa .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_cg ,_cd );_bee :=_d .Join (baselineRenderPath ,_aa .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_db ,_cd ));if _ ,_bcc :=_bc .Stat (_dg );_bcc !=nil {break ;};_edb .Logf ("\u0025\u0073",_bee );if saveBaseline {_edb .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_dg ,_bee );_gcc :=CopyFile (_dg ,_bee );if _gcc !=nil {_edb .Fatalf ("\u0045\u0052\u0052OR\u0020\u0063\u006f\u0070\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0025\u0073\u003a\u0020\u0025\u0076",_bee ,_gcc );};continue ;};_edb .Run (_aa .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_cd ),func (_gcg *_b .T ){_gcg .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_dg ,_bee );_ac ,_faf :=ComparePNGFiles (_dg ,_bee );if _bc .IsNotExist (_faf ){_gcg .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_ac {_gcg .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");};});};});};func HashFile (file string )(string ,error ){_bdce ,_ca :=_bc .Open (file );if _ca !=nil {return "",_ca ;};defer _bdce .Close ();_gc :=_fg .New ();if _ ,_ca =_a .Copy (_gc ,_bdce );_ca !=nil {return "",_ca ;};return _bd .EncodeToString (_gc .Sum (nil )),nil ;};