1 /* 2 * Copyright (C) 2022 The Android Open Source Project 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 18 package com.android.wallpaper.picker.undo.domain.interactor 19 20 import androidx.test.filters.SmallTest 21 import com.android.wallpaper.picker.undo.data.repository.UndoRepository 22 import com.android.wallpaper.testing.FAKE_RESTORERS 23 import com.android.wallpaper.testing.collectLastValue 24 import com.android.wallpaper.testing.snapshot 25 import com.google.common.truth.Truth.assertThat 26 import kotlinx.coroutines.ExperimentalCoroutinesApi 27 import kotlinx.coroutines.test.StandardTestDispatcher 28 import kotlinx.coroutines.test.TestScope 29 import kotlinx.coroutines.test.runTest 30 import org.junit.Before 31 import org.junit.Test 32 import org.junit.runner.RunWith 33 import org.junit.runners.JUnit4 34 35 @OptIn(ExperimentalCoroutinesApi::class) 36 @SmallTest 37 @RunWith(JUnit4::class) 38 class UndoInteractorTest { 39 40 private lateinit var underTest: UndoInteractor 41 42 private lateinit var testScope: TestScope 43 44 @Before setUpnull45 fun setUp() { 46 val testDispatcher = StandardTestDispatcher() 47 testScope = TestScope(testDispatcher) 48 49 underTest = 50 UndoInteractor( 51 scope = testScope.backgroundScope, 52 repository = UndoRepository(), 53 restorerByOwnerId = FAKE_RESTORERS, 54 ) 55 } 56 57 @Test start session - update - and undo allnull58 fun `start session - update - and undo all`() = 59 testScope.runTest { 60 val isUndoable = collectLastValue(underTest.isUndoable) 61 assertThat(isUndoable()).isFalse() 62 63 underTest.startSession() 64 assertThat(isUndoable()).isFalse() 65 66 FAKE_RESTORERS[1]?.update(2) 67 assertThat(isUndoable()).isTrue() 68 69 FAKE_RESTORERS[1]?.update(0) // This resets back to the initial snapshot 70 assertThat(isUndoable()).isFalse() 71 72 FAKE_RESTORERS[1]?.update(1) 73 FAKE_RESTORERS[2]?.update(2) 74 assertThat(isUndoable()).isTrue() 75 76 underTest.revertAll() 77 assertThat(isUndoable()).isFalse() 78 assertThat(FAKE_RESTORERS[1]?.restored).isEqualTo(snapshot(1, 0)) 79 assertThat(FAKE_RESTORERS[2]?.restored).isEqualTo(snapshot(2, 0)) 80 assertThat(FAKE_RESTORERS[3]?.restored).isNull() 81 } 82 } 83