Closed ntarocco closed 9 years ago
Not tried with Robolectric (will add to my todo), which version of shared prefs? 0.1.0 or the older 0.4.0?
0.0.4, I can try to see if with the version 0.1.0 the problem is not reproducible...
Are you sure you're tests are being correctly cleared down afterwards? I found with the unit tests I wrote in the lib, i had to manually delete the created pref xml files between tests otherwise it would reuse.
We saw the same issue. We worked around it by manually setting the private static SharedPreferences field to null before each test. So, the root issue is because the SecurePreferences class uses the static field, so suppling a different filename will get ignored if SecurePreferences was instantiated with a different file name in a test that ran before this one. We suspect that the sFile variable being static may be a mistake due the comment above it: //default for testing
Here's our subclass that allows us to reset the static field before instantiating SecurePreferences again:
public class TestSafeSecurePreferences extends SecurePreferences {
public TestSafeSecurePreferences(Context context) {
super(context);
}
public TestSafeSecurePreferences(Context context, String password, String sharedPrefFilename) {
super(context, password, sharedPrefFilename);
}
public static void clearStaticState() {
try {
Field staticField = SecurePreferences.class.getDeclaredField("sFile");
staticField.setAccessible(true);
staticField.set(null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Thanks for this see #22, a fix will be in version 0.1.2
Cool!. Do you have a target release date for version 0.1.2?
@pivotal-csaa-dev 0.1.2 has been pushed to maven central, awaiting replication.
I am using secure-preferences to encrypt my local preferences. However, when using Robolectric to run unit tests, the preferences are not correctly filled in, and when trying to check if the key is contained in the file, it is always empty.
My app code for the shared prefs:
Then, my test code:
and my function in "handleAction()" will read VALUE, with:
but is always null, shared preferences always empty. If I remove SecurePreferences and use standard Android prefs, everything works.
Any clue how to solve this and if it is a problem of the library?