• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.R
21 import com.android.wallpaper.model.StaticWallpaperMetadata
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             StaticWallpaperMetadata(
38                 attributions = listOf("attr1", "attr2"),
39                 actionUrl = "http://www.google.com/",
40                 actionLabelRes = R.string.explore,
41                 actionIconRes = R.drawable.ic_explore_24px,
42                 collectionId = "cultural_events",
43                 hashCode = 10013L,
44                 managerId = 3,
45                 remoteId = "ocean",
46             )
47         )
48 
49         val pref =
50             (ApplicationProvider.getApplicationContext() as Context).getSharedPreferences(
51                 DefaultWallpaperPreferences.PREFS_NAME,
52                 Context.MODE_PRIVATE
53             )
54         assertThat(pref.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ATTRIB_1, null))
55             .isEqualTo("attr1")
56         assertThat(pref.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ATTRIB_2, null))
57             .isEqualTo("attr2")
58         assertThat(pref.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ACTION_URL, null))
59             .isEqualTo("http://www.google.com/")
60         assertThat(
61                 pref.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ACTION_LABEL_RES, null)
62             )
63             .isEqualTo("com.android.wallpaper:string/explore")
64         assertThat(pref.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_ACTION_ICON_RES, null))
65             .isEqualTo("com.android.wallpaper:drawable/ic_explore_24px")
66         assertThat(pref.getString(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_COLLECTION_ID, null))
67             .isEqualTo("cultural_events")
68         assertThat(pref.getLong(WallpaperPreferenceKeys.KEY_HOME_WALLPAPER_HASH_CODE, 0L))
69             .isEqualTo(10013)
70         assertThat(pref.getInt(NoBackupKeys.KEY_HOME_WALLPAPER_MANAGER_ID, 0)).isEqualTo(3)
71         assertThat(pref.getString(NoBackupKeys.KEY_HOME_WALLPAPER_REMOTE_ID, null))
72             .isEqualTo("ocean")
73     }
74 
75     @Test
setLockStaticImageWallpaperMetadata_metadataShouldBeSavedToPreferencesnull76     fun setLockStaticImageWallpaperMetadata_metadataShouldBeSavedToPreferences() {
77         wallpaperPreferences.setLockStaticImageWallpaperMetadata(
78             StaticWallpaperMetadata(
79                 attributions = listOf("attr1", "attr2"),
80                 actionUrl = "http://www.google.com/",
81                 actionLabelRes = R.string.explore,
82                 actionIconRes = R.drawable.ic_explore_24px,
83                 collectionId = "cultural_events",
84                 hashCode = 10013L,
85                 managerId = 3,
86                 remoteId = "ocean",
87             )
88         )
89 
90         val pref =
91             (ApplicationProvider.getApplicationContext() as Context).getSharedPreferences(
92                 DefaultWallpaperPreferences.PREFS_NAME,
93                 Context.MODE_PRIVATE
94             )
95         assertThat(pref.getString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ATTRIB_1, null))
96             .isEqualTo("attr1")
97         assertThat(pref.getString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ATTRIB_2, null))
98             .isEqualTo("attr2")
99         assertThat(pref.getString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ACTION_URL, null))
100             .isEqualTo("http://www.google.com/")
101         assertThat(
102                 pref.getString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ACTION_LABEL_RES, null)
103             )
104             .isEqualTo("com.android.wallpaper:string/explore")
105         assertThat(pref.getString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_ACTION_ICON_RES, null))
106             .isEqualTo("com.android.wallpaper:drawable/ic_explore_24px")
107         assertThat(pref.getString(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_COLLECTION_ID, null))
108             .isEqualTo("cultural_events")
109         assertThat(pref.getLong(WallpaperPreferenceKeys.KEY_LOCK_WALLPAPER_HASH_CODE, 0L))
110             .isEqualTo(10013)
111         assertThat(pref.getInt(NoBackupKeys.KEY_LOCK_WALLPAPER_MANAGER_ID, 0)).isEqualTo(3)
112         assertThat(pref.getString(NoBackupKeys.KEY_LOCK_WALLPAPER_REMOTE_ID, null))
113             .isEqualTo("ocean")
114     }
115 }
116