1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ravenwoodtest.servicestest; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotNull; 21 22 import android.content.Context; 23 import android.hardware.SerialManager; 24 import android.hardware.SerialManagerInternal; 25 import android.platform.test.ravenwood.RavenwoodRule; 26 27 import androidx.test.ext.junit.runners.AndroidJUnit4; 28 29 import com.android.server.LocalServices; 30 31 import org.junit.Rule; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 35 @RunWith(AndroidJUnit4.class) 36 public class RavenwoodServicesTest { 37 private static final String TEST_VIRTUAL_PORT = "virtual:example"; 38 39 @Rule 40 public final RavenwoodRule mRavenwood = new RavenwoodRule.Builder() 41 .setProcessSystem() 42 .setServicesRequired(SerialManager.class) 43 .build(); 44 45 @Test testDefined()46 public void testDefined() { 47 final SerialManager fromName = (SerialManager) 48 mRavenwood.getContext().getSystemService(Context.SERIAL_SERVICE); 49 final SerialManager fromClass = 50 mRavenwood.getContext().getSystemService(SerialManager.class); 51 assertNotNull(fromName); 52 assertNotNull(fromClass); 53 assertEquals(fromName, fromClass); 54 55 assertNotNull(LocalServices.getService(SerialManagerInternal.class)); 56 } 57 58 @Test testSimple()59 public void testSimple() { 60 // Verify that we can obtain a manager, and talk to the backend service, and that no 61 // serial ports are configured by default 62 final SerialManager service = (SerialManager) 63 mRavenwood.getContext().getSystemService(Context.SERIAL_SERVICE); 64 final String[] ports = service.getSerialPorts(); 65 assertEquals(0, ports.length); 66 } 67 68 @Test testDriven()69 public void testDriven() { 70 final SerialManager service = (SerialManager) 71 mRavenwood.getContext().getSystemService(Context.SERIAL_SERVICE); 72 final SerialManagerInternal internal = LocalServices.getService( 73 SerialManagerInternal.class); 74 75 internal.addVirtualSerialPortForTest(TEST_VIRTUAL_PORT, () -> { 76 throw new UnsupportedOperationException( 77 "Needs socketpair() to offer accurate emulation"); 78 }); 79 final String[] ports = service.getSerialPorts(); 80 assertEquals(1, ports.length); 81 assertEquals(TEST_VIRTUAL_PORT, ports[0]); 82 } 83 } 84