1 /**
2  * Copyright (C) 2016 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;
18 
19 import android.telephony.SmsManager;
20 import android.test.suitebuilder.annotation.SmallTest;
21 
22 import com.android.internal.telephony.ISms;
23 
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 
28 import java.lang.reflect.Method;
29 
30 import org.mockito.ArgumentCaptor;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 
34 import static org.junit.Assert.assertEquals;
35 import static org.mockito.Matchers.anyInt;
36 import static org.mockito.Matchers.anyString;
37 import static org.mockito.Mockito.doReturn;
38 import static org.mockito.Mockito.times;
39 import static org.mockito.Mockito.verify;
40 
41 
42 /**
43  * Cell broadcast config service tests
44  */
45 public class CellBroadcastConfigServiceTest extends CellBroadcastTest {
46 
47     @Mock
48     ISms.Stub mSmsService;
49 
50     private CellBroadcastConfigService mConfigService;
51 
52     private SmsManager mSmsManager = SmsManager.getDefault();
53 
54     @Before
setUp()55     public void setUp() throws Exception {
56         super.setUp(getClass().getSimpleName());
57         mConfigService = new CellBroadcastConfigService();
58 
59         //mServiceManagerMockedServices.put("isms", mSmsService);
60         mMockedServiceManager.replaceService("isms", mSmsService);
61         doReturn(mSmsService).when(mSmsService).queryLocalInterface(anyString());
62     }
63 
64     @After
tearDown()65     public void tearDown() throws Exception {
66         super.tearDown();
67     }
68 
setCellBroadcastRange(boolean enable, int type, int start, int end)69     private boolean setCellBroadcastRange(boolean enable, int type, int start, int end)
70             throws Exception {
71 
72         Class[] cArgs = new Class[5];
73         cArgs[0] = SmsManager.class;
74         cArgs[1] = Boolean.TYPE;
75         cArgs[2] = cArgs[3] = cArgs[4] = Integer.TYPE;
76 
77         Method method =
78                 CellBroadcastConfigService.class.getDeclaredMethod("setCellBroadcastRange", cArgs);
79         method.setAccessible(true);
80 
81         return (boolean) method.invoke(mConfigService, mSmsManager, enable, type, start, end);
82     }
83 
setCellBroadcastOnSub(int subId, boolean enableForSub)84     private boolean setCellBroadcastOnSub(int subId, boolean enableForSub)
85             throws Exception {
86 
87         Class[] cArgs = new Class[3];
88         cArgs[0] = SmsManager.class;
89         cArgs[1] = Integer.TYPE;
90         cArgs[2] = Boolean.TYPE;
91 
92         Method method =
93                 CellBroadcastConfigService.class.getDeclaredMethod("setCellBroadcastOnSub", cArgs);
94         method.setAccessible(true);
95 
96         return (boolean) method.invoke(mConfigService, mSmsManager, subId, enableForSub);
97     }
98 
99     /**
100      * Test enable cell broadcast range
101      */
102     @Test
103     @SmallTest
testEnableCellBroadcastRange()104     public void testEnableCellBroadcastRange() throws Exception {
105         setCellBroadcastRange(true, 0, 10, 20);
106         ArgumentCaptor<Integer> captorStart = ArgumentCaptor.forClass(Integer.class);
107         ArgumentCaptor<Integer> captorEnd = ArgumentCaptor.forClass(Integer.class);
108         ArgumentCaptor<Integer> captorType = ArgumentCaptor.forClass(Integer.class);
109 
110         verify(mSmsService, times(1)).enableCellBroadcastRangeForSubscriber(anyInt(),
111                 captorStart.capture(), captorEnd.capture(), captorType.capture());
112 
113         assertEquals(10, captorStart.getValue().intValue());
114         assertEquals(20, captorEnd.getValue().intValue());
115         assertEquals(0, captorType.getValue().intValue());
116     }
117 
118     /**
119      * Test disable cell broadcast range
120      */
121     @Test
122     @SmallTest
testDisableCellBroadcastRange()123     public void testDisableCellBroadcastRange() throws Exception {
124         setCellBroadcastRange(false, 0, 10, 20);
125         ArgumentCaptor<Integer> captorStart = ArgumentCaptor.forClass(Integer.class);
126         ArgumentCaptor<Integer> captorEnd = ArgumentCaptor.forClass(Integer.class);
127         ArgumentCaptor<Integer> captorType = ArgumentCaptor.forClass(Integer.class);
128 
129         verify(mSmsService, times(1)).disableCellBroadcastRangeForSubscriber(anyInt(),
130                 captorStart.capture(), captorEnd.capture(), captorType.capture());
131 
132         assertEquals(10, captorStart.getValue().intValue());
133         assertEquals(20, captorEnd.getValue().intValue());
134         assertEquals(0, captorType.getValue().intValue());
135     }
136 }