picture_test.go 6.0 KB
Newer Older
1 2 3
package excelize

import (
4 5
	_ "image/gif"
	_ "image/jpeg"
6
	_ "image/png"
7 8 9 10

	_ "golang.org/x/image/tiff"

	"fmt"
11
	"io/ioutil"
12
	"os"
13
	"path/filepath"
M
Michael 已提交
14
	"strings"
15
	"testing"
16 17

	"github.com/stretchr/testify/assert"
18 19 20 21
)

func BenchmarkAddPictureFromBytes(b *testing.B) {
	f := NewFile()
22
	imgFile, err := ioutil.ReadFile(filepath.Join("test", "images", "excel.png"))
23
	if err != nil {
24
		b.Error("unable to load image for benchmark")
25 26 27
	}
	b.ResetTimer()
	for i := 1; i <= b.N; i++ {
28 29 30
		if err := f.AddPictureFromBytes("Sheet1", fmt.Sprint("A", i), "", "excel", ".png", imgFile); err != nil {
			b.Error(err)
		}
31 32
	}
}
33 34

func TestAddPicture(t *testing.T) {
35
	f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
36 37 38 39 40
	if !assert.NoError(t, err) {
		t.FailNow()
	}

	// Test add picture to worksheet with offset and location hyperlink.
41 42
	assert.NoError(t, f.AddPicture("Sheet2", "I9", filepath.Join("test", "images", "excel.jpg"),
		`{"x_offset": 140, "y_offset": 120, "hyperlink": "#Sheet2!D8", "hyperlink_type": "Location"}`))
43
	// Test add picture to worksheet with offset, external hyperlink and positioning.
44 45
	assert.NoError(t, f.AddPicture("Sheet1", "F21", filepath.Join("test", "images", "excel.jpg"),
		`{"x_offset": 10, "y_offset": 10, "hyperlink": "https://github.com/360EntSecGroup-Skylar/excelize", "hyperlink_type": "External", "positioning": "oneCell"}`))
46

47
	file, err := ioutil.ReadFile(filepath.Join("test", "images", "excel.png"))
48
	assert.NoError(t, err)
49 50

	// Test add picture to worksheet from bytes.
51
	assert.NoError(t, f.AddPictureFromBytes("Sheet1", "Q1", "", "Excel Logo", ".png", file))
52
	// Test add picture to worksheet from bytes with illegal cell coordinates.
53 54 55 56 57
	assert.EqualError(t, f.AddPictureFromBytes("Sheet1", "A", "", "Excel Logo", ".png", file), `cannot convert cell "A" to coordinates: invalid cell name "A"`)

	assert.NoError(t, f.AddPicture("Sheet1", "Q8", filepath.Join("test", "images", "excel.gif"), ""))
	assert.NoError(t, f.AddPicture("Sheet1", "Q15", filepath.Join("test", "images", "excel.jpg"), ""))
	assert.NoError(t, f.AddPicture("Sheet1", "Q22", filepath.Join("test", "images", "excel.tif"), ""))
58 59

	// Test write file to given path.
60
	assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddPicture.xlsx")))
61 62 63 64
}

func TestAddPictureErrors(t *testing.T) {
	xlsx, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
65
	assert.NoError(t, err)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

	// Test add picture to worksheet with invalid file path.
	err = xlsx.AddPicture("Sheet1", "G21", filepath.Join("test", "not_exists_dir", "not_exists.icon"), "")
	if assert.Error(t, err) {
		assert.True(t, os.IsNotExist(err), "Expected os.IsNotExist(err) == true")
	}

	// Test add picture to worksheet with unsupport file type.
	err = xlsx.AddPicture("Sheet1", "G21", filepath.Join("test", "Book1.xlsx"), "")
	assert.EqualError(t, err, "unsupported image extension")

	err = xlsx.AddPictureFromBytes("Sheet1", "G21", "", "Excel Logo", "jpg", make([]byte, 1))
	assert.EqualError(t, err, "unsupported image extension")

	// Test add picture to worksheet with invalid file data.
	err = xlsx.AddPictureFromBytes("Sheet1", "G21", "", "Excel Logo", ".jpg", make([]byte, 1))
	assert.EqualError(t, err, "image: unknown format")
}

