1 /*
2  * Copyright (C) 2022 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.systemui.dump
18 
19 import androidx.test.filters.SmallTest
20 
21 import com.android.systemui.SysuiTestCase
22 import com.google.common.truth.Truth.assertThat
23 
24 import org.junit.Assert.assertEquals
25 import org.junit.Before
26 import org.junit.Test
27 
28 import java.io.PrintWriter
29 import java.io.StringWriter
30 
31 @SmallTest
32 class DumpsysTableLoggerTest : SysuiTestCase() {
33     private val logger = DumpsysTableLogger(
34             TEST_SECTION_NAME,
35             TEST_COLUMNS,
36             TEST_DATA_VALID)
37 
38     private val stringWriter = StringWriter()
39     private val printWriter = PrintWriter(stringWriter)
40 
41     @Before
42     fun setup() {
43     }
44 
45     @Test
46     fun testTableLogger_header() {
47         logger.printTableData(printWriter)
48         val lines = logLines(stringWriter)
49 
50         val line1 = lines[0]
51 
52         assertEquals("table logger header is incorrect",
53                 HEADER_PREFIX + TEST_SECTION_NAME, line1)
54     }
55 
56     @Test
57     fun testTableLogger_version() {
58         logger.printTableData(printWriter)
59         val lines = logLines(stringWriter)
60 
61         val line2 = lines[1]
62 
63         assertEquals("version probably shouldn't have changed",
64         "version $VERSION", line2)
65     }
66 
67     @Test
68     fun testTableLogger_footer() {
69         logger.printTableData(printWriter)
70         val lines = logLines(stringWriter)
71 
72         val footer = lines.last()
73         android.util.Log.d("evanevan", footer)
74         android.util.Log.d("evanevan", lines.toString())
75 
76         assertEquals("table logger footer is incorrect",
77                 FOOTER_PREFIX + TEST_SECTION_NAME, footer)
78     }
79 
80     @Test
81     fun testTableLogger_data_length() {
82         logger.printTableData(printWriter)
83         val lines = logLines(stringWriter)
84 
85         // Header is 2 lines long, plus a line for the column defs so data is lines[3..last()-1]
86         val data = lines.subList(3, lines.size - 1)
87         assertEquals(TEST_DATA_LENGTH, data.size)
88     }
89 
90     @Test
91     fun testTableLogger_data_columns() {
92         logger.printTableData(printWriter)
93         val lines = logLines(stringWriter)
94 
95         // Header is always 2 lines long so data is lines[2..last()-1]
96         val data = lines.subList(3, lines.size - 1)
97 
98         data.forEach { dataLine ->
99             assertEquals(TEST_COLUMNS.size, dataLine.split(SEPARATOR).size)
100         }
101     }
102 
103     @Test
104     fun testInvalidLinesAreFiltered() {
105         // GIVEN an invalid data row, by virtue of having an extra field
106         val invalidLine = List(TEST_COLUMNS.size) { col ->
107             "data${col}X"
108         } + "INVALID COLUMN"
109         val invalidData = TEST_DATA_VALID.toMutableList().also {
110             it.add(invalidLine)
111         }
112 
113         // WHEN the table logger is created and asked to print the table
114         val tableLogger = DumpsysTableLogger(
115                 TEST_SECTION_NAME,
116                 TEST_COLUMNS,
117                 invalidData)
118 
119         tableLogger.printTableData(printWriter)
120 
121         // THEN the invalid line is filtered out
122         val invalidString = invalidLine.joinToString(separator = SEPARATOR)
123         val logString = stringWriter.toString()
124 
125         assertThat(logString).doesNotContain(invalidString)
126     }
127 
128     private fun logLines(sw: StringWriter): List<String> {
129         return sw.toString().split("\n").filter { it.isNotBlank() }
130     }
131 }
132 
133 // Copying these here from [DumpsysTableLogger] so that we catch any accidental versioning change
134 private const val HEADER_PREFIX = "SystemUI TableSection START: "
135 private const val FOOTER_PREFIX = "SystemUI TableSection END: "
136 private const val SEPARATOR = "|" // TBD
137 private const val VERSION = "1"
138 
139 const val TEST_SECTION_NAME = "TestTableSection"
140 const val TEST_DATA_LENGTH = 5
141 val TEST_COLUMNS = arrayListOf("col1", "col2", "col3")
142 val TEST_DATA_VALID = List(TEST_DATA_LENGTH) { row ->
143     List(TEST_COLUMNS.size) { col ->
144         "data$col$row"
145     }
146 }