Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5faf9e92 authored by Andre Le's avatar Andre Le Committed by Android (Google) Code Review
Browse files

Merge changes Ic51296ce,I0912e07e into main

* changes:
  Update ClockInteractorTest with more modern library usage
  ComposeClock: Move shade's clock interactor and repository to common dir
parents 183cd680 48db4828
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@
 * limitations under the License.
 */

package com.android.systemui.shade.data.repository
package com.android.systemui.clock.data.repository

import android.app.AlarmManager
import androidx.test.ext.junit.runners.AndroidJUnit4
@@ -30,17 +30,16 @@ import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.times
import org.mockito.Mockito.verify

@SmallTest
@RunWith(AndroidJUnit4::class)
class ShadeHeaderClockRepositoryTest : SysuiTestCase() {
class ClockRepositoryTest : SysuiTestCase() {
    private val kosmos = testKosmos()
    private val testScope = kosmos.testScope
    private val nextAlarmController = kosmos.nextAlarmController

    val underTest = kosmos.shadeHeaderClockRepository
    val underTest = kosmos.clockRepository

    @Test
    fun nextAlarmIntent_updates() =
+27 −39
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@
 * limitations under the License.
 */

package com.android.systemui.shade.domain.interactor
package com.android.systemui.clock.domain.interactor

import android.app.AlarmManager
import android.content.Intent
@@ -23,63 +23,57 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.broadcast.broadcastDispatcher
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.coroutines.collectValues
import com.android.systemui.kosmos.testScope
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.advanceTimeBy
import com.android.systemui.kosmos.collectLastValue
import com.android.systemui.kosmos.collectValues
import com.android.systemui.kosmos.runCurrent
import com.android.systemui.kosmos.runTest
import com.android.systemui.plugins.activityStarter
import com.android.systemui.statusbar.policy.NextAlarmController.NextAlarmChangeCallback
import com.android.systemui.statusbar.policy.nextAlarmController
import com.android.systemui.testKosmos
import com.android.systemui.util.mockito.any
import com.android.systemui.util.mockito.argThat
import com.android.systemui.util.mockito.mock
import com.android.systemui.util.mockito.withArgCaptor
import com.google.common.truth.Truth.assertThat
import java.util.Date
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatcher
import org.mockito.Mockito.verify
import org.mockito.kotlin.any
import org.mockito.kotlin.argThat
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify

@OptIn(ExperimentalCoroutinesApi::class)
@SmallTest
@RunWith(AndroidJUnit4::class)
class ShadeHeaderClockInteractorTest : SysuiTestCase() {
class ClockInteractorTest : SysuiTestCase() {

    private val kosmos = testKosmos()
    private val testScope = kosmos.testScope
    private val activityStarter = kosmos.activityStarter
    private val nextAlarmController = kosmos.nextAlarmController

    private val underTest = kosmos.shadeHeaderClockInteractor
    private val underTest = kosmos.clockInteractor

