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

Commit f3c5518a authored by Makoto Onuki's avatar Makoto Onuki
Browse files

De-flake RavenwoodServicesTest

(cherry-pick from aosp)

Flag: EXEMPT host test change only
Fix: 376755462
Test: atest RavenwoodServicesTest
Change-Id: I28096aee40a9f6d8c23eea8c11a59f3fe8c5b121
parent e67b42fe
Loading
Loading
Loading
Loading
+33 −12
Original line number Original line Diff line number Diff line
@@ -19,17 +19,22 @@ package com.android.ravenwoodtest.servicestest;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;


import android.content.Context;
import android.content.Context;
import android.hardware.SerialManager;
import android.hardware.SerialManager;
import android.hardware.SerialManagerInternal;
import android.hardware.SerialManagerInternal;
import android.platform.test.ravenwood.RavenwoodRule;
import android.platform.test.ravenwood.RavenwoodConfig;
import android.platform.test.ravenwood.RavenwoodConfig.Config;


import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;


import com.android.server.LocalServices;
import com.android.server.LocalServices;


import org.junit.Rule;
import com.google.common.collect.Lists;

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


@@ -37,18 +42,25 @@ import org.junit.runner.RunWith;
public class RavenwoodServicesTest {
public class RavenwoodServicesTest {
    private static final String TEST_VIRTUAL_PORT = "virtual:example";
    private static final String TEST_VIRTUAL_PORT = "virtual:example";


    @Rule
    @Config
    public final RavenwoodRule mRavenwood = new RavenwoodRule.Builder()
    public static final RavenwoodConfig sRavenwood = new RavenwoodConfig.Builder()
            .setProcessSystem()
            .setProcessSystem()
            .setServicesRequired(SerialManager.class)
            .setServicesRequired(SerialManager.class)
            .build();
            .build();


    private Context mContext;

    @Before
    public void setUp() {
        mContext = InstrumentationRegistry.getInstrumentation().getContext();
    }

    @Test
    @Test
    public void testDefined() {
    public void testDefined() {
        final SerialManager fromName = (SerialManager)
        final SerialManager fromName = (SerialManager)
                mRavenwood.getContext().getSystemService(Context.SERIAL_SERVICE);
                mContext.getSystemService(Context.SERIAL_SERVICE);
        final SerialManager fromClass =
        final SerialManager fromClass =
                mRavenwood.getContext().getSystemService(SerialManager.class);
                mContext.getSystemService(SerialManager.class);
        assertNotNull(fromName);
        assertNotNull(fromName);
        assertNotNull(fromClass);
        assertNotNull(fromClass);
        assertEquals(fromName, fromClass);
        assertEquals(fromName, fromClass);
@@ -61,9 +73,9 @@ public class RavenwoodServicesTest {
        // Verify that we can obtain a manager, and talk to the backend service, and that no
        // Verify that we can obtain a manager, and talk to the backend service, and that no
        // serial ports are configured by default
        // serial ports are configured by default
        final SerialManager service = (SerialManager)
        final SerialManager service = (SerialManager)
                mRavenwood.getContext().getSystemService(Context.SERIAL_SERVICE);
                mContext.getSystemService(Context.SERIAL_SERVICE);
        final String[] ports = service.getSerialPorts();
        final String[] ports = service.getSerialPorts();
        final String[] refPorts = mRavenwood.getContext().getResources().getStringArray(
        final String[] refPorts = mContext.getResources().getStringArray(
                com.android.internal.R.array.config_serialPorts);
                com.android.internal.R.array.config_serialPorts);
        assertArrayEquals(refPorts, ports);
        assertArrayEquals(refPorts, ports);
    }
    }
@@ -71,7 +83,7 @@ public class RavenwoodServicesTest {
    @Test
    @Test
    public void testDriven() {
    public void testDriven() {
        final SerialManager service = (SerialManager)
        final SerialManager service = (SerialManager)
                mRavenwood.getContext().getSystemService(Context.SERIAL_SERVICE);
                mContext.getSystemService(Context.SERIAL_SERVICE);
        final SerialManagerInternal internal = LocalServices.getService(
        final SerialManagerInternal internal = LocalServices.getService(
                SerialManagerInternal.class);
                SerialManagerInternal.class);


@@ -79,8 +91,17 @@ public class RavenwoodServicesTest {
            throw new UnsupportedOperationException(
            throw new UnsupportedOperationException(
                    "Needs socketpair() to offer accurate emulation");
                    "Needs socketpair() to offer accurate emulation");
        });
        });
        try {
            final String[] ports = service.getSerialPorts();
            final String[] ports = service.getSerialPorts();
        assertEquals(1, ports.length);
            for (var port : ports) {
        assertEquals(TEST_VIRTUAL_PORT, ports[0]);
                if (TEST_VIRTUAL_PORT.equals(port)) {
                    return; // Pass
                }
            }
            fail("Virtual port " + TEST_VIRTUAL_PORT + " not found. Actual="
                    + Lists.newArrayList(ports));
        } finally {
            internal.removeVirtualSerialPortForTest(TEST_VIRTUAL_PORT);
        }
    }
    }
}
}