Skip to content

Commit 4752dd4

Browse files
committed
Fixed tests and code style, changed 'drawableResId' attribute to 'src'
1 parent 6118da1 commit 4752dd4

File tree

6 files changed

+33
-26
lines changed

6 files changed

+33
-26
lines changed

iconpacks/iconpack-default/src/main/res/xml/iconpack_default_icons.xml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
<!--@formatter:off-->
1818
<icons width="24" height="24">
19+
1920
<category id="0" name="@string/icd_category_people">
2021
<icon id="0" tags="account,user,person" path="M12 4a4 4 0 1 1 0 8 4 4 0 0 1 0-8zm0 10c4.418 0 8 1.79 8 4v2H4v-2c0-2.21 3.582-4 8-4z"/>
2122
<icon id="1" tags="account,user,person,card,license,passport,details" path="M2 3h20c1 0 2 0.95 2 2v14c0 1-1 2-2 2H2c-1.05 0-2-1-2-2V5c0-1.05 0.953 -2 2-2zm12 3v1h8V6h-8zm0 2v1h8V8h-8zm0 2v1h7v-1h-7zm-6 3.9c-2 0-6 1.1-6 3.1v1h12v-1c0-2-4-3.1-6-3.1zM8 6C6.34 6 5 7.34 5 9c0 1.7 1.34 3 3 3s3-1.3 3-3c0-1.66-1.34-3-3-3z"/>

