Debian Codegraph @

shiny/widget/theme/theme_test.go

raw symbols SCIP
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package theme
6
7 import (
8 "testing"
9
10 "golang.org/x/exp/shiny/unit"
11 "golang.org/x/image/math/fixed"
12 )
13
14 func approxEqual(x, y, tolerance float64) bool {
15 delta := x - y
16 return -tolerance < delta && delta < +tolerance
17 }
18
19 func TestThemeIsAUnitConverter(t *testing.T) {
20 // 1.5 inches (at the default 72 DPI) should be 108 pixels.
21 c := unit.Converter(Default)
22 got := c.Pixels(unit.Inches(1.5))
23 want := fixed.I(108)
24 if got != want {
25 t.Errorf("1 inch in pixels: got %v, want %v", got, want)
26 }
27
28 // 3 em (at inconsolata.Regular8x16's 16 pixel em-height) should be 48
29 // pixels, regardless of the DPI. That font face is based on a bitmap font,
30 // not a vector font, so its height does not depend on the DPI. 48 pixels
31 // is 48 points at 72 DPI, and 48 pixels is 21.6 points at 160 DPI.
32 for _, dpi := range []float64{72, 160} {
33 c := unit.Converter(&Theme{
34 DPI: dpi,
35 })
36 got := c.Convert(unit.Ems(3), unit.Pt)
37 want := unit.Points(3 * 16 * unit.PointsPerInch / dpi)
38 if got.U != want.U || !approxEqual(got.F, want.F, 1e-10) {
39 t.Errorf("dpi=%v: 3 em in points: got %v, want %v", dpi, got, want)
40 }
41 }
42 }