summaryrefslogtreecommitdiff
path: root/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/rpc/responses/RegisterAppInterfaceResponseTest.java
blob: d95eb059475362767a3dbd8058f5ba3a36efd8ad (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package com.smartdevicelink.test.rpc.responses;

import com.smartdevicelink.marshal.JsonRPCMarshaller;
import com.smartdevicelink.protocol.enums.FunctionID;
import com.smartdevicelink.proxy.RPCMessage;
import com.smartdevicelink.proxy.rpc.AudioPassThruCapabilities;
import com.smartdevicelink.proxy.rpc.ButtonCapabilities;
import com.smartdevicelink.proxy.rpc.DisplayCapabilities;
import com.smartdevicelink.proxy.rpc.PresetBankCapabilities;
import com.smartdevicelink.proxy.rpc.RegisterAppInterfaceResponse;
import com.smartdevicelink.proxy.rpc.SdlMsgVersion;
import com.smartdevicelink.proxy.rpc.SoftButtonCapabilities;
import com.smartdevicelink.proxy.rpc.VehicleType;
import com.smartdevicelink.proxy.rpc.enums.HmiZoneCapabilities;
import com.smartdevicelink.proxy.rpc.enums.Language;
import com.smartdevicelink.proxy.rpc.enums.PrerecordedSpeech;
import com.smartdevicelink.proxy.rpc.enums.SpeechCapabilities;
import com.smartdevicelink.proxy.rpc.enums.VrCapabilities;
import com.smartdevicelink.test.BaseRpcTests;
import com.smartdevicelink.test.JsonUtils;
import com.smartdevicelink.test.TestValues;
import com.smartdevicelink.test.Validator;
import com.smartdevicelink.test.json.rpc.JsonFileReader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.assertTrue;
import static junit.framework.TestCase.fail;

/**
 * This is a unit test class for the SmartDeviceLink library project class :
 * {@link com.smartdevicelink.proxy.rpc.RegisterAppInterfaceResponse}
 */
public class RegisterAppInterfaceResponseTest extends BaseRpcTests {

    @Override
    protected RPCMessage createMessage() {
        RegisterAppInterfaceResponse msg = new RegisterAppInterfaceResponse();

        msg.setSdlMsgVersion(TestValues.GENERAL_SDLMSGVERSION);
        msg.setLanguage(TestValues.GENERAL_LANGUAGE);
        msg.setHmiDisplayLanguage(TestValues.GENERAL_LANGUAGE);
        msg.setDisplayCapabilities(TestValues.GENERAL_DISPLAYCAPABILITIES);
        msg.setPresetBankCapabilities(TestValues.GENERAL_PRESETBANKCAPABILITIES);
        msg.setVehicleType(TestValues.GENERAL_VEHICLETYPE);
        msg.setButtonCapabilities(TestValues.GENERAL_BUTTONCAPABILITIES_LIST);
        msg.setSoftButtonCapabilities(TestValues.GENERAL_SOFTBUTTONCAPABILITIES_LIST);
        msg.setAudioPassThruCapabilities(TestValues.GENERAL_AUDIOPASSTHRUCAPABILITIES_LIST);
        msg.setPcmStreamingCapabilities(TestValues.GENERAL_AUDIOPASSTHRUCAPABILITIES);
        msg.setHmiZoneCapabilities(TestValues.GENERAL_HMIZONECAPABILITIES_LIST);
        msg.setSpeechCapabilities(TestValues.GENERAL_SPEECHCAPABILITIES_LIST);
        msg.setVrCapabilities(TestValues.GENERAL_VRCAPABILITIES_LIST);
        msg.setPrerecordedSpeech(TestValues.GENERAL_PRERECORDEDSPEECH_LIST);
        msg.setSupportedDiagModes(TestValues.GENERAL_INTEGER_LIST);
        msg.setIconResumed(TestValues.GENERAL_BOOLEAN);

        return msg;
    }

    @Override
    protected String getMessageType() {
        return RPCMessage.KEY_RESPONSE;
    }

    @Override
    protected String getCommandType() {
        return FunctionID.REGISTER_APP_INTERFACE.toString();
    }


    @Override
    protected JSONObject getExpectedParameters(int sdlVersion) {
        JSONObject result = new JSONObject();

        try {
            result.put(RegisterAppInterfaceResponse.KEY_LANGUAGE, TestValues.GENERAL_LANGUAGE);
            result.put(RegisterAppInterfaceResponse.KEY_HMI_DISPLAY_LANGUAGE, TestValues.GENERAL_LANGUAGE);
            result.put(RegisterAppInterfaceResponse.KEY_SUPPORTED_DIAG_MODES, JsonUtils.createJsonArray(TestValues.GENERAL_INTEGER_LIST));
            result.put(RegisterAppInterfaceResponse.KEY_SDL_MSG_VERSION, TestValues.JSON_SDLMSGVERSION);
            result.put(RegisterAppInterfaceResponse.KEY_VEHICLE_TYPE, TestValues.GENERAL_VEHICLETYPE.serializeJSON());
            result.put(RegisterAppInterfaceResponse.KEY_PRESET_BANK_CAPABILITIES, TestValues.JSON_PRESETBANKCAPABILITIES);
            result.put(RegisterAppInterfaceResponse.KEY_DISPLAY_CAPABILITIES, TestValues.JSON_DISPLAYCAPABILITIES);
            result.put(RegisterAppInterfaceResponse.KEY_BUTTON_CAPABILITIES, TestValues.JSON_BUTTONCAPABILITIES);
            result.put(RegisterAppInterfaceResponse.KEY_SOFT_BUTTON_CAPABILITIES, TestValues.JSON_SOFTBUTTONCAPABILITIES);
            result.put(RegisterAppInterfaceResponse.KEY_AUDIO_PASS_THRU_CAPABILITIES, TestValues.JSON_AUDIOPASSTHRUCAPABILITIES);
            result.put(RegisterAppInterfaceResponse.KEY_PCM_STREAM_CAPABILITIES, TestValues.JSON_PCMSTREAMCAPABILITIES);
            result.put(RegisterAppInterfaceResponse.KEY_SPEECH_CAPABILITIES, JsonUtils.createJsonArray(TestValues.GENERAL_SPEECHCAPABILITIES_LIST));
            result.put(RegisterAppInterfaceResponse.KEY_VR_CAPABILITIES, JsonUtils.createJsonArray(TestValues.GENERAL_VRCAPABILITIES_LIST));
            result.put(RegisterAppInterfaceResponse.KEY_HMI_ZONE_CAPABILITIES, JsonUtils.createJsonArray(TestValues.GENERAL_HMIZONECAPABILITIES_LIST));
            result.put(RegisterAppInterfaceResponse.KEY_PRERECORDED_SPEECH, JsonUtils.createJsonArray(TestValues.GENERAL_PRERECORDEDSPEECH_LIST));
            result.put(RegisterAppInterfaceResponse.KEY_ICON_RESUMED, TestValues.GENERAL_BOOLEAN);
        } catch (JSONException e) {
            fail(TestValues.JSON_FAIL);
        }

        return result;
    }

    /**
     * Tests the expected values of the RPC message.
     */
    @Test
    public void testRpcValues() {
        // Test Values
        List<Integer> testSupportedDiagModes = ((RegisterAppInterfaceResponse) msg).getSupportedDiagModes();
        List<PrerecordedSpeech> testPrerecordedSpeech = ((RegisterAppInterfaceResponse) msg).getPrerecordedSpeech();
        List<VrCapabilities> testVrCapabilities = ((RegisterAppInterfaceResponse) msg).getVrCapabilities();
        List<SpeechCapabilities> testSpeechCapabilities = ((RegisterAppInterfaceResponse) msg).getSpeechCapabilities();
        List<HmiZoneCapabilities> testHmiZoneCapabilities = ((RegisterAppInterfaceResponse) msg).getHmiZoneCapabilities();
        List<SoftButtonCapabilities> testSoftButtonCapabilities = ((RegisterAppInterfaceResponse) msg).getSoftButtonCapabilities();
        List<ButtonCapabilities> testButtonCapabilities = ((RegisterAppInterfaceResponse) msg).getButtonCapabilities();
        VehicleType testVehicleType = ((RegisterAppInterfaceResponse) msg).getVehicleType();
        PresetBankCapabilities testPbc = ((RegisterAppInterfaceResponse) msg).getPresetBankCapabilities();
        DisplayCapabilities testDc = ((RegisterAppInterfaceResponse) msg).getDisplayCapabilities();
        Language testHmiLang = ((RegisterAppInterfaceResponse) msg).getHmiDisplayLanguage();
        Language testLang = ((RegisterAppInterfaceResponse) msg).getLanguage();
        SdlMsgVersion testMsgVersion = ((RegisterAppInterfaceResponse) msg).getSdlMsgVersion();
        List<AudioPassThruCapabilities> testAptc = ((RegisterAppInterfaceResponse) msg).getAudioPassThruCapabilities();
        AudioPassThruCapabilities testPcmStream = ((RegisterAppInterfaceResponse) msg).getPcmStreamingCapabilities();
        Boolean testIconResumed = ((RegisterAppInterfaceResponse) msg).getIconResumed();

        // Valid Tests
        assertEquals(TestValues.MATCH, TestValues.GENERAL_INTEGER_LIST, testSupportedDiagModes);
        assertEquals(TestValues.MATCH, TestValues.GENERAL_PRERECORDEDSPEECH_LIST, testPrerecordedSpeech);
        assertEquals(TestValues.MATCH, TestValues.GENERAL_VRCAPABILITIES_LIST, testVrCapabilities);
        assertEquals(TestValues.MATCH, TestValues.GENERAL_SPEECHCAPABILITIES_LIST, testSpeechCapabilities);
        assertEquals(TestValues.MATCH, TestValues.GENERAL_HMIZONECAPABILITIES_LIST, testHmiZoneCapabilities);
        assertTrue(TestValues.TRUE, Validator.validateSoftButtonCapabilities(TestValues.GENERAL_SOFTBUTTONCAPABILITIES_LIST, testSoftButtonCapabilities));
        assertTrue(TestValues.TRUE, Validator.validateButtonCapabilities(TestValues.GENERAL_BUTTONCAPABILITIES_LIST, testButtonCapabilities));
        assertTrue(TestValues.TRUE, Validator.validateVehicleType(TestValues.GENERAL_VEHICLETYPE, testVehicleType));
        assertTrue(TestValues.TRUE, Validator.validatePresetBankCapabilities(TestValues.GENERAL_PRESETBANKCAPABILITIES, testPbc));
        assertTrue(TestValues.TRUE, Validator.validateDisplayCapabilities(TestValues.GENERAL_DISPLAYCAPABILITIES, testDc));
        assertEquals(TestValues.MATCH, TestValues.GENERAL_LANGUAGE, testHmiLang);
        assertEquals(TestValues.MATCH, TestValues.GENERAL_LANGUAGE, testLang);
        assertTrue(TestValues.TRUE, Validator.validateSdlMsgVersion(TestValues.GENERAL_SDLMSGVERSION, testMsgVersion));
        assertTrue(TestValues.TRUE, Validator.validateAudioPassThruCapabilities(TestValues.GENERAL_AUDIOPASSTHRUCAPABILITIES_LIST, testAptc));
        assertTrue(TestValues.TRUE, Validator.validatePcmStreamCapabilities(TestValues.GENERAL_AUDIOPASSTHRUCAPABILITIES, testPcmStream));
        assertEquals(TestValues.MATCH, (Boolean) TestValues.GENERAL_BOOLEAN, testIconResumed);

        // Invalid/Null Tests
        RegisterAppInterfaceResponse msg = new RegisterAppInterfaceResponse();
        assertNotNull(TestValues.NOT_NULL, msg);
        testNullBase(msg);

        assertNull(TestValues.NULL, msg.getSdlMsgVersion());
        assertNull(TestValues.NULL, msg.getLanguage());
        assertNull(TestValues.NULL, msg.getHmiDisplayLanguage());
        assertNull(TestValues.NULL, msg.getDisplayCapabilities());
        assertNull(TestValues.NULL, msg.getPresetBankCapabilities());
        assertNull(TestValues.NULL, msg.getVehicleType());
        assertNull(TestValues.NULL, msg.getButtonCapabilities());
        assertNull(TestValues.NULL, msg.getSoftButtonCapabilities());
        assertNull(TestValues.NULL, msg.getAudioPassThruCapabilities());
        assertNull(TestValues.NULL, msg.getPcmStreamingCapabilities());
        assertNull(TestValues.NULL, msg.getHmiZoneCapabilities());
        assertNull(TestValues.NULL, msg.getSpeechCapabilities());
        assertNull(TestValues.NULL, msg.getVrCapabilities());
        assertNull(TestValues.NULL, msg.getPrerecordedSpeech());
        assertNull(TestValues.NULL, msg.getSupportedDiagModes());
        assertNull(TestValues.NULL, msg.getIconResumed());
    }

    /**
     * Tests a valid JSON construction of this RPC message.
     */
    @Test
    public void testJsonConstructor() {
        JSONObject commandJson = JsonFileReader.readId(getInstrumentation().getContext(), getCommandType(), getMessageType());
        assertNotNull(TestValues.NOT_NULL, commandJson);

        try {
            Hashtable<String, Object> hash = JsonRPCMarshaller.deserializeJSONObject(commandJson);
            RegisterAppInterfaceResponse cmd = new RegisterAppInterfaceResponse(hash);

            JSONObject body = JsonUtils.readJsonObjectFromJsonObject(commandJson, getMessageType());
            assertNotNull(TestValues.NOT_NULL, body);

            // Test everything in the json body.
            assertEquals(TestValues.MATCH, JsonUtils.readStringFromJsonObject(body, RPCMessage.KEY_FUNCTION_NAME), cmd.getFunctionName());
            assertEquals(TestValues.MATCH, JsonUtils.readIntegerFromJsonObject(body, RPCMessage.KEY_CORRELATION_ID), cmd.getCorrelationID());

            JSONObject parameters = JsonUtils.readJsonObjectFromJsonObject(body, RPCMessage.KEY_PARAMETERS);

            JSONObject vehicleTypeObj = JsonUtils.readJsonObjectFromJsonObject(parameters, RegisterAppInterfaceResponse.KEY_VEHICLE_TYPE);
            VehicleType vehicleType = new VehicleType(JsonRPCMarshaller.deserializeJSONObject(vehicleTypeObj));
            assertTrue(TestValues.TRUE, Validator.validateVehicleType(vehicleType, cmd.getVehicleType()));

            JSONObject pcmStreamObj = JsonUtils.readJsonObjectFromJsonObject(parameters, RegisterAppInterfaceResponse.KEY_PCM_STREAM_CAPABILITIES);
            AudioPassThruCapabilities pcmStreamCap = new AudioPassThruCapabilities(JsonRPCMarshaller.deserializeJSONObject(pcmStreamObj));
            assertTrue(TestValues.TRUE, Validator.validatePcmStreamCapabilities(pcmStreamCap, cmd.getPcmStreamingCapabilities()));

            JSONArray speechCapabilitiesArray = JsonUtils.readJsonArrayFromJsonObject(parameters, RegisterAppInterfaceResponse.KEY_SPEECH_CAPABILITIES);
            for (int index = 0; index < speechCapabilitiesArray.length(); index++) {
                SpeechCapabilities speechCapability = SpeechCapabilities.valueForString(speechCapabilitiesArray.get(index).toString());
                assertEquals(TestValues.MATCH, speechCapability, cmd.getSpeechCapabilities().get(index));
            }

            JSONArray vrCapabilitiesArray = JsonUtils.readJsonArrayFromJsonObject(parameters, RegisterAppInterfaceResponse.KEY_VR_CAPABILITIES);
            for (int index = 0; index < vrCapabilitiesArray.length(); index++) {
                VrCapabilities vrCapability = VrCapabilities.valueForString(vrCapabilitiesArray.get(index).toString());
                assertEquals(TestValues.MATCH, vrCapability, cmd.getVrCapabilities().get(index));
            }

            JSONArray audioPassThruCapabilitiesArray = JsonUtils.readJsonArrayFromJsonObject(parameters, RegisterAppInterfaceResponse.KEY_AUDIO_PASS_THRU_CAPABILITIES);
            List<AudioPassThruCapabilities> audioPassThruCapabilitiesList = new ArrayList<AudioPassThruCapabilities>();
            for (int index = 0; index < audioPassThruCapabilitiesArray.length(); index++) {
                AudioPassThruCapabilities audioPassThruCapability =
                        new AudioPassThruCapabilities(JsonRPCMarshaller.deserializeJSONObject((JSONObject) audioPassThruCapabilitiesArray.get(index)));
                audioPassThruCapabilitiesList.add(audioPassThruCapability);
            }
            assertTrue(TestValues.TRUE, Validator.validateAudioPassThruCapabilities(audioPassThruCapabilitiesList, cmd.getAudioPassThruCapabilities()));

            JSONArray hmiZoneCapabilitiesArray = JsonUtils.readJsonArrayFromJsonObject(parameters, RegisterAppInterfaceResponse.KEY_HMI_ZONE_CAPABILITIES);
            for (int index = 0; index < hmiZoneCapabilitiesArray.length(); index++) {
                HmiZoneCapabilities hmiZoneCapability = HmiZoneCapabilities.valueForString(hmiZoneCapabilitiesArray.get(index).toString());
                assertEquals(TestValues.MATCH, hmiZoneCapability, cmd.getHmiZoneCapabilities().get(index));
            }

            JSONArray prerecordedSpeechArray = JsonUtils.readJsonArrayFromJsonObject(parameters, RegisterAppInterfaceResponse.KEY_PRERECORDED_SPEECH);
            for (int index = 0; index < prerecordedSpeechArray.length(); index++) {
                PrerecordedSpeech prerecordedSpeech = PrerecordedSpeech.valueForString(prerecordedSpeechArray.get(index).toString());
                assertEquals(TestValues.MATCH, prerecordedSpeech, cmd.getPrerecordedSpeech().get(index));
            }

            List<Integer> supportedDiagnosticModesList = JsonUtils.readIntegerListFromJsonObject(parameters, RegisterAppInterfaceResponse.KEY_SUPPORTED_DIAG_MODES);
            List<Integer> testDiagnosticModesList = cmd.getSupportedDiagModes();
            assertEquals(TestValues.MATCH, supportedDiagnosticModesList.size(), testDiagnosticModesList.size());
            assertTrue(TestValues.TRUE, Validator.validateIntegerList(supportedDiagnosticModesList, testDiagnosticModesList));

            JSONObject sdlMsgVersionObj = JsonUtils.readJsonObjectFromJsonObject(parameters, RegisterAppInterfaceResponse.KEY_SDL_MSG_VERSION);
            SdlMsgVersion sdlMsgVersion = new SdlMsgVersion(JsonRPCMarshaller.deserializeJSONObject(sdlMsgVersionObj));
            assertTrue(TestValues.TRUE, Validator.validateSdlMsgVersion(sdlMsgVersion, cmd.getSdlMsgVersion()));
            assertEquals(TestValues.MATCH, JsonUtils.readStringFromJsonObject(parameters, RegisterAppInterfaceResponse.KEY_LANGUAGE), cmd.getLanguage().toString());

            JSONArray buttonCapabilitiesArray = JsonUtils.readJsonArrayFromJsonObject(parameters, RegisterAppInterfaceResponse.KEY_BUTTON_CAPABILITIES);
            List<ButtonCapabilities> buttonCapabilitiesList = new ArrayList<ButtonCapabilities>();
            for (int index = 0; index < buttonCapabilitiesArray.length(); index++) {
                ButtonCapabilities buttonCapability = new ButtonCapabilities(JsonRPCMarshaller.deserializeJSONObject((JSONObject) buttonCapabilitiesArray.get(index)));
                buttonCapabilitiesList.add(buttonCapability);
            }
            assertTrue(TestValues.TRUE, Validator.validateButtonCapabilities(buttonCapabilitiesList, cmd.getButtonCapabilities()));

            JSONObject displayCapabilitiesObj = JsonUtils.readJsonObjectFromJsonObject(parameters, RegisterAppInterfaceResponse.KEY_DISPLAY_CAPABILITIES);
            DisplayCapabilities displayCapabilities = new DisplayCapabilities(JsonRPCMarshaller.deserializeJSONObject(displayCapabilitiesObj));
            assertTrue(TestValues.TRUE, Validator.validateDisplayCapabilities(displayCapabilities, cmd.getDisplayCapabilities()));
            assertEquals(TestValues.MATCH, JsonUtils.readStringFromJsonObject(parameters, RegisterAppInterfaceResponse.KEY_HMI_DISPLAY_LANGUAGE), cmd.getHmiDisplayLanguage().toString());

            JSONArray softButtonCapabilitiesArray = JsonUtils.readJsonArrayFromJsonObject(parameters, RegisterAppInterfaceResponse.KEY_SOFT_BUTTON_CAPABILITIES);
            List<SoftButtonCapabilities> softButtonCapabilitiesList = new ArrayList<SoftButtonCapabilities>();
            for (int index = 0; index < softButtonCapabilitiesArray.length(); index++) {
                SoftButtonCapabilities softButtonCapability =
                        new SoftButtonCapabilities(JsonRPCMarshaller.deserializeJSONObject((JSONObject) softButtonCapabilitiesArray.get(index)));
                softButtonCapabilitiesList.add(softButtonCapability);
            }
            assertTrue(TestValues.TRUE, Validator.validateSoftButtonCapabilities(softButtonCapabilitiesList, cmd.getSoftButtonCapabilities()));

            JSONObject presetBankCapabilitiesObj = JsonUtils.readJsonObjectFromJsonObject(parameters, RegisterAppInterfaceResponse.KEY_PRESET_BANK_CAPABILITIES);
            PresetBankCapabilities presetBankCapabilities = new PresetBankCapabilities(JsonRPCMarshaller.deserializeJSONObject(presetBankCapabilitiesObj));
            assertTrue(TestValues.TRUE, Validator.validatePresetBankCapabilities(presetBankCapabilities, cmd.getPresetBankCapabilities()));

            Boolean iconResumed = JsonUtils.readBooleanFromJsonObject(parameters, RegisterAppInterfaceResponse.KEY_ICON_RESUMED);
            assertEquals(TestValues.MATCH, iconResumed, cmd.getIconResumed());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}