picture_test.go 5.5 KB

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