|
| 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.networkprotection.impl.pixels |
| 18 | + |
| 19 | +import com.duckduckgo.appbuildconfig.api.AppBuildConfig |
| 20 | +import com.duckduckgo.common.test.api.FakeChain |
| 21 | +import com.duckduckgo.networkprotection.impl.pixels.NetworkProtectionPixelNames.NETP_REPORT_EXCELLENT_LATENCY |
| 22 | +import com.duckduckgo.networkprotection.impl.pixels.NetworkProtectionPixelNames.NETP_REPORT_GOOD_LATENCY |
| 23 | +import com.duckduckgo.networkprotection.impl.pixels.NetworkProtectionPixelNames.NETP_REPORT_MODERATE_LATENCY |
| 24 | +import com.duckduckgo.networkprotection.impl.pixels.NetworkProtectionPixelNames.NETP_REPORT_POOR_LATENCY |
| 25 | +import com.duckduckgo.networkprotection.impl.pixels.NetworkProtectionPixelNames.NETP_REPORT_TERRIBLE_LATENCY |
| 26 | +import com.duckduckgo.networkprotection.store.NetPGeoswitchingRepository |
| 27 | +import com.duckduckgo.networkprotection.store.NetPGeoswitchingRepository.UserPreferredLocation |
| 28 | +import kotlinx.coroutines.test.runTest |
| 29 | +import org.junit.Assert.assertEquals |
| 30 | +import org.junit.Assert.assertTrue |
| 31 | +import org.junit.Before |
| 32 | +import org.junit.Test |
| 33 | +import org.mockito.Mock |
| 34 | +import org.mockito.MockitoAnnotations |
| 35 | +import org.mockito.kotlin.whenever |
| 36 | + |
| 37 | +class VpnLatencyPixelInterceptorTest { |
| 38 | + |
| 39 | + @Mock |
| 40 | + private lateinit var netPGeoswitchingRepository: NetPGeoswitchingRepository |
| 41 | + |
| 42 | + @Mock |
| 43 | + private lateinit var appBuildConfig: AppBuildConfig |
| 44 | + |
| 45 | + private lateinit var testee: VpnLatencyPixelInterceptor |
| 46 | + |
| 47 | + @Before |
| 48 | + fun setUp() { |
| 49 | + MockitoAnnotations.openMocks(this) |
| 50 | + testee = VpnLatencyPixelInterceptor(netPGeoswitchingRepository, appBuildConfig) |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun whenLatencyPixelWithCustomLocationAndOsAbove15ThenAddBothParameters() = runTest { |
| 55 | + whenever(netPGeoswitchingRepository.getUserPreferredLocation()).thenReturn( |
| 56 | + UserPreferredLocation(countryCode = "US"), |
| 57 | + ) |
| 58 | + whenever(appBuildConfig.sdkInt).thenReturn(35) // API 35 (Android 15) |
| 59 | + val pixelUrl = String.format(PIXEL_TEMPLATE, NETP_REPORT_TERRIBLE_LATENCY.pixelName) |
| 60 | + |
| 61 | + val result = testee.intercept(FakeChain(pixelUrl)) |
| 62 | + |
| 63 | + val resultUrl = result.request.url |
| 64 | + assertEquals("custom", resultUrl.queryParameter("location")) |
| 65 | + assertEquals("true", resultUrl.queryParameter("os15Above")) |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun whenLatencyPixelWithNearestLocationAndOsBelow15ThenAddBothParameters() = runTest { |
| 70 | + whenever(netPGeoswitchingRepository.getUserPreferredLocation()).thenReturn( |
| 71 | + UserPreferredLocation(), |
| 72 | + ) |
| 73 | + whenever(appBuildConfig.sdkInt).thenReturn(34) // API 34 (Android 14) |
| 74 | + val pixelUrl = String.format(PIXEL_TEMPLATE, NETP_REPORT_POOR_LATENCY.pixelName) |
| 75 | + |
| 76 | + val result = testee.intercept(FakeChain(pixelUrl)) |
| 77 | + |
| 78 | + val resultUrl = result.request.url |
| 79 | + assertEquals("nearest", resultUrl.queryParameter("location")) |
| 80 | + assertEquals("false", resultUrl.queryParameter("os15Above")) |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + fun whenLatencyPixelWithNullCountryCodeThenLocationIsNearest() = runTest { |
| 85 | + whenever(netPGeoswitchingRepository.getUserPreferredLocation()).thenReturn( |
| 86 | + UserPreferredLocation(countryCode = null), |
| 87 | + ) |
| 88 | + whenever(appBuildConfig.sdkInt).thenReturn(35) // API 35 (Android 15) |
| 89 | + val pixelUrl = String.format(PIXEL_TEMPLATE, NETP_REPORT_MODERATE_LATENCY.pixelName) |
| 90 | + |
| 91 | + val result = testee.intercept(FakeChain(pixelUrl)) |
| 92 | + |
| 93 | + val resultUrl = result.request.url |
| 94 | + assertEquals("nearest", resultUrl.queryParameter("location")) |
| 95 | + assertEquals("true", resultUrl.queryParameter("os15Above")) |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun whenGoodLatencyPixelThenParametersAreAdded() = runTest { |
| 100 | + whenever(netPGeoswitchingRepository.getUserPreferredLocation()).thenReturn( |
| 101 | + UserPreferredLocation(countryCode = "CA"), |
| 102 | + ) |
| 103 | + whenever(appBuildConfig.sdkInt).thenReturn(35) // API 35 (Android 15) |
| 104 | + val pixelUrl = String.format(PIXEL_TEMPLATE, NETP_REPORT_GOOD_LATENCY.pixelName) |
| 105 | + |
| 106 | + val result = testee.intercept(FakeChain(pixelUrl)) |
| 107 | + |
| 108 | + val resultUrl = result.request.url |
| 109 | + assertEquals("custom", resultUrl.queryParameter("location")) |
| 110 | + assertEquals("true", resultUrl.queryParameter("os15Above")) |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + fun whenExcellentLatencyPixelThenParametersAreAdded() = runTest { |
| 115 | + whenever(netPGeoswitchingRepository.getUserPreferredLocation()).thenReturn( |
| 116 | + UserPreferredLocation(countryCode = "FR"), |
| 117 | + ) |
| 118 | + whenever(appBuildConfig.sdkInt).thenReturn(34) // API 34 (Android 14) |
| 119 | + val pixelUrl = String.format(PIXEL_TEMPLATE, NETP_REPORT_EXCELLENT_LATENCY.pixelName) |
| 120 | + |
| 121 | + val result = testee.intercept(FakeChain(pixelUrl)) |
| 122 | + |
| 123 | + val resultUrl = result.request.url |
| 124 | + assertEquals("custom", resultUrl.queryParameter("location")) |
| 125 | + assertEquals("false", resultUrl.queryParameter("os15Above")) |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + fun whenNonLatencyPixelThenNoParametersAdded() = runTest { |
| 130 | + val pixelUrl = String.format(PIXEL_TEMPLATE, "m_netp_ev_enabled_d") |
| 131 | + |
| 132 | + val result = testee.intercept(FakeChain(pixelUrl)) |
| 133 | + |
| 134 | + val resultUrl = result.request.url |
| 135 | + assertEquals(null, resultUrl.queryParameter("location")) |
| 136 | + assertEquals(null, resultUrl.queryParameter("os15Above")) |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + fun verifyOriginalUrlIsPreservedWithAdditionalParameters() = runTest { |
| 141 | + whenever(netPGeoswitchingRepository.getUserPreferredLocation()).thenReturn( |
| 142 | + UserPreferredLocation(), |
| 143 | + ) |
| 144 | + whenever(appBuildConfig.sdkInt).thenReturn(35) // API 35 (Android 15) |
| 145 | + val originalUrl = "https://improving.duckduckgo.com/t/${NETP_REPORT_TERRIBLE_LATENCY.pixelName}_android_phone?appVersion=5.135.0&test=1" |
| 146 | + |
| 147 | + val result = testee.intercept(FakeChain(originalUrl)) |
| 148 | + |
| 149 | + val resultUrl = result.request.url |
| 150 | + // Check that original parameters are preserved |
| 151 | + assertEquals("5.135.0", resultUrl.queryParameter("appVersion")) |
| 152 | + assertEquals("1", resultUrl.queryParameter("test")) |
| 153 | + // Check that new parameters are added |
| 154 | + assertEquals("nearest", resultUrl.queryParameter("location")) |
| 155 | + assertEquals("true", resultUrl.queryParameter("os15Above")) |
| 156 | + // Check that the base URL is preserved |
| 157 | + assertTrue(resultUrl.toString().contains("improving.duckduckgo.com")) |
| 158 | + assertTrue(resultUrl.toString().contains(NETP_REPORT_TERRIBLE_LATENCY.pixelName)) |
| 159 | + } |
| 160 | + |
| 161 | + companion object { |
| 162 | + private const val PIXEL_TEMPLATE = "https://improving.duckduckgo.com/t/%s_android_phone?appVersion=5.135.0&test=1" |
| 163 | + } |
| 164 | +} |
0 commit comments