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.doReturn; 20 import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession; 21 import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify; 22 23 import static com.google.common.truth.Truth.assertThat; 24 25 import static org.mockito.ArgumentMatchers.any; 26 import static org.mockito.ArgumentMatchers.eq; 27 import static org.mockito.Mockito.never; 28 29 import android.util.Log; 30 import android.util.Slog; 31 32 import org.junit.After; 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.mockito.MockitoSession; 36 import org.mockito.quality.Strictness; 37 38 /** 39 * Run it as {@code atest FrameworksMockingServicesTests:SlogfTest} 40 */ 41 public final class SlogfTest { 42 43 private static final String TAG = SlogfTest.class.getSimpleName(); 44 45 private MockitoSession mSession; 46 47 private final Exception mException = new Exception("D'OH!"); 48 49 @Before setup()50 public void setup() { 51 mSession = mockitoSession() 52 .initMocks(this) 53 .mockStatic(Slog.class) 54 .spyStatic(Slogf.class) // for isLoggable only 55 .strictness(Strictness.LENIENT) 56 .startMocking(); 57 } 58 59 @After tearDown()60 public void tearDown() { 61 if (mSession == null) { 62 Log.w(TAG, "finishSession(): no session"); 63 } else { 64 mSession.finishMocking(); 65 } 66 } 67 68 @Test testIsLoggable()69 public void testIsLoggable() { 70 assertThat(Slogf.isLoggable(TAG, Log.VERBOSE)).isEqualTo(Log.isLoggable(TAG, Log.VERBOSE)); 71 } 72 73 @Test testV_msg()74 public void testV_msg() { 75 Slogf.v(TAG, "msg"); 76 77 verify(()-> Slog.v(TAG, "msg")); 78 } 79 80 @Test testV_msgAndException()81 public void testV_msgAndException() { 82 Slogf.v(TAG, "msg", mException); 83 84 verify(()-> Slog.v(TAG, "msg", mException)); 85 } 86 87 @Test testV_msgFormatted_enabled()88 public void testV_msgFormatted_enabled() { 89 enableLogging(Log.VERBOSE); 90 91 Slogf.v(TAG, "msg in a %s", "bottle"); 92 93 verify(()-> Slog.v(TAG, "msg in a bottle")); 94 } 95 96 @Test testV_msgFormatted_disabled()97 public void testV_msgFormatted_disabled() { 98 disableLogging(Log.VERBOSE); 99 100 Slogf.v(TAG, "msg in a %s", "bottle"); 101 102 verify(()-> Slog.v(eq(TAG), any()), never()); 103 } 104 105 @Test testD_msg()106 public void testD_msg() { 107 Slogf.d(TAG, "msg"); 108 109 verify(()-> Slog.d(TAG, "msg")); 110 } 111 112 @Test testD_msgAndException()113 public void testD_msgAndException() { 114 Slogf.d(TAG, "msg", mException); 115 116 verify(()-> Slog.d(TAG, "msg", mException)); 117 } 118 119 @Test testD_msgFormatted_enabled()120 public void testD_msgFormatted_enabled() { 121 enableLogging(Log.DEBUG); 122 123 Slogf.d(TAG, "msg in a %s", "bottle"); 124 125 verify(()-> Slog.d(TAG, "msg in a bottle")); 126 } 127 128 @Test testD_msgFormatted_disabled()129 public void testD_msgFormatted_disabled() { 130 disableLogging(Log.DEBUG); 131 132 Slogf.d(TAG, "msg in a %s", "bottle"); 133 134 verify(()-> Slog.d(eq(TAG), any()), never()); 135 } 136 137 @Test testI_msg()138 public void testI_msg() { 139 Slogf.i(TAG, "msg"); 140 141 verify(()-> Slog.i(TAG, "msg")); 142 } 143 144 @Test testI_msgAndException()145 public void testI_msgAndException() { 146 Slogf.i(TAG, "msg", mException); 147 148 verify(()-> Slog.i(TAG, "msg", mException)); 149 } 150 151 @Test testI_msgFormatted_enabled()152 public void testI_msgFormatted_enabled() { 153 enableLogging(Log.INFO); 154 155 Slogf.i(TAG, "msg in a %s", "bottle"); 156 157 verify(()-> Slog.i(TAG, "msg in a bottle")); 158 } 159 160 @Test testI_msgFormatted_disabled()161 public void testI_msgFormatted_disabled() { 162 disableLogging(Log.INFO); 163 164 Slogf.i(TAG, "msg in a %s", "bottle"); 165 166 verify(()-> Slog.i(eq(TAG), any()), never()); 167 } 168 169 @Test testW_msg()170 public void testW_msg() { 171 Slogf.w(TAG, "msg"); 172 173 verify(()-> Slog.w(TAG, "msg")); 174 } 175 176 @Test testW_msgAndException()177 public void testW_msgAndException() { 178 Slogf.w(TAG, "msg", mException); 179 180 verify(()-> Slog.w(TAG, "msg", mException)); 181 } 182 183 @Test testW_exception()184 public void testW_exception() { 185 Slogf.w(TAG, mException); 186 187 verify(()-> Slog.w(TAG, mException)); 188 } 189 190 @Test testW_msgFormatted_enabled()191 public void testW_msgFormatted_enabled() { 192 enableLogging(Log.WARN); 193 194 Slogf.w(TAG, "msg in a %s", "bottle"); 195 196 verify(()-> Slog.w(TAG, "msg in a bottle")); 197 } 198 199 @Test testW_msgFormatted_disabled()200 public void testW_msgFormatted_disabled() { 201 disableLogging(Log.WARN); 202 203 Slogf.w(TAG, "msg in a %s", "bottle"); 204 205 verify(()-> Slog.w(eq(TAG), any(String.class)), never()); 206 } 207 208 @Test testW_msgFormattedWithException_enabled()209 public void testW_msgFormattedWithException_enabled() { 210 enableLogging(Log.WARN); 211 212 Slogf.w(TAG, mException, "msg in a %s", "bottle"); 213 214 verify(()-> Slog.w(TAG, "msg in a bottle", mException)); 215 } 216 217 @Test testW_msgFormattedWithException_disabled()218 public void testW_msgFormattedWithException_disabled() { 219 disableLogging(Log.WARN); 220 221 Slogf.w(TAG, "msg in a %s", "bottle"); 222 223 verify(()-> Slog.w(eq(TAG), any(String.class), any(Throwable.class)), never()); 224 } 225 226 @Test testE_msg()227 public void testE_msg() { 228 Slogf.e(TAG, "msg"); 229 230 verify(()-> Slog.e(TAG, "msg")); 231 } 232 233 @Test testE_msgAndException()234 public void testE_msgAndException() { 235 Slogf.e(TAG, "msg", mException); 236 237 verify(()-> Slog.e(TAG, "msg", mException)); 238 } 239 240 @Test testE_msgFormatted_enabled()241 public void testE_msgFormatted_enabled() { 242 enableLogging(Log.ERROR); 243 244 Slogf.e(TAG, "msg in a %s", "bottle"); 245 246 verify(()-> Slog.e(TAG, "msg in a bottle")); 247 } 248 249 @Test testE_msgFormatted_disabled()250 public void testE_msgFormatted_disabled() { 251 disableLogging(Log.ERROR); 252 253 Slogf.e(TAG, "msg in a %s", "bottle"); 254 255 verify(()-> Slog.e(eq(TAG), any()), never()); 256 } 257 258 @Test testE_msgFormattedWithException_enabled()259 public void testE_msgFormattedWithException_enabled() { 260 enableLogging(Log.ERROR); 261 262 Slogf.e(TAG, mException, "msg in a %s", "bottle"); 263 264 verify(()-> Slog.e(TAG, "msg in a bottle", mException)); 265 } 266 267 @Test testE_msgFormattedWithException_disabled()268 public void testE_msgFormattedWithException_disabled() { 269 disableLogging(Log.ERROR); 270 271 Slogf.e(TAG, "msg in a %s", "bottle"); 272 273 verify(()-> Slog.e(eq(TAG), any(String.class), any(Throwable.class)), never()); 274 } 275 276 @Test testWtf_msg()277 public void testWtf_msg() { 278 Slogf.wtf(TAG, "msg"); 279 280 verify(()-> Slog.wtf(TAG, "msg")); 281 } 282 283 @Test testWtf_msgAndException()284 public void testWtf_msgAndException() { 285 Slogf.wtf(TAG, "msg", mException); 286 287 verify(()-> Slog.wtf(TAG, "msg", mException)); 288 } 289 290 @Test testWtf_exception()291 public void testWtf_exception() { 292 Slogf.wtf(TAG, mException); 293 294 verify(()-> Slog.wtf(TAG, mException)); 295 } 296 297 @Test testWtf_msgFormatted()298 public void testWtf_msgFormatted() { 299 Slogf.wtf(TAG, "msg in a %s", "bottle"); 300 301 verify(()-> Slog.wtf(TAG, "msg in a bottle")); 302 } 303 304 @Test testWtfQuiet()305 public void testWtfQuiet() { 306 Slogf.wtfQuiet(TAG, "msg"); 307 308 verify(()-> Slog.wtfQuiet(TAG, "msg")); 309 } 310 311 @Test testWtfStack()312 public void testWtfStack() { 313 Slogf.wtfStack(TAG, "msg"); 314 315 verify(()-> Slog.wtfStack(TAG, "msg")); 316 } 317 318 @Test testPrintln()319 public void testPrintln() { 320 Slogf.println(42, TAG, "msg"); 321 322 verify(()-> Slog.println(42, TAG, "msg")); 323 } 324 325 @Test testWtf_msgFormattedWithException()326 public void testWtf_msgFormattedWithException() { 327 Slogf.wtf(TAG, mException, "msg in a %s", "bottle"); 328 329 verify(()-> Slog.wtf(TAG, "msg in a bottle", mException)); 330 } 331 enableLogging(@og.Level int level)332 private void enableLogging(@Log.Level int level) { 333 setIsLogging(level, true); 334 } 335 disableLogging(@og.Level int level)336 private void disableLogging(@Log.Level int level) { 337 setIsLogging(level, false); 338 } 339 setIsLogging(@og.Level int level, boolean value)340 private void setIsLogging(@Log.Level int level, boolean value) { 341 doReturn(value).when(() -> Slogf.isLoggable(TAG, level)); 342 } 343 } 344