diff options
author | cyBerta <cyberta@riseup.net> | 2023-08-02 22:45:26 +0200 |
---|---|---|
committer | cyBerta <cyberta@riseup.net> | 2023-08-02 22:45:26 +0200 |
commit | 64449816223cb9abb6e75310c03dcc9353a42ee4 (patch) | |
tree | 66cb86d382cc67267441a10320f7e13cd125e66b /app/src/test/java/se | |
parent | 1ac4ec903f0d10b65810b720f76f08df969c3bec (diff) |
use single instance of shared prefernces across the app, this reduces the laggyness of the UI noticably
Diffstat (limited to 'app/src/test/java/se')
5 files changed, 44 insertions, 10 deletions
diff --git a/app/src/test/java/se/leap/bitmaskclient/base/utils/PreferenceHelperTest.java b/app/src/test/java/se/leap/bitmaskclient/base/utils/PreferenceHelperTest.java index 955ddc44..e03fccff 100644 --- a/app/src/test/java/se/leap/bitmaskclient/base/utils/PreferenceHelperTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/base/utils/PreferenceHelperTest.java @@ -13,6 +13,7 @@ import static org.junit.Assert.assertTrue; import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_EIP_DEFINITION; import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_PRIVATE_KEY; import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_VPN_CERTIFICATE; +import static se.leap.bitmaskclient.base.utils.PreferenceHelper.preferUDP; import static se.leap.bitmaskclient.testutils.TestSetupHelper.getInputAsString; import static se.leap.bitmaskclient.base.utils.PreferenceHelper.getSavedProviderFromSharedPreferences; @@ -22,16 +23,18 @@ import static se.leap.bitmaskclient.base.utils.PreferenceHelper.getSavedProvider public class PreferenceHelperTest { private SharedPreferences mockPreferences; + PreferenceHelper preferenceHelper; @Before public void setup() { mockPreferences = new MockSharedPreferences(); + preferenceHelper = new PreferenceHelper(mockPreferences); } @Test public void getSavedProviderFromSharedPreferences_notInPreferences_returnsDefaultProvider() throws Exception { - Provider provider = getSavedProviderFromSharedPreferences(mockPreferences); + Provider provider = getSavedProviderFromSharedPreferences(); assertFalse(provider.isConfigured()); } @@ -45,7 +48,7 @@ public class PreferenceHelperTest { .putString(PROVIDER_VPN_CERTIFICATE, getInputAsString(getClass().getClassLoader().getResourceAsStream("riseup.net.vpn_cert.pem"))) .putString(PROVIDER_PRIVATE_KEY, getInputAsString(getClass().getClassLoader().getResourceAsStream("private_rsa_key.pem"))) .apply(); - Provider provider = getSavedProviderFromSharedPreferences(mockPreferences); + Provider provider = getSavedProviderFromSharedPreferences(); assertTrue(provider.isConfigured()); } diff --git a/app/src/test/java/se/leap/bitmaskclient/eip/EipStatusTest.java b/app/src/test/java/se/leap/bitmaskclient/eip/EipStatusTest.java index 1e15c5ab..cec627ef 100644 --- a/app/src/test/java/se/leap/bitmaskclient/eip/EipStatusTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/eip/EipStatusTest.java @@ -61,7 +61,7 @@ public class EipStatusTest { VpnProfile mockVpnProfile = new VpnProfile("mockProfile", OPENVPN); mockVpnProfile.mPersistTun = true; doNothing().when(PreferenceHelper.class); - VpnStatus.setLastConnectedVpnProfile(null, mockVpnProfile); + VpnStatus.setLastConnectedVpnProfile(mockVpnProfile); VpnStatus.updateStateString("SCREENOFF", "", R.string.state_screenoff, LEVEL_VPNPAUSED); assertTrue("LEVEL_VPN_PAUSED eipLevel", eipStatus.getEipLevel() == CONNECTING); assertTrue("LEVEL_VPN_PAUSED level", eipStatus.getLevel() == LEVEL_VPNPAUSED); @@ -74,7 +74,7 @@ public class EipStatusTest { VpnProfile mockVpnProfile = new VpnProfile("mockProfile", OPENVPN); mockVpnProfile.mPersistTun = false; doNothing().when(PreferenceHelper.class); - VpnStatus.setLastConnectedVpnProfile(null, mockVpnProfile); + VpnStatus.setLastConnectedVpnProfile(mockVpnProfile); VpnStatus.updateStateString("SCREENOFF", "", R.string.state_screenoff, LEVEL_VPNPAUSED); assertTrue("LEVEL_VPN_PAUSED eipLevel", eipStatus.getEipLevel() == DISCONNECTED); assertTrue("LEVEL_VPN_PAUSED level", eipStatus.getLevel() == LEVEL_VPNPAUSED); diff --git a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaySelectorTest.java b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaySelectorTest.java index 6f26e494..74762813 100644 --- a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaySelectorTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaySelectorTest.java @@ -24,6 +24,8 @@ import java.util.ArrayList; import de.blinkt.openvpn.core.ConfigParser; import se.leap.bitmaskclient.base.models.Provider; import se.leap.bitmaskclient.base.utils.ConfigHelper; +import se.leap.bitmaskclient.base.utils.PreferenceHelper; +import se.leap.bitmaskclient.testutils.MockSharedPreferences; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertTrue; @@ -69,6 +71,7 @@ public class GatewaySelectorTest { */ + PreferenceHelper preferenceHelper; GatewaySelector gatewaySelector; JSONObject eipDefinition; ArrayList<Gateway> gatewayList = new ArrayList<>(); @@ -78,6 +81,7 @@ public class GatewaySelectorTest { mockStatic(ConfigHelper.class); when(ConfigHelper.timezoneDistance(anyInt(), anyInt())).thenCallRealMethod(); mockTextUtils(); + preferenceHelper = new PreferenceHelper(new MockSharedPreferences()); eipDefinition = new JSONObject(getInputAsString(getClass().getClassLoader().getResourceAsStream("eip-service-four-gateways.json"))); JSONArray gateways = eipDefinition.getJSONArray("gateways"); for (int i = 0; i < gateways.length(); i++) { diff --git a/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java b/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java index 4ef8b9cc..91823433 100644 --- a/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java +++ b/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java @@ -469,7 +469,7 @@ public class MockHelper { public static void mockPreferenceHelper(final Provider providerFromPrefs) { // FIXME use MockSharedPreferences instead of provider mockStatic(PreferenceHelper.class); - when(getFromPersistedProvider(anyString(), anyString(), any(SharedPreferences.class))).thenAnswer(new Answer<String>() { + when(getFromPersistedProvider(anyString(), anyString())).thenAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation) throws Throwable { String key = (String) invocation.getArguments()[0]; @@ -495,7 +495,7 @@ public class MockHelper { return null; } }); - when(getLongFromPersistedProvider(anyString(), anyString(), any(SharedPreferences.class))).thenAnswer(new Answer<Long>() { + when(getLongFromPersistedProvider(anyString(), anyString())).thenAnswer(new Answer<Long>() { @Override public Long answer(InvocationOnMock invocation) throws Throwable { String key = (String) invocation.getArguments()[0]; @@ -508,7 +508,7 @@ public class MockHelper { return 0L; } }); - when(getStringSetFromPersistedProvider(anyString(), anyString(), any(SharedPreferences.class))).thenAnswer(new Answer<Set<String>>() { + when(getStringSetFromPersistedProvider(anyString(), anyString())).thenAnswer(new Answer<Set<String>>() { @Override public Set<String> answer(InvocationOnMock invocation) throws Throwable { String key = (String) invocation.getArguments()[0]; @@ -519,14 +519,43 @@ public class MockHelper { return null; } }); + when(PreferenceHelper.hasKey(anyString())).then(new Answer<Boolean>() { + @Override + public Boolean answer(InvocationOnMock invocation) throws Throwable { + String key = (String) invocation.getArguments()[0]; + if (key!= null && key.contains(providerFromPrefs.getDomain())) { + key = key.substring(0, key.indexOf(providerFromPrefs.getDomain()) - 1 /* -1 -> "." at the end */); + } + switch (key) { + case PROVIDER_PRIVATE_KEY: + return providerFromPrefs.getPrivateKey() != null; + case PROVIDER_VPN_CERTIFICATE: + return providerFromPrefs.getVpnCertificate() != null; + case Provider.KEY: + return providerFromPrefs.getDefinition().keys().hasNext(); + case Provider.CA_CERT_FINGERPRINT: + return providerFromPrefs.getCaCertFingerprint().length() > 1; + case Provider.CA_CERT: + return providerFromPrefs.getCaCert() != null; + case Provider.GEOIP_URL: + return !providerFromPrefs.getGeoipUrl().isDefault(); + case Provider.MOTD_URL: + return !providerFromPrefs.getMotdUrl().isDefault(); + case PROVIDER_MOTD: + return providerFromPrefs.getMotdJson().keys().hasNext(); + } + return false; + } + }); } public static void mockPreferenceHelper(MockSharedPreferences preferences) { mockStatic(PreferenceHelper.class); - when(getEipDefinitionFromPreferences(any(SharedPreferences.class))).thenAnswer(new Answer<JSONObject>() { + when(getEipDefinitionFromPreferences()).thenAnswer(new Answer<JSONObject>() { @Override public JSONObject answer(InvocationOnMock invocation) throws Throwable { + return getEipDefinitionFromPreferences(preferences); } }); diff --git a/app/src/test/java/se/leap/bitmaskclient/tethering/TetheringStateManagerTest.java b/app/src/test/java/se/leap/bitmaskclient/tethering/TetheringStateManagerTest.java index d972923c..a2569282 100644 --- a/app/src/test/java/se/leap/bitmaskclient/tethering/TetheringStateManagerTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/tethering/TetheringStateManagerTest.java @@ -68,8 +68,6 @@ public class TetheringStateManagerTest { PowerMockito.whenNew(IntentFilter.class).withArguments(anyString()).thenReturn(intentFilter); PowerMockito.whenNew(IntentFilter.class).withNoArguments().thenReturn(intentFilter); mockStatic(PreferenceHelper.class); - SharedPreferences sharedPreferences = new MockSharedPreferences(); - when(PreferenceHelper.getSharedPreferences(any())).thenReturn(sharedPreferences); observable = TetheringObservable.getInstance(); |