Skip to content

Commit

Permalink
Merge pull request #376 from Countly/view_start_stop
Browse files Browse the repository at this point in the history
[Android] move away from resuming
  • Loading branch information
turtledreams authored Aug 22, 2024
2 parents 9669272 + 23264b0 commit 387e490
Show file tree
Hide file tree
Showing 8 changed files with 323 additions and 151 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## XX.XX.XX
* The views will be stopped and restarted now while going to the background or foreground instead of resuming and pausing.

## 24.7.2
* Mitigated an issue in the upload plugin that prevented the upload of a symbol file

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public void sessionDurationScenario_1() throws InterruptedException, JSONExcepti
assertEquals(6, TestUtils.getCurrentRQ().length); // not 5 anymore, it will send orientation event as well

TestUtils.validateRequest("ff_merge", TestUtils.map("old_device_id", "1234"), 1);
ModuleEventsTests.validateEventInRQ("ff_merge", "[CLY]_orientation", 1, 0.0d, 0.0d, 2, 0, 1, -1);
ModuleEventsTests.validateEventInRQ("ff_merge", "[CLY]_orientation", null, 1, 0.0d, 0.0d, "_CLY_", "_CLY_", "_CLY_", "_CLY_", 2, -1, 0, 1);
TestUtils.validateRequest("ff_merge", TestUtils.map("user_details", "{\"custom\":{\"prop2\":123,\"prop1\":\"string\",\"prop3\":false}}"), 3);
ModuleSessionsTests.validateSessionEndRequest(4, 3, "ff_merge");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import ly.count.android.sdk.messaging.ModulePush;
import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -773,42 +775,98 @@ public void recordEvent_unsupportedDataTypesSegmentation() throws JSONException
validateEventInRQ("key", TestUtils.map(), 1, 1.0d, 1.0d, 0);
}

