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.res.loader.DirectoryResourceLoader |
| 20 | import android.content.res.loader.ResourceLoader |
| 21 | import android.graphics.Color |
| 22 | import android.graphics.drawable.BitmapDrawable |
| 23 | import android.graphics.drawable.ColorDrawable |
| 24 | import com.google.common.truth.Truth.assertThat |
| 25 | import org.junit.After |
| 26 | import org.junit.Before |
| 27 | import org.junit.Rule |
| 28 | import org.junit.Test |
| 29 | import org.junit.rules.TestName |
| 30 | import java.io.File |
| 31 | |
| 32 | class DirectoryResourceLoaderTest : ResourceLoaderTestBase() { |
| 33 | |
| 34 | @get:Rule |
| 35 | val testName = TestName() |
| 36 | |
| 37 | private lateinit var testDir: File |
| 38 | private lateinit var loader: ResourceLoader |
| 39 | |
| 40 | @Before |
| 41 | fun setUpTestDir() { |
| 42 | testDir = context.filesDir.resolve("DirectoryResourceLoaderTest_${testName.methodName}") |
| 43 | loader = DirectoryResourceLoader(testDir) |
| 44 | } |
| 45 | |
| 46 | @After |
| 47 | fun deleteTestFiles() { |
| 48 | testDir.deleteRecursively() |
| 49 | } |
| 50 | |
| 51 | @Test |
| 52 | fun loadDrawableXml() { |
| 53 | "nonAssetDrawableOne" writeTo "res/drawable-nodpi-v4/non_asset_drawable.xml" |
| 54 | val provider = openArsc("nonAssetDrawableOne") |
| 55 | |
| 56 | fun getValue() = (resources.getDrawable(R.drawable.non_asset_drawable) as ColorDrawable) |
| 57 | .color |
| 58 | |
| 59 | assertThat(getValue()).isEqualTo(Color.parseColor("#B2D2F2")) |
| 60 | |
| 61 | addLoader(loader to provider) |
| 62 | |
| 63 | assertThat(getValue()).isEqualTo(Color.parseColor("#A3C3E3")) |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | fun loadDrawableBitmap() { |
| 68 | "nonAssetBitmapGreen" writeTo "res/drawable-nodpi-v4/non_asset_bitmap.png" |
| 69 | val provider = openArsc("nonAssetBitmapGreen") |
| 70 | |
| 71 | fun getValue() = (resources.getDrawable(R.drawable.non_asset_bitmap) as BitmapDrawable) |
| 72 | .bitmap.getColor(0, 0).toArgb() |
| 73 | |
| 74 | assertThat(getValue()).isEqualTo(Color.RED) |
| 75 | |
| 76 | addLoader(loader to provider) |
| 77 | |
| 78 | assertThat(getValue()).isEqualTo(Color.GREEN) |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | fun loadXml() { |
| 83 | "layoutOne" writeTo "res/layout/layout.xml" |
| 84 | val provider = openArsc("layoutOne") |
| 85 | |
| 86 | fun getValue() = resources.getLayout(R.layout.layout).advanceToRoot().name |
| 87 | |
| 88 | assertThat(getValue()).isEqualTo("FrameLayout") |
| 89 | |
| 90 | addLoader(loader to provider) |
| 91 | |
| 92 | assertThat(getValue()).isEqualTo("RelativeLayout") |
| 93 | } |
| 94 | |
| 95 | private infix fun String.writeTo(path: String) { |
| 96 | val testFile = testDir.resolve(path) |
| 97 | testFile.parentFile!!.mkdirs() |
| 98 | resources.openRawResource(rawFile(this)) |
| 99 | .copyTo(testFile.outputStream()) |
| 100 | } |
| 101 | } |