1 package org.robolectric.shadows; 2 3 import static java.nio.charset.StandardCharsets.UTF_8; 4 import static org.assertj.core.api.Assertions.assertThat; 5 import static org.junit.Assert.assertEquals; 6 import static org.junit.Assert.assertFalse; 7 import static org.junit.Assert.assertTrue; 8 9 import android.util.Log; 10 import com.google.common.collect.Iterables; 11 import java.io.ByteArrayOutputStream; 12 import java.io.PrintStream; 13 import java.util.List; 14 import org.junit.Test; 15 import org.junit.runner.RunWith; 16 import org.robolectric.RobolectricTestRunner; 17 import org.robolectric.shadows.ShadowLog.LogItem; 18 19 @RunWith(RobolectricTestRunner.class) 20 public class ShadowLogTest { 21 22 @Test d_shouldLogAppropriately()23 public void d_shouldLogAppropriately() { 24 Log.d("tag", "msg"); 25 26 assertLogged(Log.DEBUG, "tag", "msg", null); 27 } 28 29 @Test d_shouldLogAppropriately_withThrowable()30 public void d_shouldLogAppropriately_withThrowable() { 31 Throwable throwable = new Throwable(); 32 33 Log.d("tag", "msg", throwable); 34 35 assertLogged(Log.DEBUG, "tag", "msg", throwable); 36 } 37 38 @Test e_shouldLogAppropriately()39 public void e_shouldLogAppropriately() { 40 Log.e("tag", "msg"); 41 42 assertLogged(Log.ERROR, "tag", "msg", null); 43 } 44 45 @Test e_shouldLogAppropriately_withThrowable()46 public void e_shouldLogAppropriately_withThrowable() { 47 Throwable throwable = new Throwable(); 48 49 Log.e("tag", "msg", throwable); 50 51 assertLogged(Log.ERROR, "tag", "msg", throwable); 52 } 53 54 @Test i_shouldLogAppropriately()55 public void i_shouldLogAppropriately() { 56 Log.i("tag", "msg"); 57 58 assertLogged(Log.INFO, "tag", "msg", null); 59 } 60 61 @Test i_shouldLogAppropriately_withThrowable()62 public void i_shouldLogAppropriately_withThrowable() { 63 Throwable throwable = new Throwable(); 64 65 Log.i("tag", "msg", throwable); 66 67 assertLogged(Log.INFO, "tag", "msg", throwable); 68 } 69 70 @Test v_shouldLogAppropriately()71 public void v_shouldLogAppropriately() { 72 Log.v("tag", "msg"); 73 74 assertLogged(Log.VERBOSE, "tag", "msg", null); 75 } 76 77 @Test v_shouldLogAppropriately_withThrowable()78 public void v_shouldLogAppropriately_withThrowable() { 79 Throwable throwable = new Throwable(); 80 81 Log.v("tag", "msg", throwable); 82 83 assertLogged(Log.VERBOSE, "tag", "msg", throwable); 84 } 85 86 @Test w_shouldLogAppropriately()87 public void w_shouldLogAppropriately() { 88 Log.w("tag", "msg"); 89 90 assertLogged(Log.WARN, "tag", "msg", null); 91 } 92 93 @Test w_shouldLogAppropriately_withThrowable()94 public void w_shouldLogAppropriately_withThrowable() { 95 Throwable throwable = new Throwable(); 96 97 Log.w("tag", "msg", throwable); 98 99 assertLogged(Log.WARN, "tag", "msg", throwable); 100 } 101 102 @Test w_shouldLogAppropriately_withJustThrowable()103 public void w_shouldLogAppropriately_withJustThrowable() { 104 Throwable throwable = new Throwable(); 105 Log.w("tag", throwable); 106 assertLogged(Log.WARN, "tag", null, throwable); 107 } 108 109 @Test wtf_shouldLogAppropriately()110 public void wtf_shouldLogAppropriately() { 111 Log.wtf("tag", "msg"); 112 113 assertLogged(Log.ASSERT, "tag", "msg", null); 114 } 115 116 @Test wtf_shouldLogAppropriately_withThrowable()117 public void wtf_shouldLogAppropriately_withThrowable() { 118 Throwable throwable = new Throwable(); 119 120 Log.wtf("tag", "msg", throwable); 121 122 assertLogged(Log.ASSERT, "tag", "msg", throwable); 123 } 124 125 @Test println_shouldLogAppropriately()126 public void println_shouldLogAppropriately() { 127 int len = Log.println(Log.ASSERT, "tag", "msg"); 128 assertLogged(Log.ASSERT, "tag", "msg", null); 129 assertThat(len).isEqualTo(11); 130 } 131 132 @Test println_shouldLogNullTagAppropriately()133 public void println_shouldLogNullTagAppropriately() { 134 int len = Log.println(Log.ASSERT, null, "msg"); 135 assertLogged(Log.ASSERT, null, "msg", null); 136 assertThat(len).isEqualTo(8); 137 } 138 139 @Test println_shouldLogNullMessageAppropriately()140 public void println_shouldLogNullMessageAppropriately() { 141 int len = Log.println(Log.ASSERT, "tag", null); 142 assertLogged(Log.ASSERT, "tag", null, null); 143 assertThat(len).isEqualTo(8); 144 } 145 146 @Test println_shouldLogNullTagAndNullMessageAppropriately()147 public void println_shouldLogNullTagAndNullMessageAppropriately() { 148 int len = Log.println(Log.ASSERT, null, null); 149 assertLogged(Log.ASSERT, null, null, null); 150 assertThat(len).isEqualTo(5); 151 } 152 153 @Test shouldLogToProvidedStream()154 public void shouldLogToProvidedStream() throws Exception { 155 final ByteArrayOutputStream bos = new ByteArrayOutputStream(); 156 PrintStream old = ShadowLog.stream; 157 try { 158 ShadowLog.stream = new PrintStream(bos); 159 Log.d("tag", "msg"); 160 assertThat(new String(bos.toByteArray(), UTF_8)) 161 .isEqualTo("D/tag: msg" + System.getProperty("line.separator")); 162 163 Log.w("tag", new RuntimeException()); 164 assertTrue(new String(bos.toByteArray(), UTF_8).contains("RuntimeException")); 165 } finally { 166 ShadowLog.stream = old; 167 } 168 } 169 170 @Test shouldLogAccordingToTag()171 public void shouldLogAccordingToTag() throws Exception { 172 ShadowLog.reset(); 173 Log.d("tag1", "1"); 174 Log.i("tag2", "2"); 175 Log.e("tag3", "3"); 176 Log.w("tag1", "4"); 177 Log.i("tag1", "5"); 178 Log.d("tag2", "6"); 179 180 List<LogItem> allItems = ShadowLog.getLogs(); 181 assertThat(allItems.size()).isEqualTo(6); 182 int i = 1; 183 for (LogItem item : allItems) { 184 assertThat(item.msg).isEqualTo(Integer.toString(i)); 185 i++; 186 } 187 assertUniformLogsForTag("tag1", 3); 188 assertUniformLogsForTag("tag2", 2); 189 assertUniformLogsForTag("tag3", 1); 190 } 191 assertUniformLogsForTag(String tag, int count)192 private void assertUniformLogsForTag(String tag, int count) { 193 List<LogItem> tag1Items = ShadowLog.getLogsForTag(tag); 194 assertThat(tag1Items.size()).isEqualTo(count); 195 int last = -1; 196 for (LogItem item : tag1Items) { 197 assertThat(item.tag).isEqualTo(tag); 198 int current = Integer.parseInt(item.msg); 199 assertThat(current > last).isTrue(); 200 last = current; 201 } 202 } 203 204 @Test infoIsDefaultLoggableLevel()205 public void infoIsDefaultLoggableLevel() throws Exception { 206 PrintStream old = ShadowLog.stream; 207 ShadowLog.stream = null; 208 assertFalse(Log.isLoggable("FOO", Log.VERBOSE)); 209 assertFalse(Log.isLoggable("FOO", Log.DEBUG)); 210 211 assertTrue(Log.isLoggable("FOO", Log.INFO)); 212 assertTrue(Log.isLoggable("FOO", Log.WARN)); 213 assertTrue(Log.isLoggable("FOO", Log.ERROR)); 214 assertTrue(Log.isLoggable("FOO", Log.ASSERT)); 215 ShadowLog.stream = old; 216 } 217 218 @Test shouldAlwaysBeLoggableIfStreamIsSpecified()219 public void shouldAlwaysBeLoggableIfStreamIsSpecified() throws Exception { 220 PrintStream old = ShadowLog.stream; 221 ShadowLog.stream = new PrintStream(new ByteArrayOutputStream()); 222 assertTrue(Log.isLoggable("FOO", Log.VERBOSE)); 223 assertTrue(Log.isLoggable("FOO", Log.DEBUG)); 224 assertTrue(Log.isLoggable("FOO", Log.INFO)); 225 assertTrue(Log.isLoggable("FOO", Log.WARN)); 226 assertTrue(Log.isLoggable("FOO", Log.ERROR)); 227 assertTrue(Log.isLoggable("FOO", Log.ASSERT)); 228 ShadowLog.stream = old; 229 } 230 assertLogged(int type, String tag, String msg, Throwable throwable)231 private void assertLogged(int type, String tag, String msg, Throwable throwable) { 232 LogItem lastLog = Iterables.getLast(ShadowLog.getLogs()); 233 assertEquals(type, lastLog.type); 234 assertEquals(msg, lastLog.msg); 235 assertEquals(tag, lastLog.tag); 236 assertEquals(throwable, lastLog.throwable); 237 } 238 239 @Test identicalLogItemInstancesAreEqual()240 public void identicalLogItemInstancesAreEqual() { 241 LogItem item1 = new LogItem(Log.VERBOSE, "Foo", "Bar", null); 242 LogItem item2 = new LogItem(Log.VERBOSE, "Foo", "Bar", null); 243 assertThat(item1).isEqualTo(item2); 244 assertThat(item2).isEqualTo(item1); 245 } 246 247 @Test logsAfterSetLoggable()248 public void logsAfterSetLoggable() { 249 ShadowLog.setLoggable("Foo", Log.VERBOSE); 250 assertTrue(Log.isLoggable("Foo", Log.DEBUG)); 251 } 252 253 @Test noLogAfterSetLoggable()254 public void noLogAfterSetLoggable() { 255 PrintStream old = ShadowLog.stream; 256 ShadowLog.stream = new PrintStream(new ByteArrayOutputStream()); 257 ShadowLog.setLoggable("Foo", Log.DEBUG); 258 assertFalse(Log.isLoggable("Foo", Log.VERBOSE)); 259 ShadowLog.stream = old; 260 } 261 262 @Test getLogs_shouldReturnCopy()263 public void getLogs_shouldReturnCopy() { 264 assertThat(ShadowLog.getLogs()).isNotSameAs(ShadowLog.getLogs()); 265 assertThat(ShadowLog.getLogs()).isEqualTo(ShadowLog.getLogs()); 266 } 267 268 @Test getLogsForTag_empty()269 public void getLogsForTag_empty() { 270 assertThat(ShadowLog.getLogsForTag("non_existent")).isEmpty(); 271 } 272 273 @Test clear()274 public void clear() { 275 assertThat(ShadowLog.getLogsForTag("tag1")).isEmpty(); 276 Log.d("tag1", "1"); 277 assertThat(ShadowLog.getLogsForTag("tag1")).isNotEmpty(); 278 ShadowLog.clear(); 279 assertThat(ShadowLog.getLogsForTag("tag1")).isEmpty(); 280 assertThat(ShadowLog.getLogs()).isEmpty(); 281 } 282 }