Skip to content

Commit 9e5cc25

Browse files
committed
Add test support for loading files from the resources directory
1 parent 43be615 commit 9e5cc25

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

components/concept/engine/src/test/java/mozilla/components/concept/engine/manifest/WebAppManifestParserTest.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package mozilla.components.concept.engine.manifest
77
import android.graphics.Color
88
import android.graphics.Color.rgb
99
import androidx.test.ext.junit.runners.AndroidJUnit4
10+
import mozilla.components.support.test.file.loadResourceAsString
1011
import org.json.JSONObject
1112
import org.junit.Assert.assertEquals
1213
import org.junit.Assert.assertFalse
@@ -580,12 +581,7 @@ class WebAppManifestParserTest {
580581
}
581582

582583
private fun loadManifestAsString(fileName: String): String =
583-
javaClass.getResourceAsStream("/manifests/$fileName")!!
584-
.bufferedReader().use {
585-
it.readText()
586-
}.also {
587-
assertNotNull(it)
588-
}
584+
loadResourceAsString("/manifests/$fileName")
589585

590586
private fun loadManifest(fileName: String): JSONObject =
591587
JSONObject(loadManifestAsString(fileName))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
package mozilla.components.support.test.file
6+
7+
import org.junit.Assert
8+
9+
/**
10+
* Loads a file from the resources folder and return its content as a string object.
11+
* @param path The path where the file is located
12+
*/
13+
fun Any.loadResourceAsString(path: String): String {
14+
return javaClass.getResourceAsStream(path)!!.bufferedReader().use {
15+
it.readText()
16+
}.also {
17+
Assert.assertNotNull(it)
18+
}
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
package mozilla.components.support.test.robolectric.mozilla.components.support.test.file
6+
7+
import androidx.test.ext.junit.runners.AndroidJUnit4
8+
import mozilla.components.support.test.file.loadResourceAsString
9+
import org.junit.Assert.assertEquals
10+
import org.junit.Test
11+
import org.junit.runner.RunWith
12+
13+
@RunWith(AndroidJUnit4::class)
14+
class ResourcesTest {
15+
16+
@Test
17+
fun getProvidedAppContext() {
18+
assertEquals("42", loadResourceAsString("/example_file.txt"))
19+
}
20+
}

0 commit comments

Comments
 (0)