Emulator Connectivity Test

The test checks if there is an activity connectivity and if so can a connect be made. The test was verified again the following scenarios:
- airplane mode on (FAIL)
- wifi on only (PASS)
- data on only (PASS)
- radio on but data and wifi off (FAIL)
- data and wifi on (PASS)

Change-Id: I3c400280390f00f52bdc73c8a685f33322d821eb
This commit is contained in:
David Hu
2011-06-24 12:18:21 -07:00
parent 043c059459
commit a69cb1fd38
3 changed files with 123 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
# Copyright (C) 2011 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
# Only compile source java files in this apk.
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_PACKAGE_NAME := ConnectivityTest
LOCAL_SDK_VERSION := 4
include $(BUILD_PACKAGE)
# Use the following include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2011 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.emulator.connectivity.test"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk android:minSdkVersion="4" />
<instrumentation android:targetPackage="com.android.emulator.connectivity.test" android:name="android.test.InstrumentationTestRunner" />
<application android:label="Connectivity Test">
<uses-library android:name="android.test.runner" />
</application>
</manifest>

View File

@@ -0,0 +1,66 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.emulator.connectivity.test;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.test.AndroidTestCase;
import android.util.Log;
/**
* Network connectivity testcases.
*/
public class ConnectivityTest extends AndroidTestCase {
private ConnectivityManager connectivity;
//Connection attempt will be made to google.com
private static final String URL_NAME = "http://www.google.com";
@Override
protected void setUp() throws Exception {
connectivity = (ConnectivityManager) getContext().
getSystemService(Context.CONNECTIVITY_SERVICE);
assertNotNull(connectivity);
}
/**
* Test that there is an active network
*/
public void testActiveConnectivity() {
NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();
Log.d("ConnectivityTest", "validating active networks.");
assertNotNull(networkInfo);
assertEquals( NetworkInfo.State.CONNECTED, networkInfo.getState());
}
/**
* Test that a connection can be made over the active network
*/
public void testConnectionCreation() throws IOException {
URL url = new URL(URL_NAME);
Log.d("ConnectivityTest", "creating HTTP connection to google.com.");
URLConnection connection = url.openConnection();
connection.connect();
}
}