Skip to content
Merged
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 @@ -4,6 +4,4 @@ public interface OSTime {
long getCurrentTimeMillis();

long getElapsedRealtime();

long getCurrentThreadTimeMillis();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,4 @@ public long getCurrentTimeMillis() {
public long getElapsedRealtime() {
return SystemClock.elapsedRealtime();
}

@Override
public long getCurrentThreadTimeMillis() {
return SystemClock.currentThreadTimeMillis();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,14 @@ public static void onMessageReceived(@NonNull Context context, @NonNull RemoteMe
String data = message.getData();
try {
JSONObject messageDataJSON = new JSONObject(message.getData());
messageDataJSON.put(HMS_TTL_KEY, message.getTtl());
messageDataJSON.put(HMS_SENT_TIME_KEY, message.getSentTime());
if (message.getTtl() == 0)
messageDataJSON.put(HMS_TTL_KEY, OSNotificationRestoreWorkManager.DEFAULT_TTL_IF_NOT_IN_PAYLOAD);
else
messageDataJSON.put(HMS_TTL_KEY, message.getTtl());
if (message.getSentTime() == 0)
messageDataJSON.put(HMS_SENT_TIME_KEY, OneSignal.getTime().getCurrentTimeMillis());
else
messageDataJSON.put(HMS_SENT_TIME_KEY, message.getSentTime());
data = messageDataJSON.toString();
} catch (JSONException e) {
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "OneSignalHmsEventBridge error when trying to create RemoteMessage data JSON");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ public class MockOSTimeImpl extends OSTimeImpl {

private Long mockedTime = null;
private Long mockedElapsedTime = null;
private Long mockedCurrentThreadTimeMillis = null;

public void reset() {
mockedTime = null;
mockedElapsedTime = null;
mockedCurrentThreadTimeMillis = null;
}

@Override
Expand All @@ -22,11 +20,6 @@ public long getElapsedRealtime() {
return mockedElapsedTime != null ? mockedElapsedTime : super.getElapsedRealtime();
}

@Override
public long getCurrentThreadTimeMillis() {
return mockedCurrentThreadTimeMillis != null ? mockedCurrentThreadTimeMillis : super.getCurrentThreadTimeMillis();
}

public void setMockedTime(Long mockedTime) {
this.mockedTime = mockedTime;
}
Expand All @@ -35,20 +28,11 @@ public void setMockedElapsedTime(Long mockedForegroundTime) {
this.mockedElapsedTime = mockedForegroundTime;
}

public void setMockedCurrentThreadTimeMillis(Long mockedCurrentThreadTimeMillis) {
this.mockedCurrentThreadTimeMillis = mockedCurrentThreadTimeMillis;
}

public void advanceSystemTimeBy(long sec) {
long ms = sec * 1_000L;
setMockedTime(getCurrentTimeMillis() + ms);
}

public void advanceThreadTimeBy(long sec) {
long ms = sec * 1_000L;
setMockedCurrentThreadTimeMillis(getCurrentThreadTimeMillis() + ms);
}

public void advanceSystemAndElapsedTimeBy(long sec) {
long ms = sec * 1_000L;
setMockedElapsedTime(getCurrentTimeMillis() + ms);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ public static class InAppMessageTable extends OneSignalDbContract.InAppMessageTa
}

public static class OSNotificationRestoreWorkManager extends com.onesignal.OSNotificationRestoreWorkManager {
public static int getDEFAULT_TTL_IF_NOT_IN_PAYLOAD() {
return DEFAULT_TTL_IF_NOT_IN_PAYLOAD;
}
}

public static class OSNotificationGenerationJob extends com.onesignal.OSNotificationGenerationJob {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.onesignal;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

@Implements(NotificationPayloadProcessorHMS.class)
public class ShadowHmsNotificationPayloadProcessor {

private static @Nullable
String messageData;

public static void resetStatics() {
messageData = null;
}

@Implementation
public static void processDataMessageReceived(@NonNull final Context context, @Nullable String data) {
messageData = data;
}

@Nullable
public static String getMessageData() {
return messageData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.huawei.hms.push.RemoteMessage;
import com.onesignal.MockOSTimeImpl;
import com.onesignal.OneSignal;
import com.onesignal.OneSignalPackagePrivateHelper;
import com.onesignal.OneSignalPackagePrivateHelper.NotificationPayloadProcessorHMS;
import com.onesignal.ShadowBadgeCountUpdater;
import com.onesignal.ShadowGenerateNotification;
import com.onesignal.ShadowHmsNotificationPayloadProcessor;
import com.onesignal.ShadowHmsRemoteMessage;
import com.onesignal.ShadowNotificationManagerCompat;
import com.onesignal.ShadowOSUtils;
Expand All @@ -33,6 +37,8 @@

import java.util.UUID;

import static com.onesignal.OneSignalHmsEventBridge.HMS_SENT_TIME_KEY;
import static com.onesignal.OneSignalHmsEventBridge.HMS_TTL_KEY;
import static com.onesignal.OneSignalPackagePrivateHelper.HMSEventBridge_onMessageReceive;
import static com.onesignal.OneSignalPackagePrivateHelper.HMSProcessor_processDataMessageReceived;
import static com.onesignal.OneSignalPackagePrivateHelper.OSNotificationFormatHelper.PAYLOAD_OS_NOTIFICATION_ID;
Expand Down Expand Up @@ -126,7 +132,7 @@ public void ttl_shouldNotDisplayNotification() throws Exception {
long sentTime = 1_635_971_895_940L;
int ttl = 60;

time.setMockedCurrentThreadTimeMillis(sentTime * 1_000);
time.setMockedTime(sentTime * 1_000);

ShadowHmsRemoteMessage.data = helperBasicOSPayload();
ShadowHmsRemoteMessage.ttl = ttl;
Expand All @@ -138,6 +144,28 @@ public void ttl_shouldNotDisplayNotification() throws Exception {
assertEquals(0, ShadowBadgeCountUpdater.lastCount);
}

@Test
@Config(shadows = { ShadowGenerateNotification.class, ShadowHmsRemoteMessage.class, ShadowBadgeCountUpdater.class, ShadowHmsNotificationPayloadProcessor.class })
public void ttl_shouldDisplayNotificationWithNoTTLandSentTime() throws Exception {
blankActivityController.pause();

long sentTime = 1_635_971_895_940L;

time.setMockedTime(sentTime * 1_000);
long setSentTime = time.getCurrentTimeMillis();

ShadowHmsRemoteMessage.data = helperBasicOSPayload();

HMSEventBridge_onMessageReceive(blankActivity, new RemoteMessage(new Bundle()));
threadAndTaskWait();

String messageData = ShadowHmsNotificationPayloadProcessor.getMessageData();
JSONObject jsonObject = new JSONObject(messageData);

assertEquals(OneSignalPackagePrivateHelper.OSNotificationRestoreWorkManager.getDEFAULT_TTL_IF_NOT_IN_PAYLOAD(), jsonObject.getInt(HMS_TTL_KEY));
assertEquals(setSentTime, jsonObject.getLong(HMS_SENT_TIME_KEY));
}

// NOTE: More tests can be added but they would be duplicated with GenerateNotificationRunner
// In 4.0.0 or later these should be written in a reusable way between HMS, FCM, and ADM
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.onesignal.ShadowGoogleApiClientCompatProxy;
import com.onesignal.ShadowHMSFusedLocationProviderClient;
import com.onesignal.ShadowHmsInstanceId;
import com.onesignal.ShadowHmsNotificationPayloadProcessor;
import com.onesignal.ShadowNotificationManagerCompat;
import com.onesignal.ShadowNotificationReceivedEvent;
import com.onesignal.ShadowOSUtils;
Expand Down Expand Up @@ -130,6 +131,7 @@ static void beforeTestInitAndCleanup() throws Exception {
ShadowNotificationReceivedEvent.resetStatics();
ShadowOneSignalNotificationManager.resetStatics();
ShadowBadgeCountUpdater.resetStatics();
ShadowHmsNotificationPayloadProcessor.resetStatics();
ShadowFocusHandler.Companion.resetStatics();

lastException = null;
Expand Down