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.settings.localepicker;
18 
19 import static com.android.settings.localepicker.AppLocalePickerActivity.EXTRA_APP_LOCALE;
20 import static com.android.settings.localepicker.AppLocalePickerActivity.EXTRA_NOTIFICATION_ID;
21 
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.ArgumentMatchers.eq;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 
29 import android.app.settings.SettingsEnums;
30 import android.content.Context;
31 import android.content.Intent;
32 
33 import com.android.settings.testutils.FakeFeatureFactory;
34 
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.robolectric.RobolectricTestRunner;
41 import org.robolectric.RuntimeEnvironment;
42 
43 @RunWith(RobolectricTestRunner.class)
44 public class NotificationCancelReceiverTest {
45     private Context mContext;
46     private NotificationCancelReceiver mReceiver;
47     @Mock
48     private NotificationController mNotificationController;
49     private FakeFeatureFactory mFeatureFactory;
50 
51     @Before
setUp()52     public void setUp() {
53         MockitoAnnotations.initMocks(this);
54         mContext = RuntimeEnvironment.application;
55         mReceiver = spy(new NotificationCancelReceiver());
56         mFeatureFactory = FakeFeatureFactory.setupForTest();
57         doReturn(mNotificationController).when(mReceiver).getNotificationController(any());
58     }
59 
60     @Test
testOnReceive_incrementDismissCount()61     public void testOnReceive_incrementDismissCount() {
62         String locale = "en-US";
63         int notificationId = 100;
64         Intent intent = new Intent()
65                 .putExtra(EXTRA_APP_LOCALE, locale)
66                 .putExtra(EXTRA_NOTIFICATION_ID, notificationId);
67         when(mNotificationController.getNotificationId(locale)).thenReturn(notificationId);
68 
69         mReceiver.onReceive(mContext, intent);
70 
71         verify(mNotificationController).incrementDismissCount(eq(locale));
72         verify(mFeatureFactory.metricsFeatureProvider).action(
73                 any(), eq(SettingsEnums.ACTION_NOTIFICATION_SWIPE_FOR_SYSTEM_LOCALE));
74     }
75 }
76