Skip to content

Commit

Permalink
Merge 2bfc331 into 7b3b02b
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rken committed Mar 13, 2018
2 parents 7b3b02b + 2bfc331 commit a9ec2e6
Show file tree
Hide file tree
Showing 10 changed files with 418 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static Client generateClient() {
}

static Session generateSession() {
return new Session("test", new Date(), new User(), false);
return new Session("test", new Date(), new User.Builder().build(), false);
}

static Configuration generateConfiguration() {
Expand Down
78 changes: 68 additions & 10 deletions sdk/src/androidTest/java/com/bugsnag/android/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
import org.junit.runner.RunWith;

import static com.bugsnag.android.BugsnagTestUtils.getSharedPrefs;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

Expand Down Expand Up @@ -93,31 +97,33 @@ public void testRestoreUserFromPrefs() {
client.setErrorReportApiClient(BugsnagTestUtils.generateErrorReportApiClient());
client.setSessionTrackingApiClient(BugsnagTestUtils.generateSessionTrackingApiClient());

final User user = new User();
final User[] user = new User[1];

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());
user[0] = error.getUser();
return true;
}
});

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

assertThat(user[0], is(not(nullValue())));

// Check the user details have been set
assertEquals(USER_ID, user.getId());
assertEquals(USER_EMAIL, user.getEmail());
assertEquals(USER_NAME, user.getName());
assertEquals(USER_ID, user[0].getId());
assertEquals(USER_EMAIL, user[0].getEmail());
assertEquals(USER_NAME, user[0].getName());
}