protected static JSONObject validateEventInRQ(String deviceId, String eventName, int count, double sum, double duration, int idx, int eventIdx, int eventCount, int rqCount) throws JSONException {
protected static void validateEventInRQ(String eventName, Map<String, Object> expectedSegmentation, int count, double sum, double duration, int idx) throws JSONException {
validateEventInRQ(eventName, expectedSegmentation, count, sum, duration, idx, idx + 1);
}

protected static void validateEventInRQ(String eventName, Map<String, Object> expectedSegmentation, int count, double sum, double duration, int idx, int rqCount) throws JSONException {
validateEventInRQ(TestUtils.commonDeviceId, eventName, expectedSegmentation, count, sum, duration, "_CLY_", "_CLY_", "_CLY_", "_CLY_", idx, rqCount, 0, 1);
}

protected static void validateEventInRQ(String deviceId, String eventName, Map<String, Object> expectedSegmentation, int count, Double sum, Double duration, String id, String pvid, String cvid, String peid, int idx, int rqCount, int eventIdx, int eventCount) throws JSONException {
Map<String, String>[] RQ = TestUtils.getCurrentRQ();
if (rqCount > -1) {
Assert.assertEquals(rqCount, RQ.length);
}
TestUtils.validateRequiredParams(RQ[idx], deviceId);
if (!RQ[idx].containsKey("events")) {
Assert.fail("Not an event request idx:[" + idx + "], request:[" + RQ[idx] + "]");
}
JSONArray events = new JSONArray(RQ[idx].get("events"));
Assert.assertEquals(eventCount, events.length());
JSONObject event = events.getJSONObject(eventIdx);
Assert.assertEquals(eventName, event.get("key"));
Assert.assertEquals(count, event.getInt("count"));
Assert.assertEquals(sum, event.optDouble("sum", 0.0d), 0.0001);
Assert.assertEquals(duration, event.optDouble("dur", 0.0d), 0.0001);
return event;
}

protected static void validateEventInRQ(String eventName, Map<String, Object> expectedSegmentation, int count, double sum, double duration, int idx) throws JSONException {
JSONObject event = validateEventInRQ(TestUtils.commonDeviceId, eventName, count, sum, duration, idx, 0, 1, idx + 1);
if (!expectedSegmentation.isEmpty()) {
if (expectedSegmentation != null && !expectedSegmentation.isEmpty()) {
JSONObject segmentation = event.getJSONObject("segmentation");
Assert.assertEquals(expectedSegmentation.size(), segmentation.length());
Assert.assertEquals("Expected segmentation: " + expectedSegmentation + ", got: " + segmentation, expectedSegmentation.size(), segmentation.length());
for (Map.Entry<String, Object> entry : expectedSegmentation.entrySet()) {
Assert.assertEquals(entry.getValue(), segmentation.get(entry.getKey()));
}
}
Assert.assertEquals(count, event.getInt("count"));
Assert.assertEquals(sum, event.optDouble("sum", 0.0d), 0.0001);
Assert.assertEquals(duration, event.optDouble("dur", 0.0d), 0.0001);

int dow = event.getInt("dow");
int hour = event.getInt("hour");
long timestamp = event.getLong("timestamp");
Assert.assertTrue(dow >= 0 && dow < 7);
Assert.assertTrue(hour >= 0 && hour < 24);
Assert.assertTrue(timestamp >= 0);

validateId(id, event.optString("id", ""), "Event ID");
validateId(pvid, event.optString("pvid", ""), "Previous View ID");
validateId(cvid, event.optString("cvid", ""), "Current View ID");
validateId(peid, event.optString("peid", ""), "Previous Event ID");
}

private static void validateId(String id, String gonnaValidate, String name) {
if (id != null && id.equals("_CLY_")) {
if (gonnaValidate != null && !gonnaValidate.isEmpty()) {
validateSafeRandomVal(gonnaValidate);
}
} else {
Assert.assertEquals(name + " is not validated", id, gonnaValidate);
}
}

/**
* Validates a random generated safe value,
* Value length should be 21
* Value should contain a timestamp at the end
* Value should be base64 encoded and first 8 should be it
*
* @param val
*/
static void validateSafeRandomVal(String val) {
Assert.assertEquals(val, 21, val.length());

Pattern base64Pattern = Pattern.compile("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$");

String timestampStr = val.substring(val.length() - 13);
String base64Str = val.substring(0, val.length() - 13);

Matcher matcher = base64Pattern.matcher(base64Str);
if (matcher.matches()) {
UtilsTime.Instant instant = UtilsTime.Instant.get(Long.parseLong(timestampStr));
Assert.assertTrue(instant.dow >= 0 && instant.dow < 7);
Assert.assertTrue(instant.hour >= 0 && instant.hour < 24);
Assert.assertTrue(instant.timestampMs > 0);
} else {
Assert.fail("No match for " + val);
}
}

protected static void validateEventInRQ(String eventName, Map<String, Object> expectedSegmentation, int idx) throws JSONException {
validateEventInRQ(eventName, expectedSegmentation, 1, 0.0d, 0.0d, idx);
}

protected static void validateEventInRQ(String eventName, int rqIdx, int eventIdx, int eventCount) throws JSONException {
validateEventInRQ(TestUtils.commonDeviceId, eventName, 1, 0.0d, 0.0d, rqIdx, eventIdx, eventCount, -1);
validateEventInRQ(TestUtils.commonDeviceId, eventName, null, 1, 0.0d, 0.0d, "_CLY_", "_CLY_", "_CLY_", "_CLY_", rqIdx, -1, eventIdx, eventCount);
}

protected static void validateEventInRQ(String deviceId, String eventName, int rqIdx, int eventIdx, int eventCount) throws JSONException {
validateEventInRQ(deviceId, eventName, null, 1, 0.0d, 0.0d, "_CLY_", "_CLY_", "_CLY_", "_CLY_", rqIdx, -1, eventIdx, eventCount);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,8 +598,7 @@ public void internalLimit_setProperties_maxValueSizePicture() throws JSONExcepti
Countly.sharedInstance().userProfile().setProperties(TestUtils.map(ModuleUserProfile.PICTURE_KEY, picture));
Countly.sharedInstance().userProfile().save();

validateUserProfileRequest(TestUtils.map(ModuleUserProfile.PICTURE_KEY, picture.substring(0, 4096)), TestUtils.map()
);
validateUserProfileRequest(TestUtils.map(ModuleUserProfile.PICTURE_KEY, picture.substring(0, 4096)), TestUtils.map());
}

/**
Expand Down
Loading

0 comments on commit 387e490

Please sign in to comment.