diff --git a/internal/shareable/shareable.go b/internal/shareable/shareable.go index 6062c7503..d89387674 100644 --- a/internal/shareable/shareable.go +++ b/internal/shareable/shareable.go @@ -191,6 +191,7 @@ func (s *Image) dispose() { return } + s.backend.restorable.Dispose() index := -1 for i, sh := range theBackends { if sh == s.backend { @@ -313,3 +314,9 @@ func Restore() error { defer backendsM.Unlock() return restorable.Restore() } + +func BackendNumForTesting() int { + backendsM.Lock() + defer backendsM.Unlock() + return len(theBackends) +} diff --git a/internal/shareable/shareable_test.go b/internal/shareable/shareable_test.go index a69c85259..b9686fee7 100644 --- a/internal/shareable/shareable_test.go +++ b/internal/shareable/shareable_test.go @@ -40,13 +40,15 @@ func TestMain(m *testing.M) { os.Exit(code) } +const bigSize = 2049 + func TestEnsureNotShared(t *testing.T) { // Create img1 and img2 with this size so that the next images are allocated // with non-upper-left location. - img1 := NewImage(2049, 100) + img1 := NewImage(bigSize, 100) defer img1.Dispose() - img2 := NewImage(100, 2049) + img2 := NewImage(100, bigSize) defer img2.Dispose() const size = 32 @@ -97,3 +99,26 @@ func TestEnsureNotShared(t *testing.T) { } } } + +func TestDispose(t *testing.T) { + // There are already backend image for the offscreen or something. + // Creating a big image and the next image should be created at a new backend. + img1 := NewImage(bigSize, bigSize) + defer img1.Dispose() + + if BackendNumForTesting() != 1 { + t.Errorf("BackendNumForTesting(): got: %d, want: %d", BackendNumForTesting(), 1) + } + + img2 := NewImage(bigSize, bigSize) + + if BackendNumForTesting() != 2 { + t.Errorf("BackendNumForTesting(): got: %d, want: %d", BackendNumForTesting(), 2) + } + + img2.Dispose() + + if BackendNumForTesting() != 1 { + t.Errorf("BackendNumForTesting(): got: %d, want: %d", BackendNumForTesting(), 1) + } +}