summaryrefslogtreecommitdiffstats
path: root/samples/Alarm/tests
diff options
context:
space:
mode:
authorJoe Malin <jmalin@google.com>2010-06-05 14:03:42 -0700
committerJoe Malin <jmalin@google.com>2010-08-18 11:39:13 -0700
commit6878d8cf8bdff6f9111a8071db360b5f3e2cb7f1 (patch)
tree145a11fbeeacb94838aeea312eebfa6d9e05b4aa /samples/Alarm/tests
parent1164f90b8ace302a5b6827e27459a6d8be975eb3 (diff)
downloadandroid_development-6878d8cf8bdff6f9111a8071db360b5f3e2cb7f1.tar.gz
android_development-6878d8cf8bdff6f9111a8071db360b5f3e2cb7f1.tar.bz2
android_development-6878d8cf8bdff6f9111a8071db360b5f3e2cb7f1.zip
Sample app and test package for a Service
Change-Id: I6589a1b761f5c40b86f1319c30ead2409113b647
Diffstat (limited to 'samples/Alarm/tests')
-rw-r--r--samples/Alarm/tests/AndroidManifest.xml54
-rw-r--r--samples/Alarm/tests/_index.html50
-rw-r--r--samples/Alarm/tests/src/com/example/android/newalarm/AlarmServiceTest.java86
3 files changed, 190 insertions, 0 deletions
diff --git a/samples/Alarm/tests/AndroidManifest.xml b/samples/Alarm/tests/AndroidManifest.xml
new file mode 100644
index 000000000..868892000
--- /dev/null
+++ b/samples/Alarm/tests/AndroidManifest.xml
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 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.
+-->
+
+<!--
+ Declare the contents of this Android test package. The xmlns:android
+ attribute brings in the Android platform namespace, and the
+ "package" attribute provides a unique name for the package.
+ If you use this file as a template in your own test package, you must change
+ the package name from "com.example.android" to one that you own or have
+ control over.
+ -->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.example.android.newalarm.test"
+ android:versionCode="1"
+ android:versionName="1.0">
+ <!--
+ Must use the application element to include the uses-library element.
+ -->
+ <application>
+ <!--
+ Tells Android to include this library in the test package's class loader.
+ The test runner is not included by default in a manifest file.
+ -->
+ <uses-library android:name="android.test.runner" />
+ </application>
+ <!--
+ Specifies that the test package requires API platform level 3 (Android 1.5) or above.
+ The installer will not install this package onto a device or emulator that is running an
+ older platform version.
+ -->
+ <uses-sdk android:minSdkVersion="3" />
+ <!--
+ Declares the instrumentation for this application. The instrumentation class is
+ specified by the "name" attribute, and must name a subclass of
+ android.app.Instrumentation. The application that is run by the instrumentation object is
+ specified by the "targetPackage" attribute.
+ -->
+ <instrumentation
+ android:targetPackage="com.example.android.newalarm"
+ android:name="android.test.InstrumentationTestRunner" />
+</manifest>
diff --git a/samples/Alarm/tests/_index.html b/samples/Alarm/tests/_index.html
new file mode 100644
index 000000000..614125dd3
--- /dev/null
+++ b/samples/Alarm/tests/_index.html
@@ -0,0 +1,50 @@
+<p>
+ This sample is the test application for the <a href="../Alarm/index.html">Alarm</a>
+ sample application. It tests the application's <code>AlarmService</code> service.
+</p>
+<p>
+ The test application uses the
+ <a href="../../../reference/android/test/ServiceTestCase.html">
+ <code>ServiceTestCase</code></a> test case class,
+ which extends the JUnit <a href="../../../reference/junit/framework/TestCase.html">
+ <code>TestCase</code></a> class. The test runner is
+ <a href="../../../reference/android/test/InstrumentationTestRunner.html">
+ <code>InstrumentationTestRunner</code></a>.
+</p>
+<p>
+ The application shows how to set up a test application project,
+ how to create the <a href="AndroidManifest.html"><code>AndroidManifest.xml</code></a>
+ file for a test application, and how to set up a test case class for a service. The
+ test case class, <a href="src/com/android/example/newalarm/ServiceAlarmTest.html">
+ <code>AlarmServiceTest</code></a>, contains tests that demonstrate the following
+ Android test patterns:
+</p>
+ <ul>
+ <li>
+ Test setup: The <code>setUp()</code> method re-initializes the state of the
+ service under test before each test is run.
+ </li>
+ <li>
+ Service start: The <code>Service.testServiceCreate()</code> test confirms that the
+ service starts correctly and initializes the variables it needs to provide its
+ services.
+ </li>
+ </ul>
+<p>
+ The <a href="AndroidManifest.html">manifest</a> declares an <code>&lt;instrumentation&gt;</code>
+ element that links the test application with the application under test. Specifically, the
+ element's <code>android:name</code> attribute specifies <code>InstrumentationTestRunner</code>
+ as the instrumentation to use. The <code>android:targetPackage</code> attribute specifies
+ <code>com.android.example.newalarm</code> as the name of the Android package that contains the
+ service under test.
+</p>
+<p class="note">
+ <strong>Note:</strong> <code>AlarmServiceTest.java</code> uses the Java package name
+ <code>com.example.android.newalarm</code>, which is the same package used by service under
+ test, <code>AlarmService.java</code>. This allows the test class to access members in the
+ service under test that are defined with package visibility. To prevent conflicts, though,
+ the generated java file <code>R.java</code> for <code>AlarmServiceTest</code> uses the
+ Java package name <code>com.example.android.newalarm.test</code>. For the same reason, the
+ Android package name for the test application (specified in the manifest file), is
+ <code>com.example.android.newalarm.test</code>.
+</p>
diff --git a/samples/Alarm/tests/src/com/example/android/newalarm/AlarmServiceTest.java b/samples/Alarm/tests/src/com/example/android/newalarm/AlarmServiceTest.java
new file mode 100644
index 000000000..4d6c7aded
--- /dev/null
+++ b/samples/Alarm/tests/src/com/example/android/newalarm/AlarmServiceTest.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2010 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.example.android.newalarm;
+
+import android.content.Intent;
+import android.test.ServiceTestCase;
+import com.example.android.newalarm.AlarmService;
+
+/**
+ * Test class for the Alarm sample test package. This test class tests the AlarmService
+ * service component.
+ */
+public class AlarmServiceTest extends ServiceTestCase<AlarmService> {
+ // Contains an Intent used to start the service
+ Intent mStartServiceIntent;
+
+ // Contains a handle to the system alarm service
+ AlarmService mService;
+
+ /**
+ * Constructor for the test class. Test classes that are run by InstrumentationTestRunner
+ * must provide a constructor with no arguments that calls the base class constructor as its
+ * first statement.
+ */
+ public AlarmServiceTest() {
+ super(AlarmService.class);
+ }
+
+ /*
+ * Sets up the test fixture. This method is called before each test
+ */
+ @Override
+ protected void setUp() throws Exception {
+
+ super.setUp();
+
+ // Sets up an intent to start the service under test
+ mStartServiceIntent = new Intent(this.getSystemContext(),AlarmService.class);
+ }
+
+ /**
+ * Cleans up the test fixture
+ * Called after each test method. If you override the method, call super.tearDown() as the
+ * last statement in your override.
+ */
+ @Override
+ protected void tearDown() throws Exception {
+ // Always call the super constructor when overriding tearDown()
+ super.tearDown();
+ }
+
+ /**
+ * Tests the service's onCreate() method. Starts the service using startService(Intent)
+ */
+ public void testServiceCreate() {
+ // Starts the service under test
+ this.startService(mStartServiceIntent);
+
+ // Gets a handle to the service under test.
+ mService = this.getService();
+
+ // Asserts that the Notification Manager was created in the service under test.
+ assertNotNull(mService.mNotificationManager);
+
+ // Asserts that the PendingIntent for the expanded status window was created
+ assertNotNull(mService.mContentIntent);
+
+ // Asserts that the notification was created
+ assertNotNull(mService.mNotification);
+ }
+
+}