Skip to content

Commit

Permalink
Added tests for saving user data. Added clearUser method to remove st…
Browse files Browse the repository at this point in the history
…ored user information
  • Loading branch information
Pezzah authored and kattrali committed Sep 9, 2016
1 parent 153f973 commit 7b550f4
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
63 changes: 61 additions & 2 deletions src/androidTest/java/com/bugsnag/android/ClientTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.bugsnag.android;

import android.content.Context;
import android.content.SharedPreferences;

public class ClientTest extends BugsnagTestCase {
public void testNullContext() {
try {
Expand All @@ -10,8 +13,8 @@ public void testNullContext() {

public void testNullApiKey() {
try {
Client client = new Client(getContext(), null, true);
fail("Should throw for null Contexts");
Client client = new Client(getContext(), null);
fail("Should throw for null API Key");
} catch(NullPointerException e) { }
}

Expand All @@ -31,4 +34,60 @@ public void testConfig() {
client.notify(new RuntimeException("Testing"));
}

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);
sharedPref.edit()
.putString("user.id", "123456")
.putString("user.email", "mr.test@email.com")
.putString("user.name", "Mr Test")
.commit();

client = new Client(getContext(), "api-key");
final User user = new User();

client.beforeNotify(new BeforeNotify() {
@Override
public boolean run(Error error) {
// Pull out the user information
user.setId(error.getUser().getId());
user.setEmail(error.getUser().getEmail());
user.setName(error.getUser().getName());
return true;
}
});

client.notify(new RuntimeException("Testing"));

// Check the user details have been set
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));

// Tidy up
client.clearUser();
}

}
16 changes: 16 additions & 0 deletions src/main/java/com/bugsnag/android/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,22 @@ public void setUser(String id, String email, String name) {
setUserName(name);
}

/**
* Removes the current user data and sets it back to defaults
*/
public void clearUser() {
user.setId(deviceData.getUserId());
user.setEmail(null);
user.setName(null);

SharedPreferences sharedPref = appContext.getSharedPreferences(SHARED_PREF_KEY, Context.MODE_PRIVATE);
sharedPref.edit()
.remove(USER_ID_KEY)
.remove(USER_EMAIL_KEY)
.remove(USER_NAME_KEY)
.commit();
}

/**
* Set a unique identifier for the user currently using your application.
* By default, this will be an automatically generated unique id
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/bugsnag/android/Error.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ public void setUser(String id, String email, String name) {
this.user = new User(id, email, name);
}

/**
* @return user information associated with this Error
*/
public User getUser() {
return user;
}

/**
* Set user id associated with this Error
*
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/bugsnag/android/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,26 @@ public void toStream(@NonNull JsonStream writer) throws IOException {
writer.endObject();
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
Expand Down

0 comments on commit 7b550f4

Please sign in to comment.