From a69cb1fd38793dc9855b65d3e37fda4d939cd51a Mon Sep 17 00:00:00 2001 From: David Hu Date: Fri, 24 Jun 2011 12:18:21 -0700 Subject: [PATCH] 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 --- .../test-apps/ConnectivityTest/Android.mk | 30 +++++++++ .../ConnectivityTest/AndroidManifest.xml | 27 ++++++++ .../connectivity/test/ConnectivityTest.java | 66 +++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 tools/emulator/test-apps/ConnectivityTest/Android.mk create mode 100644 tools/emulator/test-apps/ConnectivityTest/AndroidManifest.xml create mode 100644 tools/emulator/test-apps/ConnectivityTest/src/com/android/emulator/connectivity/test/ConnectivityTest.java diff --git a/tools/emulator/test-apps/ConnectivityTest/Android.mk b/tools/emulator/test-apps/ConnectivityTest/Android.mk new file mode 100644 index 000000000..097d1189a --- /dev/null +++ b/tools/emulator/test-apps/ConnectivityTest/Android.mk @@ -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)) diff --git a/tools/emulator/test-apps/ConnectivityTest/AndroidManifest.xml b/tools/emulator/test-apps/ConnectivityTest/AndroidManifest.xml new file mode 100644 index 000000000..80f65cf80 --- /dev/null +++ b/tools/emulator/test-apps/ConnectivityTest/AndroidManifest.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/tools/emulator/test-apps/ConnectivityTest/src/com/android/emulator/connectivity/test/ConnectivityTest.java b/tools/emulator/test-apps/ConnectivityTest/src/com/android/emulator/connectivity/test/ConnectivityTest.java new file mode 100644 index 000000000..9931eb849 --- /dev/null +++ b/tools/emulator/test-apps/ConnectivityTest/src/com/android/emulator/connectivity/test/ConnectivityTest.java @@ -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(); + } + +}