Home | History | Annotate | Download | only in test
      1 /*
      2  * Copyright (C) 2017 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.car.obd2.test;
     18 
     19 import static android.hardware.automotive.vehicle.V2_1.VehicleProperty.OBD2_LIVE_FRAME;
     20 import static com.android.car.obd2.test.Utils.concatIntArrays;
     21 import static com.android.car.obd2.test.Utils.stringsToIntArray;
     22 import static org.junit.Assert.*;
     23 
     24 import android.hardware.automotive.vehicle.V2_0.VehiclePropValue;
     25 import android.util.JsonReader;
     26 import android.util.JsonWriter;
     27 import com.android.car.obd2.Obd2Connection;
     28 import com.android.car.obd2.Obd2LiveFrameGenerator;
     29 import com.android.car.vehiclehal.DiagnosticJsonReader;
     30 import java.io.StringReader;
     31 import java.io.StringWriter;
     32 import org.junit.Test;
     33 
     34 public class Obd2LiveFrameGeneratorTest {
     35     private static final String[] EXPECTED_INIT_COMMANDS =
     36             new String[] {
     37                 "ATD\r", "ATZ\r", "AT E0\r", "AT L0\r", "AT S0\r", "AT H0\r", "AT SP 0\r"
     38             };
     39 
     40     private static final String OBD2_PROMPT = ">";
     41 
     42     private static final String[] EXPECTED_INIT_RESPONSES =
     43             new String[] {
     44                 OBD2_PROMPT,
     45                 OBD2_PROMPT,
     46                 OBD2_PROMPT,
     47                 OBD2_PROMPT,
     48                 OBD2_PROMPT,
     49                 OBD2_PROMPT,
     50                 OBD2_PROMPT
     51             };
     52 
     53     private static final String[] EXPECTED_DISCOVERY_COMMANDS =
     54             new String[] {"0100\r", "0120\r", "0140\r", "0160\r"};
     55 
     56     private static final String[] EXPECTED_DISCOVERY_RESPONSES =
     57             new String[] {"00 00 00 0C 00 00", OBD2_PROMPT, OBD2_PROMPT, OBD2_PROMPT, OBD2_PROMPT};
     58 
     59     private static final String[] EXPECTED_FRAME_COMMANDS = new String[] {"010C\r", "010D\r"};
     60 
     61     private static final String[] EXPECTED_FRAME_RESPONSES =
     62             new String[] {"41 0C 12 0F", OBD2_PROMPT, "41 0D 82", OBD2_PROMPT};
     63 
     64     @Test
     65     public void testObd2LiveFrameGeneration() throws Exception {
     66         MockObd2UnderlyingTransport transport =
     67                 new MockObd2UnderlyingTransport(
     68                         concatIntArrays(
     69                                 stringsToIntArray(EXPECTED_INIT_COMMANDS),
     70                                 stringsToIntArray(EXPECTED_DISCOVERY_COMMANDS),
     71                                 stringsToIntArray(EXPECTED_FRAME_COMMANDS)),
     72                         concatIntArrays(
     73                                 stringsToIntArray(EXPECTED_INIT_RESPONSES),
     74                                 stringsToIntArray(EXPECTED_DISCOVERY_RESPONSES),
     75                                 stringsToIntArray(EXPECTED_FRAME_RESPONSES)));
     76         Obd2Connection obd2Connection = new Obd2Connection(transport);
     77         Obd2LiveFrameGenerator obd2Generator = new Obd2LiveFrameGenerator(obd2Connection);
     78         StringWriter stringWriter = new StringWriter(1024);
     79         JsonWriter jsonWriter = new JsonWriter(stringWriter);
     80         obd2Generator.generate(jsonWriter);
     81         JsonReader jsonReader = new JsonReader(new StringReader(stringWriter.toString()));
     82         DiagnosticJsonReader diagnosticJsonReader = new DiagnosticJsonReader();
     83         VehiclePropValue vehiclePropValue = diagnosticJsonReader.build(jsonReader);
     84         assertEquals(OBD2_LIVE_FRAME, vehiclePropValue.prop);
     85         assertEquals(4611, (long) vehiclePropValue.value.int32Values.get(0xC));
     86         assertEquals(130, (long) vehiclePropValue.value.int32Values.get(0xD));
     87     }
     88 }
     89