OXYGENHUB
◆ v0.1.2 oxygenhub-android / app / src / test / java / io / oxygenhub / android / ui / WidgetDataTest.kt
2.8 KiB · last changed The widget: the OxygenHub mark and Your sky behind it (closes #30) 4 days ago source blame history raw
1
package io.oxygenhub.android.ui
2
3
import io.oxygenhub.android.FakeHubApi
4
import io.oxygenhub.android.api.FlowRun
5
import io.oxygenhub.android.api.HubException
6
import io.oxygenhub.android.api.UserFlowRun
7
import io.oxygenhub.android.api.UserSky
8
import io.oxygenhub.android.widget.SkyBitmap
9
import io.oxygenhub.android.widget.WidgetData
10
import kotlinx.coroutines.test.runTest
11
import org.junit.Assert.assertEquals
12
import org.junit.Assert.assertFalse
13
import org.junit.Assert.assertNotEquals
14
import org.junit.Assert.assertNull
15
import org.junit.Assert.assertTrue
16
import org.junit.Test
17
18
class WidgetDataTest {
19
    @Test
20
    fun theWidgetShowsTheLatestRunsAndCountsAndAsksForASignInWithoutOne() = runTest {
21
        val api = FakeHubApi()
22
        api.userFlowRuns = { (1L..5L).map { UserFlowRun("andy/a", FlowRun(number = it, status = "passed")) } }
23
        val d = WidgetData.load(api)
24
        assertEquals(listOf(1L, 2L, 3L), d.runs.map { it.run.number })
25
        assertEquals(1, d.reviews)
26
        assertFalse(WidgetData.load(null).signedIn)
27
    }
28
29
    @Test
30
    fun aPartTheHubCannotGiveIsLeftOutNotTheWholeWidget() = runTest {
31
        val api = FakeHubApi()
32
        api.userFlowRuns = { throw HubException.Http(404, "no such API route") }
33
        val d = WidgetData.load(api)
34
        assertEquals(emptyList<UserFlowRun>(), d.runs)
35
        assertEquals(1, d.reviews)
36
        api.userProposals = { throw HubException.Http(500, "down") }
37
        assertNull(WidgetData.load(api).reviews)
38
    }
39
40
    @Test
41
    fun theWidgetFetchesYourSkyAndALostSkyLeavesThePlainSurface() = runTest {
42
        val api = FakeHubApi()
43
        val sky = UserSky(now = 10 * 86_400_000L, repos = listOf("andy/a"), events = listOf(listOf(0L, 0L, 9 * 86_400_000L, 3L)))
44
        api.userSky = { sky }
45
        assertEquals(sky, WidgetData.load(api).sky)
46
        api.userSky = { throw HubException.Http(404, "no such API route") }
47
        val d = WidgetData.load(api)
48
        assertNull(d.sky)
49
        assertEquals(1, d.reviews)
50
    }
51
52
    @Test
53
    fun onlyRealActivityIsDrawn() {
54
        assertFalse("nothing fetched", SkyBitmap.drawable(null))
55
        assertFalse("no repositories", SkyBitmap.drawable(UserSky()))
56
        assertFalse("repositories but no events", SkyBitmap.drawable(UserSky(repos = listOf("andy/a"))))
57
        assertFalse("only malformed events", SkyBitmap.drawable(UserSky(repos = listOf("andy/a"), events = listOf(listOf(7L, 0L, 0L, 1L)))))
58
        assertTrue(SkyBitmap.drawable(UserSky(now = 1, repos = listOf("andy/a"), events = listOf(listOf(0L, 4L, 0L, 1L)))))
59
    }
60
61
    @Test
62
    fun theSkysColoursFollowTheTheme() {
63
        val (light, dark) = SkyBitmap.colours(false) to SkyBitmap.colours(true)
64
        assertEquals(24, light.size)
65
        assertEquals(24, dark.size)
66
        assertNotEquals(light, dark)
67
        assertTrue("every channel is a fraction", (light + dark).all { it in 0f..1f })
68
    }
69
}