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 package android.voiceinteraction.common;
17 
18 import android.app.VoiceInteractor;
19 import android.app.VoiceInteractor.PickOptionRequest.Option;
20 import android.os.Bundle;
21 
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 
25 public class Utils {
26     public enum TestCaseType {
27         COMPLETION_REQUEST_TEST,
28         COMPLETION_REQUEST_CANCEL_TEST,
29         CONFIRMATION_REQUEST_TEST,
30         CONFIRMATION_REQUEST_CANCEL_TEST,
31         ABORT_REQUEST_TEST,
32         ABORT_REQUEST_CANCEL_TEST,
33         PICKOPTION_REQUEST_TEST,
34         PICKOPTION_REQUEST_CANCEL_TEST,
35         COMMANDREQUEST_TEST,
36         COMMANDREQUEST_CANCEL_TEST,
37         SUPPORTS_COMMANDS_TEST,
38     }
39     public static final String TESTCASE_TYPE = "testcase_type";
40     public static final String TESTINFO = "testinfo";
41     public static final String BROADCAST_INTENT = "android.intent.action.VOICE_TESTAPP";
42     public static final String TEST_PROMPT = "testprompt";
43     public static final String PICKOPTON_1 = "one";
44     public static final String PICKOPTON_2 = "two";
45     public static final String PICKOPTON_3 = "3";
46     public static final String TEST_COMMAND = "test_command";
47     public static final String TEST_ONCOMMAND_RESULT = "test_oncommand_result";
48     public static final String TEST_ONCOMMAND_RESULT_VALUE = "test_oncommand_result value";
49 
50     public static final String CONFIRMATION_REQUEST_SUCCESS = "confirmation ok";
51     public static final String COMPLETION_REQUEST_SUCCESS = "completion ok";
52     public static final String ABORT_REQUEST_SUCCESS = "abort ok";
53     public static final String PICKOPTION_REQUEST_SUCCESS = "pickoption ok";
54     public static final String COMMANDREQUEST_SUCCESS = "commandrequest ok";
55     public static final String SUPPORTS_COMMANDS_SUCCESS = "supportsCommands ok";
56 
57     public static final String CONFIRMATION_REQUEST_CANCEL_SUCCESS = "confirm cancel ok";
58     public static final String COMPLETION_REQUEST_CANCEL_SUCCESS = "completion canel ok";
59     public static final String ABORT_REQUEST_CANCEL_SUCCESS = "abort cancel ok";
60     public static final String PICKOPTION_REQUEST_CANCEL_SUCCESS = "pickoption  cancel ok";
61     public static final String COMMANDREQUEST_CANCEL_SUCCESS = "commandrequest cancel ok";
62     public static final String TEST_ERROR = "Error In Test:";
63 
64     public static final String PRIVATE_OPTIONS_KEY = "private_key";
65     public static final String PRIVATE_OPTIONS_VALUE = "private_value";
66 
toBundleString(Bundle bundle)67     public static final String toBundleString(Bundle bundle) {
68         if (bundle == null) {
69             return "*** Bundle is null ****";
70         }
71         StringBuffer buf = new StringBuffer("Bundle is: ");
72         String testType = bundle.getString(TESTCASE_TYPE);
73         if (testType != null) {
74             buf.append("testcase type = " + testType);
75         }
76         ArrayList<String> info = bundle.getStringArrayList(TESTINFO);
77         if (info != null) {
78             for (String s : info) {
79                 buf.append(s + "\n\t\t");
80             }
81         }
82         return buf.toString();
83     }
84 
toOptionsString(Option[] options)85     public static final String toOptionsString(Option[] options) {
86         StringBuilder sb = new StringBuilder();
87         sb.append("{");
88         for (int i = 0; i < options.length; i++) {
89             if (i >= 1) {
90                 sb.append(", ");
91             }
92             sb.append(options[i].getLabel());
93         }
94         sb.append("}");
95         return sb.toString();
96     }
97 
addErrorResult(final Bundle testinfo, final String msg)98     public static final void addErrorResult(final Bundle testinfo, final String msg) {
99         testinfo.getStringArrayList(testinfo.getString(Utils.TESTCASE_TYPE))
100             .add(TEST_ERROR + " " + msg);
101     }
102 }
103