summaryrefslogtreecommitdiff
path: root/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/rpc/requests/SetGlobalPropertiesTests.java
blob: 1ca333e73e2a01d24e03c3a4ba5dd99f7646fd13 (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
package com.smartdevicelink.test.rpc.requests;

import com.smartdevicelink.marshal.JsonRPCMarshaller;
import com.smartdevicelink.protocol.enums.FunctionID;
import com.smartdevicelink.proxy.RPCMessage;
import com.smartdevicelink.proxy.rpc.Image;
import com.smartdevicelink.proxy.rpc.KeyboardProperties;
import com.smartdevicelink.proxy.rpc.SetGlobalProperties;
import com.smartdevicelink.proxy.rpc.TTSChunk;
import com.smartdevicelink.proxy.rpc.VrHelpItem;
import com.smartdevicelink.proxy.rpc.enums.MenuLayout;
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;

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

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

/**
 * This is a unit test class for the SmartDeviceLink library project class : 
 * {@link com.smartdevicelink.proxy.rpc.SetGlobalProperties}
 */
public class SetGlobalPropertiesTests extends BaseRpcTests {
		
	@Override
	protected RPCMessage createMessage() {
		SetGlobalProperties msg = new SetGlobalProperties();
		
		msg.setVrHelpTitle(Test.GENERAL_STRING);
		msg.setMenuTitle(Test.GENERAL_STRING);
		msg.setMenuIcon(Test.GENERAL_IMAGE);
		msg.setVrHelp(Test.GENERAL_VRHELPITEM_LIST);
		msg.setHelpPrompt(Test.GENERAL_TTSCHUNK_LIST);
		msg.setTimeoutPrompt(Test.GENERAL_TTSCHUNK_LIST);
		msg.setKeyboardProperties(Test.GENERAL_KEYBOARDPROPERTIES);
		msg.setMenuLayout(Test.GENERAL_MENU_LAYOUT);

		return msg;
	}

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

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

	@Override
	protected JSONObject getExpectedParameters(int sdlVersion) {
		JSONObject result = new JSONObject();
		
		try {			
			result.put(SetGlobalProperties.KEY_MENU_ICON, Test.JSON_IMAGE);	
			result.put(SetGlobalProperties.KEY_VR_HELP, Test.JSON_VRHELPITEMS);
			result.put(SetGlobalProperties.KEY_HELP_PROMPT, Test.JSON_TTSCHUNKS);
			result.put(SetGlobalProperties.KEY_TIMEOUT_PROMPT, Test.JSON_TTSCHUNKS);
			result.put(SetGlobalProperties.KEY_MENU_TITLE, Test.GENERAL_STRING);
			result.put(SetGlobalProperties.KEY_VR_HELP_TITLE, Test.GENERAL_STRING);							
			result.put(SetGlobalProperties.KEY_KEYBOARD_PROPERTIES, Test.JSON_KEYBOARDPROPERTIES);
			result.put(SetGlobalProperties.KEY_MENU_LAYOUT, Test.GENERAL_MENU_LAYOUT);
		} catch (JSONException e) {
			fail(Test.JSON_FAIL);
		}

		return result;
	}

	/**
	 * Tests the expected values of the RPC message.
	 */
    public void testRpcValues () { 
    	// Test Values
    	Image              testImage       = ( (SetGlobalProperties) msg ).getMenuIcon();
    	String             testVrHelpTitle = ( (SetGlobalProperties) msg ).getVrHelpTitle();
    	String             testMenuTitle   = ( (SetGlobalProperties) msg ).getMenuTitle();
		List<TTSChunk>     testHelpPrompt  = ( (SetGlobalProperties) msg ).getHelpPrompt();
		List<TTSChunk>     testTimeout     = ( (SetGlobalProperties) msg ).getTimeoutPrompt();
		List<VrHelpItem>   testVrHelpItems = ( (SetGlobalProperties) msg ).getVrHelp();
		KeyboardProperties testKeyboardProperties = ( (SetGlobalProperties) msg ).getKeyboardProperties();
		MenuLayout testMenuLayout = ( (SetGlobalProperties) msg ).getMenuLayout();
		
		// Valid Tests		
		assertEquals(Test.MATCH, Test.GENERAL_STRING, testMenuTitle);
		assertEquals(Test.MATCH, Test.GENERAL_STRING, testVrHelpTitle);
		assertTrue(Test.TRUE, Validator.validateImage(Test.GENERAL_IMAGE, testImage));
		assertTrue(Test.TRUE, Validator.validateVrHelpItems(Test.GENERAL_VRHELPITEM_LIST, testVrHelpItems));
		assertTrue(Test.TRUE, Validator.validateTtsChunks(Test.GENERAL_TTSCHUNK_LIST, testHelpPrompt));
		assertTrue(Test.TRUE, Validator.validateTtsChunks(Test.GENERAL_TTSCHUNK_LIST, testTimeout));
		assertTrue(Test.TRUE, Validator.validateKeyboardProperties(Test.GENERAL_KEYBOARDPROPERTIES, testKeyboardProperties));
		assertEquals(Test.MATCH, Test.GENERAL_MENU_LAYOUT, testMenuLayout);
		
		// Invalid/Null Tests
		SetGlobalProperties msg = new SetGlobalProperties();
		assertNotNull(Test.NOT_NULL, msg);
		testNullBase(msg);

		assertNull(Test.NULL, msg.getMenuIcon());
		assertNull(Test.NULL, msg.getMenuTitle());
		assertNull(Test.NULL, msg.getVrHelp());
		assertNull(Test.NULL, msg.getHelpPrompt());
		assertNull(Test.NULL, msg.getTimeoutPrompt());
		assertNull(Test.NULL, msg.getKeyboardProperties());
		assertNull(Test.NULL, msg.getVrHelpTitle());
		assertNull(Test.NULL, msg.getMenuLayout());
	}
	
	/**
     * 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);
			SetGlobalProperties cmd = new SetGlobalProperties(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);
			assertEquals(Test.MATCH, JsonUtils.readStringFromJsonObject(parameters, SetGlobalProperties.KEY_VR_HELP_TITLE), cmd.getVrHelpTitle());
			assertEquals(Test.MATCH, JsonUtils.readStringFromJsonObject(parameters, SetGlobalProperties.KEY_MENU_TITLE), cmd.getMenuTitle());

			assertEquals(Test.MATCH, JsonUtils.readObjectFromJsonObject(parameters, SetGlobalProperties.KEY_MENU_LAYOUT), cmd.getMenuLayout());
			
			JSONObject menuIcon = JsonUtils.readJsonObjectFromJsonObject(parameters, SetGlobalProperties.KEY_MENU_ICON);
			Image referenceMenuIcon = new Image(JsonRPCMarshaller.deserializeJSONObject(menuIcon));
			assertTrue(Test.TRUE, Validator.validateImage(referenceMenuIcon, cmd.getMenuIcon()));
			
			JSONObject keyboardProperties = JsonUtils.readJsonObjectFromJsonObject(parameters, SetGlobalProperties.KEY_KEYBOARD_PROPERTIES);
			KeyboardProperties referenceKeyboardProperties = new KeyboardProperties(JsonRPCMarshaller.deserializeJSONObject(keyboardProperties));
			assertTrue(Test.TRUE, Validator.validateKeyboardProperties(referenceKeyboardProperties, cmd.getKeyboardProperties()));
			
			JSONArray helpPromptArray = JsonUtils.readJsonArrayFromJsonObject(parameters, SetGlobalProperties.KEY_HELP_PROMPT);
			List<TTSChunk> helpPromptList = new ArrayList<TTSChunk>();
			for (int index = 0; index < helpPromptArray.length(); index++) {
	        	TTSChunk chunk = new TTSChunk(JsonRPCMarshaller.deserializeJSONObject( (JSONObject)helpPromptArray.get(index)) );
	        	helpPromptList.add(chunk);
			}
			assertTrue(Test.TRUE, Validator.validateTtsChunks(helpPromptList, cmd.getHelpPrompt()));
			
			JSONArray timeoutPromptArray = JsonUtils.readJsonArrayFromJsonObject(parameters, SetGlobalProperties.KEY_TIMEOUT_PROMPT);
			List<TTSChunk> timeoutPromptList = new ArrayList<TTSChunk>();
			for (int index = 0; index < timeoutPromptArray.length(); index++) {
				TTSChunk chunk = new TTSChunk(JsonRPCMarshaller.deserializeJSONObject( (JSONObject)timeoutPromptArray.get(index)) );
				timeoutPromptList.add(chunk);
			}
			assertTrue(Test.TRUE, Validator.validateTtsChunks(timeoutPromptList, cmd.getTimeoutPrompt()));
			
			JSONArray vrHelpArray = JsonUtils.readJsonArrayFromJsonObject(parameters, SetGlobalProperties.KEY_VR_HELP);
			List<VrHelpItem> vrHelpList = new ArrayList<VrHelpItem>();
			for (int index = 0; index < vrHelpArray.length(); index++) {
				VrHelpItem chunk = new VrHelpItem(JsonRPCMarshaller.deserializeJSONObject( (JSONObject)vrHelpArray.get(index)) );
				vrHelpList.add(chunk);
			}
			assertTrue(Test.TRUE, Validator.validateVrHelpItems(vrHelpList, cmd.getVrHelp()));			
		} catch (JSONException e) {
			fail(Test.JSON_FAIL);
		}    	
    }
}