Files
mg-utility/tests/VehiclePowerOffGatewayTest.java

93 lines
5.0 KiB
Java

package cloud.molberg.mgutility;
import android.os.IBinder;
import android.os.Parcel;
public final class VehiclePowerOffGatewayTest {
private static final class FakeService implements IBinder {
final String token;
String descriptor;
int gear = 1;
float speed;
int writes;
boolean available = true;
boolean accepted = true;
FakeService(String token) { this.token = token; descriptor = token; }
@Override public String getInterfaceDescriptor() { return descriptor; }
@Override public boolean transact(int code, Parcel data, Parcel reply, int flags) {
check(token.equals(data.readString()), "Interface token must match OEM ABI");
if (!available) return false;
if (VehicleSettingsGateway.SETTING_TOKEN.equals(token)) {
check(code == 0xad, "Only the OEM power-off transaction may be sent");
check(flags == IBinder.FLAG_ONEWAY && reply == null, "Power request must be one-way without reply");
check(data.readInt() == 2, "OEM power-off payload must be integer 2");
writes++;
return accepted;
}
check(flags == 0 && reply != null, "Telemetry must be synchronous");
reply.writeNoException();
if (code == 0x05) reply.writeInt(gear);
else if (code == 0x03) reply.writeFloat(speed);
else throw new AssertionError("Unexpected telemetry transaction: " + code);
return true;
}
}
private static VehicleSettingsGateway gateway(final FakeService setting, final FakeService condition) {
return new VehicleSettingsGateway(new IBinder() {
@Override public String getInterfaceDescriptor() { return VehicleSettingsGateway.HUB_TOKEN; }
@Override public boolean transact(int code, Parcel data, Parcel reply, int flags) {
check(code == 1 && flags == 0, "Hub lookup transaction");
check(VehicleSettingsGateway.HUB_TOKEN.equals(data.readString()), "Hub token");
String service = data.readString();
reply.writeNoException();
reply.writeStrongBinder("vehiclesetting".equals(service) ? setting
: "vehiclecondition".equals(service) ? condition : null);
return true;
}
});
}
public static void main(String[] args) {
FakeService setting = new FakeService(VehicleSettingsGateway.SETTING_TOKEN);
FakeService condition = new FakeService(VehicleSettingsGateway.CONDITION_TOKEN);
VehicleSettingsGateway gateway = gateway(setting, condition);
String version = VehiclePowerOffController.SUPPORTED_SERVICE_VERSION;
expect(VehiclePowerOffController.Result.CANCELLED, gateway.powerOffController(version).request(false));
for (String unsupported : new String[] { null, "", "mg4", "2022", "VehicleSettingsService.44042" }) {
expect(VehiclePowerOffController.Result.UNSUPPORTED, gateway.powerOffController(unsupported).request(true));
}
setting.descriptor = "unknown";
expect(VehiclePowerOffController.Result.UNSUPPORTED, gateway.powerOffController(version).request(true));
setting.descriptor = setting.token;
condition.descriptor = "unknown";
expect(VehiclePowerOffController.Result.UNSUPPORTED, gateway.powerOffController(version).request(true));
condition.descriptor = condition.token;
condition.available = false;
expect(VehiclePowerOffController.Result.NOT_PARKED, gateway.powerOffController(version).request(true));
condition.available = true;
condition.speed = Float.NaN;
expect(VehiclePowerOffController.Result.NOT_STATIONARY, gateway.powerOffController(version).request(true));
check(setting.writes == 0, "Blocked/cancelled requests never reach the setter");
condition.speed = 0f;
VehiclePowerOffController controller = gateway.powerOffController(version);
expect(VehiclePowerOffController.Result.REQUEST_SENT, controller.request(true));
expect(VehiclePowerOffController.Result.ALREADY_REQUESTED, controller.request(true));
check(setting.writes == 1, "Exactly one correctly encoded request");
setting.accepted = false;
expect(VehiclePowerOffController.Result.FAILED, gateway.powerOffController(version).request(true));
expect(VehiclePowerOffController.Result.UNSUPPORTED, gateway(null, condition).powerOffController(version).request(true));
expect(VehiclePowerOffController.Result.UNSUPPORTED, gateway(setting, null).powerOffController(version).request(true));
System.out.println("VehiclePowerOffGatewayTest: OK");
}
private static void expect(VehiclePowerOffController.Result expected, VehiclePowerOffController.Result actual) {
check(expected == actual, "Expected " + expected + ", got " + actual);
}
private static void check(boolean condition, String message) {
if (!condition) throw new AssertionError(message);
}
}