    @Test
    fun launchClockActivity_default() =
        testScope.runTest {
        kosmos.runTest {
            underTest.launchClockActivity()
            verify(activityStarter)
                .postStartActivityDismissingKeyguard(
                    argThat(IntentMatcherAction(AlarmClock.ACTION_SHOW_ALARMS)),
                    any(),
                    argThat { intent: Intent? -> intent?.action == AlarmClock.ACTION_SHOW_ALARMS },
                    any<Int>(),
                )
        }

    @Test
    fun launchClockActivity_nextAlarmIntent() =
        testScope.runTest {
            val callback =
                withArgCaptor<NextAlarmChangeCallback> {
        kosmos.runTest {
            val captor =
                argumentCaptor<NextAlarmChangeCallback> {
                    verify(nextAlarmController).addCallback(capture())
                }
            callback.onNextAlarmChanged(AlarmManager.AlarmClockInfo(1L, mock()))
            captor.firstValue.onNextAlarmChanged(AlarmManager.AlarmClockInfo(1L, mock()))

            underTest.launchClockActivity()
            verify(activityStarter).postStartActivityDismissingKeyguard(any())
@@ -87,7 +81,7 @@ class ShadeHeaderClockInteractorTest : SysuiTestCase() {

    @Test
    fun onTimezoneOrLocaleChanged_localeAndTimezoneChanged_emitsForEach() =
        testScope.runTest {
        kosmos.runTest {
            val timeZoneOrLocaleChanges by collectValues(underTest.onTimezoneOrLocaleChanged)

            sendIntentActionBroadcast(Intent.ACTION_TIMEZONE_CHANGED)
@@ -100,7 +94,7 @@ class ShadeHeaderClockInteractorTest : SysuiTestCase() {

    @Test
    fun onTimezoneOrLocaleChanged_timeChanged_doesNotEmit() =
        testScope.runTest {
        kosmos.runTest {
            val timeZoneOrLocaleChanges by collectValues(underTest.onTimezoneOrLocaleChanged)
            assertThat(timeZoneOrLocaleChanges).hasSize(1)

@@ -113,7 +107,7 @@ class ShadeHeaderClockInteractorTest : SysuiTestCase() {

    @Test
    fun currentTime_timeChanged() =
        testScope.runTest {
        kosmos.runTest {
            val currentTime by collectLastValue(underTest.currentTime)

            sendIntentActionBroadcast(Intent.ACTION_TIME_CHANGED)
@@ -130,7 +124,7 @@ class ShadeHeaderClockInteractorTest : SysuiTestCase() {

    @Test
    fun currentTime_timeTicked() =
        testScope.runTest {
        kosmos.runTest {
            val currentTime by collectLastValue(underTest.currentTime)

            sendIntentActionBroadcast(Intent.ACTION_TIME_TICK)
@@ -149,14 +143,8 @@ class ShadeHeaderClockInteractorTest : SysuiTestCase() {
        return (date1.time - date2.time).milliseconds
    }

    private fun TestScope.sendIntentActionBroadcast(intentAction: String) {
        kosmos.broadcastDispatcher.sendIntentToMatchingReceiversOnly(context, Intent(intentAction))
    private fun Kosmos.sendIntentActionBroadcast(intentAction: String) {
        broadcastDispatcher.sendIntentToMatchingReceiversOnly(context, Intent(intentAction))
        runCurrent()
    }
}

private class IntentMatcherAction(private val action: String) : ArgumentMatcher<Intent> {
    override fun matches(argument: Intent?): Boolean {
        return argument?.action == action
    }
}
+2 −6
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@
 * limitations under the License.
 */

package com.android.systemui.shade.data.repository
package com.android.systemui.clock.data.repository

import android.app.PendingIntent
import com.android.systemui.dagger.SysUISingleton
@@ -22,11 +22,7 @@ import com.android.systemui.statusbar.policy.NextAlarmController
import javax.inject.Inject

@SysUISingleton
class ShadeHeaderClockRepository
@Inject
constructor(
    nextAlarmController: NextAlarmController,
) {
class ClockRepository @Inject constructor(nextAlarmController: NextAlarmController) {
    private val nextAlarmCallback =
        NextAlarmController.NextAlarmChangeCallback { nextAlarm ->
            nextAlarmIntent = nextAlarm?.showIntent
+4 −4
Original line number Diff line number Diff line
@@ -14,16 +14,16 @@
 * limitations under the License.
 */

package com.android.systemui.shade.domain.interactor
package com.android.systemui.clock.domain.interactor

import android.content.Intent
import android.content.IntentFilter
import android.os.UserHandle
import android.provider.AlarmClock
import com.android.systemui.broadcast.BroadcastDispatcher
import com.android.systemui.clock.data.repository.ClockRepository
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.plugins.ActivityStarter
import com.android.systemui.shade.data.repository.ShadeHeaderClockRepository
import com.android.systemui.util.kotlin.emitOnStart
import com.android.systemui.util.time.SystemClock
import java.util.Date
@@ -32,10 +32,10 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

@SysUISingleton
class ShadeHeaderClockInteractor
class ClockInteractor
@Inject
constructor(
    private val repository: ShadeHeaderClockRepository,
    private val repository: ClockRepository,
    private val activityStarter: ActivityStarter,
    private val broadcastDispatcher: BroadcastDispatcher,
    private val systemClock: SystemClock,
+2 −2
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.ui.unit.IntRect
import com.android.app.tracing.coroutines.launchTraced as launch
import com.android.systemui.battery.BatteryMeterViewController
import com.android.systemui.clock.domain.interactor.ClockInteractor
import com.android.systemui.kairos.ExperimentalKairosApi
import com.android.systemui.kairos.KairosNetwork
import com.android.systemui.lifecycle.ExclusiveActivatable
@@ -45,7 +46,6 @@ import com.android.systemui.scene.shared.model.Overlays
import com.android.systemui.scene.shared.model.TransitionKeys.SlightlyFasterShadeCollapse
import com.android.systemui.shade.ShadeDisplayAware
import com.android.systemui.shade.domain.interactor.PrivacyChipInteractor
import com.android.systemui.shade.domain.interactor.ShadeHeaderClockInteractor
import com.android.systemui.shade.domain.interactor.ShadeInteractor
import com.android.systemui.shade.domain.interactor.ShadeModeInteractor
import com.android.systemui.statusbar.phone.StatusBarLocation
@@ -82,7 +82,7 @@ constructor(
    mobileIconsInteractor: MobileIconsInteractor,
    val mobileIconsViewModel: MobileIconsViewModel,
    private val privacyChipInteractor: PrivacyChipInteractor,
    private val clockInteractor: ShadeHeaderClockInteractor,
    private val clockInteractor: ClockInteractor,
    private val tintedIconManagerFactory: TintedIconManager.Factory,
    private val batteryMeterViewControllerFactory: BatteryMeterViewController.Factory,
    val statusBarIconController: StatusBarIconController,
Loading