lib/src/androidTest/kotlin/com/maltaisn/icondialog/pack/IconPackLoaderTest.kt

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ import com.maltaisn.icondialog.data.Category
2121
import com.maltaisn.icondialog.data.Icon
2222
import com.maltaisn.icondialog.data.NamedTag
2323
import com.maltaisn.icondialog.test.R
24-
import com.nhaarman.mockitokotlin2.any
2524
import com.nhaarman.mockitokotlin2.mock
26-
import com.nhaarman.mockitokotlin2.whenever
2725
import org.junit.Test
2826
import org.junit.runner.RunWith
2927
import org.mockito.junit.MockitoJUnitRunner
@@ -37,7 +35,6 @@ internal class IconPackLoaderTest {
3735

3836
private val packLoader = IconPackLoader(context).apply {
3937
drawableLoader = mock()
40-
whenever(drawableLoader.loadDrawable(any())).thenReturn(mock())
4138
}
4239

4340
/*
@@ -145,6 +142,11 @@ internal class IconPackLoaderTest {
145142
packLoader.load(R.xml.icons_wrong_icon_size, R.xml.tags_empty)
146143
}
147144

145+
@Test(expected = IconPackParseException::class)
146+
fun loadIcons_wrongIconData_shouldFail() {
147+
packLoader.load(R.xml.icons_wrong_icon_data, R.xml.tags_empty)
148+
}
149+
148150
@Test
149151
fun loadIcons_valid() {
150152
val pack = packLoader.load(R.xml.icons_valid, R.xml.tags_empty)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!--@formatter:off-->
2+
<icons>
3+
<icon id="0" path=" " src="image" />
4+
</icons>

lib/src/main/kotlin/com/maltaisn/icondialog/data/Icon.kt

+3-4
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,14 @@ data class Icon(val id: Int,
3131
val pathData: String,
3232
val width: Int,
3333
val height: Int,
34-
val drawableResId: Int? = null
35-
) {
34+
val srcId: Int? = null) {
3635

3736
/**
3837
* The icon drawable.
3938
* Can be `null` if drawable isn't loaded or couldn't be loaded.
4039
* Use [IconDrawableLoader] class to load it.
41-
*
42-
* Note that each get call creates a new drawable that shares its constant state with all
40+
*
41+
* Note that each get call creates a new drawable that shares its constant state with all
4342
* drawables created for this icon. You should also call `drawable.mutate()` before tinting it.
4443
*/
4544
var drawable: Drawable? = null

lib/src/main/kotlin/com/maltaisn/icondialog/pack/IconDrawableLoader.kt

+4-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import java.nio.ByteOrder
3434
* Class use to load icon drawables.
3535
* @param context Any context, needed to get resources.
3636
*/
37-
class IconDrawableLoader(context: Context) {
37+
open class IconDrawableLoader(context: Context) {
3838

3939
private val context = context.applicationContext
4040

@@ -49,8 +49,9 @@ class IconDrawableLoader(context: Context) {
4949
}
5050

5151
val drawable: Drawable?
52-
if (icon.drawableResId != null) {
53-
drawable = ResourcesCompat.getDrawable(context.resources, icon.drawableResId, null)
52+
if (icon.srcId != null) {
53+
drawable = ResourcesCompat.getDrawable(context.resources, icon.srcId, null)
54+
5455
} else {
5556
val binXml = createDrawableBinaryXml(icon.pathData, icon.width, icon.height)
5657
try {

lib/src/main/kotlin/com/maltaisn/icondialog/pack/IconPackLoader.kt

+16-16
Original file line numberDiff line numberDiff line change
@@ -208,30 +208,30 @@ class IconPackLoader(context: Context) {
208208

209209
val pathStr = parser.getAttributeValue(null, XML_ATTR_ICON_PATH)
210210
val pathData = pathStr ?: overriden?.pathData ?: ""
211-
val drawableResId = parser.getAttributeValue(null, XML_ATTR_ICON_DRAWABLE_RES_ID)
212-
?.let { parseIconDrawableId(it) } ?: overriden?.drawableResId
211+
val srcStr = parser.getAttributeValue(null, XML_ATTR_ICON_SRC)
212+
val srcId = srcStr?.let { parseDrawableId(it) } ?: overriden?.srcId
213213

214-
if (pathData.isBlank() && drawableResId == null)
215-
parseError("Icon ID $id has no path data and no valid drawableResId")
214+
if (pathData.isBlank() && srcId == null) {
215+
parseError("Icon ID $id has no path data and no drawable resource specified.")
216+
}
216217

217218
val width = parser.getPositiveInt(XML_ATTR_ICON_WIDTH) { "Invalid icon width '$it'." } ?: packWidth
218219
val height = parser.getPositiveInt(XML_ATTR_ICON_HEIGHT) { "Invalid icon height '$it'." } ?: packHeight
219220

220-
return Icon(id, catgId, tags, pathData, width, height, drawableResId)
221+
return Icon(id, catgId, tags, pathData, width, height, srcId)
221222
}
222223

223-
private fun parseIconDrawableId(drawableIdString: String): Int? {
224-
return if (drawableIdString.startsWith('@')) {
225-
if (drawableIdString.startsWith("@drawable/")) {
226-
context.resources.getIdentifier(
227-
drawableIdString.substring(10), "drawable", context.packageName)
224+
private fun parseDrawableId(str: String) =
225+
if (str.startsWith('@')) {
226+
if (str.startsWith("@drawable/")) {
227+
context.resources.getIdentifier(
228+
str.substring(10), "drawable", context.packageName)
229+
} else {
230+
str.substring(1).toIntOrNull()
231+
}
228232
} else {
229-
drawableIdString.substring(1).toIntOrNull()
233+
null
230234
}
231-
} else {
232-
null
233-
}
234-
}
235235

236236
/**
237237
* Load tags of a [pack].
@@ -357,7 +357,7 @@ class IconPackLoader(context: Context) {
357357
private const val XML_ATTR_ICON_PATH = "path"
358358
private const val XML_ATTR_ICON_WIDTH = "width"
359359
private const val XML_ATTR_ICON_HEIGHT = "height"
360-
private const val XML_ATTR_ICON_DRAWABLE_RES_ID = "drawableResId"
360+
private const val XML_ATTR_ICON_SRC = "src"
361361

362362
private const val XML_TAG_TAG = "tag"
363363
private const val XML_TAG_ALIAS = "alias"

0 commit comments

Comments
 (0)