1 /*
2  * Copyright (C) 2015 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 
17 package com.android.tradefed.result;
18 
19 import org.json.JSONException;
20 import org.json.JSONObject;
21 
22 import java.util.regex.Pattern;
23 
24 /** encapsulates compatibility run results for a single app package tested */
25 public class CompatibilityTestResult {
26 
27     public static final String KEY_PACKAGE = "app_package";
28     // Keep app_version for backwards compatibility
29     public static final String KEY_VERSION_CODE = "app_version_code";
30     public static final String KEY_VERSION_STRING = "app_version_string";
31     public static final String KEY_NAME = "app_name";
32     public static final String KEY_RANK = "app_rank";
33     public static final String KEY_STATUS = "status";
34     public static final String KEY_MESSAGE = "message";
35 
36     public static final String SEPARATOR = "=@ppcomp@t=";
37     public static final Pattern REGEX =
38             Pattern.compile(String.format("^%s(.*?)%s", SEPARATOR, SEPARATOR));
39 
40     public static final String STATUS_SUCCESS = "success";
41     public static final String STATUS_ERROR = "error"; // installation errors etc
42     public static final String STATUS_FAILURE = "failure"; // app launch failures
43 
44     public String packageName = null;
45     public String versionString = null;
46     public String versionCode = null;
47     public String name = null;
48     public Integer rank = null;
49     public String status = null;
50     public String message = null;
51 
52     /**
53      * Return the Serialized fields into JSON string
54      *
55      * @throws JSONException
56      */
toJsonString()57     public String toJsonString() throws JSONException {
58         JSONObject o = new JSONObject();
59         o.put(KEY_PACKAGE, packageName);
60         o.put(KEY_VERSION_STRING, versionString);
61         o.put(KEY_VERSION_CODE, versionString);
62         o.put(KEY_NAME, name);
63         o.put(KEY_RANK, rank);
64         o.put(KEY_STATUS, status);
65         o.put(KEY_MESSAGE, message);
66         return o.toString();
67     }
68 
69     /**
70      * Reconstructs an instance from a JSON string
71      *
72      * @param json
73      * @return the {@link CompatibilityTestResult} instance from the JSON serialized string.
74      * @throws JSONException
75      */
fromJsonString(String json)76     public static CompatibilityTestResult fromJsonString(String json) throws JSONException {
77         JSONObject o = new JSONObject(json);
78         CompatibilityTestResult result = new CompatibilityTestResult();
79         result.packageName = o.getString(KEY_PACKAGE);
80         if (o.has(KEY_VERSION_STRING)) {
81             result.versionString = o.getString(KEY_VERSION_STRING);
82         }
83         if (o.has(KEY_VERSION_CODE)) {
84             result.versionString = o.getString(KEY_VERSION_CODE);
85         }
86         if (o.has(KEY_NAME)) {
87             result.name = o.getString(KEY_NAME);
88         }
89         if (o.has(KEY_RANK)) {
90             result.rank = o.getInt(KEY_RANK);
91         }
92         result.status = o.getString(KEY_STATUS);
93         if (o.has(KEY_MESSAGE)) {
94             result.message = o.getString(KEY_MESSAGE);
95         }
96         return result;
97     }
98 }
99