@Deprecated
@Test
public void testStoreUserInPrefs() {
public void testStoreUserInPrefsDeprecated() {
config.setPersistUserBetweenSessions(true);
Client client = new Client(context, config);
//noinspection deprecation
client.setUser(USER_ID, USER_EMAIL, USER_NAME);

// Check that the user was store in prefs
Expand All @@ -128,7 +134,21 @@ public void testStoreUserInPrefs() {
}

@Test
public void testStoreUserInPrefsDisabled() {
public void testStoreUserInPrefs() {
config.setPersistUserBetweenSessions(true);
Client client = new Client(context, config);
client.setUser(new User.Builder().id(USER_ID).email(USER_EMAIL).name(USER_NAME).build());

// Check that the user was store in prefs
SharedPreferences sharedPref = getSharedPrefs(context);
assertEquals(USER_ID, sharedPref.getString("user.id", null));
assertEquals(USER_EMAIL, sharedPref.getString("user.email", null));
assertEquals(USER_NAME, sharedPref.getString("user.name", null));
}

@Deprecated
@Test
public void testStoreUserInPrefsDisabledDeprecated() {
config.setPersistUserBetweenSessions(false);
Client client = new Client(context, config);
client.setUser(USER_ID, USER_EMAIL, USER_NAME);
Expand All @@ -141,7 +161,21 @@ public void testStoreUserInPrefsDisabled() {
}

@Test
public void testClearUser() {
public void testStoreUserInPrefsDisabled() {
config.setPersistUserBetweenSessions(false);
Client client = new Client(context, config);
client.setUser(new User.Builder().id(USER_ID).email(USER_EMAIL).name(USER_NAME).build());

// Check that the user was not stored in prefs
SharedPreferences sharedPref = getSharedPrefs(context);
assertFalse(sharedPref.contains("user.id"));
assertFalse(sharedPref.contains("user.email"));
assertFalse(sharedPref.contains("user.name"));
}

@Deprecated
@Test
public void testClearUserDeprecated() {
// Set a user in prefs
setUserPrefs();

Expand All @@ -156,6 +190,30 @@ public void testClearUser() {
assertFalse(sharedPref.contains("user.name"));
}

@Test
public void testClearUser() {
// Set a user in prefs
setUserPrefs();

// Clear the user using the command
Client client = new Client(context, "api-key");
client.setUser(null);

// Check that there is no user information in the prefs anymore
SharedPreferences sharedPref = getSharedPrefs(context);
assertFalse(sharedPref.contains("user.id"));
assertFalse(sharedPref.contains("user.email"));
assertFalse(sharedPref.contains("user.name"));
}

@Test
public void testSetGetUser() {
User user = new User.Builder().build();
Client client = new Client(context, "api-key");
client.setUser(user);
assertThat(client.getUser(), is(user));
}

@Test
public void testEmptyManifestConfig() {
Bundle data = new Bundle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void setUp() throws Exception {
sessionTracker = new SessionTracker(configuration, generateClient(), generateSessionStore(),
generateSessionTrackingApiClient());
configuration.setAutoCaptureSessions(true);
user = new User();
user = new User.Builder().build();
}

@Test
Expand Down
111 changes: 111 additions & 0 deletions sdk/src/androidTest/java/com/bugsnag/android/UserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.bugsnag.android;

import android.content.SharedPreferences;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.UUID;

import static com.bugsnag.android.BugsnagTestUtils.getSharedPrefs;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.Is.is;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class UserTest {

@Test
public void testToBuilder() {
final String id = UUID.randomUUID().toString();
final String email = UUID.randomUUID().toString();
final String name = UUID.randomUUID().toString();

User user = new User.Builder().name(name).id(id).email(email).build().toBuilder().build();

assertThat(user.getId(), is(id));
assertThat(user.getEmail(), is(email));
assertThat(user.getName(), is(name));
}

@Test
public void testBuilderDefaults() {
User user = new User.Builder().build();

assertThat(user.getId(), is(nullValue()));
assertThat(user.getEmail(), is(nullValue()));
assertThat(user.getName(), is(nullValue()));
}

@Test
public void testBuilderDefaultConstructor() {
final String id = UUID.randomUUID().toString();
final String email = UUID.randomUUID().toString();
final String name = UUID.randomUUID().toString();

User user = new User.Builder().name(name).id(id).email(email).build();

assertThat(user.getId(), is(id));
assertThat(user.getEmail(), is(email));
assertThat(user.getName(), is(name));
}

@Test
public void testBuilderUserArgument() {
final String id = UUID.randomUUID().toString();
final String email = UUID.randomUUID().toString();
final String name = UUID.randomUUID().toString();

User previous = new User.Builder().name(name).id(id).email(email).build();
User successor = new User.Builder(previous).build();

assertThat(successor.getId(), is(id));
assertThat(successor.getEmail(), is(email));
assertThat(successor.getName(), is(name));
}

@Test
public void testBuilderNullArgument() {
final String id = UUID.randomUUID().toString();
final String email = UUID.randomUUID().toString();
final String name = UUID.randomUUID().toString();

User user = new User.Builder(null).name(name).id(id).email(email).build();

assertThat(user.getId(), is(id));
assertThat(user.getEmail(), is(email));
assertThat(user.getName(), is(name));
}

@Test
public void testRepo() {
SharedPreferences sharedPref = getSharedPrefs(InstrumentationRegistry.getContext());
sharedPref.edit().clear().apply();

User pre = new User.Builder()
.name(UUID.randomUUID().toString())
.id(UUID.randomUUID().toString())
.email(UUID.randomUUID().toString())
.build();

User.Repo repo = new User.Repo(sharedPref);
assertThat(repo.get(),is(nullValue()));

repo.set(pre);

User post = repo.get();
assertThat(post, is(not(nullValue())));

assertThat(post.getId(), is(pre.getId()));
assertThat(post.getEmail(), is(pre.getEmail()));
assertThat(post.getName(), is(pre.getName()));

repo.set(null);
assertThat(repo.get(), is(nullValue()));
}
}
45 changes: 23 additions & 22 deletions sdk/src/main/java/com/bugsnag/android/Bugsnag.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,56 +234,56 @@ public static void setAutoCaptureSessions(boolean autoCapture) {
getClient().setAutoCaptureSessions(autoCapture);
}

/**
* @deprecated Use {@link #setUser(User)} and {@link com.bugsnag.android.User.Builder}<p>
*/
@Deprecated
public static void setUser(final String id, final String email, final String name) {
getClient().setUser(id, email, name);
}

/**
* Set details of the user currently using your application.
* You can search for this information in your Bugsnag dashboard.
* <p>
* For example:
* <p>
* Bugsnag.setUser("12345", "james@example.com", "James Smith");
* Bugsnag.setUser(User.builder().id("12345").email("james@example.com").name("James Smith"));
*
* @param id a unique identifier of the current user (defaults to a unique id)
* @param email the email address of the current user
* @param name the name of the current user
* @param user The user information to set or NULL to reset to defaults.
*/
public static void setUser(final String id, final String email, final String name) {
getClient().setUser(id, email, name);
public static void setUser(@Nullable User user) {
getClient().setUser(user);
}

/**
* Removes the current user data and sets it back to defaults
* @deprecated Use {@link #setUser(User)} and pass NULL<p>
*/
@Deprecated
public static void clearUser() {
getClient().clearUser();
getClient().setUser(null);
}

/**
* Set a unique identifier for the user currently using your application.
* By default, this will be an automatically generated unique id
* You can search for this information in your Bugsnag dashboard.
*
* @param id a unique identifier of the current user
* @deprecated Use {@link #setUser(User)} and {@link com.bugsnag.android.User.Builder}<p>
*/
@Deprecated
public static void setUserId(final String id) {
getClient().setUserId(id);
}

/**
* Set the email address of the current user.
* You can search for this information in your Bugsnag dashboard.
*
* @param email the email address of the current user
* @deprecated Use {@link #setUser(User)} and {@link com.bugsnag.android.User.Builder}<p>
*/
@Deprecated
public static void setUserEmail(final String email) {
getClient().setUserEmail(email);
}

/**
* Set the name of the current user.
* You can search for this information in your Bugsnag dashboard.
*
* @param name the name of the current user
* @deprecated Use {@link #setUser(User)} and {@link com.bugsnag.android.User.Builder}<p>
*/
@Deprecated
public static void setUserName(final String name) {
getClient().setUserName(name);
}
Expand Down Expand Up @@ -549,7 +549,8 @@ public static void clearTab(String tabName) {
*
* @see MetaData
*/
@NonNull public static MetaData getMetaData() {
@NonNull
public static MetaData getMetaData() {
return getClient().getMetaData();
}

Expand Down

0 comments on commit a9ec2e6

Please sign in to comment.