1 /* 2 * Copyright (C) 2021 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 17 package com.android.server.utils; 18 19 import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession; 20 import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify; 21 22 import android.util.Log; 23 import android.util.Slog; 24 25 import org.junit.After; 26 import org.junit.Before; 27 import org.junit.Test; 28 import org.mockito.MockitoSession; 29 import org.mockito.quality.Strictness; 30 31 /** 32 * Run it as {@code atest FrameworksMockingServicesTests:SlogfTest} 33 */ 34 public final class SlogfTest { 35 36 private static final String TAG = SlogfTest.class.getSimpleName(); 37 38 private MockitoSession mSession; 39 40 private final Throwable mThrowable = new Throwable("D'OH!"); 41 42 @Before setup()43 public void setup() { 44 mSession = mockitoSession() 45 .initMocks(this) 46 .mockStatic(Slog.class) 47 .strictness(Strictness.LENIENT) 48 .startMocking(); 49 } 50 51 @After tearDown()52 public void tearDown() { 53 if (mSession == null) { 54 Log.w(TAG, "finishSession(): no session"); 55 } else { 56 mSession.finishMocking(); 57 } 58 } 59 60 @Test testV_msg()61 public void testV_msg() { 62 Slogf.v(TAG, "msg"); 63 64 verify(()-> Slog.v(TAG, "msg")); 65 } 66 67 @Test testV_msgAndThrowable()68 public void testV_msgAndThrowable() { 69 Slogf.v(TAG, "msg", mThrowable); 70 71 verify(()-> Slog.v(TAG, "msg", mThrowable)); 72 } 73 74 @Test testV_msgFormatted()75 public void testV_msgFormatted() { 76 Slogf.v(TAG, "msg in a %s", "bottle"); 77 78 verify(()-> Slog.v(TAG, "msg in a bottle")); 79 } 80 81 @Test testV_msgFormattedWithThrowable()82 public void testV_msgFormattedWithThrowable() { 83 Slogf.v(TAG, mThrowable, "msg in a %s", "bottle"); 84 85 verify(()-> Slog.v(TAG, "msg in a bottle", mThrowable)); 86 } 87 88 @Test testD_msg()89 public void testD_msg() { 90 Slogf.d(TAG, "msg"); 91 92 verify(()-> Slog.d(TAG, "msg")); 93 } 94 95 @Test testD_msgAndThrowable()96 public void testD_msgAndThrowable() { 97 Slogf.d(TAG, "msg", mThrowable); 98 99 verify(()-> Slog.d(TAG, "msg", mThrowable)); 100 } 101 102 @Test testD_msgFormatted()103 public void testD_msgFormatted() { 104 Slogf.d(TAG, "msg in a %s", "bottle"); 105 106 verify(()-> Slog.d(TAG, "msg in a bottle")); 107 } 108 109 @Test testD_msgFormattedWithThrowable()110 public void testD_msgFormattedWithThrowable() { 111 Slogf.d(TAG, mThrowable, "msg in a %s", "bottle"); 112 113 verify(()-> Slog.d(TAG, "msg in a bottle", mThrowable)); 114 } 115 116 @Test testI_msg()117 public void testI_msg() { 118 Slogf.i(TAG, "msg"); 119 120 verify(()-> Slog.i(TAG, "msg")); 121 } 122 123 @Test testI_msgAndThrowable()124 public void testI_msgAndThrowable() { 125 Slogf.i(TAG, "msg", mThrowable); 126 127 verify(()-> Slog.i(TAG, "msg", mThrowable)); 128 } 129 130 @Test testI_msgFormatted()131 public void testI_msgFormatted() { 132 Slogf.i(TAG, "msg in a %s", "bottle"); 133 134 verify(()-> Slog.i(TAG, "msg in a bottle")); 135 } 136 137 @Test testI_msgFormattedWithThrowable()138 public void testI_msgFormattedWithThrowable() { 139 Slogf.i(TAG, mThrowable, "msg in a %s", "bottle"); 140 141 verify(()-> Slog.i(TAG, "msg in a bottle", mThrowable)); 142 } 143 144 @Test testW_msg()145 public void testW_msg() { 146 Slogf.w(TAG, "msg"); 147 148 verify(()-> Slog.w(TAG, "msg")); 149 } 150 151 @Test testW_msgAndThrowable()152 public void testW_msgAndThrowable() { 153 Slogf.w(TAG, "msg", mThrowable); 154 155 verify(()-> Slog.w(TAG, "msg", mThrowable)); 156 } 157 158 @Test testW_Throwable()159 public void testW_Throwable() { 160 Slogf.w(TAG, mThrowable); 161 162 verify(()-> Slog.w(TAG, mThrowable)); 163 } 164 165 @Test testW_msgFormatted()166 public void testW_msgFormatted() { 167 Slogf.w(TAG, "msg in a %s", "bottle"); 168 169 verify(()-> Slog.w(TAG, "msg in a bottle")); 170 } 171 172 @Test testW_msgFormattedWithThrowable()173 public void testW_msgFormattedWithThrowable() { 174 Slogf.w(TAG, mThrowable, "msg in a %s", "bottle"); 175 176 verify(()-> Slog.w(TAG, "msg in a bottle", mThrowable)); 177 } 178 179 @Test testE_msg()180 public void testE_msg() { 181 Slogf.e(TAG, "msg"); 182 183 verify(()-> Slog.e(TAG, "msg")); 184 } 185 186 @Test testE_msgAndThrowable()187 public void testE_msgAndThrowable() { 188 Slogf.e(TAG, "msg", mThrowable); 189 190 verify(()-> Slog.e(TAG, "msg", mThrowable)); 191 } 192 193 @Test testE_msgFormatted()194 public void testE_msgFormatted() { 195 Slogf.e(TAG, "msg in a %s", "bottle"); 196 197 verify(()-> Slog.e(TAG, "msg in a bottle")); 198 } 199 200 @Test testE_msgFormattedWithThrowable()201 public void testE_msgFormattedWithThrowable() { 202 Slogf.e(TAG, mThrowable, "msg in a %s", "bottle"); 203 204 verify(()-> Slog.e(TAG, "msg in a bottle", mThrowable)); 205 } 206 207 @Test testWtf_msg()208 public void testWtf_msg() { 209 Slogf.wtf(TAG, "msg"); 210 211 verify(()-> Slog.wtf(TAG, "msg")); 212 } 213 214 @Test testWtf_msgAndThrowable()215 public void testWtf_msgAndThrowable() { 216 Slogf.wtf(TAG, "msg", mThrowable); 217 218 verify(()-> Slog.wtf(TAG, "msg", mThrowable)); 219 } 220 221 @Test testWtf_Throwable()222 public void testWtf_Throwable() { 223 Slogf.wtf(TAG, mThrowable); 224 225 verify(()-> Slog.wtf(TAG, mThrowable)); 226 } 227 228 @Test testWtf_msgFormatted()229 public void testWtf_msgFormatted() { 230 Slogf.wtf(TAG, "msg in a %s", "bottle"); 231 232 verify(()-> Slog.wtf(TAG, "msg in a bottle")); 233 } 234 235 @Test testWtfQuiet()236 public void testWtfQuiet() { 237 Slogf.wtfQuiet(TAG, "msg"); 238 239 verify(()-> Slog.wtfQuiet(TAG, "msg")); 240 } 241 242 @Test testWtfStack()243 public void testWtfStack() { 244 Slogf.wtfStack(TAG, "msg"); 245 246 verify(()-> Slog.wtfStack(TAG, "msg")); 247 } 248 249 @Test testPrintln()250 public void testPrintln() { 251 Slogf.println(42, TAG, "msg"); 252 253 verify(()-> Slog.println(42, TAG, "msg")); 254 } 255 256 @Test testWtf_msgFormattedWithThrowable()257 public void testWtf_msgFormattedWithThrowable() { 258 Slogf.wtf(TAG, mThrowable, "msg in a %s", "bottle"); 259 260 verify(()-> Slog.wtf(TAG, "msg in a bottle", mThrowable)); 261 } 262 } 263