Skip to content

Commit

Permalink
Implement tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jerbob92 committed Feb 6, 2022
1 parent 747ff2f commit bdba065
Show file tree
Hide file tree
Showing 8 changed files with 1,064 additions and 27 deletions.
34 changes: 15 additions & 19 deletions internal/implementation/fpdfview.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,12 @@ func (p *PdfiumImplementation) FPDF_RenderPageBitmap(request *requests.FPDF_Rend
p.Lock()
defer p.Unlock()

bitmapHandle, err := p.getBitmapHandle(request.Bitmap)
pageHandle, err := p.loadPage(request.Page)
if err != nil {
return nil, err
}

pageHandle, err := p.loadPage(request.Page)
bitmapHandle, err := p.getBitmapHandle(request.Bitmap)
if err != nil {
return nil, err
}
Expand All @@ -520,12 +520,12 @@ func (p *PdfiumImplementation) FPDF_RenderPageBitmapWithMatrix(request *requests
p.Lock()
defer p.Unlock()

bitmapHandle, err := p.getBitmapHandle(request.Bitmap)
pageHandle, err := p.loadPage(request.Page)
if err != nil {
return nil, err
}

pageHandle, err := p.loadPage(request.Page)
bitmapHandle, err := p.getBitmapHandle(request.Bitmap)
if err != nil {
return nil, err
}
Expand All @@ -548,7 +548,7 @@ func (p *PdfiumImplementation) FPDF_RenderPageBitmapWithMatrix(request *requests

C.FPDF_RenderPageBitmapWithMatrix(bitmapHandle.handle, pageHandle.handle, &matrix, &clipping, C.int(request.Flags))

return nil, nil
return &responses.FPDF_RenderPageBitmapWithMatrix{}, nil
}

// FPDF_DeviceToPage converts the screen coordinates of a point to page coordinates.
Expand Down Expand Up @@ -794,7 +794,7 @@ func (p *PdfiumImplementation) FPDFBitmap_Destroy(request *requests.FPDFBitmap_D

delete(p.bitmapRefs, bitmapHandle.nativeRef)

return nil, nil
return &responses.FPDFBitmap_Destroy{}, nil
}

// FPDF_VIEWERREF_GetPrintScaling returns whether the PDF document prefers to be scaled or not.
Expand Down Expand Up @@ -874,6 +874,10 @@ func (p *PdfiumImplementation) FPDF_VIEWERREF_GetPrintPageRangeElement(request *
}

value := C.FPDF_VIEWERREF_GetPrintPageRangeElement(pageRangeHandle.handle, C.size_t(request.Index))
if int(value) == -1 {
return nil, errors.New("could not load page range element")
}

return &responses.FPDF_VIEWERREF_GetPrintPageRangeElement{
Value: int(value),
}, nil
Expand Down Expand Up @@ -911,19 +915,14 @@ func (p *PdfiumImplementation) FPDF_VIEWERREF_GetName(request *requests.FPDF_VIE
// First get the metadata length.
nameSize := C.FPDF_VIEWERREF_GetName(documentHandle.handle, cstr, nil, 0)
if nameSize == 0 {
return nil, errors.New("Could not get name")
return nil, errors.New("could not get name")
}

charData := make([]byte, uint64(nameSize))
C.FPDF_VIEWERREF_GetName(documentHandle.handle, cstr, (*C.char)(unsafe.Pointer(&charData[0])), C.ulong(len(charData)))

transformedText, err := p.transformUTF16LEToUTF8(charData)
if err != nil {
return nil, err
}

return &responses.FPDF_VIEWERREF_GetName{
Name: transformedText,
Value: string(charData[:len(charData)-1]), // Remove nil terminator
}, nil
}

Expand Down Expand Up @@ -1044,13 +1043,9 @@ func (p *PdfiumImplementation) FPDF_GetXFAPacketName(request *requests.FPDF_GetX
return nil, errors.New("could not get name of the XFA packet")
}

transformedText, err := p.transformUTF16LEToUTF8(charData)
if err != nil {
return nil, err
}

return &responses.FPDF_GetXFAPacketName{
Name: transformedText,
Index: request.Index,
Name: string(charData[:len(charData)-1]),
}, nil
}

Expand Down Expand Up @@ -1085,6 +1080,7 @@ func (p *PdfiumImplementation) FPDF_GetXFAPacketContent(request *requests.FPDF_G
}

return &responses.FPDF_GetXFAPacketContent{
Index: request.Index,
Content: contentData,
}, nil
}
4 changes: 3 additions & 1 deletion requests/render.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package requests

