Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Null check to prevent crash for TrailHead Go Android #2633

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -655,9 +655,11 @@ public Bundle asBundle() {
/**
* Build a LoginOptions from the given bundle
* @param options - bundle
* @return
*/
public static LoginOptions fromBundle(Bundle options) {
if (options == null) {
return new LoginOptions(null, null, null, null, null, null); // To prevent crash if null Bundle is passed
}
Map<String, String> additionalParameters = null;
final Serializable serializable = options.getSerializable(KEY_ADDL_PARAMS);
if (serializable != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ internal abstract class AppLockManager(
EventsObservable.get().notifyEvent(EventsObservable.EventType.AppUnlocked)
}

fun getAccountPrefs(account: UserAccount): SharedPreferences {
fun getAccountPrefs(account: UserAccount?): SharedPreferences {
val ctx = SalesforceSDKManager.getInstance().appContext
return ctx.getSharedPreferences(policyKey + account.userLevelFilenameSuffix, Context.MODE_PRIVATE)
return ctx.getSharedPreferences(policyKey + account?.userLevelFilenameSuffix, Context.MODE_PRIVATE)
}

fun getGlobalPrefs(): SharedPreferences {
val ctx = SalesforceSDKManager.getInstance().appContext
return ctx.getSharedPreferences(policyKey, Context.MODE_PRIVATE)
}

fun getPolicy(account: UserAccount): Policy {
fun getPolicy(account: UserAccount?): Policy {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect. There are two places where this is API is used and both check the account is not null first.

val accountPolicy = getAccountPrefs(account)
return accountPolicy.getBoolean(enabledKey, false) to accountPolicy.getInt(timeoutKey, 0)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ public void testLoginOptionsWithAdditionalParams() {
Assert.assertEquals("LoginOptions.additionalParameters must have parameter",additionalParams.get("p4"),loginOptions.getAdditionalParameters().get("p4"));
}

/**
* Test setting/get of Login Options as a null bundle
*/
@Test
public void testLoginOptionsWithNullBundle() {
LoginOptions loginOptions = LoginOptions.fromBundle(null);
Assert.assertNotNull("LoginOptions from bundle must not be null", loginOptions);
}

/**
* Test createNewAccount
*/
Expand Down Expand Up @@ -507,6 +516,13 @@ public void testLoginOptionsFromBundleWithSafeLoginUrl() {

}

@Test
public void testLoginOptionsNullFromBundleWithSafeLoginUrl() {
// Expect the LoginOptions to have the same login url
LoginOptions loginOptions = LoginOptions.fromBundleWithSafeLoginUrl(null);
Assert.assertNotNull(loginOptions.getLoginUrl());
}

/**
* Checks there are no test accounts
*/
Expand Down