diff options
7 files changed, 70 insertions, 8 deletions
diff --git a/core/java/android/content/ClipboardManager.java b/core/java/android/content/ClipboardManager.java index c28172c6347c..f1c2f342eb3e 100644 --- a/core/java/android/content/ClipboardManager.java +++ b/core/java/android/content/ClipboardManager.java @@ -150,7 +150,7 @@ public class ClipboardManager extends android.text.ClipboardManager { public void addPrimaryClipChangedListener(OnPrimaryClipChangedListener what) { synchronized (mPrimaryClipChangedListeners) { - if (mPrimaryClipChangedListeners.size() == 0) { + if (mPrimaryClipChangedListeners.isEmpty()) { try { mService.addPrimaryClipChangedListener( mPrimaryClipChangedServiceListener, mContext.getOpPackageName()); @@ -165,7 +165,7 @@ public class ClipboardManager extends android.text.ClipboardManager { public void removePrimaryClipChangedListener(OnPrimaryClipChangedListener what) { synchronized (mPrimaryClipChangedListeners) { mPrimaryClipChangedListeners.remove(what); - if (mPrimaryClipChangedListeners.size() == 0) { + if (mPrimaryClipChangedListeners.isEmpty()) { try { mService.removePrimaryClipChangedListener( mPrimaryClipChangedServiceListener); diff --git a/core/java/android/content/ContentProviderOperation.java b/core/java/android/content/ContentProviderOperation.java index fd1e24a48da5..8f3a31746266 100644 --- a/core/java/android/content/ContentProviderOperation.java +++ b/core/java/android/content/ContentProviderOperation.java @@ -508,14 +508,14 @@ public class ContentProviderOperation implements Parcelable { /** Create a ContentProviderOperation from this {@link Builder}. */ public ContentProviderOperation build() { if (mType == TYPE_UPDATE) { - if ((mValues == null || mValues.size() == 0) - && (mValuesBackReferences == null || mValuesBackReferences.size() == 0)) { + if ((mValues == null || mValues.isEmpty()) + && (mValuesBackReferences == null || mValuesBackReferences.isEmpty())) { throw new IllegalArgumentException("Empty values"); } } if (mType == TYPE_ASSERT) { - if ((mValues == null || mValues.size() == 0) - && (mValuesBackReferences == null || mValuesBackReferences.size() == 0) + if ((mValues == null || mValues.isEmpty()) + && (mValuesBackReferences == null || mValuesBackReferences.isEmpty()) && (mExpectedCount == null)) { throw new IllegalArgumentException("Empty values"); } diff --git a/core/java/android/content/ContentValues.java b/core/java/android/content/ContentValues.java index 3a87cb3e7a29..6f9379890a3a 100644 --- a/core/java/android/content/ContentValues.java +++ b/core/java/android/content/ContentValues.java @@ -204,6 +204,17 @@ public final class ContentValues implements Parcelable { } /** + * Indicates whether this collection is empty. + * + * @return true iff size == 0 + * {@hide} + * TODO: consider exposing this new method publicly + */ + public boolean isEmpty() { + return mValues.isEmpty(); + } + + /** * Remove a single value. * * @param key the name of the value to remove diff --git a/core/java/android/database/sqlite/SQLiteDatabase.java b/core/java/android/database/sqlite/SQLiteDatabase.java index 8e178320d66d..fe849b8a99cf 100644 --- a/core/java/android/database/sqlite/SQLiteDatabase.java +++ b/core/java/android/database/sqlite/SQLiteDatabase.java @@ -1449,7 +1449,7 @@ public final class SQLiteDatabase extends SQLiteClosable { sql.append('('); Object[] bindArgs = null; - int size = (initialValues != null && initialValues.size() > 0) + int size = (initialValues != null && !initialValues.isEmpty()) ? initialValues.size() : 0; if (size > 0) { bindArgs = new Object[size]; @@ -1541,7 +1541,7 @@ public final class SQLiteDatabase extends SQLiteClosable { */ public int updateWithOnConflict(String table, ContentValues values, String whereClause, String[] whereArgs, int conflictAlgorithm) { - if (values == null || values.size() == 0) { + if (values == null || values.isEmpty()) { throw new IllegalArgumentException("Empty values"); } diff --git a/core/tests/coretests/README b/core/tests/coretests/README index 4a6984320e61..aced4417ba02 100644 --- a/core/tests/coretests/README +++ b/core/tests/coretests/README @@ -45,6 +45,10 @@ To run tests in debug mode: -e debug true +To uninstall the package: + + adb shell pm uninstall -k com.android.frameworks.coretests + For more arguments, see the guide to command=line testing: https://developer.android.com/studio/test/command-line.html diff --git a/core/tests/coretests/src/android/content/ContentTests.java b/core/tests/coretests/src/android/content/ContentTests.java index a1299e3a0e8d..567b79a674c1 100644 --- a/core/tests/coretests/src/android/content/ContentTests.java +++ b/core/tests/coretests/src/android/content/ContentTests.java @@ -23,6 +23,7 @@ public class ContentTests { TestSuite suite = new TestSuite(ContentTests.class.getName()); suite.addTestSuite(AssetTest.class); + suite.addTestSuite(ContentValuesTest.class); return suite; } } diff --git a/core/tests/coretests/src/android/content/ContentValuesTest.java b/core/tests/coretests/src/android/content/ContentValuesTest.java new file mode 100644 index 000000000000..7b39939b2d0c --- /dev/null +++ b/core/tests/coretests/src/android/content/ContentValuesTest.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2017 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.content; + +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.SmallTest; + + +/* + runtest -c android.content.ContentValuesTest frameworks-core + + or + + make -j256 FrameworksCoreTests && \ + adb shell pm uninstall -k com.android.frameworks.coretests && \ + adb install out/target/product/bullhead/testcases/FrameworksCoreTests/FrameworksCoreTests.apk && \ + adb shell am instrument -w -e package android.content \ + com.android.frameworks.coretests/android.support.test.runner.AndroidJUnitRunner +*/ +public class ContentValuesTest extends AndroidTestCase { + + @SmallTest + public void testIsEmpty() throws Exception { + ContentValues cv = new ContentValues(); + assertTrue(cv.isEmpty()); + assertEquals(0, cv.size()); + + cv.put("key", "value"); + assertFalse(cv.isEmpty()); + assertEquals(1, cv.size()); + } +} |