Commit 6fc39140 authored by Tiger Oakes's avatar Tiger Oakes
Browse files

Add tests

parent 60d9b340
......@@ -33,6 +33,12 @@ dependencies {
implementation Dependencies.androidx_constraintlayout
implementation Dependencies.androidx_core_ktx
implementation Dependencies.google_material
testImplementation project(":support-test")
testImplementation Dependencies.androidx_test_junit
testImplementation Dependencies.testing_robolectric
testImplementation Dependencies.testing_mockito
}
apply from: '../../../publish.gradle'
......
/* 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 mozilla.components.ui.widgets
import android.content.Context
......@@ -9,12 +13,15 @@ import android.widget.FrameLayout
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.appcompat.content.res.AppCompatResources.getDrawable
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
/**
* Shared UI widget for showing a website in a list of websites,
* such as in bookmarks or history.
* such as in bookmarks, history, site exceptions, or collections.
*/
class WidgetSiteItemView @JvmOverloads constructor(
context: Context,
......@@ -38,6 +45,7 @@ class WidgetSiteItemView @JvmOverloads constructor(
/**
* Sets the text displayed inside of the site item view.
*
* @param label Main label text, such as a site title.
* @param caption Sub caption text, such as a URL. If null, the caption is hidden.
*/
......@@ -56,10 +64,13 @@ class WidgetSiteItemView @JvmOverloads constructor(
/**
* Add a secondary button, such as an overflow menu.
*
* @param icon Drawable to display in the button.
* @param contentDescription Accessible description of the button's purpose.
* @param onClickListener Listener called when the button is clicked.
*/
fun setSecondaryButton(
icon: Drawable,
icon: Drawable?,
contentDescription: CharSequence,
onClickListener: (View) -> Unit
) {
......@@ -69,6 +80,23 @@ class WidgetSiteItemView @JvmOverloads constructor(
secondaryButton.setOnClickListener(onClickListener)
}
/**
* Add a secondary button, such as an overflow menu.
*
* @param icon Drawable to display in the button.
* @param contentDescription Accessible description of the button's purpose.
* @param onClickListener Listener called when the button is clicked.
*/
fun setSecondaryButton(
@DrawableRes icon: Int,
@StringRes contentDescription: Int,
onClickListener: (View) -> Unit
) = setSecondaryButton(
icon = getDrawable(context, icon),
contentDescription = context.getString(contentDescription),
onClickListener = onClickListener
)
/**
* Removes the secondary button if it was previously set in [setSecondaryButton].
*/
......
......@@ -15,7 +15,8 @@
android:id="@+id/favicon_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
......@@ -58,7 +59,8 @@
android:layout_width="@dimen/mozac_widget_site_item_secondary_button_size"
android:layout_height="@dimen/mozac_widget_site_item_secondary_button_size"
android:padding="@dimen/mozac_widget_site_item_secondary_button_padding"
android:layout_marginHorizontal="12dp"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:visibility="gone"
tools:visibility="visible"
......
......@@ -7,8 +7,13 @@
<attr name="mozac_accent" format="reference" />
<attr name="mozac_contrast_text" format="reference" />
<!-- Background color for favicon widget box -->
<attr name="mozac_widget_favicon_background_color" format="reference" />
<!-- Border color for favicon widget box -->
<attr name="mozac_widget_favicon_border_color" format="reference" />
<!-- Label color and icon button tint for site item widget -->
<attr name="mozac_primary_text_color" format="reference" />
<!-- Caption color for site item widget -->
<attr name="mozac_caption_text_color" format="reference" />
</resources>
......@@ -4,6 +4,12 @@
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Mozac.Widgets.TestTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="mozac_widget_favicon_background_color">@color/photonWhite</item>
<item name="mozac_widget_favicon_border_color">@color/photonLightGrey30</item>
<item name="mozac_primary_text_color">@color/photonInk90</item>
<item name="mozac_caption_text_color">@color/photonInk50</item>
</style>
<!-- Button styling -->
<style name="Mozac.Widgets.NeutralButton" parent="Widget.MaterialComponents.Button.TextButton">
......
/* 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 mozilla.components.ui.widgets
import android.graphics.drawable.Drawable
import android.widget.ImageButton
import android.widget.TextView
import androidx.appcompat.view.ContextThemeWrapper
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.test.ext.junit.runners.AndroidJUnit4
import mozilla.components.support.test.mock
import mozilla.components.support.test.robolectric.testContext
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class WidgetSiteItemViewTest {
private lateinit var view: WidgetSiteItemView
@Before
fun setup() {
val context = ContextThemeWrapper(testContext, R.style.Mozac_Widgets_TestTheme)
view = WidgetSiteItemView(context)
}
@Test
fun `setText hides the caption`() {
val labelView = view.findViewById<TextView>(R.id.label)
val captionView = view.findViewById<TextView>(R.id.caption)
view.setText(label = "label", caption = null)
assertEquals("label", labelView.text)
assertTrue(captionView.isGone)
view.setText(label = "Label", caption = "")
assertEquals("Label", labelView.text)
assertEquals("", captionView.text)
assertTrue(captionView.isVisible)
}
@Test
fun `setSecondaryButton shows the button`() {
val secondaryButton = view.findViewById<ImageButton>(R.id.secondary_button)
val drawable = mock<Drawable>()
var clicked = false
view.setSecondaryButton(
icon = drawable,
contentDescription = "Menu",
onClickListener = { clicked = true }
)
assertTrue(secondaryButton.isVisible)
assertEquals(drawable, secondaryButton.drawable)
assertEquals("Menu", secondaryButton.contentDescription)
secondaryButton.performClick()
assertTrue(clicked)
}
@Test
fun `setSecondaryButton with resource IDs shows the button`() {
val secondaryButton = view.findViewById<ImageButton>(R.id.secondary_button)
var clicked = false
view.setSecondaryButton(
icon = R.drawable.mozac_ic_lock,
contentDescription = R.string.mozac_error_lock,
onClickListener = { clicked = true }
)
assertTrue(secondaryButton.isVisible)
assertNotNull(secondaryButton.drawable)
assertEquals("mozac_error_lock", secondaryButton.contentDescription)
secondaryButton.performClick()
assertTrue(clicked)
}
@Test
fun `removeSecondaryButton does nothing if set was not called`() {
val secondaryButton = view.findViewById<ImageButton>(R.id.secondary_button)
assertTrue(secondaryButton.isGone)
view.removeSecondaryButton()
assertTrue(secondaryButton.isGone)
}
}
......@@ -39,6 +39,9 @@ permalink: /changelog/
* **browser-menu2**
* Added `style` parameter to `BrowserMenuController`.
* **ui-widgets**
* Added widget for showing a website in a list, such as in bookmarks or history. The `mozac_primary_text_color` and `mozac_caption_text_color` attributes should be set.
# 55.0.0
* [Commits](https://github.com/mozilla-mobile/android-components/compare/v54.0.0...v55.0.0)
......@@ -144,6 +147,9 @@ permalink: /changelog/
* Do not show the "Download link" option for html URLs.
* Uses a speculative check, may not work in all cases.
* **ui-widgets**
* Added shared ImageView style for favicons. The `mozac_widget_favicon_background_color` and `mozac_widget_favicon_border_color` attributes should be set, then `style="@style/Mozac.Widgets.Favicon"` can be added to an ImageView.
# 52.0.0
* [Commits](https://github.com/mozilla-mobile/android-components/compare/v51.0.0...v52.0.0)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment