|
| 1 | +/* |
| 2 | + * Copyright 2025 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 |
| 18 | + |
| 19 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 20 | +import com.google.common.truth.Truth.assertThat |
| 21 | +import com.google.firebase.sessions.testing.FakeDataStore |
| 22 | +import java.io.IOException |
| 23 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 24 | +import kotlinx.coroutines.flow.catch |
| 25 | +import kotlinx.coroutines.flow.first |
| 26 | +import kotlinx.coroutines.launch |
| 27 | +import kotlinx.coroutines.test.runCurrent |
| 28 | +import kotlinx.coroutines.test.runTest |
| 29 | +import org.junit.Test |
| 30 | +import org.junit.runner.RunWith |
| 31 | + |
| 32 | +/** Tests for the [FakeDataStore] implementation. */ |
| 33 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 34 | +@RunWith(AndroidJUnit4::class) |
| 35 | +internal class FakeDataStoreTest { |
| 36 | + @Test |
| 37 | + fun emitsProvidedValues() = runTest { |
| 38 | + val fakeDataStore = FakeDataStore(23) |
| 39 | + |
| 40 | + val result = mutableListOf<Int>() |
| 41 | + |
| 42 | + // Collect data into result list |
| 43 | + backgroundScope.launch { fakeDataStore.data.collect { result.add(it) } } |
| 44 | + |
| 45 | + fakeDataStore.updateData { 1 } |
| 46 | + fakeDataStore.updateData { 2 } |
| 47 | + fakeDataStore.updateData { 3 } |
| 48 | + fakeDataStore.updateData { 4 } |
| 49 | + |
| 50 | + runCurrent() |
| 51 | + |
| 52 | + assertThat(result).containsExactly(23, 1, 2, 3, 4) |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun throwsProvidedExceptionOnEmit() = runTest { |
| 57 | + val fakeDataStore = FakeDataStore(23) |
| 58 | + |
| 59 | + val result = mutableListOf<String>() |
| 60 | + backgroundScope.launch { |
| 61 | + fakeDataStore.data |
| 62 | + .catch { ex -> result.add(ex.message!!) } |
| 63 | + .collect { result.add(it.toString()) } |
| 64 | + } |
| 65 | + |
| 66 | + fakeDataStore.updateData { 1 } |
| 67 | + fakeDataStore.throwOnNextEmit(IOException("oops")) |
| 68 | + |
| 69 | + runCurrent() |
| 70 | + |
| 71 | + assertThat(result).containsExactly("23", "1", "oops") |
| 72 | + } |
| 73 | + |
| 74 | + @Test(expected = IndexOutOfBoundsException::class) |
| 75 | + fun throwsProvidedExceptionOnUpdateData() = runTest { |
| 76 | + val fakeDataStore = FakeDataStore(23) |
| 77 | + |
| 78 | + fakeDataStore.updateData { 1 } |
| 79 | + fakeDataStore.throwOnNextUpdateData(IndexOutOfBoundsException("oops")) |
| 80 | + |
| 81 | + // Expected to throw |
| 82 | + fakeDataStore.updateData { 2 } |
| 83 | + } |
| 84 | + |
| 85 | + @Test(expected = IllegalArgumentException::class) |
| 86 | + fun throwsFirstProvidedExceptionOnCollect() = runTest { |
| 87 | + val fakeDataStore = FakeDataStore(23, IllegalArgumentException("oops")) |
| 88 | + |
| 89 | + // Expected to throw |
| 90 | + fakeDataStore.data.collect {} |
| 91 | + } |
| 92 | + |
| 93 | + @Test(expected = IllegalStateException::class) |
| 94 | + fun throwsFirstProvidedExceptionOnFirst() = runTest { |
| 95 | + val fakeDataStore = FakeDataStore(23, IllegalStateException("oops")) |
| 96 | + |
| 97 | + // Expected to throw |
| 98 | + fakeDataStore.data.first() |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + fun consistentAfterManyUpdates() = runTest { |
| 103 | + val fakeDataStore = FakeDataStore(0) |
| 104 | + |
| 105 | + var collectResult = 0 |
| 106 | + backgroundScope.launch { fakeDataStore.data.collect { collectResult = it } } |
| 107 | + |
| 108 | + var updateResult = 0 |
| 109 | + // 100 is bigger than the channel buffer size so this will cause suspending |
| 110 | + repeat(100) { updateResult = fakeDataStore.updateData { it.inc() } } |
| 111 | + |
| 112 | + runCurrent() |
| 113 | + |
| 114 | + assertThat(collectResult).isEqualTo(100) |
| 115 | + assertThat(updateResult).isEqualTo(100) |
| 116 | + |
| 117 | + fakeDataStore.close() |
| 118 | + } |
| 119 | +} |
0 commit comments