|
4 | 4 | import android.content.res.Configuration; |
5 | 5 | import androidx.annotation.NonNull; |
6 | 6 | import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 7 | +import java.util.Arrays; |
7 | 8 | import java.util.HashMap; |
| 9 | +import java.util.List; |
8 | 10 | import java.util.Map; |
9 | 11 | import java.util.concurrent.ConcurrentHashMap; |
| 12 | +import org.json.JSONArray; |
10 | 13 | import org.json.JSONException; |
11 | 14 | import org.junit.After; |
12 | 15 | import org.junit.Assert; |
13 | 16 | import org.junit.Before; |
14 | 17 | import org.junit.Test; |
15 | 18 | import org.junit.runner.RunWith; |
| 19 | +import org.mockito.Mockito; |
| 20 | +import org.mockito.internal.util.collections.Sets; |
16 | 21 |
|
17 | 22 | import static org.mockito.ArgumentMatchers.any; |
18 | 23 | import static org.mockito.ArgumentMatchers.anyDouble; |
@@ -1537,5 +1542,204 @@ public void internalLimit_recordViewsWithSegmentation_maxValueSize() throws JSON |
1537 | 1542 | ModuleEventsTests.validateEventInRQ(ModuleViews.VIEW_EVENT_KEY, expectedSegm, 1); |
1538 | 1543 | } |
1539 | 1544 |
|
| 1545 | + /** |
| 1546 | + * "startView" with Array segmentations |
| 1547 | + * Validate that all primitive types arrays are successfully recorded |
| 1548 | + * And validate that Object arrays are not recorded |
| 1549 | + * But Generic type of Object array which its values are only primitive types are recorded |
| 1550 | + * |
| 1551 | + * @throws JSONException if the JSON is not valid |
| 1552 | + */ |
| 1553 | + @Test |
| 1554 | + public void startView_validateSupportedArrays() throws JSONException { |
| 1555 | + int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; |
| 1556 | + boolean[] arrB = { true, false, true, false, true, false, true, false, true, false }; |
| 1557 | + String[] arrS = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; |
| 1558 | + long[] arrL = { Long.MAX_VALUE, Long.MIN_VALUE }; |
| 1559 | + double[] arrD = { Double.MAX_VALUE, Double.MIN_VALUE }; |
| 1560 | + Long[] arrLO = { Long.MAX_VALUE, Long.MIN_VALUE }; |
| 1561 | + Double[] arrDO = { Double.MAX_VALUE, Double.MIN_VALUE }; |
| 1562 | + Boolean[] arrBO = { Boolean.TRUE, Boolean.FALSE }; |
| 1563 | + Integer[] arrIO = { Integer.MAX_VALUE, Integer.MIN_VALUE }; |
| 1564 | + Object[] arrObj = { "1", 1, 1.1d, true, 1.1f, Long.MAX_VALUE }; |
| 1565 | + Object[] arrObjStr = { "1", "1", "1.1d", "true", "1.1f", "Long.MAX_VALUE" }; |
| 1566 | + |
| 1567 | + CountlyConfig countlyConfig = TestUtils.createBaseConfig(); |
| 1568 | + countlyConfig.setEventQueueSizeToSend(1); |
| 1569 | + Countly countly = new Countly().init(countlyConfig); |
| 1570 | + |
| 1571 | + Map<String, Object> segmentation = TestUtils.map( |
| 1572 | + "arr", arr, |
| 1573 | + "arrB", arrB, |
| 1574 | + "arrS", arrS, |
| 1575 | + "arrL", arrL, |
| 1576 | + "arrD", arrD, |
| 1577 | + "arrLO", arrLO, |
| 1578 | + "arrDO", arrDO, |
| 1579 | + "arrBO", arrBO, |
| 1580 | + "arrIO", arrIO, |
| 1581 | + "arrObj", arrObj, |
| 1582 | + "arrObjStr", arrObjStr |
| 1583 | + ); |
| 1584 | + |
| 1585 | + countly.views().startView("test", segmentation); |
| 1586 | + |
| 1587 | + Map<String, Object> expectedSegmentation = TestUtils.map(); |
| 1588 | + ClearFillSegmentationViewStart(expectedSegmentation, "test", true); |
| 1589 | + expectedSegmentation.putAll(TestUtils.map( |
| 1590 | + "arr", new JSONArray(arr), |
| 1591 | + "arrB", new JSONArray(arrB), |
| 1592 | + "arrS", new JSONArray(arrS), |
| 1593 | + "arrL", new JSONArray(arrL), |
| 1594 | + "arrD", new JSONArray(arrD), |
| 1595 | + "arrLO", new JSONArray(arrLO), |
| 1596 | + "arrDO", new JSONArray(arrDO), |
| 1597 | + "arrBO", new JSONArray(arrBO), |
| 1598 | + "arrIO", new JSONArray(arrIO) |
| 1599 | + )); |
| 1600 | + |
| 1601 | + ModuleEventsTests.validateEventInRQ(ModuleViews.VIEW_EVENT_KEY, expectedSegmentation, 0); |
| 1602 | + } |
| 1603 | + |
| 1604 | + /** |
| 1605 | + * "recordEvent" with List segmentations |
| 1606 | + * Validate that all primitive types Lists are successfully recorded |
| 1607 | + * And validate that List of Objects is not recorded |
| 1608 | + * But Generic type of Object list which its values are only primitive types are recorded |
| 1609 | + * |
| 1610 | + * @throws JSONException if the JSON is not valid |
| 1611 | + */ |
| 1612 | + @Test |
| 1613 | + public void startView__validateSupportedLists() throws JSONException { |
| 1614 | + List<Integer> arr = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 1615 | + List<Boolean> arrB = Arrays.asList(true, false, true, false, true, false, true, false, true, false); |
| 1616 | + List<String> arrS = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"); |
| 1617 | + List<Long> arrLO = Arrays.asList(Long.MAX_VALUE, Long.MIN_VALUE); |
| 1618 | + List<Double> arrDO = Arrays.asList(Double.MAX_VALUE, Double.MIN_VALUE); |
| 1619 | + List<Boolean> arrBO = Arrays.asList(Boolean.TRUE, Boolean.FALSE); |
| 1620 | + List<Integer> arrIO = Arrays.asList(Integer.MAX_VALUE, Integer.MIN_VALUE); |
| 1621 | + List<Object> arrObj = Arrays.asList("1", 1, 1.1d, true, Long.MAX_VALUE); |
| 1622 | + List<Object> arrObjStr = Arrays.asList("1", "1", "1.1d", "true", "Long.MAX_VALUE"); |
| 1623 | + |
| 1624 | + CountlyConfig countlyConfig = TestUtils.createBaseConfig(); |
| 1625 | + countlyConfig.setEventQueueSizeToSend(1); |
| 1626 | + Countly countly = new Countly().init(countlyConfig); |
| 1627 | + |
| 1628 | + // Create segmentation using maps with lists |
| 1629 | + Map<String, Object> segmentation = TestUtils.map( |
| 1630 | + "arr", arr, |
| 1631 | + "arrB", arrB, |
| 1632 | + "arrS", arrS, |
| 1633 | + "arrLO", arrLO, |
| 1634 | + "arrDO", arrDO, |
| 1635 | + "arrBO", arrBO, |
| 1636 | + "arrIO", arrIO, |
| 1637 | + "arrObj", arrObj, |
| 1638 | + "arrObjStr", arrObjStr |
| 1639 | + ); |
| 1640 | + |
| 1641 | + countly.views().startView("test", segmentation); |
| 1642 | + |
| 1643 | + Map<String, Object> expectedSegmentation = TestUtils.map(); |
| 1644 | + ClearFillSegmentationViewStart(expectedSegmentation, "test", true); |
| 1645 | + |
| 1646 | + // Prepare expected segmentation with JSONArrays |
| 1647 | + expectedSegmentation.putAll(TestUtils.map( |
| 1648 | + "arr", new JSONArray(arr), |
| 1649 | + "arrB", new JSONArray(arrB), |
| 1650 | + "arrS", new JSONArray(arrS), |
| 1651 | + "arrLO", new JSONArray(arrLO), |
| 1652 | + "arrDO", new JSONArray(arrDO), |
| 1653 | + "arrBO", new JSONArray(arrBO), |
| 1654 | + "arrIO", new JSONArray(arrIO), |
| 1655 | + "arrObjStr", new JSONArray(arrObjStr) |
| 1656 | + )); |
| 1657 | + |
| 1658 | + // Validate the recorded event with expected segmentation |
| 1659 | + ModuleEventsTests.validateEventInRQ(ModuleViews.VIEW_EVENT_KEY, expectedSegmentation, 0); |
| 1660 | + } |
| 1661 | + |
| 1662 | + /** |
| 1663 | + * "recordEvent" with JSONArray segmentations |
| 1664 | + * Validate that all primitive types JSONArrays are successfully recorded |
| 1665 | + * And validate and JSONArray of Objects is not recorded |
| 1666 | + * |
| 1667 | + * @throws JSONException if the JSON is not valid |
| 1668 | + */ |
| 1669 | + @Test |
| 1670 | + public void startView__validateSupportedJSONArrays() throws JSONException { |
| 1671 | + JSONArray arr = new JSONArray(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); |
| 1672 | + JSONArray arrB = new JSONArray(Arrays.asList(true, false, true, false, true, false, true, false, true, false)); |
| 1673 | + JSONArray arrS = new JSONArray(Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")); |
| 1674 | + JSONArray arrL = new JSONArray(Arrays.asList(Long.MAX_VALUE, Long.MIN_VALUE)); |
| 1675 | + JSONArray arrD = new JSONArray(Arrays.asList(Double.MAX_VALUE, Double.MIN_VALUE)); |
| 1676 | + JSONArray arrBO = new JSONArray(Arrays.asList(Boolean.TRUE, Boolean.FALSE)); |
| 1677 | + JSONArray arrIO = new JSONArray(Arrays.asList(Integer.MAX_VALUE, Integer.MIN_VALUE)); |
| 1678 | + JSONArray arrObj = new JSONArray(Arrays.asList("1", 1, 1.1d, true, Long.MAX_VALUE)); |
| 1679 | + |
| 1680 | + CountlyConfig countlyConfig = TestUtils.createBaseConfig(); |
| 1681 | + countlyConfig.setEventQueueSizeToSend(1); |
| 1682 | + Countly countly = new Countly().init(countlyConfig); |
| 1683 | + |
| 1684 | + // Create segmentation using maps with lists |
| 1685 | + Map<String, Object> segmentation = TestUtils.map( |
| 1686 | + "arr", arr, |
| 1687 | + "arrB", arrB, |
| 1688 | + "arrS", arrS, |
| 1689 | + "arrL", arrL, |
| 1690 | + "arrD", arrD, |
| 1691 | + "arrBO", arrBO, |
| 1692 | + "arrIO", arrIO, |
| 1693 | + "arrObj", arrObj |
| 1694 | + ); |
| 1695 | + |
| 1696 | + countly.views().startView("test", segmentation); |
| 1697 | + |
| 1698 | + Map<String, Object> expectedSegmentation = TestUtils.map(); |
| 1699 | + ClearFillSegmentationViewStart(expectedSegmentation, "test", true); |
| 1700 | + |
| 1701 | + // Prepare expected segmentation with JSONArrays |
| 1702 | + expectedSegmentation.putAll(TestUtils.map( |
| 1703 | + "arr", arr, |
| 1704 | + "arrB", arrB, |
| 1705 | + "arrS", arrS, |
| 1706 | + "arrL", arrL, |
| 1707 | + "arrD", arrD, |
| 1708 | + "arrBO", arrBO, |
| 1709 | + "arrIO", arrIO |
| 1710 | + )); |
| 1711 | + |
| 1712 | + // Validate the recorded event with expected segmentation |
| 1713 | + ModuleEventsTests.validateEventInRQ(ModuleViews.VIEW_EVENT_KEY, expectedSegmentation, 0); |
| 1714 | + } |
| 1715 | + |
| 1716 | + /** |
| 1717 | + * "recordHandledException" with invalid data types |
| 1718 | + * Validate that unsupported data types are not recorded |
| 1719 | + * |
| 1720 | + * @throws JSONException if the JSON is not valid |
| 1721 | + */ |
| 1722 | + @Test |
| 1723 | + public void startView__unsupportedDataTypesSegmentation() throws JSONException { |
| 1724 | + CountlyConfig countlyConfig = TestUtils.createBaseConfig(); |
| 1725 | + countlyConfig.setEventQueueSizeToSend(1); |
| 1726 | + Countly countly = new Countly().init(countlyConfig); |
| 1727 | + |
| 1728 | + Map<String, Object> segmentation = TestUtils.map( |
| 1729 | + "a", TestUtils.map(), |
| 1730 | + "b", TestUtils.json(), |
| 1731 | + "c", new Object(), |
| 1732 | + "d", Sets.newSet(), |
| 1733 | + "e", Mockito.mock(ModuleLog.class) |
| 1734 | + ); |
| 1735 | + |
| 1736 | + countly.views().startView("test", segmentation); |
| 1737 | + |
| 1738 | + Map<String, Object> expectedSegmentation = TestUtils.map(); |
| 1739 | + ClearFillSegmentationViewStart(expectedSegmentation, "test", true); |
| 1740 | + |
| 1741 | + ModuleEventsTests.validateEventInRQ(ModuleViews.VIEW_EVENT_KEY, expectedSegmentation, 0); |
| 1742 | + } |
| 1743 | + |
1540 | 1744 | //todo extract orientation tests |
1541 | 1745 | } |
0 commit comments