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.ui.viewmodel 19 20 import androidx.test.filters.SmallTest 21 import com.android.wallpaper.module.logging.TestUserEventLogger 22 import com.android.wallpaper.picker.undo.data.repository.UndoRepository 23 import com.android.wallpaper.picker.undo.domain.interactor.UndoInteractor 24 import com.android.wallpaper.testing.FAKE_RESTORERS 25 import com.android.wallpaper.testing.collectLastValue 26 import com.google.common.truth.Truth.assertThat 27 import kotlinx.coroutines.ExperimentalCoroutinesApi 28 import kotlinx.coroutines.test.StandardTestDispatcher 29 import kotlinx.coroutines.test.TestScope 30 import kotlinx.coroutines.test.runTest 31 import org.junit.Before 32 import org.junit.Test 33 import org.junit.runner.RunWith 34 import org.junit.runners.JUnit4 35 36 @OptIn(ExperimentalCoroutinesApi::class) 37 @SmallTest 38 @RunWith(JUnit4::class) 39 class UndoViewModelTest { 40 41 private lateinit var underTest: UndoViewModel 42 43 private lateinit var testScope: TestScope 44 private lateinit var interactor: UndoInteractor 45 46 @Before setUpnull47 fun setUp() { 48 val testDispatcher = StandardTestDispatcher() 49 testScope = TestScope(testDispatcher) 50 interactor = 51 UndoInteractor( 52 scope = testScope.backgroundScope, 53 repository = UndoRepository(), 54 restorerByOwnerId = FAKE_RESTORERS, 55 ) 56 57 underTest = 58 UndoViewModel( 59 interactor = interactor, 60 logger = TestUserEventLogger(), 61 ) 62 } 63 64 @Test revertnull65 fun revert() = 66 testScope.runTest { 67 val isRevertButtonVisible = collectLastValue(underTest.isRevertButtonVisible) 68 val dialog = collectLastValue(underTest.dialog) 69 assertThat(isRevertButtonVisible()).isFalse() 70 assertThat(dialog()).isNull() 71 72 // Start the session without anything to revert. 73 interactor.startSession() 74 assertThat(isRevertButtonVisible()).isFalse() 75 assertThat(dialog()).isNull() 76 77 // Record a change that can be reverted. 78 FAKE_RESTORERS[1]?.update(2) 79 assertThat(isRevertButtonVisible()).isTrue() 80 assertThat(dialog()).isNull() 81 82 // Click the revert button. 83 underTest.onRevertButtonClicked() 84 assertThat(isRevertButtonVisible()).isTrue() 85 assertThat(dialog()).isNotNull() 86 87 // Cancel the revert. 88 dialog()?.onDismissed?.invoke() 89 assertThat(isRevertButtonVisible()).isTrue() 90 assertThat(dialog()).isNull() 91 92 // Click the revert button again. 93 underTest.onRevertButtonClicked() 94 assertThat(isRevertButtonVisible()).isTrue() 95 assertThat(dialog()).isNotNull() 96 97 // Confirm the revert. 98 dialog()?.buttons?.last()?.onClicked?.invoke() 99 assertThat(isRevertButtonVisible()).isFalse() 100 } 101 } 102