summaryrefslogtreecommitdiff
path: root/adservices/tests/unittest/system-service/src/com/android/server/adservices/AdServicesManagerServiceTest.java
blob: aab7236ed44200e2cfb1d757d93bacc5dc36d2ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * 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.adservices;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.UserHandle;

import androidx.test.platform.app.InstrumentationRegistry;

import com.google.common.truth.Truth;

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

/** Tests for {@link AdServicesManagerService} */
public class AdServicesManagerServiceTest {
    private AdServicesManagerService mService;
    private Context mSpyContext;
    private static final String PACKAGE_NAME = "com.package.example";
    private static final String PACKAGE_CHANGED_BROADCAST =
            "com.android.adservices.PACKAGE_CHANGED";
    private static final String PACKAGE_FULLY_REMOVED = "package_fully_removed";
    private static final String PACKAGE_ADDED = "package_added";
    private static final String PACKAGE_DATA_CLEARED = "package_data_cleared";
    private static final int PACKAGE_UID = 12345;

    @Before
    public void setup() {
        Context context = InstrumentationRegistry.getInstrumentation().getContext();
        mSpyContext = Mockito.spy(context);

        InstrumentationRegistry.getInstrumentation()
                .getUiAutomation()
                .adoptShellPermissionIdentity(Manifest.permission.INTERACT_ACROSS_USERS_FULL);

        mService = new AdServicesManagerService(mSpyContext);
    }

    @Test
    public void testSendBroadcastForPackageFullyRemoved() {
        Intent i = new Intent(Intent.ACTION_PACKAGE_FULLY_REMOVED);
        i.setData(Uri.parse("package:" + PACKAGE_NAME));
        i.putExtra(Intent.EXTRA_UID, PACKAGE_UID);

        ArgumentCaptor<Intent> argumentIntent = ArgumentCaptor.forClass(Intent.class);
        ArgumentCaptor<UserHandle> argumentUser = ArgumentCaptor.forClass(UserHandle.class);

        Mockito.doNothing().when(mSpyContext).sendBroadcastAsUser(Mockito.any(), Mockito.any());
        mService.onPackageChange(i, mSpyContext.getUser());

        Mockito.verify(mSpyContext, Mockito.times(1))
                .sendBroadcastAsUser(argumentIntent.capture(), argumentUser.capture());

        Truth.assertThat(argumentIntent.getValue().getAction())
                .isEqualTo(PACKAGE_CHANGED_BROADCAST);
        Truth.assertThat(argumentIntent.getValue().getData()).isEqualTo(i.getData());
        Truth.assertThat(argumentIntent.getValue().getStringExtra("action"))
                .isEqualTo(PACKAGE_FULLY_REMOVED);
        Truth.assertThat(argumentIntent.getValue().getIntExtra(Intent.EXTRA_UID, -1))
                .isEqualTo(PACKAGE_UID);
        Truth.assertThat(argumentUser.getValue()).isEqualTo(mSpyContext.getUser());
    }

    @Test
    public void testSendBroadcastForPackageAdded() {
        Intent i = new Intent(Intent.ACTION_PACKAGE_ADDED);
        i.setData(Uri.parse("package:" + PACKAGE_NAME));
        i.putExtra(Intent.EXTRA_UID, PACKAGE_UID);
        i.putExtra(Intent.EXTRA_REPLACING, false);

        ArgumentCaptor<Intent> argumentIntent = ArgumentCaptor.forClass(Intent.class);
        ArgumentCaptor<UserHandle> argumentUser = ArgumentCaptor.forClass(UserHandle.class);

        Mockito.doNothing().when(mSpyContext).sendBroadcastAsUser(Mockito.any(), Mockito.any());
        mService.onPackageChange(i, mSpyContext.getUser());

        Mockito.verify(mSpyContext, Mockito.times(1))
                .sendBroadcastAsUser(argumentIntent.capture(), argumentUser.capture());

        Truth.assertThat(argumentIntent.getValue().getAction())
                .isEqualTo(PACKAGE_CHANGED_BROADCAST);
        Truth.assertThat(argumentIntent.getValue().getData()).isEqualTo(i.getData());
        Truth.assertThat(argumentIntent.getValue().getStringExtra("action"))
                .isEqualTo(PACKAGE_ADDED);
        Truth.assertThat(argumentIntent.getValue().getIntExtra(Intent.EXTRA_UID, -1))
                .isEqualTo(PACKAGE_UID);
        Truth.assertThat(argumentUser.getValue()).isEqualTo(mSpyContext.getUser());
    }

    @Test
    public void testSendBroadcastForPackageDataCleared() {
        Intent i = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED);
        i.setData(Uri.parse("package:" + PACKAGE_NAME));
        i.putExtra(Intent.EXTRA_UID, PACKAGE_UID);

        ArgumentCaptor<Intent> argumentIntent = ArgumentCaptor.forClass(Intent.class);
        ArgumentCaptor<UserHandle> argumentUser = ArgumentCaptor.forClass(UserHandle.class);

        Mockito.doNothing().when(mSpyContext).sendBroadcastAsUser(Mockito.any(), Mockito.any());
        mService.onPackageChange(i, mSpyContext.getUser());

        Mockito.verify(mSpyContext, Mockito.times(1))
                .sendBroadcastAsUser(argumentIntent.capture(), argumentUser.capture());

        Truth.assertThat(argumentIntent.getValue().getAction())
                .isEqualTo(PACKAGE_CHANGED_BROADCAST);
        Truth.assertThat(argumentIntent.getValue().getData()).isEqualTo(i.getData());
        Truth.assertThat(argumentIntent.getValue().getStringExtra("action"))
                .isEqualTo(PACKAGE_DATA_CLEARED);
        Truth.assertThat(argumentIntent.getValue().getIntExtra(Intent.EXTRA_UID, -1))
                .isEqualTo(PACKAGE_UID);
        Truth.assertThat(argumentUser.getValue()).isEqualTo(mSpyContext.getUser());
    }
}