|
| 1 | +/* |
| 2 | + * Copyright 2025 New Vector Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | + * Please see LICENSE files in the repository root for full details. |
| 6 | + */ |
| 7 | + |
| 8 | +package io.element.android.libraries.push.impl.battery |
| 9 | + |
| 10 | +import android.content.ActivityNotFoundException |
| 11 | +import android.content.Intent |
| 12 | +import android.provider.Settings |
| 13 | +import androidx.test.platform.app.InstrumentationRegistry |
| 14 | +import com.google.common.truth.Truth.assertThat |
| 15 | +import io.element.android.services.toolbox.api.intent.ExternalIntentLauncher |
| 16 | +import io.element.android.services.toolbox.test.intent.FakeExternalIntentLauncher |
| 17 | +import io.element.android.tests.testutils.lambda.lambdaRecorder |
| 18 | +import org.junit.Test |
| 19 | +import org.junit.runner.RunWith |
| 20 | +import org.robolectric.RobolectricTestRunner |
| 21 | + |
| 22 | +@RunWith(RobolectricTestRunner::class) |
| 23 | +class AndroidBatteryOptimizationTest { |
| 24 | + @Test |
| 25 | + fun `isIgnoringBatteryOptimizations should return false`() { |
| 26 | + val sut = createAndroidBatteryOptimization() |
| 27 | + assertThat(sut.isIgnoringBatteryOptimizations()).isFalse() |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + fun `requestDisablingBatteryOptimization is called once with expected intent`() { |
| 32 | + val launchLambda = lambdaRecorder<Intent, Unit> { intent -> |
| 33 | + assertThat(intent.action).isEqualTo(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) |
| 34 | + assertThat(intent.data.toString()).isEqualTo("package:${InstrumentationRegistry.getInstrumentation().context.packageName}") |
| 35 | + } |
| 36 | + val externalIntentLauncher = FakeExternalIntentLauncher(launchLambda) |
| 37 | + val sut = createAndroidBatteryOptimization( |
| 38 | + externalIntentLauncher = externalIntentLauncher, |
| 39 | + ) |
| 40 | + val result = sut.requestDisablingBatteryOptimization() |
| 41 | + launchLambda.assertions().isCalledOnce() |
| 42 | + assertThat(result).isTrue() |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + fun `in case of 1 error, requestDisablingBatteryOptimization returns true`() { |
| 47 | + var callNumber = 0 |
| 48 | + val launchLambda = lambdaRecorder<Intent, Unit> { intent -> |
| 49 | + callNumber++ |
| 50 | + when (callNumber) { |
| 51 | + 1 -> { |
| 52 | + assertThat(intent.action).isEqualTo(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) |
| 53 | + assertThat(intent.data.toString()).isEqualTo("package:${InstrumentationRegistry.getInstrumentation().context.packageName}") |
| 54 | + throw ActivityNotFoundException("Test exception") |
| 55 | + } |
| 56 | + 2 -> { |
| 57 | + assertThat(intent.action).isEqualTo(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS) |
| 58 | + assertThat(intent.data).isNull() |
| 59 | + // No error |
| 60 | + } |
| 61 | + else -> { |
| 62 | + throw AssertionError("Unexpected call number: $callNumber") |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + val externalIntentLauncher = FakeExternalIntentLauncher(launchLambda) |
| 67 | + val sut = createAndroidBatteryOptimization( |
| 68 | + externalIntentLauncher = externalIntentLauncher, |
| 69 | + ) |
| 70 | + val result = sut.requestDisablingBatteryOptimization() |
| 71 | + launchLambda.assertions().isCalledExactly(2) |
| 72 | + assertThat(result).isTrue() |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun `in case of 2 errors, requestDisablingBatteryOptimization returns false`() { |
| 77 | + var callNumber = 0 |
| 78 | + val launchLambda = lambdaRecorder<Intent, Unit> { intent -> |
| 79 | + callNumber++ |
| 80 | + when (callNumber) { |
| 81 | + 1 -> { |
| 82 | + assertThat(intent.action).isEqualTo(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) |
| 83 | + assertThat(intent.data.toString()).isEqualTo("package:${InstrumentationRegistry.getInstrumentation().context.packageName}") |
| 84 | + throw ActivityNotFoundException("Test exception") |
| 85 | + } |
| 86 | + 2 -> { |
| 87 | + assertThat(intent.action).isEqualTo(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS) |
| 88 | + assertThat(intent.data).isNull() |
| 89 | + throw ActivityNotFoundException("Test exception") |
| 90 | + } |
| 91 | + else -> { |
| 92 | + throw AssertionError("Unexpected call number: $callNumber") |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + val externalIntentLauncher = FakeExternalIntentLauncher(launchLambda) |
| 97 | + val sut = createAndroidBatteryOptimization( |
| 98 | + externalIntentLauncher = externalIntentLauncher, |
| 99 | + ) |
| 100 | + val result = sut.requestDisablingBatteryOptimization() |
| 101 | + launchLambda.assertions().isCalledExactly(2) |
| 102 | + assertThat(result).isFalse() |
| 103 | + } |
| 104 | + |
| 105 | + private fun createAndroidBatteryOptimization( |
| 106 | + externalIntentLauncher: ExternalIntentLauncher = FakeExternalIntentLauncher(), |
| 107 | + ): AndroidBatteryOptimization { |
| 108 | + return AndroidBatteryOptimization( |
| 109 | + context = InstrumentationRegistry.getInstrumentation().context, |
| 110 | + externalIntentLauncher = externalIntentLauncher, |
| 111 | + ) |
| 112 | + } |
| 113 | +} |
0 commit comments