OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.base; | 5 package org.chromium.base; |
6 | 6 |
7 import static org.junit.Assert.assertEquals; | 7 import static org.junit.Assert.assertEquals; |
8 import static org.junit.Assert.assertNull; | 8 import static org.junit.Assert.assertNull; |
9 import static org.junit.Assert.assertTrue; | 9 import static org.junit.Assert.assertTrue; |
10 | 10 |
11 import org.chromium.testing.local.LocalRobolectricTestRunner; | 11 import org.chromium.testing.local.LocalRobolectricTestRunner; |
12 import org.junit.Before; | |
13 import org.junit.Test; | 12 import org.junit.Test; |
14 import org.junit.runner.RunWith; | 13 import org.junit.runner.RunWith; |
15 import org.robolectric.annotation.Config; | 14 import org.robolectric.annotation.Config; |
16 import org.robolectric.annotation.Implementation; | |
17 import org.robolectric.annotation.Implements; | |
18 import org.robolectric.shadows.ShadowLog; | 15 import org.robolectric.shadows.ShadowLog; |
19 | 16 |
20 import java.util.List; | 17 import java.util.List; |
21 | 18 |
22 /** Unit tests for {@link Log}. */ | 19 /** Unit tests for {@link Log}. */ |
23 @RunWith(LocalRobolectricTestRunner.class) | 20 @RunWith(LocalRobolectricTestRunner.class) |
24 @Config(manifest = Config.NONE, shadows = {LogTest.PermissiveShadowLog.class}) | 21 @Config(manifest = Config.NONE) |
25 public class LogTest { | 22 public class LogTest { |
26 /** Tests that the computed call origin is the correct one. */ | 23 /** Tests that the computed call origin is the correct one. */ |
27 @Test | 24 @Test |
28 public void callOriginTest() { | 25 public void callOriginTest() { |
29 Log.d("Foo", "Bar"); | 26 Log.d("Foo", "Bar"); |
30 | 27 |
31 List<ShadowLog.LogItem> logs = ShadowLog.getLogs(); | 28 List<ShadowLog.LogItem> logs = ShadowLog.getLogs(); |
32 assertEquals("Only one log should be written", 1, logs.size()); | 29 assertEquals("Only one log should be written", 1, logs.size()); |
33 | 30 |
34 assertTrue("The origin of the log message (" + logs.get(0).msg + ") look
s wrong.", | 31 assertTrue("The origin of the log message (" + logs.get(0).msg + ") look
s wrong.", |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 // Non throwable are properly identified | 64 // Non throwable are properly identified |
68 Log.i("Foo", "Bar %s", t, "Baz"); | 65 Log.i("Foo", "Bar %s", t, "Baz"); |
69 assertNull(logs.get(logs.size() - 1).throwable); | 66 assertNull(logs.get(logs.size() - 1).throwable); |
70 assertEquals("Bar MyThrowable", logs.get(logs.size() - 1).msg); | 67 assertEquals("Bar MyThrowable", logs.get(logs.size() - 1).msg); |
71 | 68 |
72 // The last throwable is the one used that is going to be printed out | 69 // The last throwable is the one used that is going to be printed out |
73 Log.i("Foo", "Bar %s %s", t, t2); | 70 Log.i("Foo", "Bar %s %s", t, t2); |
74 assertEquals(t2, logs.get(logs.size() - 1).throwable); | 71 assertEquals(t2, logs.get(logs.size() - 1).throwable); |
75 assertEquals("Bar MyThrowable MyOtherThrowable", logs.get(logs.size() -
1).msg); | 72 assertEquals("Bar MyThrowable MyOtherThrowable", logs.get(logs.size() -
1).msg); |
76 } | 73 } |
77 | |
78 public void verboseLoggingTest() { | |
79 PermissiveShadowLog.setLevel(Log.VERBOSE); | |
80 List<ShadowLog.LogItem> logs = ShadowLog.getLogs(); | |
81 | |
82 Log.wtf("Foo", "Bar"); | |
83 Log.e("Foo", "Bar"); | |
84 Log.w("Foo", "Bar"); | |
85 Log.i("Foo", "Bar"); | |
86 Log.d("Foo", "Bar"); | |
87 Log.v("Foo", "Bar"); | |
88 | |
89 assertEquals(Log.ASSERT, logs.get(0).type); | |
90 assertEquals(Log.ERROR, logs.get(1).type); | |
91 assertEquals(Log.WARN, logs.get(2).type); | |
92 assertEquals(Log.INFO, logs.get(3).type); | |
93 assertEquals(Log.DEBUG, logs.get(4).type); | |
94 assertEquals(Log.VERBOSE, logs.get(5).type); | |
95 assertEquals(6, logs.size()); | |
96 } | |
97 | |
98 @Test | |
99 public void debugLoggingTest() { | |
100 PermissiveShadowLog.setLevel(Log.DEBUG); | |
101 List<ShadowLog.LogItem> logs = ShadowLog.getLogs(); | |
102 | |
103 Log.wtf("Foo", "Bar"); | |
104 Log.e("Foo", "Bar"); | |
105 Log.w("Foo", "Bar"); | |
106 Log.i("Foo", "Bar"); | |
107 Log.d("Foo", "Bar"); | |
108 Log.v("Foo", "Bar"); | |
109 | |
110 assertEquals(Log.ASSERT, logs.get(0).type); | |
111 assertEquals(Log.ERROR, logs.get(1).type); | |
112 assertEquals(Log.WARN, logs.get(2).type); | |
113 assertEquals(Log.INFO, logs.get(3).type); | |
114 assertEquals(Log.DEBUG, logs.get(4).type); | |
115 assertEquals(5, logs.size()); | |
116 } | |
117 | |
118 @Test | |
119 public void infoLoggingTest() { | |
120 PermissiveShadowLog.setLevel(Log.INFO); | |
121 List<ShadowLog.LogItem> logs = ShadowLog.getLogs(); | |
122 | |
123 Log.wtf("Foo", "Bar"); | |
124 Log.e("Foo", "Bar"); | |
125 Log.w("Foo", "Bar"); | |
126 Log.i("Foo", "Bar"); | |
127 Log.d("Foo", "Bar"); | |
128 Log.v("Foo", "Bar"); | |
129 | |
130 assertEquals(Log.ASSERT, logs.get(0).type); | |
131 assertEquals(Log.ERROR, logs.get(1).type); | |
132 assertEquals(Log.WARN, logs.get(2).type); | |
133 assertEquals(Log.INFO, logs.get(3).type); | |
134 assertEquals(4, logs.size()); | |
135 } | |
136 | |
137 @Test | |
138 public void warnLoggingTest() { | |
139 PermissiveShadowLog.setLevel(Log.WARN); | |
140 List<ShadowLog.LogItem> logs = ShadowLog.getLogs(); | |
141 | |
142 Log.wtf("Foo", "Bar"); | |
143 Log.e("Foo", "Bar"); | |
144 Log.w("Foo", "Bar"); | |
145 Log.i("Foo", "Bar"); | |
146 Log.d("Foo", "Bar"); | |
147 Log.v("Foo", "Bar"); | |
148 | |
149 assertEquals(Log.ASSERT, logs.get(0).type); | |
150 assertEquals(Log.ERROR, logs.get(1).type); | |
151 assertEquals(Log.WARN, logs.get(2).type); | |
152 assertEquals(3, logs.size()); | |
153 } | |
154 | |
155 @Test | |
156 public void errorLoggingTest() { | |
157 PermissiveShadowLog.setLevel(Log.ERROR); | |
158 List<ShadowLog.LogItem> logs = ShadowLog.getLogs(); | |
159 | |
160 Log.wtf("Foo", "Bar"); | |
161 Log.e("Foo", "Bar"); | |
162 Log.w("Foo", "Bar"); | |
163 Log.i("Foo", "Bar"); | |
164 Log.d("Foo", "Bar"); | |
165 Log.v("Foo", "Bar"); | |
166 | |
167 assertEquals(Log.ASSERT, logs.get(0).type); | |
168 assertEquals(Log.ERROR, logs.get(1).type); | |
169 assertEquals(2, logs.size()); | |
170 } | |
171 | |
172 @Test | |
173 public void assertLoggingTest() { | |
174 PermissiveShadowLog.setLevel(Log.ASSERT); | |
175 List<ShadowLog.LogItem> logs = ShadowLog.getLogs(); | |
176 | |
177 Log.wtf("Foo", "Bar"); | |
178 Log.e("Foo", "Bar"); | |
179 Log.w("Foo", "Bar"); | |
180 Log.i("Foo", "Bar"); | |
181 Log.d("Foo", "Bar"); | |
182 Log.v("Foo", "Bar"); | |
183 | |
184 assertEquals(Log.ASSERT, logs.get(0).type); | |
185 assertEquals(1, logs.size()); | |
186 } | |
187 | |
188 @Before | |
189 public void beforeTest() { | |
190 PermissiveShadowLog.reset(); | |
191 } | |
192 | |
193 /** Needed to allow debug/verbose logging that is disabled by default. */ | |
194 @Implements(android.util.Log.class) | |
195 public static class PermissiveShadowLog extends ShadowLog { | |
196 private static int sLevel = Log.VERBOSE; | |
197 | |
198 /** Sets the log level for all tags. */ | |
199 public static void setLevel(int level) { | |
200 sLevel = level; | |
201 } | |
202 | |
203 @Implementation | |
204 public static boolean isLoggable(String tag, int level) { | |
205 return level >= sLevel; | |
206 } | |
207 | |
208 public static void reset() { | |
209 sLevel = Log.VERBOSE; | |
210 } | |
211 } | |
212 } | 74 } |
OLD | NEW |