2016-07-11 02:21:19 -04:00
|
|
|
package chart
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-10-12 12:43:30 -04:00
|
|
|
"fmt"
|
2016-07-11 02:21:19 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2018-04-05 03:42:38 -04:00
|
|
|
"github.com/blend/go-sdk/assert"
|
2016-07-29 19:36:29 -04:00
|
|
|
"github.com/wcharczuk/go-chart/drawing"
|
2016-07-11 02:21:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestVectorRendererPath(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
vr, err := SVG(100, 100)
|
|
|
|
assert.Nil(err)
|
|
|
|
|
|
|
|
typed, isTyped := vr.(*vectorRenderer)
|
|
|
|
assert.True(isTyped)
|
|
|
|
|
|
|
|
typed.MoveTo(0, 0)
|
|
|
|
typed.LineTo(100, 100)
|
|
|
|
typed.LineTo(0, 100)
|
|
|
|
typed.Close()
|
|
|
|
typed.FillStroke()
|
|
|
|
|
|
|
|
buffer := bytes.NewBuffer([]byte{})
|
|
|
|
err = typed.Save(buffer)
|
|
|
|
assert.Nil(err)
|
|
|
|
|
|
|
|
raw := string(buffer.Bytes())
|
|
|
|
|
|
|
|
assert.True(strings.HasPrefix(raw, "<svg"))
|
|
|
|
assert.True(strings.HasSuffix(raw, "</svg>"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVectorRendererMeasureText(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
f, err := GetDefaultFont()
|
|
|
|
assert.Nil(err)
|
|
|
|
|
|
|
|
vr, err := SVG(100, 100)
|
|
|
|
assert.Nil(err)
|
|
|
|
|
|
|
|
vr.SetDPI(DefaultDPI)
|
|
|
|
vr.SetFont(f)
|
|
|
|
vr.SetFontSize(12.0)
|
|
|
|
|
2016-07-12 02:32:31 -04:00
|
|
|
tb := vr.MeasureText("Ljp")
|
2016-07-12 19:47:52 -04:00
|
|
|
assert.Equal(21, tb.Width())
|
|
|
|
assert.Equal(15, tb.Height())
|
2016-07-11 02:21:19 -04:00
|
|
|
}
|
2016-07-29 19:36:29 -04:00
|
|
|
|
|
|
|
func TestCanvasStyleSVG(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
f, err := GetDefaultFont()
|
|
|
|
assert.Nil(err)
|
|
|
|
|
|
|
|
set := Style{
|
|
|
|
StrokeColor: drawing.ColorWhite,
|
|
|
|
StrokeWidth: 5.0,
|
|
|
|
FillColor: drawing.ColorWhite,
|
|
|
|
FontColor: drawing.ColorWhite,
|
|
|
|
Font: f,
|
|
|
|
Padding: DefaultBackgroundPadding,
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas := &canvas{dpi: DefaultDPI}
|
|
|
|
|
|
|
|
svgString := canvas.styleAsSVG(set)
|
|
|
|
assert.NotEmpty(svgString)
|
2018-10-11 20:21:46 -04:00
|
|
|
assert.True(strings.HasPrefix(svgString, "style=\""))
|
2016-07-29 19:36:29 -04:00
|
|
|
assert.True(strings.Contains(svgString, "stroke:rgba(255,255,255,1.0)"))
|
|
|
|
assert.True(strings.Contains(svgString, "stroke-width:5"))
|
|
|
|
assert.True(strings.Contains(svgString, "fill:rgba(255,255,255,1.0)"))
|
2018-10-11 20:21:46 -04:00
|
|
|
assert.True(strings.HasSuffix(svgString, "\""))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCanvasClassSVG(t *testing.T) {
|
|
|
|
as := assert.New(t)
|
|
|
|
|
|
|
|
set := Style{
|
|
|
|
ClassName: "test-class",
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas := &canvas{dpi: DefaultDPI}
|
|
|
|
|
|
|
|
as.Equal("class=\"test-class\"", canvas.styleAsSVG(set))
|
2016-07-29 19:36:29 -04:00
|
|
|
}
|
2018-10-12 12:43:30 -04:00
|
|
|
|
|
|
|
func TestCanvasCustomInlineStylesheet(t *testing.T) {
|
|
|
|
b := strings.Builder{}
|
|
|
|
|
|
|
|
canvas := &canvas{
|
|
|
|
w: &b,
|
|
|
|
css: ".background { fill: red }",
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas.Start(200, 200)
|
|
|
|
|
|
|
|
assert.New(t).Contains(b.String(), fmt.Sprintf(`<style type="text/css"><![CDATA[%s]]></style>`, canvas.css))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCanvasCustomInlineStylesheetWithNonce(t *testing.T) {
|
|
|
|
b := strings.Builder{}
|
|
|
|
|
|
|
|
canvas := &canvas{
|
|
|
|
w: &b,
|
|
|
|
css: ".background { fill: red }",
|
|
|
|
nonce: "RAND0MSTRING",
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas.Start(200, 200)
|
|
|
|
|
|
|
|
assert.New(t).Contains(b.String(), fmt.Sprintf(`<style type="text/css" nonce="%s"><![CDATA[%s]]></style>`, canvas.nonce, canvas.css))
|
|
|
|
}
|