1package com.xtremelabs.robolectric.shadows;
2
3import android.util.Log;
4import com.xtremelabs.robolectric.WithTestDefaultsRunner;
5import junit.framework.Assert;
6import org.junit.Test;
7import org.junit.runner.RunWith;
8
9import java.io.ByteArrayOutputStream;
10import java.io.PrintStream;
11
12import static junit.framework.Assert.assertEquals;
13import static org.hamcrest.CoreMatchers.*;
14import static org.junit.Assert.assertFalse;
15import static org.junit.Assert.assertThat;
16import static org.junit.Assert.assertTrue;
17
18@RunWith(WithTestDefaultsRunner.class)
19public class LogTest {
20    @Test
21    public void d_shouldLogAppropriately() {
22        Log.d("tag", "msg");
23
24        assertLogged(Log.DEBUG, "tag", "msg", null);
25    }
26
27    @Test
28    public void d_shouldLogAppropriately_withThrowable() {
29        Throwable throwable = new Throwable();
30
31        Log.d("tag", "msg", throwable);
32
33        assertLogged(Log.DEBUG, "tag", "msg", throwable);
34    }
35
36    @Test
37    public void e_shouldLogAppropriately() {
38        Log.e("tag", "msg");
39
40        assertLogged(Log.ERROR, "tag", "msg", null);
41    }
42
43    @Test
44    public void e_shouldLogAppropriately_withThrowable() {
45        Throwable throwable = new Throwable();
46
47        Log.e("tag", "msg", throwable);
48
49        assertLogged(Log.ERROR, "tag", "msg", throwable);
50    }
51
52    @Test
53    public void i_shouldLogAppropriately() {
54        Log.i("tag", "msg");
55
56        assertLogged(Log.INFO, "tag", "msg", null);
57    }
58
59    @Test
60    public void i_shouldLogAppropriately_withThrowable() {
61        Throwable throwable = new Throwable();
62
63        Log.i("tag", "msg", throwable);
64
65        assertLogged(Log.INFO, "tag", "msg", throwable);
66    }
67
68    @Test
69    public void v_shouldLogAppropriately() {
70        Log.v("tag", "msg");
71
72        assertLogged(Log.VERBOSE, "tag", "msg", null);
73    }
74
75    @Test
76    public void v_shouldLogAppropriately_withThrowable() {
77        Throwable throwable = new Throwable();
78
79        Log.v("tag", "msg", throwable);
80
81        assertLogged(Log.VERBOSE, "tag", "msg", throwable);
82    }
83
84    @Test
85    public void w_shouldLogAppropriately() {
86        Log.w("tag", "msg");
87
88        assertLogged(Log.WARN, "tag", "msg", null);
89    }
90
91    @Test
92    public void w_shouldLogAppropriately_withThrowable() {
93        Throwable throwable = new Throwable();
94
95        Log.w("tag", "msg", throwable);
96
97        assertLogged(Log.WARN, "tag", "msg", throwable);
98    }
99
100    @Test
101    public void w_shouldLogAppropriately_withJustThrowable() {
102        Throwable throwable = new Throwable();
103        Log.w("tag", throwable);
104        assertLogged(Log.WARN, "tag", null, throwable);
105    }
106
107    @Test
108    public void wtf_shouldLogAppropriately() {
109        Log.wtf("tag", "msg");
110
111        assertLogged(Log.ASSERT, "tag", "msg", null);
112    }
113
114    @Test
115    public void wtf_shouldLogAppropriately_withThrowable() {
116        Throwable throwable = new Throwable();
117
118        Log.wtf("tag", "msg", throwable);
119
120        assertLogged(Log.ASSERT, "tag", "msg", throwable);
121    }
122
123    @Test
124    public void shouldLogToProvidedStream() throws Exception {
125        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
126        PrintStream old = ShadowLog.stream;
127        try {
128            ShadowLog.stream = new PrintStream(bos);
129            Log.d("tag", "msg");
130            assertThat(new String(bos.toByteArray()), equalTo("D/tag: msg\n"));
131
132            Log.w("tag", new RuntimeException());
133            assertTrue(new String(bos.toByteArray()).contains("RuntimeException"));
134        } finally {
135            ShadowLog.stream = old;
136        }
137    }
138
139    @Test
140    public void infoIsDefaultLoggableLevel() throws Exception {
141        PrintStream old = ShadowLog.stream;
142        ShadowLog.stream = null;
143        assertFalse(Log.isLoggable("FOO", Log.VERBOSE));
144        assertFalse(Log.isLoggable("FOO", Log.DEBUG));
145
146        assertTrue(Log.isLoggable("FOO", Log.INFO));
147        assertTrue(Log.isLoggable("FOO", Log.WARN));
148        assertTrue(Log.isLoggable("FOO", Log.ERROR));
149        assertTrue(Log.isLoggable("FOO", Log.ASSERT));
150        ShadowLog.stream = old;
151    }
152
153    @Test
154    public void shouldAlwaysBeLoggableIfStreamIsSpecified() throws Exception {
155        PrintStream old = ShadowLog.stream;
156        ShadowLog.stream = new PrintStream(new ByteArrayOutputStream());
157        assertTrue(Log.isLoggable("FOO", Log.VERBOSE));
158        assertTrue(Log.isLoggable("FOO", Log.DEBUG));
159        assertTrue(Log.isLoggable("FOO", Log.INFO));
160        assertTrue(Log.isLoggable("FOO", Log.WARN));
161        assertTrue(Log.isLoggable("FOO", Log.ERROR));
162        assertTrue(Log.isLoggable("FOO", Log.ASSERT));
163        ShadowLog.stream = old;
164    }
165
166    private void assertLogged(int type, String tag, String msg, Throwable throwable) {
167        ShadowLog.LogItem lastLog = ShadowLog.getLogs().get(0);
168        assertEquals(type, lastLog.type);
169        assertEquals(msg, lastLog.msg);
170        assertEquals(tag, lastLog.tag);
171        assertEquals(throwable, lastLog.throwable);
172    }
173}
174