Commit 55263e3e authored by mcarare's avatar mcarare
Browse files

For #4560 Added BrowserMenuCategory menu item

parent 5eaec38c
......@@ -2,4 +2,7 @@
- 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/. -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mozilla.components.browser.menu" />
package="mozilla.components.browser.menu">
<application android:supportsRtl="true" />
</manifest>
\ No newline at end of file
/* 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.browser.menu.item
import android.graphics.Typeface
import android.view.View
import android.widget.TextView
import androidx.annotation.ColorRes
import mozilla.components.browser.menu.BrowserMenu
import mozilla.components.browser.menu.BrowserMenuItem
import mozilla.components.browser.menu.R
/**
* A browser menu item displaying styleable text, usable for menu categories
*
* @param label The visible label of this menu item.
* @param textSize: The size of the label.
* @param textColorResource: The color resource to apply to the text.
* @param textStyle: The style to apply to the text.
* @param textAlignment The alignment of text
*/
class BrowserMenuCategory(
internal val label: String,
private val textSize: Float = NO_ID.toFloat(),
@ColorRes
private val textColorResource: Int = NO_ID,
private val textStyle: Int = Typeface.BOLD,
private val textAlignment: Int = View.TEXT_ALIGNMENT_VIEW_START
) : BrowserMenuItem {
override var visible: () -> Boolean = { true }
override fun getLayoutResource() = R.layout.mozac_browser_menu_category
override fun bind(menu: BrowserMenu, view: View) {
val textView = view as TextView
textView.text = label
if (textSize != NO_ID.toFloat()) {
textView.textSize = textSize
}
if (textColorResource != NO_ID) {
textView.setColorResource(textColorResource)
}
textView.setTypeface(textView.typeface, textStyle)
textView.textAlignment = textAlignment
}
}
<?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/. -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/category_text"
style="@style/Mozac.Browser.Menu.Item.Category"
android:layout_width="match_parent"
android:gravity="center_vertical"/>
......@@ -19,6 +19,13 @@
<dimen name="mozac_browser_menu_item_divider_height">1dp</dimen>
<!--BrowserMenuDivider -->
<!-- BrowserMenuCategory -->
<dimen name="mozac_browser_menu_category_text_size">14sp</dimen>
<dimen name="mozac_browser_menu_category_layout_height">40dp</dimen>
<dimen name="mozac_browser_menu_category_padding_start">24dp</dimen>
<dimen name="mozac_browser_menu_category_padding_end">24dp</dimen>
<!-- BrowserMenuCategory -->
<!--BrowserMenuImageText-->
<!--Icon-->
......
......@@ -35,6 +35,16 @@
<item name="android:clickable">true</item>
</style>
<!-- BrowserMenuCategory -->
<style name="Mozac.Browser.Menu.Item.Category" parent="">
<item name="android:layout_height">@dimen/mozac_browser_menu_category_layout_height</item>
<item name="android:textSize">@dimen/mozac_browser_menu_category_text_size</item>
<item name="android:paddingStart">@dimen/mozac_browser_menu_category_padding_start</item>
<item name="android:paddingEnd">@dimen/mozac_browser_menu_category_padding_end</item>
<item name="android:background">?android:attr/selectableItemBackground</item>
</style>
<!-- BrowserMenuCategory -->
<!-- BrowserMenuImageText -->
<style name="Mozac.Browser.Menu.Item.ImageText.Icon" parent="">
<item name="android:layout_width">@dimen/mozac_browser_menu_item_image_text_icon_width</item>
......
package mozilla.components.browser.menu.item
import android.content.Context
import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import mozilla.components.browser.menu.BrowserMenu
import org.junit.Assert.assertEquals
import mozilla.components.browser.menu.R
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito
@RunWith(AndroidJUnit4::class)
class BrowserMenuCategoryTest {
private lateinit var menuCategory: BrowserMenuCategory
private val context: Context get() = ApplicationProvider.getApplicationContext()
private val label = "test"
@Before
fun setup() {
menuCategory = BrowserMenuCategory(label)
}
@Test
fun `menu category uses correct layout`() {
assertEquals(R.layout.mozac_browser_menu_category, menuCategory.getLayoutResource())
}
@Test
fun `menu category has correct label`() {
assertEquals(label, menuCategory.label)
}
@Test
fun `menu category should handle initialization with text size`() {
val menuCategoryWithTextSize = BrowserMenuCategory(label, 12f)
val view = inflate(menuCategoryWithTextSize)
val textView = view.findViewById<TextView>(R.id.category_text)
assertEquals(12f, textView.textSize)
}
@Test
fun `menu category should handle initialization with text colour resource`() {
val menuCategoryWithTextColour = BrowserMenuCategory(label, textColorResource = android.R.color.holo_red_dark)
val view = inflate(menuCategoryWithTextColour)
val textView = view.findViewById<TextView>(R.id.category_text)
val expectedColour = ContextCompat.getColor(textView.context, android.R.color.holo_red_dark)
assertEquals(expectedColour, textView.currentTextColor)
}
@Test
fun `menu category should handle initialization with text style`() {
val menuCategoryWithTextStyle = BrowserMenuCategory(label, textStyle = Typeface.ITALIC)
val view = inflate(menuCategoryWithTextStyle)
val textView = view.findViewById<TextView>(R.id.category_text)
assertEquals(Typeface.ITALIC, textView.typeface.style)
}
@Test
fun `menu category should handle initialization with text alignment`() {
val menuCategoryWithTextAlignment = BrowserMenuCategory(label, textAlignment = View.TEXT_ALIGNMENT_VIEW_END)
val view = inflate(menuCategoryWithTextAlignment)
val textView = view.findViewById<TextView>(R.id.category_text)
assertEquals(View.TEXT_ALIGNMENT_VIEW_END, textView.textAlignment)
}
private fun inflate(browserMenuCategory: BrowserMenuCategory): View {
val view = LayoutInflater.from(context).inflate(browserMenuCategory.getLayoutResource(), null)
val mockMenu = Mockito.mock(BrowserMenu::class.java)
browserMenuCategory.bind(mockMenu, view)
return view
}
}
......@@ -12,6 +12,9 @@ permalink: /changelog/
* [Gecko](https://github.com/mozilla-mobile/android-components/blob/master/buildSrc/src/main/java/Gecko.kt)
* [Configuration](https://github.com/mozilla-mobile/android-components/blob/master/buildSrc/src/main/java/Config.kt)
* **browser-menu**
* Adds the ability to create a BrowserMenuCategory, a menu item that defines a category for other menu items
# 17.0.0
* [Commits](https://github.com/mozilla-mobile/android-components/compare/v16.0.0...v17.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