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; |
12 import org.junit.Test; | 13 import org.junit.Test; |
13 import org.junit.runner.RunWith; | 14 import org.junit.runner.RunWith; |
14 import org.robolectric.annotation.Config; | 15 import org.robolectric.annotation.Config; |
15 import org.robolectric.annotation.Implementation; | 16 import org.robolectric.annotation.Implementation; |
16 import org.robolectric.annotation.Implements; | 17 import org.robolectric.annotation.Implements; |
17 import org.robolectric.shadows.ShadowLog; | 18 import org.robolectric.shadows.ShadowLog; |
18 | 19 |
19 import java.util.List; | 20 import java.util.List; |
20 | 21 |
21 /** Unit tests for {@link Log}. */ | 22 /** Unit tests for {@link Log}. */ |
22 @RunWith(LocalRobolectricTestRunner.class) | 23 @RunWith(LocalRobolectricTestRunner.class) |
23 @Config(manifest = Config.NONE, shadows = {LogTest.PermissiveShadowLog.class}) | 24 @Config(manifest = Config.NONE, shadows = {LogTest.PermissiveShadowLog.class}) |
24 public class LogTest { | 25 public class LogTest { |
25 /** Test method for {@link Log#makeTag(String)} */ | |
26 @Test | |
27 public void testMakeTag() { | |
28 assertEquals("cr.Foo", Log.makeTag("Foo")); | |
29 assertEquals("cr", Log.makeTag(null)); | |
30 assertEquals("cr", Log.makeTag("")); | |
31 } | |
32 | |
33 /** Test method for {@link Log#makeTag(String)} */ | |
34 @Test(expected = IllegalArgumentException.class) | |
35 public void testMakeTagFailure() { | |
36 Log.makeTag("ThisIs21Char.....Long"); | |
37 } | |
38 | |
39 /** Tests that the computed call origin is the correct one. */ | 26 /** Tests that the computed call origin is the correct one. */ |
40 @Test | 27 @Test |
41 public void callOriginTest() { | 28 public void callOriginTest() { |
42 Log.d("Foo", "Bar"); | 29 Log.d("Foo", "Bar"); |
43 | 30 |
44 List<ShadowLog.LogItem> logs = ShadowLog.getLogs(); | 31 List<ShadowLog.LogItem> logs = ShadowLog.getLogs(); |
45 assertEquals("Only one log should be written", 1, logs.size()); | 32 assertEquals("Only one log should be written", 1, logs.size()); |
46 | 33 |
47 assertTrue("The origin of the log message (" + logs.get(0).msg + ") look
s wrong.", | 34 assertTrue("The origin of the log message (" + logs.get(0).msg + ") look
s wrong.", |
48 logs.get(0).msg.matches("\\[LogTest.java:\\d+\\].*")); | 35 logs.get(0).msg.matches("\\[LogTest.java:\\d+\\].*")); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 Log.i("Foo", "Bar %s", t, "Baz"); | 68 Log.i("Foo", "Bar %s", t, "Baz"); |
82 assertNull(logs.get(logs.size() - 1).throwable); | 69 assertNull(logs.get(logs.size() - 1).throwable); |
83 assertEquals("Bar MyThrowable", logs.get(logs.size() - 1).msg); | 70 assertEquals("Bar MyThrowable", logs.get(logs.size() - 1).msg); |
84 | 71 |
85 // The last throwable is the one used that is going to be printed out | 72 // The last throwable is the one used that is going to be printed out |
86 Log.i("Foo", "Bar %s %s", t, t2); | 73 Log.i("Foo", "Bar %s %s", t, t2); |
87 assertEquals(t2, logs.get(logs.size() - 1).throwable); | 74 assertEquals(t2, logs.get(logs.size() - 1).throwable); |
88 assertEquals("Bar MyThrowable MyOtherThrowable", logs.get(logs.size() -
1).msg); | 75 assertEquals("Bar MyThrowable MyOtherThrowable", logs.get(logs.size() -
1).msg); |
89 } | 76 } |
90 | 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 |
91 /** Needed to allow debug/verbose logging that is disabled by default. */ | 193 /** Needed to allow debug/verbose logging that is disabled by default. */ |
92 @Implements(android.util.Log.class) | 194 @Implements(android.util.Log.class) |
93 public static class PermissiveShadowLog extends ShadowLog { | 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 |
94 @Implementation | 203 @Implementation |
95 public static boolean isLoggable(String tag, int level) { | 204 public static boolean isLoggable(String tag, int level) { |
96 return true; | 205 return level >= sLevel; |
| 206 } |
| 207 |
| 208 public static void reset() { |
| 209 sLevel = Log.VERBOSE; |
97 } | 210 } |
98 } | 211 } |
99 } | 212 } |
OLD | NEW |