117 lines
6.9 KiB
Java
117 lines
6.9 KiB
Java
package cloud.molberg.mgutility;
|
|
|
|
import android.os.IBinder;
|
|
import android.os.Parcel;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/** Verifies the IAirConditionService wire format and the seat-heat press loop against a fake car. */
|
|
public final class ClimateGatewayTest {
|
|
/** Fake car that mirrors the OEM AirConditionBinder press semantics. */
|
|
private static final class FakeCar implements IBinder {
|
|
int driverSeat, passengerSeat, wheel, power = 1, ac, temp = 22, fan = 3;
|
|
boolean stuck;
|
|
final List<String> writes = new ArrayList<String>();
|
|
|
|
@Override public String getInterfaceDescriptor() { return ClimateGateway.TOKEN; }
|
|
@Override public boolean transact(int code, Parcel data, Parcel reply, int flags) {
|
|
check(ClimateGateway.TOKEN.equals(data.readString()), "Air-condition interface token");
|
|
check(flags == 0 && reply != null, "Climate calls are synchronous");
|
|
switch (code) {
|
|
case ClimateGateway.SET_DRV_SEAT_HEAT: press(data, "drvSeat"); if (!stuck) driverSeat = next(driverSeat); break;
|
|
case ClimateGateway.SET_PSG_SEAT_HEAT: press(data, "psgSeat"); if (!stuck) passengerSeat = next(passengerSeat); break;
|
|
case ClimateGateway.SET_STEERING_WHEEL_HEAT: press(data, "wheel"); wheel = wheel == 0 ? 1 : 0; break;
|
|
case ClimateGateway.SET_AC: press(data, "ac"); ac = ac == 0 ? 1 : 0; break;
|
|
case ClimateGateway.SET_DRV_TEMP: temp = data.readInt(); writes.add("temp=" + temp); break;
|
|
case ClimateGateway.SET_AIR_VOLUME: fan = data.readInt(); writes.add("fan=" + fan); break;
|
|
case ClimateGateway.GET_DRV_SEAT_HEAT: reply.writeNoException(); reply.writeInt(driverSeat); return true;
|
|
case ClimateGateway.GET_PSG_SEAT_HEAT: reply.writeNoException(); reply.writeInt(passengerSeat); return true;
|
|
case ClimateGateway.GET_STEERING_WHEEL_HEAT: reply.writeNoException(); reply.writeInt(wheel); return true;
|
|
case ClimateGateway.GET_OUTSIDE_TEMP: reply.writeNoException(); reply.writeFloat(-10000f); return true;
|
|
case ClimateGateway.GET_DRV_TEMP: reply.writeNoException(); reply.writeInt(temp); return true;
|
|
case ClimateGateway.GET_AIR_VOLUME: reply.writeNoException(); reply.writeInt(fan); return true;
|
|
case ClimateGateway.GET_HVAC_POWER: reply.writeNoException(); reply.writeInt(power); return true;
|
|
case ClimateGateway.GET_AC: reply.writeNoException(); reply.writeInt(ac); return true;
|
|
default:
|
|
if (code >= 0x35) { reply.writeNoException(); reply.writeInt(-1); return true; }
|
|
throw new AssertionError("Unexpected transaction 0x" + Integer.toHexString(code));
|
|
}
|
|
reply.writeNoException();
|
|
return true;
|
|
}
|
|
private void press(Parcel data, String name) {
|
|
check(data.readInt() == ClimateGateway.PRESS, name + " must send the OEM press value 1");
|
|
writes.add(name);
|
|
}
|
|
private static int next(int level) { return level == 0 ? 3 : level - 1; }
|
|
}
|
|
|
|
private static ClimateGateway gateway(final FakeCar car) {
|
|
ClimateGateway g = new ClimateGateway(new IBinder() {
|
|
@Override public String getInterfaceDescriptor() { return ClimateGateway.HUB_TOKEN; }
|
|
@Override public boolean transact(int code, Parcel data, Parcel reply, int flags) {
|
|
check(code == 1, "Hub getService transaction");
|
|
check(ClimateGateway.HUB_TOKEN.equals(data.readString()), "Hub token");
|
|
check("aircondition".equals(data.readString()), "Hub service name");
|
|
reply.writeNoException();
|
|
reply.writeStrongBinder(car);
|
|
return true;
|
|
}
|
|
});
|
|
g.setSleeper(new ClimateGateway.Sleeper() { @Override public void sleep(long ms) { } });
|
|
return g;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
check(!new ClimateGateway(null).available(), "Null hub is unavailable");
|
|
check(new ClimateGateway(null).read().driverSeat == -1, "Unavailable reads are -1");
|
|
|
|
check(ClimateGateway.pressesBetween(0, 3) == 1, "OFF -> 3 is one press");
|
|
check(ClimateGateway.pressesBetween(0, 1) == 3, "OFF -> 1 is three presses");
|
|
check(ClimateGateway.pressesBetween(1, 0) == 1, "1 -> OFF is one press");
|
|
check(ClimateGateway.pressesBetween(2, 2) == 0, "Same level needs no press");
|
|
check(ClimateGateway.pressesBetween(-1, 2) == -1, "Unknown level is rejected");
|
|
|
|
for (int target = 0; target <= 3; target++) {
|
|
for (int start = 0; start <= 3; start++) {
|
|
FakeCar car = new FakeCar();
|
|
car.driverSeat = start;
|
|
ClimateGateway g = gateway(car);
|
|
int result = g.setSeatHeatLevel(ClimateGateway.Seat.DRIVER, target);
|
|
check(result == target, "Driver seat " + start + " -> " + target + " reached " + result);
|
|
check(car.writes.size() == ClimateGateway.pressesBetween(start, target),
|
|
"Minimal presses for " + start + " -> " + target);
|
|
check(car.passengerSeat == 0, "Driver change must not touch passenger seat");
|
|
}
|
|
}
|
|
|
|
FakeCar stuck = new FakeCar();
|
|
stuck.stuck = true;
|
|
gateway(stuck).setSeatHeatLevel(ClimateGateway.Seat.PASSENGER, 2);
|
|
check(stuck.writes.size() == 1, "A seat that does not respond is pressed only once, got " + stuck.writes.size());
|
|
|
|
FakeCar car = new FakeCar();
|
|
ClimateGateway g = gateway(car);
|
|
check(g.setSteeringWheelHeat(true) == 1 && car.writes.size() == 1, "Wheel heat on");
|
|
check(g.setSteeringWheelHeat(true) == 1 && car.writes.size() == 1, "Wheel heat already on: no press");
|
|
check(g.setSteeringWheelHeat(false) == 0 && car.writes.size() == 2, "Wheel heat off");
|
|
|
|
check(!g.setTemperature(ClimateGateway.Seat.DRIVER, 16) && !g.setTemperature(ClimateGateway.Seat.DRIVER, 34),
|
|
"Out-of-range temperatures are never sent");
|
|
check(g.setTemperature(ClimateGateway.Seat.DRIVER, 24) && car.temp == 24, "Absolute temperature");
|
|
check(!g.setFanLevel(0) && !g.setFanLevel(12) && g.setFanLevel(7) && car.fan == 7, "Fan bounds");
|
|
check(g.pressAc() && car.ac == 1, "A/C press");
|
|
|
|
ClimateGateway.State s = g.read();
|
|
check(s.driverTemp == 24 && s.fan == 7 && s.ac == 1 && s.power == 1, "State read-back");
|
|
check(Float.isNaN(s.outside), "OEM -10000 outside temperature is reported as unknown");
|
|
check("LO".equals(ClimateGateway.temperatureLabel(17)) && "HI".equals(ClimateGateway.temperatureLabel(33)),
|
|
"LO/HI labels");
|
|
System.out.println("ClimateGatewayTest passed");
|
|
}
|
|
|
|
private static void check(boolean condition, String message) {
|
|
if (!condition) throw new AssertionError(message);
|
|
}
|
|
}
|