Use StateListDrawable & resources w/ TabCollection
parent
451093fd9e
commit
6da6ddb095
@ -0,0 +1,24 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.ext
|
||||
|
||||
import android.content.Context
|
||||
import androidx.annotation.ColorInt
|
||||
import androidx.core.content.ContextCompat
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.home.sessioncontrol.TabCollection
|
||||
import kotlin.math.abs
|
||||
|
||||
/**
|
||||
* Selects one of the predefined collection icon colors based on the id.
|
||||
*/
|
||||
@ColorInt
|
||||
fun TabCollection.getIconColor(context: Context): Int {
|
||||
val iconColors = context.resources.obtainTypedArray(R.array.collection_icon_colors)
|
||||
val index = abs(id % iconColors.length()).toInt()
|
||||
val color = iconColors.getColor(index, ContextCompat.getColor(context, R.color.white_color))
|
||||
iconColors.recycle()
|
||||
return color
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- This Source Code Form is subject to the terms of the Mozilla Public
|
||||
- License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize="true">
|
||||
<item android:drawable="@drawable/rounded_top_corners" android:state_activated="true" />
|
||||
<item android:drawable="@drawable/rounded_all_corners" />
|
||||
</selector>
|
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- This Source Code Form is subject to the terms of the Mozilla Public
|
||||
- License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize="true">
|
||||
<item android:drawable="@drawable/ic_chevron_up" android:state_activated="true" />
|
||||
<item android:drawable="@drawable/ic_chevron_down" />
|
||||
</selector>
|
@ -0,0 +1,43 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.ext
|
||||
|
||||
import androidx.core.content.ContextCompat
|
||||
import junit.framework.Assert.assertEquals
|
||||
import mozilla.components.feature.tab.collections.TabCollection
|
||||
import mozilla.components.support.test.mock
|
||||
import mozilla.components.support.test.robolectric.testContext
|
||||
import org.junit.Assert.assertNotEquals
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Mockito.`when`
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.TestApplication
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(application = TestApplication::class)
|
||||
class TabCollectionTest {
|
||||
|
||||
@Test
|
||||
fun getIconColor() {
|
||||
val color = mockTabCollection(100L).getIconColor(testContext)
|
||||
// Color does not change
|
||||
for (i in 0..99) {
|
||||
assertEquals(color, mockTabCollection(100L).getIconColor(testContext))
|
||||
}
|
||||
|
||||
// Returns a color for negative IDs
|
||||
val defaultColor = ContextCompat.getColor(testContext, R.color.white_color)
|
||||
assertNotEquals(defaultColor, mockTabCollection(-123L).getIconColor(testContext))
|
||||
}
|
||||
|
||||
private fun mockTabCollection(id: Long): TabCollection {
|
||||
val collection: TabCollection = mock()
|
||||
`when`(collection.id).thenReturn(id)
|
||||
return collection
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue