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.Before 35 import org.junit.Rule 36 import org.junit.Test 37 import org.junit.runner.RunWith 38 import org.robolectric.RobolectricTestRunner 39 import org.robolectric.annotation.Config 40 import org.robolectric.shadows.ShadowDisplayManager 41 42 @HiltAndroidTest 43 @OptIn(ExperimentalCoroutinesApi::class) 44 @RunWith(RobolectricTestRunner::class) 45 @Config(shadows = [ShadowDisplayManager::class]) 46 class WallpaperParserImplTest { 47 48 @get:Rule var hiltRule = HiltAndroidRule(this) 49 @Inject @ApplicationContext lateinit var context: Context 50 @Inject lateinit var partnerProvider: TestPartnerProvider 51 @Inject lateinit var mWallpaperXMLParserImpl: WallpaperParserImpl 52 @Inject lateinit var testDispatcher: TestDispatcher 53 private lateinit var resources: Resources 54 private lateinit var packageName: String 55 56 @Before setupnull57 fun setup() { 58 hiltRule.inject() 59 Dispatchers.setMain(testDispatcher) 60 mWallpaperXMLParserImpl = WallpaperParserImpl(context, partnerProvider) 61 resources = context.resources 62 partnerProvider.resources = resources 63 packageName = context.packageName 64 partnerProvider.packageName = packageName 65 } 66 67 /** 68 * This test uses the file wallpapers.xml that is defined in the resources folder to make sure 69 * that we parse categories correctly. 70 */ 71 @Test parseXMLForSystemCategories_shouldReturnCategoriesnull72 fun parseXMLForSystemCategories_shouldReturnCategories() { 73 @XmlRes 74 val wallpapersResId: Int = 75 resources.getIdentifier(PartnerProvider.WALLPAPER_RES_ID, "xml", packageName) 76 assertThat(wallpapersResId).isNotEqualTo(0) 77 val parser: XmlResourceParser = resources.getXml(wallpapersResId) 78 79 val categories = mWallpaperXMLParserImpl.parseSystemCategories(parser) 80 81 assertThat(categories).hasSize(1) 82 assertThat(categories[0].collectionId).isEqualTo("category1") 83 } 84 85 /** 86 * This test uses the file invalid_wallpapers.xml that is defined in the resources folder where 87 * if incorrect tags are defined, we return empty categories. 88 */ 89 @Test parseInvalidXMLForSystemCategories_shouldReturnEmptyCategoriesnull90 fun parseInvalidXMLForSystemCategories_shouldReturnEmptyCategories() { 91 @XmlRes 92 val wallpapersResId: Int = resources.getIdentifier("invalid_wallpapers", "xml", packageName) 93 assertThat(wallpapersResId).isNotEqualTo(0) 94 val parser: XmlResourceParser = resources.getXml(wallpapersResId) 95 96 val categories = mWallpaperXMLParserImpl.parseSystemCategories(parser) 97 98 assertThat(categories).hasSize(0) 99 } 100 101 /** 102 * This test uses the file exception_wallpapers.xml that is defined in the resources folder 103 * where if some mandatory attributes aren't defined, XMLPullParserException is thrown and empty 104 * list is returned. 105 */ 106 @Test parseInvalidXMLForSystemCategories_shouldReturnEmptyListnull107 fun parseInvalidXMLForSystemCategories_shouldReturnEmptyList() { 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 assertThat(mWallpaperXMLParserImpl.parseSystemCategories(parser)).hasSize(0) 114 } 115 116 /** 117 * This test uses the file strings.xml that is defined in resources folder to make sure we parse 118 * partner wallpaper info correctly. 119 */ 120 @Test parseValidPartnerWallpaperInfoXml_shouldReturnWallpaperInfonull121 fun parseValidPartnerWallpaperInfoXml_shouldReturnWallpaperInfo() { 122 val wallpaperInfo = mWallpaperXMLParserImpl.parsePartnerWallpaperInfoResources() 123 124 assertThat(wallpaperInfo).isNotNull() 125 assertThat(wallpaperInfo).hasSize(1) 126 assertThat(wallpaperInfo[0].getCollectionId(context)).isEqualTo("on_device_wallpapers") 127 } 128 } 129