summaryrefslogtreecommitdiff
path: root/sdl_android/src/androidTest/java/com/smartdevicelink/test/rpc/requests/RegisterAppInterfaceTests.java
blob: 5c65947a637c0c144656790d14f1e3ff01f770e4 (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
package com.smartdevicelink.test.rpc.requests;

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

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

import com.smartdevicelink.marshal.JsonRPCMarshaller;
import com.smartdevicelink.protocol.enums.FunctionID;
import com.smartdevicelink.proxy.RPCMessage;
import com.smartdevicelink.proxy.rpc.DeviceInfo;
import com.smartdevicelink.proxy.rpc.RegisterAppInterface;
import com.smartdevicelink.proxy.rpc.SdlMsgVersion;
import com.smartdevicelink.proxy.rpc.TTSChunk;
import com.smartdevicelink.proxy.rpc.TemplateColorScheme;
import com.smartdevicelink.proxy.rpc.enums.AppHMIType;
import com.smartdevicelink.proxy.rpc.enums.Language;
import com.smartdevicelink.test.BaseRpcTests;
import com.smartdevicelink.test.JsonUtils;
import com.smartdevicelink.test.Test;
import com.smartdevicelink.test.Validator;
import com.smartdevicelink.test.json.rpc.JsonFileReader;

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

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

		msg.setSdlMsgVersion(Test.GENERAL_SDLMSGVERSION);
		msg.setAppName(Test.GENERAL_STRING);
		msg.setNgnMediaScreenAppName(Test.GENERAL_STRING);
		msg.setAppID(Test.GENERAL_STRING);
		msg.setLanguageDesired(Test.GENERAL_LANGUAGE);
		msg.setHmiDisplayLanguageDesired(Test.GENERAL_LANGUAGE);
		msg.setHashID(Test.GENERAL_STRING);
		msg.setTtsName(Test.GENERAL_TTSCHUNK_LIST);
		msg.setVrSynonyms(Test.GENERAL_STRING_LIST);
		msg.setAppHMIType(Test.GENERAL_APPHMITYPE_LIST);
		msg.setIsMediaApplication(Test.GENERAL_BOOLEAN);
		msg.setDeviceInfo(Test.GENERAL_DEVICEINFO);
		msg.setDayColorScheme(Test.GENERAL_DAYCOLORSCHEME);
		msg.setNightColorScheme(Test.GENERAL_NIGHTCOLORSCHEME);

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

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

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

		try {
			result.put(RegisterAppInterface.KEY_SDL_MSG_VERSION, Test.JSON_SDLMSGVERSION);
			result.put(RegisterAppInterface.KEY_APP_NAME, Test.GENERAL_STRING);
			result.put(RegisterAppInterface.KEY_NGN_MEDIA_SCREEN_APP_NAME, Test.GENERAL_STRING);
			result.put(RegisterAppInterface.KEY_APP_ID, Test.GENERAL_STRING);
			result.put(RegisterAppInterface.KEY_LANGUAGE_DESIRED, Test.GENERAL_LANGUAGE);
			result.put(RegisterAppInterface.KEY_HMI_DISPLAY_LANGUAGE_DESIRED, Test.GENERAL_LANGUAGE);
			result.put(RegisterAppInterface.KEY_HASH_ID, Test.GENERAL_STRING);
			result.put(RegisterAppInterface.KEY_TTS_NAME, Test.JSON_TTSCHUNKS);
			result.put(RegisterAppInterface.KEY_VR_SYNONYMS, JsonUtils.createJsonArray(Test.GENERAL_STRING_LIST));
			result.put(RegisterAppInterface.KEY_APP_HMI_TYPE, JsonUtils.createJsonArrayOfJsonNames(Test.GENERAL_APPHMITYPE_LIST, SDL_VERSION_UNDER_TEST));
			result.put(RegisterAppInterface.KEY_IS_MEDIA_APPLICATION, Test.GENERAL_BOOLEAN);
			result.put(RegisterAppInterface.KEY_DEVICE_INFO, Test.JSON_DEVICEINFO);
			result.put(RegisterAppInterface.KEY_DAY_COLOR_SCHEME, Test.JSON_DAYCOLORSCHEME);
			result.put(RegisterAppInterface.KEY_NIGHT_COLOR_SCHEME, Test.JSON_NIGHTCOLORSCHEME);
		} catch (JSONException e) {
			fail(Test.JSON_FAIL);
		}

		return result;
	}

	/**
	 * Tests the expected values of the RPC message.
	 */
    public void testRpcValues () {
    	// Test Values
		SdlMsgVersion testVersion = ( (RegisterAppInterface) msg).getSdlMsgVersion();
		String testName = ( (RegisterAppInterface) msg).getAppName();
		String testNgnName = ( (RegisterAppInterface) msg).getNgnMediaScreenAppName();
		String testAppId = ( (RegisterAppInterface) msg).getAppID();
		Language testLang = ( (RegisterAppInterface) msg).getLanguageDesired();
		Language testHmiLang = ( (RegisterAppInterface) msg).getHmiDisplayLanguageDesired();
		String testHashId = ( (RegisterAppInterface) msg).getHashID();
		List<TTSChunk> testTts = ( (RegisterAppInterface) msg).getTtsName();
		List<String> testSynonyms = ( (RegisterAppInterface) msg).getVrSynonyms();
		List<AppHMIType> testApps = ( (RegisterAppInterface) msg).getAppHMIType();
		Boolean testMedia = ( (RegisterAppInterface) msg).getIsMediaApplication();
		DeviceInfo testDeviceInfo = ( (RegisterAppInterface) msg).getDeviceInfo();
		TemplateColorScheme testDayColorScheme = ( (RegisterAppInterface) msg).getDayColorScheme();
		TemplateColorScheme testNightColorScheme = ( (RegisterAppInterface) msg).getNightColorScheme();

		// Valid Tests
		assertTrue(Test.TRUE, Validator.validateSdlMsgVersion(Test.GENERAL_SDLMSGVERSION, testVersion));
		assertEquals(Test.MATCH, Test.GENERAL_STRING, testName);
		assertEquals(Test.MATCH, Test.GENERAL_STRING, testNgnName);
		assertEquals(Test.MATCH, Test.GENERAL_STRING, testAppId);
		assertEquals(Test.MATCH, Test.GENERAL_LANGUAGE, testLang);
		assertEquals(Test.MATCH, Test.GENERAL_LANGUAGE, testHmiLang);
		assertEquals(Test.MATCH, Test.GENERAL_STRING, testHashId);
		assertTrue(Test.TRUE, Validator.validateTtsChunks(Test.GENERAL_TTSCHUNK_LIST, testTts));
		assertEquals(Test.MATCH, Test.GENERAL_STRING_LIST, testSynonyms);
		assertEquals(Test.MATCH, Test.GENERAL_APPHMITYPE_LIST, testApps);
		assertEquals(Test.MATCH, (Boolean) Test.GENERAL_BOOLEAN, testMedia);
		assertTrue(Test.TRUE, Validator.validateDeviceInfo(Test.GENERAL_DEVICEINFO, testDeviceInfo));
		assertTrue(Test.TRUE, Validator.validateTemplateColorScheme(Test.GENERAL_DAYCOLORSCHEME, testDayColorScheme));
		assertTrue(Test.TRUE, Validator.validateTemplateColorScheme(Test.GENERAL_NIGHTCOLORSCHEME, testNightColorScheme));

		// Invalid/Null Tests
		RegisterAppInterface msg = new RegisterAppInterface();
		assertNotNull(Test.NOT_NULL, msg);
		testNullBase(msg);

		assertNull(Test.NULL, msg.getSdlMsgVersion());
		assertNull(Test.NULL, msg.getAppName());
		assertNull(Test.NULL, msg.getNgnMediaScreenAppName());
		assertNull(Test.NULL, msg.getAppID());
		assertNull(Test.NULL, msg.getLanguageDesired());
		assertNull(Test.NULL, msg.getHmiDisplayLanguageDesired());
		assertNull(Test.NULL, msg.getHashID());
		assertNull(Test.NULL, msg.getTtsName());
		assertNull(Test.NULL, msg.getVrSynonyms());
		assertNull(Test.NULL, msg.getAppHMIType());
		assertNull(Test.NULL, msg.getIsMediaApplication());
		assertNull(Test.NULL, msg.getDeviceInfo());
		assertNull(Test.NULL, msg.getDayColorScheme());
		assertNull(Test.NULL, msg.getNightColorScheme());
	}

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

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

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

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

			JSONObject parameters = JsonUtils.readJsonObjectFromJsonObject(body, RPCMessage.KEY_PARAMETERS);
			JSONArray ttsNameArray = JsonUtils.readJsonArrayFromJsonObject(parameters, RegisterAppInterface.KEY_TTS_NAME);
			List<TTSChunk> ttsNameList = new ArrayList<TTSChunk>();
			for (int index = 0; index < ttsNameArray.length(); index++) {
	        	TTSChunk chunk = new TTSChunk(JsonRPCMarshaller.deserializeJSONObject( (JSONObject)ttsNameArray.get(index)) );
	        	ttsNameList.add(chunk);
			}
			assertTrue(Test.TRUE,  Validator.validateTtsChunks(ttsNameList, cmd.getTtsName()));
			assertEquals(Test.MATCH, JsonUtils.readStringFromJsonObject(parameters, RegisterAppInterface.KEY_HMI_DISPLAY_LANGUAGE_DESIRED), cmd.getHmiDisplayLanguageDesired().toString());

			JSONArray appHmiTypeArray = JsonUtils.readJsonArrayFromJsonObject(parameters, RegisterAppInterface.KEY_APP_HMI_TYPE);
			for (int index = 0; index < appHmiTypeArray.length(); index++) {
				AppHMIType appHmiTypeItem = AppHMIType.valueForString( appHmiTypeArray.get(index).toString() );
				assertEquals(Test.MATCH, appHmiTypeItem, cmd.getAppHMIType().get(index) );
			}
			assertEquals(Test.MATCH, JsonUtils.readStringFromJsonObject(parameters, RegisterAppInterface.KEY_APP_ID), cmd.getAppID());
			assertEquals(Test.MATCH, JsonUtils.readStringFromJsonObject(parameters, RegisterAppInterface.KEY_LANGUAGE_DESIRED), cmd.getLanguageDesired().toString());

			JSONObject deviceInfoObj = JsonUtils.readJsonObjectFromJsonObject(parameters, RegisterAppInterface.KEY_DEVICE_INFO);
			DeviceInfo deviceInfo = new DeviceInfo(JsonRPCMarshaller.deserializeJSONObject(deviceInfoObj));
			assertTrue(Test.TRUE,  Validator.validateDeviceInfo(deviceInfo, cmd.getDeviceInfo()) );
			assertEquals(Test.MATCH, JsonUtils.readStringFromJsonObject(parameters, RegisterAppInterface.KEY_APP_NAME), cmd.getAppName());
			assertEquals(Test.MATCH, JsonUtils.readStringFromJsonObject(parameters, RegisterAppInterface.KEY_NGN_MEDIA_SCREEN_APP_NAME), cmd.getNgnMediaScreenAppName());
			assertEquals(Test.MATCH, JsonUtils.readBooleanFromJsonObject(parameters, RegisterAppInterface.KEY_IS_MEDIA_APPLICATION), cmd.getIsMediaApplication());

			List<String> vrSynonymsList = JsonUtils.readStringListFromJsonObject(parameters, RegisterAppInterface.KEY_VR_SYNONYMS);
			List<String> testSynonymsList = cmd.getVrSynonyms();
			assertEquals(Test.MATCH, vrSynonymsList.size(), testSynonymsList.size());
			assertTrue(Test.TRUE, Validator.validateStringList(vrSynonymsList, testSynonymsList));

			JSONObject sdlMsgVersionObj = JsonUtils.readJsonObjectFromJsonObject(parameters, RegisterAppInterface.KEY_SDL_MSG_VERSION);
			SdlMsgVersion sdlMsgVersion = new SdlMsgVersion(JsonRPCMarshaller.deserializeJSONObject(sdlMsgVersionObj));
			assertTrue(Test.TRUE,  Validator.validateSdlMsgVersion(sdlMsgVersion, cmd.getSdlMsgVersion()) );
			assertEquals(Test.MATCH, JsonUtils.readStringFromJsonObject(parameters, RegisterAppInterface.KEY_HASH_ID), cmd.getHashID());

			JSONObject dayColorSchemeObj = JsonUtils.readJsonObjectFromJsonObject(parameters, RegisterAppInterface.KEY_DAY_COLOR_SCHEME);
			TemplateColorScheme dayColorScheme = new TemplateColorScheme(JsonRPCMarshaller.deserializeJSONObject(dayColorSchemeObj));
			assertTrue(Test.TRUE,  Validator.validateTemplateColorScheme(dayColorScheme, cmd.getDayColorScheme()) );

			JSONObject nightColorSchemeObj = JsonUtils.readJsonObjectFromJsonObject(parameters, RegisterAppInterface.KEY_DAY_COLOR_SCHEME);
			TemplateColorScheme nightColorScheme = new TemplateColorScheme(JsonRPCMarshaller.deserializeJSONObject(nightColorSchemeObj));
			assertTrue(Test.TRUE,  Validator.validateTemplateColorScheme(nightColorScheme, cmd.getDayColorScheme()) );

		} catch (JSONException e) {
			fail(Test.JSON_FAIL);
		}
    }
}