• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 package com.android.wallpaper.util
18 
19 import android.content.Context
20 import android.content.res.Resources
21 import android.content.res.XmlResourceParser
22 import androidx.annotation.XmlRes
23 import com.android.wallpaper.module.PartnerProvider
24 import com.android.wallpaper.testing.TestPartnerProvider
25 import com.google.common.truth.Truth.assertThat
26 import dagger.hilt.android.qualifiers.ApplicationContext
27 import dagger.hilt.android.testing.HiltAndroidRule
28 import dagger.hilt.android.testing.HiltAndroidTest
29 import javax.inject.Inject
30 import kotlinx.coroutines.Dispatchers
31 import kotlinx.coroutines.ExperimentalCoroutinesApi
32 import kotlinx.coroutines.test.TestDispatcher
33 import kotlinx.coroutines.test.setMain
34 import org.junit.Assert.assertThrows
35 import org.junit.Before
36 import org.junit.Rule
37 import org.junit.Test
38 import org.junit.runner.RunWith
39 import org.robolectric.RobolectricTestRunner
40 import org.robolectric.annotation.Config
41 import org.robolectric.shadows.ShadowDisplayManager
42 
43 @HiltAndroidTest
44 @OptIn(ExperimentalCoroutinesApi::class)
45 @RunWith(RobolectricTestRunner::class)
46 @Config(shadows = [ShadowDisplayManager::class])
47 class WallpaperParserImplTest {
48 
49     @get:Rule var hiltRule = HiltAndroidRule(this)
50     @Inject @ApplicationContext lateinit var context: Context
51     @Inject lateinit var partnerProvider: TestPartnerProvider
52     @Inject lateinit var mWallpaperXMLParserImpl: WallpaperParserImpl
53     @Inject lateinit var testDispatcher: TestDispatcher
54     private lateinit var resources: Resources
55     private lateinit var packageName: String
56 
57     @Before
setupnull58     fun setup() {
59         hiltRule.inject()
60         Dispatchers.setMain(testDispatcher)
61         mWallpaperXMLParserImpl = WallpaperParserImpl(context, partnerProvider)
62         resources = context.resources
63         partnerProvider.resources = resources
64         packageName = context.packageName
65         partnerProvider.packageName = packageName
66     }
67 
68     /**
69      * This test uses the file wallpapers.xml that is defined in the resources folder to make sure
70      * that we parse categories correctly.
71      */
72     @Test
parseXMLForSystemCategories_shouldReturnCategoriesnull73     fun parseXMLForSystemCategories_shouldReturnCategories() {
74         @XmlRes
75         val wallpapersResId: Int =
76             resources.getIdentifier(PartnerProvider.WALLPAPER_RES_ID, "xml", packageName)
77         assertThat(wallpapersResId).isNotEqualTo(0)
78         val parser: XmlResourceParser = resources.getXml(wallpapersResId)
79 
80         val categories = mWallpaperXMLParserImpl.parseSystemCategories(parser)
81 
82         assertThat(categories).hasSize(1)
83         assertThat(categories[0].collectionId).isEqualTo("category1")
84     }
85 
86     /**
87      * This test uses the file invalid_wallpapers.xml that is defined in the resources folder where
88      * if incorrect tags are defined, we return empty categories.
89      */
90     @Test
parseInvalidXMLForSystemCategories_shouldReturnEmptyCategoriesnull91     fun parseInvalidXMLForSystemCategories_shouldReturnEmptyCategories() {
92         @XmlRes
93         val wallpapersResId: Int = resources.getIdentifier("invalid_wallpapers", "xml", packageName)
94         assertThat(wallpapersResId).isNotEqualTo(0)
95         val parser: XmlResourceParser = resources.getXml(wallpapersResId)
96 
97         val categories = mWallpaperXMLParserImpl.parseSystemCategories(parser)
98 
99         assertThat(categories).hasSize(0)
100     }
101 
102     /**
103      * This test uses the file exception_wallpapers.xml that is defined in the resources folder
104      * where if some mandatory attributes aren't defined, an exception will be thrown.
105      */
106     @Test
parseInvalidXMLForSystemCategories_shouldThrowExceptionnull107     fun parseInvalidXMLForSystemCategories_shouldThrowException() {
108         @XmlRes
109         val wallpapersResId: Int =
110             resources.getIdentifier("exception_wallpapers", "xml", packageName)
111         assertThat(wallpapersResId).isNotEqualTo(0)
112         val parser: XmlResourceParser = resources.getXml(wallpapersResId)
113 
114         assertThat(
115                 assertThrows(NullPointerException::class.java) {
116                     mWallpaperXMLParserImpl.parseSystemCategories(parser)
117                 }
118             )
119             .isNotNull()
120     }
121 
122     /**
123      * This test uses the file strings.xml that is defined in resources folder to make sure we parse
124      * partner wallpaper info correctly.
125      */
126     @Test
parseValidPartnerWallpaperInfoXml_shouldReturnWallpaperInfonull127     fun parseValidPartnerWallpaperInfoXml_shouldReturnWallpaperInfo() {
128         val wallpaperInfo = mWallpaperXMLParserImpl.parsePartnerWallpaperInfoResources()
129 
130         assertThat(wallpaperInfo).isNotNull()
131         assertThat(wallpaperInfo).hasSize(1)
132         assertThat(wallpaperInfo[0].getCollectionId(context)).isEqualTo("on_device_wallpapers")
133     }
134 }
135