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

Commit 7cc7a2cd authored by Sumedh Sen's avatar Sumedh Sen
Browse files

Testing replacement of IPackageDeleteObserver2

Test whether calls to methods of UninstallCompleteCallback are correctly
delegated to the intended methods.

Test: atest PackageManagerServiceUnitTests:UninstallCompleteCallbackTest
Bug: 239722595
Change-Id: I609868db3a4bc477307bad81bc7c37385caf6141
parent cfb4c353
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -33,11 +33,16 @@ android_test {
        "junit",
        "kotlin-test",
        "kotlin-reflect",
        "mockito-target-extended-minus-junit4",
        "services.core",
        "servicestests-utils",
        "servicestests-core-utils",
        "truth-prebuilt",
    ],
    jni_libs: [
        "libdexmakerjvmtiagent",
        "libstaticjvmtiagent",
    ],
    platform_apis: true,
    test_suites: ["device-tests"],
}
+3 −0
Original line number Diff line number Diff line
@@ -18,6 +18,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.server.pm.test">

    <!--required for using Mockito-->
    <application android:debuggable="true" />

    <instrumentation
        android:name="androidx.test.runner.AndroidJUnitRunner"
        android:targetPackage="com.android.server.pm.test"
+90 −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.test.uninstall

import android.app.PackageDeleteObserver
import android.content.Intent
import android.content.pm.IPackageDeleteObserver2
import android.content.pm.PackageManager
import android.content.pm.PackageManager.UninstallCompleteCallback
import android.os.Parcel
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.Mock
import org.mockito.Mockito.doReturn
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations.initMocks
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule

class UninstallCompleteCallbackTest {

    val PACKAGE_NAME: String = "com.example.package"
    val ERROR_MSG: String = "no error"

    @get:Rule
    val mockito: MockitoRule = MockitoJUnit.rule()

    @Mock
    lateinit var mockAdapter: PackageDeleteObserver

    val mockBinder: IPackageDeleteObserver2.Stub = object : IPackageDeleteObserver2.Stub() {
        override fun onUserActionRequired(intent: Intent) {
            mockAdapter.onUserActionRequired(intent)
        }
        override fun onPackageDeleted(basePackageName: String, returnCode: Int, msg: String) {
            mockAdapter.onPackageDeleted(basePackageName, returnCode, msg)
        }
    }

    @Before
    fun setUp() {
        initMocks(this)
    }

    @Test
    fun testCallDelegation () {
        doReturn(mockBinder).`when`(mockAdapter).binder

        val callback = UninstallCompleteCallback(mockAdapter.binder.asBinder())
        callback.onUninstallComplete(PACKAGE_NAME, PackageManager.DELETE_SUCCEEDED, ERROR_MSG)

        verify(mockAdapter, times(1)).onPackageDeleted(PACKAGE_NAME,
            PackageManager.DELETE_SUCCEEDED, ERROR_MSG)
    }

    @Test
    fun testClassIsParcelable() {
        doReturn(mockBinder).`when`(mockAdapter).binder

        val callback = UninstallCompleteCallback(mockAdapter.binder.asBinder())

        val parcel = Parcel.obtain()
        callback.writeToParcel(parcel, callback.describeContents())
        parcel.setDataPosition(0)

        val callbackFromParcel = UninstallCompleteCallback.CREATOR.createFromParcel(parcel)

        callbackFromParcel.onUninstallComplete(PACKAGE_NAME, PackageManager.DELETE_SUCCEEDED,
                ERROR_MSG)

        verify(mockAdapter, times(1)).onPackageDeleted(PACKAGE_NAME,
            PackageManager.DELETE_SUCCEEDED, ERROR_MSG)
    }
}