diff --git a/encode/encode.go b/encode/encode.go index 7fde7ddce5..8014bf0c80 100644 --- a/encode/encode.go +++ b/encode/encode.go @@ -54,6 +54,11 @@ func ScreensFromImages(images ...image.Image) *Screens { return &screens } +// Empty returns true if there are no render roots or images in this screen. +func (s *Screens) Empty() bool { + return len(s.roots) == 0 && len(s.images) == 0 +} + // Hash returns a hash of the render roots for this screen. This can be used for // testing whether two render trees are exactly equivalent, without having to // do the actual rendering. diff --git a/encode/encode_test.go b/encode/encode_test.go index 44633a3449..78b86ab664 100644 --- a/encode/encode_test.go +++ b/encode/encode_test.go @@ -162,6 +162,7 @@ func TestHash(t *testing.T) { roots, err := app.Run(context.Background()) require.NoError(t, err) + assert.False(t, ScreensFromRoots(roots).Empty()) // ensure we can calculate a hash hash, err := ScreensFromRoots(roots).Hash() @@ -185,11 +186,33 @@ func TestHash(t *testing.T) { // ensure we can calculate a hash on the new app hash2, err := ScreensFromRoots(roots2).Hash() + require.NoError(t, err) // ensure hashes are different require.NotEqual(t, hash, hash2) } +func TestHashEmptyApp(t *testing.T) { + app, err := runtime.NewApplet("test.star", []byte(`def main(): return []`)) + require.NoError(t, err) + + roots, err := app.Run(context.Background()) + require.NoError(t, err) + assert.True(t, ScreensFromRoots(roots).Empty()) + + // ensure we can calculate a hash + hash, err := ScreensFromRoots(roots).Hash() + require.NoError(t, err) + require.True(t, len(hash) > 0) + + // ensure the hash doesn't change + for i := 0; i < 20; i++ { + h, err := ScreensFromRoots(roots).Hash() + assert.NoError(t, err) + assert.Equal(t, hash, h) + } +} + func TestHashDelayAndMaxAge(t *testing.T) { r := []render.Root{{Child: &render.Text{Content: "derp"}}}