• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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.emulator.smoketests.connectivity;
18 
19 import java.io.IOException;
20 import java.net.URL;
21 import java.net.URLConnection;
22 
23 import android.content.Context;
24 import android.net.ConnectivityManager;
25 import android.net.NetworkInfo;
26 import android.test.AndroidTestCase;
27 import android.util.Log;
28 
29 /**
30  * Network connectivity testcases.
31  */
32 public class ConnectivityTest extends AndroidTestCase {
33 
34     private ConnectivityManager connectivity;
35 
36     //Connection attempt will be made to google.com
37     private static final String  URL_NAME = "http://www.google.com";
38 
39     @Override
setUp()40     protected void setUp() throws Exception {
41         connectivity = (ConnectivityManager) getContext().
42             getSystemService(Context.CONNECTIVITY_SERVICE);
43         assertNotNull(connectivity);
44     }
45 
46     /**
47      * Test that there is an active network
48      */
testActiveConnectivity()49     public void testActiveConnectivity() {
50         NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();
51         Log.d("ConnectivityTest", "validating active networks.");
52         assertNotNull(networkInfo);
53         assertEquals( NetworkInfo.State.CONNECTED, networkInfo.getState());
54     }
55 
56     /**
57      * Test that a connection can be made over the active network
58      */
testConnectionCreation()59     public void testConnectionCreation() throws IOException {
60         URL url = new URL(URL_NAME);
61         Log.d("ConnectivityTest", "creating HTTP connection to google.com.");
62         URLConnection connection = url.openConnection();
63         connection.connect();
64     }
65 
66 }
67