1 /*
2  * Copyright (C) 2023 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.notification;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.eq;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.verifyNoMoreInteractions;
25 
26 import android.content.ComponentName;
27 import android.content.ServiceConnection;
28 import android.content.pm.IPackageManager;
29 import android.net.Uri;
30 import android.os.IInterface;
31 import android.service.notification.Condition;
32 
33 import com.android.server.UiServiceTestCase;
34 
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 
40 public class ConditionProvidersTest extends UiServiceTestCase {
41 
42     private ConditionProviders mProviders;
43 
44     @Mock
45     private IPackageManager mIpm;
46     @Mock
47     private ManagedServices.UserProfiles mUserProfiles;
48     @Mock
49     private ConditionProviders.Callback mCallback;
50 
51     @Before
setUp()52     public void setUp() {
53         MockitoAnnotations.initMocks(this);
54 
55         mProviders = new ConditionProviders(mContext, mUserProfiles, mIpm);
56         mProviders.setCallback(mCallback);
57     }
58 
59     @Test
notifyConditions_findCondition()60     public void notifyConditions_findCondition() {
61         ComponentName cn = new ComponentName("package", "cls");
62         ManagedServices.ManagedServiceInfo msi = mProviders.new ManagedServiceInfo(
63                 mock(IInterface.class), cn, 0, false, mock(ServiceConnection.class), 33, 100);
64         Condition[] conditions = new Condition[] {
65                 new Condition(Uri.parse("a"), "summary", Condition.STATE_TRUE),
66                 new Condition(Uri.parse("b"), "summary2", Condition.STATE_TRUE)
67         };
68 
69         mProviders.notifyConditions("package", msi, conditions);
70 
71         assertThat(mProviders.findCondition(cn, Uri.parse("a"))).isEqualTo(conditions[0]);
72         assertThat(mProviders.findCondition(cn, Uri.parse("b"))).isEqualTo(conditions[1]);
73         assertThat(mProviders.findCondition(null, Uri.parse("a"))).isNull();
74         assertThat(mProviders.findCondition(cn, null)).isNull();
75     }
76 
77     @Test
notifyConditions_callbackOnConditionChanged()78     public void notifyConditions_callbackOnConditionChanged() {
79         ManagedServices.ManagedServiceInfo msi = mProviders.new ManagedServiceInfo(
80                 mock(IInterface.class), new ComponentName("package", "cls"), 0, false,
81                 mock(ServiceConnection.class), 33, 100);
82         Condition[] conditionsToNotify = new Condition[] {
83                 new Condition(Uri.parse("a"), "summary", Condition.STATE_TRUE),
84                 new Condition(Uri.parse("b"), "summary2", Condition.STATE_TRUE),
85                 new Condition(Uri.parse("c"), "summary3", Condition.STATE_TRUE)
86         };
87 
88         mProviders.notifyConditions("package", msi, conditionsToNotify);
89 
90         verify(mCallback).onConditionChanged(eq(Uri.parse("a")), eq(conditionsToNotify[0]));
91         verify(mCallback).onConditionChanged(eq(Uri.parse("b")), eq(conditionsToNotify[1]));
92         verify(mCallback).onConditionChanged(eq(Uri.parse("c")), eq(conditionsToNotify[2]));
93         verifyNoMoreInteractions(mCallback);
94     }
95 
96     @Test
notifyConditions_duplicateIds_ignored()97     public void notifyConditions_duplicateIds_ignored() {
98         ManagedServices.ManagedServiceInfo msi = mProviders.new ManagedServiceInfo(
99                 mock(IInterface.class), new ComponentName("package", "cls"), 0, false,
100                 mock(ServiceConnection.class), 33, 100);
101         Condition[] conditionsToNotify = new Condition[] {
102                 new Condition(Uri.parse("a"), "summary", Condition.STATE_TRUE),
103                 new Condition(Uri.parse("b"), "summary2", Condition.STATE_TRUE),
104                 new Condition(Uri.parse("a"), "summary3", Condition.STATE_FALSE),
105                 new Condition(Uri.parse("a"), "summary4", Condition.STATE_FALSE)
106         };
107 
108         mProviders.notifyConditions("package", msi, conditionsToNotify);
109 
110         verify(mCallback).onConditionChanged(eq(Uri.parse("a")), eq(conditionsToNotify[0]));
111         verify(mCallback).onConditionChanged(eq(Uri.parse("b")), eq(conditionsToNotify[1]));
112 
113         verifyNoMoreInteractions(mCallback);
114     }
115 
116     @Test
notifyConditions_nullItems_ignored()117     public void notifyConditions_nullItems_ignored() {
118         ManagedServices.ManagedServiceInfo msi = mProviders.new ManagedServiceInfo(
119                 mock(IInterface.class), new ComponentName("package", "cls"), 0, false,
120                 mock(ServiceConnection.class), 33, 100);
121         Condition[] conditionsToNotify = new Condition[] {
122                 new Condition(Uri.parse("a"), "summary", Condition.STATE_TRUE),
123                 null,
124                 null,
125                 new Condition(Uri.parse("b"), "summary", Condition.STATE_TRUE)
126         };
127 
128         mProviders.notifyConditions("package", msi, conditionsToNotify);
129 
130         verify(mCallback).onConditionChanged(eq(Uri.parse("a")), eq(conditionsToNotify[0]));
131         verify(mCallback).onConditionChanged(eq(Uri.parse("b")), eq(conditionsToNotify[3]));
132         verifyNoMoreInteractions(mCallback);
133     }
134 }
135