From 4800f7720a19089bfc54a72b9993b51ddab5c202 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 18 Sep 2020 18:33:57 +0100 Subject: [PATCH] Add FontCacheImplTest --- .../kemitix/fontface/FontCacheImplTest.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/main/test/java/net/kemitix/fontface/FontCacheImplTest.java diff --git a/src/main/test/java/net/kemitix/fontface/FontCacheImplTest.java b/src/main/test/java/net/kemitix/fontface/FontCacheImplTest.java new file mode 100644 index 0000000..a1cc9da --- /dev/null +++ b/src/main/test/java/net/kemitix/fontface/FontCacheImplTest.java @@ -0,0 +1,74 @@ +package net.kemitix.fontface; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.awt.*; +import java.io.File; +import java.net.URI; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; + +@ExtendWith(MockitoExtension.class) +public class FontCacheImplTest { + + private final URI fontLocation = new File("font.otf").toURI(); + private final String colour = "colour"; + @Mock + private FontLoader fontLoader; + @Mock + private Font loadedFont; + + @BeforeEach + public void setUp() { + given(fontLoader.loadFont(any())).willReturn(loadedFont); + } + + @Test + @DisplayName("When cache is empty then load font") + public void emptyCache() { + //given + final FontCacheImpl fontCache = new FontCacheImpl(fontLoader); + final FontFace fontFace = FontFace.of(fontLocation, 26, colour); + //when + fontCache.loadFont(fontFace); + //then + verify(fontLoader).loadFont(fontFace); + } + + @Test + @DisplayName("When cache has font, but not size, then don't reload font") + public void notInSize() { + //given + final FontCacheImpl fontCache = new FontCacheImpl(fontLoader); + final FontFace previousFontFace = FontFace.of(fontLocation, 16, colour); + fontCache.loadFont(previousFontFace); + final FontFace fontFace = FontFace.of(fontLocation, 26, colour); + //when + fontCache.loadFont(fontFace); + //then + verify(fontLoader, times(1)).loadFont(previousFontFace); + verifyNoMoreInteractions(fontLoader); + } + + @Test + @DisplayName("When cache has font in size, then don't load font") + public void available() { + //given + final FontCacheImpl fontCache = new FontCacheImpl(fontLoader); + final FontFace fontFace = FontFace.of(fontLocation, 26, colour); + //when + fontCache.loadFont(fontFace); + fontCache.loadFont(fontFace); + //then + verify(fontLoader, times(1)).loadFont(fontFace); + } +} \ No newline at end of file