|
| 1 | +/* |
| 2 | + * Copyright 2022 The Matrix.org Foundation C.I.C. |
| 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 org.matrix.android.sdk.internal.network |
| 18 | + |
| 19 | +import android.content.Context |
| 20 | +import android.content.pm.ApplicationInfo |
| 21 | +import android.content.pm.PackageInfo |
| 22 | +import android.content.pm.PackageManager |
| 23 | +import android.os.Build |
| 24 | +import io.mockk.every |
| 25 | +import io.mockk.mockk |
| 26 | +import org.amshove.kluent.shouldBeEqualTo |
| 27 | +import org.junit.Before |
| 28 | +import org.junit.Test |
| 29 | +import org.matrix.android.sdk.BuildConfig |
| 30 | +import java.lang.Exception |
| 31 | + |
| 32 | +private const val A_PACKAGE_NAME = "org.matrix.sdk" |
| 33 | +private const val AN_APP_NAME = "Element" |
| 34 | +private const val A_NON_ASCII_APP_NAME = "Élement" |
| 35 | +private const val AN_APP_VERSION = "1.5.1" |
| 36 | +private const val A_FLAVOUR = "GooglePlay" |
| 37 | + |
| 38 | +class ComputeUserAgentUseCaseTest { |
| 39 | + |
| 40 | + private val context = mockk<Context>() |
| 41 | + private val packageManager = mockk<PackageManager>() |
| 42 | + private val applicationInfo = mockk<ApplicationInfo>() |
| 43 | + private val packageInfo = mockk<PackageInfo>() |
| 44 | + |
| 45 | + private val computeUserAgentUseCase = ComputeUserAgentUseCase(context) |
| 46 | + |
| 47 | + @Before |
| 48 | + fun setUp() { |
| 49 | + every { context.applicationContext } returns context |
| 50 | + every { context.packageName } returns A_PACKAGE_NAME |
| 51 | + every { context.packageManager } returns packageManager |
| 52 | + every { packageManager.getApplicationInfo(any(), any()) } returns applicationInfo |
| 53 | + every { packageManager.getPackageInfo(any<String>(), any()) } returns packageInfo |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun `given a non-null app name and app version when computing user agent then returns expected user agent`() { |
| 58 | + // Given |
| 59 | + givenAppName(AN_APP_NAME) |
| 60 | + givenAppVersion(AN_APP_VERSION) |
| 61 | + |
| 62 | + // When |
| 63 | + val result = computeUserAgentUseCase.execute(A_FLAVOUR) |
| 64 | + |
| 65 | + // Then |
| 66 | + val expectedUserAgent = constructExpectedUserAgent(AN_APP_NAME, AN_APP_VERSION) |
| 67 | + result shouldBeEqualTo expectedUserAgent |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `given a null app name when computing user agent then returns user agent with package name instead of app name`() { |
| 72 | + // Given |
| 73 | + givenAppName(null) |
| 74 | + givenAppVersion(AN_APP_VERSION) |
| 75 | + |
| 76 | + // When |
| 77 | + val result = computeUserAgentUseCase.execute(A_FLAVOUR) |
| 78 | + |
| 79 | + // Then |
| 80 | + val expectedUserAgent = constructExpectedUserAgent(A_PACKAGE_NAME, AN_APP_VERSION) |
| 81 | + result shouldBeEqualTo expectedUserAgent |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + fun `given a non-ascii app name when computing user agent then returns user agent with package name instead of app name`() { |
| 86 | + // Given |
| 87 | + givenAppName(A_NON_ASCII_APP_NAME) |
| 88 | + givenAppVersion(AN_APP_VERSION) |
| 89 | + |
| 90 | + // When |
| 91 | + val result = computeUserAgentUseCase.execute(A_FLAVOUR) |
| 92 | + |
| 93 | + // Then |
| 94 | + val expectedUserAgent = constructExpectedUserAgent(A_PACKAGE_NAME, AN_APP_VERSION) |
| 95 | + result shouldBeEqualTo expectedUserAgent |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun `given a null app version when computing user agent then returns user agent with a fallback app version`() { |
| 100 | + // Given |
| 101 | + givenAppName(AN_APP_NAME) |
| 102 | + givenAppVersion(null) |
| 103 | + |
| 104 | + // When |
| 105 | + val result = computeUserAgentUseCase.execute(A_FLAVOUR) |
| 106 | + |
| 107 | + // Then |
| 108 | + val expectedUserAgent = constructExpectedUserAgent(AN_APP_NAME, ComputeUserAgentUseCase.FALLBACK_APP_VERSION) |
| 109 | + result shouldBeEqualTo expectedUserAgent |
| 110 | + } |
| 111 | + |
| 112 | + private fun constructExpectedUserAgent(appName: String, appVersion: String): String { |
| 113 | + return buildString { |
| 114 | + append(appName) |
| 115 | + append("/") |
| 116 | + append(appVersion) |
| 117 | + append(" (") |
| 118 | + append(Build.MANUFACTURER) |
| 119 | + append(" ") |
| 120 | + append(Build.MODEL) |
| 121 | + append("; ") |
| 122 | + append("Android ") |
| 123 | + append(Build.VERSION.RELEASE) |
| 124 | + append("; ") |
| 125 | + append(Build.DISPLAY) |
| 126 | + append("; ") |
| 127 | + append("Flavour ") |
| 128 | + append(A_FLAVOUR) |
| 129 | + append("; ") |
| 130 | + append("MatrixAndroidSdk2 ") |
| 131 | + append(BuildConfig.SDK_VERSION) |
| 132 | + append(")") |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private fun givenAppName(deviceName: String?) { |
| 137 | + if (deviceName == null) { |
| 138 | + every { packageManager.getApplicationLabel(any()) } throws Exception("Cannot retrieve application name") |
| 139 | + } else if (!deviceName.matches("\\A\\p{ASCII}*\\z".toRegex())) { |
| 140 | + every { packageManager.getApplicationLabel(any()) } returns A_PACKAGE_NAME |
| 141 | + } else { |
| 142 | + every { packageManager.getApplicationLabel(any()) } returns deviceName |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private fun givenAppVersion(appVersion: String?) { |
| 147 | + packageInfo.versionName = appVersion |
| 148 | + } |
| 149 | +} |
0 commit comments