Skip to content

Commit

Permalink
MM-1388 - Geo reinitalization handled. Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tjuric committed Mar 13, 2017
1 parent ad7faf2 commit 40ed906
Show file tree
Hide file tree
Showing 13 changed files with 396 additions and 155 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package org.infobip.mobile.messaging.geo;

import android.content.Context;
import android.preference.PreferenceManager;
import android.test.InstrumentationTestCase;

import com.google.android.gms.location.Geofence;

import org.infobip.mobile.messaging.Message;
import org.infobip.mobile.messaging.MobileMessaging;
import org.infobip.mobile.messaging.MobileMessagingCore;
import org.infobip.mobile.messaging.MobileMessagingProperty;
import org.infobip.mobile.messaging.api.support.Tuple;
import org.infobip.mobile.messaging.api.support.http.serialization.JsonSerializer;
import org.infobip.mobile.messaging.storage.MessageStore;
import org.infobip.mobile.messaging.storage.SQLiteMessageStore;
import org.infobip.mobile.messaging.util.DateTimeUtil;
import org.infobip.mobile.messaging.util.PreferenceHelper;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
Expand All @@ -28,7 +30,7 @@
public class GeoMonitoringTest extends InstrumentationTestCase {

private Context context;
private MessageStore messageStore;
private MessageStore geoStore;
private Long now;

@Override
Expand All @@ -38,108 +40,115 @@ protected void setUp() throws Exception {
context = getInstrumentation().getContext().getApplicationContext();
now = System.currentTimeMillis();

Geofencing.getInstance(context);

PreferenceManager.getDefaultSharedPreferences(context).edit().clear().commit();
PreferenceHelper.saveString(context, MobileMessagingProperty.MESSAGE_STORE_CLASS, SQLiteMessageStore.class.getName());
messageStore = MobileMessaging.getInstance(context).getMessageStore();
messageStore.deleteAll(context);

Geofencing.getInstance(context);
geoStore = MobileMessagingCore.getInstance(context).getMessageStoreForGeo();
geoStore.deleteAll(context);
}

public void test_shouldCalculateRefreshDateForGeoStart() throws Exception {
public void test_shouldCalculateRefreshDatesForGeoStartAndExpired() throws Exception {
// Given
Long millis15MinAfterNow = now + 15 * 60 * 1000;
Long millis30MinAfterNow = now + 30 * 60 * 1000;
String date15MinAfterNow = DateTimeUtil.ISO8601DateToString(new Date(millis15MinAfterNow));
String date30MinAfterNow = DateTimeUtil.ISO8601DateToString(new Date(millis30MinAfterNow));

Geo geo = new Geo(0.0, 0.0, new ArrayList<Area>() {{
add(new Area("SomeAreaId", "SomeAreaTitle", 0.0, 0.0, 10));
}}, null, new ArrayList<GeoEvent>(), date30MinAfterNow, date15MinAfterNow, "SomeCampaignId");

JSONObject internalData = new JSONObject(new JsonSerializer().serialize(geo));
Message message = new Message(
"SomeMessageId",
"SomeTitle",
"SomeBody",
"SomeSound",
true,
"SomeIcon",
true,
"SomeCategory",
"SomeFrom",
now,
0,
internalData,
null,
geo,
"SomeDestination",
Message.Status.UNKNOWN,
"SomeStatusMessage"
);
messageStore.save(context, message);
saveGeoMessageToDb(date15MinAfterNow, date30MinAfterNow);

// When
Tuple<List<Geofence>, Date> geofencesAndNextRefreshDate = Geofencing.calculateGeofencesToMonitorAndNextCheckDate(messageStore);
Tuple<List<Geofence>, Tuple<Date, Date>> geofencesAndNextRefreshDate = Geofencing.calculateGeofencesToMonitorAndNextCheckDates(geoStore);

// Then
assertNotNull(geofencesAndNextRefreshDate);
assertTrue(geofencesAndNextRefreshDate.getLeft().isEmpty());
assertNotNull(geofencesAndNextRefreshDate.getRight());
assertEquals(millis15MinAfterNow, geofencesAndNextRefreshDate.getRight().getTime(), 3000);

Date refreshStartDate = geofencesAndNextRefreshDate.getRight().getLeft();
Date refreshExpiryDate = geofencesAndNextRefreshDate.getRight().getRight();
assertEquals(millis15MinAfterNow, refreshStartDate.getTime(), 3000);
assertEquals(millis30MinAfterNow, refreshExpiryDate.getTime(), 3000);
}

public void test_shouldNotCalculateRefreshDateIfGeoExpired() throws Exception {
public void test_shouldNotCalculateRefreshDateForGeoStartIfGeoExpired() throws Exception {
// Given
Long millis30MinBeforeNow = now - 30 * 60 * 1000;
Long millis15MinBeforeNow = now - 15 * 60 * 1000;
String date30MinBeforeNow = DateTimeUtil.ISO8601DateToString(new Date(millis30MinBeforeNow));
String date15MinBeforeNow = DateTimeUtil.ISO8601DateToString(new Date(millis15MinBeforeNow));

Geo geo = new Geo(0.0, 0.0, new ArrayList<Area>() {{
add(new Area("SomeAreaId", "SomeAreaTitle", 0.0, 0.0, 10));
}}, null, new ArrayList<GeoEvent>(), date15MinBeforeNow, date30MinBeforeNow, "SomeCampaignId");
saveGeoMessageToDb(date30MinBeforeNow, date15MinBeforeNow);

JSONObject internalData = new JSONObject(new JsonSerializer().serialize(geo));
Message message = new Message(
"SomeMessageId",
"SomeTitle",
"SomeBody",
"SomeSound",
true,
"SomeIcon",
true,
"SomeCategory",
"SomeFrom",
now,
0,
internalData,
null,
geo,
"SomeDestination",
Message.Status.UNKNOWN,
"SomeStatusMessage"
);
messageStore.save(context, message);
// When
Tuple<List<Geofence>, Tuple<Date, Date>> geofencesAndNextRefreshDate = Geofencing.calculateGeofencesToMonitorAndNextCheckDates(geoStore);

// Then
assertNotNull(geofencesAndNextRefreshDate);
assertTrue(geofencesAndNextRefreshDate.getLeft().isEmpty());
assertNull(geofencesAndNextRefreshDate.getRight().getLeft());
}

public void test_shouldCalculateRefreshDateForGeoExpiredIfGeoExpired() throws Exception {
// Given
Long millis30MinBeforeNow = now - 30 * 60 * 1000;
Long millis15MinBeforeNow = now - 15 * 60 * 1000;
String date30MinBeforeNow = DateTimeUtil.ISO8601DateToString(new Date(millis30MinBeforeNow));
String date15MinBeforeNow = DateTimeUtil.ISO8601DateToString(new Date(millis15MinBeforeNow));

saveGeoMessageToDb(date30MinBeforeNow, date15MinBeforeNow);

// When
Tuple<List<Geofence>, Date> geofencesAndNextRefreshDate = Geofencing.calculateGeofencesToMonitorAndNextCheckDate(messageStore);
Tuple<List<Geofence>, Tuple<Date, Date>> geofencesAndNextRefreshDate = Geofencing.calculateGeofencesToMonitorAndNextCheckDates(geoStore);

// Then
assertNotNull(geofencesAndNextRefreshDate);
assertTrue(geofencesAndNextRefreshDate.getLeft().isEmpty());
assertNull(geofencesAndNextRefreshDate.getRight());
assertNull(geofencesAndNextRefreshDate.getRight().getLeft());
assertEquals(now, geofencesAndNextRefreshDate.getRight().getRight().getTime(), 3000);
}

public void test_shouldNotCalculateRefreshDateIfGeoIsMonitoredNow() throws Exception {
public void test_shouldNotCalculateRefreshDateForGeoStartIfGeoIsMonitoredNow() throws Exception {
// Given
Long millis15MinBeforeNow = now - 15 * 60 * 1000;
Long millis15MinAfterNow = now + 15 * 60 * 1000;
String date15MinBeforeNow = DateTimeUtil.ISO8601DateToString(new Date(millis15MinBeforeNow));
String date15MinAfterNow = DateTimeUtil.ISO8601DateToString(new Date(millis15MinAfterNow));

saveGeoMessageToDb(date15MinBeforeNow, date15MinAfterNow);

// When
Tuple<List<Geofence>, Tuple<Date, Date>> geofencesAndNextRefreshDate = Geofencing.calculateGeofencesToMonitorAndNextCheckDates(geoStore);

// Then
assertNotNull(geofencesAndNextRefreshDate);
assertFalse(geofencesAndNextRefreshDate.getLeft().isEmpty());
assertNull(geofencesAndNextRefreshDate.getRight().getLeft());
}

public void test_shouldCalculateRefreshDateForGeoExpiredIfGeoIsMonitoredNow() throws Exception {
// Given
Long millis15MinBeforeNow = now - 15 * 60 * 1000;
Long millis15MinAfterNow = now + 15 * 60 * 1000;
String date15MinBeforeNow = DateTimeUtil.ISO8601DateToString(new Date(millis15MinBeforeNow));
String date15MinAfterNow = DateTimeUtil.ISO8601DateToString(new Date(millis15MinAfterNow));

saveGeoMessageToDb(date15MinBeforeNow, date15MinAfterNow);

// When
Tuple<List<Geofence>, Tuple<Date, Date>> geofencesAndNextRefreshDate = Geofencing.calculateGeofencesToMonitorAndNextCheckDates(geoStore);

// Then
assertNotNull(geofencesAndNextRefreshDate);
assertFalse(geofencesAndNextRefreshDate.getLeft().isEmpty());
assertNull(geofencesAndNextRefreshDate.getRight().getLeft());
assertEquals(millis15MinAfterNow, geofencesAndNextRefreshDate.getRight().getRight().getTime(), 3000);
}

private void saveGeoMessageToDb(String startTimeMillis, String expiryTimeMillis) throws JSONException {
Geo geo = new Geo(0.0, 0.0, new ArrayList<Area>() {{
add(new Area("SomeAreaId", "SomeAreaTitle", 0.0, 0.0, 10));
}}, null, new ArrayList<GeoEvent>(), date15MinBeforeNow, date15MinAfterNow, "SomeCampaignId");
}}, null, new ArrayList<GeoEvent>(), expiryTimeMillis, startTimeMillis, "SomeCampaignId");

JSONObject internalData = new JSONObject(new JsonSerializer().serialize(geo));
Message message = new Message(
Expand All @@ -161,14 +170,7 @@ public void test_shouldNotCalculateRefreshDateIfGeoIsMonitoredNow() throws Excep
Message.Status.UNKNOWN,
"SomeStatusMessage"
);
messageStore.save(context, message);

// When
Tuple<List<Geofence>, Date> geofencesAndNextRefreshDate = Geofencing.calculateGeofencesToMonitorAndNextCheckDate(messageStore);

// Then
assertNotNull(geofencesAndNextRefreshDate);
assertTrue(geofencesAndNextRefreshDate.getLeft().isEmpty());
assertNull(geofencesAndNextRefreshDate.getRight());
geoStore.save(context, message);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.test.InstrumentationTestCase;

import org.infobip.mobile.messaging.Message;
Expand All @@ -12,10 +13,13 @@
import org.infobip.mobile.messaging.gcm.MobileMessageHandler;
import org.infobip.mobile.messaging.storage.MessageStore;
import org.infobip.mobile.messaging.storage.SQLiteMessageStore;
import org.infobip.mobile.messaging.util.DateTimeUtil;
import org.infobip.mobile.messaging.util.PreferenceHelper;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
Expand All @@ -29,19 +33,25 @@ public class GeoStorageTest extends InstrumentationTestCase {
private MessageStore geoStore;
private MessageStore commonStore;
private MobileMessageHandler handler;
private long now;

@Override
protected void setUp() throws Exception {
super.setUp();

PreferenceHelper.saveString(getInstrumentation().getContext(), MobileMessagingProperty.MESSAGE_STORE_CLASS, SQLiteMessageStore.class.getName());

context = getInstrumentation().getContext();

PreferenceManager.getDefaultSharedPreferences(context).edit().clear().commit();
PreferenceHelper.saveString(context, MobileMessagingProperty.MESSAGE_STORE_CLASS, SQLiteMessageStore.class.getName());
PreferenceHelper.saveBoolean(context, MobileMessagingProperty.PUSH_REGISTRATION_ENABLED, true);
PreferenceHelper.saveBoolean(context, MobileMessagingProperty.GEOFENCING_ACTIVATED, true);

handler = new MobileMessageHandler();
geoStore = MobileMessagingCore.getInstance(context).getMessageStoreForGeo();
geoStore.deleteAll(context);
commonStore = MobileMessagingCore.getInstance(context).getMessageStore();
commonStore.deleteAll(context);
now = System.currentTimeMillis();
}

public void test_shouldSaveGeoMessagesToGeoStore() throws Exception {
Expand Down Expand Up @@ -114,4 +124,59 @@ public void test_shouldSaveMessagesToCorrespondingSeparateStores() throws Except
assertEquals("SomeCampaignId2", messages.get(0).getGeo().getCampaignId());
assertEquals("SomeAreaId1", messages.get(0).getGeo().getAreasList().get(0).getId());
}

public void test_shouldDeleteExpiredAreas() throws Exception {
// Given
long now = System.currentTimeMillis();
Long millis30MinBeforeNow = now - 30 * 60 * 1000;
Long millis15MinBeforeNow = now - 15 * 60 * 1000;
Long millis15MinAfterNow = now + 15 * 60 * 1000;
String date30MinBeforeNow = DateTimeUtil.ISO8601DateToString(new Date(millis30MinBeforeNow));
String date15MinBeforeNow = DateTimeUtil.ISO8601DateToString(new Date(millis15MinBeforeNow));
String date15MinAfterNow = DateTimeUtil.ISO8601DateToString(new Date(millis15MinAfterNow));
String nonExpiredMessageId = "SomeMessageId5";

saveGeoMessageToDb("SomeMessageId1", null, date30MinBeforeNow);
saveGeoMessageToDb("SomeMessageId2", null, date30MinBeforeNow);
saveGeoMessageToDb("SomeMessageId3", null, date15MinBeforeNow);
saveGeoMessageToDb("SomeMessageId4", null, date15MinBeforeNow);
saveGeoMessageToDb(nonExpiredMessageId, null, date15MinAfterNow);

assertEquals(5, geoStore.countAll(context));

// When
MobileMessagingCore.getInstance(context).removeExpiredAreas();

// Then
assertEquals(1, geoStore.countAll(context));
assertEquals(nonExpiredMessageId, geoStore.findAll(context).get(0).getMessageId());
}

private void saveGeoMessageToDb(String messageId, String startTimeMillis, String expiryTimeMillis) throws JSONException {
Geo geo = new Geo(0.0, 0.0, new ArrayList<Area>() {{
add(new Area("SomeAreaId", "SomeAreaTitle", 0.0, 0.0, 10));
}}, null, new ArrayList<GeoEvent>(), expiryTimeMillis, startTimeMillis, "SomeCampaignId");

JSONObject internalData = new JSONObject(new JsonSerializer().serialize(geo));
Message message = new Message(
messageId,
"SomeTitle",
"SomeBody",
"SomeSound",
true,
"SomeIcon",
true,
"SomeCategory",
"SomeFrom",
now,
0,
internalData,
null,
geo,
"SomeDestination",
Message.Status.UNKNOWN,
"SomeStatusMessage"
);
geoStore.save(context, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_DATA_CLEARED" />
<data android:scheme="package" />
</intent-filter>
</receiver>

<receiver android:name="org.infobip.mobile.messaging.notification.NotificationTapReceiver"/>
<receiver android:name="org.infobip.mobile.messaging.notification.NotificationTapReceiver" />

</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public class BootReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MobileMessagingLogger.i("Received boot completed intent");
MobileMessagingCore.handleBootCompleted(context);
MobileMessagingCore.getInstance(context).handleBootCompleted();
}
}
Loading

0 comments on commit 40ed906

Please sign in to comment.