-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlibreofficekit_test.go
121 lines (106 loc) · 2.86 KB
/
libreofficekit_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package libreofficekit
import (
"testing"
"os"
)
const (
DefaultLibreOfficePath = "/usr/lib/libreoffice/program/"
DocumentThatDoesntExist = "testdata/kittens.docx"
SampleDocument = "testdata/sample.docx"
SaveDocumentPath = "/tmp/out.docx"
SaveDocumentFormat = "docx"
)
func TestInvalidOfficePath(t *testing.T) {
_, err := NewOffice("/etc/passwd")
if err == nil {
t.Fail()
}
}
func TestValidOfficePath(t *testing.T) {
_, err := NewOffice(DefaultLibreOfficePath)
if err != nil {
t.Fail()
}
}
func TestGetOfficeErrorMessage(t *testing.T) {
office, _ := NewOffice(DefaultLibreOfficePath)
office.LoadDocument(DocumentThatDoesntExist)
message := office.GetError()
if len(message) == 0 {
t.Fail()
}
}
func TestLoadDocumentThatDoesntExist(t *testing.T) {
office, _ := NewOffice(DefaultLibreOfficePath)
_, err := office.LoadDocument(DocumentThatDoesntExist)
if err == nil {
t.Fail()
}
}
func TestSuccessLoadDocument(t *testing.T) {
office, _ := NewOffice(DefaultLibreOfficePath)
_, err := office.LoadDocument(SampleDocument)
if err != nil {
t.Fail()
}
}
func TestSuccessfulLoadAndSaveDocument(t *testing.T) {
office, _ := NewOffice(DefaultLibreOfficePath)
doc, err := office.LoadDocument(SampleDocument)
if err != nil {
t.Fail()
}
err = doc.SaveAs(SaveDocumentPath, SaveDocumentFormat, "")
if err != nil {
t.Fail()
}
defer os.Remove(SaveDocumentPath)
}
func TestGetPartPageRectangles(t *testing.T) {
office, _ := NewOffice(DefaultLibreOfficePath)
document, _ := office.LoadDocument(SampleDocument)
rectangles := document.GetPartPageRectangles()
if len(rectangles) != 2 {
t.Fail()
}
}
func TestGetParts(t *testing.T) {
office, _ := NewOffice(DefaultLibreOfficePath)
document, _ := office.LoadDocument(SampleDocument)
parts := document.GetParts()
if parts != 2 {
t.Fail()
}
}
func TestGetTileMode(t *testing.T) {
office, _ := NewOffice(DefaultLibreOfficePath)
document, _ := office.LoadDocument(SampleDocument)
mode := document.GetTileMode()
if mode != RGBATilemode && mode != BGRATilemode {
t.Fail()
}
}
func TestGetType(t *testing.T) {
office, _ := NewOffice(DefaultLibreOfficePath)
document, _ := office.LoadDocument(SampleDocument)
documentType := document.GetType()
if documentType != TextDocument {
t.Fail()
}
}
func TestTextSelection(t *testing.T) {
office, _ := NewOffice(DefaultLibreOfficePath)
document, _ := office.LoadDocument(SampleDocument)
rectangle := document.GetPartPageRectangles()[0]
document.SetTextSelection(SetGraphicSelectionStart, rectangle.Min.X, rectangle.Min.Y)
document.SetTextSelection(SetGraphicSelectionEnd, rectangle.Max.X, rectangle.Max.Y)
plaintext := document.GetTextSelection("text/plain;charset=utf-8")
if len(plaintext) < 1000 {
t.Fail()
}
document.ResetTextSelection()
plaintext = document.GetTextSelection("text/plain;charset=utf-8")
if len(plaintext) != 0 {
t.Fail()
}
}