|
| 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.duckchat.impl.inputscreen.ui.metrics.usage |
| 18 | + |
| 19 | +import androidx.lifecycle.LifecycleOwner |
| 20 | +import com.duckduckgo.app.statistics.pixels.Pixel |
| 21 | +import com.duckduckgo.duckchat.api.DuckAiFeatureState |
| 22 | +import com.duckduckgo.duckchat.impl.pixel.DuckChatPixelName |
| 23 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 24 | +import org.junit.Before |
| 25 | +import org.junit.Test |
| 26 | +import org.mockito.kotlin.mock |
| 27 | +import org.mockito.kotlin.verify |
| 28 | +import org.mockito.kotlin.verifyNoInteractions |
| 29 | +import org.mockito.kotlin.whenever |
| 30 | + |
| 31 | +class InputScreenSessionUsageMetricTest { |
| 32 | + |
| 33 | + private val pixel: Pixel = mock() |
| 34 | + private val duckAiFeatureState: DuckAiFeatureState = mock() |
| 35 | + private val lifecycleOwner: LifecycleOwner = mock() |
| 36 | + private val showInputScreenFlow = MutableStateFlow(true) |
| 37 | + |
| 38 | + private lateinit var testee: InputScreenSessionUsageMetricImpl |
| 39 | + |
| 40 | + @Before |
| 41 | + fun setup() { |
| 42 | + whenever(duckAiFeatureState.showInputScreen).thenReturn(showInputScreenFlow) |
| 43 | + testee = InputScreenSessionUsageMetricImpl(pixel, duckAiFeatureState) |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun `when onStop called with feature enabled and no searches or prompts then pixel fired with zero counts`() { |
| 48 | + showInputScreenFlow.value = true |
| 49 | + |
| 50 | + testee.onStop(lifecycleOwner) |
| 51 | + |
| 52 | + val expectedParams = mapOf( |
| 53 | + "searches_in_session" to "0", |
| 54 | + "prompts_in_session" to "0", |
| 55 | + ) |
| 56 | + verify(pixel).enqueueFire( |
| 57 | + pixel = DuckChatPixelName.DUCK_CHAT_EXPERIMENTAL_OMNIBAR_SESSION_SUMMARY, |
| 58 | + parameters = expectedParams, |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + fun `when onStop called with feature disabled then no pixel fired`() { |
| 64 | + showInputScreenFlow.value = false |
| 65 | + |
| 66 | + testee.onSearchSubmitted() |
| 67 | + testee.onPromptSubmitted() |
| 68 | + testee.onStop(lifecycleOwner) |
| 69 | + |
| 70 | + verifyNoInteractions(pixel) |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + fun `when onSearchSubmitted called once and feature enabled then counter increments`() { |
| 75 | + showInputScreenFlow.value = true |
| 76 | + |
| 77 | + testee.onSearchSubmitted() |
| 78 | + testee.onStop(lifecycleOwner) |
| 79 | + |
| 80 | + val expectedParams = mapOf( |
| 81 | + "searches_in_session" to "1", |
| 82 | + "prompts_in_session" to "0", |
| 83 | + ) |
| 84 | + verify(pixel).enqueueFire( |
| 85 | + pixel = DuckChatPixelName.DUCK_CHAT_EXPERIMENTAL_OMNIBAR_SESSION_SUMMARY, |
| 86 | + parameters = expectedParams, |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun `when onPromptSubmitted called once and feature enabled then counter increments`() { |
| 92 | + showInputScreenFlow.value = true |
| 93 | + |
| 94 | + testee.onPromptSubmitted() |
| 95 | + testee.onStop(lifecycleOwner) |
| 96 | + |
| 97 | + val expectedParams = mapOf( |
| 98 | + "searches_in_session" to "0", |
| 99 | + "prompts_in_session" to "1", |
| 100 | + ) |
| 101 | + verify(pixel).enqueueFire( |
| 102 | + pixel = DuckChatPixelName.DUCK_CHAT_EXPERIMENTAL_OMNIBAR_SESSION_SUMMARY, |
| 103 | + parameters = expectedParams, |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + fun `when multiple searches and prompts submitted and feature enabled then counters increment correctly`() { |
| 109 | + showInputScreenFlow.value = true |
| 110 | + |
| 111 | + testee.onSearchSubmitted() |
| 112 | + testee.onSearchSubmitted() |
| 113 | + testee.onSearchSubmitted() |
| 114 | + testee.onPromptSubmitted() |
| 115 | + testee.onPromptSubmitted() |
| 116 | + |
| 117 | + testee.onStop(lifecycleOwner) |
| 118 | + |
| 119 | + val expectedParams = mapOf( |
| 120 | + "searches_in_session" to "3", |
| 121 | + "prompts_in_session" to "2", |
| 122 | + ) |
| 123 | + verify(pixel).enqueueFire( |
| 124 | + pixel = DuckChatPixelName.DUCK_CHAT_EXPERIMENTAL_OMNIBAR_SESSION_SUMMARY, |
| 125 | + parameters = expectedParams, |
| 126 | + ) |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + fun `when onStop called multiple times with feature enabled then counters reset after each call`() { |
| 131 | + showInputScreenFlow.value = true |
| 132 | + |
| 133 | + // First session |
| 134 | + testee.onSearchSubmitted() |
| 135 | + testee.onPromptSubmitted() |
| 136 | + testee.onStop(lifecycleOwner) |
| 137 | + |
| 138 | + val firstSessionParams = mapOf( |
| 139 | + "searches_in_session" to "1", |
| 140 | + "prompts_in_session" to "1", |
| 141 | + ) |
| 142 | + verify(pixel).enqueueFire( |
| 143 | + pixel = DuckChatPixelName.DUCK_CHAT_EXPERIMENTAL_OMNIBAR_SESSION_SUMMARY, |
| 144 | + parameters = firstSessionParams, |
| 145 | + ) |
| 146 | + |
| 147 | + // Second session - counters should be reset |
| 148 | + testee.onSearchSubmitted() |
| 149 | + testee.onStop(lifecycleOwner) |
| 150 | + |
| 151 | + val secondSessionParams = mapOf( |
| 152 | + "searches_in_session" to "1", |
| 153 | + "prompts_in_session" to "0", |
| 154 | + ) |
| 155 | + verify(pixel).enqueueFire( |
| 156 | + pixel = DuckChatPixelName.DUCK_CHAT_EXPERIMENTAL_OMNIBAR_SESSION_SUMMARY, |
| 157 | + parameters = secondSessionParams, |
| 158 | + ) |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + fun `when counters increment but onStop never called then no pixel fired`() { |
| 163 | + showInputScreenFlow.value = true |
| 164 | + |
| 165 | + testee.onSearchSubmitted() |
| 166 | + testee.onPromptSubmitted() |
| 167 | + |
| 168 | + verifyNoInteractions(pixel) |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + fun `when thread safety is required then atomic counters handle concurrent access`() { |
| 173 | + showInputScreenFlow.value = true |
| 174 | + |
| 175 | + // Simulate concurrent access |
| 176 | + val threads = mutableListOf<Thread>() |
| 177 | + |
| 178 | + repeat(10) { threadIndex -> |
| 179 | + val thread = Thread { |
| 180 | + repeat(5) { |
| 181 | + if (threadIndex % 2 == 0) { |
| 182 | + testee.onSearchSubmitted() |
| 183 | + } else { |
| 184 | + testee.onPromptSubmitted() |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + threads.add(thread) |
| 189 | + thread.start() |
| 190 | + } |
| 191 | + |
| 192 | + // Wait for all threads to complete |
| 193 | + threads.forEach { it.join() } |
| 194 | + |
| 195 | + testee.onStop(lifecycleOwner) |
| 196 | + |
| 197 | + // Should have 25 searches (5 even threads * 5 calls each) and 25 prompts (5 odd threads * 5 calls each) |
| 198 | + val expectedParams = mapOf( |
| 199 | + "searches_in_session" to "25", |
| 200 | + "prompts_in_session" to "25", |
| 201 | + ) |
| 202 | + verify(pixel).enqueueFire( |
| 203 | + pixel = DuckChatPixelName.DUCK_CHAT_EXPERIMENTAL_OMNIBAR_SESSION_SUMMARY, |
| 204 | + parameters = expectedParams, |
| 205 | + ) |
| 206 | + } |
| 207 | +} |
0 commit comments