Merge "Emulator Connectivity Test"

This commit is contained in:
David Hu
2011-06-29 17:03:34 -07:00
committed by Android (Google) Code Review
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();
}
}