|
| 1 | +package ly.count.android.sdk; |
| 2 | + |
| 3 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | +import org.json.JSONException; |
| 7 | +import org.json.JSONObject; |
| 8 | +import org.junit.After; |
| 9 | +import org.junit.Assert; |
| 10 | +import org.junit.Before; |
| 11 | +import org.junit.Test; |
| 12 | +import org.junit.runner.RunWith; |
| 13 | + |
| 14 | +@RunWith(AndroidJUnit4.class) |
| 15 | +public class ModuleContentTests { |
| 16 | + |
| 17 | + Countly mCountly; |
| 18 | + List<String> capturedRequests; |
| 19 | + List<String> capturedEndpoints; |
| 20 | + |
| 21 | + @Before |
| 22 | + public void setUp() { |
| 23 | + TestUtils.getCountlyStore().clear(); |
| 24 | + capturedRequests = new ArrayList<>(); |
| 25 | + capturedEndpoints = new ArrayList<>(); |
| 26 | + } |
| 27 | + |
| 28 | + @After |
| 29 | + public void tearDown() { |
| 30 | + } |
| 31 | + |
| 32 | + private ImmediateRequestGenerator createCapturingIRGenerator() { |
| 33 | + return new ImmediateRequestGenerator() { |
| 34 | + @Override public ImmediateRequestI CreateImmediateRequestMaker() { |
| 35 | + return (requestData, customEndpoint, cp, requestShouldBeDelayed, networkingIsEnabled, callback, log) -> { |
| 36 | + capturedRequests.add(requestData); |
| 37 | + capturedEndpoints.add(customEndpoint); |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + @Override public ImmediateRequestI CreatePreflightRequestMaker() { |
| 42 | + return (requestData, customEndpoint, cp, requestShouldBeDelayed, networkingIsEnabled, callback, log) -> { |
| 43 | + }; |
| 44 | + } |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + private Countly initWithConsent(boolean contentConsent) { |
| 49 | + CountlyConfig config = TestUtils.createBaseConfig(); |
| 50 | + config.setRequiresConsent(true); |
| 51 | + if (contentConsent) { |
| 52 | + config.setConsentEnabled(new String[] { Countly.CountlyFeatureNames.content }); |
| 53 | + } |
| 54 | + config.disableHealthCheck(); |
| 55 | + config.immediateRequestGenerator = createCapturingIRGenerator(); |
| 56 | + |
| 57 | + mCountly = new Countly(); |
| 58 | + mCountly.init(config); |
| 59 | + mCountly.moduleContent.countlyTimer = null; |
| 60 | + capturedRequests.clear(); |
| 61 | + capturedEndpoints.clear(); |
| 62 | + return mCountly; |
| 63 | + } |
| 64 | + |
| 65 | + private void setIsCurrentlyInContentZone(ModuleContent module, boolean value) throws Exception { |
| 66 | + java.lang.reflect.Field field = ModuleContent.class.getDeclaredField("isCurrentlyInContentZone"); |
| 67 | + field.setAccessible(true); |
| 68 | + field.set(module, value); |
| 69 | + } |
| 70 | + |
| 71 | + // ======== previewContent public API tests ======== |
| 72 | + |
| 73 | + /** |
| 74 | + * Null and empty contentId should be rejected at the public API level. |
| 75 | + * No request should be made. |
| 76 | + */ |
| 77 | + @Test |
| 78 | + public void previewContent_invalidContentId() { |
| 79 | + Countly countly = initWithConsent(true); |
| 80 | + |
| 81 | + countly.contents().previewContent(null); |
| 82 | + Assert.assertEquals(0, capturedRequests.size()); |
| 83 | + |
| 84 | + countly.contents().previewContent(""); |
| 85 | + Assert.assertEquals(0, capturedRequests.size()); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Valid contentId with consent should make a request to /o/sdk/content |
| 90 | + * containing content_id and preview=true parameters |
| 91 | + */ |
| 92 | + @Test |
| 93 | + public void previewContent_validContentId() { |
| 94 | + Countly countly = initWithConsent(true); |
| 95 | + |
| 96 | + countly.contents().previewContent("test_content_123"); |
| 97 | + |
| 98 | + Assert.assertEquals(1, capturedRequests.size()); |
| 99 | + Assert.assertEquals("/o/sdk/content", capturedEndpoints.get(0)); |
| 100 | + |
| 101 | + String request = capturedRequests.get(0); |
| 102 | + Assert.assertTrue(request.contains("content_id=test_content_123")); |
| 103 | + Assert.assertTrue(request.contains("preview=true")); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Without content consent, no request should be made |
| 108 | + */ |
| 109 | + @Test |
| 110 | + public void previewContent_noConsent() { |
| 111 | + Countly countly = initWithConsent(false); |
| 112 | + |
| 113 | + countly.contents().previewContent("test_content_id"); |
| 114 | + |
| 115 | + Assert.assertEquals(0, capturedRequests.size()); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * When content is already being displayed, no new request should be made |
| 120 | + */ |
| 121 | + @Test |
| 122 | + public void previewContent_alreadyInContentZone() throws Exception { |
| 123 | + Countly countly = initWithConsent(true); |
| 124 | + setIsCurrentlyInContentZone(countly.moduleContent, true); |
| 125 | + |
| 126 | + countly.contents().previewContent("test_content_id"); |
| 127 | + |
| 128 | + Assert.assertEquals(0, capturedRequests.size()); |
| 129 | + } |
| 130 | + |
| 131 | + // ======== validateResponse tests ======== |
| 132 | + |
| 133 | + /** |
| 134 | + * validateResponse returns true only when both "geo" and "html" are present, |
| 135 | + * false for missing geo, missing html, or empty response |
| 136 | + */ |
| 137 | + @Test |
| 138 | + public void validateResponse() throws JSONException { |
| 139 | + Countly countly = initWithConsent(true); |
| 140 | + ModuleContent mc = countly.moduleContent; |
| 141 | + |
| 142 | + // empty |
| 143 | + Assert.assertFalse(mc.validateResponse(new JSONObject())); |
| 144 | + |
| 145 | + // missing geo |
| 146 | + JSONObject noGeo = new JSONObject(); |
| 147 | + noGeo.put("html", "<html></html>"); |
| 148 | + Assert.assertFalse(mc.validateResponse(noGeo)); |
| 149 | + |
| 150 | + // missing html |
| 151 | + JSONObject noHtml = new JSONObject(); |
| 152 | + noHtml.put("geo", new JSONObject()); |
| 153 | + Assert.assertFalse(mc.validateResponse(noHtml)); |
| 154 | + |
| 155 | + // valid |
| 156 | + JSONObject valid = new JSONObject(); |
| 157 | + valid.put("geo", new JSONObject()); |
| 158 | + valid.put("html", "<html></html>"); |
| 159 | + Assert.assertTrue(mc.validateResponse(valid)); |
| 160 | + } |
| 161 | +} |
0 commit comments