|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.firebase.sessions.api |
| 18 | + |
| 19 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 20 | +import com.google.common.truth.Truth.assertThat |
| 21 | +import com.google.firebase.sessions.api.SessionSubscriber.Name.CRASHLYTICS |
| 22 | +import com.google.firebase.sessions.api.SessionSubscriber.Name.PERFORMANCE |
| 23 | +import com.google.firebase.sessions.testing.FakeSessionSubscriber |
| 24 | +import kotlin.time.Duration.Companion.seconds |
| 25 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 26 | +import kotlinx.coroutines.TimeoutCancellationException |
| 27 | +import kotlinx.coroutines.delay |
| 28 | +import kotlinx.coroutines.launch |
| 29 | +import kotlinx.coroutines.runBlocking |
| 30 | +import kotlinx.coroutines.test.runTest |
| 31 | +import kotlinx.coroutines.withTimeout |
| 32 | +import org.junit.After |
| 33 | +import org.junit.Assert.assertThrows |
| 34 | +import org.junit.Test |
| 35 | +import org.junit.runner.RunWith |
| 36 | + |
| 37 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 38 | +@RunWith(AndroidJUnit4::class) |
| 39 | +class FirebaseSessionsDependenciesTest { |
| 40 | + @After |
| 41 | + fun cleanUp() { |
| 42 | + // Reset all dependencies after each test. |
| 43 | + FirebaseSessionsDependencies.reset() |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun register_dependencyAdded_canGet() { |
| 48 | + val crashlyticsSubscriber = FakeSessionSubscriber(sessionSubscriberName = CRASHLYTICS) |
| 49 | + FirebaseSessionsDependencies.addDependency(CRASHLYTICS) |
| 50 | + FirebaseSessionsDependencies.register(crashlyticsSubscriber) |
| 51 | + |
| 52 | + assertThat(FirebaseSessionsDependencies.getSubscriber(CRASHLYTICS)) |
| 53 | + .isEqualTo(crashlyticsSubscriber) |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun register_alreadyRegisteredSameName_ignoresSecondSubscriber() { |
| 58 | + val firstSubscriber = FakeSessionSubscriber(sessionSubscriberName = CRASHLYTICS) |
| 59 | + val secondSubscriber = FakeSessionSubscriber(sessionSubscriberName = CRASHLYTICS) |
| 60 | + |
| 61 | + FirebaseSessionsDependencies.addDependency(CRASHLYTICS) |
| 62 | + |
| 63 | + // Register the first time, no problem. |
| 64 | + FirebaseSessionsDependencies.register(firstSubscriber) |
| 65 | + |
| 66 | + // Attempt to register a second subscriber with the same name. |
| 67 | + FirebaseSessionsDependencies.register(secondSubscriber) |
| 68 | + |
| 69 | + assertThat(FirebaseSessionsDependencies.getSubscriber(CRASHLYTICS)).isEqualTo(firstSubscriber) |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun getSubscriber_dependencyAdded_notRegistered_throws() { |
| 74 | + FirebaseSessionsDependencies.addDependency(PERFORMANCE) |
| 75 | + |
| 76 | + val thrown = |
| 77 | + assertThrows(IllegalStateException::class.java) { |
| 78 | + FirebaseSessionsDependencies.getSubscriber(PERFORMANCE) |
| 79 | + } |
| 80 | + |
| 81 | + assertThat(thrown).hasMessageThat().contains("Subscriber PERFORMANCE has not been registered") |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + fun getSubscriber_notDepended_throws() { |
| 86 | + val thrown = |
| 87 | + assertThrows(IllegalStateException::class.java) { |
| 88 | + // Crashlytics was never added as a dependency. |
| 89 | + FirebaseSessionsDependencies.getSubscriber(CRASHLYTICS) |
| 90 | + } |
| 91 | + |
| 92 | + assertThat(thrown).hasMessageThat().contains("Cannot get dependency CRASHLYTICS") |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + fun getSubscribers_waitsForRegister(): Unit = runBlocking { |
| 97 | + val crashlyticsSubscriber = FakeSessionSubscriber(sessionSubscriberName = CRASHLYTICS) |
| 98 | + FirebaseSessionsDependencies.addDependency(CRASHLYTICS) |
| 99 | + |
| 100 | + // Wait a few seconds and then register. |
| 101 | + launch { |
| 102 | + delay(2.seconds) |
| 103 | + FirebaseSessionsDependencies.register(crashlyticsSubscriber) |
| 104 | + } |
| 105 | + |
| 106 | + // Block until the register happens. |
| 107 | + val subscribers = FirebaseSessionsDependencies.getRegisteredSubscribers() |
| 108 | + |
| 109 | + assertThat(subscribers).containsExactly(CRASHLYTICS, crashlyticsSubscriber) |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + fun getSubscribers_noDependencies() = runTest { |
| 114 | + val subscribers = FirebaseSessionsDependencies.getRegisteredSubscribers() |
| 115 | + |
| 116 | + assertThat(subscribers).isEmpty() |
| 117 | + } |
| 118 | + |
| 119 | + @Test(expected = TimeoutCancellationException::class) |
| 120 | + fun getSubscribers_neverRegister_waitsForever() = runTest { |
| 121 | + FirebaseSessionsDependencies.addDependency(CRASHLYTICS) |
| 122 | + |
| 123 | + // The register never happens, wait until the timeout. |
| 124 | + withTimeout(2.seconds) { FirebaseSessionsDependencies.getRegisteredSubscribers() } |
| 125 | + } |
| 126 | +} |
0 commit comments