e2e42cf51a76ca1bd185e7ea972a909c371bbe62
[android/platform/frameworks/opt/telephony.git] / tests / telephonytests / src / com / android / internal / telephony / gsm / GsmCellBroadcastHandlerTest.java
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.internal.telephony.gsm;
18
19 import static android.provider.Settings.Secure.CMAS_ADDITIONAL_BROADCAST_PKG;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.mockito.Matchers.nullable;
23 import static org.mockito.Mockito.any;
24 import static org.mockito.Mockito.anyBoolean;
25 import static org.mockito.Mockito.anyInt;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.eq;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30
31 import android.Manifest;
32 import android.app.Activity;
33 import android.app.AppOpsManager;
34 import android.content.BroadcastReceiver;
35 import android.content.Intent;
36 import android.os.Handler;
37 import android.os.HandlerThread;
38 import android.os.UserHandle;
39 import android.provider.Settings;
40 import android.provider.Telephony;
41 import android.support.test.filters.FlakyTest;
42
43 import com.android.internal.telephony.SmsStorageMonitor;
44 import com.android.internal.telephony.TelephonyTest;
45 import com.android.internal.telephony.TelephonyTestUtils;
46
47 import org.junit.After;
48 import org.junit.Before;
49 import org.junit.Ignore;
50 import org.mockito.ArgumentCaptor;
51 import org.mockito.Mock;
52
53 import java.util.List;
54
55 @Ignore
56 public class GsmCellBroadcastHandlerTest extends TelephonyTest {
57     @Mock
58     private SmsStorageMonitor mSmsStorageMonitor;
59     @Mock
60     private android.telephony.SmsMessage mSmsMessage;
61     @Mock
62     private SmsMessage mGsmSmsMessage;
63
64     private GsmCellBroadcastHandler mGsmCellBroadcastHandler;
65     private GsmCellBroadcastHandlerTestHandler mGsmCellBroadcastHandlerTestHandler;
66
67     private class GsmCellBroadcastHandlerTestHandler extends HandlerThread {
68
69         private GsmCellBroadcastHandlerTestHandler(String name) {
70             super(name);
71         }
72
73         @Override
74         public void onLooperPrepared() {
75             mGsmCellBroadcastHandler = GsmCellBroadcastHandler.makeGsmCellBroadcastHandler(
76                     mContextFixture.getTestDouble(), mPhone);
77             setReady(true);
78         }
79     }
80
81     @Before
82     public void setUp() throws Exception {
83
84         super.setUp(getClass().getSimpleName());
85
86         doReturn(true).when(mTelephonyManager).getSmsReceiveCapableForPhone(anyInt(), anyBoolean());
87         doReturn(true).when(mSmsStorageMonitor).isStorageAvailable();
88
89         mGsmCellBroadcastHandlerTestHandler =
90                 new GsmCellBroadcastHandlerTestHandler(getClass().getSimpleName());
91         mGsmCellBroadcastHandlerTestHandler.start();
92         waitUntilReady();
93     }
94
95     @After
96     public void tearDown() throws Exception {
97         mGsmCellBroadcastHandler = null;
98         mGsmCellBroadcastHandlerTestHandler.quitSafely();
99         super.tearDown();
100     }
101
102     @FlakyTest
103     public void testBroadcastSms() {
104         mContextFixture.putResource(
105                 com.android.internal.R.string.config_defaultCellBroadcastReceiverPkg,
106                 "fake.cellbroadcastreceiver");
107
108         Settings.Secure.putString(mContext.getContentResolver(),
109                 CMAS_ADDITIONAL_BROADCAST_PKG, "another.fake.pkg");
110         mSimulatedCommands.notifyGsmBroadcastSms(new byte[] {
111                 (byte)0xc0, //geographical scope
112                 (byte)0x01, //serial number
113                 (byte)0x01, //serial number
114                 (byte)0x01, //message identifier
115                 (byte)0x01, //message identifier
116                 (byte)0x01
117         });
118         TelephonyTestUtils.waitForMs(100);
119         ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class);
120         verify(mContextFixture.getTestDouble(), times(2)).sendOrderedBroadcastAsUser(
121                 intentArgumentCaptor.capture(), eq(UserHandle.ALL),
122                 eq(Manifest.permission.RECEIVE_SMS), eq(AppOpsManager.OP_RECEIVE_SMS),
123                 nullable(BroadcastReceiver.class), any(Handler.class), eq(Activity.RESULT_OK),
124                 eq(null), eq(null));
125
126         List<Intent> intentList = intentArgumentCaptor.getAllValues();
127
128         assertEquals(Telephony.Sms.Intents.SMS_CB_RECEIVED_ACTION,
129                 intentList.get(0).getAction());
130         assertEquals("another.fake.pkg", intentList.get(0).getPackage());
131
132         assertEquals(Telephony.Sms.Intents.SMS_CB_RECEIVED_ACTION,
133                 intentList.get(1).getAction());
134         assertEquals("fake.cellbroadcastreceiver", intentList.get(1).getPackage());
135     }
136 }