1
+ /*
2
+ * Copyright (C) 2021 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 .settings .notification .zen ;
18
+
19
+ import static com .google .common .truth .Truth .assertThat ;
20
+
21
+ import static org .mockito .Mockito .when ;
22
+
23
+ import android .app .AutomaticZenRule ;
24
+ import android .app .NotificationManager ;
25
+ import android .content .ComponentName ;
26
+ import android .content .Context ;
27
+ import android .content .pm .ComponentInfo ;
28
+ import android .content .pm .PackageManager ;
29
+ import android .net .Uri ;
30
+ import android .os .Bundle ;
31
+ import android .service .notification .ConditionProviderService ;
32
+ import android .service .notification .ZenPolicy ;
33
+
34
+ import org .junit .Before ;
35
+ import org .junit .Test ;
36
+ import org .junit .runner .RunWith ;
37
+ import org .mockito .Mock ;
38
+ import org .mockito .MockitoAnnotations ;
39
+ import org .robolectric .RobolectricTestRunner ;
40
+ import org .robolectric .RuntimeEnvironment ;
41
+
42
+ @ RunWith (RobolectricTestRunner .class )
43
+ public class AbstractZenModeAutomaticRulePreferenceControllerTest {
44
+
45
+ @ Mock
46
+ private PackageManager mPm ;
47
+ private Context mContext ;
48
+
49
+ @ Before
50
+ public void setup () {
51
+ MockitoAnnotations .initMocks (this );
52
+ mContext = RuntimeEnvironment .application ;
53
+ }
54
+
55
+ @ Test
56
+ public void testGetSettingsActivity_configActivity () throws Exception {
57
+ AutomaticZenRule rule = new AutomaticZenRule ("name" , null ,
58
+ new ComponentName (mContext .getPackageName (), "test" ), Uri .EMPTY ,
59
+ new ZenPolicy (), NotificationManager .INTERRUPTION_FILTER_PRIORITY , true );
60
+ rule .setPackageName (mContext .getPackageName ());
61
+
62
+ when (mPm .getPackageUid (null , 0 )).thenReturn (-1 );
63
+ when (mPm .getPackageUid (mContext .getPackageName (), 0 )).thenReturn (1 );
64
+
65
+ ComponentName actual = AbstractZenModeAutomaticRulePreferenceController
66
+ .getSettingsActivity (mPm , rule , null );
67
+
68
+ assertThat (actual ).isEqualTo (new ComponentName (mContext .getPackageName (), "test" ));
69
+ }
70
+
71
+ @ Test
72
+ public void testGetSettingsActivity_configActivity_wrongPackage () throws Exception {
73
+ AutomaticZenRule rule = new AutomaticZenRule ("name" , null ,
74
+ new ComponentName ("another" , "test" ), Uri .EMPTY ,
75
+ new ZenPolicy (), NotificationManager .INTERRUPTION_FILTER_PRIORITY , true );
76
+ rule .setPackageName (mContext .getPackageName ());
77
+
78
+ when (mPm .getPackageUid (null , 0 )).thenReturn (-1 );
79
+ when (mPm .getPackageUid (mContext .getPackageName (), 0 )).thenReturn (1 );
80
+
81
+ ComponentName actual = AbstractZenModeAutomaticRulePreferenceController
82
+ .getSettingsActivity (mPm , rule , null );
83
+
84
+ assertThat (actual ).isNull ();
85
+ }
86
+
87
+ @ Test
88
+ public void testGetSettingsActivity_configActivity_unspecifiedOwner () throws Exception {
89
+ AutomaticZenRule rule = new AutomaticZenRule ("name" , null ,
90
+ new ComponentName ("another" , "test" ), Uri .EMPTY ,
91
+ new ZenPolicy (), NotificationManager .INTERRUPTION_FILTER_PRIORITY , true );
92
+
93
+ when (mPm .getPackageUid (null , 0 )).thenReturn (-1 );
94
+ when (mPm .getPackageUid (mContext .getPackageName (), 0 )).thenReturn (1 );
95
+
96
+ ComponentName actual = AbstractZenModeAutomaticRulePreferenceController
97
+ .getSettingsActivity (mPm , rule , null );
98
+
99
+ assertThat (actual ).isEqualTo (new ComponentName ("another" , "test" ));
100
+ }
101
+
102
+ @ Test
103
+ public void testGetSettingsActivity_cps () throws Exception {
104
+ AutomaticZenRule rule = new AutomaticZenRule ("name" ,
105
+ new ComponentName (mContext .getPackageName (), "service" ), null , Uri .EMPTY ,
106
+ new ZenPolicy (), NotificationManager .INTERRUPTION_FILTER_PRIORITY , true );
107
+ rule .setPackageName (mContext .getPackageName ());
108
+
109
+ ComponentInfo ci = new ComponentInfo ();
110
+ ci .packageName = mContext .getPackageName ();
111
+ ci .metaData = new Bundle ();
112
+ ci .metaData .putString (ConditionProviderService .META_DATA_CONFIGURATION_ACTIVITY ,
113
+ ComponentName .flattenToShortString (
114
+ new ComponentName (mContext .getPackageName (), "activity" )));
115
+
116
+ when (mPm .getPackageUid (null , 0 )).thenReturn (-1 );
117
+ when (mPm .getPackageUid (mContext .getPackageName (), 0 )).thenReturn (1 );
118
+
119
+ ComponentName actual = AbstractZenModeAutomaticRulePreferenceController
120
+ .getSettingsActivity (mPm , rule , ci );
121
+
122
+ assertThat (actual ).isEqualTo (new ComponentName (mContext .getPackageName (), "activity" ));
123
+ }
124
+
125
+ @ Test
126
+ public void testGetSettingsActivity_cps_wrongPackage () throws Exception {
127
+ AutomaticZenRule rule = new AutomaticZenRule ("name" ,
128
+ new ComponentName (mContext .getPackageName (), "service" ), null , Uri .EMPTY ,
129
+ new ZenPolicy (), NotificationManager .INTERRUPTION_FILTER_PRIORITY , true );
130
+ rule .setPackageName ("other" );
131
+
132
+ ComponentInfo ci = new ComponentInfo ();
133
+ ci .packageName = mContext .getPackageName ();
134
+ ci .metaData = new Bundle ();
135
+ ci .metaData .putString (ConditionProviderService .META_DATA_CONFIGURATION_ACTIVITY ,
136
+ ComponentName .flattenToShortString (
137
+ new ComponentName (mContext .getPackageName (), "activity" )));
138
+
139
+ when (mPm .getPackageUid (null , 0 )).thenReturn (-1 );
140
+ when (mPm .getPackageUid (mContext .getPackageName (), 0 )).thenReturn (1 );
141
+
142
+ ComponentName actual = AbstractZenModeAutomaticRulePreferenceController
143
+ .getSettingsActivity (mPm , rule , ci );
144
+
145
+ assertThat (actual ).isNull ();
146
+ }
147
+
148
+ @ Test
149
+ public void testGetSettingsActivity_cps_unspecifiedPackage () throws Exception {
150
+ AutomaticZenRule rule = new AutomaticZenRule ("name" ,
151
+ new ComponentName (mContext .getPackageName (), "service" ), null , Uri .EMPTY ,
152
+ new ZenPolicy (), NotificationManager .INTERRUPTION_FILTER_PRIORITY , true );
153
+
154
+ ComponentInfo ci = new ComponentInfo ();
155
+ ci .packageName = mContext .getPackageName ();
156
+ ci .metaData = new Bundle ();
157
+ ci .metaData .putString (ConditionProviderService .META_DATA_CONFIGURATION_ACTIVITY ,
158
+ ComponentName .flattenToShortString (
159
+ new ComponentName (mContext .getPackageName (), "activity" )));
160
+
161
+ when (mPm .getPackageUid (null , 0 )).thenReturn (-1 );
162
+ when (mPm .getPackageUid (mContext .getPackageName (), 0 )).thenReturn (1 );
163
+
164
+ ComponentName actual = AbstractZenModeAutomaticRulePreferenceController
165
+ .getSettingsActivity (mPm , rule , ci );
166
+
167
+ assertThat (actual ).isEqualTo (new ComponentName (mContext .getPackageName (), "activity" ));
168
+ }
169
+ }
0 commit comments