Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b831120a authored by Jeff Sharkey's avatar Jeff Sharkey Committed by Android (Google) Code Review
Browse files

Merge "Offer `TestLooperManager` on Ravenwood." into main

parents 4f7a3b18 c91451a6
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -79,6 +79,7 @@ import java.util.concurrent.TimeoutException;
 * implementation is described to the system through an AndroidManifest.xml's
 * <instrumentation> tag.
 */
@android.ravenwood.annotation.RavenwoodKeepPartialClass
public class Instrumentation {

    /**
@@ -132,6 +133,7 @@ public class Instrumentation {
    private UiAutomation mUiAutomation;
    private final Object mAnimationCompleteLock = new Object();

    @android.ravenwood.annotation.RavenwoodKeep
    public Instrumentation() {
    }

@@ -142,6 +144,7 @@ public class Instrumentation {
     * reflection, but it will serve as noticeable discouragement from
     * doing such a thing.
     */
    @android.ravenwood.annotation.RavenwoodReplace
    private void checkInstrumenting(String method) {
        // Check if we have an instrumentation context, as init should only get called by
        // the system in startup processes that are being instrumented.
@@ -151,6 +154,11 @@ public class Instrumentation {
        }
    }

    private void checkInstrumenting$ravenwood(String method) {
        // At the moment, Ravenwood doesn't attach a Context, but we're only ever
        // running code as part of tests, so we continue quietly
    }

    /**
     * Returns if it is being called in an instrumentation environment.
     *
@@ -2504,6 +2512,7 @@ public class Instrumentation {
     * Takes control of the execution of messages on the specified looper until
     * {@link TestLooperManager#release} is called.
     */
    @android.ravenwood.annotation.RavenwoodKeep
    public TestLooperManager acquireLooperManager(Looper looper) {
        checkInstrumenting("acquireLooperManager");
        return new TestLooperManager(looper);
+1 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import java.util.concurrent.LinkedBlockingQueue;
 * The test code may use {@link #next()} to acquire messages that have been queued to this
 * {@link Looper}/{@link MessageQueue} and then {@link #execute} to run any that desires.
 */
@android.ravenwood.annotation.RavenwoodKeepWholeClass
public class TestLooperManager {

    private static final ArraySet<Looper> sHeldLoopers = new ArraySet<>();
+9 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

@@ -120,6 +121,14 @@ public class BundleTest {
        assertFalse(b.isParcelled());
    }

    @Test
    public void testEmpty() throws Exception {
        assertNotNull(Bundle.EMPTY);
        assertEquals(0, Bundle.EMPTY.size());

        new Bundle(Bundle.EMPTY);
    }

    @Test
    @IgnoreUnderRavenwood(blockedBy = ParcelFileDescriptor.class)
    public void testCreateFromParcel() throws Exception {
+91 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.os;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import android.platform.test.ravenwood.RavenwoodRule;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

@RunWith(AndroidJUnit4.class)
public class TestLooperManagerTest {
    private static final String TAG = "TestLooperManagerTest";

    @Rule
    public final RavenwoodRule mRavenwood = new RavenwoodRule.Builder()
            .setProvideMainThread(true)
            .build();

    @Test
    public void testMainThread() throws Exception {
        doTest(Looper.getMainLooper());
    }

    @Test
    public void testCustomThread() throws Exception {
        final HandlerThread thread = new HandlerThread(TAG);
        thread.start();
        doTest(thread.getLooper());
    }

    private void doTest(Looper looper) throws Exception {
        final TestLooperManager tlm =
                InstrumentationRegistry.getInstrumentation().acquireLooperManager(looper);

        final Handler handler = new Handler(looper);
        final CountDownLatch latch = new CountDownLatch(1);

        assertFalse(tlm.hasMessages(handler, null, 42));

        handler.sendEmptyMessage(42);
        handler.post(() -> {
            latch.countDown();
        });
        assertTrue(tlm.hasMessages(handler, null, 42));
        assertFalse(latch.await(100, TimeUnit.MILLISECONDS));

        final Message first = tlm.next();
        assertEquals(42, first.what);
        assertNull(first.callback);
        tlm.execute(first);
        assertFalse(tlm.hasMessages(handler, null, 42));
        assertFalse(latch.await(100, TimeUnit.MILLISECONDS));
        tlm.recycle(first);

        final Message second = tlm.next();
        assertNotNull(second.callback);
        tlm.execute(second);
        assertFalse(tlm.hasMessages(handler, null, 42));
        assertTrue(latch.await(100, TimeUnit.MILLISECONDS));
        tlm.recycle(second);

        tlm.release();
    }
}
+17 −0
Original line number Diff line number Diff line
@@ -30,6 +30,9 @@ java_library {
        "junit-src/**/*.java",
        "junit-impl-src/**/*.java",
    ],
    static_libs: [
        "androidx.test.monitor-for-device",
    ],
    libs: [
        "framework-minus-apex.ravenwood",
        "junit",
@@ -61,3 +64,17 @@ java_host_for_device {
        "core-xml-for-host",
    ],
}

java_host_for_device {
    name: "androidx.test.monitor-for-device",
    libs: [
        "androidx.test.monitor-for-host",
    ],
}

java_device_for_host {
    name: "androidx.test.monitor-for-host",
    libs: [
        "androidx.test.monitor",
    ],
}
Loading