DO NOT MERGE DeviceInfoActivity: Add ability start anonymous array and object
Bug: 22305795
Change-Id: I0de0e74507ad36d4e1c5e7d7aa8f59e1d178030e
diff --git a/common/device-side/device-info/src/com/android/compatibility/common/deviceinfo/DeviceInfoActivity.java b/common/device-side/device-info/src/com/android/compatibility/common/deviceinfo/DeviceInfoActivity.java
index 62e512c..f9de6eb 100644
--- a/common/device-side/device-info/src/com/android/compatibility/common/deviceinfo/DeviceInfoActivity.java
+++ b/common/device-side/device-info/src/com/android/compatibility/common/deviceinfo/DeviceInfoActivity.java
@@ -182,6 +182,17 @@
/**
* Start a new group of result.
*/
+ public void startGroup() {
+ try {
+ mJsonWriter.beginObject();
+ } catch (Exception e) {
+ error("Failed to begin JSON group: " + e.getMessage());
+ }
+ }
+
+ /**
+ * Start a new group of result with specified name.
+ */
public void startGroup(String name) {
try {
mJsonWriter.name(name);
@@ -203,6 +214,41 @@
}
/**
+ * Start a new array of result.
+ */
+ public void startArray() {
+ try {
+ mJsonWriter.beginArray();
+ } catch (Exception e) {
+ error("Failed to begin JSON array: " + e.getMessage());
+ }
+ }
+
+ /**
+ * Start a new array of result with specified name.
+ */
+ public void startArray(String name) {
+ checkName(name);
+ try {
+ mJsonWriter.name(name);
+ mJsonWriter.beginArray();
+ } catch (Exception e) {
+ error("Failed to begin JSON array: " + e.getMessage());
+ }
+ }
+
+ /**
+ * Complete adding result to the last started array.
+ */
+ public void endArray() {
+ try {
+ mJsonWriter.endArray();
+ } catch (Exception e) {
+ error("Failed to end JSON group: " + e.getMessage());
+ }
+ }
+
+ /**
* Add a double value result.
*/
public void addResult(String name, double value) {
diff --git a/common/device-side/device-info/tests/src/com/android/compatibility/common/deviceinfo/DeviceInfoActivityTest.java b/common/device-side/device-info/tests/src/com/android/compatibility/common/deviceinfo/DeviceInfoActivityTest.java
index 8a8a66c..d092f4e 100644
--- a/common/device-side/device-info/tests/src/com/android/compatibility/common/deviceinfo/DeviceInfoActivityTest.java
+++ b/common/device-side/device-info/tests/src/com/android/compatibility/common/deviceinfo/DeviceInfoActivityTest.java
@@ -25,15 +25,15 @@
/**
* Test for {@link DeviceInfoActivity}.
*/
-public class DeviceInfoActivityTest extends ActivityInstrumentationTestCase2<SampleDeviceInfo> {
+public class DeviceInfoActivityTest extends ActivityInstrumentationTestCase2<TestDeviceInfo> {
private static final String EXPECTED_FILE_PATH =
- "/storage/emulated/0/device-info-files/SampleDeviceInfo.deviceinfo.json";
+ "/storage/emulated/0/device-info-files/TestDeviceInfo.deviceinfo.json";
- private SampleDeviceInfo mActivity;
+ private TestDeviceInfo mActivity;
public DeviceInfoActivityTest() {
- super(SampleDeviceInfo.class);
+ super(TestDeviceInfo.class);
}
@Override
@@ -59,7 +59,7 @@
assertEquals("Incorrect file path", EXPECTED_FILE_PATH, resultFilePath);
// Check json file content
String jsonContent = readFile(resultFilePath);
- assertEquals("Incorrect json output", ExampleObjects.sampleDeviceInfoJson(), jsonContent);
+ assertEquals("Incorrect json output", ExampleObjects.testDeviceInfoJson(), jsonContent);
}
private String readFile(String filePath) throws IOException {
diff --git a/common/device-side/device-info/tests/src/com/android/compatibility/common/deviceinfo/ExampleObjects.java b/common/device-side/device-info/tests/src/com/android/compatibility/common/deviceinfo/ExampleObjects.java
index 92e7df1..570bdd5 100644
--- a/common/device-side/device-info/tests/src/com/android/compatibility/common/deviceinfo/ExampleObjects.java
+++ b/common/device-side/device-info/tests/src/com/android/compatibility/common/deviceinfo/ExampleObjects.java
@@ -20,53 +20,73 @@
*/
public final class ExampleObjects {
+ // Must match DeviceInfoActivity.MAX_STRING_VALUE_LENGTH and
+ // DeviceInfoActivity.MAX_ARRAY_LENGTH
private static final int MAX_LENGTH = 1000;
- private static final String SAMPLE_DEVICE_INFO_JSON = "{\n" +
- " \"foo\": {\n" +
- " \"foo_boolean\": true,\n" +
- " \"bar\": {\n" +
- " \"bar_string\": [\n" +
- " \"bar-string-1\",\n" +
- " \"bar-string-2\",\n" +
- " \"bar-string-3\"\n" +
- " ],\n" +
- " \"bar_boolean\": [\n" +
- " true,\n" +
- " false\n" +
- " ],\n" +
- " \"bar_double\": [\n" +
- " 1.7976931348623157E308,\n" +
- " 4.9E-324\n" +
- " ],\n" +
- " \"bar_int\": [\n" +
- " 2147483647,\n" +
- " -2147483648\n" +
- " ],\n" +
- " \"bar_long\": [\n" +
- " 9223372036854775807,\n" +
- " -9223372036854775808\n" +
+ private static final String TEST_DEVICE_INFO_JSON = "{\n" +
+ " \"test_boolean\": true,\n" +
+ " \"test_double\": 1.23456789,\n" +
+ " \"test_int\": 123456789,\n" +
+ " \"test_long\": 9223372036854775807,\n" +
+ " \"test_string\": \"test string\",\n" +
+ " \"test_strings\": [\n" +
+ " \"test string 1\",\n" +
+ " \"test string 2\",\n" +
+ " \"test string 3\"\n" +
+ " ],\n" +
+ " \"test_group\": {\n" +
+ " \"test_boolean\": false,\n" +
+ " \"test_double\": 9.87654321,\n" +
+ " \"test_int\": 987654321,\n" +
+ " \"test_long\": 9223372036854775807,\n" +
+ " \"test_string\": \"test group string\",\n" +
+ " \"test_strings\": [\n" +
+ " \"test group string 1\",\n" +
+ " \"test group string 2\",\n" +
+ " \"test group string 3\"\n" +
+ " ]\n" +
+ " },\n" +
+ " \"test_groups\": [\n" +
+ " {\n" +
+ " \"test_string\": \"test groups string 1\",\n" +
+ " \"test_strings\": [\n" +
+ " \"test groups string 1-1\",\n" +
+ " \"test groups string 1-2\",\n" +
+ " \"test groups string 1-3\"\n" +
" ]\n" +
" },\n" +
- " \"foo_double\": 1.7976931348623157E308,\n" +
- " \"foo_int\": 2147483647,\n" +
- " \"foo_long\": 9223372036854775807,\n" +
- " \"foo_string\": \"foo-string\",\n" +
- " \"long_string\": \"%s\",\n" +
- " \"long_int_array\": [\n%s" +
- " ]\n" +
- " }\n" +
+ " {\n" +
+ " \"test_string\": \"test groups string 2\",\n" +
+ " \"test_strings\": [\n" +
+ " \"test groups string 2-1\",\n" +
+ " \"test groups string 2-2\",\n" +
+ " \"test groups string 2-3\"\n" +
+ " ]\n" +
+ " },\n" +
+ " {\n" +
+ " \"test_string\": \"test groups string 3\",\n" +
+ " \"test_strings\": [\n" +
+ " \"test groups string 3-1\",\n" +
+ " \"test groups string 3-2\",\n" +
+ " \"test groups string 3-3\"\n" +
+ " ]\n" +
+ " }\n" +
+ " ],\n" +
+ " \"max_length_string\": \"%s\",\n" +
+ " \"max_num_ints\": [\n%s" +
+ " ]\n" +
"}\n";
- public static String sampleDeviceInfoJson() {
+ public static String testDeviceInfoJson() {
StringBuilder longStringSb = new StringBuilder();
StringBuilder longArraySb = new StringBuilder();
int lastNum = MAX_LENGTH - 1;
for (int i = 0; i < MAX_LENGTH; i++) {
longStringSb.append("a");
- longArraySb.append(String.format(" %d%s\n", i, ((i == lastNum)? "" : ",")));
+ longArraySb.append(String.format(" %d%s\n", i, ((i == lastNum)? "" : ",")));
}
- return String.format(SAMPLE_DEVICE_INFO_JSON,
+ return String.format(TEST_DEVICE_INFO_JSON,
longStringSb.toString(), longArraySb.toString());
}
}
\ No newline at end of file
diff --git a/common/device-side/device-info/tests/src/com/android/compatibility/common/deviceinfo/TestDeviceInfo.java b/common/device-side/device-info/tests/src/com/android/compatibility/common/deviceinfo/TestDeviceInfo.java
new file mode 100644
index 0000000..7f82942
--- /dev/null
+++ b/common/device-side/device-info/tests/src/com/android/compatibility/common/deviceinfo/TestDeviceInfo.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.compatibility.common.deviceinfo;
+
+import android.os.Bundle;
+
+import java.lang.StringBuilder;
+
+/**
+ * Collector for testing DeviceInfoActivity
+ */
+public class TestDeviceInfo extends DeviceInfoActivity {
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ }
+
+ @Override
+ protected void collectDeviceInfo() {
+
+ // Test primitive results
+ addResult("test_boolean", true);
+ addResult("test_double", 1.23456789);
+ addResult("test_int", 123456789);
+ addResult("test_long", Long.MAX_VALUE);
+ addResult("test_string", "test string");
+ addArray("test_strings", new String[] {
+ "test string 1",
+ "test string 2",
+ "test string 3",
+ });
+
+ // Test group
+ startGroup("test_group");
+ addResult("test_boolean", false);
+ addResult("test_double", 9.87654321);
+ addResult("test_int", 987654321);
+ addResult("test_long", Long.MAX_VALUE);
+ addResult("test_string", "test group string");
+ addArray("test_strings", new String[] {
+ "test group string 1",
+ "test group string 2",
+ "test group string 3"
+ });
+ endGroup(); // test_group
+
+ // Test array of groups
+ startArray("test_groups");
+ for (int i = 1; i < 4; i++) {
+ startGroup();
+ addResult("test_string", "test groups string " + i);
+ addArray("test_strings", new String[] {
+ "test groups string " + i + "-1",
+ "test groups string " + i + "-2",
+ "test groups string " + i + "-3"
+ });
+ endGroup();
+ }
+ endArray(); // test_groups
+
+ // Test max
+ StringBuilder sb = new StringBuilder();
+ int[] arr = new int[1001];
+ for (int i = 0; i < 1001; i++) {
+ sb.append("a");
+ arr[i] = i;
+ }
+ addResult("max_length_string", sb.toString());
+ addArray("max_num_ints", arr);
+ }
+}