From 256d907060c0080d3404125edf01daecd97d71c0 Mon Sep 17 00:00:00 2001 From: Jeff Sharkey <jsharkey@google.com> Date: Tue, 5 Dec 2023 11:28:28 -0700 Subject: [PATCH] Support more internal utils under Ravenwood. Tests for these internal utils are local within the frameworks/base/ repo, instead of in CTS. To help future-proof, bring the entire utils/* test directory by default into the Ravenwood suites, and give developers the way to easily opt-out with @IgnoreUnderRavenwood at the class level. Bug: 292141694 Test: atest FrameworksCoreTestsRavenwood FrameworksCoreTests Test: atest FrameworksUtilTestsRavenwood FrameworksUtilTests Change-Id: I5251dca752396faa77774659d0923d8cdb719830 --- core/java/android/util/DumpableContainer.java | 1 + .../com/android/internal/os/SomeArgs.java | 1 + .../com/android/internal/util/BitUtils.java | 1 + .../internal/util/BitwiseInputStream.java | 1 + .../internal/util/BitwiseOutputStream.java | 1 + .../internal/util/CallbackRegistry.java | 1 + .../com/android/internal/util/DumpUtils.java | 7 + .../com/android/internal/util/FastMath.java | 1 + .../internal/util/FastPrintWriter.java | 1 + .../internal/util/GrowingArrayUtils.java | 1 + .../internal/util/HeavyHitterSketch.java | 1 + .../util/LineBreakBufferedWriter.java | 1 + .../android/internal/util/ObjectUtils.java | 1 + .../com/android/internal/util/Parcelling.java | 1 + .../com/android/internal/util/ParseUtils.java | 1 + .../android/internal/util/ProcFileReader.java | 1 + .../internal/util/ProgressReporter.java | 4 +- .../android/internal/util/QuickSelect.java | 1 + .../com/android/internal/util/RingBuffer.java | 1 + .../com/android/internal/util/StringPool.java | 1 + .../util/dump/DumpableContainerImpl.java | 1 + .../com/google/android/collect/Lists.java | 1 + .../java/com/google/android/collect/Maps.java | 1 + .../java/com/google/android/collect/Sets.java | 1 + core/tests/coretests/Android.bp | 3 + .../android/internal/util/BitUtilsTest.java | 2 +- .../internal/util/ContrastColorUtilTest.java | 34 ++- .../android/internal/util/DumpUtilsTest.java | 26 +- .../util/DumpableContainerImplTest.java | 20 +- .../internal/util/FakeLatencyTrackerTest.java | 8 +- .../android/internal/util/FastDataTest.java | 12 +- .../android/internal/util/FastMathTest.java | 34 +++ .../internal/util/GrowingArrayUtilsTest.java | 140 ++++++++++ .../android/internal/util/HexDumpTest.java | 22 +- .../internal/util/LatencyTrackerTest.java | 8 +- .../util/LineBreakBufferedWriterTest.java | 28 +- .../android/internal/util/ParseUtilsTest.java | 36 ++- .../internal/util/ProgressReporterTest.java | 28 +- .../android/internal/util/RingBufferTest.java | 2 +- .../internal/util/TokenBucketTest.java | 19 +- core/tests/utiltests/Android.bp | 17 +- .../src/android/util/MemoryIntArrayTest.java | 17 +- .../src/android/util/MetadataReaderTest.java | 16 +- ...SystemConfigFileCommitEventLoggerTest.java | 12 +- .../android/internal/util/ArrayUtilsTest.java | 49 +++- .../internal/util/BitwiseStreamsTest.java | 23 +- .../internal/util/CallbackRegistryTest.java | 26 +- .../internal/util/CharSequencesTest.java | 20 +- .../internal/util/FastXmlSerializerTest.java | 25 +- .../internal/util/FileRotatorTest.java | 47 ++-- .../internal/util/HeavyHitterSketchTest.java | 15 +- .../InlinePresentationStyleUtilsTest.java | 8 + .../internal/util/LockPatternUtilsTest.java | 6 + .../internal/util/MessageUtilsTest.java | 6 +- .../internal/util/MimeIconUtilsTest.java | 19 +- .../internal/util/ObjectUtilsTest.java | 13 +- .../util/ObservableServiceConnectionTest.java | 10 + .../util/PersistentServiceConnectionTest.java | 11 + .../internal/util/ProcFileReaderTest.java | 28 +- .../internal/util/QuickSelectTest.java | 11 +- .../android/internal/util/StringPoolTest.java | 16 +- .../internal/util/WakeupMessageTest.java | 19 +- .../android/internal/util/XmlUtilsTest.java | 11 +- .../util/test/FakeSettingsProviderTest.java | 32 ++- ...ramework-minus-apex-ravenwood-policies.txt | 12 +- .../annotations/IgnoreUnderRavenwood.java | 2 +- .../test/ravenwood/RavenwoodRule.java | 5 +- .../ravenwood-annotation-allowed-classes.txt | 28 ++ .../libcore/util/HexEncoding.java | 240 ++++++++++++++++++ 69 files changed, 1043 insertions(+), 156 deletions(-) create mode 100644 core/tests/coretests/src/com/android/internal/util/FastMathTest.java create mode 100644 core/tests/coretests/src/com/android/internal/util/GrowingArrayUtilsTest.java create mode 100644 tools/hoststubgen/hoststubgen/helper-framework-runtime-src/libcore-fake/libcore/util/HexEncoding.java diff --git a/core/java/android/util/DumpableContainer.java b/core/java/android/util/DumpableContainer.java index fef5acd42e4e..24640c974500 100644 --- a/core/java/android/util/DumpableContainer.java +++ b/core/java/android/util/DumpableContainer.java @@ -20,6 +20,7 @@ import android.annotation.NonNull; /** * Represents a container that manages {@link Dumpable dumpables}. */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public interface DumpableContainer { /** diff --git a/core/java/com/android/internal/os/SomeArgs.java b/core/java/com/android/internal/os/SomeArgs.java index b12df6ed36ca..4701af3f2ddc 100644 --- a/core/java/com/android/internal/os/SomeArgs.java +++ b/core/java/com/android/internal/os/SomeArgs.java @@ -26,6 +26,7 @@ import android.os.Build; * it is responsibility of the client to recycle and instance * once it is no longer used. */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public final class SomeArgs { private static final int MAX_POOL_SIZE = 10; diff --git a/core/java/com/android/internal/util/BitUtils.java b/core/java/com/android/internal/util/BitUtils.java index 154ea52bf9ba..d928bbc3e9ad 100644 --- a/core/java/com/android/internal/util/BitUtils.java +++ b/core/java/com/android/internal/util/BitUtils.java @@ -31,6 +31,7 @@ import java.util.function.IntFunction; * sugar methods for {@link ByteBuffer}. Useful for networking and packet manipulations. * {@hide} */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public final class BitUtils { private BitUtils() {} diff --git a/core/java/com/android/internal/util/BitwiseInputStream.java b/core/java/com/android/internal/util/BitwiseInputStream.java index ffae3ce89ce4..91f89f39fc4d 100644 --- a/core/java/com/android/internal/util/BitwiseInputStream.java +++ b/core/java/com/android/internal/util/BitwiseInputStream.java @@ -26,6 +26,7 @@ import android.compat.annotation.UnsupportedAppUsage; * * NOTE -- This class is not threadsafe. */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public class BitwiseInputStream { // The byte array being read from. diff --git a/core/java/com/android/internal/util/BitwiseOutputStream.java b/core/java/com/android/internal/util/BitwiseOutputStream.java index 9f4150860887..9b7568b54639 100644 --- a/core/java/com/android/internal/util/BitwiseOutputStream.java +++ b/core/java/com/android/internal/util/BitwiseOutputStream.java @@ -26,6 +26,7 @@ import android.compat.annotation.UnsupportedAppUsage; * * NOTE -- This class is not threadsafe. */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public class BitwiseOutputStream { // The byte array being written to, which will be grown as needed. diff --git a/core/java/com/android/internal/util/CallbackRegistry.java b/core/java/com/android/internal/util/CallbackRegistry.java index 0f228d4775fd..0ae8e1d1e0c4 100644 --- a/core/java/com/android/internal/util/CallbackRegistry.java +++ b/core/java/com/android/internal/util/CallbackRegistry.java @@ -39,6 +39,7 @@ import java.util.List; * @param <T> The notification sender type. Typically this is the containing class. * @param <A> Opaque argument used to pass additional data beyond an int. */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public class CallbackRegistry<C, T, A> implements Cloneable { private static final String TAG = "CallbackRegistry"; diff --git a/core/java/com/android/internal/util/DumpUtils.java b/core/java/com/android/internal/util/DumpUtils.java index 8fe2b9cdf1e5..d1ff034ff001 100644 --- a/core/java/com/android/internal/util/DumpUtils.java +++ b/core/java/com/android/internal/util/DumpUtils.java @@ -37,6 +37,7 @@ import java.util.function.Predicate; * Test: atest FrameworksCoreTests:DumpUtilsTest */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public final class DumpUtils { /** @@ -92,6 +93,8 @@ public final class DumpUtils { * @return true if access should be granted. * @hide */ + @android.ravenwood.annotation.RavenwoodThrow( + blockedBy = android.permission.PermissionManager.class) public static boolean checkDumpPermission(Context context, String tag, PrintWriter pw) { if (context.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) { @@ -112,6 +115,8 @@ public final class DumpUtils { * @return true if access should be granted. * @hide */ + @android.ravenwood.annotation.RavenwoodThrow( + blockedBy = android.permission.PermissionManager.class) public static boolean checkUsageStatsPermission(Context context, String tag, PrintWriter pw) { // System internals always get access final int uid = Binder.getCallingUid(); @@ -166,6 +171,8 @@ public final class DumpUtils { * @return true if access should be granted. * @hide */ + @android.ravenwood.annotation.RavenwoodThrow( + blockedBy = android.permission.PermissionManager.class) public static boolean checkDumpAndUsageStatsPermission(Context context, String tag, PrintWriter pw) { return checkDumpPermission(context, tag, pw) && checkUsageStatsPermission(context, tag, pw); diff --git a/core/java/com/android/internal/util/FastMath.java b/core/java/com/android/internal/util/FastMath.java index b7dbee53a4be..6e8fef281445 100644 --- a/core/java/com/android/internal/util/FastMath.java +++ b/core/java/com/android/internal/util/FastMath.java @@ -21,6 +21,7 @@ import android.compat.annotation.UnsupportedAppUsage; /** * Fast and loose math routines. */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public class FastMath { /** diff --git a/core/java/com/android/internal/util/FastPrintWriter.java b/core/java/com/android/internal/util/FastPrintWriter.java index 63124def6d2f..fcd6df091c88 100644 --- a/core/java/com/android/internal/util/FastPrintWriter.java +++ b/core/java/com/android/internal/util/FastPrintWriter.java @@ -32,6 +32,7 @@ import java.nio.charset.CharsetEncoder; import java.nio.charset.CoderResult; import java.nio.charset.CodingErrorAction; +@android.ravenwood.annotation.RavenwoodKeepWholeClass public class FastPrintWriter extends PrintWriter { private static class DummyWriter extends Writer { @Override diff --git a/core/java/com/android/internal/util/GrowingArrayUtils.java b/core/java/com/android/internal/util/GrowingArrayUtils.java index 8c12e36a27d1..83d06d180179 100644 --- a/core/java/com/android/internal/util/GrowingArrayUtils.java +++ b/core/java/com/android/internal/util/GrowingArrayUtils.java @@ -29,6 +29,7 @@ import android.os.Build; * * @hide */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public final class GrowingArrayUtils { /** diff --git a/core/java/com/android/internal/util/HeavyHitterSketch.java b/core/java/com/android/internal/util/HeavyHitterSketch.java index e18acaf36797..2973d305f517 100644 --- a/core/java/com/android/internal/util/HeavyHitterSketch.java +++ b/core/java/com/android/internal/util/HeavyHitterSketch.java @@ -35,6 +35,7 @@ import java.util.List; * <p> * {@hide} */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public interface HeavyHitterSketch<T> { /** * Return the default implementation. diff --git a/core/java/com/android/internal/util/LineBreakBufferedWriter.java b/core/java/com/android/internal/util/LineBreakBufferedWriter.java index 552a93f6666a..8393b229e011 100644 --- a/core/java/com/android/internal/util/LineBreakBufferedWriter.java +++ b/core/java/com/android/internal/util/LineBreakBufferedWriter.java @@ -27,6 +27,7 @@ import java.util.Arrays; * * Note: this class is not thread-safe. */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public class LineBreakBufferedWriter extends PrintWriter { /** diff --git a/core/java/com/android/internal/util/ObjectUtils.java b/core/java/com/android/internal/util/ObjectUtils.java index 0e7b93d406f5..292ee14911c5 100644 --- a/core/java/com/android/internal/util/ObjectUtils.java +++ b/core/java/com/android/internal/util/ObjectUtils.java @@ -22,6 +22,7 @@ import android.annotation.Nullable; import java.util.Objects; /** @hide */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public class ObjectUtils { private ObjectUtils() {} diff --git a/core/java/com/android/internal/util/Parcelling.java b/core/java/com/android/internal/util/Parcelling.java index 3147c34a7129..70c3869d8e23 100644 --- a/core/java/com/android/internal/util/Parcelling.java +++ b/core/java/com/android/internal/util/Parcelling.java @@ -38,6 +38,7 @@ import java.util.regex.Pattern; * * @param <T> the type being [un]parcelled */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public interface Parcelling<T> { /** diff --git a/core/java/com/android/internal/util/ParseUtils.java b/core/java/com/android/internal/util/ParseUtils.java index a591f4aa41fc..a16ce2b0570f 100644 --- a/core/java/com/android/internal/util/ParseUtils.java +++ b/core/java/com/android/internal/util/ParseUtils.java @@ -23,6 +23,7 @@ import android.annotation.Nullable; * Test: atest /android/pi-dev/frameworks/base/core/tests/coretests/src/com/android/internal/util/ParseUtilsTest.java */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public final class ParseUtils { private ParseUtils() { } diff --git a/core/java/com/android/internal/util/ProcFileReader.java b/core/java/com/android/internal/util/ProcFileReader.java index b726d5d75239..6cf241e65d00 100644 --- a/core/java/com/android/internal/util/ProcFileReader.java +++ b/core/java/com/android/internal/util/ProcFileReader.java @@ -32,6 +32,7 @@ import java.nio.charset.StandardCharsets; * Currently doesn't support formats based on {@code \0}, tabs. * Consecutive spaces are treated as a single delimiter. */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public class ProcFileReader implements Closeable { private final InputStream mStream; private final byte[] mBuffer; diff --git a/core/java/com/android/internal/util/ProgressReporter.java b/core/java/com/android/internal/util/ProgressReporter.java index 7a8efba8a637..5640d8f28330 100644 --- a/core/java/com/android/internal/util/ProgressReporter.java +++ b/core/java/com/android/internal/util/ProgressReporter.java @@ -25,6 +25,7 @@ import android.os.RemoteException; import android.util.MathUtils; import com.android.internal.annotations.GuardedBy; +import com.android.internal.annotations.VisibleForTesting; /** * Tracks and reports progress of a single task to a {@link IProgressListener}. @@ -175,7 +176,8 @@ public class ProgressReporter { } } - int getProgress() { + @VisibleForTesting + public int getProgress() { return mProgress; } diff --git a/core/java/com/android/internal/util/QuickSelect.java b/core/java/com/android/internal/util/QuickSelect.java index 17739c9c8832..052efede2fba 100644 --- a/core/java/com/android/internal/util/QuickSelect.java +++ b/core/java/com/android/internal/util/QuickSelect.java @@ -28,6 +28,7 @@ import java.util.List; * * @hide */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public final class QuickSelect { private static <T> int selectImpl(@NonNull List<T> list, int left, int right, int k, @NonNull Comparator<? super T> comparator) { diff --git a/core/java/com/android/internal/util/RingBuffer.java b/core/java/com/android/internal/util/RingBuffer.java index 8fc4c30e54ab..04886598de35 100644 --- a/core/java/com/android/internal/util/RingBuffer.java +++ b/core/java/com/android/internal/util/RingBuffer.java @@ -27,6 +27,7 @@ import java.util.Arrays; * full, oldest events are dropped when new events are added. * {@hide} */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public class RingBuffer<T> { // Array for storing events. diff --git a/core/java/com/android/internal/util/StringPool.java b/core/java/com/android/internal/util/StringPool.java index c5180a3fe8cf..94f6ec9677ec 100644 --- a/core/java/com/android/internal/util/StringPool.java +++ b/core/java/com/android/internal/util/StringPool.java @@ -23,6 +23,7 @@ package com.android.internal.util; * * @hide */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public final class StringPool { private final String[] mPool = new String[512]; diff --git a/core/java/com/android/internal/util/dump/DumpableContainerImpl.java b/core/java/com/android/internal/util/dump/DumpableContainerImpl.java index ccec6c61b92a..bb5224f65904 100644 --- a/core/java/com/android/internal/util/dump/DumpableContainerImpl.java +++ b/core/java/com/android/internal/util/dump/DumpableContainerImpl.java @@ -32,6 +32,7 @@ import java.util.Objects; * * @hide */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public final class DumpableContainerImpl implements DumpableContainer { private static final String TAG = DumpableContainerImpl.class.getSimpleName(); diff --git a/core/java/com/google/android/collect/Lists.java b/core/java/com/google/android/collect/Lists.java index 585847da566c..392b87e6bd04 100644 --- a/core/java/com/google/android/collect/Lists.java +++ b/core/java/com/google/android/collect/Lists.java @@ -25,6 +25,7 @@ import java.util.Collections; * Provides static methods for creating {@code List} instances easily, and other * utility methods for working with lists. */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public class Lists { /** diff --git a/core/java/com/google/android/collect/Maps.java b/core/java/com/google/android/collect/Maps.java index cd4c1280545e..6492b0350669 100644 --- a/core/java/com/google/android/collect/Maps.java +++ b/core/java/com/google/android/collect/Maps.java @@ -24,6 +24,7 @@ import java.util.HashMap; /** * Provides static methods for creating mutable {@code Maps} instances easily. */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public class Maps { /** * Creates a {@code HashMap} instance. diff --git a/core/java/com/google/android/collect/Sets.java b/core/java/com/google/android/collect/Sets.java index e2429157d529..76eaf21cae04 100644 --- a/core/java/com/google/android/collect/Sets.java +++ b/core/java/com/google/android/collect/Sets.java @@ -31,6 +31,7 @@ import java.util.TreeSet; * other static methods for working with Sets. * */ +@android.ravenwood.annotation.RavenwoodKeepWholeClass public class Sets { /** diff --git a/core/tests/coretests/Android.bp b/core/tests/coretests/Android.bp index 37f592f5797b..21a406988104 100644 --- a/core/tests/coretests/Android.bp +++ b/core/tests/coretests/Android.bp @@ -168,11 +168,14 @@ android_library { android_ravenwood_test { name: "FrameworksCoreTestsRavenwood", static_libs: [ + "androidx.core_core", "androidx.annotation_annotation", "androidx.test.rules", ], srcs: [ "src/android/os/FileUtilsTest.java", + "src/com/android/internal/util/**/*.java", + "testdoubles/src/com/android/internal/util/**/*.java", ], auto_gen_config: true, } diff --git a/core/tests/coretests/src/com/android/internal/util/BitUtilsTest.java b/core/tests/coretests/src/com/android/internal/util/BitUtilsTest.java index fdba811f3eaa..fdde36ad8e35 100644 --- a/core/tests/coretests/src/com/android/internal/util/BitUtilsTest.java +++ b/core/tests/coretests/src/com/android/internal/util/BitUtilsTest.java @@ -30,8 +30,8 @@ import static com.android.internal.util.BitUtils.unpackBits; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/core/tests/coretests/src/com/android/internal/util/ContrastColorUtilTest.java b/core/tests/coretests/src/com/android/internal/util/ContrastColorUtilTest.java index 5f5bf1165004..e6ebfefd2aea 100644 --- a/core/tests/coretests/src/com/android/internal/util/ContrastColorUtilTest.java +++ b/core/tests/coretests/src/com/android/internal/util/ContrastColorUtilTest.java @@ -23,6 +23,8 @@ import static com.google.common.truth.Truth.assertThat; import android.content.Context; import android.content.res.ColorStateList; import android.graphics.Color; +import android.platform.test.annotations.IgnoreUnderRavenwood; +import android.platform.test.ravenwood.RavenwoodRule; import android.text.Spannable; import android.text.SpannableString; import android.text.SpannableStringBuilder; @@ -32,23 +34,21 @@ import android.text.style.TextAppearanceSpan; import androidx.test.InstrumentationRegistry; import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; import com.android.internal.R; -import junit.framework.TestCase; - -import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.runner.RunWith; -public class ContrastColorUtilTest extends TestCase { - - private Context mContext; - - @Before - public void setUp() { - mContext = InstrumentationRegistry.getContext(); - } +@RunWith(AndroidJUnit4.class) +@IgnoreUnderRavenwood(blockedBy = Color.class) +public class ContrastColorUtilTest { + @Rule + public final RavenwoodRule mRavenwood = new RavenwoodRule(); + @Test @SmallTest public void testEnsureTextContrastAgainstDark() { int darkBg = 0xFF35302A; @@ -70,6 +70,7 @@ public class ContrastColorUtilTest extends TestCase { assertContrastIsWithinRange(selfContrastColor, darkBg, 4.5, 4.75); } + @Test @SmallTest public void testEnsureTextContrastAgainstLight() { int lightBg = 0xFFFFF8F2; @@ -91,13 +92,16 @@ public class ContrastColorUtilTest extends TestCase { assertContrastIsWithinRange(selfContrastColor, lightBg, 4.5, 4.75); } + @Test public void testBuilder_ensureColorSpanContrast_removesAllFullLengthColorSpans() { + Context context = InstrumentationRegistry.getContext(); + Spannable text = new SpannableString("blue text with yellow and green"); text.setSpan(new ForegroundColorSpan(Color.YELLOW), 15, 21, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); text.setSpan(new ForegroundColorSpan(Color.BLUE), 0, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); - TextAppearanceSpan taSpan = new TextAppearanceSpan(mContext, + TextAppearanceSpan taSpan = new TextAppearanceSpan(context, R.style.TextAppearance_DeviceDefault_Notification_Title); assertThat(taSpan.getTextColor()).isNotNull(); // it must be set to prove it is cleared. text.setSpan(taSpan, 0, text.length(), @@ -123,6 +127,7 @@ public class ContrastColorUtilTest extends TestCase { assertThat(((ForegroundColorSpan) spans[2]).getForegroundColor()).isEqualTo(Color.GREEN); } + @Test public void testBuilder_ensureColorSpanContrast_partialLength_adjusted() { int background = 0xFFFF0101; // Slightly lighter red CharSequence text = new SpannableStringBuilder() @@ -138,14 +143,17 @@ public class ContrastColorUtilTest extends TestCase { assertContrastIsWithinRange(foregroundColor, background, 3, 3.2); } + @Test public void testBuilder_ensureColorSpanContrast_worksWithComplexInput() { + Context context = InstrumentationRegistry.getContext(); + Spannable text = new SpannableString("blue text with yellow and green and cyan"); text.setSpan(new ForegroundColorSpan(Color.YELLOW), 15, 21, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); text.setSpan(new ForegroundColorSpan(Color.BLUE), 0, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); // cyan TextAppearanceSpan - TextAppearanceSpan taSpan = new TextAppearanceSpan(mContext, + TextAppearanceSpan taSpan = new TextAppearanceSpan(context, R.style.TextAppearance_DeviceDefault_Notification_Title); taSpan = new TextAppearanceSpan(taSpan.getFamily(), taSpan.getTextStyle(), taSpan.getTextSize(), ColorStateList.valueOf(Color.CYAN), null); diff --git a/core/tests/coretests/src/com/android/internal/util/DumpUtilsTest.java b/core/tests/coretests/src/com/android/internal/util/DumpUtilsTest.java index 36c2a62ae6ed..d2d3c134f390 100644 --- a/core/tests/coretests/src/com/android/internal/util/DumpUtilsTest.java +++ b/core/tests/coretests/src/com/android/internal/util/DumpUtilsTest.java @@ -25,10 +25,16 @@ import static com.android.internal.util.DumpUtils.isPlatformPackage; import static com.google.common.truth.Truth.assertWithMessage; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import android.content.ComponentName; import android.util.SparseArray; -import junit.framework.TestCase; +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; import java.io.PrintWriter; import java.io.StringWriter; @@ -37,7 +43,8 @@ import java.io.StringWriter; * Run with: atest FrameworksCoreTests:DumpUtilsTest */ -public class DumpUtilsTest extends TestCase { +@RunWith(AndroidJUnit4.class) +public class DumpUtilsTest { private final StringWriter mStringWriter = new StringWriter(); private final PrintWriter mPrintWriter = new PrintWriter(mStringWriter); @@ -56,6 +63,7 @@ public class DumpUtilsTest extends TestCase { return () -> cn(componentName); } + @Test public void testIsPlatformPackage() { assertTrue(isPlatformPackage("android")); assertTrue(isPlatformPackage("android.abc")); @@ -79,6 +87,7 @@ public class DumpUtilsTest extends TestCase { assertFalse(isPlatformPackage(wcn("com.google.def/abc"))); } + @Test public void testIsNonPlatformPackage() { assertFalse(isNonPlatformPackage("android")); assertFalse(isNonPlatformPackage("android.abc")); @@ -102,6 +111,7 @@ public class DumpUtilsTest extends TestCase { assertTrue(isNonPlatformPackage(wcn("com.google.def/abc"))); } + @Test public void testIsPlatformCriticalPackage() { for (final ComponentName componentName : CRITICAL_SECTION_COMPONENTS) { assertTrue(isPlatformCriticalPackage(() -> componentName)); @@ -115,6 +125,7 @@ public class DumpUtilsTest extends TestCase { assertFalse(isPlatformCriticalPackage(null)); } + @Test public void testIsPlatformNonCriticalPackage() { for (final ComponentName componentName : CRITICAL_SECTION_COMPONENTS) { assertFalse(isPlatformNonCriticalPackage(() -> componentName)); @@ -128,6 +139,7 @@ public class DumpUtilsTest extends TestCase { assertFalse(isPlatformNonCriticalPackage(null)); } + @Test public void testFilterRecord() { assertFalse(filterRecord(null).test(wcn("com.google.p/abc"))); assertFalse(filterRecord(null).test(wcn("com.android.p/abc"))); @@ -178,6 +190,7 @@ public class DumpUtilsTest extends TestCase { wcn("com.google/.abc"))); } + @Test public void testDumpSparseArray_empty() { SparseArray<String> array = new SparseArray<>(); @@ -188,6 +201,7 @@ public class DumpUtilsTest extends TestCase { assertWithMessage("empty array dump").that(output).isEqualTo("...No whatevers\n"); } + @Test public void testDumpSparseArray_oneElement() { SparseArray<String> array = new SparseArray<>(); array.put(1, "uno"); @@ -201,6 +215,7 @@ public class DumpUtilsTest extends TestCase { + "..0: 1->uno\n"); } + @Test public void testDumpSparseArray_oneNullElement() { SparseArray<String> array = new SparseArray<>(); array.put(1, null); @@ -214,6 +229,7 @@ public class DumpUtilsTest extends TestCase { + "..0: 1->(null)\n"); } + @Test public void testDumpSparseArray_multipleElements() { SparseArray<String> array = new SparseArray<>(); array.put(1, "uno"); @@ -231,6 +247,7 @@ public class DumpUtilsTest extends TestCase { + "..2: 42->(null)\n"); } + @Test public void testDumpSparseArray_keyDumperOnly() { SparseArray<String> array = new SparseArray<>(); array.put(1, "uno"); @@ -251,6 +268,7 @@ public class DumpUtilsTest extends TestCase { + "_2=42_(null)\n"); } + @Test public void testDumpSparseArray_valueDumperOnly() { SparseArray<String> array = new SparseArray<>(); array.put(1, "uno"); @@ -272,6 +290,7 @@ public class DumpUtilsTest extends TestCase { + "..2: 42->(null)\n"); } + @Test public void testDumpSparseArray_keyAndValueDumpers() { SparseArray<String> array = new SparseArray<>(); array.put(1, "uno"); @@ -295,6 +314,7 @@ public class DumpUtilsTest extends TestCase { + "_2=42_(null)\n"); } + @Test public void testDumpSparseArrayValues() { SparseArray<String> array = new SparseArray<>(); array.put(1, "uno"); @@ -306,7 +326,7 @@ public class DumpUtilsTest extends TestCase { String output = flushPrintWriter(); assertWithMessage("dump of %s", array).that(output).isEqualTo("" - + ".3 numbers:\n" + + ".3 number(s):\n" + "..uno\n" + "..duo\n" + "..(null)\n"); diff --git a/core/tests/coretests/src/com/android/internal/util/DumpableContainerImplTest.java b/core/tests/coretests/src/com/android/internal/util/DumpableContainerImplTest.java index 589e4f98a32a..61d4e3da5036 100644 --- a/core/tests/coretests/src/com/android/internal/util/DumpableContainerImplTest.java +++ b/core/tests/coretests/src/com/android/internal/util/DumpableContainerImplTest.java @@ -17,30 +17,32 @@ package com.android.internal.util; import static com.google.common.truth.Truth.assertWithMessage; -import static org.testng.Assert.assertThrows; - import android.util.Dumpable; +import androidx.test.runner.AndroidJUnit4; + import com.android.internal.util.dump.DumpableContainerImpl; import org.junit.Test; +import org.junit.runner.RunWith; import java.io.PrintWriter; import java.io.StringWriter; import java.util.concurrent.atomic.AtomicReference; -public final class DumpableContainerImplTest { +@RunWith(AndroidJUnit4.class) +public class DumpableContainerImplTest { private final DumpableContainerImpl mImpl = new DumpableContainerImpl(); private final StringWriter mSw = new StringWriter(); private final PrintWriter mWriter = new PrintWriter(mSw); - @Test + @Test(expected = NullPointerException.class) public void testAddDumpable_null() { - assertThrows(NullPointerException.class, () -> mImpl.addDumpable(null)); + mImpl.addDumpable(null); } - @Test + @Test(expected = NullPointerException.class) public void testAddDumpable_dumpableWithoutName() { Dumpable namelessDumpable = new Dumpable() { @@ -55,7 +57,7 @@ public final class DumpableContainerImplTest { } }; - assertThrows(NullPointerException.class, () -> mImpl.addDumpable(namelessDumpable)); + mImpl.addDumpable(namelessDumpable); } @Test @@ -179,9 +181,9 @@ public final class DumpableContainerImplTest { + "......6 Args: 4,8,15,16,23,42,\n"); } - @Test + @Test(expected = NullPointerException.class) public void testRemoveDumpable_null() { - assertThrows(NullPointerException.class, () -> mImpl.removeDumpable(null)); + mImpl.removeDumpable(null); } @Test diff --git a/core/tests/coretests/src/com/android/internal/util/FakeLatencyTrackerTest.java b/core/tests/coretests/src/com/android/internal/util/FakeLatencyTrackerTest.java index 3946cdf76821..6bd67ea486d1 100644 --- a/core/tests/coretests/src/com/android/internal/util/FakeLatencyTrackerTest.java +++ b/core/tests/coretests/src/com/android/internal/util/FakeLatencyTrackerTest.java @@ -24,12 +24,15 @@ import static com.android.internal.util.LatencyTracker.ACTION_SHOW_VOICE_INTERAC import static com.google.common.truth.Truth.assertThat; +import android.platform.test.annotations.IgnoreUnderRavenwood; +import android.platform.test.ravenwood.RavenwoodRule; import android.provider.DeviceConfig; -import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.runner.AndroidJUnit4; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -42,7 +45,10 @@ import java.util.List; * {@link LatencyTrackerTest} */ @RunWith(AndroidJUnit4.class) +@IgnoreUnderRavenwood(blockedBy = DeviceConfig.class) public class FakeLatencyTrackerTest { + @Rule + public final RavenwoodRule mRavenwood = new RavenwoodRule(); private FakeLatencyTracker mFakeLatencyTracker; private int mInitialSyncDisabledMode; diff --git a/core/tests/coretests/src/com/android/internal/util/FastDataTest.java b/core/tests/coretests/src/com/android/internal/util/FastDataTest.java index de325ab7d186..316b95ac1b7d 100644 --- a/core/tests/coretests/src/com/android/internal/util/FastDataTest.java +++ b/core/tests/coretests/src/com/android/internal/util/FastDataTest.java @@ -21,6 +21,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import android.annotation.NonNull; +import android.platform.test.ravenwood.RavenwoodRule; import android.util.ExceptionUtils; import com.android.modules.utils.FastDataInput; @@ -29,6 +30,7 @@ import com.android.modules.utils.FastDataOutput; import libcore.util.HexEncoding; import org.junit.Assume; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -51,6 +53,9 @@ import java.util.function.Consumer; @RunWith(Parameterized.class) public class FastDataTest { + @Rule + public final RavenwoodRule mRavenwood = new RavenwoodRule(); + private final boolean use4ByteSequence; private static final String TEST_SHORT_STRING = "a"; @@ -59,7 +64,12 @@ public class FastDataTest { @Parameters(name = "use4ByteSequence={0}") public static Collection<Object[]> data() { - return Arrays.asList(new Object[][] { {true}, {false} }); + if (RavenwoodRule.isUnderRavenwood()) { + // TODO: 4-byte sequences are only supported on ART + return Arrays.asList(new Object[][]{{false}}); + } else { + return Arrays.asList(new Object[][]{{true}, {false}}); + } } public FastDataTest(boolean use4ByteSequence) { diff --git a/core/tests/coretests/src/com/android/internal/util/FastMathTest.java b/core/tests/coretests/src/com/android/internal/util/FastMathTest.java new file mode 100644 index 000000000000..dd263345022b --- /dev/null +++ b/core/tests/coretests/src/com/android/internal/util/FastMathTest.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2023 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.internal.util; + +import static org.junit.Assert.assertEquals; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +public class FastMathTest { + @Test + public void testRound() { + assertEquals(-1, FastMath.round(-1.0f)); + assertEquals(0, FastMath.round(0.0f)); + assertEquals(1, FastMath.round(1.0f)); + } +} diff --git a/core/tests/coretests/src/com/android/internal/util/GrowingArrayUtilsTest.java b/core/tests/coretests/src/com/android/internal/util/GrowingArrayUtilsTest.java new file mode 100644 index 000000000000..8456161f9709 --- /dev/null +++ b/core/tests/coretests/src/com/android/internal/util/GrowingArrayUtilsTest.java @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2023 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.internal.util; + +import static com.android.internal.util.GrowingArrayUtils.append; +import static com.android.internal.util.GrowingArrayUtils.insert; + +import static org.junit.Assert.assertArrayEquals; + +import android.util.EmptyArray; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.Arrays; + +@RunWith(AndroidJUnit4.class) +public class GrowingArrayUtilsTest { + private final Object TEST_OBJECT = new Object(); + + @Test + public void testAppend_Object() { + assertArrayEqualsPrefix(new Object[]{TEST_OBJECT}, + append(EmptyArray.OBJECT, 0, TEST_OBJECT)); + assertArrayEqualsPrefix(new Object[]{TEST_OBJECT, TEST_OBJECT}, + append(new Object[]{TEST_OBJECT}, 1, TEST_OBJECT)); + assertArrayEqualsPrefix(new Object[]{TEST_OBJECT}, + append(new Object[]{null, null}, 0, TEST_OBJECT)); + } + + @Test + public void testInsert_Object() { + assertArrayEqualsPrefix(new Object[]{TEST_OBJECT}, + insert(EmptyArray.OBJECT, 0, 0, TEST_OBJECT)); + assertArrayEqualsPrefix(new Object[]{null, TEST_OBJECT}, + insert(new Object[]{TEST_OBJECT}, 1, 0, null)); + assertArrayEqualsPrefix(new Object[]{TEST_OBJECT, null}, + insert(new Object[]{TEST_OBJECT}, 1, 1, null)); + assertArrayEqualsPrefix(new Object[]{TEST_OBJECT, null, TEST_OBJECT}, + insert(new Object[]{TEST_OBJECT, TEST_OBJECT}, 2, 1, null)); + } + + @Test + public void testAppend_Int() { + assertArrayEqualsPrefix(new int[]{42}, + append(EmptyArray.INT, 0, 42)); + assertArrayEqualsPrefix(new int[]{42, 42}, + append(new int[]{42}, 1, 42)); + assertArrayEqualsPrefix(new int[]{42}, + append(new int[]{0, 0}, 0, 42)); + } + + @Test + public void testInsert_Int() { + assertArrayEqualsPrefix(new int[]{42}, + insert(EmptyArray.INT, 0, 0, 42)); + assertArrayEqualsPrefix(new int[]{21, 42}, + insert(new int[]{42}, 1, 0, 21)); + assertArrayEqualsPrefix(new int[]{42, 21}, + insert(new int[]{42}, 1, 1, 21)); + assertArrayEqualsPrefix(new int[]{42, 21, 43}, + insert(new int[]{42, 43}, 2, 1, 21)); + } + + @Test + public void testAppend_Long() { + assertArrayEqualsPrefix(new long[]{42}, + append(EmptyArray.LONG, 0, 42)); + assertArrayEqualsPrefix(new long[]{42, 42}, + append(new long[]{42}, 1, 42)); + assertArrayEqualsPrefix(new long[]{42}, + append(new long[]{0, 0}, 0, 42)); + } + + @Test + public void testInsert_Long() { + assertArrayEqualsPrefix(new long[]{42}, + insert(EmptyArray.LONG, 0, 0, 42)); + assertArrayEqualsPrefix(new long[]{21, 42}, + insert(new long[]{42}, 1, 0, 21)); + assertArrayEqualsPrefix(new long[]{42, 21}, + insert(new long[]{42}, 1, 1, 21)); + assertArrayEqualsPrefix(new long[]{42, 21, 43}, + insert(new long[]{42, 43}, 2, 1, 21)); + } + + @Test + public void testAppend_Boolean() { + assertArrayEqualsPrefix(new boolean[]{true}, + append(EmptyArray.BOOLEAN, 0, true)); + assertArrayEqualsPrefix(new boolean[]{true, true}, + append(new boolean[]{true}, 1, true)); + assertArrayEqualsPrefix(new boolean[]{true}, + append(new boolean[]{false, false}, 0, true)); + } + + @Test + public void testInsert_Boolean() { + assertArrayEqualsPrefix(new boolean[]{true}, + insert(EmptyArray.BOOLEAN, 0, 0, true)); + assertArrayEqualsPrefix(new boolean[]{false, true}, + insert(new boolean[]{true}, 1, 0, false)); + assertArrayEqualsPrefix(new boolean[]{true, false}, + insert(new boolean[]{true}, 1, 1, false)); + assertArrayEqualsPrefix(new boolean[]{true, false, true}, + insert(new boolean[]{true, true}, 2, 1, false)); + } + + private <T> void assertArrayEqualsPrefix(T[] expected, T[] actual) { + assertArrayEquals(expected, Arrays.copyOf(actual, expected.length)); + } + + private void assertArrayEqualsPrefix(int[] expected, int[] actual) { + assertArrayEquals(expected, Arrays.copyOf(actual, expected.length)); + } + + private void assertArrayEqualsPrefix(long[] expected, long[] actual) { + assertArrayEquals(expected, Arrays.copyOf(actual, expected.length)); + } + + private void assertArrayEqualsPrefix(boolean[] expected, boolean[] actual) { + assertArrayEquals(expected, Arrays.copyOf(actual, expected.length)); + } +} diff --git a/core/tests/coretests/src/com/android/internal/util/HexDumpTest.java b/core/tests/coretests/src/com/android/internal/util/HexDumpTest.java index f1cd89bf49f4..dcffa1cf6975 100644 --- a/core/tests/coretests/src/com/android/internal/util/HexDumpTest.java +++ b/core/tests/coretests/src/com/android/internal/util/HexDumpTest.java @@ -19,14 +19,22 @@ package com.android.internal.util; import static com.android.internal.util.HexDump.hexStringToByteArray; import static com.android.internal.util.HexDump.toHexString; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; import java.io.PrintWriter; import java.io.StringWriter; import java.util.Arrays; import java.util.Random; -public final class HexDumpTest extends TestCase { +@RunWith(AndroidJUnit4.class) +public final class HexDumpTest { + @Test public void testBytesToHexString() { assertEquals("abcdef", HexDump.toHexString( new byte[] { (byte) 0xab, (byte) 0xcd, (byte) 0xef }, false)); @@ -34,12 +42,14 @@ public final class HexDumpTest extends TestCase { new byte[] { (byte) 0xab, (byte) 0xcd, (byte) 0xef }, true)); } + @Test public void testNullByteArray() { assertThrows( NullPointerException.class, () -> HexDump.toHexString(null)); } + @Test public void testBytesToHexString_allByteValues() { byte[] bytes = new byte[256]; for (int i = 0; i < bytes.length; i++) { @@ -57,6 +67,7 @@ public final class HexDumpTest extends TestCase { assertEquals(expected, HexDump.toHexString(bytes)); } + @Test public void testRoundTrip_fromBytes() { Random deterministicRandom = new Random(31337); // arbitrary but deterministic for (int length = 0; length < 100; length++) { @@ -68,6 +79,7 @@ public final class HexDumpTest extends TestCase { } } + @Test public void testRoundTrip_fromString() { String hexString = "0123456789ABCDEF72f9a3438934c378d34f32a8b932"; for (int length = 0; length < hexString.length(); length += 2) { @@ -77,6 +89,7 @@ public final class HexDumpTest extends TestCase { } } + @Test public void testToHexString_offsetLength() { byte[] bytes = new byte[32]; for (int i = 0; i < 16; i++) { @@ -97,6 +110,7 @@ public final class HexDumpTest extends TestCase { } } + @Test public void testToHexString_case() { byte[] bytes = new byte[32]; for (int i = 0; i < 16; i++) { @@ -113,16 +127,19 @@ public final class HexDumpTest extends TestCase { assertEquals(expected.toUpperCase(), toHexString(bytes)); } + @Test public void testHexStringToByteArray_empty() { assertBytesEqual(new byte[0], HexDump.hexStringToByteArray("")); } + @Test public void testHexStringToByteArray_null() { assertThrows( NullPointerException.class, () -> HexDump.hexStringToByteArray((String) null)); } + @Test public void testHexStringToByteArray_invalidCharacters() { // IllegalArgumentException would probably have been better than RuntimeException, but it // might be too late to change now. @@ -137,6 +154,7 @@ public final class HexDumpTest extends TestCase { () -> HexDump.hexStringToByteArray("abcdefgh")); } + @Test public void testHexStringToByteArray_oddLength() { // IllegalArgumentException would probably have been better than // StringIndexOutOfBoundsException, but it might be too late to change now. diff --git a/core/tests/coretests/src/com/android/internal/util/LatencyTrackerTest.java b/core/tests/coretests/src/com/android/internal/util/LatencyTrackerTest.java index f24894e7f263..010f72466f3a 100644 --- a/core/tests/coretests/src/com/android/internal/util/LatencyTrackerTest.java +++ b/core/tests/coretests/src/com/android/internal/util/LatencyTrackerTest.java @@ -25,9 +25,11 @@ import static com.android.internal.util.LatencyTracker.STATSD_ACTION; import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; +import android.platform.test.annotations.IgnoreUnderRavenwood; +import android.platform.test.ravenwood.RavenwoodRule; import android.provider.DeviceConfig; -import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.runner.AndroidJUnit4; import com.android.internal.util.LatencyTracker.ActionProperties; @@ -49,7 +51,11 @@ import java.util.Map; import java.util.stream.Collectors; @RunWith(AndroidJUnit4.class) +@IgnoreUnderRavenwood(blockedBy = DeviceConfig.class) public class LatencyTrackerTest { + @Rule + public final RavenwoodRule mRavenwood = new RavenwoodRule(); + private static final String ENUM_NAME_PREFIX = "UIACTION_LATENCY_REPORTED__ACTION__"; @Rule diff --git a/core/tests/coretests/src/com/android/internal/util/LineBreakBufferedWriterTest.java b/core/tests/coretests/src/com/android/internal/util/LineBreakBufferedWriterTest.java index b2a2265d30ae..e6418fae7dca 100644 --- a/core/tests/coretests/src/com/android/internal/util/LineBreakBufferedWriterTest.java +++ b/core/tests/coretests/src/com/android/internal/util/LineBreakBufferedWriterTest.java @@ -16,7 +16,13 @@ package com.android.internal.util; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; import java.io.ByteArrayOutputStream; import java.io.Writer; @@ -26,18 +32,18 @@ import java.util.List; /** * Tests for {@link IndentingPrintWriter}. */ -public class LineBreakBufferedWriterTest extends TestCase { +@RunWith(AndroidJUnit4.class) +public class LineBreakBufferedWriterTest { private ByteArrayOutputStream mStream; private RecordingWriter mWriter; - @Override - protected void setUp() throws Exception { - super.setUp(); - + @Before + public void setUp() throws Exception { mWriter = new RecordingWriter(); } + @Test public void testLessThanBufferSize() { final LineBreakBufferedWriter lw = new LineBreakBufferedWriter(mWriter, 1000); @@ -49,6 +55,7 @@ public class LineBreakBufferedWriterTest extends TestCase { assertOutput("Hello\nWorld\nTest\n"); } + @Test public void testMoreThanBufferSizeNoLineBreaks() { final LineBreakBufferedWriter lw = new LineBreakBufferedWriter(mWriter, 20); @@ -71,6 +78,7 @@ public class LineBreakBufferedWriterTest extends TestCase { } } + @Test public void testMoreThanBufferSizeNoLineBreaksSingleString() { final LineBreakBufferedWriter lw = new LineBreakBufferedWriter(mWriter, 20); @@ -92,6 +100,7 @@ public class LineBreakBufferedWriterTest extends TestCase { } } + @Test public void testMoreThanBufferSizeLineBreakBefore() { final LineBreakBufferedWriter lw = new LineBreakBufferedWriter(mWriter, 20); @@ -104,6 +113,7 @@ public class LineBreakBufferedWriterTest extends TestCase { assertOutput("aaaaaaaaaa", "bbbbcccccccccc"); } + @Test public void testMoreThanBufferSizeLineBreakBeforeSingleString() { final LineBreakBufferedWriter lw = new LineBreakBufferedWriter(mWriter, 20); @@ -115,6 +125,7 @@ public class LineBreakBufferedWriterTest extends TestCase { assertOutput("aaaaaaaaaa", "bbbbcccccccccc"); } + @Test public void testMoreThanBufferSizeLineBreakNew() { final LineBreakBufferedWriter lw = new LineBreakBufferedWriter(mWriter, 20); @@ -127,6 +138,7 @@ public class LineBreakBufferedWriterTest extends TestCase { assertOutput("aaaaaaaaaabbbbbc\nd", "ddddddddd"); } + @Test public void testMoreThanBufferSizeLineBreakBeforeAndNew() { final LineBreakBufferedWriter lw = new LineBreakBufferedWriter(mWriter, 20); @@ -139,6 +151,7 @@ public class LineBreakBufferedWriterTest extends TestCase { assertOutput("aaaaaaaaaa\nbbbbbc\nd", "ddddddddd"); } + @Test public void testMoreThanBufferSizeInt() { final LineBreakBufferedWriter lw = new LineBreakBufferedWriter(mWriter, 15); @@ -151,6 +164,7 @@ public class LineBreakBufferedWriterTest extends TestCase { assertOutput("123456789098765", "4321"); } + @Test public void testMoreThanBufferSizeChar() { final LineBreakBufferedWriter lw = new LineBreakBufferedWriter(mWriter, 15); @@ -165,6 +179,7 @@ public class LineBreakBufferedWriterTest extends TestCase { assertOutput("$$$$$$$$$$%%%%%", "%%%%%"); } + @Test public void testMoreThanBufferSizeLineBreakNewChars() { final LineBreakBufferedWriter lw = new LineBreakBufferedWriter(mWriter, 20); @@ -177,6 +192,7 @@ public class LineBreakBufferedWriterTest extends TestCase { assertOutput("aaaaaaaaaabbbbbc\nd", "ddddddddd"); } + @Test public void testMoreThenInitialCapacitySimpleWrites() { // This check is different from testMoreThanBufferSizeChar. The initial capacity is lower // than the maximum buffer size here. diff --git a/core/tests/coretests/src/com/android/internal/util/ParseUtilsTest.java b/core/tests/coretests/src/com/android/internal/util/ParseUtilsTest.java index 867152e720dd..d24cbfef9f10 100644 --- a/core/tests/coretests/src/com/android/internal/util/ParseUtilsTest.java +++ b/core/tests/coretests/src/com/android/internal/util/ParseUtilsTest.java @@ -16,13 +16,23 @@ package com.android.internal.util; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; /** * Run with: atest /android/pi-dev/frameworks/base/core/tests/coretests/src/com/android/internal/util/ParseUtilsTest.java */ -public class ParseUtilsTest extends TestCase { +@RunWith(AndroidJUnit4.class) +public class ParseUtilsTest { + private static final float DELTA_FLOAT = 0.0f; + private static final double DELTA_DOUBLE = 0.0d; + + @Test public void testParseInt() { assertEquals(1, ParseUtils.parseInt(null, 1)); assertEquals(1, ParseUtils.parseInt("", 1)); @@ -33,6 +43,7 @@ public class ParseUtilsTest extends TestCase { assertEquals(-2, ParseUtils.parseInt("-2", 1)); } + @Test public void testParseIntWithBase() { assertEquals(1, ParseUtils.parseIntWithBase(null, 10, 1)); assertEquals(1, ParseUtils.parseIntWithBase("", 10, 1)); @@ -45,6 +56,7 @@ public class ParseUtilsTest extends TestCase { assertEquals(-3, ParseUtils.parseIntWithBase("-10", 3, 1)); } + @Test public void testParseLong() { assertEquals(1L, ParseUtils.parseLong(null, 1)); assertEquals(1L, ParseUtils.parseLong("", 1)); @@ -52,6 +64,7 @@ public class ParseUtilsTest extends TestCase { assertEquals(2L, ParseUtils.parseLong("2", 1)); } + @Test public void testParseLongWithBase() { assertEquals(1L, ParseUtils.parseLongWithBase(null, 10, 1)); assertEquals(1L, ParseUtils.parseLongWithBase("", 10, 1)); @@ -69,20 +82,23 @@ public class ParseUtilsTest extends TestCase { assertEquals(10_000_000_000L, ParseUtils.parseLongWithBase(null, 10, 10_000_000_000L)); } + @Test public void testParseFloat() { - assertEquals(0.5f, ParseUtils.parseFloat(null, 0.5f)); - assertEquals(0.5f, ParseUtils.parseFloat("", 0.5f)); - assertEquals(0.5f, ParseUtils.parseFloat("1x", 0.5f)); - assertEquals(1.5f, ParseUtils.parseFloat("1.5", 0.5f)); + assertEquals(0.5f, ParseUtils.parseFloat(null, 0.5f), DELTA_FLOAT); + assertEquals(0.5f, ParseUtils.parseFloat("", 0.5f), DELTA_FLOAT); + assertEquals(0.5f, ParseUtils.parseFloat("1x", 0.5f), DELTA_FLOAT); + assertEquals(1.5f, ParseUtils.parseFloat("1.5", 0.5f), DELTA_FLOAT); } + @Test public void testParseDouble() { - assertEquals(0.5, ParseUtils.parseDouble(null, 0.5)); - assertEquals(0.5, ParseUtils.parseDouble("", 0.5)); - assertEquals(0.5, ParseUtils.parseDouble("1x", 0.5)); - assertEquals(1.5, ParseUtils.parseDouble("1.5", 0.5)); + assertEquals(0.5, ParseUtils.parseDouble(null, 0.5), DELTA_DOUBLE); + assertEquals(0.5, ParseUtils.parseDouble("", 0.5), DELTA_DOUBLE); + assertEquals(0.5, ParseUtils.parseDouble("1x", 0.5), DELTA_DOUBLE); + assertEquals(1.5, ParseUtils.parseDouble("1.5", 0.5), DELTA_DOUBLE); } + @Test public void testParseBoolean() { assertEquals(false, ParseUtils.parseBoolean(null, false)); assertEquals(true, ParseUtils.parseBoolean(null, true)); diff --git a/core/tests/coretests/src/com/android/internal/util/ProgressReporterTest.java b/core/tests/coretests/src/com/android/internal/util/ProgressReporterTest.java index 87f2a8a67947..0d213357c9a5 100644 --- a/core/tests/coretests/src/com/android/internal/util/ProgressReporterTest.java +++ b/core/tests/coretests/src/com/android/internal/util/ProgressReporterTest.java @@ -16,14 +16,28 @@ package com.android.internal.util; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; + +import android.platform.test.annotations.IgnoreUnderRavenwood; +import android.platform.test.ravenwood.RavenwoodRule; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +@IgnoreUnderRavenwood(blockedBy = ProgressReporter.class) +public class ProgressReporterTest { + @Rule + public final RavenwoodRule mRavenwood = new RavenwoodRule(); -public class ProgressReporterTest extends TestCase { private ProgressReporter r; - @Override - protected void setUp() throws Exception { - super.setUp(); + @Before + public void setUp() throws Exception { r = new ProgressReporter(0); } @@ -37,6 +51,7 @@ public class ProgressReporterTest extends TestCase { assertEquals("len", len, range[1]); } + @Test public void testBasic() throws Exception { assertProgress(0); @@ -50,6 +65,7 @@ public class ProgressReporterTest extends TestCase { assertProgress(100); } + @Test public void testSegment() throws Exception { r.setProgress(20); assertProgress(20); @@ -68,6 +84,7 @@ public class ProgressReporterTest extends TestCase { assertProgress(80); } + @Test public void testSegmentOvershoot() throws Exception { r.setProgress(20); assertProgress(20); @@ -87,6 +104,7 @@ public class ProgressReporterTest extends TestCase { assertProgress(60); } + @Test public void testSegmentNested() throws Exception { r.setProgress(20); assertProgress(20); diff --git a/core/tests/coretests/src/com/android/internal/util/RingBufferTest.java b/core/tests/coretests/src/com/android/internal/util/RingBufferTest.java index 4497770ef40d..d7a100a30ac8 100644 --- a/core/tests/coretests/src/com/android/internal/util/RingBufferTest.java +++ b/core/tests/coretests/src/com/android/internal/util/RingBufferTest.java @@ -20,8 +20,8 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; -import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/core/tests/coretests/src/com/android/internal/util/TokenBucketTest.java b/core/tests/coretests/src/com/android/internal/util/TokenBucketTest.java index 8b30828a8936..ef579fe07af5 100644 --- a/core/tests/coretests/src/com/android/internal/util/TokenBucketTest.java +++ b/core/tests/coretests/src/com/android/internal/util/TokenBucketTest.java @@ -16,22 +16,29 @@ package com.android.internal.util; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; import android.os.SystemClock; import android.text.format.DateUtils; -import junit.framework.TestCase; +import androidx.test.runner.AndroidJUnit4; -public class TokenBucketTest extends TestCase { +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +public class TokenBucketTest { static final int FILL_DELTA_VERY_SHORT = 1; static final int FILL_DELTA_VERY_LONG = Integer.MAX_VALUE; + @Test public void testArgumentValidation() { assertThrow(() -> new TokenBucket(0, 1, 1)); assertThrow(() -> new TokenBucket(1, 0, 1)); - assertThrow(() -> new TokenBucket(1, 1, 0)); assertThrow(() -> new TokenBucket(0, 1)); assertThrow(() -> new TokenBucket(1, 0)); assertThrow(() -> new TokenBucket(-1, 1, 1)); @@ -46,6 +53,7 @@ public class TokenBucketTest extends TestCase { new TokenBucket(5000, 1); } + @Test public void testInitialCapacity() { drain(new TokenBucket(FILL_DELTA_VERY_LONG, 1), 1); drain(new TokenBucket(FILL_DELTA_VERY_LONG, 10), 10); @@ -62,6 +70,7 @@ public class TokenBucketTest extends TestCase { drain(new TokenBucket((int) DateUtils.DAY_IN_MILLIS, 200), 200); } + @Test public void testReset() { TokenBucket tb = new TokenBucket(FILL_DELTA_VERY_LONG, 100, 10); drain(tb, 10); @@ -77,6 +86,7 @@ public class TokenBucketTest extends TestCase { drain(tb, 30); } + @Test public void testFill() throws Exception { int delta = 50; TokenBucket tb = new TokenBucket(delta, 10, 0); @@ -88,6 +98,7 @@ public class TokenBucketTest extends TestCase { assertTrue(tb.has()); } + @Test public void testRefill() throws Exception { TokenBucket tb = new TokenBucket(FILL_DELTA_VERY_SHORT, 10, 10); @@ -107,6 +118,7 @@ public class TokenBucketTest extends TestCase { assertEquals(10, tb.get(100)); } + @Test public void testAverage() throws Exception { final int delta = 3; final int want = 60; @@ -124,6 +136,7 @@ public class TokenBucketTest extends TestCase { assertDuration(want * delta, SystemClock.elapsedRealtime() - start); } + @Test public void testBurst() throws Exception { final int delta = 2; final int capacity = 20; diff --git a/core/tests/utiltests/Android.bp b/core/tests/utiltests/Android.bp index 967047e74555..2ccee71c8ff7 100644 --- a/core/tests/utiltests/Android.bp +++ b/core/tests/utiltests/Android.bp @@ -54,21 +54,20 @@ android_test { android_ravenwood_test { name: "FrameworksUtilTestsRavenwood", + libs: [ + "android.test.mock", + ], static_libs: [ "androidx.annotation_annotation", "androidx.test.rules", "mockito_ravenwood", + "frameworks-base-testutils", + "servicestests-utils", ], srcs: [ - "src/android/util/AtomicFileTest.java", - "src/android/util/DataUnitTest.java", - "src/android/util/EventLogTest.java", - "src/android/util/IndentingPrintWriterTest.java", - "src/android/util/IntArrayTest.java", - "src/android/util/LocalLogTest.java", - "src/android/util/LongArrayTest.java", - "src/android/util/SlogTest.java", - "src/android/util/TimeUtilsTest.java", + "src/android/util/IRemoteMemoryIntArray.aidl", + "src/android/util/**/*.java", + "src/com/android/internal/util/**/*.java", ], auto_gen_config: true, } diff --git a/core/tests/utiltests/src/android/util/MemoryIntArrayTest.java b/core/tests/utiltests/src/android/util/MemoryIntArrayTest.java index 1966e122ee5b..51013e4b4f00 100644 --- a/core/tests/utiltests/src/android/util/MemoryIntArrayTest.java +++ b/core/tests/utiltests/src/android/util/MemoryIntArrayTest.java @@ -23,11 +23,14 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import android.os.Parcel; +import android.platform.test.annotations.IgnoreUnderRavenwood; +import android.platform.test.ravenwood.RavenwoodRule; -import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.runner.AndroidJUnit4; import libcore.io.IoUtils; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -35,13 +38,17 @@ import java.lang.reflect.Field; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; - -import androidx.test.ext.junit.runners.AndroidJUnit4; @RunWith(AndroidJUnit4.class) +@IgnoreUnderRavenwood(blockedBy = MemoryIntArray.class) public class MemoryIntArrayTest { + @Rule + public final RavenwoodRule mRavenwood = new RavenwoodRule(); + static { - System.loadLibrary("cutils"); - System.loadLibrary("memoryintarraytest"); + if (!RavenwoodRule.isUnderRavenwood()) { + System.loadLibrary("cutils"); + System.loadLibrary("memoryintarraytest"); + } } @Test diff --git a/core/tests/utiltests/src/android/util/MetadataReaderTest.java b/core/tests/utiltests/src/android/util/MetadataReaderTest.java index a828edbac6ab..24e9e0cd38c9 100644 --- a/core/tests/utiltests/src/android/util/MetadataReaderTest.java +++ b/core/tests/utiltests/src/android/util/MetadataReaderTest.java @@ -16,25 +16,35 @@ package android.util; +import static org.junit.Assert.assertEquals; + import android.media.ExifInterface; import android.os.Bundle; +import android.platform.test.annotations.IgnoreUnderRavenwood; +import android.platform.test.ravenwood.RavenwoodRule; import android.provider.DocumentsContract; import android.provider.MetadataReader; -import libcore.io.IoUtils; +import androidx.test.runner.AndroidJUnit4; -import junit.framework.TestCase; +import libcore.io.IoUtils; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.runner.RunWith; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; -public class MetadataReaderTest extends TestCase { +@RunWith(AndroidJUnit4.class) +@IgnoreUnderRavenwood(blockedBy = MetadataReader.class) +public class MetadataReaderTest { + @Rule + public final RavenwoodRule mRavenwood = new RavenwoodRule(); private InputStream mInputStream; private Bundle mData; diff --git a/core/tests/utiltests/src/android/util/SystemConfigFileCommitEventLoggerTest.java b/core/tests/utiltests/src/android/util/SystemConfigFileCommitEventLoggerTest.java index 5f6c20141c37..3bb79ec91d09 100644 --- a/core/tests/utiltests/src/android/util/SystemConfigFileCommitEventLoggerTest.java +++ b/core/tests/utiltests/src/android/util/SystemConfigFileCommitEventLoggerTest.java @@ -21,12 +21,22 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import android.os.SystemClock; +import android.platform.test.annotations.IgnoreUnderRavenwood; +import android.platform.test.ravenwood.RavenwoodRule; +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.Mockito; - +@RunWith(AndroidJUnit4.class) +@IgnoreUnderRavenwood(blockedBy = SystemConfigFileCommitEventLogger.class) public class SystemConfigFileCommitEventLoggerTest { + @Rule + public final RavenwoodRule mRavenwood = new RavenwoodRule(); + @Test public void testSimple() throws Exception { var logger = spy(new SystemConfigFileCommitEventLogger("name")); diff --git a/core/tests/utiltests/src/com/android/internal/util/ArrayUtilsTest.java b/core/tests/utiltests/src/com/android/internal/util/ArrayUtilsTest.java index 72f3af640b67..0c5e9664bbde 100644 --- a/core/tests/utiltests/src/com/android/internal/util/ArrayUtilsTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/ArrayUtilsTest.java @@ -17,19 +17,28 @@ package com.android.internal.util; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import junit.framework.TestCase; - /** * Tests for {@link ArrayUtils} */ -public class ArrayUtilsTest extends TestCase { +@RunWith(AndroidJUnit4.class) +public class ArrayUtilsTest { + @Test public void testContains() throws Exception { final Object A = new Object(); final Object B = new Object(); @@ -46,6 +55,7 @@ public class ArrayUtilsTest extends TestCase { assertFalse(ArrayUtils.contains(new Object[] { null }, A)); } + @Test public void testIndexOf() throws Exception { final Object A = new Object(); final Object B = new Object(); @@ -66,6 +76,7 @@ public class ArrayUtilsTest extends TestCase { assertEquals(2, ArrayUtils.indexOf(new Object[] { A, null, B }, B)); } + @Test public void testContainsAll() throws Exception { final Object A = new Object(); final Object B = new Object(); @@ -86,6 +97,7 @@ public class ArrayUtilsTest extends TestCase { assertFalse(ArrayUtils.containsAll(new Object[] { A }, new Object[] { null })); } + @Test public void testContainsInt() throws Exception { assertTrue(ArrayUtils.contains(new int[] { 1, 2, 3 }, 1)); assertTrue(ArrayUtils.contains(new int[] { 1, 2, 3 }, 2)); @@ -96,6 +108,7 @@ public class ArrayUtilsTest extends TestCase { assertFalse(ArrayUtils.contains(new int[] { }, 2)); } + @Test public void testAppendInt() throws Exception { assertArrayEquals(new int[] { 1 }, ArrayUtils.appendInt(null, 1)); @@ -107,6 +120,7 @@ public class ArrayUtilsTest extends TestCase { ArrayUtils.appendInt(new int[] { 1, 2 }, 1)); } + @Test public void testRemoveInt() throws Exception { assertNull(ArrayUtils.removeInt(null, 1)); assertArrayEquals(new int[] { }, @@ -123,6 +137,7 @@ public class ArrayUtilsTest extends TestCase { ArrayUtils.removeInt(new int[] { 1, 2, 3, 1 }, 1)); } + @Test public void testContainsLong() throws Exception { assertTrue(ArrayUtils.contains(new long[] { 1, 2, 3 }, 1)); assertTrue(ArrayUtils.contains(new long[] { 1, 2, 3 }, 2)); @@ -133,6 +148,7 @@ public class ArrayUtilsTest extends TestCase { assertFalse(ArrayUtils.contains(new long[] { }, 2)); } + @Test public void testAppendLong() throws Exception { assertArrayEquals(new long[] { 1 }, ArrayUtils.appendLong(null, 1)); @@ -144,6 +160,7 @@ public class ArrayUtilsTest extends TestCase { ArrayUtils.appendLong(new long[] { 1, 2 }, 1)); } + @Test public void testRemoveLong() throws Exception { assertNull(ArrayUtils.removeLong(null, 1)); assertArrayEquals(new long[] { }, @@ -160,6 +177,7 @@ public class ArrayUtilsTest extends TestCase { ArrayUtils.removeLong(new long[] { 1, 2, 3, 1 }, 1)); } + @Test public void testConcat_zeroObjectArrays() { // empty varargs array assertArrayEquals(new String[] {}, ArrayUtils.concat(String.class)); @@ -167,16 +185,19 @@ public class ArrayUtilsTest extends TestCase { assertArrayEquals(new String[] {}, ArrayUtils.concat(String.class, (String[][]) null)); } + @Test public void testConcat_oneObjectArray() { assertArrayEquals(new String[] { "1", "2" }, ArrayUtils.concat(String.class, new String[] { "1", "2" })); } + @Test public void testConcat_oneEmptyObjectArray() { assertArrayEquals(new String[] {}, ArrayUtils.concat(String.class, (String[]) null)); assertArrayEquals(new String[] {}, ArrayUtils.concat(String.class, new String[] {})); } + @Test public void testConcat_twoObjectArrays() { assertArrayEquals(new Long[] { 1L }, ArrayUtils.concat(Long.class, new Long[] { 1L }, new Long[] {})); @@ -188,6 +209,7 @@ public class ArrayUtilsTest extends TestCase { ArrayUtils.concat(Long.class, new Long[] { 1L, 2L }, new Long[] { 3L, 4L })); } + @Test public void testConcat_twoEmptyObjectArrays() { assertArrayEquals(new Long[] {}, ArrayUtils.concat(Long.class, null, null)); assertArrayEquals(new Long[] {}, ArrayUtils.concat(Long.class, new Long[] {}, null)); @@ -196,6 +218,7 @@ public class ArrayUtilsTest extends TestCase { ArrayUtils.concat(Long.class, new Long[] {}, new Long[] {})); } + @Test public void testConcat_threeObjectArrays() { String[] array1 = { "1", "2" }; String[] array2 = { "3", "4" }; @@ -205,6 +228,7 @@ public class ArrayUtilsTest extends TestCase { assertArrayEquals(expectation, ArrayUtils.concat(String.class, array1, array2, array3)); } + @Test public void testConcat_threeObjectArraysWithNull() { String[] array1 = { "1", "2" }; String[] array2 = null; @@ -214,6 +238,7 @@ public class ArrayUtilsTest extends TestCase { assertArrayEquals(expectation, ArrayUtils.concat(String.class, array1, array2, array3)); } + @Test public void testConcat_zeroByteArrays() { // empty varargs array assertArrayEquals(new byte[] {}, ArrayUtils.concat()); @@ -221,15 +246,18 @@ public class ArrayUtilsTest extends TestCase { assertArrayEquals(new byte[] {}, ArrayUtils.concat((byte[][]) null)); } + @Test public void testConcat_oneByteArray() { assertArrayEquals(new byte[] { 1, 2 }, ArrayUtils.concat(new byte[] { 1, 2 })); } + @Test public void testConcat_oneEmptyByteArray() { assertArrayEquals(new byte[] {}, ArrayUtils.concat((byte[]) null)); assertArrayEquals(new byte[] {}, ArrayUtils.concat(new byte[] {})); } + @Test public void testConcat_twoByteArrays() { assertArrayEquals(new byte[] { 1 }, ArrayUtils.concat(new byte[] { 1 }, new byte[] {})); assertArrayEquals(new byte[] { 1 }, ArrayUtils.concat(new byte[] {}, new byte[] { 1 })); @@ -239,6 +267,7 @@ public class ArrayUtilsTest extends TestCase { ArrayUtils.concat(new byte[] { 1, 2 }, new byte[] { 3, 4 })); } + @Test public void testConcat_twoEmptyByteArrays() { assertArrayEquals(new byte[] {}, ArrayUtils.concat((byte[]) null, null)); assertArrayEquals(new byte[] {}, ArrayUtils.concat(new byte[] {}, null)); @@ -246,6 +275,7 @@ public class ArrayUtilsTest extends TestCase { assertArrayEquals(new byte[] {}, ArrayUtils.concat(new byte[] {}, new byte[] {})); } + @Test public void testConcat_threeByteArrays() { byte[] array1 = { 1, 2 }; byte[] array2 = { 3, 4 }; @@ -255,6 +285,7 @@ public class ArrayUtilsTest extends TestCase { assertArrayEquals(expectation, ArrayUtils.concat(array1, array2, array3)); } + @Test public void testConcat_threeByteArraysWithNull() { byte[] array1 = { 1, 2 }; byte[] array2 = null; @@ -264,6 +295,7 @@ public class ArrayUtilsTest extends TestCase { assertArrayEquals(expectation, ArrayUtils.concat(array1, array2, array3)); } + @Test @SmallTest public void testUnstableRemoveIf() throws Exception { java.util.function.Predicate<Object> isNull = new java.util.function.Predicate<Object>() { @@ -357,31 +389,37 @@ public class ArrayUtilsTest extends TestCase { assertEquals(0, collection.size()); } + @Test @SmallTest public void testThrowsIfOutOfBounds_passesWhenRangeInsideArray() { ArrayUtils.throwsIfOutOfBounds(10, 2, 6); } + @Test @SmallTest public void testThrowsIfOutOfBounds_passesWhenRangeIsWholeArray() { ArrayUtils.throwsIfOutOfBounds(10, 0, 10); } + @Test @SmallTest public void testThrowsIfOutOfBounds_passesWhenEmptyRangeAtStart() { ArrayUtils.throwsIfOutOfBounds(10, 0, 0); } + @Test @SmallTest public void testThrowsIfOutOfBounds_passesWhenEmptyRangeAtEnd() { ArrayUtils.throwsIfOutOfBounds(10, 10, 0); } + @Test @SmallTest public void testThrowsIfOutOfBounds_passesWhenEmptyArray() { ArrayUtils.throwsIfOutOfBounds(0, 0, 0); } + @Test @SmallTest public void testThrowsIfOutOfBounds_failsWhenRangeStartNegative() { try { @@ -392,6 +430,7 @@ public class ArrayUtilsTest extends TestCase { } } + @Test @SmallTest public void testThrowsIfOutOfBounds_failsWhenCountNegative() { try { @@ -402,6 +441,7 @@ public class ArrayUtilsTest extends TestCase { } } + @Test @SmallTest public void testThrowsIfOutOfBounds_failsWhenRangeStartTooHigh() { try { @@ -412,6 +452,7 @@ public class ArrayUtilsTest extends TestCase { } } + @Test @SmallTest public void testThrowsIfOutOfBounds_failsWhenRangeEndTooHigh() { try { @@ -422,6 +463,7 @@ public class ArrayUtilsTest extends TestCase { } } + @Test @SmallTest public void testThrowsIfOutOfBounds_failsWhenLengthNegative() { try { @@ -432,6 +474,7 @@ public class ArrayUtilsTest extends TestCase { } } + @Test @SmallTest public void testThrowsIfOutOfBounds_failsWhenOverflowRangeEndTooHigh() { try { diff --git a/core/tests/utiltests/src/com/android/internal/util/BitwiseStreamsTest.java b/core/tests/utiltests/src/com/android/internal/util/BitwiseStreamsTest.java index 306f58fcce2e..092d6999e85e 100644 --- a/core/tests/utiltests/src/com/android/internal/util/BitwiseStreamsTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/BitwiseStreamsTest.java @@ -16,20 +16,23 @@ package com.android.internal.util; -import com.android.internal.util.BitwiseInputStream; -import com.android.internal.util.BitwiseOutputStream; -import com.android.internal.util.HexDump; - -import android.test.AndroidTestCase; -import android.test.suitebuilder.annotation.SmallTest; +import static org.junit.Assert.assertEquals; import android.util.Log; +import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + import java.util.Random; -public class BitwiseStreamsTest extends AndroidTestCase { +@RunWith(AndroidJUnit4.class) +public class BitwiseStreamsTest { private final static String LOG_TAG = "BitwiseStreamsTest"; + @Test @SmallTest public void testOne() throws Exception { int offset = 3; @@ -45,6 +48,7 @@ public class BitwiseStreamsTest extends AndroidTestCase { assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup)); } + @Test @SmallTest public void testTwo() throws Exception { int offset = 3; @@ -59,6 +63,7 @@ public class BitwiseStreamsTest extends AndroidTestCase { assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup)); } + @Test @SmallTest public void testThree() throws Exception { int offset = 4; @@ -73,6 +78,7 @@ public class BitwiseStreamsTest extends AndroidTestCase { assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup)); } + @Test @SmallTest public void testFour() throws Exception { int offset = 7; @@ -90,6 +96,7 @@ public class BitwiseStreamsTest extends AndroidTestCase { assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup)); } + @Test @SmallTest public void testFive() throws Exception { Random random = new Random(); @@ -111,6 +118,7 @@ public class BitwiseStreamsTest extends AndroidTestCase { } } + @Test @SmallTest public void testSix() throws Exception { int num_runs = 10; @@ -134,6 +142,7 @@ public class BitwiseStreamsTest extends AndroidTestCase { Log.d(LOG_TAG, "repeated encode-decode took " + (end - start) + " ms"); } + @Test @SmallTest public void testExpandArray() throws Exception { Random random = new Random(); diff --git a/core/tests/utiltests/src/com/android/internal/util/CallbackRegistryTest.java b/core/tests/utiltests/src/com/android/internal/util/CallbackRegistryTest.java index 1581abb5a9c6..9a1402e04011 100644 --- a/core/tests/utiltests/src/com/android/internal/util/CallbackRegistryTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/CallbackRegistryTest.java @@ -15,19 +15,21 @@ */ package com.android.internal.util; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import androidx.test.runner.AndroidJUnit4; import org.junit.Test; +import org.junit.runner.RunWith; import java.util.ArrayList; import java.util.Objects; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -public class CallbackRegistryTest extends TestCase { +@RunWith(AndroidJUnit4.class) +public class CallbackRegistryTest { final Integer callback1 = 1; final Integer callback2 = 2; @@ -50,6 +52,7 @@ public class CallbackRegistryTest extends TestCase { deepNotifyCount[callback]++; } + @Test public void testAddListener() { CallbackRegistry.NotifierCallback<Integer, CallbackRegistryTest, Integer> notifier = new CallbackRegistry.NotifierCallback<Integer, CallbackRegistryTest, Integer>() { @@ -89,6 +92,7 @@ public class CallbackRegistryTest extends TestCase { assertEquals(otherListener, callbacks.get(0)); } + @Test public void testSimpleNotify() { CallbackRegistry.NotifierCallback<Integer, CallbackRegistryTest, Integer> notifier = new CallbackRegistry.NotifierCallback<Integer, CallbackRegistryTest, Integer>() { @@ -108,6 +112,7 @@ public class CallbackRegistryTest extends TestCase { assertEquals(1, notify2); } + @Test public void testRemoveWhileNotifying() { CallbackRegistry.NotifierCallback<Integer, CallbackRegistryTest, Integer> notifier = new CallbackRegistry.NotifierCallback<Integer, CallbackRegistryTest, Integer>() { @@ -135,6 +140,7 @@ public class CallbackRegistryTest extends TestCase { assertEquals(callback3, callbacks.get(0)); } + @Test public void testDeepRemoveWhileNotifying() { CallbackRegistry.NotifierCallback<Integer, CallbackRegistryTest, Integer> notifier = new CallbackRegistry.NotifierCallback<Integer, CallbackRegistryTest, Integer>() { @@ -159,6 +165,7 @@ public class CallbackRegistryTest extends TestCase { assertEquals(0, callbacks.size()); } + @Test public void testAddRemovedListener() { CallbackRegistry.NotifierCallback<Integer, CallbackRegistryTest, Integer> notifier = @@ -191,6 +198,7 @@ public class CallbackRegistryTest extends TestCase { assertEquals(1, notify3); } + @Test public void testVeryDeepRemoveWhileNotifying() { final Integer[] callbacks = new Integer[deepNotifyCount.length]; for (int i = 0; i < callbacks.length; i++) { @@ -221,6 +229,7 @@ public class CallbackRegistryTest extends TestCase { assertEquals(0, callbackList.size()); } + @Test public void testClear() { CallbackRegistry.NotifierCallback<Integer, CallbackRegistryTest, Integer> notifier = new CallbackRegistry.NotifierCallback<Integer, CallbackRegistryTest, Integer>() { @@ -245,6 +254,7 @@ public class CallbackRegistryTest extends TestCase { } } + @Test public void testNestedClear() { CallbackRegistry.NotifierCallback<Integer, CallbackRegistryTest, Integer> notifier = new CallbackRegistry.NotifierCallback<Integer, CallbackRegistryTest, Integer>() { @@ -268,6 +278,7 @@ public class CallbackRegistryTest extends TestCase { assertEquals(0, callbackList.size()); } + @Test public void testIsEmpty() throws Exception { CallbackRegistry.NotifierCallback<Integer, CallbackRegistryTest, Integer> notifier = new CallbackRegistry.NotifierCallback<Integer, CallbackRegistryTest, Integer>() { @@ -284,6 +295,7 @@ public class CallbackRegistryTest extends TestCase { assertFalse(registry.isEmpty()); } + @Test public void testClone() throws Exception { CallbackRegistry.NotifierCallback<Integer, CallbackRegistryTest, Integer> notifier = new CallbackRegistry.NotifierCallback<Integer, CallbackRegistryTest, Integer>() { diff --git a/core/tests/utiltests/src/com/android/internal/util/CharSequencesTest.java b/core/tests/utiltests/src/com/android/internal/util/CharSequencesTest.java index 469a4ccd05e4..988854038e53 100644 --- a/core/tests/utiltests/src/com/android/internal/util/CharSequencesTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/CharSequencesTest.java @@ -18,12 +18,26 @@ package com.android.internal.util; import static com.android.internal.util.CharSequences.forAsciiBytes; -import android.test.suitebuilder.annotation.SmallTest; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; -import junit.framework.TestCase; +import android.platform.test.annotations.IgnoreUnderRavenwood; +import android.platform.test.ravenwood.RavenwoodRule; -public class CharSequencesTest extends TestCase { +import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +@IgnoreUnderRavenwood(blockedBy = CharSequences.class) +public class CharSequencesTest { + @Rule + public final RavenwoodRule mRavenwood = new RavenwoodRule(); + + @Test @SmallTest public void testCharSequences() { String s = "Hello Bob"; diff --git a/core/tests/utiltests/src/com/android/internal/util/FastXmlSerializerTest.java b/core/tests/utiltests/src/com/android/internal/util/FastXmlSerializerTest.java index 3cef33621a01..7723d589a723 100644 --- a/core/tests/utiltests/src/com/android/internal/util/FastXmlSerializerTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/FastXmlSerializerTest.java @@ -16,13 +16,23 @@ package com.android.internal.util; -import android.test.suitebuilder.annotation.LargeTest; -import android.test.suitebuilder.annotation.SmallTest; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import android.platform.test.annotations.IgnoreUnderRavenwood; +import android.platform.test.ravenwood.RavenwoodRule; +import android.provider.DeviceConfig; import android.util.Log; import android.util.Xml; -import junit.framework.TestCase; +import androidx.test.filters.LargeTest; +import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlSerializer; @@ -34,7 +44,9 @@ import java.nio.charset.StandardCharsets; * Tests for {@link FastXmlSerializer} */ @SmallTest -public class FastXmlSerializerTest extends TestCase { +@RunWith(AndroidJUnit4.class) +@IgnoreUnderRavenwood(blockedBy = Xml.class) +public class FastXmlSerializerTest { private static final String TAG = "FastXmlSerializerTest"; private static final boolean ENABLE_DUMP = false; // DO NOT SUBMIT WITH TRUE. @@ -42,6 +54,10 @@ public class FastXmlSerializerTest extends TestCase { private static final String ROOT_TAG = "root"; private static final String ATTR = "attr"; + @Rule + public final RavenwoodRule mRavenwood = new RavenwoodRule(); + + @Test public void testEmptyText() throws Exception { final ByteArrayOutputStream stream = new ByteArrayOutputStream(); @@ -128,6 +144,7 @@ public class FastXmlSerializerTest extends TestCase { return ok; } + @Test @LargeTest public void testAllCharacters() throws Exception { boolean ok = true; diff --git a/core/tests/utiltests/src/com/android/internal/util/FileRotatorTest.java b/core/tests/utiltests/src/com/android/internal/util/FileRotatorTest.java index 73e47e1635b4..47c17959d28d 100644 --- a/core/tests/utiltests/src/com/android/internal/util/FileRotatorTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/FileRotatorTest.java @@ -23,16 +23,23 @@ import static android.text.format.DateUtils.SECOND_IN_MILLIS; import static android.text.format.DateUtils.WEEK_IN_MILLIS; import static android.text.format.DateUtils.YEAR_IN_MILLIS; -import android.test.AndroidTestCase; -import android.test.suitebuilder.annotation.Suppress; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + import android.util.Log; +import androidx.test.runner.AndroidJUnit4; + import com.android.internal.util.FileRotator.Reader; import com.android.internal.util.FileRotator.Writer; -import com.android.internal.util.test.FsUtil; import com.google.android.collect.Lists; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; @@ -40,6 +47,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; @@ -47,7 +55,8 @@ import java.util.Random; /** * Tests for {@link FileRotator}. */ -public class FileRotatorTest extends AndroidTestCase { +@RunWith(AndroidJUnit4.class) +public class FileRotatorTest { private static final String TAG = "FileRotatorTest"; private File mBasePath; @@ -59,14 +68,12 @@ public class FileRotatorTest extends AndroidTestCase { // TODO: test throwing rolls back correctly - @Override - protected void setUp() throws Exception { - super.setUp(); - - mBasePath = getContext().getFilesDir(); - FsUtil.deleteContents(mBasePath); + @Before + public void setUp() throws Exception { + mBasePath = Files.createTempDirectory(TAG).toFile(); } + @Test public void testEmpty() throws Exception { final FileRotator rotate1 = new FileRotator( mBasePath, PREFIX, DAY_IN_MILLIS, WEEK_IN_MILLIS); @@ -85,6 +92,7 @@ public class FileRotatorTest extends AndroidTestCase { assertReadAll(rotate2); } + @Test public void testCombine() throws Exception { final FileRotator rotate = new FileRotator( mBasePath, PREFIX, DAY_IN_MILLIS, WEEK_IN_MILLIS); @@ -106,6 +114,7 @@ public class FileRotatorTest extends AndroidTestCase { assertReadAll(rotate, "bar"); } + @Test public void testRotate() throws Exception { final FileRotator rotate = new FileRotator( mBasePath, PREFIX, DAY_IN_MILLIS, WEEK_IN_MILLIS); @@ -138,6 +147,7 @@ public class FileRotatorTest extends AndroidTestCase { assertReadAll(rotate, "bar", "baz"); } + @Test public void testDelete() throws Exception { final FileRotator rotate = new FileRotator( mBasePath, PREFIX, MINUTE_IN_MILLIS, DAY_IN_MILLIS); @@ -168,6 +178,7 @@ public class FileRotatorTest extends AndroidTestCase { assertReadAll(rotate); } + @Test public void testThrowRestoresBackup() throws Exception { final FileRotator rotate = new FileRotator( mBasePath, PREFIX, MINUTE_IN_MILLIS, DAY_IN_MILLIS); @@ -201,6 +212,7 @@ public class FileRotatorTest extends AndroidTestCase { assertReadAll(rotate, "foo"); } + @Test public void testOtherFilesAndMalformed() throws Exception { final FileRotator rotate = new FileRotator( mBasePath, PREFIX, SECOND_IN_MILLIS, SECOND_IN_MILLIS); @@ -229,6 +241,7 @@ public class FileRotatorTest extends AndroidTestCase { private static final String BLUE = "blue"; private static final String YELLOW = "yellow"; + @Test public void testQueryMatch() throws Exception { final FileRotator rotate = new FileRotator( mBasePath, PREFIX, HOUR_IN_MILLIS, YEAR_IN_MILLIS); @@ -277,6 +290,7 @@ public class FileRotatorTest extends AndroidTestCase { assertReadMatching(rotate, Long.MIN_VALUE, TEST_TIME - DAY_IN_MILLIS); } + @Test public void testClockRollingBackwards() throws Exception { final FileRotator rotate = new FileRotator( mBasePath, PREFIX, DAY_IN_MILLIS, YEAR_IN_MILLIS); @@ -325,7 +339,8 @@ public class FileRotatorTest extends AndroidTestCase { assertReadAll(rotate, "meow", "yay"); } - @Suppress + @Test + @Ignore public void testFuzz() throws Exception { final FileRotator rotate = new FileRotator( mBasePath, PREFIX, HOUR_IN_MILLIS, DAY_IN_MILLIS); @@ -352,6 +367,7 @@ public class FileRotatorTest extends AndroidTestCase { Log.d(TAG, Arrays.toString(mBasePath.list())); } + @Test public void testRecoverAtomic() throws Exception { write("rotator.1024-2048", "foo"); write("rotator.1024-2048.backup", "bar"); @@ -366,6 +382,7 @@ public class FileRotatorTest extends AndroidTestCase { assertReadAll(rotate, "bar"); } + @Test public void testReadSorted() throws Exception { write("rotator.1024-2048", "2"); write("rotator.2048-4096", "3"); @@ -376,11 +393,11 @@ public class FileRotatorTest extends AndroidTestCase { assertReadAll(rotate, "1", "2", "3"); } + @Test public void testFileSystemInaccessible() throws Exception { - File inaccessibleDir = null; - String dirPath = getContext().getFilesDir() + File.separator + "inaccessible"; - inaccessibleDir = new File(dirPath); - final FileRotator rotate = new FileRotator(inaccessibleDir, PREFIX, SECOND_IN_MILLIS, SECOND_IN_MILLIS); + File inaccessibleDir = mBasePath.toPath().resolve("does_not_exist").toFile(); + final FileRotator rotate = new FileRotator(inaccessibleDir, PREFIX, + SECOND_IN_MILLIS, SECOND_IN_MILLIS); // rotate should not throw on dir not mkdir-ed (or otherwise inaccessible) rotate.maybeRotate(TEST_TIME); diff --git a/core/tests/utiltests/src/com/android/internal/util/HeavyHitterSketchTest.java b/core/tests/utiltests/src/com/android/internal/util/HeavyHitterSketchTest.java index f2285a12e30a..74184cacc91c 100644 --- a/core/tests/utiltests/src/com/android/internal/util/HeavyHitterSketchTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/HeavyHitterSketchTest.java @@ -16,10 +16,17 @@ package com.android.internal.util; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import android.util.ArraySet; import android.util.Pair; -import junit.framework.TestCase; +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; import java.util.ArrayList; import java.util.Comparator; @@ -32,7 +39,8 @@ import java.util.stream.Collectors; /** * Tests for {@link HeavyHitterSketch}. */ -public final class HeavyHitterSketchTest extends TestCase { +@RunWith(AndroidJUnit4.class) +public final class HeavyHitterSketchTest { private static final float EPSILON = 0.00001f; @@ -163,6 +171,7 @@ public final class HeavyHitterSketchTest extends TestCase { return input; } + @Test public void testPositive() throws Exception { // Simple case verify(new int[]{2, 9, 9, 9, 7, 6, 4, 9, 9, 9, 3, 9}, 2, new int[]{9}, @@ -179,6 +188,7 @@ public final class HeavyHitterSketchTest extends TestCase { new float[]{0.32f, 0.24f, 0.16f, 0.08f}); } + @Test public void testNegative() throws Exception { // Simple case verifyNotExpected(new int[]{2, 9, 9, 9, 7, 6, 4, 9, 9, 9, 3, 9}, 2, new int[]{0, 1, 2}); @@ -193,6 +203,7 @@ public final class HeavyHitterSketchTest extends TestCase { verifyNotExpected(input, 12, new int[]{0, 1, 2, 1000, 1005}); } + @Test public void testFalsePositive() throws Exception { // Simple case verifyNotExpected(new int[]{2, 9, 2, 2, 7, 6, 4, 9, 9, 9, 3, 9}, 2, new int[]{9}); diff --git a/core/tests/utiltests/src/com/android/internal/util/InlinePresentationStyleUtilsTest.java b/core/tests/utiltests/src/com/android/internal/util/InlinePresentationStyleUtilsTest.java index 35c56818a108..7203b8c6a8c8 100644 --- a/core/tests/utiltests/src/com/android/internal/util/InlinePresentationStyleUtilsTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/InlinePresentationStyleUtilsTest.java @@ -22,20 +22,28 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import android.graphics.Color; import android.os.Binder; import android.os.Bundle; +import android.platform.test.annotations.IgnoreUnderRavenwood; +import android.platform.test.ravenwood.RavenwoodRule; import androidx.test.filters.SmallTest; import androidx.test.runner.AndroidJUnit4; import com.android.internal.widget.InlinePresentationStyleUtils; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @RunWith(AndroidJUnit4.class) @SmallTest +@IgnoreUnderRavenwood(blockedBy = InlinePresentationStyleUtils.class) public class InlinePresentationStyleUtilsTest { + @Rule + public final RavenwoodRule mRavenwood = new RavenwoodRule(); + @Test public void testBundleEquals_empty() { Bundle bundle1 = new Bundle(); diff --git a/core/tests/utiltests/src/com/android/internal/util/LockPatternUtilsTest.java b/core/tests/utiltests/src/com/android/internal/util/LockPatternUtilsTest.java index 0b7019995acb..0df5b0a4093e 100644 --- a/core/tests/utiltests/src/com/android/internal/util/LockPatternUtilsTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/LockPatternUtilsTest.java @@ -43,6 +43,8 @@ import android.content.pm.UserInfo; import android.os.Looper; import android.os.RemoteException; import android.os.UserManager; +import android.platform.test.annotations.IgnoreUnderRavenwood; +import android.platform.test.ravenwood.RavenwoodRule; import android.provider.Settings; import android.test.mock.MockContentResolver; @@ -58,6 +60,7 @@ import com.android.internal.widget.LockPatternUtils; import com.google.android.collect.Lists; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; @@ -68,7 +71,10 @@ import java.util.List; @RunWith(AndroidJUnit4.class) @SmallTest +@IgnoreUnderRavenwood(blockedBy = LockPatternUtils.class) public class LockPatternUtilsTest { + @Rule + public final RavenwoodRule mRavenwood = new RavenwoodRule(); private static final int DEMO_USER_ID = 5; diff --git a/core/tests/utiltests/src/com/android/internal/util/MessageUtilsTest.java b/core/tests/utiltests/src/com/android/internal/util/MessageUtilsTest.java index 32b969a8d1b5..36f238e7429c 100644 --- a/core/tests/utiltests/src/com/android/internal/util/MessageUtilsTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/MessageUtilsTest.java @@ -19,10 +19,13 @@ package com.android.internal.util; import static org.junit.Assert.*; import com.android.internal.util.MessageUtils; -import android.test.suitebuilder.annotation.SmallTest; +import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; + import android.util.SparseArray; import org.junit.Test; +import org.junit.runner.RunWith; class A { @@ -48,6 +51,7 @@ class B { * Unit tests for {@link com.android.util.MessageUtils}. */ @SmallTest +@RunWith(AndroidJUnit4.class) public class MessageUtilsTest { private static final Class[] CLASSES = { A.class, B.class }; diff --git a/core/tests/utiltests/src/com/android/internal/util/MimeIconUtilsTest.java b/core/tests/utiltests/src/com/android/internal/util/MimeIconUtilsTest.java index 4412c2c6949c..6c3479722526 100644 --- a/core/tests/utiltests/src/com/android/internal/util/MimeIconUtilsTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/MimeIconUtilsTest.java @@ -16,12 +16,27 @@ package com.android.internal.util; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; + +import android.platform.test.annotations.IgnoreUnderRavenwood; +import android.platform.test.ravenwood.RavenwoodRule; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; /** * Tests for {@link MimeIconUtils}. */ -public class MimeIconUtilsTest extends TestCase { +@RunWith(AndroidJUnit4.class) +@IgnoreUnderRavenwood(blockedBy = MimeIconUtils.class) +public class MimeIconUtilsTest { + @Rule + public final RavenwoodRule mRavenwood = new RavenwoodRule(); + + @Test public void testSimple() throws Exception { assertEquals("PNG image", MimeIconUtils.getTypeInfo("image/png").getLabel()); diff --git a/core/tests/utiltests/src/com/android/internal/util/ObjectUtilsTest.java b/core/tests/utiltests/src/com/android/internal/util/ObjectUtilsTest.java index 443183eeba4d..4eaacfffd433 100644 --- a/core/tests/utiltests/src/com/android/internal/util/ObjectUtilsTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/ObjectUtilsTest.java @@ -15,11 +15,18 @@ */ package com.android.internal.util; -import android.test.AndroidTestCase; -import android.test.suitebuilder.annotation.SmallTest; +import static org.junit.Assert.assertEquals; + +import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; @SmallTest -public class ObjectUtilsTest extends AndroidTestCase { +@RunWith(AndroidJUnit4.class) +public class ObjectUtilsTest { + @Test public void testCompare() { assertEquals(0, ObjectUtils.compare(null, null)); assertEquals(1, ObjectUtils.compare("a", null)); diff --git a/core/tests/utiltests/src/com/android/internal/util/ObservableServiceConnectionTest.java b/core/tests/utiltests/src/com/android/internal/util/ObservableServiceConnectionTest.java index d124ad9ddfb0..c852e9f17bc0 100644 --- a/core/tests/utiltests/src/com/android/internal/util/ObservableServiceConnectionTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/ObservableServiceConnectionTest.java @@ -29,14 +29,19 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.os.IBinder; +import android.platform.test.annotations.IgnoreUnderRavenwood; +import android.platform.test.ravenwood.RavenwoodRule; import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; import com.android.internal.util.ObservableServiceConnection.ServiceTransformer; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -46,7 +51,12 @@ import java.util.Queue; import java.util.concurrent.Executor; @SmallTest +@RunWith(AndroidJUnit4.class) +@IgnoreUnderRavenwood(blockedBy = ObservableServiceConnection.class) public class ObservableServiceConnectionTest { + @Rule + public final RavenwoodRule mRavenwood = new RavenwoodRule(); + private static final ComponentName COMPONENT_NAME = new ComponentName("test.package", "component"); diff --git a/core/tests/utiltests/src/com/android/internal/util/PersistentServiceConnectionTest.java b/core/tests/utiltests/src/com/android/internal/util/PersistentServiceConnectionTest.java index fee46545ac62..096f303247ff 100644 --- a/core/tests/utiltests/src/com/android/internal/util/PersistentServiceConnectionTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/PersistentServiceConnectionTest.java @@ -30,6 +30,10 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.os.IBinder; +import android.platform.test.annotations.IgnoreUnderRavenwood; +import android.platform.test.ravenwood.RavenwoodRule; + +import androidx.test.runner.AndroidJUnit4; import com.android.internal.util.ObservableServiceConnection.ServiceTransformer; import com.android.server.testutils.OffsettableClock; @@ -37,7 +41,9 @@ import com.android.server.testutils.TestHandler; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -45,7 +51,12 @@ import java.util.ArrayDeque; import java.util.Queue; import java.util.concurrent.Executor; +@RunWith(AndroidJUnit4.class) +@IgnoreUnderRavenwood(blockedBy = PersistentServiceConnection.class) public class PersistentServiceConnectionTest { + @Rule + public final RavenwoodRule mRavenwood = new RavenwoodRule(); + private static final ComponentName COMPONENT_NAME = new ComponentName("test.package", "component"); private static final int MAX_RETRIES = 2; diff --git a/core/tests/utiltests/src/com/android/internal/util/ProcFileReaderTest.java b/core/tests/utiltests/src/com/android/internal/util/ProcFileReaderTest.java index b93276053caf..4c00c1667e3c 100644 --- a/core/tests/utiltests/src/com/android/internal/util/ProcFileReaderTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/ProcFileReaderTest.java @@ -16,7 +16,15 @@ package com.android.internal.util; -import android.test.AndroidTestCase; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; import java.io.ByteArrayInputStream; import java.io.File; @@ -28,8 +36,10 @@ import java.nio.file.Files; /** * Tests for {@link ProcFileReader}. */ -public class ProcFileReaderTest extends AndroidTestCase { +@RunWith(AndroidJUnit4.class) +public class ProcFileReaderTest { + @Test public void testEmpty() throws Exception { final ProcFileReader reader = buildReader(""); @@ -43,6 +53,7 @@ public class ProcFileReaderTest extends AndroidTestCase { assertFalse(reader.hasMoreData()); } + @Test public void testSingleString() throws Exception { final ProcFileReader reader = buildReader("a\nb\nc\n"); @@ -59,6 +70,7 @@ public class ProcFileReaderTest extends AndroidTestCase { assertFalse(reader.hasMoreData()); } + @Test public void testMixedNumbersSkip() throws Exception { final ProcFileReader reader = buildReader("1 2 3\n4 abc_def 5 6 7 8 9\n10\n"); @@ -79,6 +91,7 @@ public class ProcFileReaderTest extends AndroidTestCase { assertFalse(reader.hasMoreData()); } + @Test public void testBufferSize() throws Exception { // read numbers using very small buffer size, exercising fillBuf() final ProcFileReader reader = buildReader("1 21 3 41 5 61 7 81 9 10\n", 3); @@ -97,6 +110,7 @@ public class ProcFileReaderTest extends AndroidTestCase { assertFalse(reader.hasMoreData()); } + @Test public void testBlankLines() throws Exception { final ProcFileReader reader = buildReader("1\n\n2\n\n3\n"); @@ -117,6 +131,7 @@ public class ProcFileReaderTest extends AndroidTestCase { assertFalse(reader.hasMoreData()); } + @Test public void testMinMax() throws Exception { final ProcFileReader reader = buildReader( "1 -1024 9223372036854775807 -9223372036854775808\n"); @@ -129,6 +144,7 @@ public class ProcFileReaderTest extends AndroidTestCase { assertFalse(reader.hasMoreData()); } + @Test public void testDelimiterNeverFound() throws Exception { final ProcFileReader reader = buildReader("teststringwithoutdelimiters"); @@ -141,6 +157,7 @@ public class ProcFileReaderTest extends AndroidTestCase { } } + @Test public void testLargerThanBuffer() throws Exception { // try finishing line larger than buffer final ProcFileReader reader = buildReader("1 teststringlongerthanbuffer\n", 4); @@ -155,6 +172,7 @@ public class ProcFileReaderTest extends AndroidTestCase { } } + @Test public void testOptionalLongs() throws Exception { final ProcFileReader reader = buildReader("123 456\n789\n"); @@ -169,6 +187,7 @@ public class ProcFileReaderTest extends AndroidTestCase { assertEquals(-1L, reader.nextOptionalLong(-1L)); } + @Test public void testInvalidLongs() throws Exception { final ProcFileReader reader = buildReader("12: 34\n56 78@#\n"); @@ -183,6 +202,7 @@ public class ProcFileReaderTest extends AndroidTestCase { assertFalse(reader.hasMoreData()); } + @Test public void testConsecutiveDelimiters() throws Exception { final ProcFileReader reader = buildReader("1 2 3 4 5\n"); @@ -195,6 +215,7 @@ public class ProcFileReaderTest extends AndroidTestCase { assertFalse(reader.hasMoreData()); } + @Test public void testIgnore() throws Exception { final ProcFileReader reader = buildReader("a b c\n"); @@ -209,6 +230,7 @@ public class ProcFileReaderTest extends AndroidTestCase { assertFalse(reader.hasMoreData()); } + @Test public void testRewind() throws Exception { final ProcFileReader reader = buildReader("abc\n"); @@ -224,7 +246,7 @@ public class ProcFileReaderTest extends AndroidTestCase { assertFalse(reader.hasMoreData()); } - + @Test public void testRewindFileInputStream() throws Exception { File tempFile = File.createTempFile("procfile", null, null); Files.write(tempFile.toPath(), "abc\n".getBytes(StandardCharsets.US_ASCII)); diff --git a/core/tests/utiltests/src/com/android/internal/util/QuickSelectTest.java b/core/tests/utiltests/src/com/android/internal/util/QuickSelectTest.java index 1b9d2ef58c10..1eb5e3ab7d6c 100644 --- a/core/tests/utiltests/src/com/android/internal/util/QuickSelectTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/QuickSelectTest.java @@ -16,7 +16,12 @@ package com.android.internal.util; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; import java.util.Arrays; import java.util.List; @@ -24,8 +29,10 @@ import java.util.List; /** * Tests for {@link QuickSelect}. */ -public final class QuickSelectTest extends TestCase { +@RunWith(AndroidJUnit4.class) +public class QuickSelectTest { + @Test public void testQuickSelect() throws Exception { test((List<Integer>) null, 0, null); test(Arrays.asList(), -1, null); diff --git a/core/tests/utiltests/src/com/android/internal/util/StringPoolTest.java b/core/tests/utiltests/src/com/android/internal/util/StringPoolTest.java index f67fd516fcf6..6523ed7fabb7 100644 --- a/core/tests/utiltests/src/com/android/internal/util/StringPoolTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/StringPoolTest.java @@ -16,12 +16,21 @@ package com.android.internal.util; -import android.test.AndroidTestCase; -import android.test.suitebuilder.annotation.SmallTest; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertSame; + +import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; @SmallTest -public final class StringPoolTest extends AndroidTestCase { +@RunWith(AndroidJUnit4.class) +public final class StringPoolTest { + @Test public void testStringPool() { StringPool stringPool = new StringPool(); String bcd = stringPool.get(new char[] { 'a', 'b', 'c', 'd', 'e' }, 1, 3); @@ -29,6 +38,7 @@ public final class StringPoolTest extends AndroidTestCase { assertSame(bcd, stringPool.get(new char[] { 'a', 'b', 'c', 'd', 'e' }, 1, 3)); } + @Test public void testHashCollision() { StringPool stringPool = new StringPool(); char[] a = { (char) 1, (char) 0 }; diff --git a/core/tests/utiltests/src/com/android/internal/util/WakeupMessageTest.java b/core/tests/utiltests/src/com/android/internal/util/WakeupMessageTest.java index 734ebef0007e..b0db8a1da3a8 100644 --- a/core/tests/utiltests/src/com/android/internal/util/WakeupMessageTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/WakeupMessageTest.java @@ -24,11 +24,17 @@ import android.content.Context; import android.os.Handler; import android.os.Looper; import android.os.Message; -import android.test.suitebuilder.annotation.SmallTest; +import android.platform.test.annotations.IgnoreUnderRavenwood; +import android.platform.test.ravenwood.RavenwoodRule; + +import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -40,6 +46,8 @@ import org.mockito.stubbing.Answer; * Unit tests for {@link com.android.internal.util.WakeupMessage}. */ @SmallTest +@RunWith(AndroidJUnit4.class) +@IgnoreUnderRavenwood(blockedBy = WakeupMessage.class) public class WakeupMessageTest { private static final String TEST_CMD_NAME = "TEST cmd Name"; private static final int TEST_CMD = 18; @@ -47,11 +55,16 @@ public class WakeupMessageTest { private static final int TEST_ARG2 = 182; private static final Object TEST_OBJ = "hello"; + @Rule + public final RavenwoodRule mRavenwood = new RavenwoodRule.Builder() + .setProvideMainThread(true) + .build(); + @Mock Context mContext; @Mock AlarmManager mAlarmManager; WakeupMessage mMessage; // Make a spy so that we can verify calls to it - @Spy MessageCapturingHandler mHandler = new MessageCapturingHandler(); + @Spy MessageCapturingHandler mHandler; ArgumentCaptor<AlarmManager.OnAlarmListener> mListenerCaptor = ArgumentCaptor.forClass(AlarmManager.OnAlarmListener.class); @@ -85,6 +98,8 @@ public class WakeupMessageTest { */ @Before public void setUp() { + mHandler = new MessageCapturingHandler(); + MockitoAnnotations.initMocks(this); when(mContext.getSystemService(Context.ALARM_SERVICE)).thenReturn(mAlarmManager); diff --git a/core/tests/utiltests/src/com/android/internal/util/XmlUtilsTest.java b/core/tests/utiltests/src/com/android/internal/util/XmlUtilsTest.java index 0484068bf900..24eb213751bc 100644 --- a/core/tests/utiltests/src/com/android/internal/util/XmlUtilsTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/XmlUtilsTest.java @@ -17,13 +17,17 @@ package com.android.internal.util; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; import android.util.Xml; +import androidx.test.runner.AndroidJUnit4; + import com.android.modules.utils.TypedXmlPullParser; import com.android.modules.utils.TypedXmlSerializer; -import junit.framework.TestCase; +import org.junit.Test; +import org.junit.runner.RunWith; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -32,9 +36,11 @@ import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; -public class XmlUtilsTest extends TestCase { +@RunWith(AndroidJUnit4.class) +public class XmlUtilsTest { // https://code.google.com/p/android/issues/detail?id=63717 + @Test public void testMapWithNullKeys() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); Map<String, Object> map = new HashMap<String, Object>(); @@ -48,6 +54,7 @@ public class XmlUtilsTest extends TestCase { assertEquals("fooValue", deserialized.get("foo")); } + @Test public void testreadWriteXmlByteArrayValue() throws Exception { byte[] testByteArray = {0x1 , 0xa, 0xb, 0x9, 0x34, (byte) 0xaa, (byte) 0xba, (byte) 0x99}; diff --git a/core/tests/utiltests/src/com/android/internal/util/test/FakeSettingsProviderTest.java b/core/tests/utiltests/src/com/android/internal/util/test/FakeSettingsProviderTest.java index f2be109c8602..502d6b6d7270 100644 --- a/core/tests/utiltests/src/com/android/internal/util/test/FakeSettingsProviderTest.java +++ b/core/tests/utiltests/src/com/android/internal/util/test/FakeSettingsProviderTest.java @@ -16,33 +16,41 @@ package com.android.internal.util.test; -import android.content.ContentResolver; -import android.database.ContentObserver; -import android.net.Uri; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import android.content.ContentProvider; +import android.platform.test.annotations.IgnoreUnderRavenwood; +import android.platform.test.ravenwood.RavenwoodRule; import android.provider.Settings; -import android.test.AndroidTestCase; import android.test.mock.MockContentResolver; -import android.test.mock.MockContext; -import android.test.suitebuilder.annotation.SmallTest; -import android.test.suitebuilder.annotation.Suppress; -import android.util.Log; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; +import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; /** * Unit tests for FakeSettingsProvider. */ -public class FakeSettingsProviderTest extends AndroidTestCase { +@RunWith(AndroidJUnit4.class) +@IgnoreUnderRavenwood(blockedBy = ContentProvider.class) +public class FakeSettingsProviderTest { + @Rule + public final RavenwoodRule mRavenwood = new RavenwoodRule(); private MockContentResolver mCr; - @Override + @Before public void setUp() throws Exception { mCr = new MockContentResolver(); mCr.addProvider(Settings.AUTHORITY, new FakeSettingsProvider()); } + @Test @SmallTest public void testBasicOperation() throws Exception { String settingName = Settings.System.SCREEN_BRIGHTNESS; diff --git a/ravenwood/framework-minus-apex-ravenwood-policies.txt b/ravenwood/framework-minus-apex-ravenwood-policies.txt index a75bba6a347f..63f687264990 100644 --- a/ravenwood/framework-minus-apex-ravenwood-policies.txt +++ b/ravenwood/framework-minus-apex-ravenwood-policies.txt @@ -81,15 +81,11 @@ class android.util.Patterns stubclass class android.util.UtilConfig stubclass # Internals -class com.android.internal.util.FastMath stubclass -class com.android.internal.util.FastPrintWriter stubclass -class com.android.internal.util.GrowingArrayUtils stubclass -class com.android.internal.util.LineBreakBufferedWriter stubclass -class com.android.internal.util.Parcelling stubclass +class com.android.internal.util.FileRotator stubclass +class com.android.internal.util.HexDump stubclass +class com.android.internal.util.MessageUtils stubclass class com.android.internal.util.Preconditions stubclass -class com.android.internal.util.StringPool stubclass - -class com.android.internal.os.SomeArgs stubclass +class com.android.internal.util.TokenBucket stubclass # Parcel class android.os.Parcel stubclass diff --git a/ravenwood/junit-src/android/platform/test/annotations/IgnoreUnderRavenwood.java b/ravenwood/junit-src/android/platform/test/annotations/IgnoreUnderRavenwood.java index edb0442e7b29..916dd5943059 100644 --- a/ravenwood/junit-src/android/platform/test/annotations/IgnoreUnderRavenwood.java +++ b/ravenwood/junit-src/android/platform/test/annotations/IgnoreUnderRavenwood.java @@ -36,7 +36,7 @@ import java.lang.annotation.Target; * * @hide */ -@Target(ElementType.METHOD) +@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface IgnoreUnderRavenwood { /** diff --git a/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodRule.java b/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodRule.java index 9db5b9895749..d175713eb92f 100644 --- a/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodRule.java +++ b/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodRule.java @@ -93,7 +93,7 @@ public class RavenwoodRule implements TestRule { /** * Return if the current process is running under a Ravenwood test environment. */ - public boolean isUnderRavenwood() { + public static boolean isUnderRavenwood() { return IS_UNDER_RAVENWOOD; } @@ -102,6 +102,9 @@ public class RavenwoodRule implements TestRule { return new Statement() { @Override public void evaluate() throws Throwable { + if (description.getTestClass().getAnnotation(IgnoreUnderRavenwood.class) != null) { + Assume.assumeFalse(IS_UNDER_RAVENWOOD); + } if (description.getAnnotation(IgnoreUnderRavenwood.class) != null) { Assume.assumeFalse(IS_UNDER_RAVENWOOD); } diff --git a/ravenwood/ravenwood-annotation-allowed-classes.txt b/ravenwood/ravenwood-annotation-allowed-classes.txt index 72e9ba367bc9..0e33d5620d60 100644 --- a/ravenwood/ravenwood-annotation-allowed-classes.txt +++ b/ravenwood/ravenwood-annotation-allowed-classes.txt @@ -4,6 +4,7 @@ com.android.internal.util.ArrayUtils android.util.AtomicFile android.util.DataUnit +android.util.DumpableContainer android.util.EventLog android.util.IntArray android.util.LongArray @@ -66,3 +67,30 @@ android.graphics.Rect android.graphics.RectF com.android.server.LocalServices + +com.android.internal.os.SomeArgs + +com.android.internal.util.BitUtils +com.android.internal.util.BitwiseInputStream +com.android.internal.util.BitwiseOutputStream +com.android.internal.util.CallbackRegistry +com.android.internal.util.DumpableContainer +com.android.internal.util.dump.DumpableContainerImpl +com.android.internal.util.DumpUtils +com.android.internal.util.FastMath +com.android.internal.util.FastPrintWriter +com.android.internal.util.FileRotator +com.android.internal.util.GrowingArrayUtils +com.android.internal.util.HeavyHitterSketch +com.android.internal.util.LineBreakBufferedWriter +com.android.internal.util.ObjectUtils +com.android.internal.util.Parcelling +com.android.internal.util.ParseUtils +com.android.internal.util.ProcFileReader +com.android.internal.util.QuickSelect +com.android.internal.util.RingBuffer +com.android.internal.util.StringPool + +com.google.android.collect.Lists +com.google.android.collect.Maps +com.google.android.collect.Sets diff --git a/tools/hoststubgen/hoststubgen/helper-framework-runtime-src/libcore-fake/libcore/util/HexEncoding.java b/tools/hoststubgen/hoststubgen/helper-framework-runtime-src/libcore-fake/libcore/util/HexEncoding.java new file mode 100644 index 000000000000..cc2fb7bbf236 --- /dev/null +++ b/tools/hoststubgen/hoststubgen/helper-framework-runtime-src/libcore-fake/libcore/util/HexEncoding.java @@ -0,0 +1,240 @@ +/* + * Copyright (C) 2014 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 libcore.util; + +/** + * Hexadecimal encoding where each byte is represented by two hexadecimal digits. + * @hide + */ +public class HexEncoding { + + private static final char[] LOWER_CASE_DIGITS = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' + }; + + private static final char[] UPPER_CASE_DIGITS = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' + }; + + /** Hidden constructor to prevent instantiation. */ + private HexEncoding() {} + + /** + * Encodes the provided byte as a two-digit hexadecimal String value. + * + * @param b byte to encode + * @param upperCase {@code true} to use uppercase letters, {@code false} + * for lowercase + * @return the encoded string + * + * @hide + */ + public static String encodeToString(byte b, boolean upperCase) { + char[] digits = upperCase ? UPPER_CASE_DIGITS : LOWER_CASE_DIGITS; + char[] buf = new char[2]; // We always want two digits. + buf[0] = digits[(b >> 4) & 0xf]; + buf[1] = digits[b & 0xf]; + return new String(buf, 0, 2); + } + + /** + * Encodes the provided data as a sequence of hexadecimal characters. + * + * @param data byte array to encode + * @return the encoded data, using uppercase letters + * + * @hide + */ + public static char[] encode(byte[] data) { + return encode(data, 0, data.length, true /* upperCase */); + } + + /** + * Encodes the provided data as a sequence of hexadecimal characters. + * + * @param data byte array to encode + * @param upperCase {@code true} to use uppercase letters, {@code false} + * for lowercase + * @return the encoded data + * + * @hide + */ + public static char[] encode(byte[] data, boolean upperCase) { + return encode(data, 0, data.length, upperCase); + } + + /** + * Encodes the provided data as a sequence of hexadecimal characters. + * + * @param data byte array containing the data to encode + * @param offset offset of the data to encode in the {@code data} array + * @param len length of the data to encode in the {@code data} array + * @return the encoded data, using uppercase letters + * + * @hide + */ + public static char[] encode(byte[] data, int offset, int len) { + return encode(data, offset, len, true /* upperCase */); + } + + /** + * Encodes the provided data as a sequence of hexadecimal characters. + */ + private static char[] encode(byte[] data, int offset, int len, boolean upperCase) { + char[] digits = upperCase ? UPPER_CASE_DIGITS : LOWER_CASE_DIGITS; + char[] result = new char[len * 2]; + for (int i = 0; i < len; i++) { + byte b = data[offset + i]; + int resultIndex = 2 * i; + result[resultIndex] = (digits[(b >> 4) & 0x0f]); + result[resultIndex + 1] = (digits[b & 0x0f]); + } + + return result; + } + + /** + * Encodes the provided data as a sequence of hexadecimal characters. + * + * @param data byte array to encode + * @return the encoded data, using uppercase letters + * + * @hide + */ + public static String encodeToString(byte[] data) { + return encodeToString(data, true /* upperCase */); + } + + /** + * Encodes the provided data as a sequence of hexadecimal characters. + * + * @param data byte array to encode. + * @param upperCase {@code true} to use uppercase letters, {@code false} + * for lowercase + * @return the encoded data + * + * @hide + */ + public static String encodeToString(byte[] data, boolean upperCase) { + return new String(encode(data, upperCase)); + } + + /** + * Decodes the provided hexadecimal sequence. Odd-length inputs are not + * allowed. + * + * @param encoded string of hexadecimal characters to decode. Letters + * can be either uppercase or lowercase. + * @return the decoded data + * @throws IllegalArgumentException if the input is malformed + * + * @hide + */ + public static byte[] decode(String encoded) throws IllegalArgumentException { + return decode(encoded.toCharArray()); + } + + /** + * Decodes the provided hexadecimal sequence. + * + * @param encoded string of hexadecimal characters to decode. Letters + * can be either uppercase or lowercase. + * @param allowSingleChar If {@code true} odd-length inputs are allowed and + * the first character is interpreted as the lower bits of the first + * result byte. If {@code false} odd-length inputs are not allowed. + * @return the decoded data + * @throws IllegalArgumentException if the input is malformed + * + * @hide + */ + public static byte[] decode(String encoded, boolean allowSingleChar) + throws IllegalArgumentException { + return decode(encoded.toCharArray(), allowSingleChar); + } + + /** + * Decodes the provided hexadecimal sequence. Odd-length inputs are not + * allowed. + * + * @param encoded char array of hexadecimal characters to decode. Letters + * can be either uppercase or lowercase. + * @return the decoded data + * @throws IllegalArgumentException if the input is malformed + * + * @hide + */ + public static byte[] decode(char[] encoded) throws IllegalArgumentException { + return decode(encoded, false); + } + + /** + * Decodes the provided hexadecimal sequence. + * + * @param encoded char array of hexadecimal characters to decode. Letters + * can be either uppercase or lowercase. + * @param allowSingleChar If {@code true} odd-length inputs are allowed and + * the first character is interpreted as the lower bits of the first + * result byte. If {@code false} odd-length inputs are not allowed. + * @return the decoded data + * @throws IllegalArgumentException if the input is malformed + * + * @hide + */ + public static byte[] decode(char[] encoded, boolean allowSingleChar) + throws IllegalArgumentException { + int encodedLength = encoded.length; + int resultLengthBytes = (encodedLength + 1) / 2; + byte[] result = new byte[resultLengthBytes]; + + int resultOffset = 0; + int i = 0; + if (allowSingleChar) { + if ((encodedLength % 2) != 0) { + // Odd number of digits -- the first digit is the lower 4 bits of the first result + // byte. + result[resultOffset++] = (byte) toDigit(encoded, i); + i++; + } + } else { + if ((encodedLength % 2) != 0) { + throw new IllegalArgumentException("Invalid input length: " + encodedLength); + } + } + + for (; i < encodedLength; i += 2) { + result[resultOffset++] = (byte) ((toDigit(encoded, i) << 4) | toDigit(encoded, i + 1)); + } + + return result; + } + + private static int toDigit(char[] str, int offset) throws IllegalArgumentException { + // NOTE: that this isn't really a code point in the traditional sense, since we're + // just rejecting surrogate pairs outright. + int pseudoCodePoint = str[offset]; + + if ('0' <= pseudoCodePoint && pseudoCodePoint <= '9') { + return pseudoCodePoint - '0'; + } else if ('a' <= pseudoCodePoint && pseudoCodePoint <= 'f') { + return 10 + (pseudoCodePoint - 'a'); + } else if ('A' <= pseudoCodePoint && pseudoCodePoint <= 'F') { + return 10 + (pseudoCodePoint - 'A'); + } + + throw new IllegalArgumentException("Illegal char: " + str[offset] + " at offset " + offset); + } +} -- GitLab