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