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

Commit 14bf0736 authored by Makoto Onuki's avatar Makoto Onuki
Browse files

De-flake RavenwoodServicesTest

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

import android.content.Context;
import android.hardware.SerialManager;
import android.hardware.SerialManagerInternal;
import android.platform.test.annotations.DisabledOnRavenwood;
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.platform.app.InstrumentationRegistry;

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.runner.RunWith;

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

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

    private Context mContext;

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

    @Test
    public void testDefined() {
        final SerialManager fromName = (SerialManager)
                mRavenwood.getContext().getSystemService(Context.SERIAL_SERVICE);
                mContext.getSystemService(Context.SERIAL_SERVICE);
        final SerialManager fromClass =
                mRavenwood.getContext().getSystemService(SerialManager.class);
                mContext.getSystemService(SerialManager.class);
        assertNotNull(fromName);
        assertNotNull(fromClass);
        assertEquals(fromName, fromClass);
@@ -63,9 +75,9 @@ public class RavenwoodServicesTest {
        // Verify that we can obtain a manager, and talk to the backend service, and that no
        // serial ports are configured by default
        final SerialManager service = (SerialManager)
                mRavenwood.getContext().getSystemService(Context.SERIAL_SERVICE);
                mContext.getSystemService(Context.SERIAL_SERVICE);
        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);
        assertArrayEquals(refPorts, ports);
    }
@@ -73,7 +85,7 @@ public class RavenwoodServicesTest {
    @Test
    public void testDriven() {
        final SerialManager service = (SerialManager)
                mRavenwood.getContext().getSystemService(Context.SERIAL_SERVICE);
                mContext.getSystemService(Context.SERIAL_SERVICE);
        final SerialManagerInternal internal = LocalServices.getService(
                SerialManagerInternal.class);

@@ -81,8 +93,17 @@ public class RavenwoodServicesTest {
            throw new UnsupportedOperationException(
                    "Needs socketpair() to offer accurate emulation");
        });
        try {
            final String[] ports = service.getSerialPorts();
        assertEquals(1, ports.length);
        assertEquals(TEST_VIRTUAL_PORT, ports[0]);
            for (var port : ports) {
                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);
        }
    }
}