• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 package org.chromium.base;
6 
7 import static org.junit.Assert.assertEquals;
8 import static org.junit.Assert.assertNull;
9 import static org.junit.Assert.assertTrue;
10 
11 import org.chromium.testing.local.LocalRobolectricTestRunner;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 import org.robolectric.annotation.Config;
15 import org.robolectric.shadows.ShadowLog;
16 
17 import java.util.List;
18 
19 /** Unit tests for {@link Log}. */
20 @RunWith(LocalRobolectricTestRunner.class)
21 @Config(manifest = Config.NONE)
22 public class LogTest {
23     /** Tests that the computed call origin is the correct one. */
24     @Test
callOriginTest()25     public void callOriginTest() {
26         Log.d("Foo", "Bar");
27 
28         List<ShadowLog.LogItem> logs = ShadowLog.getLogs();
29         assertEquals("Only one log should be written", 1, logs.size());
30 
31         assertTrue("The origin of the log message (" + logs.get(0).msg + ") looks wrong.",
32                 logs.get(0).msg.matches("\\[LogTest.java:\\d+\\].*"));
33     }
34 
35     @Test
normalizeTagTest()36     public void normalizeTagTest() {
37         assertEquals("cr_foo", Log.normalizeTag("cr.foo"));
38         assertEquals("cr_foo", Log.normalizeTag("cr_foo"));
39         assertEquals("cr_foo", Log.normalizeTag("foo"));
40         assertEquals("cr_ab_foo", Log.normalizeTag("ab_foo"));
41     }
42 
43     /** Tests that exceptions provided to the log functions are properly recognized and printed. */
44     @Test
exceptionLoggingTest()45     public void exceptionLoggingTest() {
46         Throwable t = new Throwable() {
47             @Override
48             public String toString() {
49                 return "MyThrowable";
50             }
51         };
52 
53         Throwable t2 = new Throwable() {
54             @Override
55             public String toString() {
56                 return "MyOtherThrowable";
57             }
58         };
59 
60         List<ShadowLog.LogItem> logs = ShadowLog.getLogs();
61 
62         // The throwable gets printed out
63         Log.i("Foo", "Bar", t);
64         assertEquals(t, logs.get(logs.size() - 1).throwable);
65         assertEquals("Bar", logs.get(logs.size() - 1).msg);
66 
67         // The throwable can be both added to the message itself and printed out
68         Log.i("Foo", "Bar %s", t);
69         assertEquals(t, logs.get(logs.size() - 1).throwable);
70         assertEquals("Bar MyThrowable", logs.get(logs.size() - 1).msg);
71 
72         // Non throwable are properly identified
73         Log.i("Foo", "Bar %s", t, "Baz");
74         assertNull(logs.get(logs.size() - 1).throwable);
75         assertEquals("Bar MyThrowable", logs.get(logs.size() - 1).msg);
76 
77         // The last throwable is the one used that is going to be printed out
78         Log.i("Foo", "Bar %s %s", t, t2);
79         assertEquals(t2, logs.get(logs.size() - 1).throwable);
80         assertEquals("Bar MyThrowable MyOtherThrowable", logs.get(logs.size() - 1).msg);
81     }
82 }
83