Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 android.content.res.loader.test |
| 18 | |
| 19 | import android.content.Context |
| 20 | import android.content.res.AssetManager |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 21 | import android.content.res.Resources |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 22 | import android.content.res.loader.AssetsProvider |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 23 | import android.content.res.loader.ResourcesProvider |
| 24 | import android.os.ParcelFileDescriptor |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 25 | import androidx.test.InstrumentationRegistry |
| 26 | import org.junit.After |
| 27 | import org.junit.Before |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 28 | import org.mockito.ArgumentMatchers.anyInt |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 29 | import org.mockito.ArgumentMatchers.eq |
| 30 | import org.mockito.Mockito.doAnswer |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 31 | import org.mockito.Mockito.mock |
| 32 | import java.io.Closeable |
| 33 | |
| 34 | abstract class ResourceLoaderTestBase { |
| 35 | |
| 36 | open lateinit var dataType: DataType |
| 37 | |
| 38 | protected lateinit var context: Context |
| 39 | protected open val resources: Resources |
| 40 | get() = context.resources |
| 41 | protected open val assets: AssetManager |
| 42 | get() = resources.assets |
| 43 | |
| 44 | // Track opened streams and ResourcesProviders to close them after testing |
| 45 | private val openedObjects = mutableListOf<Closeable>() |
| 46 | |
| 47 | @Before |
| 48 | fun setUpBase() { |
| 49 | context = InstrumentationRegistry.getTargetContext() |
| 50 | } |
| 51 | |
| 52 | @After |
| 53 | fun removeAllLoaders() { |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 54 | resources.clearLoaders() |
| 55 | context.applicationContext.resources.clearLoaders() |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 56 | openedObjects.forEach { |
| 57 | try { |
| 58 | it.close() |
| 59 | } catch (ignored: Exception) { |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 64 | protected fun String.openProvider( |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 65 | dataType: DataType = this@ResourceLoaderTestBase.dataType |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 66 | ): ResourcesProvider = when (dataType) { |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 67 | DataType.APK -> { |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 68 | context.copiedRawFile("${this}Apk").use { |
| 69 | ResourcesProvider.loadFromApk(it, mock(AssetsProvider::class.java)) |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 70 | }.also { openedObjects += it } |
| 71 | } |
| 72 | DataType.ARSC -> { |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 73 | openArsc(this, mock(AssetsProvider::class.java)) |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 74 | } |
| 75 | DataType.SPLIT -> { |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 76 | ResourcesProvider.loadFromSplit(context, this) |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 77 | } |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 78 | DataType.ASSET -> { |
| 79 | val assetsProvider = mock(AssetsProvider::class.java) |
| 80 | doAnswer { byteInputStream() }.`when`(assetsProvider) |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 81 | .loadAsset(eq("assets/Asset.txt"), anyInt()) |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 82 | ResourcesProvider.empty(assetsProvider) |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 83 | } |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 84 | DataType.ASSET_FD -> { |
| 85 | val assetsProvider = mock(AssetsProvider::class.java) |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 86 | doAnswer { |
| 87 | val file = context.filesDir.resolve("Asset.txt") |
| 88 | file.writeText(this) |
| 89 | ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY) |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 90 | }.`when`(assetsProvider).loadAssetParcelFd("assets/Asset.txt") |
| 91 | ResourcesProvider.empty(assetsProvider) |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 92 | } |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 93 | DataType.NON_ASSET -> { |
| 94 | val assetsProvider = mock(AssetsProvider::class.java) |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 95 | doAnswer { |
| 96 | val file = context.filesDir.resolve("NonAsset.txt") |
| 97 | file.writeText(this) |
| 98 | ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY) |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 99 | }.`when`(assetsProvider).loadAssetParcelFd("NonAsset.txt") |
| 100 | ResourcesProvider.empty(assetsProvider) |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 101 | } |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 102 | DataType.NON_ASSET_DRAWABLE -> { |
| 103 | val assetsProvider = mock(AssetsProvider::class.java) |
| 104 | doAnswer { context.copiedRawFile(this) }.`when`(assetsProvider) |
| 105 | .loadAssetParcelFd("res/drawable-nodpi-v4/non_asset_drawable.xml") |
| 106 | openArsc(this, assetsProvider) |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 107 | } |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 108 | DataType.NON_ASSET_BITMAP -> { |
| 109 | val assetsProvider = mock(AssetsProvider::class.java) |
| 110 | doAnswer { resources.openRawResource(rawFile(this)) } |
| 111 | .`when`(assetsProvider) |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 112 | .loadAsset(eq("res/drawable-nodpi-v4/non_asset_bitmap.png"), anyInt()) |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 113 | openArsc(this, assetsProvider) |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 114 | } |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 115 | DataType.NON_ASSET_LAYOUT -> { |
| 116 | val assetsProvider = mock(AssetsProvider::class.java) |
| 117 | doAnswer { resources.openRawResource(rawFile(this)) }.`when`(assetsProvider) |
| 118 | .loadAsset(eq("res/layout/layout.xml"), anyInt()) |
| 119 | doAnswer { context.copiedRawFile(this) }.`when`(assetsProvider) |
| 120 | .loadAssetParcelFd("res/layout/layout.xml") |
| 121 | openArsc(this, assetsProvider) |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 125 | protected fun openArsc(rawName: String, assetsProvider: AssetsProvider): ResourcesProvider { |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 126 | return context.copiedRawFile("${rawName}Arsc") |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 127 | .use { ResourcesProvider.loadFromTable(it, assetsProvider) } |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 128 | .also { openedObjects += it } |
| 129 | } |
| 130 | |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 131 | enum class DataType { |
| 132 | APK, |
| 133 | ARSC, |
| 134 | SPLIT, |
| 135 | ASSET, |
| 136 | ASSET_FD, |
| 137 | NON_ASSET, |
| 138 | NON_ASSET_DRAWABLE, |
| 139 | NON_ASSET_BITMAP, |
| 140 | NON_ASSET_LAYOUT, |
| 141 | } |
| 142 | } |