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
17package com.android.settings;
18
19import static junit.framework.Assert.assertEquals;
20
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
24import android.net.NetworkTemplate;
25import android.support.test.InstrumentationRegistry;
26import android.support.test.filters.SmallTest;
27import android.support.test.runner.AndroidJUnit4;
28import android.telephony.SubscriptionManager;
29import android.telephony.TelephonyManager;
30
31import org.junit.Test;
32import org.junit.runner.RunWith;
33
34@RunWith(AndroidJUnit4.class)
35@SmallTest
36public class MobileDataUsageActivityTest {
37    private static final String TAG = "MobileDataUsageTest";
38    @Test
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
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