Skip to content

Commit

Permalink
Merge 3a215e6 into a6458a0
Browse files Browse the repository at this point in the history
  • Loading branch information
Anna Kocheshkova authored Feb 22, 2019
2 parents a6458a0 + 3a215e6 commit a589056
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ public void loginThenDownloadValidConfigurationThenForeground() throws Exception
/* Just call back and nothing to verify. */
callback.onCancel();
callback.onSuccess(mockResult);
verify(mPreferenceTokenStorage).saveToken(eq(mockIdToken), eq(mockAccountId));
callback.onError(mock(MsalException.class));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.microsoft.appcenter.push.ingestion.models.PushInstallationLog;
import com.microsoft.appcenter.push.ingestion.models.json.PushInstallationLogFactory;
import com.microsoft.appcenter.utils.AppCenterLog;
import com.microsoft.appcenter.utils.context.AbstractTokenContextListener;
import com.microsoft.appcenter.utils.context.AuthTokenContext;
import com.microsoft.appcenter.utils.context.UserIdContext;
import com.microsoft.appcenter.utils.async.AppCenterFuture;

Expand Down Expand Up @@ -99,6 +101,16 @@ public class Push extends AbstractAppCenterService {
*/
private boolean mTokenNeedsRegistrationInForeground;

/**
* The last value of push token.
*/
private String mLatestPushToken;

/**
* Authorization listener for {@link AuthTokenContext}.
*/
private AbstractTokenContextListener mAuthListener;

/**
* Init.
*/
Expand Down Expand Up @@ -175,8 +187,7 @@ public static void checkLaunchedFromNotification(Activity activity, Intent inten
* Sender ID of your project before starting the Push service.
*
* @param senderId sender ID of your project.
*
* @deprecated For all the Android developers using App Center, there is a change coming where
* @deprecated For all the Android developers using App Center, there is a change coming where
* Firebase SDK is required to use Push Notifications. For Android P, its scheduled at
* the release date for the latest OS version. For all other versions of Android, it will be
* required after April 2019. Please follow the migration guide at https://aka.ms/acfba.
Expand Down Expand Up @@ -238,6 +249,7 @@ private void enqueuePushInstallationLog(@NonNull String pushToken) {
synchronized void onTokenRefresh(final String pushToken) {
if (pushToken != null) {
AppCenterLog.debug(LOG_TAG, "Push token refreshed: " + pushToken);
mLatestPushToken = pushToken;
post(new Runnable() {

@Override
Expand All @@ -256,7 +268,10 @@ public void run() {
@Override
protected synchronized void applyEnabledState(boolean enabled) {
if (enabled) {
AuthTokenContext.getInstance().addListener(mAuthListener);
registerPushToken();
} else {
AuthTokenContext.getInstance().removeListener(mAuthListener);
}
}

Expand Down Expand Up @@ -288,6 +303,15 @@ public Map<String, LogFactory> getLogFactories() {
@Override
public synchronized void onStarted(@NonNull Context context, @NonNull Channel channel, String appSecret, String transmissionTargetToken, boolean startedFromApp) {
mContext = context;
mAuthListener = new AbstractTokenContextListener() {

@Override
public synchronized void onNewUser(String authToken) {
if (mLatestPushToken != null) {
enqueuePushInstallationLog(mLatestPushToken);
}
}
};
super.onStarted(context, channel, appSecret, transmissionTargetToken, startedFromApp);
if (FirebaseUtils.isFirebaseAvailable() && !mFirebaseAnalyticsEnabled) {
AppCenterLog.debug(LOG_TAG, "Disabling Firebase analytics collection by default.");
Expand All @@ -306,7 +330,6 @@ private synchronized void setInstanceListener(PushListener instanceListener) {
* We can miss onCreate onStarted depending on how developers init the SDK.
* So look for multiple events.
*/

@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
checkPushInActivityIntent(activity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import com.microsoft.appcenter.push.ingestion.models.json.PushInstallationLogFactory;
import com.microsoft.appcenter.utils.AppCenterLog;
import com.microsoft.appcenter.utils.HandlerUtils;
import com.microsoft.appcenter.utils.context.UserIdContext;
import com.microsoft.appcenter.utils.UUIDUtils;
import com.microsoft.appcenter.utils.async.AppCenterConsumer;
import com.microsoft.appcenter.utils.async.AppCenterFuture;
import com.microsoft.appcenter.utils.context.AuthTokenContext;
import com.microsoft.appcenter.utils.context.UserIdContext;
import com.microsoft.appcenter.utils.storage.SharedPreferencesManager;

import org.junit.Before;
Expand Down Expand Up @@ -57,6 +59,7 @@
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.same;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
Expand Down Expand Up @@ -718,6 +721,72 @@ public void firebaseAnalyticsThrowsIllegalAccessError() {
FirebaseAnalytics.getInstance(any(Context.class));
}

@Test
public void verifyEnqueueCalledOnNewAuthToken() {
Push push = Push.getInstance();
Channel channel = mock(Channel.class);
doNothing().when(channel).enqueue(any(com.microsoft.appcenter.ingestion.models.Log.class), anyString(), anyInt());
start(push, channel);
push.onTokenRefresh("push-token");
String mockToken = UUIDUtils.randomUUID().toString();
String mockHomeId = UUIDUtils.randomUUID().toString();
AuthTokenContext.getInstance().setAuthToken(mockToken, mockHomeId);
verify(channel, times(2)).enqueue(any(com.microsoft.appcenter.ingestion.models.Log.class), anyString(), anyInt());
}

@Test
public void verifyEnqueueNotCalledOnTheSameUser() {
Push push = Push.getInstance();
Channel channel = mock(Channel.class);
doNothing().when(channel).enqueue(any(com.microsoft.appcenter.ingestion.models.Log.class), anyString(), anyInt());
start(push, channel);
push.onTokenRefresh("push-token");
String mockToken = UUIDUtils.randomUUID().toString();
String mockHomeId = UUIDUtils.randomUUID().toString();
AuthTokenContext.getInstance().setAuthToken(mockToken, mockHomeId);
AuthTokenContext.getInstance().setAuthToken(mockToken, mockHomeId);
verify(channel, times(2)).enqueue(any(com.microsoft.appcenter.ingestion.models.Log.class), anyString(), anyInt());
}

@Test
public void verifyEnqueueCalledOnNewUser() {
Push push = Push.getInstance();
Channel channel = mock(Channel.class);
doNothing().when(channel).enqueue(any(com.microsoft.appcenter.ingestion.models.Log.class), anyString(), anyInt());
start(push, channel);
push.onTokenRefresh("push-token");
String mockToken = UUIDUtils.randomUUID().toString();
String mockHomeId = UUIDUtils.randomUUID().toString();
AuthTokenContext.getInstance().setAuthToken(mockToken, mockHomeId);
AuthTokenContext.getInstance().setAuthToken(mockToken, "new-id");
verify(channel, times(3)).enqueue(any(com.microsoft.appcenter.ingestion.models.Log.class), anyString(), anyInt());
}

@Test
public void verifyEnqueueNotCalledOnNewAuthTokenBeforeRegistration() {
Push push = Push.getInstance();
Channel channel = mock(Channel.class);
doNothing().when(channel).enqueue(any(com.microsoft.appcenter.ingestion.models.Log.class), anyString(), anyInt());
start(push, channel);
String mockToken = UUIDUtils.randomUUID().toString();
String mockHomeId = UUIDUtils.randomUUID().toString();
AuthTokenContext.getInstance().setAuthToken(mockToken, mockHomeId);
verify(channel, never()).enqueue(any(com.microsoft.appcenter.ingestion.models.Log.class), anyString(), anyInt());
}

@Test
public void verifyEnqueueNotCalledOnPushDisabled() {
Push push = Push.getInstance();
Channel channel = mock(Channel.class);
doNothing().when(channel).enqueue(any(com.microsoft.appcenter.ingestion.models.Log.class), anyString(), anyInt());
start(push, channel);
Push.setEnabled(false);
String mockToken = UUIDUtils.randomUUID().toString();
String mockHomeId = UUIDUtils.randomUUID().toString();
AuthTokenContext.getInstance().setAuthToken(mockToken, mockHomeId);
verify(channel, never()).enqueue(any(com.microsoft.appcenter.ingestion.models.Log.class), anyString(), anyInt());
}

private static Intent createPushIntent(String title, String message, final Map<String, String> customData) {
mockStatic(PushIntentUtils.class);
Intent pushIntentMock = mock(Intent.class);
Expand All @@ -730,4 +799,4 @@ private static Intent createPushIntent(String title, String message, final Map<S
}
return pushIntentMock;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.microsoft.appcenter.utils.HandlerUtils;
import com.microsoft.appcenter.utils.IdHelper;
import com.microsoft.appcenter.utils.context.AuthTokenContext;
import com.microsoft.appcenter.utils.context.AbstractTokenContextListener;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -40,7 +41,7 @@

import static com.microsoft.appcenter.AppCenter.LOG_TAG;

public class DefaultChannel implements Channel, AuthTokenContext.Listener {
public class DefaultChannel implements Channel {

/**
* Persistence batch size for {@link Persistence#getLogs(String, Collection, int, List)} when clearing.
Expand Down Expand Up @@ -151,7 +152,13 @@ public DefaultChannel(@NonNull Context context, String appSecret, @NonNull LogSe
mEnabled = true;
AuthTokenContext authTokenContext = AuthTokenContext.getInstance();
mIngestion.setAuthToken(authTokenContext.getAuthToken());
authTokenContext.addListener(this);
authTokenContext.addListener(new AbstractTokenContextListener() {

@Override
public synchronized void onNewAuthToken(String authToken) {
mIngestion.setAuthToken(authToken);
}
});
}

/**
Expand Down Expand Up @@ -745,11 +752,6 @@ public synchronized void shutdown() {
suspend(false, new CancellationException());
}

@Override
public synchronized void onNewAuthToken(String authToken) {
mIngestion.setAuthToken(authToken);
}

/**
* State for a specific log group.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.microsoft.appcenter.utils.context;

/**
* Empty implementation to make callbacks optional.
*/
public abstract class AbstractTokenContextListener implements AuthTokenContext.Listener {

@Override
public void onNewAuthToken(String authToken) {
}

@Override
public void onNewUser(String authToken) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,25 @@ public synchronized String getAuthToken() {
*/
public void setAuthToken(String authToken, String homeAccountId) {
mAuthToken = authToken;
mLastHomeAccountId = homeAccountId;

/* Call listeners so that they can react on new token. */
for (Listener listener : mListeners) {
listener.onNewAuthToken(authToken);
if (isNewUser(homeAccountId)) {
listener.onNewUser(authToken);
}
}
mLastHomeAccountId = homeAccountId;
}

/**
* Check whether the user is new.
*
* @param newAccountId account id of the logged in user.
* @return true if this user is not the same as previous, false otehrwise.
*/
private synchronized boolean isNewUser(String newAccountId) {
return mLastHomeAccountId == null || !mLastHomeAccountId.equals(newAccountId);
}

/**
Expand All @@ -117,5 +130,10 @@ public interface Listener {
* Called whenever a new token is set.
*/
void onNewAuthToken(String authToken);

/**
* Called whenever a new user logs in.
*/
void onNewUser(String authToken);
}
}

0 comments on commit a589056

Please sign in to comment.