import "github.com/klippa-app/go-pdfium/enums"
import (
"github.com/klippa-app/go-pdfium/enums"
)

type RenderPageInDPI struct {
Page Page
Expand Down
6 changes: 4 additions & 2 deletions responses/fpdfview.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ type FPDF_VIEWERREF_GetDuplex struct {
}

type FPDF_VIEWERREF_GetName struct {
Name string
Value string
}

type FPDF_CountNamedDests struct {
Expand All @@ -206,9 +206,11 @@ type FPDF_GetXFAPacketCount struct {
}

type FPDF_GetXFAPacketName struct {
Name string
Index int
Name string
}

type FPDF_GetXFAPacketContent struct {
Index int
Content []byte
}
4 changes: 3 additions & 1 deletion responses/render.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package responses

import "image"
import (
"image"
)

type RenderPage struct {
Page int // The rendered page number (0-index based).
Expand Down
9 changes: 9 additions & 0 deletions shared_tests/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,16 @@ var _ = Describe("document", func() {
Expect(err).To(MatchError(errors.ErrPassword.Error()))
Expect(doc).To(BeNil())
})

It("returns the password error", func() {
doc, err := PdfiumInstance.FPDF_LoadMemDocument64(&requests.FPDF_LoadMemDocument64{
Data: &pdfData,
})
Expect(err).To(MatchError(errors.ErrPassword.Error()))
Expect(doc).To(BeNil())
})
})

When("is opened with the wrong password", func() {
It("returns the password error", func() {
wrongPassword := "test"
Expand Down
18 changes: 14 additions & 4 deletions shared_tests/fpdf_thumbnail.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ var _ = Describe("fpdf_thumbnail", func() {
Expect(FPDF_CloseDocument).To(Not(BeNil()))
})

It("returns no decoded thumbnail data", func() {
It("returns decoded thumbnail data", func() {
FPDFPage_GetDecodedThumbnailData, err := PdfiumInstance.FPDFPage_GetDecodedThumbnailData(&requests.FPDFPage_GetDecodedThumbnailData{
Page: requests.Page{
ByIndex: &requests.PageByIndex{
Expand All @@ -132,7 +132,7 @@ var _ = Describe("fpdf_thumbnail", func() {
Expect(FPDFPage_GetDecodedThumbnailData.Thumbnail).To(HaveLen(1138))
})

It("returns no raw thumbnail data", func() {
It("returns raw thumbnail data", func() {
FPDFPage_GetRawThumbnailData, err := PdfiumInstance.FPDFPage_GetRawThumbnailData(&requests.FPDFPage_GetRawThumbnailData{
Page: requests.Page{
ByIndex: &requests.PageByIndex{
Expand All @@ -146,7 +146,8 @@ var _ = Describe("fpdf_thumbnail", func() {
Expect(FPDFPage_GetRawThumbnailData.RawThumbnail).To(HaveLen(1851))
})

It("returns no decoded thumbnail data", func() {
It("returns thumbnail as bitmap", func() {
By("when the thumbnail is fetched")
FPDFPage_GetThumbnailAsBitmap, err := PdfiumInstance.FPDFPage_GetThumbnailAsBitmap(&requests.FPDFPage_GetThumbnailAsBitmap{
Page: requests.Page{
ByIndex: &requests.PageByIndex{
Expand All @@ -157,7 +158,16 @@ var _ = Describe("fpdf_thumbnail", func() {
})
Expect(err).To(BeNil())
Expect(FPDFPage_GetThumbnailAsBitmap).To(Not(BeNil()))
// @todo: render thumbnail when FPDFBitmap_* is implemented and compare hash.
Expect(FPDFPage_GetThumbnailAsBitmap.Bitmap).To(Not(BeNil()))

By("the first byte in the bitmap buffer is white")
FPDFBitmap_GetBuffer, err := PdfiumInstance.FPDFBitmap_GetBuffer(&requests.FPDFBitmap_GetBuffer{
Bitmap: *FPDFPage_GetThumbnailAsBitmap.Bitmap,
})
Expect(err).To(BeNil())
Expect(FPDFBitmap_GetBuffer).To(Not(BeNil()))
Expect(FPDFBitmap_GetBuffer.Buffer).To(Not(BeNil()))
Expect(FPDFBitmap_GetBuffer.Buffer[0]).To(Equal(uint8(255)))
})
})
})
Loading

0 comments on commit bdba065

Please sign in to comment.