|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 DuckDuckGo |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.duckduckgo.adclick.impl.metrics |
| 18 | + |
| 19 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 20 | +import com.duckduckgo.app.attributed.metrics.api.AttributedMetricClient |
| 21 | +import com.duckduckgo.app.attributed.metrics.api.EventStats |
| 22 | +import com.duckduckgo.browser.api.install.AppInstall |
| 23 | +import com.duckduckgo.common.test.CoroutineTestRule |
| 24 | +import kotlinx.coroutines.test.runTest |
| 25 | +import org.junit.Assert.assertEquals |
| 26 | +import org.junit.Assert.assertNull |
| 27 | +import org.junit.Before |
| 28 | +import org.junit.Rule |
| 29 | +import org.junit.Test |
| 30 | +import org.junit.runner.RunWith |
| 31 | +import org.mockito.kotlin.mock |
| 32 | +import org.mockito.kotlin.never |
| 33 | +import org.mockito.kotlin.verify |
| 34 | +import org.mockito.kotlin.whenever |
| 35 | +import java.time.Instant |
| 36 | +import java.time.ZoneId |
| 37 | + |
| 38 | +@RunWith(AndroidJUnit4::class) |
| 39 | +class RealAdClickAttributedMetricTest { |
| 40 | + |
| 41 | + @get:Rule val coroutineRule = CoroutineTestRule() |
| 42 | + |
| 43 | + private val attributedMetricClient: AttributedMetricClient = mock() |
| 44 | + private val appInstall: AppInstall = mock() |
| 45 | + |
| 46 | + private lateinit var testee: RealAdClickAttributedMetric |
| 47 | + |
| 48 | + @Before fun setup() { |
| 49 | + testee = RealAdClickAttributedMetric( |
| 50 | + appCoroutineScope = coroutineRule.testScope, |
| 51 | + dispatcherProvider = coroutineRule.testDispatcherProvider, |
| 52 | + attributedMetricClient = attributedMetricClient, |
| 53 | + appInstall = appInstall, |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + @Test fun whenPixelNameRequestedThenReturnCorrectName() { |
| 58 | + assertEquals("user_average_ad_clicks_past_week", testee.getPixelName()) |
| 59 | + } |
| 60 | + |
| 61 | + @Test fun whenAdClickAndDaysInstalledIsZeroThenDoNotEmitMetric() = runTest { |
| 62 | + givenDaysSinceInstalled(0) |
| 63 | + |
| 64 | + testee.onAdClick() |
| 65 | + |
| 66 | + verify(attributedMetricClient).collectEvent("ad_click") |
| 67 | + verify(attributedMetricClient, never()).emitMetric(testee) |
| 68 | + } |
| 69 | + |
| 70 | + @Test fun whenAdClickAndNoEventsThenDoNotEmitMetric() = runTest { |
| 71 | + givenDaysSinceInstalled(7) |
| 72 | + whenever(attributedMetricClient.getEventStats("ad_click", 7)).thenReturn( |
| 73 | + EventStats( |
| 74 | + daysWithEvents = 0, |
| 75 | + rollingAverage = 0.0, |
| 76 | + totalEvents = 0, |
| 77 | + ), |
| 78 | + ) |
| 79 | + |
| 80 | + testee.onAdClick() |
| 81 | + |
| 82 | + verify(attributedMetricClient).collectEvent("ad_click") |
| 83 | + verify(attributedMetricClient, never()).emitMetric(testee) |
| 84 | + } |
| 85 | + |
| 86 | + @Test fun whenAdClickAndHasEventsThenEmitMetric() = runTest { |
| 87 | + givenDaysSinceInstalled(7) |
| 88 | + whenever(attributedMetricClient.getEventStats("ad_click", 7)).thenReturn( |
| 89 | + EventStats( |
| 90 | + daysWithEvents = 1, |
| 91 | + rollingAverage = 1.0, |
| 92 | + totalEvents = 1, |
| 93 | + ), |
| 94 | + ) |
| 95 | + |
| 96 | + testee.onAdClick() |
| 97 | + |
| 98 | + verify(attributedMetricClient).collectEvent("ad_click") |
| 99 | + verify(attributedMetricClient).emitMetric(testee) |
| 100 | + } |
| 101 | + |
| 102 | + @Test fun whenDaysInstalledLessThanWindowThenIncludeDayAverageParameter() = runTest { |
| 103 | + givenDaysSinceInstalled(5) |
| 104 | + whenever(attributedMetricClient.getEventStats("ad_click", 5)).thenReturn( |
| 105 | + EventStats( |
| 106 | + daysWithEvents = 1, |
| 107 | + rollingAverage = 1.0, |
| 108 | + totalEvents = 1, |
| 109 | + ), |
| 110 | + ) |
| 111 | + |
| 112 | + val params = testee.getMetricParameters() |
| 113 | + |
| 114 | + assertEquals("5", params["dayAverage"]) |
| 115 | + } |
| 116 | + |
| 117 | + @Test fun whenDaysInstalledGreaterThanWindowThenOmitDayAverageParameter() = runTest { |
| 118 | + givenDaysSinceInstalled(8) |
| 119 | + whenever(attributedMetricClient.getEventStats("ad_click", 7)).thenReturn( |
| 120 | + EventStats( |
| 121 | + daysWithEvents = 1, |
| 122 | + rollingAverage = 1.0, |
| 123 | + totalEvents = 1, |
| 124 | + ), |
| 125 | + ) |
| 126 | + |
| 127 | + val params = testee.getMetricParameters() |
| 128 | + |
| 129 | + assertNull(params["dayAverage"]) |
| 130 | + } |
| 131 | + |
| 132 | + @Test fun whenGetMetricParametersThenReturnCorrectBucketValue() = runTest { |
| 133 | + // Map of average clicks to expected bucket value |
| 134 | + // clicks avg -> bucket |
| 135 | + val bucketRanges = mapOf( |
| 136 | + 0.0 to 0, |
| 137 | + 1.0 to 0, |
| 138 | + 2.2 to 0, |
| 139 | + 2.6 to 1, |
| 140 | + 3.0 to 1, |
| 141 | + 5.4 to 1, |
| 142 | + 6.0 to 2, |
| 143 | + 10.0 to 2, |
| 144 | + ) |
| 145 | + |
| 146 | + bucketRanges.forEach { (clicksAvg, expectedBucket) -> |
| 147 | + givenDaysSinceInstalled(8) |
| 148 | + whenever(attributedMetricClient.getEventStats("ad_click", 7)).thenReturn( |
| 149 | + EventStats( |
| 150 | + daysWithEvents = 1, // not relevant for this test |
| 151 | + rollingAverage = clicksAvg, |
| 152 | + totalEvents = 1, // not relevant for this test |
| 153 | + ), |
| 154 | + ) |
| 155 | + |
| 156 | + val params = testee.getMetricParameters() |
| 157 | + |
| 158 | + assertEquals( |
| 159 | + "For $clicksAvg clicks, should return bucket $expectedBucket", |
| 160 | + mapOf("count" to expectedBucket.toString()), |
| 161 | + params, |
| 162 | + ) |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @Test fun whenDaysInstalledThenReturnCorrectTag() = runTest { |
| 167 | + // Test different days |
| 168 | + // days installed -> expected tag |
| 169 | + val testCases = mapOf( |
| 170 | + 0 to "0", |
| 171 | + 1 to "1", |
| 172 | + 7 to "7", |
| 173 | + 30 to "30", |
| 174 | + ) |
| 175 | + |
| 176 | + testCases.forEach { (days, expectedTag) -> |
| 177 | + givenDaysSinceInstalled(days) |
| 178 | + |
| 179 | + val tag = testee.getTag() |
| 180 | + |
| 181 | + assertEquals( |
| 182 | + "For $days days installed, should return tag $expectedTag", |
| 183 | + expectedTag, |
| 184 | + tag, |
| 185 | + ) |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + private fun givenDaysSinceInstalled(days: Int) { |
| 190 | + val etZone = ZoneId.of("America/New_York") |
| 191 | + val now = Instant.now() |
| 192 | + val nowInEt = now.atZone(etZone) |
| 193 | + val installInEt = nowInEt.minusDays(days.toLong()) |
| 194 | + whenever(appInstall.getInstallationTimestamp()).thenReturn(installInEt.toInstant().toEpochMilli()) |
| 195 | + } |
| 196 | +} |
0 commit comments