Skip to content

Commit

Permalink
Added setup and teardown to client tests, and added test for clear user
Browse files Browse the repository at this point in the history
  • Loading branch information
Pezzah authored and kattrali committed Sep 9, 2016
1 parent 7b550f4 commit 9c17102
Showing 1 changed file with 48 additions and 12 deletions.
60 changes: 48 additions & 12 deletions src/androidTest/java/com/bugsnag/android/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -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() {
Expand All @@ -67,27 +79,51 @@ 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
SharedPreferences sharedPref = getContext().getSharedPreferences("com.bugsnag.android", Context.MODE_PRIVATE);
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();
}
}

0 comments on commit 9c17102

Please sign in to comment.