diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2009-01-09 17:51:23 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2009-01-09 17:51:23 -0800 |
commit | b798689749c64baba81f02e10cf2157c747d6b46 (patch) | |
tree | da394a395ddb1a6cf69193314846b03fe47a397e /tests/FrameworkTest | |
parent | f013e1afd1e68af5e3b868c26a653bbfb39538f8 (diff) | |
download | frameworks_base-b798689749c64baba81f02e10cf2157c747d6b46.zip frameworks_base-b798689749c64baba81f02e10cf2157c747d6b46.tar.gz frameworks_base-b798689749c64baba81f02e10cf2157c747d6b46.tar.bz2 |
auto import from //branches/cupcake/...@125939
Diffstat (limited to 'tests/FrameworkTest')
5 files changed, 363 insertions, 15 deletions
diff --git a/tests/FrameworkTest/AndroidManifest.xml b/tests/FrameworkTest/AndroidManifest.xml index 9913bfc..8106a50 100644 --- a/tests/FrameworkTest/AndroidManifest.xml +++ b/tests/FrameworkTest/AndroidManifest.xml @@ -923,6 +923,14 @@ <category android:name="android.intent.category.FRAMEWORK_INSTRUMENTATION_TEST" /> </intent-filter> </activity> + + <activity android:name="android.widget.AutoCompleteTextViewSimple" + android:label="AutoCompleteTextViewSimple"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.FRAMEWORK_INSTRUMENTATION_TEST" /> + </intent-filter> + </activity> </application> diff --git a/tests/FrameworkTest/res/layout/autocompletetextview_simple.xml b/tests/FrameworkTest/res/layout/autocompletetextview_simple.xml new file mode 100644 index 0000000..d408a86 --- /dev/null +++ b/tests/FrameworkTest/res/layout/autocompletetextview_simple.xml @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +/* //device/samples/SampleCode/res/layout/baseline_1.xml +** +** Copyright 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. +*/ +--> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@+id/layout" + android:orientation="vertical" + android:layout_width="fill_parent" + android:layout_height="fill_parent"> + + <AutoCompleteTextView + android:id="@+id/autocompletetextview1" + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:inputType="text|textAutoComplete" + android:completionThreshold="1" /> + /> + +</LinearLayout> diff --git a/tests/FrameworkTest/src/android/widget/AutoCompleteTextViewSimple.java b/tests/FrameworkTest/src/android/widget/AutoCompleteTextViewSimple.java new file mode 100644 index 0000000..af16cf8 --- /dev/null +++ b/tests/FrameworkTest/src/android/widget/AutoCompleteTextViewSimple.java @@ -0,0 +1,122 @@ +/* + * 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 android.widget; + +import com.android.frameworktest.R; + +import android.app.Activity; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.AdapterView.OnItemClickListener; +import android.widget.AdapterView.OnItemSelectedListener; + +public class AutoCompleteTextViewSimple extends Activity + implements OnItemClickListener, OnItemSelectedListener { + + private final String LOG_TAG = "AutoCompleteTextViewSimple"; + + private AutoCompleteTextView mTextView; + + /** These are cleared by resetItemListeners(), and set by the callback listeners */ + public boolean mItemClickCalled; + public int mItemClickPosition; + public boolean mItemSelectedCalled; + public int mItemSelectedPosition; + public boolean mNothingSelectedCalled; + + @Override + protected void onCreate(Bundle icicle) + { + // Be sure to call the super class. + super.onCreate(icicle); + + // setup layout & views + setContentView(R.layout.autocompletetextview_simple); + mTextView = (AutoCompleteTextView) findViewById(R.id.autocompletetextview1); + + // configure callbacks used for monitoring + mTextView.setOnItemClickListener(this); + mTextView.setOnItemSelectedListener(this); + resetItemListeners(); + + setStringAdapter(5, "a"); + } + + /** + * @return The AutoCompleteTextView used in this test activity. + */ + public AutoCompleteTextView getTextView() { + return mTextView; + } + + /** + * Set the autocomplete data to an adapter containing 0..n strings with a consistent prefix. + */ + public void setStringAdapter(int numSuggestions, String prefix) { + // generate the string array + String[] strings = new String[numSuggestions]; + for (int i = 0; i < numSuggestions; ++i) { + strings[i] = prefix + String.valueOf(i); + } + + // install it with an adapter + ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, + android.R.layout.simple_dropdown_item_1line, strings); + mTextView.setAdapter(adapter); + } + + /** + * For monitoring OnItemClickListener & OnItemSelectedListener + * + * An alternative here would be to provide a set of pass-through callbacks + */ + public void resetItemListeners() { + mItemClickCalled = false; + mItemClickPosition = -1; + mItemSelectedCalled = false; + mItemSelectedPosition = -1; + mNothingSelectedCalled = false; + } + + /** + * Implements OnItemClickListener + */ + public void onItemClick(AdapterView<?> parent, View view, int position, long id) { + Log.d(LOG_TAG, "onItemClick() position " + position); + mItemClickCalled = true; + mItemClickPosition = position; + } + + /** + * Implements OnItemSelectedListener + */ + public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { + Log.d(LOG_TAG, "onItemSelected() position " + position); + mItemSelectedCalled = true; + mItemSelectedPosition = position; + } + + /** + * Implements OnItemSelectedListener + */ + public void onNothingSelected(AdapterView<?> parent) { + Log.d(LOG_TAG, "onNothingSelected()"); + mNothingSelectedCalled = true; + } + +} diff --git a/tests/FrameworkTest/tests/src/android/widget/AutoCompleteTextViewCallbacks.java b/tests/FrameworkTest/tests/src/android/widget/AutoCompleteTextViewCallbacks.java new file mode 100644 index 0000000..93cb84a --- /dev/null +++ b/tests/FrameworkTest/tests/src/android/widget/AutoCompleteTextViewCallbacks.java @@ -0,0 +1,110 @@ +/* + * 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 android.widget; + +import android.app.Instrumentation; +import android.test.ActivityInstrumentationTestCase2; +import android.test.suitebuilder.annotation.MediumTest; + +public class AutoCompleteTextViewCallbacks + extends ActivityInstrumentationTestCase2<AutoCompleteTextViewSimple> { + + public AutoCompleteTextViewCallbacks() { + super("com.android.frameworktest", AutoCompleteTextViewSimple.class); + } + + /** Test that the initial popup of the suggestions does not select anything */ + @MediumTest + public void testPopupNoSelection() { + AutoCompleteTextViewSimple theActivity = getActivity(); + AutoCompleteTextView textView = theActivity.getTextView(); + final Instrumentation instrumentation = getInstrumentation(); + + // focus and type + textView.requestFocus(); + instrumentation.waitForIdleSync(); + sendKeys("A"); + + // now check for selection callbacks. Nothing should be clicked or selected. + assertFalse("onItemClick should not be called", theActivity.mItemClickCalled); + assertFalse("onItemSelected should not be called", theActivity.mItemSelectedCalled); + + // arguably, this should be "false", because we aren't deselecting - we shouldn't + // really be calling it. But it's not the end of the world, and we might wind up + // breaking something if we change this. + assertTrue("onNothingSelected should be called", theActivity.mNothingSelectedCalled); + } + + /** Test that arrow-down into the popup calls the onSelected callback */ + @MediumTest + public void testPopupEnterSelection() { + AutoCompleteTextViewSimple theActivity = getActivity(); + AutoCompleteTextView textView = theActivity.getTextView(); + final Instrumentation instrumentation = getInstrumentation(); + + // focus and type + textView.requestFocus(); + instrumentation.waitForIdleSync(); + sendKeys("A"); + + // prepare to move down into the popup + theActivity.resetItemListeners(); + sendKeys("DPAD_DOWN"); + + // now check for selection callbacks. + assertFalse("onItemClick should not be called", theActivity.mItemClickCalled); + assertTrue("onItemSelected should be called", theActivity.mItemSelectedCalled); + assertEquals("onItemSelected position", 0, theActivity.mItemSelectedPosition); + assertFalse("onNothingSelected should not be called", theActivity.mNothingSelectedCalled); + + // try one more time - should move from 0 to 1 + theActivity.resetItemListeners(); + sendKeys("DPAD_DOWN"); + + // now check for selection callbacks. + assertFalse("onItemClick should not be called", theActivity.mItemClickCalled); + assertTrue("onItemSelected should be called", theActivity.mItemSelectedCalled); + assertEquals("onItemSelected position", 1, theActivity.mItemSelectedPosition); + assertFalse("onNothingSelected should not be called", theActivity.mNothingSelectedCalled); + } + + /** Test that arrow-up out of the popup calls the onNothingSelected callback */ + @MediumTest + public void testPopupLeaveSelection() { + AutoCompleteTextViewSimple theActivity = getActivity(); + AutoCompleteTextView textView = theActivity.getTextView(); + final Instrumentation instrumentation = getInstrumentation(); + + // focus and type + textView.requestFocus(); + instrumentation.waitForIdleSync(); + sendKeys("A"); + + // move down into the popup + sendKeys("DPAD_DOWN"); + + // now move back up out of the popup + theActivity.resetItemListeners(); + sendKeys("DPAD_UP"); + + // now check for selection callbacks. + assertFalse("onItemClick should not be called", theActivity.mItemClickCalled); + assertFalse("onItemSelected should not be called", theActivity.mItemSelectedCalled); + assertTrue("onNothingSelected should be called", theActivity.mNothingSelectedCalled); + } + +} diff --git a/tests/FrameworkTest/tests/src/android/widget/SimpleCursorAdapterTest.java b/tests/FrameworkTest/tests/src/android/widget/SimpleCursorAdapterTest.java index 1aa37c5..58f4ccb 100644 --- a/tests/FrameworkTest/tests/src/android/widget/SimpleCursorAdapterTest.java +++ b/tests/FrameworkTest/tests/src/android/widget/SimpleCursorAdapterTest.java @@ -16,16 +16,16 @@ package android.widget; -import java.util.ArrayList; -import java.util.Random; +import com.android.internal.database.ArrayListCursor; +import com.google.android.collect.Lists; import android.content.Context; import android.database.Cursor; -import android.test.InstrumentationTestCase; -import android.test.suitebuilder.annotation.MediumTest; +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.SmallTest; -import com.android.internal.database.ArrayListCursor; -import com.google.android.collect.Lists; +import java.util.ArrayList; +import java.util.Random; /** * This is a series of tests of basic API contracts for SimpleCursorAdapter. It is @@ -34,7 +34,7 @@ import com.google.android.collect.Lists; * NOTE: This contract holds for underlying cursor types too and these should * be extracted into a set of tests that can be run on any descendant of CursorAdapter. */ -public class SimpleCursorAdapterTest extends InstrumentationTestCase { +public class SimpleCursorAdapterTest extends AndroidTestCase { String[] mFrom; int[] mTo; @@ -55,7 +55,7 @@ public class SimpleCursorAdapterTest extends InstrumentationTestCase { mFrom = new String[]{"Column1", "Column2"}; mTo = new int[]{com.android.internal.R.id.text1, com.android.internal.R.id.text2}; mLayout = com.android.internal.R.layout.simple_list_item_2; - mContext = getInstrumentation().getTargetContext(); + mContext = getContext(); // raw data for building a basic test cursor mData2x2 = createTestList(2, 2); @@ -65,7 +65,7 @@ public class SimpleCursorAdapterTest extends InstrumentationTestCase { /** * Borrowed from CursorWindowTest.java */ - private static ArrayList<ArrayList> createTestList(int rows, int cols) { + private ArrayList<ArrayList> createTestList(int rows, int cols) { ArrayList<ArrayList> list = Lists.newArrayList(); Random generator = new Random(); @@ -84,7 +84,7 @@ public class SimpleCursorAdapterTest extends InstrumentationTestCase { /** * Test creating with a live cursor */ - @MediumTest + @SmallTest public void testCreateLive() { SimpleCursorAdapter ca = new SimpleCursorAdapter(mContext, mLayout, mCursor2x2, mFrom, mTo); @@ -95,7 +95,7 @@ public class SimpleCursorAdapterTest extends InstrumentationTestCase { /** * Test creating with a null cursor */ - @MediumTest + @SmallTest public void testCreateNull() { SimpleCursorAdapter ca = new SimpleCursorAdapter(mContext, mLayout, null, mFrom, mTo); @@ -106,7 +106,7 @@ public class SimpleCursorAdapterTest extends InstrumentationTestCase { /** * Test changeCursor() with live cursor */ - @MediumTest + @SmallTest public void testChangeCursorLive() { SimpleCursorAdapter ca = new SimpleCursorAdapter(mContext, mLayout, mCursor2x2, mFrom, mTo); @@ -125,7 +125,7 @@ public class SimpleCursorAdapterTest extends InstrumentationTestCase { /** * Test changeCursor() with null cursor */ - @MediumTest + @SmallTest public void testChangeCursorNull() { SimpleCursorAdapter ca = new SimpleCursorAdapter(mContext, mLayout, mCursor2x2, mFrom, mTo); @@ -144,7 +144,7 @@ public class SimpleCursorAdapterTest extends InstrumentationTestCase { * deal with cursors that have the same essential data (as defined by the original mFrom * array) but it's OK if the physical structure of the cursor changes (columns rearranged). */ - @MediumTest + @SmallTest public void testChangeCursorColumns() { TestSimpleCursorAdapter ca = new TestSimpleCursorAdapter(mContext, mLayout, mCursor2x2, mFrom, mTo); @@ -167,11 +167,81 @@ public class SimpleCursorAdapterTest extends InstrumentationTestCase { } /** + * Test that you can safely construct with a null cursor *and* null to/from arrays. + * This is new functionality added in 12/2008. + */ + @SmallTest + public void testNullConstructor() { + SimpleCursorAdapter ca = new SimpleCursorAdapter(mContext, mLayout, null, null, null); + assertEquals(0, ca.getCount()); + } + + /** + * Test going from a null cursor to a non-null cursor *and* setting the to/from arrays + * This is new functionality added in 12/2008. + */ + @SmallTest + public void testChangeNullToMapped() { + TestSimpleCursorAdapter ca = new TestSimpleCursorAdapter(mContext, mLayout, null, null, null); + assertEquals(0, ca.getCount()); + + ca.changeCursorAndColumns(mCursor2x2, mFrom, mTo); + assertEquals(2, ca.getCount()); + + // check columns of original - mFrom and mTo should line up + int[] columns = ca.getConvertedFrom(); + assertEquals(2, columns.length); + assertEquals(0, columns[0]); + assertEquals(1, columns[1]); + int[] viewIds = ca.getTo(); + assertEquals(2, viewIds.length); + assertEquals(com.android.internal.R.id.text1, viewIds[0]); + assertEquals(com.android.internal.R.id.text2, viewIds[1]); + } + + /** + * Test going from one mapping to a different mapping + * This is new functionality added in 12/2008. + */ + @SmallTest + public void testChangeMapping() { + TestSimpleCursorAdapter ca = new TestSimpleCursorAdapter(mContext, mLayout, mCursor2x2, + mFrom, mTo); + assertEquals(2, ca.getCount()); + + // Now create a new configuration with same cursor and just one column mapped + String[] singleFrom = new String[]{"Column1"}; + int[] singleTo = new int[]{com.android.internal.R.id.text1}; + ca.changeCursorAndColumns(mCursor2x2, singleFrom, singleTo); + + // And examine the results, make sure they're still consistent + int[] columns = ca.getConvertedFrom(); + assertEquals(1, columns.length); + assertEquals(0, columns[0]); + int[] viewIds = ca.getTo(); + assertEquals(1, viewIds.length); + assertEquals(com.android.internal.R.id.text1, viewIds[0]); + + // And again, same cursor, different map + singleFrom = new String[]{"Column2"}; + singleTo = new int[]{com.android.internal.R.id.text2}; + ca.changeCursorAndColumns(mCursor2x2, singleFrom, singleTo); + + // And examine the results, make sure they're still consistent + columns = ca.getConvertedFrom(); + assertEquals(1, columns.length); + assertEquals(1, columns[0]); + viewIds = ca.getTo(); + assertEquals(1, viewIds.length); + assertEquals(com.android.internal.R.id.text2, viewIds[0]); + } + + /** * This is simply a way to sneak a look at the protected mFrom() array. A more API- * friendly way to do this would be to mock out a View and a ViewBinder and exercise * it via those seams. */ - private class TestSimpleCursorAdapter extends SimpleCursorAdapter { + private static class TestSimpleCursorAdapter extends SimpleCursorAdapter { public TestSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { @@ -181,5 +251,9 @@ public class SimpleCursorAdapterTest extends InstrumentationTestCase { int[] getConvertedFrom() { return mFrom; } + + int[] getTo() { + return mTo; + } } } |