Android Weekly Notes

Android Weekly Notes #464

2021-06-20  本文已影响0人  圣骑士wind

Android Weekly Issue #464

WebRTC Sample in Kotlin

WebRTC是一个传递视频, 音频和通用数据的平台.

要在两个设备之间传输, 需要一个signaling server.

本文的作者使用了Cloud Firestore作为Signaling Server. 实现了一个简易的视频通话.

Flow/LiveData….What Are They?

这个作者用了一个登录的例子, 代码有点过于详细了.

LiveData的优点:

LiveData的缺点:

Flow的缺点:

Navigation: Conditional Navigation

作者的例子挺好的, Repository还用的DataStore.

这个例子的app有三种模式:

然后介绍了如何测试: (androidTest)

@Test
fun testFirstRun() {
    // Create a mock NavController
    val mockNavController = TestNavHostController(
        ApplicationProvider.getApplicationContext()
    )
    mockNavController.setGraph(R.navigation.nav_graph)
    //...
}

How to test a time-dependent coroutine

如何测试一个依赖时间的协程.

例子还挺好玩的:

测试两分钟做60个俯卧撑:

@Test
fun `makes 60 push ups during 2 minutes exercise`() = runBlockingTest {
    val expectedPushUps = 60

    // Set coroutine clock to exercise duration (2 minutes)
    advanceTimeBy(PushUpTraining.EXERCISE_DURATION)

    // Collect all training states (BodyUp & BodyDown) to a list     
    val results = pushUpTraining.start()
        .toList()

    // Dividing by two because two states represent a push up (BodyUp & BodyDown)
    val actualPushUps = results.size / 2

    assertEquals(
        expected = expectedPushUps,
        actual = actualPushUps,
    )
}

测试一分钟做30个:

@Test
fun `makes 30 push ups in 1 minute of the exercise`() = runBlockingTest {
    val exerciseDurationInMillis = 60000L
    val expectedPushUps = 30

    // Collect all training states (BodyUp & BodyDown) to a list
    val results = mutableListOf<PushUpState>()
    val job = launch {
        pushUpTraining.start()
            .collect { results.add(it) }
    }

    // Set coroutine clock to 1 minute
    advanceTimeBy(exerciseDurationInMillis)
    // Cancel coroutine to check result at specific time
    job.cancel()

    // Dividing by two because two states represent a push up (BodyUp & BodyDown)
    val actualPushUps = results.size / 2

    assertEquals(
        expected = expectedPushUps,
        actual = actualPushUps,
    )
}

Bye XML, it was nice knowing you (pt. 1)

xml的缺点和Compose的优点.

Modular Navigation with Jetpack Compose

Compose的跨module跳转.

Code

Videos

上一篇下一篇

猜你喜欢

热点阅读