• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.server.wm.flicker.service.assertors
18 
19 import android.util.Log
20 import com.android.server.wm.flicker.FLICKER_TAG
21 import com.android.server.wm.traces.common.tags.Transition
22 import org.json.JSONArray
23 import org.json.JSONException
24 import org.json.JSONObject
25 
26 object AssertionConfigParser {
27     private const val ASSERTORS_KEY = "assertors"
28     private const val CLASS_KEY = "class"
29     private const val ARGS_KEY = "args"
30     private const val TRANSITION_KEY = "transition"
31     private const val ASSERTIONS_KEY = "assertions"
32 
33     internal const val PRESUBMIT_KEY = "presubmit"
34     internal const val POSTSUBMIT_KEY = "postsubmit"
35     internal const val FLAKY_KEY = "flaky"
36 
37     /**
38      * Parses assertor config JSON file. The format expected is:
39      * <pre>
40      * {
41      *  "assertors": [
42      *   {
43      *      "transition": "ROTATION",
44      *      "assertions": {
45      *         "presubmit": [
46      *              "navBarWindowIsVisible"
47      *              "navBarLayerIsVisible",
48      *              "navBarLayerRotatesAndScales"
49      *          ],
50      *          "postsubmit": [ ],
51      *          "flaky": [
52      *              "entireScreenCovered"
53      *          ]
54      *      }
55      *   }
56      *  ]
57      * }
58      * </pre>
59      *
60      * @param config string containing a json file
61      * @return a list of [AssertionData] assertions
62      */
63     @JvmStatic
parseConfigFilenull64     fun parseConfigFile(config: String): List<AssertionData> {
65         val assertorsConfig = mutableListOf<AssertionData>()
66         val jsonArray = JSONObject(config).getJSONArray(ASSERTORS_KEY)
67 
68         for (i in 0 until jsonArray.length()) {
69             val jsonObject = jsonArray.getJSONObject(i)
70             val jsonAssertions = jsonObject.getJSONObject(ASSERTIONS_KEY)
71             val transitionType = Transition.valueOf(jsonObject.getString(TRANSITION_KEY))
72             val presubmit = parseAssertionArray(
73                 jsonAssertions.getJSONArray(PRESUBMIT_KEY), transitionType, PRESUBMIT_KEY)
74             val postsubmit = parseAssertionArray(
75                 jsonAssertions.getJSONArray(POSTSUBMIT_KEY), transitionType, POSTSUBMIT_KEY)
76             val flaky = parseAssertionArray(
77                 jsonAssertions.getJSONArray(FLAKY_KEY), transitionType, FLAKY_KEY)
78             val assertionsList = presubmit + postsubmit + flaky
79 
80             assertorsConfig.addAll(assertionsList)
81         }
82 
83         return assertorsConfig
84     }
85 
86     /**
87      * Splits an assertions JSONArray into an array of [AssertionData].
88      *
89      * @param assertionsArray a [JSONArray] with assertion names
90      * @param transitionType type of transition connected to this assertion
91      * @param category the category of the assertion (presubmit/postsubmit/flaky)
92      * @return an array of assertion details
93      */
94     @JvmStatic
parseAssertionArraynull95     private fun parseAssertionArray(
96         assertionsArray: JSONArray,
97         transitionType: Transition,
98         category: String
99     ): List<AssertionData> {
100         val assertions = mutableListOf<AssertionData>()
101         try {
102             for (i in 0 until assertionsArray.length()) {
103                 val assertionObj = assertionsArray.getJSONObject(i)
104                 val assertionClass = assertionObj.getString(CLASS_KEY)
105                 val args = mutableListOf<String>()
106                 if (assertionObj.has(ARGS_KEY)) {
107                     val assertionArgsArray = assertionObj.getJSONArray(ARGS_KEY)
108                     for (j in 0 until assertionArgsArray.length()) {
109                         val arg = assertionArgsArray.getString(j)
110                         args.add(arg)
111                     }
112                 }
113                 Log.v(FLICKER_TAG, "Creating assertion for class $assertionClass")
114                 assertions.add(
115                     AssertionData.fromString(transitionType, category, assertionClass, args))
116             }
117         } catch (e: JSONException) {
118             throw RuntimeException(e)
119         }
120 
121         return assertions
122     }
123 }