fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 Google Inc. |
| 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 | package com.android.carrierdefaultapp; |
| 17 | |
| 18 | import android.app.Notification; |
| 19 | import android.app.NotificationManager; |
| 20 | import android.app.PendingIntent; |
| 21 | import android.content.Intent; |
| 22 | import android.os.PersistableBundle; |
| 23 | import android.telephony.CarrierConfigManager; |
| 24 | import android.telephony.Rlog; |
| 25 | import android.telephony.TelephonyManager; |
| 26 | import android.test.InstrumentationTestCase; |
| 27 | |
| 28 | import com.android.internal.telephony.PhoneConstants; |
| 29 | import com.android.internal.telephony.TelephonyIntents; |
| 30 | |
| 31 | import org.junit.After; |
| 32 | import org.junit.Before; |
| 33 | import org.junit.Test; |
| 34 | import org.mockito.ArgumentCaptor; |
| 35 | import org.mockito.Captor; |
| 36 | import org.mockito.Mock; |
| 37 | import org.mockito.MockitoAnnotations; |
| 38 | import static org.mockito.Matchers.eq; |
| 39 | import static org.mockito.Mockito.doReturn; |
| 40 | import static org.mockito.Mockito.times; |
| 41 | import static org.mockito.Mockito.verify; |
| 42 | |
| 43 | public class CarrierDefaultReceiverTest extends InstrumentationTestCase { |
| 44 | @Mock |
| 45 | private NotificationManager mNotificationMgr; |
| 46 | @Mock |
| 47 | private TelephonyManager mTelephonyMgr; |
| 48 | @Mock |
| 49 | private CarrierConfigManager mCarrierConfigMgr; |
| 50 | @Captor |
| 51 | private ArgumentCaptor<Integer> mInt; |
| 52 | @Captor |
| 53 | private ArgumentCaptor<Notification> mNotification; |
| 54 | @Captor |
| 55 | private ArgumentCaptor<String> mString; |
| 56 | private TestContext mContext; |
| 57 | private CarrierDefaultBroadcastReceiver mReceiver; |
| 58 | private static String TAG; |
| 59 | |
| 60 | private static final String PORTAL_NOTIFICATION_TAG = "CarrierDefault.Portal.Notification"; |
| 61 | private static final int PORTAL_NOTIFICATION_ID = 0; |
| 62 | private static int subId = 0; |
| 63 | |
| 64 | @Before |
| 65 | public void setUp() throws Exception { |
| 66 | super.setUp(); |
| 67 | TAG = this.getClass().getSimpleName(); |
| 68 | MockitoAnnotations.initMocks(this); |
| 69 | mContext = new TestContext(getInstrumentation().getTargetContext()); |
| 70 | mContext.injectSystemService(NotificationManager.class, mNotificationMgr); |
| 71 | mContext.injectSystemService(TelephonyManager.class, mTelephonyMgr); |
| 72 | mContext.injectSystemService(CarrierConfigManager.class, mCarrierConfigMgr); |
| 73 | |
| 74 | mReceiver = new CarrierDefaultBroadcastReceiver(); |
| 75 | } |
| 76 | |
| 77 | @After |
| 78 | public void tearDown() throws Exception { |
| 79 | super.tearDown(); |
| 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void testOnReceiveRedirection() { |
| 84 | // carrier action idx list includes 4(portal notification) & 1(disable metered APNs) |
| 85 | // upon redirection signal |
| 86 | PersistableBundle b = new PersistableBundle(); |
| 87 | b.putStringArray(CarrierConfigManager |
| 88 | .KEY_CARRIER_DEFAULT_ACTIONS_ON_REDIRECTION_STRING_ARRAY, new String[]{"4,1"}); |
| 89 | doReturn(b).when(mCarrierConfigMgr).getConfig(); |
| 90 | |
| 91 | Intent intent = new Intent(TelephonyIntents.ACTION_CARRIER_SIGNAL_REDIRECTED); |
| 92 | intent.putExtra(PhoneConstants.SUBSCRIPTION_KEY, subId); |
| 93 | Rlog.d(TAG, "OnReceive redirection intent"); |
| 94 | mReceiver.onReceive(mContext, intent); |
| 95 | |
| 96 | mContext.waitForMs(100); |
| 97 | |
| 98 | Rlog.d(TAG, "verify carrier action: showPortalNotification"); |
| 99 | verify(mNotificationMgr, times(1)).notify(mString.capture(), mInt.capture(), |
| 100 | mNotification.capture()); |
| 101 | assertEquals(PORTAL_NOTIFICATION_ID, (int) mInt.getValue()); |
| 102 | assertEquals(PORTAL_NOTIFICATION_TAG, mString.getValue()); |
| 103 | PendingIntent pendingIntent = mNotification.getValue().contentIntent; |
| 104 | assertNotNull(pendingIntent); |
| 105 | |
| 106 | Rlog.d(TAG, "verify carrier action: disable all metered apns"); |
| 107 | verify(mTelephonyMgr).carrierActionSetMeteredApnsEnabled(eq(subId), eq(false)); |
| 108 | } |
| 109 | } |