summaryrefslogtreecommitdiffstats
path: root/chromium/base/android/junit/src/org/chromium/base/LogTest.java
blob: ee686635fe4db93a6c9c700e1f1e50a35b61e13b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.base;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowLog;

import org.chromium.base.test.BaseRobolectricTestRunner;

import java.util.List;

/** Unit tests for {@link Log}. */
@RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class LogTest {
    /** Tests that the computed call origin is the correct one. */
    @Test
    public void callOriginTest() {
        Log.d("Foo", "Bar");

        List<ShadowLog.LogItem> logs = ShadowLog.getLogs();

        assertTrue("The origin of the log message (" + logs.get(logs.size() - 1).msg
                        + ") looks wrong.",
                logs.get(logs.size() - 1).msg.matches("\\[LogTest.java:\\d+\\].*"));
    }

    @Test
    public void normalizeTagTest() {
        assertEquals("cr_foo", Log.normalizeTag("foo"));
    }

    /** Tests that exceptions provided to the log functions are properly recognized and printed. */
    @Test
    public void exceptionLoggingTest() {
        Throwable t = new Throwable() {
            @Override
            public String toString() {
                return "MyThrowable";
            }
        };

        Throwable t2 = new Throwable() {
            @Override
            public String toString() {
                return "MyOtherThrowable";
            }
        };

        List<ShadowLog.LogItem> logs;

        // The throwable gets printed out
        Log.i("Foo", "Bar", t);
        logs = ShadowLog.getLogs();
        assertEquals(t, logs.get(logs.size() - 1).throwable);
        assertEquals("Bar", logs.get(logs.size() - 1).msg);

        // messageTemplate include %xx, print out normally.
        Log.i("Foo", "search?q=%E6%B5%8B%E8%AF%95", t);
        logs = ShadowLog.getLogs();
        assertEquals(t, logs.get(logs.size() - 1).throwable);
        assertEquals("search?q=%E6%B5%8B%E8%AF%95", logs.get(logs.size() - 1).msg);

        // Non throwable are properly identified
        Log.i("Foo", "Bar %s", t, "Baz");
        logs = ShadowLog.getLogs();
        assertNull(logs.get(logs.size() - 1).throwable);
        assertEquals("Bar MyThrowable", logs.get(logs.size() - 1).msg);

        // The last throwable is the one used that is going to be printed out
        Log.i("Foo", "Bar %s", t, t2);
        logs = ShadowLog.getLogs();
        assertEquals(t2, logs.get(logs.size() - 1).throwable);
        assertEquals("Bar MyThrowable", logs.get(logs.size() - 1).msg);
    }
}