1 /*
2  * Copyright (C) 2016 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 android.util;
18 
19 import androidx.test.filters.LargeTest;
20 
21 import junit.framework.TestCase;
22 
23 import java.io.PrintWriter;
24 import java.io.StringWriter;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.List;
28 
29 @LargeTest
30 public class LocalLogTest extends TestCase {
31 
testA_localTimestamps()32     public void testA_localTimestamps() {
33         boolean localTimestamps = true;
34         doTestA(localTimestamps);
35     }
36 
testA_nonLocalTimestamps()37     public void testA_nonLocalTimestamps() {
38         boolean localTimestamps = false;
39         doTestA(localTimestamps);
40     }
41 
doTestA(boolean localTimestamps)42     private void doTestA(boolean localTimestamps) {
43         String[] lines = {
44                 "foo",
45                 "bar",
46                 "baz"
47         };
48         String[] want = lines;
49         testcase(new LocalLog(10, localTimestamps), lines, want);
50     }
51 
testB()52     public void testB() {
53         String[] lines = {
54             "foo",
55             "bar",
56             "baz"
57         };
58         String[] want = {};
59         testcase(new LocalLog(0), lines, want);
60     }
61 
testC()62     public void testC() {
63         String[] lines = {
64             "dropped",
65             "dropped",
66             "dropped",
67             "dropped",
68             "dropped",
69             "dropped",
70             "foo",
71             "bar",
72             "baz",
73         };
74         String[] want = {
75             "foo",
76             "bar",
77             "baz",
78         };
79         testcase(new LocalLog(3), lines, want);
80     }
81 
testcase(LocalLog logger, String[] input, String[] want)82     void testcase(LocalLog logger, String[] input, String[] want) {
83         for (String l : input) {
84             logger.log(l);
85         }
86         verifyAllLines(want, dump(logger).split("\n"));
87         verifyAllLines(reverse(want), reverseDump(logger).split("\n"));
88     }
89 
verifyAllLines(String[] wantLines, String[] gotLines)90     void verifyAllLines(String[] wantLines, String[] gotLines) {
91         for (int i = 0; i < wantLines.length; i++) {
92             String want = wantLines[i];
93             String got = gotLines[i];
94             String msg = String.format("%s did not contain %s", quote(got), quote(want));
95             assertTrue(msg, got.contains(want));
96         }
97     }
98 
dump(LocalLog logger)99     static String dump(LocalLog logger) {
100         StringWriter buffer = new StringWriter();
101         PrintWriter writer = new PrintWriter(buffer);
102         logger.dump(null, writer, new String[0]);
103         return buffer.toString();
104     }
105 
reverseDump(LocalLog logger)106     static String reverseDump(LocalLog logger) {
107         StringWriter buffer = new StringWriter();
108         PrintWriter writer = new PrintWriter(buffer);
109         logger.reverseDump(null, writer, new String[0]);
110         return buffer.toString();
111     }
112 
quote(String s)113     static String quote(String s) {
114         return '"' + s + '"';
115     }
116 
reverse(String[] ary)117     static String[] reverse(String[] ary) {
118         List<String> ls = Arrays.asList(ary);
119         Collections.reverse(ls);
120         return  ls.toArray(new String[ary.length]);
121     }
122 }
123