restorable: Add TestRestoreRecursive (#265) (#357)

This commit is contained in:
Hajime Hoshi 2017-06-01 02:37:21 +09:00
parent baeadfb7fe
commit 82f102c422
2 changed files with 46 additions and 2 deletions

View File

@ -142,6 +142,7 @@ func (p *Image) ReplacePixels(pixels []uint8) {
func (p *Image) DrawImage(img *Image, vertices []float32, colorm *affine.ColorM, mode opengl.CompositeMode) { func (p *Image) DrawImage(img *Image, vertices []float32, colorm *affine.ColorM, mode opengl.CompositeMode) {
theImages.resetPixelsIfDependingOn(p) theImages.resetPixelsIfDependingOn(p)
if img.stale || img.volatile { if img.stale || img.volatile {
// TODO: What will happen if there are images depending on p?
p.makeStale() p.makeStale()
} else { } else {
p.appendDrawImageHistory(img, vertices, colorm, mode) p.appendDrawImageHistory(img, vertices, colorm, mode)

View File

@ -71,12 +71,11 @@ func vertices() []float32 {
} }
} }
func TestRestoreReversedOrder(t *testing.T) { func TestRestoreOverrideSource(t *testing.T) {
img0 := NewImage(1, 1, opengl.Nearest, false) img0 := NewImage(1, 1, opengl.Nearest, false)
img1 := NewImage(1, 1, opengl.Nearest, false) img1 := NewImage(1, 1, opengl.Nearest, false)
img2 := NewImage(1, 1, opengl.Nearest, false) img2 := NewImage(1, 1, opengl.Nearest, false)
img3 := NewImage(1, 1, opengl.Nearest, false) img3 := NewImage(1, 1, opengl.Nearest, false)
println(img0, img1, img2, img3)
defer func() { defer func() {
img3.Dispose() img3.Dispose()
img2.Dispose() img2.Dispose()
@ -128,3 +127,47 @@ func TestRestoreReversedOrder(t *testing.T) {
} }
} }
} }
func TestRestoreRecursive(t *testing.T) {
img0 := NewImage(1, 1, opengl.Nearest, false)
img1 := NewImage(1, 1, opengl.Nearest, false)
defer func() {
img1.Dispose()
img0.Dispose()
}()
clr0 := color.RGBA{0x00, 0x00, 0x01, 0xff}
clr1 := color.RGBA{0x00, 0x00, 0x02, 0xff}
img0.Fill(clr0)
img1.Fill(clr1)
img1.DrawImage(img0, vertices(), &affine.ColorM{}, opengl.CompositeModeLighter)
img0.DrawImage(img1, vertices(), &affine.ColorM{}, opengl.CompositeModeLighter)
if err := ResolveStalePixels(); err != nil {
t.Fatal(err)
}
if err := Restore(); err != nil {
t.Fatal(err)
}
testCases := []struct {
name string
want color.RGBA
got color.RGBA
}{
{
"0",
color.RGBA{0x00, 0x00, 0x04, 0xff},
uint8SliceToColor(img0.BasePixelsForTesting()),
},
{
"1",
color.RGBA{0x00, 0x00, 0x03, 0xff},
uint8SliceToColor(img1.BasePixelsForTesting()),
},
}
for _, c := range testCases {
if c.got != c.want {
t.Errorf("%s: got %v, want %v", c.name, c.got, c.want)
}
}
}
// TODO: How about volatile/screen images?