picture_test.go 4.9 KB

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