picture_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package excelize
  2. import (
  3. _ "image/gif"
  4. _ "image/jpeg"
  5. _ "image/png"
  6. _ "golang.org/x/image/tiff"
  7. "fmt"
  8. "io/ioutil"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "testing"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func BenchmarkAddPictureFromBytes(b *testing.B) {
  16. f := NewFile()
  17. imgFile, err := ioutil.ReadFile(filepath.Join("test", "images", "excel.png"))
  18. if err != nil {
  19. b.Error("unable to load image for benchmark")
  20. }
  21. b.ResetTimer()
  22. for i := 1; i <= b.N; i++ {
  23. f.AddPictureFromBytes("Sheet1", fmt.Sprint("A", i), "", "excel", ".png", imgFile)
  24. }
  25. }
  26. func TestAddPicture(t *testing.T) {
  27. f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
  28. if !assert.NoError(t, err) {
  29. t.FailNow()
  30. }
  31. // Test add picture to worksheet with offset and location hyperlink.
  32. err = f.AddPicture("Sheet2", "I9", filepath.Join("test", "images", "excel.jpg"),
  33. `{"x_offset": 140, "y_offset": 120, "hyperlink": "#Sheet2!D8", "hyperlink_type": "Location"}`)
  34. if !assert.NoError(t, err) {
  35. t.FailNow()
  36. }
  37. // Test add picture to worksheet with offset, external hyperlink and positioning.
  38. err = f.AddPicture("Sheet1", "F21", filepath.Join("test", "images", "excel.jpg"),
  39. `{"x_offset": 10, "y_offset": 10, "hyperlink": "https://github.com/360EntSecGroup-Skylar/excelize", "hyperlink_type": "External", "positioning": "oneCell"}`)
  40. if !assert.NoError(t, err) {
  41. t.FailNow()
  42. }
  43. file, err := ioutil.ReadFile(filepath.Join("test", "images", "excel.png"))
  44. if !assert.NoError(t, err) {
  45. t.FailNow()
  46. }
  47. // Test add picture to worksheet from bytes.
  48. assert.NoError(t, f.AddPictureFromBytes("Sheet1", "Q1", "", "Excel Logo", ".png", file))
  49. // Test add picture to worksheet from bytes with illegal cell coordinates.
  50. assert.EqualError(t, f.AddPictureFromBytes("Sheet1", "A", "", "Excel Logo", ".png", file), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  51. assert.NoError(t, f.AddPicture("Sheet1", "Q8", filepath.Join("test", "images", "excel.gif"), ""))
  52. assert.NoError(t, f.AddPicture("Sheet1", "Q15", filepath.Join("test", "images", "excel.jpg"), ""))
  53. assert.NoError(t, f.AddPicture("Sheet1", "Q22", filepath.Join("test", "images", "excel.tif"), ""))
  54. // Test write file to given path.
  55. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddPicture.xlsx")))
  56. }
  57. func TestAddPictureErrors(t *testing.T) {
  58. xlsx, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
  59. if !assert.NoError(t, err) {
  60. t.FailNow()
  61. }
  62. // Test add picture to worksheet with invalid file path.
  63. err = xlsx.AddPicture("Sheet1", "G21", filepath.Join("test", "not_exists_dir", "not_exists.icon"), "")
  64. if assert.Error(t, err) {
  65. assert.True(t, os.IsNotExist(err), "Expected os.IsNotExist(err) == true")
  66. }
  67. // Test add picture to worksheet with unsupport file type.
  68. err = xlsx.AddPicture("Sheet1", "G21", filepath.Join("test", "Book1.xlsx"), "")
  69. assert.EqualError(t, err, "unsupported image extension")
  70. err = xlsx.AddPictureFromBytes("Sheet1", "G21", "", "Excel Logo", "jpg", make([]byte, 1))
  71. assert.EqualError(t, err, "unsupported image extension")
  72. // Test add picture to worksheet with invalid file data.
  73. err = xlsx.AddPictureFromBytes("Sheet1", "G21", "", "Excel Logo", ".jpg", make([]byte, 1))
  74. assert.EqualError(t, err, "image: unknown format")
  75. }
  76. func TestGetPicture(t *testing.T) {
  77. xlsx, err := prepareTestBook1()
  78. if !assert.NoError(t, err) {
  79. t.FailNow()
  80. }
  81. file, raw, err := xlsx.GetPicture("Sheet1", "F21")
  82. assert.NoError(t, err)
  83. if !assert.NotEmpty(t, filepath.Join("test", file)) || !assert.NotEmpty(t, raw) ||
  84. !assert.NoError(t, ioutil.WriteFile(filepath.Join("test", file), raw, 0644)) {
  85. t.FailNow()
  86. }
  87. // Try to get picture from a worksheet with illegal cell coordinates.
  88. _, _, err = xlsx.GetPicture("Sheet1", "A")
  89. assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  90. // Try to get picture from a worksheet that doesn't contain any images.
  91. file, raw, err = xlsx.GetPicture("Sheet3", "I9")
  92. assert.EqualError(t, err, "sheet Sheet3 is not exist")
  93. assert.Empty(t, file)
  94. assert.Empty(t, raw)
  95. // Try to get picture from a cell that doesn't contain an image.
  96. file, raw, err = xlsx.GetPicture("Sheet2", "A2")
  97. assert.NoError(t, err)
  98. assert.Empty(t, file)
  99. assert.Empty(t, raw)
  100. xlsx.getDrawingRelationships("xl/worksheets/_rels/sheet1.xml.rels", "rId8")
  101. xlsx.getDrawingRelationships("", "")
  102. xlsx.getSheetRelationshipsTargetByID("", "")
  103. xlsx.deleteSheetRelationships("", "")
  104. // Try to get picture from a local storage file.
  105. if !assert.NoError(t, xlsx.SaveAs(filepath.Join("test", "TestGetPicture.xlsx"))) {
  106. t.FailNow()
  107. }
  108. xlsx, err = OpenFile(filepath.Join("test", "TestGetPicture.xlsx"))
  109. if !assert.NoError(t, err) {
  110. t.FailNow()
  111. }
  112. file, raw, err = xlsx.GetPicture("Sheet1", "F21")
  113. assert.NoError(t, err)
  114. if !assert.NotEmpty(t, filepath.Join("test", file)) || !assert.NotEmpty(t, raw) ||
  115. !assert.NoError(t, ioutil.WriteFile(filepath.Join("test", file), raw, 0644)) {
  116. t.FailNow()
  117. }
  118. // Try to get picture from a local storage file that doesn't contain an image.
  119. file, raw, err = xlsx.GetPicture("Sheet1", "F22")
  120. assert.NoError(t, err)
  121. assert.Empty(t, file)
  122. assert.Empty(t, raw)
  123. }
  124. func TestAddDrawingPicture(t *testing.T) {
  125. // testing addDrawingPicture with illegal cell coordinates.
  126. f := NewFile()
  127. assert.EqualError(t, f.addDrawingPicture("sheet1", "", "A", "", 0, 0, 0, 0, nil), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  128. }
  129. func TestAddPictureFromBytes(t *testing.T) {
  130. f := NewFile()
  131. imgFile, err := ioutil.ReadFile("logo.png")
  132. if err != nil {
  133. t.Error("Unable to load logo for test")
  134. }
  135. f.AddPictureFromBytes("Sheet1", fmt.Sprint("A", 1), "", "logo", ".png", imgFile)
  136. f.AddPictureFromBytes("Sheet1", fmt.Sprint("A", 50), "", "logo", ".png", imgFile)
  137. imageCount := 0
  138. for fileName := range f.XLSX {
  139. if strings.Contains(fileName, "media/image") {
  140. imageCount++
  141. }
  142. }
  143. assert.Equal(t, 1, imageCount, "Duplicate image should only be stored once.")
  144. }