oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1package layout_test
2
3// A scene block in a picture of a slide (thumbnails, the PDF): the
4// slide fully built, so the block's `to` state (scenes and clips spec
5// §8.2), as its still or, with none, the panel Slides draws: the
6// scene's name and the state's description on a panel in the theme's
7// colours (plan call R4).
8
9import (
10 "strings"
11 "testing"
12
13 "amadan.net/oficina/slides/internal/slides/deck"
14 "amadan.net/oficina/slides/internal/slides/layout"
15)
16
17func sceneLayoutDeck(to string) *deck.Deck {
18 return &deck.Deck{
19 Meta: deck.Meta{Title: "Scenes", Theme: "paper", Aspect: "16:9"},
20 Assets: map[string]deck.Asset{"stilltown001": {Ref: "5f1e1d02", Width: 1920, Height: 1080}},
21 Scenes: map[string]deck.Scene{"worldaaaaaaa": {
22 Name: "The world",
23 States: []deck.SceneState{
24 {ID: "field", Name: "Field", Description: "An empty field"},
25 {ID: "town", Name: "Town", Description: "A town by a river"},
26 {ID: "city", Name: "City", Description: "A city at night"},
27 },
28 Stills: map[string]deck.Still{"town": {Ref: "5f1e1d02"}},
29 }},
30 Slides: []deck.Slide{
31 {ID: "slidecanvas1", Layout: "canvas", Blocks: []deck.Block{{
32 ID: "scenecanvas1", Type: "scene", Box: &deck.Box{X: 0.1, Y: 0.1, W: 0.5, H: 0.5},
33 Props: map[string]any{"scene": "worldaaaaaaa", "from": "field", "to": to},
34 }}},
35 {ID: "slideflow001", Layout: "flow", Template: "two-column", Blocks: []deck.Block{{
36 ID: "sceneflow001", Type: "scene", Region: "body",
37 Props: map[string]any{"scene": "worldaaaaaaa", "from": "field", "to": to},
38 }}},
39 {ID: "slidegone001", Layout: "flow", Template: "blank", Blocks: []deck.Block{{
40 ID: "scenegone001", Type: "scene",
41 Props: map[string]any{"scene": "goneaaaaaaaa", "from": "field", "to": "town"},
42 }}},
43 },
44 }
45}
46
47func TestASceneIsPicturedAtItsToState(t *testing.T) {
48 p, _ := paintOf(t, sceneLayoutDeck("town"), 0)
49 var images []layout.Image
50 for _, op := range p.Ops {
51 if im, ok := op.(layout.Image); ok {
52 images = append(images, im)
53 }
54 }
55 if len(images) != 1 {
56 t.Fatalf("the canvas scene paints %d pictures, want 1 (its to state's still): %+v", len(images), p.Ops)
57 }
58 im := images[0]
59 if im.Asset != "stilltown001" || im.Fit != "contain" || im.Alt != "A town by a river" {
60 t.Errorf("the still is %+v, want asset stilltown001, contain, the state's description", im)
61 }
62 // The box as placed: 10% in, half the frame.
63 if im.X != 192 || im.Y != 108 || im.W != 960 || im.H != 540 {
64 t.Errorf("the still is at %v,%v %vx%v, want the block's box 192,108 960x540", im.X, im.Y, im.W, im.H)
65 }
66
67 // In a region with room for it, it is 16:9 at the column's width.
68 p, _ = paintOf(t, sceneLayoutDeck("town"), 1)
69 pictured := 0
70 for _, op := range p.Ops {
71 if im, ok := op.(layout.Image); ok {
72 pictured++
73 if r := im.H / im.W; r < 0.5624 || r > 0.5626 {
74 t.Errorf("a flow scene is %vx%v, want 16:9", im.W, im.H)
75 }
76 }
77 }
78 if pictured != 1 {
79 t.Errorf("the flow scene paints %d pictures", pictured)
80 }
81}
82
83func TestAStateWithNoStillIsPicturedAsThePanel(t *testing.T) {
84 for _, c := range []struct {
85 slide int
86 want []string
87 }{
88 {0, []string{"The world", "A city at night"}},
89 // A scene the deck no longer has: the panel with nothing on it.
90 {2, nil},
91 } {
92 p, _ := paintOf(t, sceneLayoutDeck("city"), c.slide)
93 var panel bool
94 var words []string
95 for _, op := range p.Ops {
96 switch o := op.(type) {
97 case layout.Image:
98 t.Errorf("slide %d paints a picture %+v, want the panel", c.slide, o)
99 case layout.Rect:
100 if o.W > 100 && o.H > 100 && o.Fill != "" {
101 panel = true
102 }
103 case layout.Text:
104 for _, r := range o.Runs {
105 words = append(words, r.S)
106 }
107 }
108 }
109 if !panel {
110 t.Errorf("slide %d paints no panel: %+v", c.slide, p.Ops)
111 }
112 got := strings.Join(words, " ")
113 for _, w := range c.want {
114 if !strings.Contains(got, w) {
115 t.Errorf("slide %d's panel says %q, want %q in it", c.slide, got, w)
116 }
117 }
118 if c.want == nil && got != "" {
119 t.Errorf("a removed scene's panel says %q, want nothing", got)
120 }
121 }
122}
123
124// TestAPanelsWordsWrapInsideIt: in a region too short for a 16:9 box at
125// its width the panel is narrower than the column, and its words wrap
126// at the panel's width, every line inside it.
127func TestAPanelsWordsWrapInsideIt(t *testing.T) {
128 d := sceneLayoutDeck("city")
129 long := strings.Repeat("A city at night, lit from below by streets and windows. ", 6)
130 sc := d.Scenes["worldaaaaaaa"]
131 sc.States[2].Description = long
132 d.Scenes["worldaaaaaaa"] = sc
133 d.Slides[1].Template = "blank"
134 p, _ := paintOf(t, d, 1)
135 var panel layout.Rect
136 var lines int
137 for _, op := range p.Ops {
138 switch o := op.(type) {
139 case layout.Rect:
140 if o.W > 100 && o.H > 100 {
141 panel = o
142 }
143 case layout.Text:
144 lines++
145 if o.X < panel.X {
146 t.Errorf("a line of the panel starts at %v, left of the panel at %v", o.X, panel.X)
147 }
148 }
149 }
150 if panel.W == 0 || panel.W >= 1680 {
151 t.Fatalf("the panel is %vx%v, want narrower than the 1680 column", panel.W, panel.H)
152 }
153 if lines < 3 {
154 t.Errorf("the description is %d lines, want it wrapped", lines)
155 }
156}
157