1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.loganalysis.item;
17 
18 import com.android.loganalysis.item.LocationDumpsItem.LocationInfoItem;
19 
20 import junit.framework.TestCase;
21 
22 import org.json.JSONArray;
23 import org.json.JSONException;
24 import org.json.JSONObject;
25 
26 /**
27  * Unit test for {@link LocationDumpsItem}.
28  */
29 public class LocationDumpsItemTest extends TestCase {
30 
31     /**
32      * Test that {@link LocationDumpsItem#toJson()} returns correctly.
33      */
testToJson()34     public void testToJson() throws JSONException {
35         LocationDumpsItem item = new LocationDumpsItem();
36         item.addLocationClient("com.google.android.gms", 500, 60, 1000, "PRIORITY_ACCURACY", 45);
37         item.addLocationClient("com.google.android.maps", 0, 0, 0, "PRIORITY_ACCURACY", 55);
38 
39         // Convert to JSON string and back again
40         JSONObject output = new JSONObject(item.toJson().toString());
41 
42         assertTrue(output.has(LocationDumpsItem.LOCATION_CLIENTS));
43         assertTrue(output.get(LocationDumpsItem.LOCATION_CLIENTS) instanceof JSONArray);
44 
45         JSONArray locationClients = output.getJSONArray(LocationDumpsItem.LOCATION_CLIENTS);
46 
47         assertEquals(2, locationClients.length());
48         assertTrue(locationClients.getJSONObject(0).has(LocationInfoItem.PACKAGE));
49         assertTrue(locationClients.getJSONObject(0).has(LocationInfoItem.EFFECTIVE_INTERVAL));
50         assertTrue(locationClients.getJSONObject(0).has(LocationInfoItem.MIN_INTERVAL));
51         assertTrue(locationClients.getJSONObject(0).has(LocationInfoItem.MAX_INTERVAL));
52         assertTrue(locationClients.getJSONObject(0).has(LocationInfoItem.REQUEST_PRIORITY));
53         assertTrue(locationClients.getJSONObject(0).has(LocationInfoItem.LOCATION_DURATION));
54 
55         assertTrue(locationClients.getJSONObject(1).has(LocationInfoItem.PACKAGE));
56         assertTrue(locationClients.getJSONObject(1).has(LocationInfoItem.EFFECTIVE_INTERVAL));
57         assertTrue(locationClients.getJSONObject(1).has(LocationInfoItem.MIN_INTERVAL));
58         assertTrue(locationClients.getJSONObject(1).has(LocationInfoItem.MAX_INTERVAL));
59         assertTrue(locationClients.getJSONObject(1).has(LocationInfoItem.REQUEST_PRIORITY));
60         assertTrue(locationClients.getJSONObject(1).has(LocationInfoItem.LOCATION_DURATION));
61     }
62 
63     /**
64      * Test that {@link LocationDumpsItem#getLocationClients()} returns correctly.
65      */
testGetLocationDumps()66     public void testGetLocationDumps() {
67         LocationDumpsItem item = new LocationDumpsItem();
68         item.addLocationClient("com.google.android.gms", 500, 60, 1000, "PRIORITY_ACCURACY", 45);
69 
70         assertEquals(item.getLocationClients().size(), 1);
71         LocationInfoItem client = item.getLocationClients().iterator().next();
72         assertNotNull(client);
73         assertEquals(client.getPackage(), "com.google.android.gms");
74         assertEquals(client.getEffectiveInterval(), 500);
75         assertEquals(client.getMinInterval(), 60);
76         assertEquals(client.getMaxInterval(), 1000);
77         assertEquals(client.getPriority(), "PRIORITY_ACCURACY");
78         assertEquals(client.getDuration(), 45);
79     }
80 
81 }
82