Skip to content

Commit

Permalink
For mozilla-mobile#4118 - Creates setting for auto closing tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
ekager committed Aug 17, 2020
1 parent 54c2401 commit 951fada
Show file tree
Hide file tree
Showing 14 changed files with 302 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class SettingsBasicsTest {
verifyBasicsHeading()
verifySearchEngineButton()
verifyDefaultBrowserItem()
verifyCloseTabsItem()
// drill down to submenu
}.openSearchSubMenu {
verifyDefaultSearchEngineHeader()
Expand Down Expand Up @@ -168,6 +169,17 @@ class SettingsBasicsTest {
}
}

@Test
fun changeCloseTabsSetting() {
// Goes through the settings and verified the close tabs setting options.
homeScreen {
}.openThreeDotMenu {
}.openSettings {
}.openCloseTabsSubMenu {
verifyOptions()
}
}

@Test
fun changeAccessibiltySettings() {
// Goes through the settings and changes the default text on a webpage, then verifies if the text has changed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class SettingsRobot {
fun verifyAccessibilityButton() = assertAccessibilityButton()
fun verifySetAsDefaultBrowserButton() = assertSetAsDefaultBrowserButton()
fun verifyDefaultBrowserItem() = assertDefaultBrowserItem()
fun verifyCloseTabsItem() = assertCloseTabsItem()
fun verifyDefaultBrowserIsDisaled() = assertDefaultBrowserIsDisabled()
fun clickDefaultBrowserSwitch() = toggleDefaultBrowserSwitch()
fun verifyAndroidDefaultAppsMenuAppears() = assertAndroidDefaultAppsMenuAppears()
Expand Down Expand Up @@ -134,6 +135,15 @@ class SettingsRobot {
return SettingsSubMenuThemeRobot.Transition()
}

fun openCloseTabsSubMenu(interact: SettingsSubMenuTabsRobot.() -> Unit): SettingsSubMenuTabsRobot.Transition {

fun closeTabsButton() = onView(withText("Close tabs"))
closeTabsButton().click()

SettingsSubMenuTabsRobot().interact()
return SettingsSubMenuTabsRobot.Transition()
}

fun openAccessibilitySubMenu(interact: SettingsSubMenuAccessibilityRobot.() -> Unit): SettingsSubMenuAccessibilityRobot.Transition {

fun accessibilityButton() = onView(withText("Accessibility"))
Expand Down Expand Up @@ -284,6 +294,12 @@ private fun assertDefaultBrowserItem() {
.check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
}

private fun assertCloseTabsItem() {
mDevice.wait(Until.findObject(By.text("Close tabs")), waitingTime)
onView(withText("Close tabs"))
.check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
}

// PRIVACY SECTION
private fun assertPrivacyHeading() {
onView(withText("Privacy and security"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* 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/. */

@file:Suppress("TooManyFunctions")

package org.mozilla.fenix.ui.robots

import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import org.hamcrest.CoreMatchers.allOf

/**
* Implementation of Robot Pattern for the settings Tabs sub menu.
*/
class SettingsSubMenuTabsRobot {

fun verifyOptions() = assertOptions()

class Transition {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())

fun goBack(interact: SettingsRobot.() -> Unit): SettingsRobot.Transition {
mDevice.waitForIdle()
goBackButton().perform(ViewActions.click())

SettingsRobot().interact()
return SettingsRobot.Transition()
}
}
}

private fun assertOptions() {
afterOneDayToggle()
.check(ViewAssertions.matches(ViewMatchers.withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
manualToggle()
.check(ViewAssertions.matches(ViewMatchers.withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
afterOneWeekToggle()
.check(ViewAssertions.matches(ViewMatchers.withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
afterOneMonthToggle()
.check(ViewAssertions.matches(ViewMatchers.withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
}

private fun manualToggle() = onView(withText("Manually"))

private fun afterOneDayToggle() = onView(withText("After one day"))

private fun afterOneWeekToggle() = onView(withText("After one week"))

private fun afterOneMonthToggle() = onView(withText("After one month"))

private fun goBackButton() =
onView(allOf(ViewMatchers.withContentDescription("Navigate up")))
11 changes: 11 additions & 0 deletions app/src/main/java/org/mozilla/fenix/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {

sessionObserver = UriOpenedObserver(this)

// Launch this on a background thread so as not to affect startup performance
lifecycleScope.launch(IO) {
if (!settings().manuallyCloseTabs) {
components.core.store.state.tabs.filter {
(System.currentTimeMillis() - it.lastAccess) > settings().getTabTimeout()
}.forEach {
components.useCases.tabsUseCases.removeTab(it.id)
}
}
}

checkPrivateShortcutEntryPoint(intent)
privateNotificationObserver = PrivateNotificationFeature(
applicationContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* 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.settings

import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.showToolbar
import org.mozilla.fenix.utils.view.addToRadioGroup

/**
* Lets the user customize auto closing tabs.
*/
class CloseTabsSettingsFragment : PreferenceFragmentCompat() {
private lateinit var radioManual: RadioButtonPreference
private lateinit var radioOneDay: RadioButtonPreference
private lateinit var radioOneWeek: RadioButtonPreference
private lateinit var radioOneMonth: RadioButtonPreference

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.close_tabs_preferences, rootKey)
}

override fun onResume() {
super.onResume()
showToolbar(getString(R.string.preferences_close_tabs))
setupPreferences()
}

private fun setupPreferences() {
radioManual = requirePreference(R.string.pref_key_close_tabs_manually)
radioOneDay = requirePreference(R.string.pref_key_close_tabs_after_one_day)
radioOneWeek = requirePreference(R.string.pref_key_close_tabs_after_one_week)
radioOneMonth = requirePreference(R.string.pref_key_close_tabs_after_one_month)
setupRadioGroups()
}

private fun setupRadioGroups() {
addToRadioGroup(
radioManual,
radioOneDay,
radioOneMonth,
radioOneWeek
)
}
}
10 changes: 8 additions & 2 deletions app/src/main/java/org/mozilla/fenix/settings/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,16 @@ class SettingsFragment : PreferenceFragmentCompat() {
if (requireContext().hasCamera()) {
SettingsFragmentDirections.actionSettingsFragmentToTurnOnSyncFragment()
} else {
requireComponents.services.accountsAuthFeature.beginAuthentication(requireContext())
requireComponents.services.accountsAuthFeature.beginAuthentication(
requireContext()
)
requireComponents.analytics.metrics.track(Event.SyncAuthUseEmail)
null
}
}
resources.getString(R.string.pref_key_close_tabs) -> {
SettingsFragmentDirections.actionSettingsFragmentToCloseTabsSettingsFragment()
}
resources.getString(R.string.pref_key_search_settings) -> {
SettingsFragmentDirections.actionSettingsFragmentToSearchEngineFragment()
}
Expand Down Expand Up @@ -306,7 +311,8 @@ class SettingsFragment : PreferenceFragmentCompat() {
requirePreference<Preference>(R.string.pref_key_external_download_manager)
val preferenceLeakCanary = findPreference<Preference>(leakKey)
val preferenceRemoteDebugging = findPreference<Preference>(debuggingKey)
val preferenceMakeDefaultBrowser = requirePreference<Preference>(R.string.pref_key_make_default_browser)
val preferenceMakeDefaultBrowser =
requirePreference<Preference>(R.string.pref_key_make_default_browser)

if (!Config.channel.isReleased) {
preferenceLeakCanary?.setOnPreferenceChangeListener { _, newValue ->
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/java/org/mozilla/fenix/utils/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class Settings(private val appContext: Context) : PreferencesHolder {
private const val CFR_COUNT_CONDITION_FOCUS_INSTALLED = 1
private const val CFR_COUNT_CONDITION_FOCUS_NOT_INSTALLED = 3

const val ONE_DAY_MS = 60 * 60 * 24 * 1000L
const val ONE_WEEK_MS = 60 * 60 * 24 * 7 * 1000L
const val ONE_MONTH_MS = (60 * 60 * 24 * 365 * 1000L) / 12

private fun Action.toInt() = when (this) {
Action.BLOCKED -> BLOCKED_INT
Action.ASK_TO_ALLOW -> ASK_TO_ALLOW_INT
Expand Down Expand Up @@ -321,6 +325,33 @@ class Settings(private val appContext: Context) : PreferencesHolder {
default = false
)

var manuallyCloseTabs by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_close_tabs_manually),
default = true
)

var closeTabsAfterOneDay by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_close_tabs_after_one_day),
default = false
)

var closeTabsAfterOneWeek by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_close_tabs_after_one_week),
default = false
)

var closeTabsAfterOneMonth by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_close_tabs_after_one_month),
default = false
)

fun getTabTimeout(): Long = when {
closeTabsAfterOneDay -> ONE_DAY_MS
closeTabsAfterOneWeek -> ONE_WEEK_MS
closeTabsAfterOneMonth -> ONE_MONTH_MS
else -> -1
}

val shouldUseDarkTheme by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_dark_theme),
default = false
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/drawable/ic_multiple_tabs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?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/. -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M15.22 22H5.78A3.78 3.78 0 0 1 2 18.22V8.78C2 6.69 3.7 5 5.78 5h9.44C17.31 5 19 6.7 19 8.78v9.44c0 2-1.7 3.78-3.78 3.78zM5.86 7C4.83 7 4 7.83 4 8.86v9.28C4 19.17 4.83 20 5.86 20h9.28c1 0 1.86-0.83 1.86-1.86V8.86C17 8 16.17 7 15 7H6zM6 4c0-1.1 1-2 2-2h8a6 6 0 0 1 6 6v0L22 16a2 2 0 0 1-2 2L20 8V8a4 4 0 0 0-4-4H6z"
android:fillColor="?primaryText" />
</vector>
20 changes: 17 additions & 3 deletions app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,18 @@
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right" />
<action
android:id="@+id/action_settingsFragment_to_closeTabsSettingsFragment"
app:destination="@id/closeTabsSettingsFragment"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right" />
</fragment>
<fragment
android:id="@+id/closeTabsSettingsFragment"
android:name="org.mozilla.fenix.settings.CloseTabsSettingsFragment"
android:label="@string/preferences_close_tabs" />
<fragment
android:id="@+id/dataChoicesFragment"
android:name="org.mozilla.fenix.settings.DataChoicesFragment"
Expand Down Expand Up @@ -775,7 +786,8 @@
android:id="@+id/tabHistoryDialogFragment"
android:name="org.mozilla.fenix.tabhistory.TabHistoryDialogFragment" />

<navigation android:id="@+id/site_permissions_exceptions_graph"
<navigation
android:id="@+id/site_permissions_exceptions_graph"
app:startDestination="@id/sitePermissionsExceptionsFragment">

<fragment
Expand Down Expand Up @@ -814,7 +826,8 @@
</fragment>
</navigation>

<navigation android:id="@+id/addons_management_graph"
<navigation
android:id="@+id/addons_management_graph"
app:startDestination="@id/addonsManagementFragment">
<fragment
android:id="@+id/addonsManagementFragment"
Expand Down Expand Up @@ -881,7 +894,8 @@
</fragment>
</navigation>

<navigation android:id="@+id/search_engine_graph"
<navigation
android:id="@+id/search_engine_graph"
app:startDestination="@id/searchEngineFragment">
<fragment
android:id="@+id/searchEngineFragment"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values/preference_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,10 @@
<string name="pref_key_default_browser" translatable="false">pref_key_default_browser</string>

<string name="pref_key_login_exceptions" translatable="false">pref_key_login_exceptions</string>

<string name="pref_key_close_tabs" translatable="false">pref_key_close_tabs</string>
<string name="pref_key_close_tabs_manually" translatable="false">pref_key_close_tabs_manually</string>
<string name="pref_key_close_tabs_after_one_day" translatable="false">pref_key_close_tabs_after_one_day</string>
<string name="pref_key_close_tabs_after_one_week" translatable="false">pref_key_close_tabs_after_one_week</string>
<string name="pref_key_close_tabs_after_one_month" translatable="false">pref_key_close_tabs_after_one_month</string>
</resources>
11 changes: 11 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1455,4 +1455,15 @@
<!-- Confirmation dialog button text when top sites limit is reached. -->
<string name="top_sites_max_limit_confirmation_button">OK, Got It</string>

<!-- Tab Management -->
<!-- Title of preference that allows a user to auto close tabs after a specified amount of time -->
<string name="preferences_close_tabs">Close tabs</string>
<!-- Option for auto closing tabs that will never auto close tabs, always allows user to manually close tabs -->
<string name="close_tabs_manually">Manually</string>
<!-- Option for auto closing tabs that will auto close tabs after one day -->
<string name="close_tabs_after_one_day">After one day</string>
<!-- Option for auto closing tabs that will auto close tabs after one week -->
<string name="close_tabs_after_one_week">After one week</string>
<!-- Option for auto closing tabs that will auto close tabs after one month -->
<string name="close_tabs_after_one_month">After one month</string>
</resources>
25 changes: 25 additions & 0 deletions app/src/main/res/xml/close_tabs_preferences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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/. -->
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<org.mozilla.fenix.settings.RadioButtonPreference
android:defaultValue="true"
android:key="@string/pref_key_close_tabs_manually"
android:title="@string/close_tabs_manually" />

<org.mozilla.fenix.settings.RadioButtonPreference
android:defaultValue="false"
android:key="@string/pref_key_close_tabs_after_one_day"
android:title="@string/close_tabs_after_one_day" />

<org.mozilla.fenix.settings.RadioButtonPreference
android:defaultValue="false"
android:key="@string/pref_key_close_tabs_after_one_week"
android:title="@string/close_tabs_after_one_week" />

<org.mozilla.fenix.settings.RadioButtonPreference
android:defaultValue="false"
android:key="@string/pref_key_close_tabs_after_one_month"
android:title="@string/close_tabs_after_one_month" />
</androidx.preference.PreferenceScreen>
5 changes: 5 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@
android:icon="@drawable/ic_internet"
android:key="@string/pref_key_make_default_browser"
android:title="@string/preferences_set_as_default_browser" />

<androidx.preference.Preference
android:icon="@drawable/ic_multiple_tabs"
android:key="@string/pref_key_close_tabs"
android:title="@string/preferences_close_tabs" />
</androidx.preference.PreferenceCategory>

<androidx.preference.PreferenceCategory
Expand Down
Loading

0 comments on commit 951fada

Please sign in to comment.