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.testing
19
20 import com.android.wallpaper.picker.undo.domain.interactor.SnapshotRestorer
21 import com.android.wallpaper.picker.undo.domain.interactor.SnapshotStore
22 import com.android.wallpaper.picker.undo.shared.model.RestorableSnapshot
23
24 val FAKE_RESTORERS =
25 mapOf(
26 1 to FakeSnapshotRestorer(1),
27 2 to FakeSnapshotRestorer(2),
28 3 to FakeSnapshotRestorer(3),
29 )
30
snapshotnull31 fun snapshot(ownerId: Int, version: Int): RestorableSnapshot {
32 return RestorableSnapshot(
33 mapOf(
34 KEY_OWNER_ID to "$ownerId",
35 KEY_VERSION to "$version",
36 )
37 )
38 }
39
40 class FakeSnapshotRestorer(
41 private val ownerId: Int,
42 ) : SnapshotRestorer {
43 private lateinit var store: SnapshotStore
44 var restored: RestorableSnapshot? = null
45 private set
46
updatenull47 fun update(version: Int) {
48 store.store(snapshot(ownerId, version))
49 }
50
setUpSnapshotRestorernull51 override suspend fun setUpSnapshotRestorer(
52 store: SnapshotStore,
53 ): RestorableSnapshot {
54 this.store = store
55 return snapshot(
56 ownerId = ownerId,
57 version = 0,
58 )
59 }
60
restoreToSnapshotnull61 override suspend fun restoreToSnapshot(snapshot: RestorableSnapshot) {
62 restored = snapshot
63 }
64 }
65
66 private const val KEY_OWNER_ID = "ownerId"
67 private const val KEY_VERSION = "version"
68