1 /*
2  * Copyright (C) 2020 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.cellbroadcastreceiver.unit;
18 
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.ArgumentMatchers.anyLong;
21 import static org.mockito.ArgumentMatchers.nullable;
22 import static org.mockito.Mockito.doNothing;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.verify;
27 
28 import android.app.NotificationManager;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.content.res.Configuration;
32 import android.os.UserManager;
33 
34 import com.android.cellbroadcastreceiver.CellBroadcastAlertService;
35 import com.android.cellbroadcastreceiver.CellBroadcastInternalReceiver;
36 import com.android.cellbroadcastreceiver.CellBroadcastReceiver;
37 
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 
44 public class CellBroadcastInternalReceiverTest extends CellBroadcastTest {
45 
46     @Mock Intent mIntent;
47     @Mock UserManager mUserManager;
48 
49     private Configuration mConfiguration = new Configuration();
50     private CellBroadcastInternalReceiver mReceiver;
51 
52     private static final int TEST_NOTIFICATION_ID = 0x123;
53 
54     @Before
setUp()55     public void setUp() throws Exception {
56         super.setUp(this.getClass().getSimpleName());
57         MockitoAnnotations.initMocks(this);
58         doReturn(mConfiguration).when(mResources).getConfiguration();
59         mReceiver = spy(new CellBroadcastInternalReceiver());
60         doReturn(mContext).when(mContext).getApplicationContext();
61         doReturn(mUserManager).when(mContext).getSystemService(Context.USER_SERVICE);
62         doReturn(false).when(mUserManager).isSystemUser();
63     }
64 
65     @Test
testOnReceive_actionMarkAsRead()66     public void testOnReceive_actionMarkAsRead() {
67         NotificationManager mockNotificationManager = mock(NotificationManager.class);
68         doReturn(Context.NOTIFICATION_SERVICE).when(mContext)
69             .getSystemServiceName(NotificationManager.class);
70         doReturn(mockNotificationManager).when(mContext)
71                 .getSystemService(Context.NOTIFICATION_SERVICE);
72         doReturn(CellBroadcastReceiver.ACTION_MARK_AS_READ).when(mIntent).getAction();
73         doReturn(TEST_NOTIFICATION_ID).when(mIntent).getIntExtra(
74                 CellBroadcastReceiver.EXTRA_NOTIF_ID,
75                 CellBroadcastAlertService.NOTIFICATION_ID);
76         doNothing().when(mReceiver).getCellBroadcastTask(nullable(Context.class), anyLong());
77 
78         mReceiver.onReceive(mContext, mIntent);
79 
80         verify(mIntent).getLongExtra(CellBroadcastReceiver.EXTRA_DELIVERY_TIME, -1);
81         verify(mReceiver).getCellBroadcastTask(nullable(Context.class), anyLong());
82         verify(mockNotificationManager).cancel(TEST_NOTIFICATION_ID);
83     }
84 
85     @Test
testOnReceive_cellbroadcastStartConfigAction()86     public void testOnReceive_cellbroadcastStartConfigAction() {
87         doReturn(CellBroadcastReceiver.CELLBROADCAST_START_CONFIG_ACTION).when(mIntent).getAction();
88         mReceiver.onReceive(mContext, mIntent);
89 
90         verify(mReceiver).startConfigServiceToEnableChannels(any());
91     }
92 
93     @After
tearDown()94     public void tearDown() throws Exception {
95         super.tearDown();
96     }
97 
98 }
99