public boolean isCriteriaMet(FieldManager fieldManager, boolean verbose) { try { if (!isNull(KEY_CRITERIA)) { JSONObject criteriaObject = getJSONObject(KEY_CRITERIA); InteractionCriteria criteria = new InteractionCriteria(criteriaObject.toString()); return criteria.isMet(fieldManager, verbose); } } catch (JSONException e) { logException(e); } return false; } }
@Test public void criteriaApplicationDebug() throws JSONException { AppRelease appRelease = AppReleaseManager.generateCurrentAppRelease(targetContext, null); String json = loadTextAssetAsString(TEST_DATA_DIR + "criteria/testCriteriaApplicationDebug.json"); json = json.replace("APPLICATION_DEBUG", Boolean.toString(appRelease.isDebug())); InteractionCriteria criteria = new InteractionCriteria(json); FieldManager fieldManager = new FieldManager(targetContext, new VersionHistory(), new EventData(), new Person(), new Device(), appRelease); assertTrue(criteria.isMet(fieldManager)); }
@Test public void criteriaApplicationVersionCode() throws JSONException { AppRelease appRelease = AppReleaseManager.generateCurrentAppRelease(targetContext, null); String json = loadTextAssetAsString(TEST_DATA_DIR + "criteria/testCriteriaApplicationVersionCode.json"); json = json.replace("\"APPLICATION_VERSION_CODE\"", String.valueOf(appRelease.getVersionCode())); InteractionCriteria criteria = new InteractionCriteria(json); FieldManager fieldManager = new FieldManager(targetContext, new VersionHistory(), new EventData(), new Person(), new Device(), appRelease); assertTrue(criteria.isMet(fieldManager)); }
@Test public void criteriaApplicationVersionName() throws JSONException { AppRelease appRelease = AppReleaseManager.generateCurrentAppRelease(targetContext, null); String json = loadTextAssetAsString(TEST_DATA_DIR + "criteria/testCriteriaApplicationVersionName.json"); json = json.replace("APPLICATION_VERSION_NAME", appRelease.getVersionName()); InteractionCriteria criteria = new InteractionCriteria(json); FieldManager fieldManager = new FieldManager(targetContext, new VersionHistory(), new EventData(), new Person(), new Device(), appRelease); assertTrue(criteria.isMet(fieldManager)); }
@Test public void defaultValues() throws JSONException { String json = loadTextAssetAsString(TEST_DATA_DIR + "testDefaultValues.json"); InteractionCriteria criteria = new InteractionCriteria(json); FieldManager fieldManager = new FieldManager(targetContext, new VersionHistory(), new EventData(), new Person(), new Device(), new AppRelease()); assertNotNull("Criteria was null, but it shouldn't be.", criteria); assertTrue(criteria.isMet(fieldManager)); } }
@Test public void savingCodePointAndCheckingForApplicableInteraction() throws JSONException { if (isRunningOnEmulator()) { ApptentiveLog.e("Running on emulator. Skipping test."); return; } final int iterations = 100; String json = loadTextAssetAsString(TEST_DATA_DIR + "payloads/testListOfVariousInteractions.json"); InteractionCriteria criteria = new InteractionCriteria(json); EventData eventData = new EventData(); FieldManager fieldManager = new FieldManager(targetContext, new VersionHistory(), eventData, new Person(), new Device(), new AppRelease()); eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "app.launch"); eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "app.launch"); eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "big.win"); eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "big.win"); long start = System.currentTimeMillis(); for (int i = 0; i < iterations; i++) { assertTrue(criteria.isMet(fieldManager)); } long end = System.currentTimeMillis(); long duration = end - start; double average = (double) duration / iterations; double limit = 20d; String message = String.format("Finished %d iterations in %,dms, average of %.2fms per run, limit was %.2fms", iterations, duration, average, limit); ApptentiveLog.e(message); assertTrue(message, average < limit); }
@Test public void cornerCasesThatShouldBeTrue() throws JSONException { String json = loadTextAssetAsString(TEST_DATA_DIR + "testCornerCasesThatShouldBeTrue.json"); InteractionCriteria criteria = new InteractionCriteria(json); EventData eventData = new EventData(); Device device = new Device(); device.getCustomData().put("key_with_null_value", null); FieldManager fieldManager = new FieldManager(targetContext, new VersionHistory(), eventData, new Person(), device, new AppRelease()); assertNotNull("Criteria was null, but it shouldn't be.", criteria); assertTrue(criteria.isMet(fieldManager)); ApptentiveLog.e("Finished test."); }
@Test public void criteriaProcessingPerformance() throws JSONException { if (isRunningOnEmulator()) { ApptentiveLog.e("Running on emulator. Skipping test."); return; } final int iterations = 100; String json = loadTextAssetAsString(TEST_DATA_DIR + "payloads/testListOfVariousInteractions.json"); InteractionCriteria criteria = new InteractionCriteria(json); VersionHistory versionHistory = new VersionHistory(); EventData eventData = new EventData(); FieldManager fieldManager = new FieldManager(targetContext, versionHistory, eventData, new Person(), new Device(), new AppRelease()); versionHistory.updateVersionHistory(Util.currentTimeSeconds(), versionCode, versionName); eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "app.launch"); eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "app.launch"); eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "big.win"); eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "big.win"); long start = System.currentTimeMillis(); for (int i = 0; i < iterations; i++) { assertTrue(criteria.isMet(fieldManager)); } long end = System.currentTimeMillis(); long duration = end - start; double average = (double) duration / iterations; double limit = 7d; String message = String.format("Finished %d iterations in %,dms, average of %.2fms per run, limit was %.2fms", iterations, duration, average, limit); ApptentiveLog.e(message); assertTrue(message, average < limit); }
@Test public void cornerCasesThatShouldBeFalse() throws JSONException { ApptentiveLog.e("Running test: testCornerCasesThatShouldBeFalse()\n\n"); String json = loadTextAssetAsString(TEST_DATA_DIR + "testCornerCasesThatShouldBeFalse.json"); InteractionCriteria criteria = new InteractionCriteria(json); EventData eventData = new EventData(); Device device = new Device(); device.getCustomData().put("key_with_null_value", null); FieldManager fieldManager = new FieldManager(targetContext, new VersionHistory(), eventData, new Person(), device, new AppRelease()); assertNotNull("Criteria was null, but it shouldn't be.", criteria); assertTrue(criteria.isMet(fieldManager)); ApptentiveLog.e("Finished test."); } }
private void doTest(String testFile) throws JSONException { String json = loadTextAssetAsString(TEST_DATA_DIR + testFile); InteractionCriteria criteria = new InteractionCriteria(json); Device device = new Device(); FieldManager fieldManager = new FieldManager(targetContext, new VersionHistory(), new EventData(), new Person(), device, new AppRelease()); device.getCustomData().put(" string_qwerty ", "qwerty"); device.getCustomData().put(" string with spaces ", "string with spaces"); /* TODO: It looks like we weren't trimming custom_data keys before storing them. What is the implication of doing that? We can't store a key under " key " and then retrieve it with "key" */ assertTrue(criteria.isMet(fieldManager)); } }
@Test public void queriesAgainstDevice() throws JSONException { String json = loadTextAssetAsString(TEST_DATA_DIR + "testQueriesAgainstDevice.json"); json = json.replace("\"OS_API_LEVEL\"", String.valueOf(Build.VERSION.SDK_INT)); InteractionCriteria criteria = new InteractionCriteria(json); Device device = DeviceManager.generateNewDevice(targetContext); device.getCustomData().put("foo", "bar"); EventData eventData = new EventData(); FieldManager fieldManager = new FieldManager(targetContext, new VersionHistory(), eventData, new Person(), device, new AppRelease()); // 0 assertTrue(criteria.isMet(fieldManager)); // 1 eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertTrue(criteria.isMet(fieldManager)); // 2 eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertFalse(criteria.isMet(fieldManager)); // 3 eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertTrue(criteria.isMet(fieldManager)); // 4 eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertTrue(criteria.isMet(fieldManager)); }
@Test public void queriesAgainstSdk() throws JSONException { String json = loadTextAssetAsString(TEST_DATA_DIR + "testQueriesAgainstSdk.json"); InteractionCriteria criteria = new InteractionCriteria(json); Device device = new Device(); device.getCustomData().put("foo", "bar"); EventData eventData = new EventData(); FieldManager fieldManager = new FieldManager(targetContext, new VersionHistory(), eventData, new Person(), device, new AppRelease()); // 0 assertTrue(criteria.isMet(fieldManager)); // 1 eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertTrue(criteria.isMet(fieldManager)); // 2 eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertFalse(criteria.isMet(fieldManager)); } }
@Test public void upgradeMessageOnVersionCode() throws JSONException { AppRelease appRelease = AppReleaseManager.generateCurrentAppRelease(targetContext, null); String json = loadTextAssetAsString(TEST_DATA_DIR + "payloads/upgradeMessageOnVersionCode.json") .replace("\"APPLICATION_VERSION_CODE\"", String.valueOf(appRelease.getVersionCode())); InteractionCriteria criteria = new InteractionCriteria(json); VersionHistory versionHistory = new VersionHistory(); FieldManager fieldManager = new FieldManager(targetContext, versionHistory, new EventData(), new Person(), new Device(), appRelease); versionHistory.updateVersionHistory(Util.currentTimeSeconds(), versionCode, versionName); // Test version targeted UpgradeMessage // Saw this build too long ago. ApptentiveLog.e("ONE"); versionHistory.clear(); versionHistory.updateVersionHistory(Util.currentTimeSeconds() - 1000000, 1, "1.0"); versionHistory.updateVersionHistory(Util.currentTimeSeconds() - 600000, versionCode, "2.0.0"); ApptentiveLog.e("Current time: %f", Util.currentTimeSeconds()); assertFalse(criteria.isMet(fieldManager)); // Haven't upgraded ApptentiveLog.e("TWO"); versionHistory.clear(); versionHistory.updateVersionHistory(Util.currentTimeSeconds() - 499500, versionCode, "2.0.0"); assertFalse(criteria.isMet(fieldManager)); // Just right ApptentiveLog.e("THREE"); versionHistory.clear(); versionHistory.updateVersionHistory(Util.currentTimeSeconds() - 1000000, 1, "1.0"); versionHistory.updateVersionHistory(Util.currentTimeSeconds() - 499500, versionCode, "2.0.0"); assertTrue(criteria.isMet(fieldManager)); }
private void doTest(String testFile) throws JSONException { String json = loadTextAssetAsString(TEST_DATA_DIR + testFile); InteractionCriteria criteria = new InteractionCriteria(json); Device device = new Device(); FieldManager fieldManager = new FieldManager(targetContext, new VersionHistory(), new EventData(), new Person(), device, new AppRelease()); Apptentive.DateTime dateTime = new Apptentive.DateTime(1000d); Apptentive.Version version = new Apptentive.Version(); version.setVersion("1.2.3"); device.getCustomData().put("number_5", 5); device.getCustomData().put("string_qwerty", "qwerty"); device.getCustomData().put("boolean_true", true); device.getCustomData().put("key_with_null_value", (String) null); device.getCustomData().put("datetime_1000", dateTime); device.getCustomData().put("version_1.2.3", version); assertTrue(criteria.isMet(fieldManager)); } }
@Test public void upgradeMessageOnVersionName() throws JSONException { AppRelease appRelease = AppReleaseManager.generateCurrentAppRelease(targetContext, null); String json = loadTextAssetAsString(TEST_DATA_DIR + "payloads/upgradeMessageOnVersionName.json") .replace("APPLICATION_VERSION_NAME", appRelease.getVersionName()); InteractionCriteria criteria = new InteractionCriteria(json); VersionHistory versionHistory = new VersionHistory(); EventData eventData = new EventData(); FieldManager fieldManager = new FieldManager(targetContext, versionHistory, eventData, new Person(), new Device(), appRelease); versionHistory.updateVersionHistory(Util.currentTimeSeconds(), versionCode, versionName); eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "app.launch"); // Test version targeted UpgradeMessage // Saw this build too long ago. ApptentiveLog.e("ONE"); versionHistory.clear(); versionHistory.updateVersionHistory(Util.currentTimeSeconds() - 1000000, 3, "1.0"); versionHistory.updateVersionHistory(Util.currentTimeSeconds() - 600000, 4, "2.0.0"); assertFalse(criteria.isMet(fieldManager)); // Haven't upgraded ApptentiveLog.e("TWO"); versionHistory.clear(); versionHistory.updateVersionHistory(Util.currentTimeSeconds() - 499500, 4, "2.0.0"); assertFalse(criteria.isMet(fieldManager)); // Just right ApptentiveLog.e("THREE"); versionHistory.clear(); versionHistory.updateVersionHistory(Util.currentTimeSeconds() - 1000000, 3, "1.0"); versionHistory.updateVersionHistory(Util.currentTimeSeconds() - 499500, 4, "2.0.0"); assertTrue(criteria.isMet(fieldManager)); }
@Test public void queriesAgainstPerson() throws JSONException { String json = loadTextAssetAsString(TEST_DATA_DIR + "testQueriesAgainstPerson.json"); InteractionCriteria criteria = new InteractionCriteria(json); Person person = new Person(); person.setEmail("example@example.com"); person.getCustomData().put("foo", "bar"); EventData eventData = new EventData(); FieldManager fieldManager = new FieldManager(targetContext, new VersionHistory(), eventData, person, new Device(), new AppRelease()); // 0 assertTrue(criteria.isMet(fieldManager)); // 1 eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertFalse(criteria.isMet(fieldManager)); // 2 eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertTrue(criteria.isMet(fieldManager)); // 3 person.getCustomData().put("foo", "bar"); eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertTrue(criteria.isMet(fieldManager)); // 4 eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertFalse(criteria.isMet(fieldManager)); // 5 eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertTrue(criteria.isMet(fieldManager)); }
@Test public void criteriaTimeAtInstall() throws JSONException { String json = loadTextAssetAsString(TEST_DATA_DIR + "payloads/testCriteriaTimeAtInstall.json"); InteractionCriteria criteria = new InteractionCriteria(json);
String json = loadTextAssetAsString(TEST_DIR + "testCodePointLastInvokedAt.json"); try { InteractionCriteria criteria = new InteractionCriteria(json);
String json = loadTextAssetAsString(TEST_DIR + "testInteractionInvokesTotal.json"); InteractionCriteria criteria = new InteractionCriteria(json);
String json = loadTextAssetAsString(TEST_DIR + "testCodePointInvokesVersionCode.json"); try { InteractionCriteria criteria = new InteractionCriteria(json);