|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.android.internal.services.applifecycle |
| 7 | + |
| 8 | +import androidx.core.app.ComponentActivity |
| 9 | +import org.junit.jupiter.api.BeforeEach |
| 10 | +import org.junit.jupiter.api.Test |
| 11 | +import org.junit.jupiter.api.extension.ExtendWith |
| 12 | +import org.mockito.Mock |
| 13 | +import org.mockito.Mockito |
| 14 | +import org.mockito.junit.jupiter.MockitoExtension |
| 15 | + |
| 16 | +@ExtendWith(MockitoExtension::class) |
| 17 | +internal class ApplicationStateWatcherTest { |
| 18 | + @Mock |
| 19 | + private lateinit var activity: ComponentActivity |
| 20 | + |
| 21 | + @Mock |
| 22 | + private lateinit var listener1: ApplicationStateListener |
| 23 | + |
| 24 | + @Mock |
| 25 | + private lateinit var listener2: ApplicationStateListener |
| 26 | + |
| 27 | + private lateinit var underTest: ApplicationStateWatcher |
| 28 | + |
| 29 | + @BeforeEach |
| 30 | + fun setUp() { |
| 31 | + underTest = ApplicationStateWatcher() |
| 32 | + underTest.registerListener(listener1) |
| 33 | + underTest.registerListener(listener2) |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + fun appForegrounded() { |
| 38 | + underTest.onStart(activity) |
| 39 | + |
| 40 | + val io = Mockito.inOrder(listener1, listener2) |
| 41 | + io.verify(listener1).onApplicationForegrounded() |
| 42 | + io.verify(listener2).onApplicationForegrounded() |
| 43 | + io.verifyNoMoreInteractions() |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun appBackgrounded() { |
| 48 | + underTest.onStart(activity) |
| 49 | + underTest.onStop(activity) |
| 50 | + |
| 51 | + val io = Mockito.inOrder(listener1, listener2) |
| 52 | + io.verify(listener1).onApplicationForegrounded() |
| 53 | + io.verify(listener2).onApplicationForegrounded() |
| 54 | + io.verify(listener1).onApplicationBackgrounded() |
| 55 | + io.verify(listener2).onApplicationBackgrounded() |
| 56 | + io.verifyNoMoreInteractions() |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun closing() { |
| 61 | + underTest.close() |
| 62 | + |
| 63 | + underTest.onStart(activity) |
| 64 | + underTest.onStop(activity) |
| 65 | + |
| 66 | + Mockito.verifyNoInteractions(listener1, listener2) |
| 67 | + } |
| 68 | +} |
0 commit comments