summaryrefslogtreecommitdiff
path: root/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/rpc/datatypes/ImageFieldTests.java
blob: 0c4090870a49e9929bfa62fce0d1a446000cbe0b (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
package com.smartdevicelink.test.rpc.datatypes;

import com.smartdevicelink.marshal.JsonRPCMarshaller;
import com.smartdevicelink.proxy.rpc.ImageField;
import com.smartdevicelink.proxy.rpc.ImageResolution;
import com.smartdevicelink.proxy.rpc.enums.FileType;
import com.smartdevicelink.proxy.rpc.enums.ImageFieldName;
import com.smartdevicelink.test.JsonUtils;
import com.smartdevicelink.test.TestValues;
import com.smartdevicelink.test.Validator;

import junit.framework.TestCase;

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

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

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

    private ImageField msg;

    @Override
    public void setUp(){
        msg = new ImageField();

        msg.setImageResolution(TestValues.GENERAL_IMAGERESOLUTION);
        msg.setImageTypeSupported(TestValues.GENERAL_FILETYPE_LIST);
        msg.setName(TestValues.GENERAL_IMAGEFIELDNAME);
    }

    /**
	 * Tests the expected values of the RPC message.
	 */
    public void testRpcValues () {
    	// Test Values
        ImageResolution imageRes = msg.getImageResolution();
        List<FileType> imageTypes = msg.getImageTypeSupported();
        ImageFieldName name = msg.getName();
        
        // Valid Tests
        assertTrue(TestValues.TRUE, Validator.validateImageResolution(TestValues.GENERAL_IMAGERESOLUTION, imageRes));
        assertEquals(TestValues.MATCH, TestValues.GENERAL_IMAGEFIELDNAME, name);
        assertTrue(TestValues.TRUE, Validator.validateFileTypes(TestValues.GENERAL_FILETYPE_LIST, imageTypes));
                
        // Invalid/Null Tests
        ImageField msg = new ImageField();
        assertNotNull(TestValues.NOT_NULL, msg);

        assertNull(TestValues.NULL, msg.getImageResolution());
        assertNull(TestValues.NULL, msg.getImageTypeSupported());
        assertNull(TestValues.NULL, msg.getName());
    }

    public void testJson(){
        JSONObject reference = new JSONObject();

        try{
            reference.put(ImageField.KEY_IMAGE_RESOLUTION, TestValues.JSON_IMAGERESOLUTION);
            reference.put(ImageField.KEY_IMAGE_TYPE_SUPPORTED, JsonUtils.createJsonArray(TestValues.GENERAL_FILETYPE_LIST));
            reference.put(ImageField.KEY_NAME, TestValues.GENERAL_IMAGEFIELDNAME);
            
            JSONObject underTest = msg.serializeJSON();
            assertEquals(TestValues.MATCH, reference.length(), underTest.length());

            Iterator<?> iterator = reference.keys();
            while(iterator.hasNext()){
                String key = (String) iterator.next();

                if(key.equals(ImageField.KEY_IMAGE_RESOLUTION)){
                	JSONObject objectEquals = JsonUtils.readJsonObjectFromJsonObject(reference, key);
                	JSONObject testEquals = JsonUtils.readJsonObjectFromJsonObject(underTest, key);
                	Hashtable<String, Object> hashReference = JsonRPCMarshaller.deserializeJSONObject(objectEquals);
                	Hashtable<String, Object> hashTest = JsonRPCMarshaller.deserializeJSONObject(testEquals);                	
                    assertTrue(TestValues.TRUE, Validator.validateImageResolution( new ImageResolution(hashReference), new ImageResolution(hashTest)));
                } else if(key.equals(ImageField.KEY_IMAGE_TYPE_SUPPORTED)) {
					JSONArray imageTypeArrayReference = JsonUtils.readJsonArrayFromJsonObject(reference, key);
					JSONArray imageTypeArrayTest = JsonUtils.readJsonArrayFromJsonObject(underTest, key);
					List<FileType> imageTypeListReference = new ArrayList<FileType>();
					List<FileType> imageTypeListTest = new ArrayList<FileType>();					
					
					assertEquals(TestValues.MATCH, imageTypeArrayReference.length(), imageTypeArrayTest.length());
					
				    for (int index = 0 ; index < imageTypeArrayReference.length(); index++) {
				    	imageTypeListReference.add( (FileType)imageTypeArrayReference.get(index) );
				    	imageTypeListTest.add( (FileType)imageTypeArrayTest.get(index) );
				    }				    
					assertTrue(TestValues.TRUE, imageTypeListReference.containsAll(imageTypeListTest) && imageTypeListTest.containsAll(imageTypeListReference));
				} else{
                    assertEquals(TestValues.MATCH, JsonUtils.readObjectFromJsonObject(reference, key), JsonUtils.readObjectFromJsonObject(underTest, key));
                }
            }
        }catch(JSONException e){
        	fail(TestValues.JSON_FAIL);
        }
    }
}