Skip to content

Commit

Permalink
Customized Settings as per MC with test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
surinder-tsys committed Oct 19, 2023
1 parent def5dba commit fb2bf74
Show file tree
Hide file tree
Showing 17 changed files with 1,099 additions and 215 deletions.
212 changes: 212 additions & 0 deletions app/src/androidTest/java/com/nmc/android/ui/SettingsPreferenceIT.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package com.nmc.android.ui

import android.preference.ListPreference
import android.preference.Preference
import androidx.test.espresso.Espresso.onData
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.PreferenceMatchers
import androidx.test.espresso.matcher.PreferenceMatchers.withKey
import androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed
import androidx.test.ext.junit.rules.ActivityScenarioRule
import com.owncloud.android.AbstractIT
import com.owncloud.android.R
import com.owncloud.android.ui.AppVersionPreference
import com.owncloud.android.ui.PreferenceCustomCategory
import com.owncloud.android.ui.ThemeableSwitchPreference
import com.owncloud.android.ui.activity.SettingsActivity
import org.hamcrest.Matchers.allOf
import org.hamcrest.Matchers.instanceOf
import org.hamcrest.Matchers.`is`
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test

class SettingsPreferenceIT : AbstractIT() {

@get:Rule
val activityRule = ActivityScenarioRule(SettingsActivity::class.java)

@Test
fun verifyPreferenceSectionCustomClass() {
activityRule.scenario.onActivity {
val preferenceAccountInfo = it.findPreference("account_info")
val preferenceGeneral = it.findPreference("general")
val preferenceDetails = it.findPreference("details")
val preferenceMore = it.findPreference("more")
val preferenceDataProtection = it.findPreference("data_protection")
val preferenceInfo = it.findPreference("info")

val preferenceCategoryList = listOf(
preferenceAccountInfo,
preferenceGeneral,
preferenceDetails,
preferenceMore,
preferenceDataProtection,
preferenceInfo
)

for (preference in preferenceCategoryList) {
assertEquals(PreferenceCustomCategory::class.java, preference.javaClass)
}
}
}

@Test
fun verifySwitchPreferenceCustomClass() {
activityRule.scenario.onActivity {
val preferenceShowHiddenFiles = it.findPreference("show_hidden_files")
assertEquals(ThemeableSwitchPreference::class.java, preferenceShowHiddenFiles.javaClass)
}
}

@Test
fun verifyAppVersionPreferenceCustomClass() {
activityRule.scenario.onActivity {
val preferenceAboutApp = it.findPreference("about_app")
assertEquals(AppVersionPreference::class.java, preferenceAboutApp.javaClass)
}
}

@Test
fun verifyPreferenceChildCustomLayout() {
activityRule.scenario.onActivity {
val userName = it.findPreference("user_name")
val storagePath = it.findPreference("storage_path")
val lock = it.findPreference("lock")
val showHiddenFiles = it.findPreference("show_hidden_files")
val syncedFolders = it.findPreference("syncedFolders")
val backup = it.findPreference("backup")
val mnemonic = it.findPreference("mnemonic")
val privacySettings = it.findPreference("privacy_settings")
val privacyPolicy = it.findPreference("privacy_policy")
val sourceCode = it.findPreference("sourcecode")
val help = it.findPreference("help")
val imprint = it.findPreference("imprint")

val preferenceList = listOf(
userName,
storagePath,
lock,
showHiddenFiles,
syncedFolders,
backup,
mnemonic,
privacySettings,
privacyPolicy,
sourceCode,
help,
imprint
)

for (preference in preferenceList) {
assertEquals(R.layout.custom_preference_layout, preference.layoutResource)
}

val aboutApp = it.findPreference("about_app")
assertEquals(R.layout.custom_app_preference_layout, aboutApp.layoutResource)

}
}

@Test
fun verifyPreferencesTitleText() {
onData(allOf(`is`(instanceOf(PreferenceCustomCategory::class.java)), withKey("account_info"),
PreferenceMatchers.withTitleText("Account Information")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(Preference::class.java)), withKey("user_name"),
PreferenceMatchers.withTitleText("test")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(PreferenceCustomCategory::class.java)), withKey("general"),
PreferenceMatchers.withTitleText("General")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(ListPreference::class.java)), withKey("storage_path"),
PreferenceMatchers.withTitleText("Data storage folder")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(PreferenceCustomCategory::class.java)), withKey("details"),
PreferenceMatchers.withTitleText("Details")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(ListPreference::class.java)), withKey("lock"),
PreferenceMatchers.withTitleText("App passcode")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(ThemeableSwitchPreference::class.java)), withKey("show_hidden_files"),
PreferenceMatchers.withTitleText("Show hidden files")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(PreferenceCustomCategory::class.java)), withKey("more"),
PreferenceMatchers.withTitleText("More")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(Preference::class.java)), withKey("syncedFolders"),
PreferenceMatchers.withTitleText("Auto upload")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(Preference::class.java)), withKey("backup"),
PreferenceMatchers.withTitleText("Back up contacts")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(Preference::class.java)), withKey("mnemonic"),
PreferenceMatchers.withTitleText("E2E mnemonic")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(Preference::class.java)), withKey("logger"),
PreferenceMatchers.withTitleText("Logs")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(PreferenceCustomCategory::class.java)), withKey("data_protection"),
PreferenceMatchers.withTitleText("Data Privacy")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(Preference::class.java)), withKey("privacy_settings"),
PreferenceMatchers.withTitleText("Privacy Settings")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(Preference::class.java)), withKey("privacy_policy"),
PreferenceMatchers.withTitleText("Privacy Policy")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(Preference::class.java)), withKey("sourcecode"),
PreferenceMatchers.withTitleText("Used OpenSource Software")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(PreferenceCustomCategory::class.java)), withKey("service"),
PreferenceMatchers.withTitleText("Service")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(Preference::class.java)), withKey("help"),
PreferenceMatchers.withTitleText("Help")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(Preference::class.java)), withKey("imprint"),
PreferenceMatchers.withTitleText("Imprint")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(PreferenceCustomCategory::class.java)), withKey("info"),
PreferenceMatchers.withTitleText("Info")))
.check(matches(isCompletelyDisplayed()))
}

