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.data.repository
19 
20 import androidx.test.filters.SmallTest
21 import com.android.wallpaper.testing.collectLastValue
22 import com.android.wallpaper.testing.snapshot
23 import com.google.common.truth.Truth.assertThat
24 import kotlinx.coroutines.ExperimentalCoroutinesApi
25 import kotlinx.coroutines.test.runTest
26 import org.junit.Before
27 import org.junit.Test
28 import org.junit.runner.RunWith
29 import org.junit.runners.JUnit4
30 
31 @OptIn(ExperimentalCoroutinesApi::class)
32 @SmallTest
33 @RunWith(JUnit4::class)
34 class UndoRepositoryTest {
35 
36     private lateinit var underTest: UndoRepository
37 
38     @Before
setUpnull39     fun setUp() {
40         underTest = UndoRepository()
41     }
42 
43     @Test
put and get initial snapshotnull44     fun `put and get initial snapshot`() {
45         val ownerId1 = 1
46         val ownerId2 = 2
47 
48         underTest.putSnapshot(ownerId1, snapshot(ownerId1, 1))
49         underTest.putSnapshot(ownerId2, snapshot(ownerId2, 1))
50 
51         assertThat(underTest.getSnapshot(ownerId1)).isEqualTo(snapshot(ownerId1, 1))
52         assertThat(underTest.getSnapshot(ownerId2)).isEqualTo(snapshot(ownerId2, 1))
53     }
54 
55     @Test
dirtynull56     fun dirty() = runTest {
57         val ownerId1 = 1
58         val ownerId2 = 2
59         val isUndoable = collectLastValue(underTest.isAnythingDirty)
60 
61         assertThat(isUndoable()).isFalse()
62         assertThat(underTest.getAllDirty()).isEmpty()
63 
64         underTest.putDirty(ownerId1, true)
65         assertThat(isUndoable()).isTrue()
66         assertThat(underTest.getAllDirty()).isEqualTo(setOf(ownerId1))
67 
68         underTest.putDirty(ownerId2, true)
69         assertThat(isUndoable()).isTrue()
70         assertThat(underTest.getAllDirty()).isEqualTo(setOf(ownerId1, ownerId2))
71 
72         underTest.putDirty(ownerId1, false)
73         assertThat(isUndoable()).isTrue()
74         assertThat(underTest.getAllDirty()).isEqualTo(setOf(ownerId2))
75 
76         underTest.putDirty(ownerId2, false)
77         assertThat(isUndoable()).isFalse()
78         assertThat(underTest.getAllDirty()).isEmpty()
79     }
80 }
81