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 package com.android.server.cts.netstats; 17 18 import android.net.TrafficStats; 19 import android.support.test.runner.AndroidJUnit4; 20 import android.util.Log; 21 22 import org.junit.Test; 23 import org.junit.runner.RunWith; 24 25 import java.net.URL; 26 27 import javax.net.ssl.HttpsURLConnection; 28 29 /** 30 * Used by NetstatsIncidentTest. Makes some network requests so "dumpsys netstats" will have 31 * something to show. 32 */ 33 @RunWith(AndroidJUnit4.class) 34 public class NetstatsDeviceTest { 35 private static final String TAG = "NetstatsDeviceTest"; 36 37 private static final int NET_TAG = 123123123; 38 39 @Test 40 public void testDoNetworkWithoutTagging() throws Exception { 41 Log.i(TAG, "testDoNetworkWithoutTagging"); 42 43 makeNetworkRequest(); 44 } 45 46 @Test 47 public void testDoNetworkWithTagging() throws Exception { 48 Log.i(TAG, "testDoNetworkWithTagging"); 49 50 TrafficStats.getAndSetThreadStatsTag(NET_TAG); 51 makeNetworkRequest(); 52 } 53 54 private void makeNetworkRequest() throws Exception { 55 final URL url = new URL("https://www.android.com/"); 56 final HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection(); 57 HttpsURLConnection.setFollowRedirects(true); 58 try { 59 final int status = urlConnection.getResponseCode(); 60 61 Log.i(TAG, "Response code from " + url + ": " + status); 62 63 // Doesn't matter what response code we got. We touched the network, which is enough. 64 } finally { 65 urlConnection.disconnect(); 66 } 67 } 68 } 69