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 package com.android.wallpaper.module
17 
18 import android.content.Context
19 import androidx.test.core.app.ApplicationProvider
20 import com.android.wallpaper.model.LiveWallpaperPrefMetadata
21 import com.android.wallpaper.model.StaticWallpaperPrefMetadata
22 import com.android.wallpaper.module.WallpaperPreferenceKeys.NoBackupKeys
23 import com.google.common.truth.Truth.assertThat
24 import org.junit.Test
25 import org.junit.runner.RunWith
26 import org.robolectric.RobolectricTestRunner
27 
28 @RunWith(RobolectricTestRunner::class)
29 class DefaultWallpaperPreferencesTest {
30 
31     private val wallpaperPreferences: DefaultWallpaperPreferences =
32         DefaultWallpaperPreferences(ApplicationProvider.getApplicationContext())
33 
34     @Test
setHomeStaticImageWallpaperMetadata_metadataShouldBeSavedToPreferencesnull35     fun setHomeStaticImageWallpaperMetadata_metadataShouldBeSavedToPreferences() {
36         wallpaperPreferences.setHomeStaticImageWallpaperMetadata(
37             StaticWallpaperPrefMetadata(
38                 attributions = listOf("attr1", "attr2"),
39                 actionUrl = "http://www.google.com/",
40                 collectionId = "cultural_events",
41                 hashCode = 10013L,
42                 managerId = 3,
43                 remoteId = "ocean",
44             )
45         )
46 
47         val sharedPref =
48             (ApplicationProvider.getApplicationContext() as Context).getSharedPreferences(
49                 DefaultWallpaperPreferences.PREFS_NAME,
50                 Context.MODE_PRIVATE
51             )
52         assertThat(sharedPref.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ATTRIB_1, null))
53             .isEqualTo("attr1")
54         assertThat(sharedPref.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ATTRIB_2, null))
55             .isEqualTo("attr2")
56         assertThat(
57                 sharedPref.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ACTION_URL, null)
58             )
59             .isEqualTo("http://www.google.com/")
60         assertThat(
61                 sharedPref.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_COLLECTION_ID, null)
62             )
63             .isEqualTo("cultural_events")
64         assertThat(sharedPref.getLong(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_HASH_CODE, 0L))
65             .isEqualTo(10013)
66         val noBackupPref =
67             (ApplicationProvider.getApplicationContext() as Context).getSharedPreferences(
68                 DefaultWallpaperPreferences.NO_BACKUP_PREFS_NAME,
69                 Context.MODE_PRIVATE
70             )
71         assertThat(noBackupPref.getInt(NoBackupKeys.KEY_HOME_WALLPAPER_MANAGER_ID, 0)).isEqualTo(3)
72         assertThat(noBackupPref.getString(NoBackupKeys.KEY_HOME_WALLPAPER_REMOTE_ID, null))
73             .isEqualTo("ocean")
74     }
75 
76     @Test
setHomeLiveWallpaperMetadata_metadataShouldBeSavedToPreferencesnull77     fun setHomeLiveWallpaperMetadata_metadataShouldBeSavedToPreferences() {
78         wallpaperPreferences.setHomeLiveWallpaperMetadata(
79             LiveWallpaperPrefMetadata(
80                 attributions = listOf("attr1", "attr2"),
81                 serviceName =
82                     "com.google.pixel.livewallpaper.dioramas.fiji.wallpapers.FijiWallpaper",
83                 effectName = null,
84                 collectionId = "living_universe",
85                 managerId = 2,
86             )
87         )
88 
89         val sharedPref =
90             (ApplicationProvider.getApplicationContext() as Context).getSharedPreferences(
91                 DefaultWallpaperPreferences.PREFS_NAME,
92                 Context.MODE_PRIVATE
93             )
94         assertThat(sharedPref.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ATTRIB_1, null))
95             .isEqualTo("attr1")
96         assertThat(sharedPref.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ATTRIB_2, null))
97             .isEqualTo("attr2")
98         assertThat(
99                 sharedPref.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_COLLECTION_ID, null)
100             )
101             .isEqualTo("living_universe")
102         val noBackupPref =
103             (ApplicationProvider.getApplicationContext() as Context).getSharedPreferences(
104                 DefaultWallpaperPreferences.NO_BACKUP_PREFS_NAME,
105                 Context.MODE_PRIVATE
106             )
107         assertThat(noBackupPref.getString(NoBackupKeys.KEY_HOME_WALLPAPER_SERVICE_NAME, null))
108             .isEqualTo("com.google.pixel.livewallpaper.dioramas.fiji.wallpapers.FijiWallpaper")
109         assertThat(noBackupPref.getString(NoBackupKeys.KEY_HOME_WALLPAPER_EFFECTS, null))
110             .isEqualTo(null)
111         assertThat(noBackupPref.getInt(NoBackupKeys.KEY_HOME_WALLPAPER_MANAGER_ID, 0)).isEqualTo(2)
112     }
113 
114     @Test
setLockStaticImageWallpaperMetadata_metadataShouldBeSavedToPreferencesnull115     fun setLockStaticImageWallpaperMetadata_metadataShouldBeSavedToPreferences() {
116         wallpaperPreferences.setLockStaticImageWallpaperMetadata(
117             StaticWallpaperPrefMetadata(
118                 attributions = listOf("attr1", "attr2"),
119                 actionUrl = "http://www.google.com/",
120                 collectionId = "cultural_events",
121                 hashCode = 10013L,
122                 managerId = 3,
123                 remoteId = "ocean",
124             )
125         )
126 
127         val sharedPref =
128             (ApplicationProvider.getApplicationContext() as Context).getSharedPreferences(
129                 DefaultWallpaperPreferences.PREFS_NAME,
130                 Context.MODE_PRIVATE
131             )
132         assertThat(sharedPref.getString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ATTRIB_1, null))
133             .isEqualTo("attr1")
134         assertThat(sharedPref.getString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ATTRIB_2, null))
135             .isEqualTo("attr2")
136         assertThat(
137                 sharedPref.getString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ACTION_URL, null)
138             )
139             .isEqualTo("http://www.google.com/")
140         assertThat(
141                 sharedPref.getString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_COLLECTION_ID, null)
142             )
143             .isEqualTo("cultural_events")
144         assertThat(sharedPref.getLong(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_HASH_CODE, 0L))
145             .isEqualTo(10013)
146         val noBackupPref =
147             (ApplicationProvider.getApplicationContext() as Context).getSharedPreferences(
148                 DefaultWallpaperPreferences.NO_BACKUP_PREFS_NAME,
149                 Context.MODE_PRIVATE
150             )
151         assertThat(noBackupPref.getInt(NoBackupKeys.KEY_LOCK_WALLPAPER_MANAGER_ID, 0)).isEqualTo(3)
152         assertThat(noBackupPref.getString(NoBackupKeys.KEY_LOCK_WALLPAPER_REMOTE_ID, null))
153             .isEqualTo("ocean")
154     }
155 
156     @Test
setLockLiveWallpaperMetadata_metadataShouldBeSavedToPreferencesnull157     fun setLockLiveWallpaperMetadata_metadataShouldBeSavedToPreferences() {
158         wallpaperPreferences.setLockLiveWallpaperMetadata(
159             LiveWallpaperPrefMetadata(
160                 attributions = listOf("attr1", "attr2"),
161                 serviceName =
162                     "com.google.pixel.livewallpaper.dioramas.fiji.wallpapers.FijiWallpaper",
163                 effectName = null,
164                 collectionId = "living_universe",
165                 managerId = 2,
166             )
167         )
168 
169         val sharedPref =
170             (ApplicationProvider.getApplicationContext() as Context).getSharedPreferences(
171                 DefaultWallpaperPreferences.PREFS_NAME,
172                 Context.MODE_PRIVATE
173             )
174         assertThat(sharedPref.getString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ATTRIB_1, null))
175             .isEqualTo("attr1")
176         assertThat(sharedPref.getString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ATTRIB_2, null))
177             .isEqualTo("attr2")
178         assertThat(
179                 sharedPref.getString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_COLLECTION_ID, null)
180             )
181             .isEqualTo("living_universe")
182         val noBackupPref =
183             (ApplicationProvider.getApplicationContext() as Context).getSharedPreferences(
184                 DefaultWallpaperPreferences.NO_BACKUP_PREFS_NAME,
185                 Context.MODE_PRIVATE
186             )
187         assertThat(noBackupPref.getString(NoBackupKeys.KEY_LOCK_WALLPAPER_SERVICE_NAME, null))
188             .isEqualTo("com.google.pixel.livewallpaper.dioramas.fiji.wallpapers.FijiWallpaper")
189         assertThat(noBackupPref.getString(NoBackupKeys.KEY_LOCK_WALLPAPER_EFFECTS, null))
190             .isEqualTo(null)
191         assertThat(noBackupPref.getInt(NoBackupKeys.KEY_LOCK_WALLPAPER_MANAGER_ID, 0)).isEqualTo(2)
192     }
193 }
194