1 /* 2 * Copyright 2018 The WebRTC Project Authors. All rights reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 package org.webrtc; 12 13 import static org.junit.Assert.assertFalse; 14 import static org.junit.Assert.assertTrue; 15 16 import android.support.test.InstrumentationRegistry; 17 import androidx.test.filters.SmallTest; 18 import java.util.ArrayList; 19 import org.junit.Test; 20 import org.webrtc.Loggable; 21 import org.webrtc.Logging.Severity; 22 import org.webrtc.PeerConnectionFactory; 23 24 public class LoggableTest { 25 private static String TAG = "LoggableTest"; 26 private static String NATIVE_FILENAME_TAG = "loggable_test.cc"; 27 28 private static class MockLoggable implements Loggable { 29 private ArrayList<String> messages = new ArrayList<>(); 30 private ArrayList<Severity> sevs = new ArrayList<>(); 31 private ArrayList<String> tags = new ArrayList<>(); 32 33 @Override onLogMessage(String message, Severity sev, String tag)34 public void onLogMessage(String message, Severity sev, String tag) { 35 messages.add(message); 36 sevs.add(sev); 37 tags.add(tag); 38 } 39 isMessageReceived(String message)40 public boolean isMessageReceived(String message) { 41 for (int i = 0; i < messages.size(); i++) { 42 if (messages.get(i).contains(message)) { 43 return true; 44 } 45 } 46 return false; 47 } 48 isMessageReceived(String message, Severity sev, String tag)49 public boolean isMessageReceived(String message, Severity sev, String tag) { 50 for (int i = 0; i < messages.size(); i++) { 51 if (messages.get(i).contains(message) && sevs.get(i) == sev && tags.get(i).equals(tag)) { 52 return true; 53 } 54 } 55 return false; 56 } 57 } 58 59 private final MockLoggable mockLoggable = new MockLoggable(); 60 61 @Test 62 @SmallTest testLoggableSetWithoutError()63 public void testLoggableSetWithoutError() throws InterruptedException { 64 PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions 65 .builder(InstrumentationRegistry.getTargetContext()) 66 .setInjectableLogger(mockLoggable, Severity.LS_INFO) 67 .setNativeLibraryName(TestConstants.NATIVE_LIBRARY) 68 .createInitializationOptions()); 69 } 70 71 @Test 72 @SmallTest testMessageIsLoggedCorrectly()73 public void testMessageIsLoggedCorrectly() throws InterruptedException { 74 PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions 75 .builder(InstrumentationRegistry.getTargetContext()) 76 .setInjectableLogger(mockLoggable, Severity.LS_INFO) 77 .setNativeLibraryName(TestConstants.NATIVE_LIBRARY) 78 .createInitializationOptions()); 79 String msg = "Message that should be logged"; 80 Logging.d(TAG, msg); 81 assertTrue(mockLoggable.isMessageReceived(msg, Severity.LS_INFO, TAG)); 82 } 83 84 @Test 85 @SmallTest testLowSeverityIsFiltered()86 public void testLowSeverityIsFiltered() throws InterruptedException { 87 // Set severity to LS_WARNING to filter out LS_INFO and below. 88 PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions 89 .builder(InstrumentationRegistry.getTargetContext()) 90 .setInjectableLogger(mockLoggable, Severity.LS_WARNING) 91 .setNativeLibraryName(TestConstants.NATIVE_LIBRARY) 92 .createInitializationOptions()); 93 String msg = "Message that should NOT be logged"; 94 Logging.d(TAG, msg); 95 assertFalse(mockLoggable.isMessageReceived(msg)); 96 } 97 98 @Test 99 @SmallTest testLoggableDoesNotReceiveMessagesAfterUnsetting()100 public void testLoggableDoesNotReceiveMessagesAfterUnsetting() { 101 PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions 102 .builder(InstrumentationRegistry.getTargetContext()) 103 .setInjectableLogger(mockLoggable, Severity.LS_INFO) 104 .setNativeLibraryName(TestConstants.NATIVE_LIBRARY) 105 .createInitializationOptions()); 106 // Reinitialize without Loggable 107 PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions 108 .builder(InstrumentationRegistry.getTargetContext()) 109 .setNativeLibraryName(TestConstants.NATIVE_LIBRARY) 110 .createInitializationOptions()); 111 String msg = "Message that should NOT be logged"; 112 Logging.d(TAG, msg); 113 assertFalse(mockLoggable.isMessageReceived(msg)); 114 } 115 116 @Test 117 @SmallTest testNativeMessageIsLoggedCorrectly()118 public void testNativeMessageIsLoggedCorrectly() throws InterruptedException { 119 PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions 120 .builder(InstrumentationRegistry.getTargetContext()) 121 .setInjectableLogger(mockLoggable, Severity.LS_INFO) 122 .setNativeLibraryName(TestConstants.NATIVE_LIBRARY) 123 .createInitializationOptions()); 124 String msg = "Message that should be logged"; 125 nativeLogInfoTestMessage(msg); 126 assertTrue(mockLoggable.isMessageReceived(msg, Severity.LS_INFO, NATIVE_FILENAME_TAG)); 127 } 128 129 @Test 130 @SmallTest testNativeLowSeverityIsFiltered()131 public void testNativeLowSeverityIsFiltered() throws InterruptedException { 132 PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions 133 .builder(InstrumentationRegistry.getTargetContext()) 134 .setInjectableLogger(mockLoggable, Severity.LS_WARNING) 135 .setNativeLibraryName(TestConstants.NATIVE_LIBRARY) 136 .createInitializationOptions()); 137 String msg = "Message that should NOT be logged"; 138 nativeLogInfoTestMessage(msg); 139 assertFalse(mockLoggable.isMessageReceived(msg)); 140 } 141 142 @Test 143 @SmallTest testNativeLoggableDoesNotReceiveMessagesAfterUnsetting()144 public void testNativeLoggableDoesNotReceiveMessagesAfterUnsetting() { 145 PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions 146 .builder(InstrumentationRegistry.getTargetContext()) 147 .setInjectableLogger(mockLoggable, Severity.LS_INFO) 148 .setNativeLibraryName(TestConstants.NATIVE_LIBRARY) 149 .createInitializationOptions()); 150 // Reinitialize without Loggable 151 PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions 152 .builder(InstrumentationRegistry.getTargetContext()) 153 .setNativeLibraryName(TestConstants.NATIVE_LIBRARY) 154 .createInitializationOptions()); 155 String msg = "Message that should NOT be logged"; 156 nativeLogInfoTestMessage(msg); 157 assertFalse(mockLoggable.isMessageReceived(msg)); 158 } 159 nativeLogInfoTestMessage(String message)160 private static native void nativeLogInfoTestMessage(String message); 161 } 162