@Test
fun verifyPreferencesSummaryText() {
onData(allOf(`is`(instanceOf(Preference::class.java)), withKey("lock"),
PreferenceMatchers.withSummaryText("None")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(Preference::class.java)), withKey("syncedFolders"),
PreferenceMatchers.withSummaryText("Manage folders for auto upload")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(Preference::class.java)), withKey("backup"),
PreferenceMatchers.withSummaryText("Daily backup of your calendar & contacts")))
.check(matches(isCompletelyDisplayed()))

onData(allOf(`is`(instanceOf(Preference::class.java)), withKey("mnemonic"),
PreferenceMatchers.withSummaryText("To show mnemonic please enable device credentials.")))
.check(matches(isCompletelyDisplayed()))
}
}
13 changes: 13 additions & 0 deletions app/src/main/java/com/nmc/android/ui/PrivacySettingsInterface.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.nmc.android.ui

import android.content.Context

/**
* interface to open privacy settings activity from nmc/1921-settings branch
* for implementation look nmc/1878-privacy branch
* this class will have the declaration for it since it has the PrivacySettingsActivity.java in place
* since we don't have privacy settings functionality in this branch so to handle the redirection we have used interface
*/
interface PrivacySettingsInterface {
fun openPrivacySettingsActivity(context: Context)
}
51 changes: 51 additions & 0 deletions app/src/main/java/com/owncloud/android/ui/AppVersionPreference.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.owncloud.android.ui

import android.content.Context
import android.content.pm.PackageManager
import android.preference.Preference
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.owncloud.android.R
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.utils.StringUtils

class AppVersionPreference : Preference {
constructor(context: Context?) : super(context)
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle)

override fun getView(convertView: View?, parent: ViewGroup?): View {
val v = super.getView(convertView, parent)
updatePreferenceView(v.findViewById(R.id.title), v.findViewById(R.id.summary))
return v
}

private fun updatePreferenceView(title: TextView, summary: TextView) {
val appVersion = appVersion
val titleColor: Int = context.resources.getColor(R.color.fontAppbar, null)
title.text = StringUtils.getColorSpan(
context.getString(R.string.app_name),
titleColor
)
summary.text = String.format(context.getString(R.string.about_version), appVersion)
}

