Add AppConfigManagerTests

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-04-18 12:49:19 +02:00 committed by Alper Öztürk
parent b727fc364c
commit d429a76590
4 changed files with 104 additions and 16 deletions

View file

@ -0,0 +1,85 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2024 Your Name <your@email.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package com.nextcloud.utils
import android.os.Bundle
import com.owncloud.android.AbstractIT
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory
import com.owncloud.android.utils.appConfig.AppConfigKeys
import com.owncloud.android.utils.appConfig.AppConfigManager
import org.junit.Test
class AppConfigManagerTests : AbstractIT() {
@Test
fun testSetProxyConfigWhenGivenClientBrandedAndCorrectBundleDataProxyConfigurationShouldSet() {
val proxySetting = Bundle().apply {
putString(AppConfigKeys.ProxyHost.key, "nextcloud.cloud.cloud.com")
putInt(AppConfigKeys.ProxyPort.key, 441212)
}
AppConfigManager(targetContext, proxySetting).run {
setProxyConfig(true)
}
val proxyHost = OwnCloudClientManagerFactory.getProxyHost()
val proxyPort = OwnCloudClientManagerFactory.getProxyPort()
assert(proxyHost.equals("nextcloud.cloud.cloud.com"))
assert(proxyPort == 441212)
}
@Test
fun testSetProxyConfigWhenGivenClientNotBrandedAndCorrectBundleDataProxyConfigurationShouldNotSet() {
val proxySetting = Bundle().apply {
putString(AppConfigKeys.ProxyHost.key, "nextcloud.cloud.cloud.com")
putInt(AppConfigKeys.ProxyPort.key, 441212)
}
AppConfigManager(targetContext, proxySetting).run {
setProxyConfig(false)
}
val proxyHost = OwnCloudClientManagerFactory.getProxyHost()
val proxyPort = OwnCloudClientManagerFactory.getProxyPort()
assert(proxyHost.equals(""))
assert(proxyPort == -1)
}
@Test
fun testSetProxyConfigWhenGivenClientBrandedAndBrokenBundleDataProxyConfigurationShouldSetDefaultValues() {
val proxySetting = Bundle()
AppConfigManager(targetContext, proxySetting).run {
setProxyConfig(true)
}
val proxyHost = OwnCloudClientManagerFactory.getProxyHost()
val proxyPort = OwnCloudClientManagerFactory.getProxyPort()
assert(proxyHost.equals(""))
assert(proxyPort == -1)
}
@Test
fun testGetBaseUrlConfigWhenGivenClientBrandedAndCorrectBundleDataBaseUrlConfigurationShouldSet() {
val baseUrlConfig = Bundle().apply {
putString(AppConfigKeys.BaseUrl.key, "nextcloud.cloud.cloud")
}
val sut = AppConfigManager(targetContext, baseUrlConfig)
assert(!sut.getBaseUrl().isNullOrEmpty())
}
@Test
fun testGetBaseUrlConfigWhenGivenClientBrandedAndBrokenBundleDataBaseUrlConfigurationShouldNotSet() {
val baseUrlConfig = Bundle()
val sut = AppConfigManager(targetContext, baseUrlConfig)
assert(sut.getBaseUrl().isNullOrEmpty())
}
}

View file

@ -26,9 +26,11 @@ import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.RestrictionsManager;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
@ -298,7 +300,8 @@ public class MainApp extends MultiDexApplication implements HasAndroidInjector {
setAppTheme(preferences.getDarkThemeMode());
super.onCreate();
appConfigManager = new AppConfigManager(this);
RestrictionsManager restrictionsManager = (RestrictionsManager) getSystemService(Context.RESTRICTIONS_SERVICE);
appConfigManager = new AppConfigManager(this, restrictionsManager.getApplicationRestrictions());
// Listen app config changes
ContextExtensionsKt.registerBroadcastReceiver(this, restrictionsReceiver, restrictionsFilter, ReceiverFlag.NotExported);
@ -326,7 +329,7 @@ public class MainApp extends MultiDexApplication implements HasAndroidInjector {
OwnCloudClientManagerFactory.setUserAgent(getUserAgent());
appConfigManager.setProxyConfig();
appConfigManager.setProxyConfig(isClientBranded());
// initialise thumbnails cache on background thread
new ThumbnailsCacheManager.InitDiskCacheTask().execute();
@ -373,16 +376,21 @@ public class MainApp extends MultiDexApplication implements HasAndroidInjector {
passCodeManager.setCanAskPin(true);
Log_OC.d(TAG, "APP IN BACKGROUND");
} else if (event == Lifecycle.Event.ON_RESUME) {
appConfigManager.setProxyConfig();
appConfigManager.setProxyConfig(isClientBranded());
Log_OC.d(TAG, "APP ON RESUME");
}
});
public static boolean isClientBranded() {
Resources resources = getAppContext().getResources();
return (resources.getBoolean(R.bool.is_branded_client) || resources.getBoolean(R.bool.is_branded_plus_client));
}
private final IntentFilter restrictionsFilter = new IntentFilter(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED);
private final BroadcastReceiver restrictionsReceiver = new BroadcastReceiver() {
@Override public void onReceive(Context context, Intent intent) {
appConfigManager.setProxyConfig();
appConfigManager.setProxyConfig(isClientBranded());
}
};

View file

@ -21,6 +21,7 @@ import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.RestrictionsManager;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
@ -322,7 +323,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
String webloginUrl = null;
boolean webViewLoginMethod;
AppConfigManager appConfigManager = new AppConfigManager(this);
RestrictionsManager restrictionsManager = (RestrictionsManager) getSystemService(Context.RESTRICTIONS_SERVICE);
AppConfigManager appConfigManager = new AppConfigManager(this, restrictionsManager.getApplicationRestrictions());
if (getResources().getBoolean(R.bool.is_branded_plus_client)) {
webViewLoginMethod = true;

View file

@ -8,25 +8,18 @@
package com.owncloud.android.utils.appConfig
import android.content.Context
import android.content.RestrictionsManager
import android.content.res.Resources
import android.os.Bundle
import com.owncloud.android.R
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory
import com.owncloud.android.lib.common.utils.Log_OC
class AppConfigManager(private val context: Context) {
private val restrictionsManager =
context.getSystemService(Context.RESTRICTIONS_SERVICE) as RestrictionsManager
private val appRestrictions = restrictionsManager.applicationRestrictions
class AppConfigManager(private val context: Context, private val appRestrictions: Bundle) {
private val tag = "AppConfigManager"
fun setProxyConfig() {
if (!context.resources.getBoolean(R.bool.is_branded_client) ||
!context.resources.getBoolean(R.bool.is_branded_plus_client)
) {
fun setProxyConfig(isBranded: Boolean) {
if (!isBranded) {
Log_OC.d(tag, "Proxy configuration cannot be set. Client is not branded.")
return
}