Initial Contribution

This commit is contained in:
The Android Open Source Project
2008-10-21 07:00:00 -07:00
commit 5c11852110
2143 changed files with 253519 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
/*
* Copyright (C) 2008 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.apis;
import junit.framework.Test;
import junit.framework.TestSuite;
import android.test.suitebuilder.TestSuiteBuilder;
/**
* A test suite containing all tests for ApiDemos.
*
* To run all suites found in this apk:
* $ adb shell am instrument -w \
* com.example.android.apis.tests/android.test.InstrumentationTestRunner
*
* To run just this suite from the command line:
* $ adb shell am instrument -w \
* -e class com.example.android.apis.AllTests \
* com.example.android.apis.tests/android.test.InstrumentationTestRunner
*
* To run an individual test case, e.g. {@link com.example.android.apis.os.MorseCodeConverterTest}:
* $ adb shell am instrument -w \
* -e class com.example.android.apis.os.MorseCodeConverterTest \
* com.example.android.apis.tests/android.test.InstrumentationTestRunner
*
* To run an individual test, e.g. {@link com.example.android.apis.os.MorseCodeConverterTest#testCharacterS()}:
* $ adb shell am instrument -w \
* -e class com.example.android.apis.os.MorseCodeConverterTest#testCharacterS \
* com.example.android.apis.tests/android.test.InstrumentationTestRunner
*/
public class AllTests extends TestSuite {
public static Test suite() {
return new TestSuiteBuilder(AllTests.class)
.includeAllPackagesUnderHere()
.build();
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright (C) 2008 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.apis;
import android.test.ApplicationTestCase;
/**
* This is a simple framework for a test of an Application. See
* {@link android.test.ApplicationTestCase ApplicationTestCase} for more information on
* how to write and extend Application tests.
*
* To run this test, you can type:
* adb shell am instrument -w \
* -e class com.example.android.apis.ApiDemosApplicationTests \
* com.example.android.apis.tests/android.test.InstrumentationTestRunner
*/
public class ApiDemosApplicationTests extends ApplicationTestCase<ApiDemosApplication> {
public ApiDemosApplicationTests() {
super(ApiDemosApplication.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
}
/**
* The name 'test preconditions' is a convention to signal that if this
* test doesn't pass, the test case was not set up properly and it might
* explain any and all failures in other tests. This is not guaranteed
* to run before other tests, as junit uses reflection to find the tests.
*/
public void testPreconditions() {
}
/**
* Test basic startup/shutdown of Application
*/
public void testSimpleCreate() {
createApplication();
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright (C) 2008 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.apis;
import android.test.ActivityInstrumentationTestCase;
/**
* Make sure that the main launcher activity opens up properly, which will be
* verified by {@link ActivityInstrumentationTestCase#testActivityTestCaseSetUpProperly}.
*/
public class ApiDemosTest extends ActivityInstrumentationTestCase<ApiDemos> {
public ApiDemosTest() {
super("com.example.android.apis", ApiDemos.class);
}
}

View File

@@ -0,0 +1,125 @@
/*
* Copyright (C) 2008 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.apis.app;
import com.example.android.apis.R;
import com.example.android.apis.view.Focus2ActivityTest;
import android.content.Context;
import android.content.Intent;
import android.test.ActivityUnitTestCase;
import android.widget.Button;
/**
* This demonstrates completely isolated "unit test" of an Activity class.
*
* <p>This model for testing creates the entire Activity (like {@link Focus2ActivityTest}) but does
* not attach it to the system (for example, it cannot launch another Activity). It allows you to
* inject additional behaviors via the
* {@link android.test.ActivityUnitTestCase#setActivityContext(Context)} and
* {@link android.test.ActivityUnitTestCase#setApplication(android.app.Application)} methods.
* It also allows you to more carefully test your Activity's performance
* Writing unit tests in this manner requires more care and attention, but allows you to test
* very specific behaviors, and can also be an easier way to test error conditions.
*
* <p>Because ActivityUnitTestCase creates the Activity under test completely outside of
* the usual system, tests of layout and point-click UI interaction are much less useful
* in this configuration. It's more useful here to concentrate on tests that involve the
* underlying data model, internal business logic, or exercising your Activity's life cycle.
*
* <p>See {@link com.example.android.apis.AllTests} for documentation on running
* all tests and individual tests in this application.
*/
public class ForwardingTest extends ActivityUnitTestCase<Forwarding> {
private Intent mStartIntent;
private Button mButton;
public ForwardingTest() {
super(Forwarding.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
// In setUp, you can create any shared test data, or set up mock components to inject
// into your Activity. But do not call startActivity() until the actual test methods.
mStartIntent = new Intent(Intent.ACTION_MAIN);
}
/**
* The name 'test preconditions' is a convention to signal that if this
* test doesn't pass, the test case was not set up properly and it might
* explain any and all failures in other tests. This is not guaranteed
* to run before other tests, as junit uses reflection to find the tests.
*/
public void testPreconditions() {
startActivity(mStartIntent, null, null);
mButton = (Button) getActivity().findViewById(R.id.go);
assertNotNull(getActivity());
assertNotNull(mButton);
}
/**
* This test demonstrates examining the way that activity calls startActivity() to launch
* other activities.
*/
public void testSubLaunch() {
Forwarding activity = startActivity(mStartIntent, null, null);
mButton = (Button) activity.findViewById(R.id.go);
// This test confirms that when you click the button, the activity attempts to open
// another activity (by calling startActivity) and close itself (by calling finish()).
mButton.performClick();
assertNotNull(getStartedActivityIntent());
assertTrue(isFinishCalled());
}
/**
* This test demonstrates ways to exercise the Activity's life cycle.
*/
public void testLifeCycleCreate() {
Forwarding activity = startActivity(mStartIntent, null, null);
// At this point, onCreate() has been called, but nothing else
// Complete the startup of the activity
getInstrumentation().callActivityOnStart(activity);
getInstrumentation().callActivityOnResume(activity);
// At this point you could test for various configuration aspects, or you could
// use a Mock Context to confirm that your activity has made certain calls to the system
// and set itself up properly.
getInstrumentation().callActivityOnPause(activity);
// At this point you could confirm that the activity has paused properly, as if it is
// no longer the topmost activity on screen.
getInstrumentation().callActivityOnStop(activity);
// At this point, you could confirm that the activity has shut itself down appropriately,
// or you could use a Mock Context to confirm that your activity has released any system
// resources it should no longer be holding.
// ActivityUnitTestCase.tearDown(), which is always automatically called, will take care
// of calling onDestroy().
}
}

View File

@@ -0,0 +1,75 @@
/*
* Copyright (C) 2008 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.apis.app;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.test.MoreAsserts;
import android.test.ServiceTestCase;
/**
* This is a simple framework for a test of a Service. See {@link android.test.ServiceTestCase
* ServiceTestCase} for more information on how to write and extend service tests.
*
* To run this test, you can type:
* adb shell am instrument -w \
* -e class com.example.android.apis.app.LocalServiceTest \
* com.example.android.apis.tests/android.test.InstrumentationTestRunner
*/
public class LocalServiceTest extends ServiceTestCase<LocalService> {
public LocalServiceTest() {
super(LocalService.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
}
/**
* The name 'test preconditions' is a convention to signal that if this
* test doesn't pass, the test case was not set up properly and it might
* explain any and all failures in other tests. This is not guaranteed
* to run before other tests, as junit uses reflection to find the tests.
*/
public void testPreconditions() {
}
/**
* Test basic startup/shutdown of Service
*/
public void testStartable() {
Intent startIntent = new Intent();
startIntent.setClass(getContext(), LocalService.class);
startService(startIntent);
}
/**
* Test binding to service
*/
public void testBindable() {
Intent startIntent = new Intent();
startIntent.setClass(getContext(), LocalService.class);
IBinder service = bindService(startIntent);
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright (C) 2008 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.apis.os;
import junit.framework.TestCase;
/**
* An example of a true unit test that tests the utility class {@link MorseCodeConverter}.
* Since this test doesn't need a {@link android.content.Context}, or any other
* dependencies injected, it simply extends the standard {@link TestCase}.
*
* See {@link com.example.android.apis.AllTests} for documentation on running
* all tests and individual tests in this application.
*/
public class MorseCodeConverterTest extends TestCase {
public void testCharacterS() throws Exception {
long[] expectedBeeps = {
MorseCodeConverter.DOT,
MorseCodeConverter.DOT,
MorseCodeConverter.DOT,
MorseCodeConverter.DOT,
MorseCodeConverter.DOT};
long[] beeps = MorseCodeConverter.pattern('s');
assertArraysEqual(expectedBeeps, beeps);
}
private void assertArraysEqual(long[] expected, long[] actual) {
assertEquals("Unexpected array length.", expected.length, actual.length);
for (int i = 0; i < expected.length; i++) {
long expectedLong = expected[i];
long actualLong = actual[i];
assertEquals("Unexpected long at index: " + i, expectedLong, actualLong);
}
}
}

View File

@@ -0,0 +1,103 @@
/*
* Copyright (C) 2008 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.apis.view;
import com.example.android.apis.R;
import android.test.ActivityInstrumentationTestCase;
import android.view.KeyEvent;
import android.widget.Button;
/**
* An example of an {@link ActivityInstrumentationTestCase} of a specific activity {@link Focus2}.
* By virtue of extending {@link ActivityInstrumentationTestCase}, the target activity is automatically
* launched and finished before and after each test. This also extends
* {@link android.test.InstrumentationTestCase}, which provides
* access to methods for sending events to the target activity, such as key and
* touch events. See {@link #sendKeys}.
*
* In general, {@link android.test.InstrumentationTestCase}s and {@link ActivityInstrumentationTestCase}s
* are heavier weight functional tests available for end to end testing of your
* user interface. When run via a {@link android.test.InstrumentationTestRunner},
* the necessary {@link android.app.Instrumentation} will be injected for you to
* user via {@link #getInstrumentation} in your tests.
*
* See {@link com.example.android.apis.app.ForwardingTest} for an example of an Activity unit test.
*
* See {@link com.example.android.apis.AllTests} for documentation on running
* all tests and individual tests in this application.
*/
public class Focus2ActivityTest extends ActivityInstrumentationTestCase<Focus2> {
private Button mLeftButton;
private Button mCenterButton;
private Button mRightButton;
public Focus2ActivityTest() {
super("com.example.android.apis", Focus2.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
final Focus2 a = getActivity();
mLeftButton = (Button) a.findViewById(R.id.leftButton);
mCenterButton = (Button) a.findViewById(R.id.centerButton);
mRightButton = (Button) a.findViewById(R.id.rightButton);
}
/**
* The name 'test preconditions' is a convention to signal that if this
* test doesn't pass, the test case was not set up properly and it might
* explain any and all failures in other tests. This is not guaranteed
* to run before other tests, as junit uses reflection to find the tests.
*/
public void testPreconditions() {
assertTrue("center button should be right of left button",
mLeftButton.getRight() < mCenterButton.getLeft());
assertTrue("right button should be right of center button",
mCenterButton.getRight() < mRightButton.getLeft());
assertTrue("left button should be focused", mLeftButton.isFocused());
}
public void testGoingRightFromLeftButtonJumpsOverCenterToRight() {
sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
assertTrue("right button should be focused", mRightButton.isFocused());
}
public void testGoingLeftFromRightButtonGoesToCenter() {
// Give right button focus by having it request focus. We post it
// to the UI thread because we are not running on the same thread, and
// any direct api calls that change state must be made from the UI thread.
// This is in contrast to instrumentation calls that send events that are
// processed through the framework and eventually find their way to
// affecting the ui thread.
getActivity().runOnUiThread(new Runnable() {
public void run() {
mRightButton.requestFocus();
}
});
// wait for the request to go through
getInstrumentation().waitForIdleSync();
assertTrue(mRightButton.isFocused());
sendKeys(KeyEvent.KEYCODE_DPAD_LEFT);
assertTrue("center button should be focused", mCenterButton.isFocused());
}
}

View File

@@ -0,0 +1,109 @@
/*
* Copyright (C) 2008 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.apis.view;
import com.example.android.apis.R;
import android.content.Context;
import android.test.AndroidTestCase;
import android.view.FocusFinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* This exercises the same logic as {@link Focus2ActivityTest} but in a lighter
* weight manner; it doesn't need to launch the activity, and it can test the
* focus behavior by calling {@link FocusFinder} methods directly.
*
* {@link Focus2ActivityTest} is still useful to verify that, at an end to end
* level, key events actually translate to focus transitioning in the way we expect.
* A good complementary way to use both types of tests might be to have more exhaustive
* coverage in the lighter weight test case, and a few end to end scenarios in the
* functional {@link android.test.ActivityInstrumentationTestCase}. This would provide reasonable
* assurance that the end to end system is working, while avoiding the overhead of
* having every corner case exercised in the slower, heavier weight way.
*
* Even as a lighter weight test, this test still needs access to a {@link Context}
* to inflate the file, which is why it extends {@link AndroidTestCase}.
*
* If you ever need a context to do your work in tests, you can extend
* {@link AndroidTestCase}, and when run via an {@link android.test.InstrumentationTestRunner},
* the context will be injected for you.
*
* See {@link com.example.android.apis.app.ForwardingTest} for an example of an Activity unit test.
*
* See {@link com.example.android.apis.AllTests} for documentation on running
* all tests and individual tests in this application.
*/
public class Focus2AndroidTest extends AndroidTestCase {
private FocusFinder mFocusFinder;
private ViewGroup mRoot;
private Button mLeftButton;
private Button mCenterButton;
private Button mRightButton;
@Override
protected void setUp() throws Exception {
super.setUp();
mFocusFinder = FocusFinder.getInstance();
// inflate the layout
final Context context = getContext();
final LayoutInflater inflater = LayoutInflater.from(context);
mRoot = (ViewGroup) inflater.inflate(R.layout.focus_2, null);
// manually measure it, and lay it out
mRoot.measure(500, 500);
mRoot.layout(0, 0, 500, 500);
mLeftButton = (Button) mRoot.findViewById(R.id.leftButton);
mCenterButton = (Button) mRoot.findViewById(R.id.centerButton);
mRightButton = (Button) mRoot.findViewById(R.id.rightButton);
}
/**
* The name 'test preconditions' is a convention to signal that if this
* test doesn't pass, the test case was not set up properly and it might
* explain any and all failures in other tests. This is not guaranteed
* to run before other tests, as junit uses reflection to find the tests.
*/
public void testPreconditions() {
assertNotNull(mLeftButton);
assertTrue("center button should be right of left button",
mLeftButton.getRight() < mCenterButton.getLeft());
assertTrue("right button should be right of center button",
mCenterButton.getRight() < mRightButton.getLeft());
}
public void testGoingRightFromLeftButtonJumpsOverCenterToRight() {
assertEquals("right should be next focus from left",
mRightButton,
mFocusFinder.findNextFocus(mRoot, mLeftButton, View.FOCUS_RIGHT));
}
public void testGoingLeftFromRightButtonGoesToCenter() {
assertEquals("center should be next focus from right",
mCenterButton,
mFocusFinder.findNextFocus(mRoot, mRightButton, View.FOCUS_LEFT));
}
}