draw_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package drawimg
  2. import (
  3. "image"
  4. "image/draw"
  5. "io/ioutil"
  6. "reflect"
  7. "testing"
  8. "github.com/bouk/monkey"
  9. "github.com/golang/freetype/raster"
  10. "github.com/golang/freetype/truetype"
  11. "github.com/smartystreets/goconvey/convey"
  12. "golang.org/x/image/font"
  13. "golang.org/x/image/math/fixed"
  14. )
  15. var (
  16. di = DrawImg{
  17. size: 100,
  18. CanvasWidth: 20,
  19. CanvasHeight: 20,
  20. File: "",
  21. txtWidth: 20,
  22. srcImg: image.NewAlpha(imgRectangle),
  23. Canvas: &image.NRGBA{},
  24. c: &c,
  25. f: &truetype.Font{},
  26. }
  27. c = Context{
  28. r: nil,
  29. f: &truetype.Font{},
  30. glyphBuf: truetype.GlyphBuf{},
  31. clip: image.Rectangle{},
  32. dst: nil,
  33. src: nil,
  34. fontSize: 0,
  35. dpi: 0,
  36. scale: 0,
  37. hinting: 0,
  38. cache: [1024]cacheEntry{},
  39. }
  40. imgRectangle = image.Rectangle{
  41. Min: image.Point{
  42. X: 0,
  43. Y: 0,
  44. },
  45. Max: image.Point{
  46. X: 1,
  47. Y: 1,
  48. },
  49. }
  50. )
  51. func TestDrawimgNewDrawImg(t *testing.T) {
  52. convey.Convey("NewDrawImg", t, func(ctx convey.C) {
  53. var (
  54. fontfile = ""
  55. size = int(10)
  56. )
  57. monkeyReadFile([]byte{}, nil)
  58. monkeyTrueTypeParser(&truetype.Font{}, nil)
  59. monkeyFreeTypeSetFont()
  60. monkeySetFontSize()
  61. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  62. w := NewDrawImg(fontfile, size)
  63. ctx.Convey("Then w should not be nil.", func(ctx convey.C) {
  64. ctx.So(w, convey.ShouldNotBeNil)
  65. })
  66. })
  67. })
  68. }
  69. func TestDrawimgreadFont(t *testing.T) {
  70. convey.Convey("readFont", t, func(ctx convey.C) {
  71. var (
  72. path = ""
  73. size = int(0)
  74. )
  75. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  76. monkeyReadFile([]byte{}, nil)
  77. monkeyTrueTypeParser(&truetype.Font{}, nil)
  78. monkeyFreeTypeSetFont()
  79. monkeySetFontSize()
  80. f, err := di.readFont(path, size)
  81. ctx.Convey("Then err should be nil.f should not be nil.", func(ctx convey.C) {
  82. ctx.So(err, convey.ShouldBeNil)
  83. ctx.So(f, convey.ShouldNotBeNil)
  84. })
  85. })
  86. })
  87. }
  88. func TestDrawimgnewCanvas(t *testing.T) {
  89. convey.Convey("newCanvas", t, func(ctx convey.C) {
  90. var (
  91. width = int(0)
  92. height = int(0)
  93. )
  94. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  95. p1 := di.newCanvas(width, height)
  96. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  97. ctx.So(p1, convey.ShouldNotBeNil)
  98. })
  99. })
  100. })
  101. }
  102. func TestDrawimgfillColor(t *testing.T) {
  103. convey.Convey("fillColor", t, func(ctx convey.C) {
  104. var (
  105. r = int32(0)
  106. g = int32(0)
  107. b = int32(0)
  108. a = int32(0)
  109. )
  110. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  111. p1 := di.fillColor(r, g, b, a)
  112. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  113. ctx.So(p1, convey.ShouldNotBeNil)
  114. })
  115. })
  116. })
  117. }
  118. func TestDrawimgtextWidth(t *testing.T) {
  119. convey.Convey("textWidth", t, func(ctx convey.C) {
  120. var (
  121. text = "12"
  122. )
  123. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  124. monkeyFontBox(fixed.Point26_6{}, nil)
  125. err := di.textWidth(text)
  126. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  127. ctx.So(err, convey.ShouldBeNil)
  128. })
  129. })
  130. })
  131. }
  132. func TestDrawimgpt(t *testing.T) {
  133. convey.Convey("pt", t, func(ctx convey.C) {
  134. var (
  135. x = int(0)
  136. y = int(0)
  137. )
  138. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  139. p1 := di.pt(x, y)
  140. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  141. ctx.So(p1, convey.ShouldNotBeNil)
  142. })
  143. })
  144. })
  145. }
  146. func TestDrawimgsetFont(t *testing.T) {
  147. convey.Convey("setFont", t, func(ctx convey.C) {
  148. var (
  149. text = ""
  150. dstRgba = &image.NRGBA{}
  151. fsrc image.Image
  152. pt fixed.Point26_6
  153. )
  154. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  155. monkeyDrawString(fixed.Point26_6{}, nil)
  156. err := di.setFont(text, dstRgba, fsrc, pt)
  157. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  158. ctx.So(err, convey.ShouldBeNil)
  159. })
  160. })
  161. })
  162. }
  163. func TestDrawimgcomposite(t *testing.T) {
  164. convey.Convey("composite", t, func(ctx convey.C) {
  165. var (
  166. dstCanvas = &image.NRGBA{}
  167. src image.Image
  168. isLeft bool
  169. )
  170. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  171. monkeyDraw()
  172. //monkeybounds(imgRectangle)
  173. di.composite(dstCanvas, src, isLeft)
  174. ctx.Convey("No return values", func(ctx convey.C) {
  175. })
  176. })
  177. })
  178. }
  179. func TestDrawimgDraw(t *testing.T) {
  180. convey.Convey("Draw", t, func(ctx convey.C) {
  181. var (
  182. text = "123"
  183. savepath = ""
  184. isLeft bool
  185. )
  186. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  187. //monkeybounds(imgRectangle)
  188. monkeyFreeTypeSetFont()
  189. err := di.Draw(text, savepath, isLeft)
  190. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  191. ctx.So(err, convey.ShouldBeNil)
  192. })
  193. })
  194. })
  195. }
  196. func monkeyReadFile(file []byte, err error) {
  197. monkey.Patch(ioutil.ReadFile, func(_ string) ([]byte, error) {
  198. return file, err
  199. })
  200. }
  201. func monkeyTrueTypeParser(font *truetype.Font, err error) {
  202. monkey.Patch(truetype.Parse, func(_ []byte) (*truetype.Font, error) {
  203. return font, err
  204. })
  205. }
  206. func monkeyFreeTypeSetFont() {
  207. monkey.PatchInstanceMethod(reflect.TypeOf(di.c), "SetFont", func(_ *Context, _ *truetype.Font) {})
  208. }
  209. func monkeySetFontSize() {
  210. monkey.PatchInstanceMethod(reflect.TypeOf(di.c), "SetFontSize", func(_ *Context, _ float64) {})
  211. }
  212. func monkeyDrawString(p fixed.Point26_6, err error) {
  213. monkey.PatchInstanceMethod(reflect.TypeOf(di.c), "DrawString", func(_ *Context, _ string, _ fixed.Point26_6) (fixed.Point26_6, error) {
  214. return p, err
  215. })
  216. }
  217. func monkeyDraw() {
  218. monkey.Patch(draw.Draw, func(_ draw.Image, _ image.Rectangle, _ image.Image, _ image.Point, _ draw.Op) {})
  219. }
  220. func monkeyFontBox(p fixed.Point26_6, err error) {
  221. monkey.PatchInstanceMethod(reflect.TypeOf(di.c), "FontBox", func(_ *Context, _ string, _ fixed.Point26_6) (fixed.Point26_6, error) {
  222. return p, err
  223. })
  224. }
  225. func monkeyLoad() {
  226. monkey.PatchInstanceMethod(reflect.TypeOf(&di.c.glyphBuf), "Load", func(_ *truetype.GlyphBuf, _ *truetype.Font, _ fixed.Int26_6, _ truetype.Index, _ font.Hinting) error {
  227. return nil
  228. })
  229. }
  230. func monkeyClear() {
  231. monkey.PatchInstanceMethod(reflect.TypeOf(di.c.r), "Clear", func(_ *raster.Rasterizer) {})
  232. }
  233. func monkeyRasterizer() {
  234. monkey.PatchInstanceMethod(reflect.TypeOf(di.c.r), "Rasterize", func(_ *raster.Rasterizer, _ raster.Painter) {})
  235. }