Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 521429ac authored by Songchun Fan's avatar Songchun Fan
Browse files

[pm] unit test for InitAppsHelper system scan flags

BUG: 240742850
Test: unit test
Change-Id: I58a0956b0b1f183defdb1d3cdac604a4fdca0ae4
parent 86ccb95a
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ import android.util.EventLog;
import android.util.Slog;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.content.om.OverlayConfig;
import com.android.internal.util.FrameworkStatsLog;
import com.android.server.EventLogTags;
@@ -401,4 +402,9 @@ final class InitAppsHelper {
    public List<ScanPartition> getDirsToScanAsSystem() {
        return mDirsToScanAsSystem;
    }

    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
    public int getSystemScanFlags() {
        return mSystemScanFlags;
    }
}
+135 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.pm

import android.os.Build
import android.os.SystemProperties
import com.android.server.extendedtestutils.wheneverStatic
import com.android.server.testutils.spy
import com.android.server.testutils.whenever
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class InitAppsHelperTest {
    @Rule
    @JvmField
    val rule = MockSystemRule()

    @Before
    fun setUp() {
        rule.system().stageNominalSystemState()
    }

    private fun mockNoFirstBoot() {
        // To mock that this is not the first boot
        rule.system().stageScanExistingPackage("a.data.package", 1L,
            rule.system().dataAppDirectory)
    }

    private fun mockFingerprintChanged() {
        val versionInfo = Settings.VersionInfo()
        versionInfo.fingerprint = "mockFingerprintForTesting"
        whenever(rule.mocks().settings.internalVersion) {
            versionInfo
        }
    }

    private fun mockFingerprintUnchanged() {
        // To mock that this is not the first boot
        val versionInfo = Settings.VersionInfo()
        versionInfo.fingerprint = MockSystem.DEFAULT_VERSION_INFO.fingerprint
        whenever(rule.mocks().settings.internalVersion) {
            versionInfo
        }
    }

    private fun mockOta() {
        wheneverStatic {
            SystemProperties.getBoolean("persist.pm.mock-upgrade", false)
        }.thenReturn(true)
    }

    private fun createPackageManagerService(): PackageManagerService {
        return spy(PackageManagerService(rule.mocks().injector,
            false /*factoryTest*/,
            MockSystem.DEFAULT_VERSION_INFO.fingerprint,
            false /*isEngBuild*/,
            false /*isUserDebugBuild*/,
            Build.VERSION_CODES.CUR_DEVELOPMENT,
            Build.VERSION.INCREMENTAL))
    }

    @Test
    fun testSystemScanFlagOnFirstBoot() {
        val pms = createPackageManagerService()
        assertThat(pms.isFirstBoot).isEqualTo(true)
        assertThat(pms.isDeviceUpgrading).isEqualTo(false)
        val initAppsHelper = InitAppsHelper(pms, rule.mocks().apexManager, null, null,
            listOf<ScanPartition>())
        assertThat(
            initAppsHelper.systemScanFlags and PackageManagerService.SCAN_FIRST_BOOT_OR_UPGRADE)
            .isEqualTo(PackageManagerService.SCAN_FIRST_BOOT_OR_UPGRADE)
    }

    @Test
    fun testSystemScanFlagWithMockOTA() {
        mockNoFirstBoot()
        mockFingerprintUnchanged()
        mockOta()
        val pms = createPackageManagerService()
        assertThat(pms.isFirstBoot).isEqualTo(false)
        assertThat(pms.isDeviceUpgrading).isEqualTo(true)
        val initAppsHelper = InitAppsHelper(pms, rule.mocks().apexManager, null, null,
            listOf<ScanPartition>())
        assertThat(
            initAppsHelper.systemScanFlags and PackageManagerService.SCAN_FIRST_BOOT_OR_UPGRADE)
            .isEqualTo(PackageManagerService.SCAN_FIRST_BOOT_OR_UPGRADE)
    }

    @Test
    fun testSystemScanFlagNoOTA() {
        mockNoFirstBoot()
        mockFingerprintUnchanged()
        val pms = createPackageManagerService()
        assertThat(pms.isFirstBoot).isEqualTo(false)
        assertThat(pms.isDeviceUpgrading).isEqualTo(false)
        val initAppsHelper = InitAppsHelper(pms, rule.mocks().apexManager, null, null,
            listOf<ScanPartition>())
        assertThat(
            initAppsHelper.systemScanFlags and PackageManagerService.SCAN_FIRST_BOOT_OR_UPGRADE)
            .isEqualTo(0)
    }

    @Test
    fun testSystemScanFlagWithFingerprintChanged() {
        mockNoFirstBoot()
        mockFingerprintChanged()
        val pms = createPackageManagerService()
        assertThat(pms.isFirstBoot).isEqualTo(false)
        assertThat(pms.isDeviceUpgrading).isEqualTo(true)
        val initAppsHelper = InitAppsHelper(pms, rule.mocks().apexManager, null, null,
            listOf<ScanPartition>())
        assertThat(
            initAppsHelper.systemScanFlags and PackageManagerService.SCAN_FIRST_BOOT_OR_UPGRADE)
            .isEqualTo(PackageManagerService.SCAN_FIRST_BOOT_OR_UPGRADE)
    }
}