github.com/pdfcpu/pdfcpu@v0.11.1/pkg/api/test/images_test.go (about) 1 /* 2 Copyright 2024 The pdf Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package test 18 19 import ( 20 "path/filepath" 21 "testing" 22 23 "github.com/pdfcpu/pdfcpu/pkg/api" 24 ) 25 26 func testUpdateImages(t *testing.T, msg string, inFile, imgFile, outFile string, objNr, pageNr int, id string) { 27 t.Helper() 28 29 if err := api.UpdateImagesFile(inFile, imgFile, outFile, objNr, pageNr, id, conf); err != nil { 30 t.Fatalf("%s %s: %v\n", msg, outFile, err) 31 } 32 if err := api.ValidateFile(outFile, conf); err != nil { 33 t.Fatalf("%s: %v\n", msg, err) 34 } 35 } 36 37 func TestUpdateImages(t *testing.T) { 38 39 outDir := filepath.Join(samplesDir, "images") 40 inDir := outDir 41 42 for _, tt := range []struct { 43 msg string 44 inFile string 45 imgFile string 46 outFile string 47 objNr int // by objNr 48 pageNr int // or by (pageNr, id) 49 id string 50 }{ 51 {"TestUpdateByObjNr", 52 "test.pdf", 53 "test_1_Im1.png", 54 "ImageUpdatedByObjNr.pdf", 55 8, 56 0, 57 ""}, 58 59 {"TestUpdateByPageNrAndId", 60 "test.pdf", 61 "test_1_Im1.png", 62 "imageUpdatedByPageNrAndIdPage1.pdf", 63 0, 64 1, 65 "Im1"}, 66 67 {"TestUpdateByPageNrAndId", 68 "test.pdf", 69 "test_1_Im1.png", 70 "imageUpdatedByPageNrAndIdPage2.pdf", 71 0, 72 2, 73 "Im1"}, 74 75 {"TestUpdateByImageFileName", 76 "test.pdf", 77 "test_1_Im1.png", 78 "imageUpdatedByFileName.pdf", 79 0, 80 0, 81 ""}, 82 83 {"TestUpdateByPageNrAndId", 84 "test.pdf", 85 "any.png", 86 "imageUpdatedByPageNrAndIdAny.pdf", 87 0, 88 1, 89 "Im1"}, 90 91 {"TestUpdateByObjNrPNG", 92 "test.pdf", 93 "any.png", 94 "imageUpdatedByObjNrPNG.pdf", 95 8, 96 0, 97 ""}, 98 99 {"TestUpdateByObjNrJPG", 100 "test.pdf", 101 "any.jpg", 102 "imageUpdatedByObjNrJPG.pdf", 103 8, 104 0, 105 ""}, 106 107 {"TestUpdateByObjNrTIFF", 108 "test.pdf", 109 "any.tiff", 110 "imageUpdatedByObjNrTIFF.pdf", 111 8, 112 0, 113 ""}, 114 115 {"TestUpdateByObjNrWEBP", 116 "test.pdf", 117 "any.webp", 118 "imageUpdatedByObjNrWEBP.pdf", 119 8, 120 0, 121 ""}, 122 {"TestUpdateByObjNrPNGGray", 123 "test.pdf", 124 "any_gray.png", 125 "imageUpdatedByObjNrPNGGray.pdf", 126 8, 127 0, 128 ""}, 129 {"TestUpdateByPageNrAndIdGray", 130 "test.pdf", 131 "any_gray.png", 132 "imageUpdatedByPageNrAndIdAnyGray.pdf", 133 0, 134 1, 135 "Im1"}, 136 } { 137 testUpdateImages(t, tt.msg, 138 filepath.Join(inDir, tt.inFile), 139 filepath.Join(outDir, tt.imgFile), 140 filepath.Join(outDir, tt.outFile), 141 tt.objNr, 142 tt.pageNr, 143 tt.id) 144 } 145 }