private val appVersion: String
get() {
var temp: String
try {
val pkg = context.packageManager.getPackageInfo(context.packageName, 0)
temp = pkg.versionName
} catch (e: PackageManager.NameNotFoundException) {
temp = ""
Log_OC.e(TAG, "Error while showing about dialog", e)
}
return temp
}

companion object {
private val TAG = AppVersionPreference::class.java.simpleName
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.owncloud.android.ui

import android.content.Context
import android.graphics.Typeface
import android.preference.PreferenceCategory
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.widget.TextView
import com.owncloud.android.R

class PreferenceCustomCategory : PreferenceCategory {
constructor(context: Context?) : super(context)
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
constructor(
context: Context?, attrs: AttributeSet?,
defStyle: Int
) : super(context, attrs, defStyle)

override fun onBindView(view: View) {
super.onBindView(view)
val titleView = view.findViewById<TextView>(android.R.id.title)
titleView.setTextColor(context.resources.getColor(R.color.text_color))
titleView.setTextSize(
TypedValue.COMPLEX_UNIT_PX,
context.resources.getDimensionPixelSize(R.dimen.txt_size_16sp).toFloat()
)
titleView.setTypeface(null, Typeface.BOLD)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@
package com.owncloud.android.ui;

import android.content.Context;
import android.content.res.ColorStateList;
import android.preference.SwitchPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;

import com.owncloud.android.MainApp;
import com.owncloud.android.R;
import com.owncloud.android.utils.theme.ViewThemeUtils;

import javax.inject.Inject;

import androidx.core.graphics.drawable.DrawableCompat;

/**
* Themeable switch preference TODO Migrate to androidx
Expand Down Expand Up @@ -65,13 +68,60 @@ protected void onBindView(View view) {
}

private void findSwitch(ViewGroup viewGroup) {
ColorStateList thumbColorStateList;
ColorStateList trackColorStateList;

for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);

if (child instanceof Switch) {
Switch switchView = (Switch) child;

viewThemeUtils.platform.colorSwitch(switchView);
int thumbColorCheckedEnabled = getContext().getResources().getColor(R.color.switch_thumb_checked_enabled, null);
int thumbColorUncheckedEnabled = getContext().getResources().getColor(R.color.switch_thumb_unchecked_enabled, null);
int thumbColorCheckedDisabled =
getContext().getResources().getColor(R.color.switch_thumb_checked_disabled, null);
int thumbColorUncheckedDisabled =
getContext().getResources().getColor(R.color.switch_thumb_unchecked_disabled, null);

int[][] states = new int[][]{
new int[]{android.R.attr.state_enabled, android.R.attr.state_checked}, // enabled and checked
new int[]{-android.R.attr.state_enabled, android.R.attr.state_checked}, // disabled and checked
new int[]{android.R.attr.state_enabled, -android.R.attr.state_checked}, // enabled and unchecked
new int[]{-android.R.attr.state_enabled, -android.R.attr.state_checked} // disabled and unchecked
};

int[] thumbColors = new int[]{
thumbColorCheckedEnabled,
thumbColorCheckedDisabled,
thumbColorUncheckedEnabled,
thumbColorUncheckedDisabled
};

thumbColorStateList = new ColorStateList(states, thumbColors);


int trackColorCheckedEnabled =
getContext().getResources().getColor(R.color.switch_track_checked_enabled, null);
int trackColorUncheckedEnabled = getContext().getResources().getColor(R.color.switch_track_unchecked_enabled, null);
int trackColorCheckedDisabled =
getContext().getResources().getColor(R.color.switch_track_checked_disabled, null);
int trackColorUncheckedDisabled =
getContext().getResources().getColor(R.color.switch_track_unchecked_disabled, null);

int[] trackColors = new int[]{
trackColorCheckedEnabled,
trackColorCheckedDisabled,
trackColorUncheckedEnabled,
trackColorUncheckedDisabled
};
trackColorStateList = new ColorStateList(states, trackColors);

// setting the thumb color
DrawableCompat.setTintList(switchView.getThumbDrawable(), thumbColorStateList);

// setting the track color
DrawableCompat.setTintList(switchView.getTrackDrawable(), trackColorStateList);

break;
} else if (child instanceof ViewGroup) {
Expand Down
Loading

0 comments on commit fb2bf74

Please sign in to comment.