diff --git a/src/androidTest/java/com/bugsnag/android/ClientTest.java b/src/androidTest/java/com/bugsnag/android/ClientTest.java index 4ea90c79ab..c6d929d624 100644 --- a/src/androidTest/java/com/bugsnag/android/ClientTest.java +++ b/src/androidTest/java/com/bugsnag/android/ClientTest.java @@ -4,6 +4,21 @@ import android.content.SharedPreferences; public class ClientTest extends BugsnagTestCase { + + @Override + protected void setUp() throws Exception { + super.setUp(); + + // Make sure no user is stored + SharedPreferences sharedPref = getContext().getSharedPreferences("com.bugsnag.android", Context.MODE_PRIVATE); + sharedPref.edit() + .remove("user.id") + .remove("user.email") + .remove("user.name") + .commit(); + } + + public void testNullContext() { try { Client client = new Client(null, "api-key"); @@ -35,9 +50,6 @@ public void testConfig() { } public void testRestoreUserFromPrefs() { - // Ensure that there is no user set in prefs to start with - Client client = new Client(getContext(), "api-key"); - client.clearUser(); // Set a user in prefs SharedPreferences sharedPref = getContext().getSharedPreferences("com.bugsnag.android", Context.MODE_PRIVATE); @@ -47,7 +59,7 @@ public void testRestoreUserFromPrefs() { .putString("user.name", "Mr Test") .commit(); - client = new Client(getContext(), "api-key"); + Client client = new Client(getContext(), "api-key"); final User user = new User(); client.beforeNotify(new BeforeNotify() { @@ -67,17 +79,10 @@ public boolean run(Error error) { assertEquals("123456", user.getId()); assertEquals("mr.test@email.com", user.getEmail()); assertEquals("Mr Test", user.getName()); - - // Tidy up - client.clearUser(); } public void testStoreUserInPrefs() { - // Ensure that there is no user set in prefs to start with Client client = new Client(getContext(), "api-key"); - client.clearUser(); - - client = new Client(getContext(), "api-key"); client.setUser("123456", "mr.test@email.com", "Mr Test"); // Check that the user was store in prefs @@ -85,9 +90,40 @@ public void testStoreUserInPrefs() { assertEquals("123456", sharedPref.getString("user.id", null)); assertEquals("mr.test@email.com", sharedPref.getString("user.email", null)); assertEquals("Mr Test", sharedPref.getString("user.name", null)); + } + + public void TestClearUser() { - // Tidy up + // Set a user in prefs + SharedPreferences sharedPref = getContext().getSharedPreferences("com.bugsnag.android", Context.MODE_PRIVATE); + sharedPref.edit() + .putString("user.id", "123456") + .putString("user.email", "mr.test@email.com") + .putString("user.name", "Mr Test") + .commit(); + + // Clear the user using the command + Client client = new Client(getContext(), "api-key"); client.clearUser(); + + // Check that there is no user information in the prefs anymore + sharedPref = getContext().getSharedPreferences("com.bugsnag.android", Context.MODE_PRIVATE); + assertFalse(sharedPref.contains("user.id")); + assertFalse(sharedPref.contains("user.email")); + assertFalse(sharedPref.contains("user.name")); } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + + // Make sure no user is stored + SharedPreferences sharedPref = getContext().getSharedPreferences("com.bugsnag.android", Context.MODE_PRIVATE); + sharedPref.edit() + .remove("user.id") + .remove("user.email") + .remove("user.name") + .commit(); + } }