1 /*
2  * Copyright (C) 2022 The Android Open Source Project
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.android.server.pm.test.uninstall
18 
19 import android.app.PackageDeleteObserver
20 import android.content.Intent
21 import android.content.pm.IPackageDeleteObserver2
22 import android.content.pm.PackageManager
23 import android.content.pm.PackageManager.UninstallCompleteCallback
24 import android.os.Parcel
25 import org.junit.Before
26 import org.junit.Rule
27 import org.junit.Test
28 import org.mockito.Mock
29 import org.mockito.Mockito.doReturn
30 import org.mockito.Mockito.times
31 import org.mockito.Mockito.verify
32 import org.mockito.MockitoAnnotations.initMocks
33 import org.mockito.junit.MockitoJUnit
34 import org.mockito.junit.MockitoRule
35 
36 class UninstallCompleteCallbackTest {
37 
38     val PACKAGE_NAME: String = "com.example.package"
39     val ERROR_MSG: String = "no error"
40 
41     @get:Rule
42     val mockito: MockitoRule = MockitoJUnit.rule()
43 
44     @Mock
45     lateinit var mockAdapter: PackageDeleteObserver
46 
47     val mockBinder: IPackageDeleteObserver2.Stub = object : IPackageDeleteObserver2.Stub() {
onUserActionRequirednull48         override fun onUserActionRequired(intent: Intent) {
49             mockAdapter.onUserActionRequired(intent)
50         }
onPackageDeletednull51         override fun onPackageDeleted(basePackageName: String, returnCode: Int, msg: String) {
52             mockAdapter.onPackageDeleted(basePackageName, returnCode, msg)
53         }
54     }
55 
56     @Before
setUpnull57     fun setUp() {
58         initMocks(this)
59     }
60 
61     @Test
testCallDelegationnull62     fun testCallDelegation () {
63         doReturn(mockBinder).`when`(mockAdapter).binder
64 
65         val callback = UninstallCompleteCallback(mockAdapter.binder.asBinder())
66         callback.onUninstallComplete(PACKAGE_NAME, PackageManager.DELETE_SUCCEEDED, ERROR_MSG)
67 
68         verify(mockAdapter, times(1)).onPackageDeleted(PACKAGE_NAME,
69             PackageManager.DELETE_SUCCEEDED, ERROR_MSG)
70     }
71 
72     @Test
testClassIsParcelablenull73     fun testClassIsParcelable() {
74         doReturn(mockBinder).`when`(mockAdapter).binder
75 
76         val callback = UninstallCompleteCallback(mockAdapter.binder.asBinder())
77 
78         val parcel = Parcel.obtain()
79         callback.writeToParcel(parcel, callback.describeContents())
80         parcel.setDataPosition(0)
81 
82         val callbackFromParcel = UninstallCompleteCallback.CREATOR.createFromParcel(parcel)
83 
84         callbackFromParcel.onUninstallComplete(PACKAGE_NAME, PackageManager.DELETE_SUCCEEDED,
85                 ERROR_MSG)
86 
87         verify(mockAdapter, times(1)).onPackageDeleted(PACKAGE_NAME,
88             PackageManager.DELETE_SUCCEEDED, ERROR_MSG)
89     }
90 }
91