1 /* 2 * Copyright (C) 2023 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.customization.model.mode 19 20 import androidx.test.filters.SmallTest 21 import com.android.wallpaper.testing.FakeSnapshotStore 22 import com.google.common.truth.Truth.assertThat 23 import kotlinx.coroutines.ExperimentalCoroutinesApi 24 import kotlinx.coroutines.test.StandardTestDispatcher 25 import kotlinx.coroutines.test.TestScope 26 import kotlinx.coroutines.test.runTest 27 import org.junit.Before 28 import org.junit.Test 29 import org.junit.runner.RunWith 30 import org.junit.runners.JUnit4 31 32 @OptIn(ExperimentalCoroutinesApi::class) 33 @SmallTest 34 @RunWith(JUnit4::class) 35 class DarkModeSnapshotRestorerTest { 36 37 private lateinit var underTest: DarkModeSnapshotRestorer 38 private lateinit var testScope: TestScope 39 40 private var isActive = false 41 42 @Before setUpnull43 fun setUp() { 44 val testDispatcher = StandardTestDispatcher() 45 testScope = TestScope(testDispatcher) 46 underTest = 47 DarkModeSnapshotRestorer( 48 backgroundDispatcher = testDispatcher, 49 isActive = { isActive }, 50 setActive = { isActive = it }, 51 ) 52 } 53 54 @Test set up and restore - activenull55 fun `set up and restore - active`() = 56 testScope.runTest { 57 isActive = true 58 59 val store = FakeSnapshotStore() 60 store.store(underTest.setUpSnapshotRestorer(store = store)) 61 val storedSnapshot = store.retrieve() 62 63 underTest.restoreToSnapshot(snapshot = storedSnapshot) 64 assertThat(isActive).isTrue() 65 } 66 67 @Test set up and restore - inactivenull68 fun `set up and restore - inactive`() = 69 testScope.runTest { 70 isActive = false 71 72 val store = FakeSnapshotStore() 73 store.store(underTest.setUpSnapshotRestorer(store = store)) 74 val storedSnapshot = store.retrieve() 75 76 underTest.restoreToSnapshot(snapshot = storedSnapshot) 77 assertThat(isActive).isFalse() 78 } 79 80 @Test set up - deactivate - restore to activenull81 fun `set up - deactivate - restore to active`() = 82 testScope.runTest { 83 isActive = true 84 val store = FakeSnapshotStore() 85 store.store(underTest.setUpSnapshotRestorer(store = store)) 86 val initialSnapshot = store.retrieve() 87 88 underTest.store(isActivated = false) 89 90 underTest.restoreToSnapshot(snapshot = initialSnapshot) 91 assertThat(isActive).isTrue() 92 } 93 94 @Test set up - activate - restore to inactivenull95 fun `set up - activate - restore to inactive`() = 96 testScope.runTest { 97 isActive = false 98 val store = FakeSnapshotStore() 99 store.store(underTest.setUpSnapshotRestorer(store = store)) 100 val initialSnapshot = store.retrieve() 101 102 underTest.store(isActivated = true) 103 104 underTest.restoreToSnapshot(snapshot = initialSnapshot) 105 assertThat(isActive).isFalse() 106 } 107 } 108