func TestGetPicture(t *testing.T) {
xurime's avatar
xurime 已提交
86
	f, err := prepareTestBook1()
87 88 89 90
	if !assert.NoError(t, err) {
		t.FailNow()
	}

xurime's avatar
xurime 已提交
91
	file, raw, err := f.GetPicture("Sheet1", "F21")
92 93 94 95 96 97 98 99
	assert.NoError(t, err)
	if !assert.NotEmpty(t, filepath.Join("test", file)) || !assert.NotEmpty(t, raw) ||
		!assert.NoError(t, ioutil.WriteFile(filepath.Join("test", file), raw, 0644)) {

		t.FailNow()
	}

	// Try to get picture from a worksheet with illegal cell coordinates.
xurime's avatar
xurime 已提交
100
	_, _, err = f.GetPicture("Sheet1", "A")
101 102 103
	assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`)

	// Try to get picture from a worksheet that doesn't contain any images.
xurime's avatar
xurime 已提交
104
	file, raw, err = f.GetPicture("Sheet3", "I9")
xurime's avatar
xurime 已提交
105
	assert.EqualError(t, err, "sheet Sheet3 is not exist")
106 107 108 109
	assert.Empty(t, file)
	assert.Empty(t, raw)

	// Try to get picture from a cell that doesn't contain an image.
xurime's avatar
xurime 已提交
110
	file, raw, err = f.GetPicture("Sheet2", "A2")
111 112 113 114
	assert.NoError(t, err)
	assert.Empty(t, file)
	assert.Empty(t, raw)

xurime's avatar
xurime 已提交
115 116 117 118
	f.getDrawingRelationships("xl/worksheets/_rels/sheet1.xml.rels", "rId8")
	f.getDrawingRelationships("", "")
	f.getSheetRelationshipsTargetByID("", "")
	f.deleteSheetRelationships("", "")
119 120

	// Try to get picture from a local storage file.
121
	assert.NoError(t, f.SaveAs(filepath.Join("test", "TestGetPicture.xlsx")))
122

xurime's avatar
xurime 已提交
123
	f, err = OpenFile(filepath.Join("test", "TestGetPicture.xlsx"))
124
	assert.NoError(t, err)
125

xurime's avatar
xurime 已提交
126
	file, raw, err = f.GetPicture("Sheet1", "F21")
127 128 129 130 131 132 133 134
	assert.NoError(t, err)
	if !assert.NotEmpty(t, filepath.Join("test", file)) || !assert.NotEmpty(t, raw) ||
		!assert.NoError(t, ioutil.WriteFile(filepath.Join("test", file), raw, 0644)) {

		t.FailNow()
	}

	// Try to get picture from a local storage file that doesn't contain an image.
xurime's avatar
xurime 已提交
135 136 137 138 139 140 141 142
	file, raw, err = f.GetPicture("Sheet1", "F22")
	assert.NoError(t, err)
	assert.Empty(t, file)
	assert.Empty(t, raw)

	// Test get picture from none drawing worksheet.
	f = NewFile()
	file, raw, err = f.GetPicture("Sheet1", "F22")
143 144 145 146 147 148 149 150 151 152
	assert.NoError(t, err)
	assert.Empty(t, file)
	assert.Empty(t, raw)
}

func TestAddDrawingPicture(t *testing.T) {
	// testing addDrawingPicture with illegal cell coordinates.
	f := NewFile()
	assert.EqualError(t, f.addDrawingPicture("sheet1", "", "A", "", 0, 0, 0, 0, nil), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
}
M
Michael 已提交
153 154 155 156

func TestAddPictureFromBytes(t *testing.T) {
	f := NewFile()
	imgFile, err := ioutil.ReadFile("logo.png")
xurime's avatar
xurime 已提交
157 158 159
	assert.NoError(t, err, "Unable to load logo for test")
	assert.NoError(t, f.AddPictureFromBytes("Sheet1", fmt.Sprint("A", 1), "", "logo", ".png", imgFile))
	assert.NoError(t, f.AddPictureFromBytes("Sheet1", fmt.Sprint("A", 50), "", "logo", ".png", imgFile))
M
Michael 已提交
160 161 162 163 164 165 166
	imageCount := 0
	for fileName := range f.XLSX {
		if strings.Contains(fileName, "media/image") {
			imageCount++
		}
	}
	assert.Equal(t, 1, imageCount, "Duplicate image should only be stored once.")
xurime's avatar
xurime 已提交
167
	assert.EqualError(t, f.AddPictureFromBytes("SheetN", fmt.Sprint("A", 1), "", "logo", ".png", imgFile), "sheet SheetN is not exist")
M
Michael 已提交
168
}