1 /* 2 * Copyright (C) 2017 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; 18 19 import static junit.framework.Assert.assertEquals; 20 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.PackageManager; 24 import android.net.NetworkTemplate; 25 import android.support.test.InstrumentationRegistry; 26 import android.support.test.filters.SmallTest; 27 import android.support.test.runner.AndroidJUnit4; 28 import android.telephony.SubscriptionManager; 29 import android.telephony.TelephonyManager; 30 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 34 @RunWith(AndroidJUnit4.class) 35 @SmallTest 36 public class MobileDataUsageActivityTest { 37 private static final String TAG = "MobileDataUsageTest"; 38 @Test test_mobileDataUsageIntent()39 public void test_mobileDataUsageIntent() { 40 final Context context = InstrumentationRegistry.getTargetContext(); 41 final PackageManager packageManager = context.getPackageManager(); 42 final int subId = SubscriptionManager.getDefaultSubscriptionId(); 43 final NetworkTemplate template = getNetworkTemplate(context, subId); 44 45 Intent intent = new Intent(android.provider.Settings.ACTION_MOBILE_DATA_USAGE); 46 intent.putExtra(android.provider.Settings.EXTRA_NETWORK_TEMPLATE, template); 47 intent.putExtra(android.provider.Settings.EXTRA_SUB_ID, subId); 48 49 assertEquals(packageManager.queryIntentActivities(intent, 0).size(), 1); 50 51 context.startActivity(intent); 52 // Should exit gracefully without crashing. 53 } 54 getNetworkTemplate(Context context, int subId)55 private NetworkTemplate getNetworkTemplate(Context context, int subId) { 56 TelephonyManager tm = (TelephonyManager) context 57 .getSystemService(Context.TELEPHONY_SERVICE); 58 NetworkTemplate mobileAll = NetworkTemplate.buildTemplateMobileAll( 59 tm.getSubscriberId(subId)); 60 return NetworkTemplate.normalize(mobileAll, 61 tm.getMergedSubscriberIds()); 62 } 63 } 64