• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.ravenwoodtest.coretest;
17 
18 import static org.junit.Assert.assertEquals;
19 
20 import android.platform.test.ravenwood.RavenwoodRule;
21 import android.util.Log;
22 
23 import com.android.ravenwood.common.RavenwoodCommonUtils;
24 
25 import org.junit.Test;
26 
27 public class RavenwoodLogLevelTest {
28     /**
29      * Assert that the `priority` is loggable, but one level below is not.
30      */
assertBarelyLoggable(String tag, int priority)31     private void assertBarelyLoggable(String tag, int priority) {
32         assertEquals(true, Log.isLoggable(tag, priority));
33         assertEquals(false, Log.isLoggable(tag, priority - 1));
34     }
35 
36     @Test
testDefaultLogTags()37     public void testDefaultLogTags() {
38         RavenwoodRule.setAndroidLogTags(null);
39 
40         // Info should always be loggable.
41         assertEquals(true, Log.isLoggable("TAG1", Log.INFO));
42         assertEquals(true, Log.isLoggable("TAG2", Log.INFO));
43 
44         assertEquals(true, Log.isLoggable("TAG1", Log.DEBUG));
45         assertEquals(true, Log.isLoggable("TAG2", Log.VERBOSE));
46     }
47 
48     @Test
testAllVerbose()49     public void testAllVerbose() {
50         RavenwoodRule.setAndroidLogTags("*:V");
51 
52         assertEquals(true, Log.isLoggable("TAG1", Log.INFO));
53         assertEquals(true, Log.isLoggable("TAG2", Log.INFO));
54 
55         assertEquals(true, Log.isLoggable("TAG1", Log.DEBUG));
56         assertEquals(true, Log.isLoggable("TAG2", Log.VERBOSE));
57     }
58 
59     @Test
testAllSilent()60     public void testAllSilent() {
61         RavenwoodRule.setAndroidLogTags("*:S");
62 
63         assertEquals(false, Log.isLoggable("TAG1", Log.ASSERT));
64         assertEquals(false, Log.isLoggable("TAG2", Log.ASSERT));
65     }
66 
67     @Test
testComplex()68     public void testComplex() {
69         RavenwoodRule.setAndroidLogTags("TAG1:W TAG2:D *:I");
70 
71         assertBarelyLoggable("TAG1", Log.WARN);
72         assertBarelyLoggable("TAG2", Log.DEBUG);
73         assertBarelyLoggable("TAG3", Log.INFO);
74     }
75 
76     @Test
testAllVerbose_setLogLevel()77     public void testAllVerbose_setLogLevel() {
78         RavenwoodRule.setAndroidLogTags(null);
79         RavenwoodRule.setLogLevel(null, Log.VERBOSE);
80 
81         assertEquals(true, Log.isLoggable("TAG1", Log.INFO));
82         assertEquals(true, Log.isLoggable("TAG2", Log.INFO));
83 
84         assertEquals(true, Log.isLoggable("TAG1", Log.DEBUG));
85         assertEquals(true, Log.isLoggable("TAG2", Log.VERBOSE));
86     }
87 
88     @Test
testAllSilent_setLogLevel()89     public void testAllSilent_setLogLevel() {
90         RavenwoodRule.setAndroidLogTags(null);
91         RavenwoodRule.setLogLevel(null, Log.ASSERT + 1);
92 
93         assertEquals(false, Log.isLoggable("TAG1", Log.ASSERT));
94         assertEquals(false, Log.isLoggable("TAG2", Log.ASSERT));
95     }
96 
97     @Test
testComplex_setLogLevel()98     public void testComplex_setLogLevel() {
99         RavenwoodRule.setAndroidLogTags(null);
100         RavenwoodRule.setLogLevel(null, Log.INFO);
101         RavenwoodRule.setLogLevel("TAG1", Log.WARN);
102         RavenwoodRule.setLogLevel("TAG2", Log.DEBUG);
103 
104         assertBarelyLoggable("TAG1", Log.WARN);
105         assertBarelyLoggable("TAG2", Log.DEBUG);
106         assertBarelyLoggable("TAG3", Log.INFO);
107     }
108 }
109