aboutsummaryrefslogtreecommitdiff
path: root/tests/obd2_test/src/com/android/car/obd2/test/Obd2ConnectionTest.java
blob: a5f06e1591bff39d8a079af6429326d6689faa73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.car.obd2.test;

import static org.junit.Assert.*;

import com.android.car.obd2.Obd2Command;
import com.android.car.obd2.Obd2Connection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Optional;
import org.junit.Test;

public class Obd2ConnectionTest {
    private static int[] stringsToIntArray(String... strings) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (String string : strings) {
            string.chars().forEach(arrayList::add);
        }
        return arrayList.stream().mapToInt(Integer::intValue).toArray();
    }

    private static int[] concatIntArrays(int[] array1, int[] array2) {
        int[] newArray = Arrays.copyOf(array1, array1.length + array2.length);
        System.arraycopy(array2, 0, newArray, array1.length, array2.length);
        return newArray;
    }

    private static final String[] EXPECTED_INIT_COMMANDS =
            new String[] {
                "ATD\r", "ATZ\r", "AT E0\r", "AT L0\r", "AT S0\r", "AT H0\r", "AT SP 0\r"
            };

    private static final int AIR_TEMPERATURE_PID = 0x46;
    private static final int ENGINE_OIL_TEMPERATURE_PID = 0x5C;

    private static final String[] EXPECTED_AIR_TEMPERATURE_COMMANDS = new String[] {"0146\r"};
    private static final String[] EXPECTED_ENGINE_OIL_TEMPERATURE_COMMANDS =
            new String[] {"015C\r"};

    private static final String OBD2_PROMPT = ">";

    private static final String[] EXPECTED_INIT_RESPONSES =
            new String[] {
                OBD2_PROMPT,
                OBD2_PROMPT,
                OBD2_PROMPT,
                OBD2_PROMPT,
                OBD2_PROMPT,
                OBD2_PROMPT,
                OBD2_PROMPT
            };

    private static final String[] EXPECTED_AIR_TEMPERATURE_RESPONSES =
            new String[] {"41 46 A1", OBD2_PROMPT};
    private static final String[] EXPECTED_ENGINE_OIL_TEMPERATURE_RESPONSES =
            new String[] {"41 5C 87", OBD2_PROMPT};

    @Test
    public void testEstablishConnection() {
        MockObd2UnderlyingTransport transport =
                new MockObd2UnderlyingTransport(
                        stringsToIntArray(EXPECTED_INIT_COMMANDS),
                        stringsToIntArray(EXPECTED_INIT_RESPONSES));
        Obd2Connection obd2Connection = new Obd2Connection(transport);
    }

    @Test
    public void testAmbientAirTemperature() {
        MockObd2UnderlyingTransport transport =
                new MockObd2UnderlyingTransport(
                        concatIntArrays(
                                stringsToIntArray(EXPECTED_INIT_COMMANDS),
                                stringsToIntArray(EXPECTED_AIR_TEMPERATURE_COMMANDS)),
                        concatIntArrays(
                                stringsToIntArray(EXPECTED_INIT_RESPONSES),
                                stringsToIntArray(EXPECTED_AIR_TEMPERATURE_RESPONSES)));
        Obd2Connection obd2Connection = new Obd2Connection(transport);
        Obd2Command<Float> airTemperatureCommand =
                Obd2Command.getLiveFrameCommand(Obd2Command.getFloatCommand(AIR_TEMPERATURE_PID));
        Optional<Float> airTemperature = Optional.empty();
        try {
            airTemperature = airTemperatureCommand.run(obd2Connection);
        } catch (Exception e) {
            assertTrue("airTemperature caused an exception: " + e, false);
        }
        assertTrue(airTemperature.isPresent());
        assertEquals(63.137257, (double) airTemperature.get(), 0.001);
    }

    @Test
    public void testEngineOilTemperature() {
        MockObd2UnderlyingTransport transport =
                new MockObd2UnderlyingTransport(
                        concatIntArrays(
                                stringsToIntArray(EXPECTED_INIT_COMMANDS),
                                stringsToIntArray(EXPECTED_ENGINE_OIL_TEMPERATURE_COMMANDS)),
                        concatIntArrays(
                                stringsToIntArray(EXPECTED_INIT_RESPONSES),
                                stringsToIntArray(EXPECTED_ENGINE_OIL_TEMPERATURE_RESPONSES)));
        Obd2Connection obd2Connection = new Obd2Connection(transport);
        Obd2Command<Integer> engineOilTemperatureCommand =
                Obd2Command.getLiveFrameCommand(
                        Obd2Command.getIntegerCommand(ENGINE_OIL_TEMPERATURE_PID));
        Optional<Integer> engineOilTemperature = Optional.empty();
        try {
            engineOilTemperature = engineOilTemperatureCommand.run(obd2Connection);
        } catch (Exception e) {
            assertTrue("engineOilTemperature caused an exception: " + e, false);
        }
        assertTrue(engineOilTemperature.isPresent());
        assertEquals(95, (int) engineOilTemperature.get());
    }
}